]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet.c
Add support for new POSIX getaddrinfo interface.
[bacula/bacula] / bacula / src / lib / bnet.c
index 8f802b15111eaa286f8b88bc474c96734e8db3b7..1e8b8f4d3eca2ec52497c37f3a6b5442e18ae0c7 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
@@ -15,7 +15,7 @@
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
  * Adapted and enhanced for Bacula, originally written
  * for inclusion in the Apcupsd package
  *
  * Adapted and enhanced for Bacula, originally written
  * for inclusion in the Apcupsd package
  *
- *   Version $Id$
  */
 
  */
 
-
 #include "bacula.h"
 #include "jcr.h"
 #include <netdb.h>
 #include "bacula.h"
 #include "jcr.h"
 #include <netdb.h>
 #define socketClose(fd)           close(fd)
 #endif
 
 #define socketClose(fd)           close(fd)
 #endif
 
+#ifndef HAVE_GETADDRINFO
 static pthread_mutex_t ip_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t ip_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
 
 /*
  * Read a nbytes from the network.
  * It is possible that the total bytes require in several
  * read requests
  */
 
 /*
  * Read a nbytes from the network.
  * It is possible that the total bytes require in several
  * read requests
  */
-
 int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
 {
    int32_t nleft, nread;
 int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
 {
    int32_t nleft, nread;
@@ -79,19 +78,38 @@ int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
       errno = 0;
       nread = socketRead(bsock->m_fd, ptr, nleft);
       if (bsock->is_timed_out() || bsock->is_terminated()) {
       errno = 0;
       nread = socketRead(bsock->m_fd, ptr, nleft);
       if (bsock->is_timed_out() || bsock->is_terminated()) {
-         return nread;
+         return -1;
       }
       }
+
+#ifdef HAVE_WIN32
+      /*
+       * For Windows, we must simulate Unix errno on a socket
+       *  error in order to handle errors correctly.
+       */
+      if (nread == SOCKET_ERROR) {
+        DWORD err = WSAGetLastError();
+        nread = -1;
+        if (err == WSAEINTR) {
+           errno = EINTR;
+        } else if (err == WSAEWOULDBLOCK) {
+           errno = EAGAIN;
+        } else {
+           errno = EIO;            /* some other error */
+        }
+     }
+#endif
+
       if (nread == -1) {
          if (errno == EINTR) {
             continue;
          }
          if (errno == EAGAIN) {
       if (nread == -1) {
          if (errno == EINTR) {
             continue;
          }
          if (errno == EAGAIN) {
-            bmicrosleep(0, 200000);  /* try again in 200ms */
+            bmicrosleep(0, 20000);  /* try again in 20ms */
             continue;
          }
       }
       if (nread <= 0) {
             continue;
          }
       }
       if (nread <= 0) {
-         return nread;             /* error, or EOF */
+         return -1;                /* error, or EOF */
       }
       nleft -= nread;
       ptr += nread;
       }
       nleft -= nread;
       ptr += nread;
@@ -135,8 +153,27 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
          errno = 0;
          nwritten = socketWrite(bsock->m_fd, ptr, nleft);
          if (bsock->is_timed_out() || bsock->is_terminated()) {
          errno = 0;
          nwritten = socketWrite(bsock->m_fd, ptr, nleft);
          if (bsock->is_timed_out() || bsock->is_terminated()) {
-            return nwritten;
+            return -1;
+         }
+
+#ifdef HAVE_WIN32
+         /*
+          * For Windows, we must simulate Unix errno on a socket
+          *  error in order to handle errors correctly.
+          */
+         if (nwritten == SOCKET_ERROR) {
+            DWORD err = WSAGetLastError();
+            nwritten = -1;
+            if (err == WSAEINTR) {
+               errno = EINTR;
+            } else if (err == WSAEWOULDBLOCK) {
+               errno = EAGAIN;
+            } else {
+               errno = EIO;        /* some other error */
+            }
          }
          }
+#endif
+
       } while (nwritten == -1 && errno == EINTR);
       /*
        * If connection is non-blocking, we will get EAGAIN, so
       } while (nwritten == -1 && errno == EINTR);
       /*
        * If connection is non-blocking, we will get EAGAIN, so
@@ -149,13 +186,13 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
 
          FD_ZERO(&fdset);
          FD_SET((unsigned)bsock->m_fd, &fdset);
 
          FD_ZERO(&fdset);
          FD_SET((unsigned)bsock->m_fd, &fdset);
-         tv.tv_sec = 10;
+         tv.tv_sec = 1;
          tv.tv_usec = 0;
          select(bsock->m_fd + 1, NULL, &fdset, NULL, &tv);
          continue;
       }
       if (nwritten <= 0) {
          tv.tv_usec = 0;
          select(bsock->m_fd + 1, NULL, &fdset, NULL, &tv);
          continue;
       }
       if (nwritten <= 0) {
-         return nwritten;          /* error */
+         return -1;                /* error */
       }
       nleft -= nwritten;
       ptr += nwritten;
       }
       nleft -= nwritten;
       ptr += nwritten;
