X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=lib%2Fsocket-util.c;h=6f959b271a785b2daf98bc188e5c47d6c6e0f916;hb=8b8ef592521e32d0e32581bf39c5d2a5cd445977;hp=206e17bd23b3740cf7eeecb6b48e972a66fa8b3f;hpb=afc1d536743e2d869fe90712b51a57334b57e3cc;p=cascardo%2Fovs.git diff --git a/lib/socket-util.c b/lib/socket-util.c index 206e17bd2..6f959b271 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -136,13 +136,22 @@ set_dscp(int fd, int family, uint8_t dscp) return retval ? sock_errno() : 0; } +/* Checks whether 'host_name' is an IPv4 or IPv6 address. It is assumed + * that 'host_name' is valid. Returns false if it is IPv4 address, true if + * it is IPv6 address. */ +bool +addr_is_ipv6(const char *host_name) +{ + return strchr(host_name, ':') != NULL; +} + /* Translates 'host_name', which must be a string representation of an IP * address, into a numeric IP address in '*addr'. Returns 0 if successful, * otherwise a positive errno value. */ int lookup_ip(const char *host_name, struct in_addr *addr) { - if (!inet_pton(AF_INET, host_name, addr)) { + if (!ip_parse(host_name, &addr->s_addr)) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name); return ENOENT; @@ -156,7 +165,7 @@ lookup_ip(const char *host_name, struct in_addr *addr) int lookup_ipv6(const char *host_name, struct in6_addr *addr) { - if (inet_pton(AF_INET6, host_name, addr) != 1) { + if (!ipv6_parse(host_name, addr)) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name); return ENOENT; @@ -179,7 +188,7 @@ lookup_hostname(const char *host_name, struct in_addr *addr) struct addrinfo *result; struct addrinfo hints; - if (inet_pton(AF_INET, host_name, addr)) { + if (ip_parse(host_name, &addr->s_addr)) { return 0; } @@ -362,14 +371,14 @@ parse_sockaddr_components(struct sockaddr_storage *ss, sin6->sin6_family = AF_INET6; sin6->sin6_port = htons(port); - if (!inet_pton(AF_INET6, host_s, sin6->sin6_addr.s6_addr)) { + if (!ipv6_parse(host_s, &sin6->sin6_addr)) { VLOG_ERR("%s: bad IPv6 address \"%s\"", s, host_s); goto exit; } } else { sin->sin_family = AF_INET; sin->sin_port = htons(port); - if (!inet_pton(AF_INET, host_s, &sin->sin_addr.s_addr)) { + if (!ip_parse(host_s, &sin->sin_addr.s_addr)) { VLOG_ERR("%s: bad IPv4 address \"%s\"", s, host_s); goto exit; }