X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/b819effdc984e492bbce459faa7432931ccfeb58..766761fd836d9e247daea924809965c21cc65dc7:/methods/rfc2553emu.cc diff --git a/methods/rfc2553emu.cc b/methods/rfc2553emu.cc index 05d9f0c32..372882769 100644 --- a/methods/rfc2553emu.cc +++ b/methods/rfc2553emu.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: rfc2553emu.cc,v 1.6 1999/12/11 02:11:10 jgg Exp $ +// $Id: rfc2553emu.cc,v 1.8 2001/02/20 07:03:18 jgg Exp $ /* ###################################################################### RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo, @@ -14,12 +14,14 @@ ##################################################################### */ /*}}}*/ -#include "rfc2553emu.h" +#include + #include #include #include #include #include +#include "rfc2553emu.h" #ifndef HAVE_GETADDRINFO // getaddrinfo - Resolve a hostname /*{{{*/ @@ -36,20 +38,6 @@ int getaddrinfo(const char *nodename, const char *servname, const char *End; char **CurAddr; - Addr = gethostbyname(nodename); - if (Addr == 0) - { - if (h_errno == TRY_AGAIN) - return EAI_AGAIN; - if (h_errno == NO_RECOVERY) - return EAI_FAIL; - return EAI_NONAME; - } - - // No A records - if (Addr->h_addr_list[0] == 0) - return EAI_NONAME; - // Try to convert the service as a number Port = htons(strtol(servname,(char **)&End,0)); Proto = SOCK_STREAM; @@ -86,10 +74,32 @@ int getaddrinfo(const char *nodename, const char *servname, hints->ai_socktype != 0) return EAI_SERVICE; } + + // Hostname lookup, only if this is not a listening socket + if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE) + { + Addr = gethostbyname(nodename); + if (Addr == 0) + { + if (h_errno == TRY_AGAIN) + return EAI_AGAIN; + if (h_errno == NO_RECOVERY) + return EAI_FAIL; + return EAI_NONAME; + } + + // No A records + if (Addr->h_addr_list[0] == 0) + return EAI_NONAME; + + CurAddr = Addr->h_addr_list; + } + else + CurAddr = (char **)&End; // Fake! // Start constructing the linked list *res = 0; - for (CurAddr = Addr->h_addr_list; *CurAddr != 0; CurAddr++) + for (; *CurAddr != 0; CurAddr++) { // New result structure *Result = (struct addrinfo *)calloc(sizeof(**Result),1); @@ -124,8 +134,15 @@ int getaddrinfo(const char *nodename, const char *servname, // Set the address ((struct sockaddr_in *)(*Result)->ai_addr)->sin_family = AF_INET; ((struct sockaddr_in *)(*Result)->ai_addr)->sin_port = Port; - ((struct sockaddr_in *)(*Result)->ai_addr)->sin_addr = *(in_addr *)(*CurAddr); - + + if (hints != 0 && (hints->ai_flags & AI_PASSIVE) != AI_PASSIVE) + ((struct sockaddr_in *)(*Result)->ai_addr)->sin_addr = *(in_addr *)(*CurAddr); + else + { + // Already zerod by calloc. + break; + } + Result = &(*Result)->ai_next; } @@ -137,11 +154,9 @@ int getaddrinfo(const char *nodename, const char *servname, /* */ void freeaddrinfo(struct addrinfo *ai) { - struct addrinfo *Tmp; while (ai != 0) { free(ai->ai_addr); - Tmp = ai; ai = ai->ai_next; free(ai); } @@ -202,9 +217,9 @@ int getnameinfo(const struct sockaddr *sa, socklen_t salen, { struct servent *Ent; if ((flags & NI_DATAGRAM) == NI_DATAGRAM) - Ent = getservbyport(sin->sin_port,"udp"); + Ent = getservbyport(ntohs(sin->sin_port),"udp"); else - Ent = getservbyport(sin->sin_port,"tcp"); + Ent = getservbyport(ntohs(sin->sin_port),"tcp"); if (Ent != 0) strncpy(serv,Ent->s_name,servlen); @@ -220,7 +235,7 @@ int getnameinfo(const struct sockaddr *sa, socklen_t salen, // Resolve as a plain numberic if ((flags & NI_NUMERICSERV) == NI_NUMERICSERV) { - snprintf(serv,servlen,"%u",sin->sin_port); + snprintf(serv,servlen,"%u",ntohs(sin->sin_port)); } }