diff --git a/client/fwknop_common.h b/client/fwknop_common.h index 52134de7..45fc75bc 100644 --- a/client/fwknop_common.h +++ b/client/fwknop_common.h @@ -87,8 +87,8 @@ typedef struct fko_cli_options int no_save_args; int use_hmac; char spa_server_str[MAX_SERVER_STR_LEN]; /* may be a hostname */ - char allow_ip_str[MAX_IPV4_STR_LEN]; - char spoof_ip_src_str[MAX_IPV4_STR_LEN]; + char allow_ip_str[MAX_IPV46_STR_LEN]; + char spoof_ip_src_str[MAX_IPV46_STR_LEN]; char spoof_user[MAX_USERNAME_LEN]; int rand_port; char gpg_recipient_key[MAX_GPG_KEY_ID]; diff --git a/client/http_resolve_host.c b/client/http_resolve_host.c index 6db56270..6a7a0dc5 100644 --- a/client/http_resolve_host.c +++ b/client/http_resolve_host.c @@ -58,9 +58,8 @@ struct url static int try_url(struct url *url, fko_cli_options_t *options) { - int sock=-1, sock_success=0, res, error, http_buf_len, i; + int sock=-1, sock_success=0, res, error, http_buf_len; int bytes_read = 0, position = 0; - int o1, o2, o3, o4; struct addrinfo *result=NULL, *rp, hints; char http_buf[HTTP_MAX_REQUEST_LEN] = {0}; char http_response[HTTP_MAX_RESPONSE_LEN] = {0}; @@ -197,45 +196,35 @@ try_url(struct url *url, fko_cli_options_t *options) } ndx += 4; - /* Walk along the content to try to find the end of the IP address. - * Note: We are expecting the content to be just an IP address + /* Try to parse the content as an IP address. + * Note: We are expecting the content to be exactly that * (possibly followed by whitespace or other not-digit value). */ - for(i=0; i= 0 && o1 <= 255 - && o2 >= 0 && o2 <= 255 - && o3 >= 0 && o3 <= 255 - && o4 >= 0 && o4 <= 255) - { - strlcpy(options->allow_ip_str, ndx, sizeof(options->allow_ip_str)); - - log_msg(LOG_VERBOSITY_INFO, - "\n[+] Resolved external IP (via http://%s%s) as: %s", - url->host, - url->path, - options->allow_ip_str); - - return(1); - } - else + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + hints.ai_flags = AI_NUMERICHOST | AI_CANONNAME; + error = getaddrinfo(ndx, NULL, &hints, &result); + if (error != 0) { log_msg(LOG_VERBOSITY_ERROR, "[-] From http://%s%s\n Invalid IP (%s) in HTTP response:\n\n%s", url->host, url->path, ndx, http_response); return(-1); } + for (rp = result; rp != NULL; rp = rp->ai_next) { + strlcpy(options->allow_ip_str, + rp->ai_canonname, sizeof(options->allow_ip_str)); + break; + } + freeaddrinfo(result); + + log_msg(LOG_VERBOSITY_INFO, + "\n[+] Resolved external IP (via http://%s%s) as: %s", + url->host, + url->path, + options->allow_ip_str); + + return(1); } static int diff --git a/lib/fko_limits.h b/lib/fko_limits.h index 5f02cc18..2a326008 100644 --- a/lib/fko_limits.h +++ b/lib/fko_limits.h @@ -59,6 +59,12 @@ #define MAX_IPV4_STR_LEN 16 #define MIN_IPV4_STR_LEN 7 +#define MAX_IPV46_STR_LEN 40 +#define MIN_IPV46_STR_LEN 3 + +#define MAX_IPV6_STR_LEN 40 +#define MIN_IPV6_STR_LEN 3 + #define MAX_PROTO_STR_LEN 4 /* tcp, udp, icmp for now */ #define MAX_PORT_STR_LEN 5 #define MAX_PORT 65535