From f2097a1f86c64b9c7b7c5988f25343c261100cc1 Mon Sep 17 00:00:00 2001 From: Caoruihong Date: Mon, 2 Nov 2020 18:27:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0IPv6=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=87=BD=E6=95=B0ip6addr=5Faton=EF=BC=8C?= =?UTF-8?q?=E4=BB=A3=E6=9B=BFlwip=E8=87=AA=E5=B8=A6=E7=9A=84=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3ifconfig=E9=85=8D=E7=BD=AEipv6=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=97=B6=E7=9A=84=E8=AF=AF=E6=8A=A5=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/lwip-2.1/enhancement/include/lwip/fixme.h | 2 +- net/lwip-2.1/enhancement/src/fixme.c | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/net/lwip-2.1/enhancement/include/lwip/fixme.h b/net/lwip-2.1/enhancement/include/lwip/fixme.h index 38bfbd19..75687417 100644 --- a/net/lwip-2.1/enhancement/include/lwip/fixme.h +++ b/net/lwip-2.1/enhancement/include/lwip/fixme.h @@ -49,7 +49,7 @@ #define DUP_ARP_DETECT_TIME 2000 /* 2 seconds period */ #define SOF_BINDNONUNICAST 0x0800U /* socket has bind to a non unicast */ #define NETCONN_PKT_RAW 0x80 -#define SYS_ARCH_ERROR SYS_ARCH_TIMEOUT +#define SYS_ARCH_ERROR 0x7fffffffUL #define LWIP_ENABLE_LOS_SHELL_CMD LOSCFG_SHELL #define LWIP_SHELL_CMD_PING_RETRY_TIMES 4 diff --git a/net/lwip-2.1/enhancement/src/fixme.c b/net/lwip-2.1/enhancement/src/fixme.c index 6143567d..3f473f3c 100644 --- a/net/lwip-2.1/enhancement/src/fixme.c +++ b/net/lwip-2.1/enhancement/src/fixme.c @@ -393,3 +393,85 @@ const char *const tcp_state_str[] = { }; volatile int tcpip_init_finish = 1; // needed by api_shell.c + +int ip6addr_aton(const char *cp, ip6_addr_t *addr) +{ + const int ipv6_blocks = 8; + u16_t current_block_index = 0; + u16_t current_block_value = 0; + u16_t addr16[ipv6_blocks]; + u16_t *a16 = (u16_t *)addr->addr; + int squash_pos = ipv6_blocks; + int i; + const char *s = cp; + const char *ss = cp-1; + + for (; ; s++) { + if (current_block_index >= ipv6_blocks) { + return 0; // address too long + } + if (*s == 0) { + if (s - ss == 1) { + if (squash_pos != current_block_index) { + return 0; // empty address or address ends with a single ':' + } // else address ends with one valid "::" + } else { + addr16[current_block_index++] = current_block_value; + } + break; + } else if (*s == ':') { + if (s - ss == 1) { + if (s != cp || s[1] != ':') { + return 0; // address begins with a single ':' or contains ":::" + } // else address begins with one valid "::" + } else { + addr16[current_block_index++] = current_block_value; + } + if (s[1] == ':') { + if (squash_pos != ipv6_blocks) { + return 0; // more than one "::" + } + squash_pos = current_block_index; + s++; + } + ss = s; // ss points to the recent ':' position + current_block_value = 0; + } else if (lwip_isxdigit(*s) && (s - ss) < 5) { // 4 hex-digits at most + current_block_value = (current_block_value << 4) + + (*s | ('a' - 'A')) - '0' - ('a' - '9' - 1) * (*s >= 'A'); +#if LWIP_IPV4 + } else if (*s == '.' && current_block_index < ipv6_blocks - 1) { + ip4_addr_t ip4; + int ret = ip4addr_aton(ss+1, &ip4); + if (!ret) { + return 0; + } + ip4.addr = lwip_ntohl(ip4.addr); + addr16[current_block_index++] = (u16_t)(ip4.addr >> 16); + addr16[current_block_index++] = (u16_t)(ip4.addr); + break; +#endif /* LWIP_IPV4 */ + } else { + return 0; // unexpected char or too many digits + } + } + + if (squash_pos == ipv6_blocks && current_block_index != ipv6_blocks) { + return 0; // address too short + } + if (squash_pos != ipv6_blocks && current_block_index == ipv6_blocks) { + return 0; // unexpected "::" in address + } + + for (i = 0; i < squash_pos; ++i) { + a16[i] = lwip_htons(addr16[i]); + } + for (; i < ipv6_blocks - current_block_index + squash_pos; ++i) { + a16[i] = 0; + } + for (; i < ipv6_blocks; ++i) { + a16[i] = lwip_htons(addr16[i - ipv6_blocks + current_block_index]); + } + + return 1; +}