X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fcups.git;a=blobdiff_plain;f=cups%2Fhttp-addrlist.c;h=52b5aeca9c075304be94bac2313d51b679b0c948;hp=8f8e5c06f533bee22fec843aba6f17680df10b26;hb=49d8745278805ede5c41bae3c299e14ba40457bf;hpb=bc44d92092094935265183305a38196ce2822756 diff --git a/cups/http-addrlist.c b/cups/http-addrlist.c index 8f8e5c06f..52b5aeca9 100644 --- a/cups/http-addrlist.c +++ b/cups/http-addrlist.c @@ -1,9 +1,9 @@ /* - * "$Id: http-addrlist.c 6649 2007-07-11 21:46:42Z mike $" + * "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $" * * HTTP address list routines for the Common UNIX Printing System (CUPS). * - * Copyright 2007 by Apple Inc. + * Copyright 2007-2008 by Apple Inc. * Copyright 1997-2007 by Easy Software Products, all rights reserved. * * These coded instructions, statements, and computer programs are the @@ -27,6 +27,10 @@ #include "globals.h" #include "debug.h" #include +#include +#ifdef HAVE_RESOLV_H +# include +#endif /* HAVE_RESOLV_H */ /* @@ -41,7 +45,18 @@ httpAddrConnect( int *sock) /* O - Socket */ { int val; /* Socket option value */ +#ifdef DEBUG + char temp[256]; /* Temporary address string */ +#endif /* DEBUG */ + + DEBUG_printf(("httpAddrConnect(addrlist=%p, sock=%p)\n", addrlist, sock)); + + if (!sock) + { + errno = EINVAL; + return (NULL); + } /* * Loop through each address until we connect or run out of addresses... @@ -53,7 +68,12 @@ httpAddrConnect( * Create the socket... */ - if ((*sock = (int)socket(addrlist->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) + DEBUG_printf(("httpAddrConnect: Trying %s:%d...\n", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)))); + + if ((*sock = (int)socket(addrlist->addr.addr.sa_family, SOCK_STREAM, + 0)) < 0) { /* * Don't abort yet, as this could just be an issue with the local @@ -113,7 +133,16 @@ httpAddrConnect( if (!connect(*sock, &(addrlist->addr.addr), httpAddrLength(&(addrlist->addr)))) + { + DEBUG_printf(("httpAddrConnect: Connected to %s:%d...\n", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)))); break; + } + + DEBUG_printf(("httpAddrConnect: Unable to connect to %s:%d: %s\n", + httpAddrString(&(addrlist->addr), temp, sizeof(temp)), + _httpAddrPort(&(addrlist->addr)), strerror(errno))); /* * Close this socket and move to the next address... @@ -125,6 +154,7 @@ httpAddrConnect( close(*sock); #endif /* WIN32 */ + *sock = -1; addrlist = addrlist->next; } @@ -174,21 +204,46 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p http_addrlist_t *first, /* First address in list */ *addr, /* Current address in list */ *temp; /* New address */ + _cups_globals_t *cg = _cupsGlobals(); + /* Global data */ #ifdef DEBUG - printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, service=\"%s\")\n", - hostname ? hostname : "(nil)", - family == AF_UNSPEC ? "UNSPEC" : + _cups_debug_printf("httpAddrGetList(hostname=\"%s\", family=AF_%s, " + "service=\"%s\")\n", + hostname ? hostname : "(nil)", + family == AF_UNSPEC ? "UNSPEC" : # ifdef AF_LOCAL - family == AF_LOCAL ? "LOCAL" : + family == AF_LOCAL ? "LOCAL" : # endif /* AF_LOCAL */ # ifdef AF_INET6 - family == AF_INET6 ? "INET6" : + family == AF_INET6 ? "INET6" : # endif /* AF_INET6 */ - family == AF_INET ? "INET" : "???", service); + family == AF_INET ? "INET" : "???", service); #endif /* DEBUG */ +#ifdef HAVE_RES_INIT + /* + * STR #2920: Initialize resolver after failure in cups-polld + * + * If the previous lookup failed, re-initialize the resolver to prevent + * temporary network errors from persisting. This *should* be handled by + * the resolver libraries, but apparently the glibc folks do not agree. + * + * We set a flag at the end of this function if we encounter an error that + * requires reinitialization of the resolver functions. We then call + * res_init() if the flag is set on the next call here or in httpAddrLookup(). + */ + + if (cg->need_res_init) + { + res_init(); + + cg->need_res_init = 0; + } +#endif /* HAVE_RES_INIT */ + + /* * Lookup the address the best way we can... */ @@ -202,9 +257,11 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p * Domain socket address... */ - first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t)); - first->addr.un.sun_family = AF_LOCAL; - strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path)); + if ((first = (http_addrlist_t *)calloc(1, sizeof(http_addrlist_t))) != NULL) + { + first->addr.un.sun_family = AF_LOCAL; + strlcpy(first->addr.un.sun_path, hostname, sizeof(first->addr.un.sun_path)); + } } else #endif /* AF_LOCAL */ @@ -216,6 +273,8 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p char ipv6[1024], /* IPv6 address */ *ipv6zone; /* Pointer to zone separator */ int ipv6len; /* Length of IPv6 address */ + int error; /* getaddrinfo() error */ + /* * Lookup the address as needed... @@ -267,7 +326,7 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p } } - if (!getaddrinfo(hostname, service, &hints, &results)) + if ((error = getaddrinfo(hostname, service, &hints, &results)) == 0) { /* * Copy the results to our own address list structure... @@ -313,6 +372,9 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p freeaddrinfo(results); } + else if (error == EAI_FAIL) + cg->need_res_init = 1; + #else if (hostname) { @@ -429,6 +491,8 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p addr = temp; } } + else if (h_errno == NO_RECOVERY) + cg->need_res_init = 1; } #endif /* HAVE_GETADDRINFO */ } @@ -526,8 +590,6 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p if (addr) addr->next = temp; - else - addr = temp; } } else if (!hostname) @@ -581,8 +643,6 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p if (addr) addr->next = temp; - else - addr = temp; } } } @@ -596,5 +656,5 @@ httpAddrGetList(const char *hostname, /* I - Hostname, IP address, or NULL for p /* - * End of "$Id: http-addrlist.c 6649 2007-07-11 21:46:42Z mike $". + * End of "$Id: http-addrlist.c 7460 2008-04-16 02:19:54Z mike $". */