X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fipaddr%2Fng_ipaddr.c;h=37f75b6de919f64d7ddbdfb9ac24a5b3d0e51a75;hp=bbfb5a73741f3eab1d0eb69ec31a406770322eed;hb=HEAD;hpb=feb31e4200b42e0a5e9fb9637fa5f03c7ec05fcb diff --git a/src/ipaddr/ng_ipaddr.c b/src/ipaddr/ng_ipaddr.c index bbfb5a73..37f75b6d 100644 --- a/src/ipaddr/ng_ipaddr.c +++ b/src/ipaddr/ng_ipaddr.c @@ -1,11 +1,14 @@ /* - * Functions for AF_ agnostic ipv4/ipv6 handling. - * * (c) 2008 Florian Westphal , public domain. */ #include "portab.h" +/** + * @file + * Functions for AF_ agnostic ipv4/ipv6 handling. + */ + #include #include #include @@ -14,46 +17,51 @@ #include #include #endif -#include - -#include #include "ng_ipaddr.h" GLOBAL bool ng_ipaddr_init(ng_ipaddr_t *addr, const char *ip_str, UINT16 port) { -#ifdef HAVE_GETADDRINFO +#ifdef HAVE_WORKING_GETADDRINFO int ret; char portstr[64]; struct addrinfo *res0; - struct addrinfo hints = { -#ifndef WANT_IPV6 /* only accept v4 addresses */ - .ai_family = AF_INET, + struct addrinfo hints; + + assert(ip_str); + + memset(&hints, 0, sizeof(hints)); +#ifdef AI_NUMERICHOST + hints.ai_flags = AI_NUMERICHOST; +#endif +#ifndef WANT_IPV6 /* do not convert ipv6 addresses */ + hints.ai_family = AF_INET; #endif - .ai_flags = AI_NUMERICHOST - }; - if (ip_str == NULL) - hints.ai_flags |= AI_PASSIVE; + /* some getaddrinfo implementations require that ai_socktype is set. */ + hints.ai_socktype = SOCK_STREAM; /* silly, but ngircd stores UINT16 in server config, not string */ snprintf(portstr, sizeof(portstr), "%u", (unsigned int) port); + ret = getaddrinfo(ip_str, portstr, &hints, &res0); - assert(ret == 0); if (ret != 0) return false; - assert(sizeof(*addr) >= res0->ai_addrlen); - if (sizeof(*addr) >= res0->ai_addrlen) + assert(sizeof(*addr) >= (size_t)res0->ai_addrlen); + if (sizeof(*addr) >= (size_t)res0->ai_addrlen) memcpy(addr, res0->ai_addr, res0->ai_addrlen); else ret = -1; freeaddrinfo(res0); return ret == 0; #else /* HAVE_GETADDRINFO */ - if (ip_str == NULL) - ip_str = "0.0.0.0"; + assert(ip_str); + memset(addr, 0, sizeof *addr); +#ifdef HAVE_sockaddr_in_len + addr->sin4.sin_len = sizeof(addr->sin4); +#endif addr->sin4.sin_family = AF_INET; # ifdef HAVE_INET_ATON if (inet_aton(ip_str, &addr->sin4.sin_addr) == 0) @@ -154,7 +162,8 @@ ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *str) if (*str == ':') { char tmp[NG_INET_ADDRSTRLEN] = "0"; ret = getnameinfo(sa, ng_ipaddr_salen(addr), - tmp+1, sizeof(tmp) -1, NULL, 0, NI_NUMERICHOST); + tmp + 1, (socklen_t)sizeof(tmp) - 1, + NULL, 0, NI_NUMERICHOST); if (ret == 0) strlcpy(str, tmp, NG_INET_ADDRSTRLEN); }