1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering
|
---|
5 | * Tomakomai National College of Technology, JAPAN
|
---|
6 | *
|
---|
7 | * ä¸è¨èä½æ¨©è
|
---|
8 | ã¯ï¼ä»¥ä¸ã® (1)ï½(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
9 | * ã«ãã£ã¦å
|
---|
10 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
11 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
12 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
13 | å¸ï¼ä»¥ä¸ï¼
|
---|
14 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
15 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
16 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
17 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
18 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
19 | * ç¨ã§ããå½¢ã§åé
|
---|
20 | å¸ããå ´åã«ã¯ï¼åé
|
---|
21 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
22 | * è
|
---|
23 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
24 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
25 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
26 | * ç¨ã§ããªãå½¢ã§åé
|
---|
27 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®æ¡ä»¶ãæºãããã¨ï¼
|
---|
28 | * (a) åé
|
---|
29 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
30 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
31 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
32 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
33 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
34 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
35 | 責ãããã¨ï¼
|
---|
36 | *
|
---|
37 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
38 | ã
|
---|
39 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
40 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
41 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
42 | *
|
---|
43 | * @(#) $Id: netapp_subr.c 317 2017-08-03 13:14:26Z coas-nagasima $
|
---|
44 | */
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * ãããã¯ã¼ã¯å¿ç¨ããã°ã©ã ãµãã¼ãã«ã¼ãã³
|
---|
48 | */
|
---|
49 |
|
---|
50 | #include <stdarg.h>
|
---|
51 | #include <string.h>
|
---|
52 |
|
---|
53 | #ifdef TARGET_KERNEL_ASP
|
---|
54 |
|
---|
55 | #include <kernel.h>
|
---|
56 | #include <sil.h>
|
---|
57 | #include <syssvc/serial.h>
|
---|
58 | #include <syssvc/logtask.h>
|
---|
59 | #include <t_syslog.h>
|
---|
60 | #include "kernel_cfg.h"
|
---|
61 | #include "tinet_cfg.h"
|
---|
62 |
|
---|
63 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
64 |
|
---|
65 | #ifdef TARGET_KERNEL_JSP
|
---|
66 |
|
---|
67 | #include <s_services.h>
|
---|
68 | #include <t_services.h>
|
---|
69 | #include "kernel_id.h"
|
---|
70 | #include "tinet_id.h"
|
---|
71 |
|
---|
72 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
73 |
|
---|
74 | #include <tinet_defs.h>
|
---|
75 | #include <tinet_config.h>
|
---|
76 |
|
---|
77 | #include <net/net.h>
|
---|
78 | #include <net/net_endian.h>
|
---|
79 |
|
---|
80 | #include <netinet/in.h>
|
---|
81 | #include <netinet/in_itron.h>
|
---|
82 |
|
---|
83 | #include <netapp/netapp.h>
|
---|
84 | #include <netapp/netapp_var.h>
|
---|
85 | #include <netapp/resolver.h>
|
---|
86 |
|
---|
87 | #ifdef USE_NETAPP_SUBR
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * IPv6 㨠IPv4 ã§å¼æ°ãç°ãªãé¢æ°ã®ã³ã³ãã¤ã«
|
---|
91 | */
|
---|
92 |
|
---|
93 | #if defined(SUPPORT_INET6)
|
---|
94 |
|
---|
95 | #ifdef USE_TCP_EXTENTIONS
|
---|
96 |
|
---|
97 | const ID vrid_tcp6_cep[] =
|
---|
98 | {
|
---|
99 | #if NUM_VRID_TCP6_CEPS >= 1
|
---|
100 | TCP6_RSV_CEPID1,
|
---|
101 | #endif
|
---|
102 | #if NUM_VRID_TCP6_CEPS >= 2
|
---|
103 | TCP6_RSV_CEPID2,
|
---|
104 | #endif
|
---|
105 | #if NUM_VRID_TCP6_CEPS >= 3
|
---|
106 | TCP6_RSV_CEPID3,
|
---|
107 | #endif
|
---|
108 | #if NUM_VRID_TCP6_CEPS >= 4
|
---|
109 | TCP6_RSV_CEPID4,
|
---|
110 | #endif
|
---|
111 | };
|
---|
112 |
|
---|
113 | const ID vrid_tcp6_rep[] =
|
---|
114 | {
|
---|
115 | #if NUM_VRID_TCP6_REPS >= 1
|
---|
116 | TCP6_RSV_REPID1,
|
---|
117 | #endif
|
---|
118 | #if NUM_VRID_TCP6_REPS >= 2
|
---|
119 | TCP6_RSV_REPID2,
|
---|
120 | #endif
|
---|
121 | };
|
---|
122 |
|
---|
123 | ID tskid_tcp6_cep[NUM_VRID_TCP6_CEPS];
|
---|
124 | ID tskid_tcp6_rep[NUM_VRID_TCP6_REPS];
|
---|
125 |
|
---|
126 | #endif /* #ifdef USE_TCP_EXTENTIONS */
|
---|
127 |
|
---|
128 | #ifdef USE_UDP_EXTENTIONS
|
---|
129 |
|
---|
130 | const ID vrid_udp6_cep[] =
|
---|
131 | {
|
---|
132 | #if NUM_VRID_UDP6_CEPS >= 1
|
---|
133 | UDP6_RSV_CEPID1,
|
---|
134 | #endif
|
---|
135 | #if NUM_VRID_UDP6_CEPS >= 2
|
---|
136 | UDP6_RSV_CEPID2,
|
---|
137 | #endif
|
---|
138 | };
|
---|
139 |
|
---|
140 | ID tskid_udp6_cep[NUM_VRID_UDP6_CEPS];
|
---|
141 |
|
---|
142 | #endif /* #ifdef USE_UDP_EXTENTIONS */
|
---|
143 |
|
---|
144 | #undef TCP_CRE_REP
|
---|
145 | #undef UDP_CRE_CEP
|
---|
146 | #undef UDP_DEL_CEP
|
---|
147 | #undef TCP_IS_CEPID
|
---|
148 | #undef ALLOC_TCP_REP
|
---|
149 | #undef ALLOC_TCP_CEP
|
---|
150 | #undef ALLOC_UDP_CEP
|
---|
151 | #undef FREE_TCP_REP
|
---|
152 | #undef FREE_TCP_CEP
|
---|
153 | #undef FREE_UDP_CEP
|
---|
154 | #undef T_TCPN_CREP
|
---|
155 | #undef T_UDPN_CCEP
|
---|
156 | #undef NUM_VRID_TCP_CEPS
|
---|
157 | #undef NUM_VRID_TCP_REPS
|
---|
158 | #undef NUM_VRID_UDP_CEPS
|
---|
159 | #undef VRID_TCP_CEP
|
---|
160 | #undef VRID_UDP_CEP
|
---|
161 | #undef VRID_TCP_REP
|
---|
162 | #undef TSKID_TCP_CEP
|
---|
163 | #undef TSKID_TCP_REP
|
---|
164 | #undef TSKID_UDP_CEP
|
---|
165 | #undef API_IPPROTO
|
---|
166 |
|
---|
167 | #define TCP_CRE_REP tcp6_cre_rep
|
---|
168 | #define UDP_CRE_CEP udp6_cre_cep
|
---|
169 | #define UDP_DEL_CEP udp6_del_cep
|
---|
170 |
|
---|
171 | #define TCP_IS_CEPID tcp6_is_cepid
|
---|
172 | #define ALLOC_TCP_REP alloc_tcp6_rep
|
---|
173 | #define ALLOC_TCP_CEP alloc_tcp6_cep
|
---|
174 | #define ALLOC_UDP_CEP alloc_udp6_cep
|
---|
175 | #define FREE_TCP_REP free_tcp6_rep
|
---|
176 | #define FREE_TCP_CEP free_tcp6_cep
|
---|
177 | #define FREE_UDP_CEP free_udp6_cep
|
---|
178 | #define T_TCPN_CREP T_TCP6_CREP
|
---|
179 | #define T_UDPN_CCEP T_UDP6_CCEP
|
---|
180 | #define NUM_VRID_TCP_CEPS NUM_VRID_TCP6_CEPS
|
---|
181 | #define NUM_VRID_TCP_REPS NUM_VRID_TCP6_REPS
|
---|
182 | #define NUM_VRID_UDP_CEPS NUM_VRID_UDP6_CEPS
|
---|
183 | #define VRID_TCP_CEP vrid_tcp6_cep
|
---|
184 | #define VRID_TCP_REP vrid_tcp6_rep
|
---|
185 | #define VRID_UDP_CEP vrid_udp6_cep
|
---|
186 | #define TSKID_TCP_CEP tskid_tcp6_cep
|
---|
187 | #define TSKID_TCP_REP tskid_tcp6_rep
|
---|
188 | #define TSKID_UDP_CEP tskid_udp6_cep
|
---|
189 | #define API_IPPROTO API_PROTO_IPV6
|
---|
190 |
|
---|
191 | #include <netapp/netappn_subr.c>
|
---|
192 |
|
---|
193 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
194 |
|
---|
195 | #if defined(SUPPORT_INET4)
|
---|
196 |
|
---|
197 | #ifdef USE_TCP_EXTENTIONS
|
---|
198 |
|
---|
199 | const ID vrid_tcp4_cep[] =
|
---|
200 | {
|
---|
201 | #if NUM_VRID_TCP4_CEPS >= 1
|
---|
202 | TCP4_RSV_CEPID1,
|
---|
203 | #endif
|
---|
204 | #if NUM_VRID_TCP4_CEPS >= 2
|
---|
205 | TCP4_RSV_CEPID2,
|
---|
206 | #endif
|
---|
207 | #if NUM_VRID_TCP4_CEPS >= 3
|
---|
208 | TCP4_RSV_CEPID3,
|
---|
209 | #endif
|
---|
210 | #if NUM_VRID_TCP4_CEPS >= 4
|
---|
211 | TCP4_RSV_CEPID4,
|
---|
212 | #endif
|
---|
213 | };
|
---|
214 |
|
---|
215 | const ID vrid_tcp4_rep[] =
|
---|
216 | {
|
---|
217 | #if NUM_VRID_TCP4_REPS >= 1
|
---|
218 | TCP4_RSV_REPID1,
|
---|
219 | #endif
|
---|
220 | #if NUM_VRID_TCP4_REPS >= 2
|
---|
221 | TCP4_RSV_REPID2,
|
---|
222 | #endif
|
---|
223 | };
|
---|
224 |
|
---|
225 | ID tskid_tcp4_cep[NUM_VRID_TCP4_CEPS];
|
---|
226 | ID tskid_tcp4_rep[NUM_VRID_TCP4_REPS];
|
---|
227 |
|
---|
228 | #endif /* #ifdef USE_TCP_EXTENTIONS */
|
---|
229 |
|
---|
230 | #ifdef USE_UDP_EXTENTIONS
|
---|
231 |
|
---|
232 | const ID vrid_udp4_cep[] =
|
---|
233 | {
|
---|
234 | #if NUM_VRID_UDP4_CEPS >= 1
|
---|
235 | UDP4_RSV_CEPID1,
|
---|
236 | #endif
|
---|
237 | #if NUM_VRID_UDP4_CEPS >= 2
|
---|
238 | UDP4_RSV_CEPID2,
|
---|
239 | #endif
|
---|
240 | };
|
---|
241 |
|
---|
242 | ID tskid_udp4_cep[NUM_VRID_UDP4_CEPS];
|
---|
243 |
|
---|
244 | #endif /* #ifdef USE_UDP_EXTENTIONS */
|
---|
245 |
|
---|
246 | #undef TCP_CRE_REP
|
---|
247 | #undef UDP_CRE_CEP
|
---|
248 | #undef UDP_DEL_CEP
|
---|
249 | #undef TCP_IS_CEPID
|
---|
250 | #undef ALLOC_TCP_REP
|
---|
251 | #undef ALLOC_TCP_CEP
|
---|
252 | #undef ALLOC_UDP_CEP
|
---|
253 | #undef FREE_TCP_REP
|
---|
254 | #undef FREE_TCP_CEP
|
---|
255 | #undef FREE_UDP_CEP
|
---|
256 | #undef T_TCPN_CREP
|
---|
257 | #undef T_UDPN_CCEP
|
---|
258 | #undef NUM_VRID_TCP_CEPS
|
---|
259 | #undef NUM_VRID_TCP_REPS
|
---|
260 | #undef NUM_VRID_UDP_CEPS
|
---|
261 | #undef VRID_TCP_CEP
|
---|
262 | #undef VRID_TCP_REP
|
---|
263 | #undef VRID_UDP_CEP
|
---|
264 | #undef TSKID_TCP_CEP
|
---|
265 | #undef TSKID_TCP_REP
|
---|
266 | #undef TSKID_UDP_CEP
|
---|
267 | #undef API_IPPROTO
|
---|
268 |
|
---|
269 | #define TCP_CRE_REP tcp_cre_rep
|
---|
270 | #define UDP_CRE_CEP udp_cre_cep
|
---|
271 | #define UDP_DEL_CEP udp_del_cep
|
---|
272 | #define TCP_IS_CEPID tcp4_is_cepid
|
---|
273 | #define ALLOC_TCP_REP alloc_tcp4_rep
|
---|
274 | #define ALLOC_TCP_CEP alloc_tcp4_cep
|
---|
275 | #define ALLOC_UDP_CEP alloc_udp4_cep
|
---|
276 | #define FREE_TCP_REP free_tcp4_rep
|
---|
277 | #define FREE_TCP_CEP free_tcp4_cep
|
---|
278 | #define FREE_UDP_CEP free_udp4_cep
|
---|
279 | #define T_TCPN_CREP T_TCP_CREP
|
---|
280 | #define T_UDPN_CCEP T_UDP_CCEP
|
---|
281 | #define NUM_VRID_TCP_CEPS NUM_VRID_TCP4_CEPS
|
---|
282 | #define NUM_VRID_TCP_REPS NUM_VRID_TCP4_REPS
|
---|
283 | #define NUM_VRID_UDP_CEPS NUM_VRID_UDP4_CEPS
|
---|
284 | #define VRID_TCP_CEP vrid_tcp4_cep
|
---|
285 | #define VRID_TCP_REP vrid_tcp4_rep
|
---|
286 | #define VRID_UDP_CEP vrid_udp4_cep
|
---|
287 | #define TSKID_TCP_CEP tskid_tcp4_cep
|
---|
288 | #define TSKID_TCP_REP tskid_tcp4_rep
|
---|
289 | #define TSKID_UDP_CEP tskid_udp4_cep
|
---|
290 | #define API_IPPROTO API_PROTO_IPV4
|
---|
291 |
|
---|
292 | #include <netapp/netappn_subr.c>
|
---|
293 |
|
---|
294 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
295 |
|
---|
296 | #undef TCP_CRE_REP
|
---|
297 | #undef ALLOC_TCPN_REP
|
---|
298 | #undef ALLOC_TCPN_CEP
|
---|
299 | #undef ALLOC_UDPN_CEP
|
---|
300 | #undef FREE_TCPN_REP
|
---|
301 | #undef FREE_TCPN_CEP
|
---|
302 | #undef FREE_UDPN_CEP
|
---|
303 | #undef T_TCPN_CREP
|
---|
304 | #undef NUM_VRID_TCP_REPS
|
---|
305 | #undef NUM_VRID_UDP_CEPS
|
---|
306 | #undef VRID_TCP_CEP
|
---|
307 | #undef VRID_TCP_REP
|
---|
308 | #undef VRID_UDP_CEP
|
---|
309 | #undef TSKID_TCP_CEP
|
---|
310 | #undef TSKID_TCP_REP
|
---|
311 | #undef TSKID_UDP_CEP
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * å¤æ°
|
---|
315 | */
|
---|
316 |
|
---|
317 | static uint32_t rand_next = ULONG_C(1);
|
---|
318 |
|
---|
319 | #if !defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
|
---|
320 |
|
---|
321 | const T_IN6_ADDR in6_addr_unspecified =
|
---|
322 | IPV6_ADDR_UNSPECIFIED_INIT;
|
---|
323 |
|
---|
324 | #endif /* of #if !defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * ã·ãªã¢ã«ãã¼ãã¸ã®æ¸å¼ä»æåååºåã©ã¤ãã©ãª
|
---|
328 | */
|
---|
329 |
|
---|
330 | #define EOF (-1)
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * æ°å¤å¤æã®ããã®å¤æ表
|
---|
334 | */
|
---|
335 |
|
---|
336 | const char radhex[] = "0123456789abcdef";
|
---|
337 | const char radHEX[] = "0123456789ABCDEF";
|
---|
338 |
|
---|
339 | /*
|
---|
340 | * netapp_rand -- ä¹±æ°ãè¿ãã
|
---|
341 | */
|
---|
342 |
|
---|
343 | uint32_t
|
---|
344 | netapp_rand (void)
|
---|
345 | {
|
---|
346 | rand_next = (rand_next * 99991 + 12345) & ULONG_C(0x7fffffff);
|
---|
347 | return rand_next;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * srand -- ä¹±æ°ãåæåããã
|
---|
352 | */
|
---|
353 |
|
---|
354 | void
|
---|
355 | netapp_srand (uint32_t seed)
|
---|
356 | {
|
---|
357 | SYSTIM now;
|
---|
358 |
|
---|
359 | syscall(get_tim(&now));
|
---|
360 | rand_next += now + seed;
|
---|
361 | }
|
---|
362 |
|
---|
363 | #ifndef USE_NET_CONS
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * cons_putchar -- ã·ãªã¢ã«ãã¼ãã¸ã®æååºå
|
---|
367 | */
|
---|
368 |
|
---|
369 | void
|
---|
370 | cons_putchar (ID portid, char ch)
|
---|
371 | {
|
---|
372 | serial_wri_dat(portid, &ch, 1);
|
---|
373 | }
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * cons_getchar -- ã·ãªã¢ã«ãã¼ãããã®æåå
|
---|
377 | ¥å
|
---|
378 | */
|
---|
379 |
|
---|
380 | int_t
|
---|
381 | cons_getchar (ID portid)
|
---|
382 | {
|
---|
383 | char ch;
|
---|
384 |
|
---|
385 | if (serial_rea_dat(portid, &ch, sizeof(ch)) > 0)
|
---|
386 | return ch;
|
---|
387 | else
|
---|
388 | return EOF;
|
---|
389 | }
|
---|
390 |
|
---|
391 | #endif /* of #ifndef USE_NET_CONS */
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * cons_putnumber -- cons_printf ã®æ°å¤å¤æ
|
---|
395 | */
|
---|
396 |
|
---|
397 | int_t
|
---|
398 | cons_putnumber(ID portid, ulong_t val, int_t radix,
|
---|
399 | const char *radchar, int_t width, bool_t minus, char padchar)
|
---|
400 | {
|
---|
401 | char digits[24];
|
---|
402 | int_t ix, pad, pchars;
|
---|
403 | bool_t left;
|
---|
404 |
|
---|
405 | if (width < 0) {
|
---|
406 | width = -width;
|
---|
407 | left = true;
|
---|
408 | }
|
---|
409 | else
|
---|
410 | left = false;
|
---|
411 |
|
---|
412 | ix = 0;
|
---|
413 | do {
|
---|
414 | digits[ix ++] = radchar[val % radix];
|
---|
415 | val /= radix;
|
---|
416 | } while (val != 0);
|
---|
417 |
|
---|
418 | if (minus)
|
---|
419 | digits[ix ++] = '-';
|
---|
420 |
|
---|
421 | if (width > ix)
|
---|
422 | pchars = width;
|
---|
423 | else
|
---|
424 | pchars = ix;
|
---|
425 |
|
---|
426 | pad = ix;
|
---|
427 | if (!left) /* å³è©°ã */
|
---|
428 | for ( ; pad < width; pad ++)
|
---|
429 | cons_putchar(portid, padchar);
|
---|
430 |
|
---|
431 | while (ix -- > 0)
|
---|
432 | cons_putchar(portid, digits[ix]);
|
---|
433 |
|
---|
434 | if (left) /* 左詰ã */
|
---|
435 | for ( ; pad < width; pad ++)
|
---|
436 | cons_putchar(portid, padchar);
|
---|
437 |
|
---|
438 | return pchars;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * put_ipv4addr -- IPv4 ã¢ãã¬ã¹åºå
|
---|
443 | */
|
---|
444 |
|
---|
445 | int_t
|
---|
446 | put_ipv4addr (ID portid, T_IN4_ADDR *addr, int_t width)
|
---|
447 | {
|
---|
448 | int_t len = 3; /* 3 㯠'.' ã®æåæ° */
|
---|
449 |
|
---|
450 | len += cons_putnumber(portid, (*addr >> 24) & 0xff, 10, radhex, 0, false, ' ');
|
---|
451 | cons_putchar(portid, '.');
|
---|
452 | len += cons_putnumber(portid, (*addr >> 16) & 0xff, 10, radhex, 0, false, ' ');
|
---|
453 | cons_putchar(portid, '.');
|
---|
454 | len += cons_putnumber(portid, (*addr >> 8) & 0xff, 10, radhex, 0, false, ' ');
|
---|
455 | cons_putchar(portid, '.');
|
---|
456 | len += cons_putnumber(portid, *addr & 0xff, 10, radhex, 0, false, ' ');
|
---|
457 |
|
---|
458 | for ( ; len < width; len ++)
|
---|
459 | cons_putchar(portid, ' ');
|
---|
460 |
|
---|
461 | return len;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /*
|
---|
465 | * ipv6addr -- IPv6 ã¢ãã¬ã¹åºå
|
---|
466 | */
|
---|
467 |
|
---|
468 | int_t
|
---|
469 | put_ipv6addr (ID portid, const T_IN6_ADDR *addr, int_t width)
|
---|
470 | {
|
---|
471 | int_t len = 0, ix, len6;
|
---|
472 | bool_t omit = false, zero = false;
|
---|
473 |
|
---|
474 | if (addr == NULL || IN6_IS_ADDR_UNSPECIFIED(addr)) {
|
---|
475 | cons_putchar(portid, '0');
|
---|
476 | cons_putchar(portid, ':');
|
---|
477 | cons_putchar(portid, ':');
|
---|
478 | cons_putchar(portid, '0');
|
---|
479 | len = 4;
|
---|
480 | }
|
---|
481 | else {
|
---|
482 | if (in6_is_addr_ipv4mapped(addr))
|
---|
483 | len6 = sizeof(T_IN6_ADDR) / 2 - 2;
|
---|
484 | else
|
---|
485 | len6 = sizeof(T_IN6_ADDR) / 2;
|
---|
486 | for (ix = 0; ix < len6; ix ++) {
|
---|
487 | if (omit) {
|
---|
488 | len += cons_putnumber(portid, ntohs(addr->s6_addr16[ix]), 16, radhex, 0, false, ' ');
|
---|
489 | if (ix < 7) {
|
---|
490 | cons_putchar(portid, ':');
|
---|
491 | len ++;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | else if (ix > 0 && ix < 7 && addr->s6_addr16[ix] == 0)
|
---|
495 | zero = true;
|
---|
496 | else {
|
---|
497 | if (zero) {
|
---|
498 | omit = true;
|
---|
499 | cons_putchar(portid, ':');
|
---|
500 | len ++;
|
---|
501 | }
|
---|
502 | len += cons_putnumber(portid, ntohs(addr->s6_addr16[ix]), 16, radhex, 0, false, ' ');
|
---|
503 | if (ix < 7) {
|
---|
504 | cons_putchar(portid, ':');
|
---|
505 | len ++;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | }
|
---|
509 |
|
---|
510 | if (len6 == sizeof(T_IN6_ADDR) / 2 - 2) {
|
---|
511 | T_IN4_ADDR ip4addr;
|
---|
512 |
|
---|
513 | ip4addr = ntohl(addr->s6_addr32[3]);
|
---|
514 | len += put_ipv4addr(portid, &ip4addr, 0);
|
---|
515 | }
|
---|
516 |
|
---|
517 | for ( ; len < width; len ++)
|
---|
518 | cons_putchar(portid, ' ');
|
---|
519 | }
|
---|
520 | return len;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * put_macaddr -- MAC ã¢ãã¬ã¹åºå
|
---|
525 | */
|
---|
526 |
|
---|
527 | int_t
|
---|
528 | put_macaddr (ID portid, uint8_t *mac, int_t width)
|
---|
529 | {
|
---|
530 | int_t oct, len;
|
---|
531 |
|
---|
532 | for (oct = 5; oct -- > 0; ) {
|
---|
533 | cons_putnumber(portid, *mac ++, 16, radhex, 2, false, '0');
|
---|
534 | cons_putchar(portid, ':');
|
---|
535 | }
|
---|
536 | cons_putnumber(portid, *mac, 16, radhex, 2, false, '0');
|
---|
537 |
|
---|
538 | for (len = 17; len < width; len ++)
|
---|
539 | cons_putchar(portid, ' ');
|
---|
540 |
|
---|
541 | return len;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /*
|
---|
545 | * cons_printf -- ã·ãªã¢ã«ãã¼ãã¸ã®æ¸å¼ä»æåååºå
|
---|
546 | */
|
---|
547 |
|
---|
548 | void
|
---|
549 | cons_printf (ID portid, const char *fmt, ...)
|
---|
550 | {
|
---|
551 | va_list ap;
|
---|
552 | long_t val;
|
---|
553 | char padchar, *str;
|
---|
554 | int_t ch, width, longflag, shortflag, left;
|
---|
555 | T_IN4_ADDR *addr;
|
---|
556 |
|
---|
557 | va_start(ap, fmt);
|
---|
558 | while ((ch = *fmt ++) != '\0') {
|
---|
559 | if (ch != '%') { /* æ¸å¼æå®ä»¥å¤ */
|
---|
560 | cons_putchar(portid, (char)ch);
|
---|
561 | continue;
|
---|
562 | }
|
---|
563 |
|
---|
564 | width = longflag = shortflag = 0;
|
---|
565 | padchar = ' ';
|
---|
566 |
|
---|
567 | if (*fmt == '-') { /* 左詰ã */
|
---|
568 | fmt ++;
|
---|
569 | left = -1;
|
---|
570 | }
|
---|
571 | else
|
---|
572 | left = 1;
|
---|
573 |
|
---|
574 | if ((ch = *fmt ++) == '0') { /* ä¸ä½æ¡ã® 0 */
|
---|
575 | padchar = '0';
|
---|
576 | ch = *fmt ++;
|
---|
577 | }
|
---|
578 |
|
---|
579 | while ('0' <= ch && ch <= '9') { /* åºåå¹
|
---|
580 | */
|
---|
581 | width = width * 10 + ch - '0';
|
---|
582 | ch = *fmt ++;
|
---|
583 | }
|
---|
584 |
|
---|
585 | while (ch == 'l') { /* long (long) ã®æå® */
|
---|
586 | longflag ++;
|
---|
587 | ch = *fmt ++;
|
---|
588 | }
|
---|
589 |
|
---|
590 | while (ch == 'h') { /* short ã®æå® */
|
---|
591 | shortflag ++;
|
---|
592 | ch = *fmt ++;
|
---|
593 | }
|
---|
594 |
|
---|
595 | switch (ch) {
|
---|
596 | case 'd':
|
---|
597 | val = longflag ? (ulong_t)va_arg(ap, long_t)
|
---|
598 | : (ulong_t)va_arg(ap, int_t);
|
---|
599 | if (val >= 0)
|
---|
600 | cons_putnumber(portid, val, 10, radhex, width * left, false, padchar);
|
---|
601 | else
|
---|
602 | cons_putnumber(portid, -val, 10, radhex, width * left, true, padchar);
|
---|
603 | break;
|
---|
604 |
|
---|
605 | case 'u':
|
---|
606 | val = longflag ? (ulong_t)va_arg(ap, ulong_t)
|
---|
607 | : (ulong_t)va_arg(ap, uint_t);
|
---|
608 | cons_putnumber(portid, val, 10, radhex, width * left, false, padchar);
|
---|
609 | break;
|
---|
610 |
|
---|
611 | case 'x':
|
---|
612 | val = longflag ? (ulong_t)va_arg(ap, ulong_t)
|
---|
613 | : (ulong_t)va_arg(ap, uint_t);
|
---|
614 | cons_putnumber(portid, val, 16, radhex, width * left, false, padchar);
|
---|
615 | break;
|
---|
616 |
|
---|
617 | case 'X':
|
---|
618 | val = longflag ? (ulong_t)va_arg(ap, ulong_t)
|
---|
619 | : (ulong_t)va_arg(ap, uint_t);
|
---|
620 | cons_putnumber(portid, val, 16, radHEX, width * left, false, padchar);
|
---|
621 | break;
|
---|
622 |
|
---|
623 | case 'c':
|
---|
624 | ch = va_arg(ap, int_t);
|
---|
625 | cons_putchar(portid, (char)ch);
|
---|
626 | break;
|
---|
627 |
|
---|
628 | case 's':
|
---|
629 | str = va_arg(ap, char*);
|
---|
630 | while ((ch = *str ++) != '\0') {
|
---|
631 | cons_putchar(portid, (char)ch);
|
---|
632 | width --;
|
---|
633 | }
|
---|
634 | while (width -- > 0)
|
---|
635 | cons_putchar(portid, ' ');
|
---|
636 | break;
|
---|
637 |
|
---|
638 | case 'I':
|
---|
639 |
|
---|
640 | if (longflag) {
|
---|
641 | str = va_arg(ap, char*);
|
---|
642 | put_ipv6addr(portid, (T_IN6_ADDR *)str, width);
|
---|
643 | }
|
---|
644 | else if (shortflag) {
|
---|
645 | addr = va_arg(ap, T_IN4_ADDR *);
|
---|
646 | put_ipv4addr(portid, addr, width);
|
---|
647 | }
|
---|
648 | else {
|
---|
649 |
|
---|
650 | #if defined(SUPPORT_INET6)
|
---|
651 |
|
---|
652 | str = va_arg(ap, char*);
|
---|
653 | put_ipv6addr(portid, (T_IN6_ADDR *)str, width);
|
---|
654 |
|
---|
655 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
656 |
|
---|
657 | #if defined(SUPPORT_INET4)
|
---|
658 |
|
---|
659 | addr = va_arg(ap, T_IN4_ADDR *);
|
---|
660 | put_ipv4addr(portid, addr, width);
|
---|
661 |
|
---|
662 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
663 |
|
---|
664 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
665 |
|
---|
666 | }
|
---|
667 | break;
|
---|
668 |
|
---|
669 | case 'M':
|
---|
670 | str = va_arg(ap, char*);
|
---|
671 | put_macaddr(portid, (uint8_t *)str, width);
|
---|
672 | break;
|
---|
673 |
|
---|
674 | case '%':
|
---|
675 | cons_putchar(portid, '%');
|
---|
676 | break;
|
---|
677 |
|
---|
678 | case '0':
|
---|
679 | fmt --;
|
---|
680 | break;
|
---|
681 |
|
---|
682 | default:
|
---|
683 | break;
|
---|
684 | }
|
---|
685 |
|
---|
686 | }
|
---|
687 | va_end(ap);
|
---|
688 | }
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * host2msg16 -- 16ãããã®å¤ãããã¤ããªã¼ãã¼ã調æ´ãã¦ã¡ãã»ã¼ã¸ã«æ¸ãè¾¼ãã
|
---|
692 | */
|
---|
693 |
|
---|
694 | void
|
---|
695 | host2msg16 (void *msg, uint16_t host_data)
|
---|
696 | {
|
---|
697 | uint16_t msg_data;
|
---|
698 |
|
---|
699 | msg_data = htons(host_data);
|
---|
700 | memcpy(msg, &msg_data, sizeof(msg_data));
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * host2msg32 -- 32ãããã®å¤ãããã¤ããªã¼ãã¼ã調æ´ãã¦ã¡ãã»ã¼ã¸ã«æ¸ãè¾¼ãã
|
---|
705 | */
|
---|
706 |
|
---|
707 | void
|
---|
708 | host2msg32 (void *msg, uint32_t host_data)
|
---|
709 | {
|
---|
710 | uint32_t msg_data;
|
---|
711 |
|
---|
712 | msg_data = htonl(host_data);
|
---|
713 | memcpy(msg, &msg_data, sizeof(msg_data));
|
---|
714 | }
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * msg2host16 -- 16ãããã®å¤ãããã¤ããªã¼ãã¼ã調æ´ãã¦ã¡ãã»ã¼ã¸ããèªã¿åºãã
|
---|
718 | */
|
---|
719 |
|
---|
720 | uint16_t
|
---|
721 | msg2host16 (void *msg)
|
---|
722 | {
|
---|
723 | uint16_t host_data;
|
---|
724 |
|
---|
725 | memcpy(&host_data, msg, sizeof(host_data));
|
---|
726 | return htons(host_data);
|
---|
727 | }
|
---|
728 |
|
---|
729 | /*
|
---|
730 | * msg2host32 -- 32ãããã®å¤ãããã¤ããªã¼ãã¼ã調æ´ãã¦ã¡ãã»ã¼ã¸ããèªã¿åºãã
|
---|
731 | */
|
---|
732 |
|
---|
733 | uint32_t
|
---|
734 | msg2host32 (void *msg)
|
---|
735 | {
|
---|
736 | uint32_t host_data;
|
---|
737 |
|
---|
738 | memcpy(&host_data, msg, sizeof(host_data));
|
---|
739 | return htonl(host_data);
|
---|
740 | }
|
---|
741 |
|
---|
742 | /*
|
---|
743 | * skip_blanks -- 空ç½ã¨ TAB ãã¹ãããããã
|
---|
744 | */
|
---|
745 |
|
---|
746 | char *
|
---|
747 | skip_blanks (char *line)
|
---|
748 | {
|
---|
749 | while (*line == ' ' || *line == '\t')
|
---|
750 | line ++;
|
---|
751 | return line;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * get_ipv4addr -- æååã® IPv4 ã¢ãã¬ã¹ã T_IN4_ADDR å¤ã«å¤æããã
|
---|
756 | */
|
---|
757 |
|
---|
758 | char *
|
---|
759 | get_ipv4addr (T_IN4_ADDR *addr, char *line)
|
---|
760 | {
|
---|
761 | int_t oct;
|
---|
762 |
|
---|
763 | *addr = IPV4_ADDRANY;
|
---|
764 | while ('0' <= *line && *line <= '9') {
|
---|
765 | oct = 0;
|
---|
766 | while ('0' <= *line && *line <= '9')
|
---|
767 | oct = oct * 10 + (*line ++) - '0';
|
---|
768 | *addr = (*addr << 8) | (oct & 0xff);
|
---|
769 | if (*line == '.')
|
---|
770 | line ++;
|
---|
771 | }
|
---|
772 | #if 1
|
---|
773 | return *addr == IPV4_ADDRANY ? NULL : line;
|
---|
774 | #else
|
---|
775 | return line;
|
---|
776 | #endif
|
---|
777 | }
|
---|
778 |
|
---|
779 | #if defined(SUPPORT_INET6) || defined(USE_RESOLVER)
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * get_ipv6addr -- æååã® IPv6 ã¢ãã¬ã¹ã T_IN6_ADDR å¤ã«å¤æããã
|
---|
783 | */
|
---|
784 |
|
---|
785 | char *
|
---|
786 | get_ipv6addr (T_IN6_ADDR *addr, char *line)
|
---|
787 | {
|
---|
788 | int_t word, ix = 0, omit = 0, six;
|
---|
789 | char *ipv4line;
|
---|
790 |
|
---|
791 | memset(addr, 0, sizeof(T_IN6_ADDR));
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * IPv4 ã¢ãã¬ã¹ï¼æ°å.ï¼ãæ¤åºããã IPv4 ã¢ãã¬ã¹ã¨ãã¦å
|
---|
795 | ¥åãã
|
---|
796 | * IPv4 å°å½±ã¢ãã¬ã¹ã«å¤æããã
|
---|
797 | */
|
---|
798 | for (ipv4line = line; '0' <= *ipv4line && *ipv4line <= '9'; ipv4line ++)
|
---|
799 | ;
|
---|
800 | if (*ipv4line == '.') {
|
---|
801 | T_IN4_ADDR ipv4addr;
|
---|
802 |
|
---|
803 | line = get_ipv4addr(&ipv4addr, line);
|
---|
804 | in6_make_ipv4mapped (addr, ipv4addr);
|
---|
805 | }
|
---|
806 | else {
|
---|
807 | while (ix < 8 &&
|
---|
808 | (('0' <= *line && *line <= '9') ||
|
---|
809 | ('a' <= *line && *line <= 'f') ||
|
---|
810 | ('A' <= *line && *line <= 'F'))) {
|
---|
811 | word = 0;
|
---|
812 | while (('0' <= *line && *line <= '9') ||
|
---|
813 | ('a' <= *line && *line <= 'f') ||
|
---|
814 | ('A' <= *line && *line <= 'F')) {
|
---|
815 | if ('0' <= *line && *line <= '9')
|
---|
816 | word = (word << 4) + (*line ++) - '0';
|
---|
817 | else if ('a' <= *line && *line <= 'f')
|
---|
818 | word = (word << 4) + (*line ++) - 'a' + 10;
|
---|
819 | else if ('A' <= *line && *line <= 'F')
|
---|
820 | word = (word << 4) + (*line ++) - 'A' + 10;
|
---|
821 | }
|
---|
822 | addr->s6_addr16[ix ++] = htons(word);
|
---|
823 | if (*line == ':') {
|
---|
824 | line ++;
|
---|
825 | if (*line == ':') {
|
---|
826 | omit = ix;
|
---|
827 | line ++;
|
---|
828 | }
|
---|
829 | }
|
---|
830 | }
|
---|
831 | if (omit > 0) {
|
---|
832 | six = 7;
|
---|
833 | while (ix > omit) {
|
---|
834 | addr->s6_addr16[six --] = addr->s6_addr16[-- ix];
|
---|
835 | addr->s6_addr16[ ix ] = 0;
|
---|
836 | }
|
---|
837 | }
|
---|
838 | }
|
---|
839 | return line;
|
---|
840 | }
|
---|
841 |
|
---|
842 | #endif /* of #if defined(SUPPORT_INET6) || defined(USE_RESOLVER) */
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * ãããã°è¡å
|
---|
846 | ¥å
|
---|
847 | */
|
---|
848 |
|
---|
849 | int_t
|
---|
850 | cons_getline (ID portid, char *line, int_t size)
|
---|
851 | {
|
---|
852 | int_t ch, len;
|
---|
853 |
|
---|
854 | len = size;
|
---|
855 | while (size > 0) {
|
---|
856 | if ((ch = cons_getchar(portid)) != EOF) {
|
---|
857 | if (ch == '\r') {
|
---|
858 | cons_putchar(portid, '\n');
|
---|
859 | break;
|
---|
860 | }
|
---|
861 | else if (ch == '\b') {
|
---|
862 | if (size < len) {
|
---|
863 | line --;
|
---|
864 | size ++;
|
---|
865 | }
|
---|
866 | }
|
---|
867 | else {
|
---|
868 | *line ++ = ch;
|
---|
869 | size --;
|
---|
870 | }
|
---|
871 | }
|
---|
872 | }
|
---|
873 | *line = '\0';
|
---|
874 | return len - size;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /*
|
---|
878 | * get_int -- æ´æ°å¤æ
|
---|
879 | */
|
---|
880 |
|
---|
881 | char *
|
---|
882 | get_int (int_t *val, char *line)
|
---|
883 | {
|
---|
884 | int_t sign = 1;
|
---|
885 |
|
---|
886 | line = skip_blanks(line);
|
---|
887 | if (*line == '-') {
|
---|
888 | sign = -1;
|
---|
889 | line ++;
|
---|
890 | }
|
---|
891 | else if (*line == '+')
|
---|
892 | line ++;
|
---|
893 |
|
---|
894 | line = skip_blanks(line);
|
---|
895 | *val = 0;
|
---|
896 | while ('0' <= *line && *line <= '9') {
|
---|
897 | *val = *val * 10 + *line - '0';
|
---|
898 | line ++;
|
---|
899 | }
|
---|
900 |
|
---|
901 | *val = *val * sign;
|
---|
902 | return line;
|
---|
903 | }
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * get_xuint -- 符å·ãªãæ´æ° (16é²) å¤æ
|
---|
907 | */
|
---|
908 |
|
---|
909 | char *
|
---|
910 | get_xuint (uint_t *val, char *line)
|
---|
911 | {
|
---|
912 | line = skip_blanks(line);
|
---|
913 | *val = 0;
|
---|
914 | while (1) {
|
---|
915 | if ('0' <= *line && *line <= '9')
|
---|
916 | *val = (*val << 4) + *line - '0';
|
---|
917 | else if ('a' <= *line && *line <= 'f')
|
---|
918 | *val = (*val << 4) + *line - 'a' + 10;
|
---|
919 | else if ('A' <= *line && *line <= 'F')
|
---|
920 | *val = (*val << 4) + *line - 'A' + 10;
|
---|
921 | else
|
---|
922 | break;
|
---|
923 | line ++;
|
---|
924 | }
|
---|
925 | return line;
|
---|
926 | }
|
---|
927 |
|
---|
928 | #ifdef USE_RESOLVER
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * dns_strtype -- DNS ã® type ã®æååãè¿ãã
|
---|
932 | */
|
---|
933 |
|
---|
934 | const char *
|
---|
935 | dns_strtype (uint_t type)
|
---|
936 | {
|
---|
937 | switch (type) {
|
---|
938 | case DNS_TYPE_A:
|
---|
939 | return "A";
|
---|
940 | break;
|
---|
941 | case DNS_TYPE_NS:
|
---|
942 | return "NS";
|
---|
943 | break;
|
---|
944 | case DNS_TYPE_CNAME:
|
---|
945 | return "CNAME";
|
---|
946 | break;
|
---|
947 | case DNS_TYPE_SOA:
|
---|
948 | return "SOA";
|
---|
949 | break;
|
---|
950 | case DNS_TYPE_PTR:
|
---|
951 | return "PTR";
|
---|
952 | break;
|
---|
953 | case DNS_TYPE_AAAA:
|
---|
954 | return "AAAA";
|
---|
955 | break;
|
---|
956 | default:
|
---|
957 | return "unknown type";
|
---|
958 | break;
|
---|
959 | }
|
---|
960 | }
|
---|
961 |
|
---|
962 | /*
|
---|
963 | * dns_strclass -- DNS ã® class ã表示ããã
|
---|
964 | */
|
---|
965 |
|
---|
966 | const char *
|
---|
967 | dns_strclass (uint_t class)
|
---|
968 | {
|
---|
969 | switch (class) {
|
---|
970 | case DNS_CLASS_IN:
|
---|
971 | return "IN";
|
---|
972 | break;
|
---|
973 | default:
|
---|
974 | return "unknown class";
|
---|
975 | break;
|
---|
976 | }
|
---|
977 | }
|
---|
978 |
|
---|
979 | /*
|
---|
980 | * resolv_hoststr -- æååã®FQDNã»ãã¹ãåã»IPv6/IPv4 ã¢ãã¬ã¹ã解æããã
|
---|
981 | */
|
---|
982 |
|
---|
983 | #define IS_HOSTCH_DIGIT(c) (('0'<=(c)&&(c)<='9'))
|
---|
984 | #define IS_HOSTCH_XDIGIT(c) (IS_HOSTCH_DIGIT(c)||('a'<=(c)&&(c)<='f')||('A'<=(c)&&(c)<='F'))
|
---|
985 | #define IS_HOSTCH_ALPHA(c) (('a'<=(c)&&(c)<='z')||('A'<=(c)&&(c)<='z'))
|
---|
986 | #define IS_HOSTCH_ALNUM(c) (IS_HOSTCH_ALPHA(c)||IS_HOSTCH_DIGIT(c))
|
---|
987 | #define IS_HOSTCH_IPV6ADDR(c) (IS_HOSTCH_XDIGIT(c)||(c)==':')
|
---|
988 | #define IS_HOSTCH_IPV4ADDR(c) (IS_HOSTCH_DIGIT(c)||(c)=='.')
|
---|
989 | #define IS_HOSTCH_IPADDR(c) (IS_HOSTCH_XDIGIT(c)||(c)==':'||(c)=='.')
|
---|
990 | #define IS_HOSTCH_NAME_FIRST(c) (IS_HOSTCH_ALPHA(c))
|
---|
991 | #define IS_HOSTCH_NAME(c) (IS_HOSTCH_ALNUM(c)||(c)=='-'||(c)=='.')
|
---|
992 | #define IS_HOSTCH_BLANK(c) ((c)=='\0'||(c)==' '||(c)=='\t')
|
---|
993 |
|
---|
994 | char *
|
---|
995 | resolv_hoststr (uint_t *flags, char *hostname, uint_t name_size, char *line)
|
---|
996 | {
|
---|
997 | char *h, *p;
|
---|
998 |
|
---|
999 | h = hostname;
|
---|
1000 | p = line = skip_blanks(line);
|
---|
1001 |
|
---|
1002 | if (IS_HOSTCH_IPADDR(*p)) {
|
---|
1003 | while (IS_HOSTCH_DIGIT(*p)) {
|
---|
1004 | if (h - hostname > name_size)
|
---|
1005 | return NULL;
|
---|
1006 | *h ++ = *p ++;
|
---|
1007 | }
|
---|
1008 | if (p > line && *p == '.') {
|
---|
1009 | while (IS_HOSTCH_IPV4ADDR(*p)) {
|
---|
1010 | if (h - hostname > name_size)
|
---|
1011 | return NULL;
|
---|
1012 | *h ++ = *p ++;
|
---|
1013 | }
|
---|
1014 | if (IS_HOSTCH_BLANK(*p)) {
|
---|
1015 | *flags |= HOSTSTR_IPV4;
|
---|
1016 | *h = '\0';
|
---|
1017 | return p;
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 | else if (IS_HOSTCH_IPADDR(*p)) {
|
---|
1021 | while (IS_HOSTCH_IPADDR(*p)) {
|
---|
1022 | if (h - hostname > name_size)
|
---|
1023 | return NULL;
|
---|
1024 | *h ++ = *p ++;
|
---|
1025 | }
|
---|
1026 | if (IS_HOSTCH_BLANK(*p)) {
|
---|
1027 | *flags |= HOSTSTR_IPV6;
|
---|
1028 | *h = '\0';
|
---|
1029 | return p;
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 | h = hostname;
|
---|
1033 | p = line;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | if (IS_HOSTCH_NAME_FIRST(*p)) {
|
---|
1037 | *flags |= HOSTSTR_HOSTNAME;
|
---|
1038 | while (IS_HOSTCH_NAME(*p)) {
|
---|
1039 | if (h - hostname > name_size)
|
---|
1040 | return NULL;
|
---|
1041 | if (*p == '.')
|
---|
1042 | *flags |= HOSTSTR_FQDN;
|
---|
1043 | *h ++ = *p ++;
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 | else
|
---|
1047 | *flags |= HOSTSTR_OTHER;
|
---|
1048 |
|
---|
1049 | *h = '\0';
|
---|
1050 | return p;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | /*
|
---|
1054 | * show_dns_domain_name -- DNS ã®ãã¡ã¤ã³åã表示ããã
|
---|
1055 | */
|
---|
1056 |
|
---|
1057 | uint_t
|
---|
1058 | show_dns_domain_name (ID portid, uint8_t *hdr, uint_t offset)
|
---|
1059 | {
|
---|
1060 | uint8_t *ptr;
|
---|
1061 | uint_t c;
|
---|
1062 |
|
---|
1063 | ptr = hdr + offset;
|
---|
1064 | while (*ptr) {
|
---|
1065 | if ((*ptr & DNS_MSG_COMP_MARK) == DNS_MSG_COMP_MARK) {
|
---|
1066 | show_dns_domain_name(portid, hdr, (*ptr & ~DNS_MSG_COMP_MARK) << 8 | *(ptr + 1));
|
---|
1067 | ptr += 2;
|
---|
1068 | break;
|
---|
1069 | }
|
---|
1070 | else {
|
---|
1071 | for (c = 1; c <= *ptr; c++)
|
---|
1072 | cons_printf(portid, "%c", *(ptr + c));
|
---|
1073 | ptr += *ptr + 1;
|
---|
1074 | if (*ptr)
|
---|
1075 | cons_printf(portid, ".");
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 | return ptr - hdr;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /*
|
---|
1082 | * resolv_options -- ã¢ãã¬ã¹è§£æ±ºã®ãªãã·ã§ã³è§£æ
|
---|
1083 | */
|
---|
1084 |
|
---|
1085 | char *
|
---|
1086 | resolv_options (uint_t *flags, char *line, char apip)
|
---|
1087 | {
|
---|
1088 | #if defined(SUPPORT_INET6)
|
---|
1089 | T_IN6_ADDR in6_addr;
|
---|
1090 | #endif
|
---|
1091 |
|
---|
1092 | #if defined(SUPPORT_INET4)
|
---|
1093 | T_IN4_ADDR in4_addr;
|
---|
1094 | #endif
|
---|
1095 |
|
---|
1096 | line = skip_blanks(line);
|
---|
1097 |
|
---|
1098 | #if defined(SUPPORT_INET6)
|
---|
1099 |
|
---|
1100 | #if defined(SUPPORT_INET4)
|
---|
1101 |
|
---|
1102 | *flags |= DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4;
|
---|
1103 | if (apip == API_PROTO_IPV4)
|
---|
1104 | *flags |= DNS_LUP_FLAGS_QTYPE_A;
|
---|
1105 | else
|
---|
1106 | *flags |= DNS_LUP_FLAGS_QTYPE_AAAA;
|
---|
1107 |
|
---|
1108 | if (*line == '-') {
|
---|
1109 | line ++;
|
---|
1110 | if (*line == '6') {
|
---|
1111 | line ++;
|
---|
1112 | *flags &= ~DNS_LUP_FLAGS_PROTO_IPV4;
|
---|
1113 | }
|
---|
1114 | else if (*line == '4') {
|
---|
1115 | line ++;
|
---|
1116 | *flags &= ~DNS_LUP_FLAGS_PROTO_IPV6;
|
---|
1117 | }
|
---|
1118 | if (*line == 'Q' || *line == 'q') {
|
---|
1119 | *flags &= ~DNS_LUP_FLAGS_QTYPE_A;
|
---|
1120 | line ++;
|
---|
1121 | }
|
---|
1122 | else if (*line == 'A' || *line == 'a') {
|
---|
1123 | *flags &= ~DNS_LUP_FLAGS_QTYPE_AAAA;
|
---|
1124 | line ++;
|
---|
1125 | }
|
---|
1126 | while (*line && !(*line == ' ' || *line == '\t'))
|
---|
1127 | line ++;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | #else /* of #if defined(SUPPORT_INET4) */
|
---|
1131 |
|
---|
1132 | *flags = DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_QTYPE_AAAA;
|
---|
1133 | if (*line == '-') {
|
---|
1134 | line ++;
|
---|
1135 | while (*line && !(*line == ' ' || *line == '\t'))
|
---|
1136 | line ++;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1140 |
|
---|
1141 | #else /* of #if defined(SUPPORT_INET6) */
|
---|
1142 |
|
---|
1143 | #if defined(SUPPORT_INET4)
|
---|
1144 |
|
---|
1145 | *flags = DNS_LUP_FLAGS_PROTO_IPV4 | DNS_LUP_FLAGS_QTYPE_A;
|
---|
1146 | if (*line == '-') {
|
---|
1147 | line ++;
|
---|
1148 | while (*line && !(*line == ' ' || *line == '\t'))
|
---|
1149 | line ++;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1153 |
|
---|
1154 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
1155 |
|
---|
1156 | #if defined(SUPPORT_INET6)
|
---|
1157 |
|
---|
1158 | if (*flags | DNS_LUP_FLAGS_PROTO_IPV6) {
|
---|
1159 | /*
|
---|
1160 | * IPv6 㧠DNS ãµã¼ãã«ç
|
---|
1161 | §ä¼ãããã¨ãæå®ããã¦ãã
|
---|
1162 | * ãµã¼ãã® IPv6 ã¢ãã¬ã¹ãæªå®ç¾©ã®æã¯ãæå®ãå¤ãã
|
---|
1163 | */
|
---|
1164 | dns_in6_get_addr(&in6_addr);
|
---|
1165 | if (IN6_IS_ADDR_UNSPECIFIED(&in6_addr))
|
---|
1166 | *flags &= ~DNS_LUP_FLAGS_PROTO_IPV6;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
1170 |
|
---|
1171 | #if defined(SUPPORT_INET4)
|
---|
1172 |
|
---|
1173 | if (*flags | DNS_LUP_FLAGS_PROTO_IPV4) {
|
---|
1174 | /*
|
---|
1175 | * IPv4 㧠DNS ãµã¼ãã«ç
|
---|
1176 | §ä¼ãããã¨ãæå®ããã¦ãã
|
---|
1177 | * ãµã¼ãã® IPv4 ã¢ãã¬ã¹ãæªå®ç¾©ã®æã¯ãæå®ãå¤ãã
|
---|
1178 | */
|
---|
1179 | dns_in4_get_addr(&in4_addr);
|
---|
1180 | if (in4_addr == IPV4_ADDRANY)
|
---|
1181 | *flags &= ~DNS_LUP_FLAGS_PROTO_IPV4;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
1185 |
|
---|
1186 | return line;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | #endif /* of #ifdef USE_RESOLVER */
|
---|
1190 |
|
---|
1191 | /*
|
---|
1192 | * lookup_ipaddr -- ãã¹ãåã»ãªãã©ã«ã®IPã¢ãã¬ã¹ãIP ã¢ãã¬ã¹ã«å¤æããã
|
---|
1193 | */
|
---|
1194 |
|
---|
1195 | char *
|
---|
1196 | lookup_ipaddr (T_IN_ADDR *addr, char *line, char apip)
|
---|
1197 | {
|
---|
1198 |
|
---|
1199 | #ifdef USE_RESOLVER
|
---|
1200 |
|
---|
1201 | static char hostname[DBG_LINE_SIZE + 1];
|
---|
1202 |
|
---|
1203 | ER_UINT error;
|
---|
1204 | uint_t flags = 0;
|
---|
1205 | char *last;
|
---|
1206 |
|
---|
1207 | line = skip_blanks(resolv_options(&flags, line, apip));
|
---|
1208 | if ((last = resolv_hoststr(&flags, hostname, sizeof(hostname), line)) == NULL) {
|
---|
1209 | cons_printf(CONSOLE_PORTID, "unknown host: %s.\n", hostname);
|
---|
1210 | return NULL;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | switch (flags & DNS_LUP_FLAGS_NAME_MASK) {
|
---|
1214 | case DNS_LUP_FLAGS_NAME_IPV4:
|
---|
1215 | case DNS_LUP_FLAGS_NAME_IPV6:
|
---|
1216 | line = GET_IPADDR(addr, line); /* IP Address by Literal */
|
---|
1217 | break;
|
---|
1218 |
|
---|
1219 | case DNS_LUP_FLAGS_NAME_HOST:
|
---|
1220 | case DNS_LUP_FLAGS_NAME_FQDN:
|
---|
1221 | line = last;
|
---|
1222 | if ((flags & (DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4)) == 0) {
|
---|
1223 | cons_printf(CONSOLE_PORTID, "DNS server not available.\n");
|
---|
1224 | return NULL;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | flags |= DNS_LUP_OPCODE_FORWARD;
|
---|
1228 | if ((error = dns_host_addr(flags, hostname, addr)) != E_OK) {
|
---|
1229 | cons_printf(CONSOLE_PORTID, "error: %s.\n", itron_strerror(error));
|
---|
1230 | return NULL;
|
---|
1231 | }
|
---|
1232 | break;
|
---|
1233 |
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | return line;
|
---|
1237 |
|
---|
1238 | #else /* of #ifdef USE_RESOLVER */
|
---|
1239 |
|
---|
1240 | return GET_IPADDR(addr, skip_blanks(line)); /* IP Address by Literal */
|
---|
1241 |
|
---|
1242 | #endif /* of #ifdef USE_RESOLVER */
|
---|
1243 |
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | #endif /* of #ifdef USE_NETAPP_SUBR */
|
---|