Add TCP ping

This commit is contained in:
Nick Peng
2018-06-10 00:29:07 +08:00
parent b05dd784bc
commit ec83f75582
9 changed files with 898 additions and 686 deletions

17
util.c
View File

@@ -44,11 +44,13 @@ errout:
return NULL;
}
int parse_ip(char *value, char *ip, int *port)
int parse_ip(const char *value, char *ip, int *port)
{
int offset = 0;
char *colon = NULL;
colon = strstr(value, ":");
if (strstr(value, "[")) {
/* ipv6 with port */
char *bracket_end = strstr(value, "]");
@@ -60,9 +62,11 @@ int parse_ip(char *value, char *ip, int *port)
memcpy(ip, value + 1, offset);
ip[offset] = 0;
colon = bracket_end + 1;
} else if (strstr(value, "::")) {
colon = strstr(bracket_end, ":");
if (colon) {
colon++;
}
} else if (colon && strstr(colon + 1, ":")) {
/* ipv6 without port */
strncpy(ip, value, MAX_IP_LEN);
colon = NULL;
@@ -77,6 +81,7 @@ int parse_ip(char *value, char *ip, int *port)
offset = colon - value;
colon++;
memcpy(ip, value, offset);
ip[offset] = 0;
}
}
@@ -87,5 +92,9 @@ int parse_ip(char *value, char *ip, int *port)
*port = PORT_NOT_DEFINED;
}
if (ip[0] == 0) {
return -1;
}
return 0;
}