Changeset 331 for EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/netapp_subr.c
- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/netapp_subr.c
r321 r331 2 2 * TINET (TCP/IP Protocol Stack) 3 3 * 4 * Copyright (C) 2001-20 09by Dep. of Computer Science and Engineering4 * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering 5 5 * Tomakomai National College of Technology, JAPAN 6 6 * … … 44 44 #include <sil.h> 45 45 #include <syssvc/serial.h> 46 #include <syssvc/logtask.h> 47 #include <t_syslog.h> 46 48 #include "kernel_cfg.h" 47 49 #include "tinet_cfg.h" … … 62 64 63 65 #include <net/net.h> 66 #include <net/net_endian.h> 64 67 65 68 #include <netinet/in.h> … … 68 71 #include <netapp/netapp.h> 69 72 #include <netapp/netapp_var.h> 73 #include <netapp/resolver.h> 70 74 71 75 #ifdef USE_NETAPP_SUBR 76 77 /* 78 * IPv6 と IPv4 で引数が異なる関数のコンパイル 79 */ 80 81 #if defined(SUPPORT_INET6) 82 83 #ifdef USE_TCP_EXTENTIONS 84 85 const ID vrid_tcp6_cep[] = 86 { 87 #if NUM_VRID_TCP6_CEPS >= 1 88 TCP6_RSV_CEPID1, 89 #endif 90 #if NUM_VRID_TCP6_CEPS >= 2 91 TCP6_RSV_CEPID2, 92 #endif 93 #if NUM_VRID_TCP6_CEPS >= 3 94 TCP6_RSV_CEPID3, 95 #endif 96 #if NUM_VRID_TCP6_CEPS >= 4 97 TCP6_RSV_CEPID4, 98 #endif 99 }; 100 101 const ID vrid_tcp6_rep[] = 102 { 103 #if NUM_VRID_TCP6_REPS >= 1 104 TCP6_RSV_REPID1, 105 #endif 106 #if NUM_VRID_TCP6_REPS >= 2 107 TCP6_RSV_REPID2, 108 #endif 109 }; 110 111 ID tskid_tcp6_cep[NUM_VRID_TCP6_CEPS]; 112 ID tskid_tcp6_rep[NUM_VRID_TCP6_REPS]; 113 114 #endif /* #ifdef USE_TCP_EXTENTIONS */ 115 116 #ifdef USE_UDP_EXTENTIONS 117 118 const ID vrid_udp6_cep[] = 119 { 120 #if NUM_VRID_UDP6_CEPS >= 1 121 UDP6_RSV_CEPID1, 122 #endif 123 #if NUM_VRID_UDP6_CEPS >= 2 124 UDP6_RSV_CEPID2, 125 #endif 126 }; 127 128 ID tskid_udp6_cep[NUM_VRID_UDP6_CEPS]; 129 130 #endif /* #ifdef USE_UDP_EXTENTIONS */ 131 132 #undef TCP_CRE_REP 133 #undef UDP_CRE_CEP 134 #undef UDP_DEL_CEP 135 #undef TCP_IS_CEPID 136 #undef ALLOC_TCP_REP 137 #undef ALLOC_TCP_CEP 138 #undef ALLOC_UDP_CEP 139 #undef FREE_TCP_REP 140 #undef FREE_TCP_CEP 141 #undef FREE_UDP_CEP 142 #undef T_TCPN_CREP 143 #undef T_UDPN_CCEP 144 #undef NUM_VRID_TCP_CEPS 145 #undef NUM_VRID_TCP_REPS 146 #undef NUM_VRID_UDP_CEPS 147 #undef VRID_TCP_CEP 148 #undef VRID_UDP_CEP 149 #undef VRID_TCP_REP 150 #undef TSKID_TCP_CEP 151 #undef TSKID_TCP_REP 152 #undef TSKID_UDP_CEP 153 #undef API_IPPROTO 154 155 #define TCP_CRE_REP tcp6_cre_rep 156 #define UDP_CRE_CEP udp6_cre_cep 157 #define UDP_DEL_CEP udp6_del_cep 158 159 #define TCP_IS_CEPID tcp6_is_cepid 160 #define ALLOC_TCP_REP alloc_tcp6_rep 161 #define ALLOC_TCP_CEP alloc_tcp6_cep 162 #define ALLOC_UDP_CEP alloc_udp6_cep 163 #define FREE_TCP_REP free_tcp6_rep 164 #define FREE_TCP_CEP free_tcp6_cep 165 #define FREE_UDP_CEP free_udp6_cep 166 #define T_TCPN_CREP T_TCP6_CREP 167 #define T_UDPN_CCEP T_UDP6_CCEP 168 #define NUM_VRID_TCP_CEPS NUM_VRID_TCP6_CEPS 169 #define NUM_VRID_TCP_REPS NUM_VRID_TCP6_REPS 170 #define NUM_VRID_UDP_CEPS NUM_VRID_UDP6_CEPS 171 #define VRID_TCP_CEP vrid_tcp6_cep 172 #define VRID_TCP_REP vrid_tcp6_rep 173 #define VRID_UDP_CEP vrid_udp6_cep 174 #define TSKID_TCP_CEP tskid_tcp6_cep 175 #define TSKID_TCP_REP tskid_tcp6_rep 176 #define TSKID_UDP_CEP tskid_udp6_cep 177 #define API_IPPROTO API_PROTO_IPV6 178 179 #include <netapp/netappn_subr.c> 180 181 #endif /* of #if defined(SUPPORT_INET6) */ 182 183 #if defined(SUPPORT_INET4) 184 185 #ifdef USE_TCP_EXTENTIONS 186 187 const ID vrid_tcp4_cep[] = 188 { 189 #if NUM_VRID_TCP4_CEPS >= 1 190 TCP4_RSV_CEPID1, 191 #endif 192 #if NUM_VRID_TCP4_CEPS >= 2 193 TCP4_RSV_CEPID2, 194 #endif 195 #if NUM_VRID_TCP4_CEPS >= 3 196 TCP4_RSV_CEPID3, 197 #endif 198 #if NUM_VRID_TCP4_CEPS >= 4 199 TCP4_RSV_CEPID4, 200 #endif 201 }; 202 203 const ID vrid_tcp4_rep[] = 204 { 205 #if NUM_VRID_TCP4_REPS >= 1 206 TCP4_RSV_REPID1, 207 #endif 208 #if NUM_VRID_TCP4_REPS >= 2 209 TCP4_RSV_REPID2, 210 #endif 211 }; 212 213 ID tskid_tcp4_cep[NUM_VRID_TCP4_CEPS]; 214 ID tskid_tcp4_rep[NUM_VRID_TCP4_REPS]; 215 216 #endif /* #ifdef USE_TCP_EXTENTIONS */ 217 218 #ifdef USE_UDP_EXTENTIONS 219 220 const ID vrid_udp4_cep[] = 221 { 222 #if NUM_VRID_UDP4_CEPS >= 1 223 UDP4_RSV_CEPID1, 224 #endif 225 #if NUM_VRID_UDP4_CEPS >= 2 226 UDP4_RSV_CEPID2, 227 #endif 228 }; 229 230 ID tskid_udp4_cep[NUM_VRID_UDP4_CEPS]; 231 232 #endif /* #ifdef USE_UDP_EXTENTIONS */ 233 234 #undef TCP_CRE_REP 235 #undef UDP_CRE_CEP 236 #undef UDP_DEL_CEP 237 #undef TCP_IS_CEPID 238 #undef ALLOC_TCP_REP 239 #undef ALLOC_TCP_CEP 240 #undef ALLOC_UDP_CEP 241 #undef FREE_TCP_REP 242 #undef FREE_TCP_CEP 243 #undef FREE_UDP_CEP 244 #undef T_TCPN_CREP 245 #undef T_UDPN_CCEP 246 #undef NUM_VRID_TCP_CEPS 247 #undef NUM_VRID_TCP_REPS 248 #undef NUM_VRID_UDP_CEPS 249 #undef VRID_TCP_CEP 250 #undef VRID_TCP_REP 251 #undef VRID_UDP_CEP 252 #undef TSKID_TCP_CEP 253 #undef TSKID_TCP_REP 254 #undef TSKID_UDP_CEP 255 #undef API_IPPROTO 256 257 #define TCP_CRE_REP tcp_cre_rep 258 #define UDP_CRE_CEP udp_cre_cep 259 #define UDP_DEL_CEP udp_del_cep 260 #define TCP_IS_CEPID tcp4_is_cepid 261 #define ALLOC_TCP_REP alloc_tcp4_rep 262 #define ALLOC_TCP_CEP alloc_tcp4_cep 263 #define ALLOC_UDP_CEP alloc_udp4_cep 264 #define FREE_TCP_REP free_tcp4_rep 265 #define FREE_TCP_CEP free_tcp4_cep 266 #define FREE_UDP_CEP free_udp4_cep 267 #define T_TCPN_CREP T_TCP_CREP 268 #define T_UDPN_CCEP T_UDP_CCEP 269 #define NUM_VRID_TCP_CEPS NUM_VRID_TCP4_CEPS 270 #define NUM_VRID_TCP_REPS NUM_VRID_TCP4_REPS 271 #define NUM_VRID_UDP_CEPS NUM_VRID_UDP4_CEPS 272 #define VRID_TCP_CEP vrid_tcp4_cep 273 #define VRID_TCP_REP vrid_tcp4_rep 274 #define VRID_UDP_CEP vrid_udp4_cep 275 #define TSKID_TCP_CEP tskid_tcp4_cep 276 #define TSKID_TCP_REP tskid_tcp4_rep 277 #define TSKID_UDP_CEP tskid_udp4_cep 278 #define API_IPPROTO API_PROTO_IPV4 279 280 #include <netapp/netappn_subr.c> 281 282 #endif /* of #if defined(SUPPORT_INET4) */ 283 284 #undef TCP_CRE_REP 285 #undef ALLOC_TCPN_REP 286 #undef ALLOC_TCPN_CEP 287 #undef ALLOC_UDPN_CEP 288 #undef FREE_TCPN_REP 289 #undef FREE_TCPN_CEP 290 #undef FREE_UDPN_CEP 291 #undef T_TCPN_CREP 292 #undef NUM_VRID_TCP_REPS 293 #undef NUM_VRID_UDP_CEPS 294 #undef VRID_TCP_CEP 295 #undef VRID_TCP_REP 296 #undef VRID_UDP_CEP 297 #undef TSKID_TCP_CEP 298 #undef TSKID_TCP_REP 299 #undef TSKID_UDP_CEP 300 301 /* 302 * 変数 303 */ 304 305 static uint32_t rand_next = ULONG_C(1); 306 307 #if !defined(SUPPORT_INET6) && defined(SUPPORT_INET4) 308 309 const T_IN6_ADDR in6_addr_unspecified = 310 IPV6_ADDR_UNSPECIFIED_INIT; 311 312 #endif /* of #if !defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */ 72 313 73 314 /* … … 83 324 const char radhex[] = "0123456789abcdef"; 84 325 const char radHEX[] = "0123456789ABCDEF"; 326 327 /* 328 * netapp_rand -- 乱数を返す。 329 */ 330 331 uint32_t 332 netapp_rand (void) 333 { 334 rand_next = (rand_next * 99991 + 12345) & ULONG_C(0x7fffffff); 335 return rand_next; 336 } 337 338 /* 339 * srand -- 乱数を初期化する。 340 */ 341 342 void 343 netapp_srand (uint32_t seed) 344 { 345 SYSTIM now; 346 347 syscall(get_tim(&now)); 348 rand_next += now + seed; 349 } 85 350 86 351 #ifndef USE_NET_CONS … … 160 425 return pchars; 161 426 } 162 163 #if defined(SUPPORT_INET4)164 427 165 428 /* … … 186 449 } 187 450 188 #define PUT_IPADDR(p,a,w) put_ipv4addr(p,a,w)189 190 #endif /* of #if defined(SUPPORT_INET4) */191 192 #if defined(SUPPORT_INET6)193 194 451 /* 195 452 * ipv6addr -- IPv6 アドレス出力 … … 199 456 put_ipv6addr (ID portid, const T_IN6_ADDR *addr, int_t width) 200 457 { 201 int_t len = 0, ix ;458 int_t len = 0, ix, len6; 202 459 bool_t omit = false, zero = false; 203 460 204 if (addr == NULL ) {461 if (addr == NULL || IN6_IS_ADDR_UNSPECIFIED(addr)) { 205 462 cons_putchar(portid, '0'); 206 463 cons_putchar(portid, ':'); … … 210 467 } 211 468 else { 212 for (ix = 0; ix < sizeof(T_IN6_ADDR) / 2; ix ++) { 469 if (in6_is_addr_ipv4mapped(addr)) 470 len6 = sizeof(T_IN6_ADDR) / 2 - 2; 471 else 472 len6 = sizeof(T_IN6_ADDR) / 2; 473 for (ix = 0; ix < len6; ix ++) { 213 474 if (omit) { 214 475 len += cons_putnumber(portid, ntohs(addr->s6_addr16[ix]), 16, radhex, 0, false, ' '); … … 234 495 } 235 496 497 if (len6 == sizeof(T_IN6_ADDR) / 2 - 2) { 498 T_IN4_ADDR ip4addr; 499 500 ip4addr = ntohl(addr->s6_addr32[3]); 501 len += put_ipv4addr(portid, &ip4addr, 0); 502 } 503 236 504 for ( ; len < width; len ++) 237 505 cons_putchar(portid, ' '); … … 239 507 return len; 240 508 } 241 242 #define PUT_IPADDR(p,a,w) put_ipv6addr(p,a,w)243 244 #endif /* of #if defined(SUPPORT_INET6) */245 509 246 510 /* … … 275 539 long_t val; 276 540 char padchar, *str; 277 int_t ch, width, longflag, left; 278 279 #if defined(SUPPORT_INET4) 541 int_t ch, width, longflag, shortflag, left; 280 542 T_IN4_ADDR *addr; 281 #endif /* of #if defined(SUPPORT_INET4) */282 543 283 544 va_start(ap, fmt); … … 288 549 } 289 550 290 width = longflag = 0;551 width = longflag = shortflag = 0; 291 552 padchar = ' '; 292 553 293 if ( ch== '-') { /* 左詰め */554 if (*fmt == '-') { /* 左詰め */ 294 555 fmt ++; 295 556 left = -1; … … 310 571 while (ch == 'l') { /* long (long) の指定 */ 311 572 longflag ++; 573 ch = *fmt ++; 574 } 575 576 while (ch == 'h') { /* short の指定 */ 577 shortflag ++; 312 578 ch = *fmt ++; 313 579 } … … 358 624 case 'I': 359 625 626 if (longflag) { 627 str = va_arg(ap, char*); 628 put_ipv6addr(portid, (T_IN6_ADDR *)str, width); 629 } 630 else if (shortflag) { 631 addr = va_arg(ap, T_IN4_ADDR *); 632 put_ipv4addr(portid, addr, width); 633 } 634 else { 635 636 #if defined(SUPPORT_INET6) 637 638 str = va_arg(ap, char*); 639 put_ipv6addr(portid, (T_IN6_ADDR *)str, width); 640 641 #else /* of #if defined(SUPPORT_INET6) */ 642 360 643 #if defined(SUPPORT_INET4) 361 644 362 addr = va_arg(ap, T_IN4_ADDR *);363 put_ipv4addr(portid, addr, width);645 addr = va_arg(ap, T_IN4_ADDR *); 646 put_ipv4addr(portid, addr, width); 364 647 365 648 #endif /* of #if defined(SUPPORT_INET4) */ 366 649 367 #if defined(SUPPORT_INET6)368 369 str = va_arg(ap, char*);370 put_ipv6addr(portid, (T_IN6_ADDR *)str, width);371 372 650 #endif /* of #if defined(SUPPORT_INET6) */ 373 651 652 } 374 653 break; 375 654 376 655 case 'M': 377 656 str = va_arg(ap, char*); 378 put_macaddr(portid, str, width);657 put_macaddr(portid, (uint8_t *)str, width); 379 658 break; 380 659 … … 396 675 397 676 /* 677 * host2msg16 -- 16ビットの値を、バイトオーダーを調整してメッセージに書き込む。 678 */ 679 680 void 681 host2msg16 (void *msg, uint16_t host_data) 682 { 683 uint16_t msg_data; 684 685 msg_data = htons(host_data); 686 memcpy(msg, &msg_data, sizeof(msg_data)); 687 } 688 689 /* 690 * host2msg32 -- 32ビットの値を、バイトオーダーを調整してメッセージに書き込む。 691 */ 692 693 void 694 host2msg32 (void *msg, uint32_t host_data) 695 { 696 uint32_t msg_data; 697 698 msg_data = htonl(host_data); 699 memcpy(msg, &msg_data, sizeof(msg_data)); 700 } 701 702 /* 703 * msg2host16 -- 16ビットの値を、バイトオーダーを調整してメッセージから読み出す。 704 */ 705 706 uint16_t 707 msg2host16 (void *msg) 708 { 709 uint16_t host_data; 710 711 memcpy(&host_data, msg, sizeof(host_data)); 712 return htons(host_data); 713 } 714 715 /* 716 * msg2host32 -- 32ビットの値を、バイトオーダーを調整してメッセージから読み出す。 717 */ 718 719 uint32_t 720 msg2host32 (void *msg) 721 { 722 uint32_t host_data; 723 724 memcpy(&host_data, msg, sizeof(host_data)); 725 return htonl(host_data); 726 } 727 728 /* 398 729 * skip_blanks -- 空白と TAB をスキップする。 399 730 */ … … 416 747 int_t oct; 417 748 418 *addr = 0;749 *addr = IPV4_ADDRANY; 419 750 while ('0' <= *line && *line <= '9') { 420 751 oct = 0; … … 425 756 line ++; 426 757 } 758 #if 1 759 return *addr == IPV4_ADDRANY ? NULL : line; 760 #else 427 761 return line; 428 } 429 430 #if defined(SUPPORT_INET6) 762 #endif 763 } 764 765 #if defined(SUPPORT_INET6) || defined(USE_RESOLVER) 431 766 432 767 /* … … 438 773 { 439 774 int_t word, ix = 0, omit = 0, six; 775 char *ipv4line; 440 776 441 777 memset(addr, 0, sizeof(T_IN6_ADDR)); 442 while (ix < 8 && 443 (('0' <= *line && *line <= '9') || 444 ('a' <= *line && *line <= 'f') || 445 ('A' <= *line && *line <= 'F'))) { 446 word = 0; 447 while (('0' <= *line && *line <= '9') || 448 ('a' <= *line && *line <= 'f') || 449 ('A' <= *line && *line <= 'F')) { 450 if ('0' <= *line && *line <= '9') 451 word = (word << 4) + (*line ++) - '0'; 452 else if ('a' <= *line && *line <= 'f') 453 word = (word << 4) + (*line ++) - 'a' + 10; 454 else if ('A' <= *line && *line <= 'F') 455 word = (word << 4) + (*line ++) - 'A' + 10; 456 } 457 addr->s6_addr16[ix ++] = htons(word); 458 if (*line == ':') { 459 line ++; 778 779 /* 780 * IPv4 アドレス(数字.)を検出したら IPv4 アドレスとして入力し、 781 * IPv4 射影アドレスに変換する。 782 */ 783 for (ipv4line = line; '0' <= *ipv4line && *ipv4line <= '9'; ipv4line ++) 784 ; 785 if (*ipv4line == '.') { 786 T_IN4_ADDR ipv4addr; 787 788 line = get_ipv4addr(&ipv4addr, line); 789 in6_make_ipv4mapped (addr, ipv4addr); 790 } 791 else { 792 while (ix < 8 && 793 (('0' <= *line && *line <= '9') || 794 ('a' <= *line && *line <= 'f') || 795 ('A' <= *line && *line <= 'F'))) { 796 word = 0; 797 while (('0' <= *line && *line <= '9') || 798 ('a' <= *line && *line <= 'f') || 799 ('A' <= *line && *line <= 'F')) { 800 if ('0' <= *line && *line <= '9') 801 word = (word << 4) + (*line ++) - '0'; 802 else if ('a' <= *line && *line <= 'f') 803 word = (word << 4) + (*line ++) - 'a' + 10; 804 else if ('A' <= *line && *line <= 'F') 805 word = (word << 4) + (*line ++) - 'A' + 10; 806 } 807 addr->s6_addr16[ix ++] = htons(word); 460 808 if (*line == ':') { 461 omit = ix;462 809 line ++; 463 } 464 } 465 } 466 if (omit > 0) { 467 six = 7; 468 while (ix > omit) { 469 addr->s6_addr16[six --] = addr->s6_addr16[-- ix]; 470 addr->s6_addr16[ ix ] = 0; 810 if (*line == ':') { 811 omit = ix; 812 line ++; 813 } 814 } 815 } 816 if (omit > 0) { 817 six = 7; 818 while (ix > omit) { 819 addr->s6_addr16[six --] = addr->s6_addr16[-- ix]; 820 addr->s6_addr16[ ix ] = 0; 821 } 471 822 } 472 823 } … … 474 825 } 475 826 476 #endif /* of #if defined(SUPPORT_INET6) */827 #endif /* of #if defined(SUPPORT_INET6) || defined(USE_RESOLVER) */ 477 828 478 829 /* … … 559 910 } 560 911 561 #ifdef USE_TCP_EXTENTIONS 562 563 const ID vrid_tcp_rep[] = 564 { 565 #if NUM_VRID_TCP_REPS >= 1 566 TCP_RSV_REPID1, 567 #endif 568 #if NUM_VRID_TCP_REPS >= 2 569 TCP_RSV_REPID2, 570 #endif 571 }; 572 573 const ID vrid_tcp_cep[] = 574 { 575 #if NUM_VRID_TCP_CEPS >= 1 576 TCP_RSV_CEPID1, 577 #endif 578 #if NUM_VRID_TCP_CEPS >= 2 579 TCP_RSV_CEPID2, 580 #endif 581 #if NUM_VRID_TCP_CEPS >= 3 582 TCP_RSV_CEPID3, 583 #endif 584 #if NUM_VRID_TCP_CEPS >= 4 585 TCP_RSV_CEPID4, 586 #endif 587 }; 588 589 ID tskid_tcp_rep[NUM_VRID_TCP_REPS]; 590 ID tskid_tcp_cep[NUM_VRID_TCP_CEPS]; 591 592 /* 593 * alloc_tcp_rep -- TCP 受付口を獲得する。 594 */ 595 596 ER 597 alloc_tcp_rep (ID *repid, ID tskid, T_TCP_CREP *crep) 598 { 599 int_t ix; 600 601 *repid = TCP_REP_NONE; 602 syscall(wai_sem(SEM_ALLOC_TCP_REP_LOCK)); 603 for (ix = NUM_VRID_TCP_REPS; ix -- > 0; ) { 604 if (tskid_tcp_rep[ix] == TSK_NONE) { 605 *repid = vrid_tcp_rep[ix]; 606 tskid_tcp_rep[ix] = tskid; 912 #ifdef USE_RESOLVER 913 914 /* 915 * dns_strtype -- DNS の type の文字列を返す。 916 */ 917 918 const char * 919 dns_strtype (uint_t type) 920 { 921 switch (type) { 922 case DNS_TYPE_A: 923 return "A"; 924 break; 925 case DNS_TYPE_NS: 926 return "NS"; 927 break; 928 case DNS_TYPE_CNAME: 929 return "CNAME"; 930 break; 931 case DNS_TYPE_SOA: 932 return "SOA"; 933 break; 934 case DNS_TYPE_PTR: 935 return "PTR"; 936 break; 937 case DNS_TYPE_AAAA: 938 return "AAAA"; 939 break; 940 default: 941 return "unknown type"; 942 break; 943 } 944 } 945 946 /* 947 * dns_strclass -- DNS の class を表示する。 948 */ 949 950 const char * 951 dns_strclass (uint_t class) 952 { 953 switch (class) { 954 case DNS_CLASS_IN: 955 return "IN"; 956 break; 957 default: 958 return "unknown class"; 959 break; 960 } 961 } 962 963 /* 964 * resolv_hoststr -- 文字列のFQDN・ホスト名・IPv6/IPv4 アドレスを解析する。 965 */ 966 967 #define IS_HOSTCH_DIGIT(c) (('0'<=(c)&&(c)<='9')) 968 #define IS_HOSTCH_XDIGIT(c) (IS_HOSTCH_DIGIT(c)||('a'<=(c)&&(c)<='f')||('A'<=(c)&&(c)<='F')) 969 #define IS_HOSTCH_ALPHA(c) (('a'<=(c)&&(c)<='z')||('A'<=(c)&&(c)<='z')) 970 #define IS_HOSTCH_ALNUM(c) (IS_HOSTCH_ALPHA(c)||IS_HOSTCH_DIGIT(c)) 971 #define IS_HOSTCH_IPV6ADDR(c) (IS_HOSTCH_XDIGIT(c)||(c)==':') 972 #define IS_HOSTCH_IPV4ADDR(c) (IS_HOSTCH_DIGIT(c)||(c)=='.') 973 #define IS_HOSTCH_IPADDR(c) (IS_HOSTCH_XDIGIT(c)||(c)==':'||(c)=='.') 974 #define IS_HOSTCH_NAME_FIRST(c) (IS_HOSTCH_ALPHA(c)) 975 #define IS_HOSTCH_NAME(c) (IS_HOSTCH_ALNUM(c)||(c)=='-'||(c)=='.') 976 #define IS_HOSTCH_BLANK(c) ((c)=='\0'||(c)==' '||(c)=='\t') 977 978 char * 979 resolv_hoststr (uint_t *flags, char *hostname, uint_t name_size, char *line) 980 { 981 char *h, *p; 982 983 h = hostname; 984 p = line = skip_blanks(line); 985 986 if (IS_HOSTCH_IPADDR(*p)) { 987 while (IS_HOSTCH_DIGIT(*p)) { 988 if (h - hostname > name_size) 989 return NULL; 990 *h ++ = *p ++; 991 } 992 if (p > line && *p == '.') { 993 while (IS_HOSTCH_IPV4ADDR(*p)) { 994 if (h - hostname > name_size) 995 return NULL; 996 *h ++ = *p ++; 997 } 998 if (IS_HOSTCH_BLANK(*p)) { 999 *flags |= HOSTSTR_IPV4; 1000 *h = '\0'; 1001 return p; 1002 } 1003 } 1004 else if (IS_HOSTCH_IPADDR(*p)) { 1005 while (IS_HOSTCH_IPADDR(*p)) { 1006 if (h - hostname > name_size) 1007 return NULL; 1008 *h ++ = *p ++; 1009 } 1010 if (IS_HOSTCH_BLANK(*p)) { 1011 *flags |= HOSTSTR_IPV6; 1012 *h = '\0'; 1013 return p; 1014 } 1015 } 1016 h = hostname; 1017 p = line; 1018 } 1019 1020 if (IS_HOSTCH_NAME_FIRST(*p)) { 1021 *flags |= HOSTSTR_HOSTNAME; 1022 while (IS_HOSTCH_NAME(*p)) { 1023 if (h - hostname > name_size) 1024 return NULL; 1025 if (*p == '.') 1026 *flags |= HOSTSTR_FQDN; 1027 *h ++ = *p ++; 1028 } 1029 } 1030 else 1031 *flags |= HOSTSTR_OTHER; 1032 1033 *h = '\0'; 1034 return p; 1035 } 1036 1037 /* 1038 * show_dns_domain_name -- DNS のドメイン名を表示する。 1039 */ 1040 1041 uint_t 1042 show_dns_domain_name (ID portid, uint8_t *hdr, uint_t offset) 1043 { 1044 uint8_t *ptr; 1045 uint_t c; 1046 1047 ptr = hdr + offset; 1048 while (*ptr) { 1049 if ((*ptr & DNS_MSG_COMP_MARK) == DNS_MSG_COMP_MARK) { 1050 show_dns_domain_name(portid, hdr, (*ptr & ~DNS_MSG_COMP_MARK) << 8 | *(ptr + 1)); 1051 ptr += 2; 607 1052 break; 608 1053 } 609 } 610 syscall(sig_sem(SEM_ALLOC_TCP_REP_LOCK)); 611 612 if (*repid == TCP_REP_NONE) 613 return E_NOEXS; 1054 else { 1055 for (c = 1; c <= *ptr; c++) 1056 cons_printf(portid, "%c", *(ptr + c)); 1057 ptr += *ptr + 1; 1058 if (*ptr) 1059 cons_printf(portid, "."); 1060 } 1061 } 1062 return ptr - hdr; 1063 } 1064 1065 /* 1066 * resolv_options -- アドレス解決のオプション解析 1067 */ 1068 1069 char * 1070 resolv_options (uint_t *flags, char *line, char apip) 1071 { 1072 #if defined(SUPPORT_INET6) 1073 T_IN6_ADDR in6_addr; 1074 #endif 1075 1076 #if defined(SUPPORT_INET4) 1077 T_IN4_ADDR in4_addr; 1078 #endif 1079 1080 line = skip_blanks(line); 1081 1082 #if defined(SUPPORT_INET6) 1083 1084 #if defined(SUPPORT_INET4) 1085 1086 *flags |= DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4; 1087 if (apip == API_PROTO_IPV4) 1088 *flags |= DNS_LUP_FLAGS_QTYPE_A; 614 1089 else 615 return TCP_CRE_REP(*repid, crep); 616 } 617 618 /* 619 * free_tcp_rep -- TCP 受付口を解放する。 620 */ 621 622 ER 623 free_tcp_rep (ID repid, bool_t call_tcp_del_rep) 624 { 625 int_t ix; 626 627 syscall(wai_sem(SEM_ALLOC_TCP_REP_LOCK)); 628 for (ix = NUM_VRID_TCP_REPS; ix -- > 0; ) { 629 if (repid == vrid_tcp_rep[ix]) { 630 tskid_tcp_rep[ix] = TSK_NONE; 631 break; 632 } 633 } 634 syscall(sig_sem(SEM_ALLOC_TCP_REP_LOCK)); 635 636 if (call_tcp_del_rep) 637 return tcp_del_rep(repid); 638 else 639 return E_OK; 640 } 641 642 /* 643 * alloc_tcp_cep -- TCP 通信端点を獲得する。 644 */ 645 646 ER 647 alloc_tcp_cep (ID *cepid, ID tskid, T_TCP_CCEP *ccep) 648 { 649 int_t ix; 650 651 *cepid = TCP_CEP_NONE; 652 syscall(wai_sem(SEM_ALLOC_TCP_CEP_LOCK)); 653 for (ix = NUM_VRID_TCP_CEPS; ix -- > 0; ) { 654 if (tskid_tcp_cep[ix] == TSK_NONE) { 655 *cepid = vrid_tcp_cep[ix]; 656 tskid_tcp_cep[ix] = tskid; 657 break; 658 } 659 } 660 syscall(sig_sem(SEM_ALLOC_TCP_CEP_LOCK)); 661 662 if (*cepid == TCP_CEP_NONE) 663 return E_NOEXS; 664 else 665 return tcp_cre_cep(*cepid, ccep); 666 } 667 668 /* 669 * free_tcp_cep -- TCP 通信端点を解放する。 670 */ 671 672 ER 673 free_tcp_cep (ID cepid) 674 { 675 int_t ix; 676 677 syscall(wai_sem(SEM_ALLOC_TCP_CEP_LOCK)); 678 for (ix = NUM_VRID_TCP_CEPS; ix -- > 0; ) { 679 if (cepid == vrid_tcp_cep[ix]) { 680 tskid_tcp_cep[ix] = TSK_NONE; 681 break; 682 } 683 } 684 syscall(sig_sem(SEM_ALLOC_TCP_CEP_LOCK)); 685 686 return tcp_del_cep(cepid); 687 } 688 689 #endif /* of #ifdef USE_TCP_EXTENTIONS */ 690 691 #ifdef USE_UDP_EXTENTIONS 692 693 const ID vrid_udp_cep[] = 694 { 695 #if NUM_VRID_UDP_CEPS >= 1 696 UDP_RSV_CEPID1, 697 #endif 698 #if NUM_VRID_UDP_CEPS >= 2 699 UDP_RSV_CEPID2, 700 #endif 701 }; 702 703 ID tskid_udp_cep[NUM_VRID_UDP_CEPS]; 704 705 /* 706 * alloc_udp_cep -- UDP 通信端点を獲得する。 707 */ 708 709 ER 710 alloc_udp_cep (ID *cepid, ID tskid, T_UDP_CCEP *ccep) 711 { 712 int_t ix; 713 714 *cepid = UDP_CEP_NONE; 715 syscall(wai_sem(SEM_ALLOC_UDP_CEP_LOCK)); 716 for (ix = NUM_VRID_UDP_CEPS; ix -- > 0; ) { 717 if (tskid_udp_cep[ix] == TSK_NONE) { 718 *cepid = vrid_udp_cep[ix]; 719 tskid_udp_cep[ix] = tskid; 720 break; 721 } 722 } 723 syscall(sig_sem(SEM_ALLOC_UDP_CEP_LOCK)); 724 725 if (*cepid == UDP_CEP_NONE) 726 return E_NOEXS; 727 else 728 return UDP_CRE_CEP(*cepid, ccep); 729 } 730 731 /* 732 * free_udp_cep -- UDP 通信端点を解放する。 733 */ 734 735 ER 736 free_udp_cep (ID cepid, bool_t call_udp_del_cep) 737 { 738 int_t ix; 739 740 syscall(wai_sem(SEM_ALLOC_UDP_CEP_LOCK)); 741 for (ix = NUM_VRID_UDP_CEPS; ix -- > 0; ) { 742 if (cepid == vrid_udp_cep[ix]) { 743 tskid_udp_cep[ix] = TSK_NONE; 744 break; 745 } 746 } 747 syscall(sig_sem(SEM_ALLOC_UDP_CEP_LOCK)); 748 749 if (call_udp_del_cep) 750 return udp_del_cep(cepid); 751 else 752 return E_OK; 753 } 754 755 #endif /* of #ifdef USE_UDP_EXTENTIONS */ 1090 *flags |= DNS_LUP_FLAGS_QTYPE_AAAA; 1091 1092 if (*line == '-') { 1093 line ++; 1094 if (*line == '6') { 1095 line ++; 1096 *flags &= ~DNS_LUP_FLAGS_PROTO_IPV4; 1097 } 1098 else if (*line == '4') { 1099 line ++; 1100 *flags &= ~DNS_LUP_FLAGS_PROTO_IPV6; 1101 } 1102 if (*line == 'Q' || *line == 'q') { 1103 *flags &= ~DNS_LUP_FLAGS_QTYPE_A; 1104 line ++; 1105 } 1106 else if (*line == 'A' || *line == 'a') { 1107 *flags &= ~DNS_LUP_FLAGS_QTYPE_AAAA; 1108 line ++; 1109 } 1110 while (*line && !(*line == ' ' || *line == '\t')) 1111 line ++; 1112 } 1113 1114 #else /* of #if defined(SUPPORT_INET4) */ 1115 1116 *flags = DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_QTYPE_AAAA; 1117 if (*line == '-') { 1118 line ++; 1119 while (*line && !(*line == ' ' || *line == '\t')) 1120 line ++; 1121 } 1122 1123 #endif /* of #if defined(SUPPORT_INET4) */ 1124 1125 #else /* of #if defined(SUPPORT_INET6) */ 1126 1127 #if defined(SUPPORT_INET4) 1128 1129 *flags = DNS_LUP_FLAGS_PROTO_IPV4 | DNS_LUP_FLAGS_QTYPE_A; 1130 if (*line == '-') { 1131 line ++; 1132 while (*line && !(*line == ' ' || *line == '\t')) 1133 line ++; 1134 } 1135 1136 #endif /* of #if defined(SUPPORT_INET4) */ 1137 1138 #endif /* of #if defined(SUPPORT_INET6) */ 1139 1140 #if defined(SUPPORT_INET6) 1141 1142 if (*flags | DNS_LUP_FLAGS_PROTO_IPV6) { 1143 /* 1144 * IPv6 で DNS サーバに照会することが指定されても、 1145 * サーバの IPv6 アドレスが未定義の時は、指定を外す。 1146 */ 1147 dns_in6_get_addr(&in6_addr); 1148 if (IN6_IS_ADDR_UNSPECIFIED(&in6_addr)) 1149 *flags &= ~DNS_LUP_FLAGS_PROTO_IPV6; 1150 } 1151 1152 #endif /* of #if defined(SUPPORT_INET6) */ 1153 1154 #if defined(SUPPORT_INET4) 1155 1156 if (*flags | DNS_LUP_FLAGS_PROTO_IPV4) { 1157 /* 1158 * IPv4 で DNS サーバに照会することが指定されても、 1159 * サーバの IPv4 アドレスが未定義の時は、指定を外す。 1160 */ 1161 dns_in4_get_addr(&in4_addr); 1162 if (in4_addr == IPV4_ADDRANY) 1163 *flags &= ~DNS_LUP_FLAGS_PROTO_IPV4; 1164 } 1165 1166 #endif /* of #if defined(SUPPORT_INET4) */ 1167 1168 return line; 1169 } 1170 1171 #endif /* of #ifdef USE_RESOLVER */ 1172 1173 /* 1174 * lookup_ipaddr -- ホスト名・リテラルのIPアドレスをIP アドレスに変換する。 1175 */ 1176 1177 char * 1178 lookup_ipaddr (T_IN_ADDR *addr, char *line, char apip) 1179 { 1180 1181 #ifdef USE_RESOLVER 1182 1183 static char hostname[DBG_LINE_SIZE + 1]; 1184 1185 ER_UINT error; 1186 uint_t flags = 0; 1187 char *last; 1188 1189 line = skip_blanks(resolv_options(&flags, line, apip)); 1190 if ((last = resolv_hoststr(&flags, hostname, sizeof(hostname), line)) == NULL) { 1191 cons_printf(CONSOLE_PORTID, "unknown host: %s.\n", hostname); 1192 return NULL; 1193 } 1194 1195 switch (flags & DNS_LUP_FLAGS_NAME_MASK) { 1196 case DNS_LUP_FLAGS_NAME_IPV4: 1197 case DNS_LUP_FLAGS_NAME_IPV6: 1198 line = GET_IPADDR(addr, line); /* IP Address by Literal */ 1199 break; 1200 1201 case DNS_LUP_FLAGS_NAME_HOST: 1202 case DNS_LUP_FLAGS_NAME_FQDN: 1203 line = last; 1204 if ((flags & (DNS_LUP_FLAGS_PROTO_IPV6 | DNS_LUP_FLAGS_PROTO_IPV4)) == 0) { 1205 cons_printf(CONSOLE_PORTID, "DNS server not available.\n"); 1206 return NULL; 1207 } 1208 1209 flags |= DNS_LUP_OPCODE_FORWARD; 1210 if ((error = dns_host_addr(flags, hostname, addr)) != E_OK) { 1211 cons_printf(CONSOLE_PORTID, "error: %s.\n", itron_strerror(error)); 1212 return NULL; 1213 } 1214 break; 1215 1216 } 1217 1218 return line; 1219 1220 #else /* of #ifdef USE_RESOLVER */ 1221 1222 return GET_IPADDR(addr, skip_blanks(line)); /* IP Address by Literal */ 1223 1224 #endif /* of #ifdef USE_RESOLVER */ 1225 1226 } 756 1227 757 1228 #endif /* of #ifdef USE_NETAPP_SUBR */
Note:
See TracChangeset
for help on using the changeset viewer.