判断IP地址是否正确
int os_sock_ipv4_is_valied(const char *addr) { uint32_t seg[4] = {0}; const char *start, *end; int cnt = 0; if (!addr || (!*addr)) { error("not invalid value"); return -1; } start = addr; do { end = start; // 判断是否'\0' while (*start) { if (!((*start >= '0') && (*start <= '9'))) break; // 正常退出循环 seg[cnt] *= 10; seg[cnt] += (*start++) - '0'; if (seg[cnt] > 255) { error("num(%d) is too big", seg[cnt]); return -1; } } if (start == end) { error("not one can be do"); return -1; // 判断是否至少执行一个字节解析 } cnt++; } while ((*start++)); if (cnt < 4) { error("Failed to next item, cnt = %d", cnt); return -1; } else if (cnt > 4) { error("Failed to more item, cnt = %d", cnt); return -1; } debug("seg = %d.%d.%d.%d", seg[0], seg[1], seg[2], seg[3]); return 0; }