]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / bnet.c
index 080b3a4bd67050ae523982eb5575d83cf016960b..13485befa132c1d06570fe1af69cbba655d00a7e 100644 (file)
@@ -43,6 +43,8 @@ extern time_t watchdog_time;
 #endif
 
 
+
+
 /*
  * Read a nbytes from the network.
  * It is possible that the total bytes require in several
@@ -58,7 +60,10 @@ static int32_t read_nbytes(BSOCK *bsock, char *ptr, int32_t nbytes)
       do {
         errno = 0;
         nread = read(bsock->fd, ptr, nleft);    
-      } while (!bsock->timed_out && nread == -1 && (errno == EINTR || errno == EAGAIN));
+        if (bsock->timed_out || bsock->terminated) {
+           return nread;
+        }
+      } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
       if (nread <= 0) {
         return nread;               /* error, or EOF */
       }
@@ -91,7 +96,26 @@ static int32_t write_nbytes(BSOCK *bsock, char *ptr, int32_t nbytes)
       do {
         errno = 0;
         nwritten = write(bsock->fd, ptr, nleft);
-      } while (!bsock->timed_out && nwritten == -1 && (errno == EINTR || errno == EAGAIN));
+        if (bsock->timed_out || bsock->terminated) {
+           return nwritten;
+        }
+      } while (nwritten == -1 && errno == EINTR);
+      /*
+       * If connection is non-blocking, we will get EAGAIN, so
+       * use select() to keep from consuming all the CPU
+       * and try again.
+       */
+      if (nwritten == -1 && errno == EAGAIN) {
+        fd_set fdset;
+        struct timeval tv;
+
+        FD_ZERO(&fdset);
+        FD_SET(bsock->fd, &fdset);
+        tv.tv_sec = 10;
+        tv.tv_usec = 0;
+        select(bsock->fd + 1, NULL, &fdset, NULL, &tv);
+        continue;
+      }
       if (nwritten <= 0) {
         return nwritten;            /* error */
       }
@@ -123,7 +147,8 @@ int32_t bnet_recv(BSOCK *bsock)
    int32_t nbytes;
    int32_t pktsiz;
 
-   bsock->msg[0] = 0;
+   ASSERT(bsock != NULL);
+   mp_chr(bsock->msg)[0] = 0;
    if (bsock->errors || bsock->terminated) {
       return BNET_HARDEOF;
    }
@@ -147,8 +172,8 @@ int32_t bnet_recv(BSOCK *bsock)
    if (nbytes != sizeof(int32_t)) {
       bsock->errors++;
       bsock->b_errno = EIO;
-      Jmsg3(bsock->jcr, M_ERROR, 0, _("Read %d expected %d from %s\n"), nbytes, sizeof(int32_t),
-           bsock->who);
+      Jmsg5(bsock->jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"), pktsiz, nbytes,
+           bsock->who, bsock->host, bsock->port);
       return BNET_ERROR;
    }
 
