]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
use macros ERRNO, SET_ERRNO(), SOCKERRNO and SET_SOCKERRNO() for errno handling
authorYang Tse <yangsita@gmail.com>
Fri, 16 Feb 2007 15:04:44 +0000 (15:04 +0000)
committerYang Tse <yangsita@gmail.com>
Fri, 16 Feb 2007 15:04:44 +0000 (15:04 +0000)
ares/adig.c
ares/ares_init.c
ares/ares_process.c
ares/inet_net_pton.c
ares/inet_ntop.c
ares/windows_port.c

index 6a23c513812102fa58481cb0335de2e38bafdf07..32e07217d5c7cd09ff20d386bdf3923fa6c07627 100644 (file)
@@ -289,7 +289,7 @@ int main(int argc, char **argv)
         break;
       tvp = ares_timeout(channel, NULL, &tv);
       count = select(nfds, &read_fds, &write_fds, NULL, tvp);
-      if (count < 0 && errno != EINVAL)
+      if (count < 0 && SOCKERRNO != EINVAL)
         {
           perror("select");
           return 1;
index b0fa50f2b257feae683881c082c0c99ea07f1826..5268745d0b0a55f0b523ac22572ccfc6520e611b 100644 (file)
@@ -610,6 +610,7 @@ DhcpNameServer
     char *p;
     FILE *fp;
     int linesize;
+    int error;
 
     fp = fopen(PATH_RESOLV_CONF, "r");
     if (fp) {
@@ -635,13 +636,14 @@ DhcpNameServer
       fclose(fp);
     }
     else {
-      switch(errno) {
+      error = ERRNO;
+      switch(error) {
       case ENOENT:
         status = ARES_EOF;
         break;
       default:
         DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
-                       errno, strerror(errno)));
+                       error, strerror(error)));
         DEBUGF(fprintf(stderr, "Error opening file: %s\n", PATH_RESOLV_CONF));
         status = ARES_EFILE;
       }
@@ -659,13 +661,14 @@ DhcpNameServer
         fclose(fp);
       }
       else {
-        switch(errno) {
+        error = ERRNO;
+        switch(error) {
         case ENOENT:
           status = ARES_EOF;
           break;
         default:
           DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
-                         errno, strerror(errno)));
+                         error, strerror(error)));
           DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/nsswitch.conf"));
           status = ARES_EFILE;
         }
@@ -684,13 +687,14 @@ DhcpNameServer
         fclose(fp);
       }
       else {
-        switch(errno) {
+        error = ERRNO;
+        switch(error) {
         case ENOENT:
           status = ARES_EOF;
           break;
         default:
           DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
-                         errno, strerror(errno)));
+                         error, strerror(error)));
           DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
           status = ARES_EFILE;
         }
@@ -709,13 +713,14 @@ DhcpNameServer
         fclose(fp);
       }
       else {
-        switch(errno) {
+        error = ERRNO;
+        switch(error) {
         case ENOENT:
           status = ARES_EOF;
           break;
         default:
           DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
-                         errno, strerror(errno)));
+                         error, strerror(error)));
           DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
           status = ARES_EFILE;
         }
index f6669ca989577d7ec674701156a0dc8ab075f215..480948ebefb79e1dabde6e603bbd614dede320d7 100644 (file)
 #define TRUE 1
 #endif
 
-#ifdef USE_WINSOCK
-#define GET_ERRNO()  WSAGetLastError()
-#else
-#define GET_ERRNO()  errno
-#endif
-
 static int try_again(int errnum);
 static void write_tcp_data(ares_channel channel, fd_set *write_fds,
                            time_t now);
@@ -97,7 +91,7 @@ void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
   process_timeouts(channel, now);
 }
 
-/* Return 1 if the specified errno describes a readiness error, or 0
+/* Return 1 if the specified error number describes a readiness error, or 0
  * otherwise. This is mostly for HP-UX, which could return EAGAIN or
  * EWOULDBLOCK. See this man page
  *
@@ -164,7 +158,7 @@ static void write_tcp_data(ares_channel channel, fd_set *write_fds, time_t now)
           free(vec);
           if (wcount < 0)
             {
-              if (!try_again(GET_ERRNO()))
+              if (!try_again(SOCKERRNO))
                   handle_error(channel, i, now);
               continue;
             }
@@ -200,7 +194,7 @@ static void write_tcp_data(ares_channel channel, fd_set *write_fds, time_t now)
           scount = swrite(server->tcp_socket, sendreq->data, sendreq->len);
           if (scount < 0)
             {
-              if (!try_again(GET_ERRNO()))
+              if (!try_again(SOCKERRNO))
                   handle_error(channel, i, now);
               continue;
             }
@@ -253,7 +247,7 @@ static void read_tcp_data(ares_channel channel, fd_set *read_fds, time_t now)
                         2 - server->tcp_lenbuf_pos);
           if (count <= 0)
             {
-              if (!(count == -1 && try_again(GET_ERRNO())))
+              if (!(count == -1 && try_again(SOCKERRNO)))
                   handle_error(channel, i, now);
               continue;
             }
@@ -280,7 +274,7 @@ static void read_tcp_data(ares_channel channel, fd_set *read_fds, time_t now)
                         server->tcp_length - server->tcp_buffer_pos);
           if (count <= 0)
             {
-              if (!(count == -1 && try_again(GET_ERRNO())))
+              if (!(count == -1 && try_again(SOCKERRNO)))
                   handle_error(channel, i, now);
               continue;
             }
@@ -322,7 +316,7 @@ static void read_udp_packets(ares_channel channel, fd_set *read_fds,
         continue;
 
       count = sread(server->udp_socket, buf, sizeof(buf));
-      if (count == -1 && try_again(GET_ERRNO()))
+      if (count == -1 && try_again(SOCKERRNO))
         continue;
       else if (count <= 0)
         handle_error(channel, i, now);
@@ -615,7 +609,7 @@ static int open_tcp_socket(ares_channel channel, struct server_state *server)
   sockin.sin_addr = server->addr;
   sockin.sin_port = (unsigned short)(channel->tcp_port & 0xffff);
   if (connect(s, (struct sockaddr *) &sockin, sizeof(sockin)) == -1) {
-    int err = GET_ERRNO();
+    int err = SOCKERRNO;
 
     if (err != EINPROGRESS && err != EWOULDBLOCK) {
       closesocket(s);
index 49bac69a686c02c2e704469381942c00d35b7823..af941bf6ba26fbcbae8672d242ccbcfeb94f3bdd 100644 (file)
  * note:
  *      network byte order assumed.  this means 192.5.5.240/28 has
  *      0b11110000 in its fourth octet.
+ * note:
+ *      On Windows we store the error in the thread errno, not
+ *      in the winsock error code. This is to avoid loosing the
+ *      actual last winsock error. So use macro ERRNO to fetch the
+ *      errno this funtion sets when returning (-1), not SOCKERRNO.
  * author:
  *      Paul Vixie (ISC), June 1996
  */
