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