@@ -164,6 +189,9 @@ int32_t bnet_recv(BSOCK *bsock)
    /* If signal or packet size too big */
    if (pktsiz < 0 || pktsiz > 1000000) {
       if (pktsiz > 0) {              /* if packet too big */
+        Jmsg3(bsock->jcr, M_FATAL, 0, 
+            _("Packet size too big from \"%s:%s:%d. Terminating connection.\n"),
+           bsock->who, bsock->host, bsock->port);
         pktsiz = BNET_TERMINATE;     /* hang up */
       }
       if (pktsiz == BNET_TERMINATE) {
@@ -183,7 +211,7 @@ int32_t bnet_recv(BSOCK *bsock)
    bsock->timer_start = watchdog_time; /* set start wait time */
    bsock->timed_out = 0;
    /* now read the actual data */
-   if ((nbytes = read_nbytes(bsock, bsock->msg, pktsiz)) <=  0) {
+   if ((nbytes = read_nbytes(bsock, mp_chr(bsock->msg), pktsiz)) <=  0) {
       bsock->timer_start = 0;        /* clear timer */
       if (errno == 0) {
         bsock->b_errno = ENODATA;
@@ -209,7 +237,7 @@ int32_t bnet_recv(BSOCK *bsock)
     * string that was send to us. Note, we ensured above that the
     * buffer is at least one byte longer than the message length.
     */
-   bsock->msg[nbytes] = 0;           /* terminate in case it is a string */
+   mp_chr(bsock->msg)[nbytes] = 0;    /* terminate in case it is a string */
    sm_check(__FILE__, __LINE__, False);
    return nbytes;                    /* return actual length of message */
 }
@@ -256,7 +284,7 @@ int bnet_despool(BSOCK *bsock)
       bsock->msglen = ntohl(pktsiz);
       if (bsock->msglen > 0) {
         if (bsock->msglen > (int32_t)sizeof_pool_memory(bsock->msg)) {
-           bsock->msg = realloc_pool_memory(bsock->msg, bsock->msglen);
+           bsock->msg = realloc_pool_memory(bsock->msg, bsock->msglen + 1);
         }
         nbytes = fread(bsock->msg, 1, bsock->msglen, bsock->spool_fd);
         if (nbytes != (size_t)bsock->msglen) {
@@ -288,10 +316,13 @@ bnet_send(BSOCK *bsock)
 {
    int32_t rc;
    int32_t pktsiz;
+   int32_t msglen;
 
    if (bsock->errors || bsock->terminated) {
       return 0;
    }
+   msglen = bsock->msglen;
+   ASSERT(bsock->msglen < 1000000);
    pktsiz = htonl((int32_t)bsock->msglen);
    /* send int32_t containing size of data packet */
    bsock->timer_start = watchdog_time; /* start timer */
@@ -299,6 +330,10 @@ bnet_send(BSOCK *bsock)
    rc = write_nbytes(bsock, (char *)&pktsiz, sizeof(int32_t));
    bsock->timer_start = 0;           /* clear timer */
    if (rc != sizeof(int32_t)) {
+      if (bsock->msglen == BNET_TERMINATE) { /* if we were terminating */
+        bsock->terminated = 1;
+        return 0;                    /* ignore any errors */
+      }
       bsock->errors++;
       if (errno == 0) {
         bsock->b_errno = EIO;
@@ -306,7 +341,7 @@ bnet_send(BSOCK *bsock)
         bsock->b_errno = errno;
       }
       if (rc < 0) {
-        if (!bsock->suppress_error_msgs) {
+        if (!bsock->suppress_error_msgs && !bsock->timed_out) {
             Jmsg4(bsock->jcr, M_ERROR, 0, _("Write error sending to %s:%s:%d: ERR=%s\n"), 
                  bsock->who, bsock->host, bsock->port,  bnet_strerror(bsock));
         }
@@ -319,13 +354,14 @@ bnet_send(BSOCK *bsock)
 
    bsock->out_msg_no++;              /* increment message number */
    if (bsock->msglen <= 0) {         /* length only? */
+      ASSERT(msglen == bsock->msglen);
       return 1;                      /* yes, no data */
    }
 
    /* send data packet */
    bsock->timer_start = watchdog_time; /* start timer */
    bsock->timed_out = 0;              
-   rc = write_nbytes(bsock, bsock->msg, bsock->msglen);
+   rc = write_nbytes(bsock, mp_chr(bsock->msg), bsock->msglen);
    bsock->timer_start = 0;           /* clear timer */
    if (rc != bsock->msglen) {
       bsock->errors++;
@@ -341,13 +377,39 @@ bnet_send(BSOCK *bsock)
         }
       } else {
          Jmsg5(bsock->jcr, M_ERROR, 0, _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"), 
-              bsock->who, bsock->host, bsock->port, bsock->msglen, rc);
+              bsock->msglen, bsock->who, bsock->host, bsock->port, rc);
       }
       return 0;
    }
+   ASSERT(msglen == bsock->msglen);
+   return 1;
+}
+
+/*
+ * Establish an SSL connection -- server side        
+ *  Codes that ssl_need and ssl_has can take
+ *    BNET_SSL_NONE     I cannot do ssl
+ *    BNET_SSL_OK       I can do ssl, but it is not required on my end
+ *    BNET_SSL_REQUIRED  ssl is required on my end
+ */
+int    
+bnet_ssl_server(BSOCK *bsock, char *password, int ssl_need, int ssl_has)
+{
+   /* Check to see if what we need (ssl_need) corresponds to what he has (ssl_has) */
+   /* The other side expects a response from us */
    return 1;
 }
 
+/*
+ * Establish an SSL connection -- client side  
+ */
+int bnet_ssl_client(BSOCK *bsock, char *password, int ssl_need)
+{
+   /* We are the client so we must wait for the server to notify us */
+   return 1;
+}
+
+
 /*
  * Wait for a specified time for data to appear on
  * the BSOCK connection.
@@ -357,7 +419,7 @@ bnet_send(BSOCK *bsock)
  *          -1 if error
  */
 int 
-bnet_wait_data(BSOCK *bsock, int sec)
+bnet_wait_data(BSOCK *bsock, int sec)         
 {
    fd_set fdset;
    struct timeval tv;
@@ -368,29 +430,102 @@ bnet_wait_data(BSOCK *bsock, int sec)
    tv.tv_usec = 0;
    for ( ;; ) {
       switch(select(bsock->fd + 1, &fdset, NULL, NULL, &tv)) {
-        case 0:                         /* timeout */
-           bsock->b_errno = 0;
-           return 0;
-        case -1:
-           bsock->b_errno = errno;
-           if (errno == EINTR || errno == EAGAIN) {
-              continue;
-           }
-           return -1;                  /* error return */
-        default:
-           bsock->b_errno = 0;
-           return 1;
+      case 0:                        /* timeout */
+        bsock->b_errno = 0;
+        return 0;
+      case -1:
+        bsock->b_errno = errno;
+        if (errno == EINTR || errno == EAGAIN) {
+           continue;
+        }
+        return -1;                  /* error return */
+      default:
+        bsock->b_errno = 0;
+        return 1;
+      }
+   }
+}
+
+/*
+ * As above, but returns on interrupt
+ */
+int
+bnet_wait_data_intr(BSOCK *bsock, int sec)         
+{
+   fd_set fdset;
+   struct timeval tv;
+
+   FD_ZERO(&fdset);
+   FD_SET(bsock->fd, &fdset);
+   tv.tv_sec = sec;
+   tv.tv_usec = 0;
+   for ( ;; ) {
+      switch(select(bsock->fd + 1, &fdset, NULL, NULL, &tv)) {
+      case 0:                        /* timeout */
+        bsock->b_errno = 0;
+        return 0;
+      case -1:
+        bsock->b_errno = errno;
+        return -1;                  /* error return */
+      default:
+        bsock->b_errno = 0;
+        return 1;
       }
    }
 }
 
+#ifndef NETDB_INTERNAL
+#define NETDB_INTERNAL -1      /* See errno. */
+#endif
+#ifndef NETDB_SUCCESS
+#define NETDB_SUCCESS  0       /* No problem. */
+#endif
+#ifndef HOST_NOT_FOUND
+#define HOST_NOT_FOUND 1       /* Authoritative Answer Host not found. */
+#endif
+#ifndef TRY_AGAIN
+#define TRY_AGAIN      2       /* Non-Authoritative Host not found, or SERVERFAIL. */
+#endif
+#ifndef NO_RECOVERY
+#define NO_RECOVERY    3       /* Non recoverable errors, FORMERR, REFUSED, NOTIMP. */
+#endif
+#ifndef NO_DATA
+#define NO_DATA        4       /* Valid name, no data record of requested type. */
+#endif
+
+extern int h_errno;            /* On error has one of the above */
+
+/*
+ * Get human readable error for gethostbyname()
+ */
+static char *gethost_strerror() 
+{
+   switch (h_errno) {
+   case NETDB_INTERNAL:
+      return strerror(errno);
+   case NETDB_SUCCESS:
+      return "No problem.";
+   case HOST_NOT_FOUND:
+      return "Authoritative answer Host not found.";
+   case TRY_AGAIN:
+      return "Non-authoritative Host not found, or ServerFail.";
+   case NO_RECOVERY:
+      return "Non-recoverable errors, FORMERR, REFUSED, or NOTIMP.";
+   case NO_DATA:
+      return "Valid name, no data record of resquested type.";
+   default:
+      return "Unknown error.";
+   }
+}
+
+
 static pthread_mutex_t ip_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /*
  * Convert a hostname or dotted IP address into   
  * a s_addr.  We handle only IPv4.
  */
-static uint32_t *bget_host_ip(void *jcr, char *host)
+static uint32_t *bget_host_ip(JCR *jcr, char *host)
 {
    struct in_addr inaddr;
    uint32_t *addr_list;              /* this really should be struct in_addr */
@@ -406,7 +541,7 @@ static uint32_t *bget_host_ip(void *jcr, char *host)
       P(ip_mutex);
       if ((hp = gethostbyname(host)) == NULL) {
          Jmsg2(jcr, M_ERROR, 0, "gethostbyname() for %s failed: ERR=%s\n", 
-              host, strerror(errno));
+              host, gethost_strerror());
         V(ip_mutex);
         return NULL;
       }
@@ -421,7 +556,7 @@ Wanted %d got %d bytes for s_addr.\n"), sizeof(inaddr.s_addr), hp->h_length);
         i++;
       }
       i++;
-      addr_list = (uint32_t *) malloc(sizeof(uint32_t) * i);
+      addr_list = (uint32_t *)malloc(sizeof(uint32_t) * i);
       i = 0;
       for (p = hp->h_addr_list; *p != 0; p++) {
         addr_list[i++] = (*(struct in_addr **)p)->s_addr;
@@ -440,7 +575,7 @@ Wanted %d got %d bytes for s_addr.\n"), sizeof(inaddr.s_addr), hp->h_length);
  *  ***FIXME*** implement service from /etc/services
  */
 static BSOCK *
-bnet_open(void *jcr, char *name, char *host, char *service, int port, int *fatal)
+bnet_open(JCR *jcr, char *name, char *host, char *service, int port, int *fatal)
 {
    int sockfd;
    struct sockaddr_in tcp_serv_addr;    /* socket information */
@@ -497,16 +632,15 @@ bnet_open(void *jcr, char *name, char *host, char *service, int port, int *fatal
  * Try to connect to host for max_retry_time at retry_time intervals.
  */
 BSOCK *
-bnet_connect(void *vjcr, int retry_interval, int max_retry_time, char *name,
+bnet_connect(JCR *jcr, int retry_interval, int max_retry_time, char *name,
             char *host, char *service, int port, int verbose)
 {
    int i;
    BSOCK *bsock;
-   JCR *jcr = (JCR *)vjcr;
    int fatal = 0;
 
    for (i=0; (bsock = bnet_open(jcr, name, host, service, port, &fatal)) == NULL; i -= retry_interval) {
-     if (fatal || (jcr && job_cancelled(jcr))) {
+     if (fatal || (jcr && job_canceled(jcr))) {
        return NULL;
      }
      Dmsg4(100, "Unable to connect to %s on %s:%d. ERR=%s\n",
@@ -517,7 +651,7 @@ bnet_connect(void *vjcr, int retry_interval, int max_retry_time, char *name,
             Jmsg(jcr, M_WARNING, 0, "Could not connect to %s on %s:%d. ERR=%s\n\
 Retrying ...\n", name, host, port, strerror(errno));
       }
-      sleep(retry_interval);
+      bmicrosleep(retry_interval, 0);
       max_retry_time -= retry_interval;
       if (max_retry_time <= 0) {
          Jmsg(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
@@ -549,6 +683,9 @@ bnet_fsend(BSOCK *bs, char *fmt, ...)
    va_list arg_ptr;
    int maxlen;
 
+   if (bs->errors || bs->terminated) {
+      return 0;
+   }
    /* This probably won't work, but we vsnprintf, then if we
     * get a negative length or a length greater than our buffer
     * (depending on which library is used), the printf was truncated, so
@@ -557,13 +694,13 @@ bnet_fsend(BSOCK *bs, char *fmt, ...)
 again:
    maxlen = sizeof_pool_memory(bs->msg) - 1;
    va_start(arg_ptr, fmt);
-   bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
+   bs->msglen = bvsnprintf(mp_chr(bs->msg), maxlen, fmt, arg_ptr);
    va_end(arg_ptr);
    if (bs->msglen < 0 || bs->msglen >= maxlen) {
-      bs->msg = realloc_pool_memory(bs->msg, maxlen + 200);
+      bs->msg = realloc_pool_memory(bs->msg, maxlen + maxlen / 2);
       goto again;
    }
-   return bnet_send(bs) < 0 ? 0 : 1;
+   return bnet_send(bs);
 }
 
 /* 
@@ -670,7 +807,7 @@ char *bnet_sig_to_ascii(BSOCK *bs)
  *  This probably should be done in net_open
  */
 BSOCK *
-init_bsock(void *jcr, int sockfd, char *who, char *host, int port) 
+init_bsock(JCR *jcr, int sockfd, char *who, char *host, int port) 
 {
    BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memset(bsock, 0, sizeof(BSOCK));
@@ -693,10 +830,16 @@ init_bsock(void *jcr, int sockfd, char *who, char *host, int port)
 BSOCK *
 dup_bsock(BSOCK *osock)
 {
-   BSOCK *bsock = (BSOCK *) malloc(sizeof(BSOCK));
+   BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
    memcpy(bsock, osock, sizeof(BSOCK));
    bsock->msg = get_pool_memory(PM_MESSAGE);
    bsock->errmsg = get_pool_memory(PM_MESSAGE);
+   if (osock->who) {
+      bsock->who = bstrdup(osock->who);
+   }
+   if (osock->host) {
+      bsock->host = bstrdup(osock->host);
+   }
    bsock->duped = TRUE;
    return bsock;
 }
@@ -710,12 +853,12 @@ bnet_close(BSOCK *bsock)
    for ( ; bsock != NULL; bsock = next) {
       next = bsock->next;
       if (!bsock->duped) {
-//      shutdown(bsock->fd, SHUT_RDWR);
-        close(bsock->fd);
-        term_bsock(bsock);
-      } else {
-        free(bsock);
+        if (bsock->timed_out) {
+           shutdown(bsock->fd, 2);   /* discard any pending I/O */
+        }
+        close(bsock->fd);         /* normal close */
       }
+      term_bsock(bsock);
    }
    return;
 }