From: Danny Mayer Date: Sat, 24 Sep 2005 21:39:29 +0000 (-0400) Subject: Revamped the code in getaddrinfo X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98476cd73fb50df787fcc328fc95f9d7574ad6ee;p=thirdparty%2Fntp.git Revamped the code in getaddrinfo bk: 4335c79138mT8nqVOBgl3j2SrI2UZg --- diff --git a/libntp/ntp_rfc2553.c b/libntp/ntp_rfc2553.c index bd8436e298..ee6e321b31 100644 --- a/libntp/ntp_rfc2553.c +++ b/libntp/ntp_rfc2553.c @@ -110,54 +110,48 @@ getaddrinfo (const char *nodename, const char *servname, { int rval; struct addrinfo *ai = NULL; - struct sockaddr_in *sockin; short ntpport = htons(NTP_PORT); + + /* + * If no name is provide just return an error + */ + if (nodename == NULL || servname == NULL) + return (EAI_NONAME); + ai = calloc(sizeof(struct addrinfo), 1); if (ai == NULL) return (EAI_MEMORY); /* - * Heiko: Default values taken from hint + * Copy default values from hints, if available */ - memcpy (ai, hints, sizeof(struct addrinfo)); - - if (nodename != NULL) { - rval = do_nodename(nodename, ai, hints); - if (rval != 0) { - freeaddrinfo(ai); - return (rval); - } + if (hints != NULL) { + ai->ai_flags = hints->ai_flags; + ai->ai_family = hints->ai_family; + ai->ai_socktype = hints->ai_socktype; + ai->ai_protocol = hints->ai_protocol; } - if (nodename == NULL && hints != NULL) { - if (ai->ai_addr == NULL) { - ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1); - if (ai->ai_addr == NULL) { - freeaddrinfo(ai); - return (EAI_MEMORY); - } - } - ai->ai_addrlen = sizeof(struct sockaddr_storage); - sockin = (struct sockaddr_in *)ai->ai_addr; - sockin->sin_family = (short) ai->ai_family; - sockin->sin_addr.s_addr = htonl(INADDR_ANY); -#ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR - ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr); -#endif + + rval = do_nodename(nodename, ai, hints); + if (rval != 0) { + freeaddrinfo(ai); + return (rval); } - if (servname != NULL) { - if (ai->ai_addr == NULL) { - ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1); - if (ai->ai_addr == NULL) { - freeaddrinfo(ai); - return (EAI_MEMORY); - } - } + + /* + * This normally is not the place for this, but as long as this is + * only used within NTP is should be okay. This will only get called + * if the O/S doesn't supply the getaddrinfo() API. + */ + if (ai->ai_socktype == 0) { ai->ai_socktype = SOCK_DGRAM; - if (strcmp(servname, "ntp") != 0) { - freeaddrinfo(ai); - return (EAI_SERVICE); - } + } + if (strcmp(servname, "ntp") != 0 && strcmp(servname, "123") != 0) { + freeaddrinfo(ai); + return (EAI_SERVICE); + } + else { /* * Set up the port number */