Commit ab6bebf6 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Fix parsing of IPv6 address/port pairs.

parent f8c441d7
...@@ -272,9 +272,10 @@ main(int argc, char **argv) ...@@ -272,9 +272,10 @@ main(int argc, char **argv)
if(af == 4) { if(af == 4) {
memcpy(&sin6.sin6_addr, v4mapped, 12); memcpy(&sin6.sin6_addr, v4mapped, 12);
memcpy((unsigned char*)&sin6.sin6_addr + 12, buf, 4); memcpy((unsigned char*)&sin6.sin6_addr + 12, buf, 4);
} else { } else if(af == 6) {
memcpy(&sin6.sin6_addr, buf, 16); memcpy(&sin6.sin6_addr, buf, 16);
} } else
goto usage;
sin6.sin6_port = htons(port); sin6.sin6_port = htons(port);
find_neighbour(&sin6, 1, 0, 1); find_neighbour(&sin6, 1, 0, 1);
} else { } else {
......
...@@ -246,38 +246,45 @@ int parse_addrport(const char *string, unsigned char *a_return, ...@@ -246,38 +246,45 @@ int parse_addrport(const char *string, unsigned char *a_return,
unsigned short *port_return) unsigned short *port_return)
{ {
char buf[INET6_ADDRSTRLEN]; char buf[INET6_ADDRSTRLEN];
int rc, port; int rc, ret, port;
char *colon;
struct in_addr ina; struct in_addr ina;
struct in6_addr ina6; struct in6_addr ina6;
char *colon, *end;
strncpy(buf, string, INET6_ADDRSTRLEN); strncpy(buf, string, INET6_ADDRSTRLEN);
buf[INET6_ADDRSTRLEN - 1] = '\0'; buf[INET6_ADDRSTRLEN - 1] = '\0';
colon = strchr(buf, ':'); if(buf[0] == '[') {
if(colon == NULL) { char *bracket;
return -1; bracket = strchr(buf, ']');
if(bracket == NULL || *(bracket + 1) != ':')
return -1;
colon = bracket + 1;
*bracket = '\0';
rc = inet_pton(AF_INET6, buf + 1, &ina6);
if(rc <= 0)
return -1;
memcpy(a_return, &ina6, 16);
ret = 6;
} else { } else {
char *end; colon = strchr(buf, ':');
port = strtol(colon + 1, &end, 0); if(colon == NULL) {
if(*end != '\0' || port <= 0 || port > 0xFFFF)
return -1; return -1;
}
*colon = '\0'; *colon = '\0';
} rc = inet_pton(AF_INET, buf, &ina);
if(rc <= 0)
rc = inet_pton(AF_INET, buf, &ina); return -1;
if(rc > 0) {
memcpy(a_return, &ina, 4); memcpy(a_return, &ina, 4);
*port_return = port; ret = 4;
return 4;
}
rc = inet_pton(AF_INET6, buf, &ina6);
if(rc > 0) {
memcpy(a_return, &ina6, 16);
*port_return = port;
return 6;
} }
return -1;
port = strtol(colon + 1, &end, 0);
if(*end != '\0' || port <= 0 || port > 0xFFFF)
return -1;
*port_return = port;
return ret;
} }
static const unsigned char zeroes[6]; static const unsigned char zeroes[6];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment