提交 0b2ac37c 编写于 作者: O openharmony_ci 提交者: Gitee

!27 [Lightweight PR]: 实现IPv6地址解析函数ip6addr_aton,代替lwip自带的,修正ifconfig配置ipv6地址时的误报错误

Merge pull request !27 from Caoruihong/N/A
......@@ -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
......
......@@ -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;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册