@@ -371,6 +408,64 @@ int bnet_wait_data_intr(BSOCK * bsock, int sec)
 #define NO_DATA         4          /* Valid name, no data record of requested type. */
 #endif
 
 #define NO_DATA         4          /* Valid name, no data record of requested type. */
 #endif
 
+#if HAVE_GETADDRINFO
+const char *resolv_host(int family, const char *host, dlist *addr_list)
+{
+   int res;
+   struct addrinfo hints;
+   struct addrinfo *ai, *rp;
+   IPADDR *addr;
+
+   memset(&hints, 0, sizeof(struct addrinfo));
+   hints.ai_family = family;
+   hints.ai_socktype = 0;
+   hints.ai_protocol = 0;
+   hints.ai_flags = 0;
+
+   res = getaddrinfo(host, NULL, &hints, &ai);
+   if (res != 0) {
+      return gai_strerror(res);
+   }
+
+   for (rp = ai; rp != NULL; rp = rp->ai_next) {
+      switch (rp->ai_addr->sa_family) {
+      case AF_INET:
+         addr = New(IPADDR(rp->ai_addr->sa_family));
+         addr->set_type(IPADDR::R_MULTIPLE);
+         /*
+          * Some serious casting to get the struct in_addr *
+          * rp->ai_addr == struct sockaddr
+          * as this is AF_INET family we can cast that
+          * to struct_sockaddr_in. Of that we need the
+          * address of the sin_addr member which contains a
+          * struct in_addr
+          */
+         addr->set_addr4(&(((struct sockaddr_in *)rp->ai_addr)->sin_addr));
+         break;
+#ifdef HAVE_IPV6
+      case AF_INET6:
+         addr = New(IPADDR(rp->ai_addr->sa_family));
+         addr->set_type(IPADDR::R_MULTIPLE);
+         /*
+          * Some serious casting to get the struct in6_addr *
+          * rp->ai_addr == struct sockaddr
+          * as this is AF_INET6 family we can cast that
+          * to struct_sockaddr_in6. Of that we need the
+          * address of the sin6_addr member which contains a
+          * struct in6_addr
+          */
+         addr->set_addr6(&(((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr));
+         break;
+#endif
+      default:
+         continue;
+      }
+      addr_list->append(addr);
+   }
+   freeaddrinfo(ai);
+   return NULL;
+}
+#else
 /*
  * Get human readable error for gethostbyname()
  */
 /*
  * Get human readable error for gethostbyname()
  */
@@ -403,21 +498,12 @@ static const char *gethost_strerror()
    return msg;
 }
 
    return msg;
 }
 