@@ -186,11 +191,11 @@ inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
   return (bits);
 
   enoent:
-  errno = ENOENT;
+  SET_ERRNO(ENOENT);
   return (-1);
 
   emsgsize:
-  errno = EMSGSIZE;
+  SET_ERRNO(EMSGSIZE);
   return (-1);
 }
 
@@ -381,11 +386,11 @@ inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
   return (bits);
 
   enoent:
-  errno = ENOENT;
+  SET_ERRNO(ENOENT);
   return (-1);
 
   emsgsize:
-  errno = EMSGSIZE;
+  SET_ERRNO(EMSGSIZE);
   return (-1);
 }
 
@@ -399,6 +404,11 @@ inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
  *      number of bits, either imputed classfully or specified with /CIDR,
  *      or -1 if some failure occurred (check errno).  ENOENT means it was
  *      not a valid network specification.
+ * note:
+ *      On Windows we store the error in the thread errno, not
+ *      in the winsock error code. This is to avoid loosing the
+ *      actual last winsock error. So use macro ERRNO to fetch the
+ *      errno this funtion sets when returning (-1), not SOCKERRNO.
  * author:
  *      Paul Vixie (ISC), June 1996
  */
@@ -411,7 +421,7 @@ ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
   case AF_INET6:
     return (inet_net_pton_ipv6(src, dst, size));
   default:
-    errno = EAFNOSUPPORT;
+    SET_ERRNO(EAFNOSUPPORT);
     return (-1);
   }
 }
@@ -429,11 +439,11 @@ int ares_inet_pton(int af, const char *src, void *dst)
     size = sizeof(struct in6_addr);
   else
   {
-    errno = EAFNOSUPPORT;
+    SET_ERRNO(EAFNOSUPPORT);
     return -1;
   }
   result = ares_inet_net_pton(af, src, dst, size);
-  if (result == -1 && errno == ENOENT)
+  if (result == -1 && ERRNO == ENOENT)
     return 0;
   return (result > -1 ? 1 : -1);
 }
index 097e7764e0de9a7f8fdd370e3ce0226cb97d636e..f076e06654561d6066a61794ea8461b95c98eaec 100644 (file)
@@ -68,6 +68,11 @@ static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
  *     convert a network format address to presentation format.
  * return:
  *     pointer to presentation format address (`dst'), or NULL (see errno).
+ * note:
+ *      On Windows we store the error in the thread errno, not
+ *      in the winsock error code. This is to avoid loosing the
+ *      actual last winsock error. So use macro ERRNO to fetch the
+ *      errno this funtion sets when returning NULL, not SOCKERRNO.
  * author:
  *     Paul Vixie, 1996.
  */
@@ -81,7 +86,7 @@ ares_inet_ntop(int af, const void *src, char *dst, size_t size)
     case AF_INET6:
       return (inet_ntop6(src, dst, size));
     default:
-      errno = EAFNOSUPPORT;
+      SET_ERRNO(EAFNOSUPPORT);
       return (NULL);
     }
   /* NOTREACHED */
@@ -106,7 +111,7 @@ inet_ntop4(const unsigned char *src, char *dst, size_t size)
 
   if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size)
     {
-      errno = ENOSPC;
+      SET_ERRNO(ENOSPC);
       return (NULL);
     }
     strcpy(dst, tmp);
@@ -218,7 +223,7 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
    */
   if ((size_t)(tp - tmp) > size)
     {
-      errno = ENOSPC;
+      SET_ERRNO(ENOSPC);
       return (NULL);
     }
   strcpy(dst, tmp);
index dabe0bcccd7c94b32df9dabf16a7535ec9e88173..d9c358e2f57ee8461a25f1180b0ea0c7efe3d543 100644 (file)
@@ -105,7 +105,7 @@ ares_writev (ares_socket_t s, const struct iovec *vector, size_t count)
   buffer = bp = (char*) alloca (bytes);
   if (!buffer)
   {
-    errno = ENOMEM;
+    SET_ERRNO(ENOMEM);
     return (-1);
   }