-
-
-
-static IPADDR *add_any(int family)
-{
-   IPADDR *addr = New(IPADDR(family));
-   addr->set_type(IPADDR::R_MULTIPLE);
-   addr->set_addr_any();
-   return addr;
-}
-
-static const char *resolv_host(int family, const char *host, dlist * addr_list)
+static const char *resolv_host(int family, const char *host, dlist *addr_list)
 {
    struct hostent *hp;
    const char *errmsg;
 {
    struct hostent *hp;
    const char *errmsg;
+   char **p;
+   IPADDR *addr;
 
    P(ip_mutex);                       /* gethostbyname() is not thread safe */
 #ifdef HAVE_GETHOSTBYNAME2
 
    P(ip_mutex);                       /* gethostbyname() is not thread safe */
 #ifdef HAVE_GETHOSTBYNAME2
@@ -430,24 +516,38 @@ static const char *resolv_host(int family, const char *host, dlist * addr_list)
       V(ip_mutex);
       return errmsg;
    } else {
       V(ip_mutex);
       return errmsg;
    } else {
-      char **p;
       for (p = hp->h_addr_list; *p != 0; p++) {
       for (p = hp->h_addr_list; *p != 0; p++) {
-         IPADDR *addr =  New(IPADDR(hp->h_addrtype));
-         addr->set_type(IPADDR::R_MULTIPLE);
-         if (addr->get_family() == AF_INET) {
-             addr->set_addr4((struct in_addr*)*p);
-         }
+         switch (hp->h_addrtype) {
+         case AF_INET:
+            addr = New(IPADDR(hp->h_addrtype));
+            addr->set_type(IPADDR::R_MULTIPLE);
+            addr->set_addr4((struct in_addr *)*p);
+            break;
 #ifdef HAVE_IPV6
 #ifdef HAVE_IPV6
-         else {
-             addr->set_addr6((struct in6_addr*)*p);
-         }
+          case AF_INET6:
+            addr = New(IPADDR(hp->h_addrtype));
+            addr->set_type(IPADDR::R_MULTIPLE);
+            addr->set_addr6((struct in6_addr *)*p);
+            break;
 #endif
 #endif
+         default:
+            continue;
+         }
          addr_list->append(addr);
       }
       V(ip_mutex);
    }
    return NULL;
 }
          addr_list->append(addr);
       }
       V(ip_mutex);
    }
    return NULL;
 }
+#endif
+
+static IPADDR *add_any(int family)
+{
+   IPADDR *addr = New(IPADDR(family));
+   addr->set_type(IPADDR::R_MULTIPLE);
+   addr->set_addr_any();
+   return addr;
+}
 
 /*
  * i host = 0 mean INADDR_ANY only ipv4
 
 /*
  * i host = 0 mean INADDR_ANY only ipv4
@@ -532,8 +632,6 @@ BSOCK *bnet_connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
    return bsock;
 }
 
    return bsock;
 }
 
-
-
 /*
  * Return the string for the error that occurred
  * on the socket. Only the first error is retained.
 /*
  * Return the string for the error that occurred
  * on the socket. Only the first error is retained.
@@ -617,7 +715,6 @@ void bnet_restore_blocking (BSOCK *bsock, int flags)
    bsock->restore_blocking(flags);
 }
 
    bsock->restore_blocking(flags);
 }
 
-
 /*
  * Send a network "signal" to the other end
  *  This consists of sending a negative packet length
 /*
  * Send a network "signal" to the other end
  *  This consists of sending a negative packet length
@@ -652,8 +749,10 @@ const char *bnet_sig_to_ascii(BSOCK * bs)
       return "BNET_HEARTBEAT";
    case BNET_HB_RESPONSE:
       return "BNET_HB_RESPONSE";
       return "BNET_HEARTBEAT";
    case BNET_HB_RESPONSE:
       return "BNET_HB_RESPONSE";
-   case BNET_PROMPT:
-      return "BNET_PROMPT";
+   case BNET_SUB_PROMPT:
+      return "BNET_SUB_PROMPT";
+   case BNET_TEXT_INPUT:
+      return "BNET_TEXT_INPUT";
    default:
       sprintf(buf, _("Unknown sig %d"), (int)bs->msglen);
       return buf;
    default:
       sprintf(buf, _("Unknown sig %d"), (int)bs->msglen);
       return buf;
@@ -673,7 +772,7 @@ BSOCK *init_bsock(JCR * jcr, int sockfd, const char *who, const char *host, int
    bsock->tls = NULL;
    bsock->errors = 0;
    bsock->m_blocking = 1;
    bsock->tls = NULL;
    bsock->errors = 0;
    bsock->m_blocking = 1;
-   bsock->msg = get_pool_memory(PM_MESSAGE);
+   bsock->msg = get_pool_memory(PM_BSOCK);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    bsock->set_who(bstrdup(who));
    bsock->set_host(bstrdup(host));
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    bsock->set_who(bstrdup(who));
    bsock->set_host(bstrdup(host));
@@ -693,7 +792,7 @@ BSOCK *dup_bsock(BSOCK *osock)
 {
    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memcpy(bsock, osock, sizeof(BSOCK));
 {
    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memcpy(bsock, osock, sizeof(BSOCK));
-   bsock->msg = get_pool_memory(PM_MESSAGE);
+   bsock->msg = get_pool_memory(PM_BSOCK);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    if (osock->who()) {
       bsock->set_who(bstrdup(osock->who()));
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
    if (osock->who()) {
       bsock->set_who(bstrdup(osock->who()));