]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bnet.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / lib / bnet.c
index ac65544ba82cdc4a1de78e5eef48b4e84536e3cd..f0e21a628d77fbf46638358d5bf3ff71057f663f 100644 (file)
@@ -3,7 +3,7 @@
  *
  *  by Kern Sibbald
  *
- * Adapted and enhanced for Bacula, originally written 
+ * Adapted and enhanced for Bacula, originally written
  * for inclusion in the Apcupsd package
  *
  *   Version $Id$
@@ -54,6 +54,19 @@ extern time_t watchdog_time;
 #endif
 
 
+#ifdef HAVE_OLD_SOCKOPT
+int inet_aton(const char *cp, struct in_addr *inp)
+{
+   struct in_addr inaddr;
+
+   if((inaddr.s_addr = inet_addr(cp)) != INADDR_NONE) {
+      inp->s_addr = inaddr.s_addr;
+      return 1;
+   }
+   return 0;
+}
+#endif
+
 /*
  * Read a nbytes from the network.
  * It is possible that the total bytes require in several
@@ -95,9 +108,11 @@ static int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
       nwritten = fwrite(ptr, 1, nbytes, bsock->spool_fd);
       if (nwritten != nbytes) {
         berrno be;
-         Qmsg1(bsock->jcr, M_FATAL, 0, _("Attr spool write error. ERR=%s\n"),
+        bsock->b_errno = errno;
+        Qmsg1(bsock->jcr, M_FATAL, 0, _("Attr spool write error. ERR=%s\n"),
               be.strerror());
-         Dmsg2(400, "nwritten=%d nbytes=%d.\n", nwritten, nbytes);
+        Dmsg2(400, "nwritten=%d nbytes=%d.\n", nwritten, nbytes);
+        errno = bsock->b_errno;
         return -1;
       }
       return nbytes;
@@ -136,12 +151,12 @@ static int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
    return nbytes - nleft;
 }
 
-/* 
+/*
  * Receive a message from the other end. Each message consists of
  * two packets. The first is a header that contains the size
  * of the data that follows in the second packet.
  * Returns number of bytes read (may return zero)
- * Returns -1 on signal (BNET_SIGNAL) 
+ * Returns -1 on signal (BNET_SIGNAL)
  * Returns -2 on hard end of file (BNET_HARDEOF)
  * Returns -3 on error (BNET_ERROR)
  *
@@ -149,7 +164,7 @@ static int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes)
  *    four return types:
  *    1. Normal data
  *    2. Signal including end of data stream
- *    3. Hard end of file                
+ *    3. Hard end of file
  *    4. Error
  *  Using is_bnet_stop() and is_bnet_error() you can figure this all out.
  */
@@ -159,7 +174,8 @@ int32_t bnet_recv(BSOCK * bsock)
    int32_t pktsiz;
 
    ASSERT(bsock != NULL);
-   mp_chr(bsock->msg)[0] = 0;
+   bsock->msg[0] = 0;
+   bsock->msglen = 0;
    if (bsock->errors || bsock->terminated) {
       return BNET_HARDEOF;
    }
@@ -201,7 +217,7 @@ int32_t bnet_recv(BSOCK * bsock)
    if (pktsiz < 0 || pktsiz > 1000000) {
       if (pktsiz > 0) {           /* if packet too big */
         Qmsg3(bsock->jcr, M_FATAL, 0,
-               _("Packet size too big from \"%s:%s:%d. Terminating connection.\n"),
+              _("Packet size too big from \"%s:%s:%d. Terminating connection.\n"),
               bsock->who, bsock->host, bsock->port);
         pktsiz = BNET_TERMINATE;  /* hang up */
       }
@@ -222,7 +238,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, mp_chr(bsock->msg), pktsiz)) <= 0) {
+   if ((nbytes = read_nbytes(bsock, bsock->msg, pktsiz)) <= 0) {
       bsock->timer_start = 0;     /* clear timer */
       if (errno == 0) {
         bsock->b_errno = ENODATA;
@@ -248,14 +264,14 @@ 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.
     */
-   mp_chr(bsock->msg)[nbytes] = 0; /* terminate in case it is a string */
+   bsock->msg[nbytes] = 0; /* terminate in case it is a string */
    sm_check(__FILE__, __LINE__, false);
    return nbytes;                 /* return actual length of message */
 }
 
 
 /*
- * Return 1 if there are errors on this bsock or it is closed, 
+ * Return 1 if there are errors on this bsock or it is closed,
  *   i.e. stop communicating on this line.
  */
 bool is_bnet_stop(BSOCK * bsock)
@@ -264,7 +280,7 @@ bool is_bnet_stop(BSOCK * bsock)
 }
 
 /*
- * Return number of errors on socket 
+ * Return number of errors on socket
  */
 int is_bnet_error(BSOCK * bsock)
 {
@@ -304,8 +320,8 @@ int bnet_despool_to_bsock(BSOCK * bsock, void update_attr_spool_size(ssize_t siz
         nbytes = fread(bsock->msg, 1, bsock->msglen, bsock->spool_fd);
         if (nbytes != (size_t) bsock->msglen) {
            berrno be;
-            Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, bsock->msglen);
-            Qmsg1(bsock->jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
+           Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, bsock->msglen);
+           Qmsg1(bsock->jcr, M_FATAL, 0, _("fread attr spool error. ERR=%s\n"),
                  be.strerror());
            update_attr_spool_size(tsize - last);
            return 0;
@@ -334,8 +350,8 @@ int bnet_despool_to_bsock(BSOCK * bsock, void update_attr_spool_size(ssize_t siz
  * two network packets. The first is sends a 32 bit integer containing
  * the length of the data packet which follows.
  *
- * Returns: 0 on failure
- *         1 on success
+ * Returns: false on failure
+ *         true  on success
  */
 bool bnet_send(BSOCK * bsock)
 {
@@ -365,12 +381,12 @@ bool bnet_send(BSOCK * bsock)
       if (rc < 0) {
         if (!bsock->suppress_error_msgs && !bsock->timed_out) {
            Qmsg4(bsock->jcr, M_ERROR, 0,
-                  _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
+                 _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
                  bsock->host, bsock->port, bnet_strerror(bsock));
         }
       } else {
         Qmsg5(bsock->jcr, M_ERROR, 0,
-               _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"), bsock->who,
+              _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"), bsock->who,
               bsock->host, bsock->port, bsock->msglen, rc);
       }
       return false;
@@ -384,7 +400,7 @@ bool bnet_send(BSOCK * bsock)
    /* send data packet */
    bsock->timer_start = watchdog_time; /* start timer */
    bsock->timed_out = 0;
-   rc = write_nbytes(bsock, mp_chr(bsock->msg), bsock->msglen);
+   rc = write_nbytes(bsock, bsock->msg, bsock->msglen);
    bsock->timer_start = 0;        /* clear timer */
    if (rc != bsock->msglen) {
       bsock->errors++;
@@ -396,12 +412,12 @@ bool bnet_send(BSOCK * bsock)
       if (rc < 0) {
         if (!bsock->suppress_error_msgs) {
            Qmsg4(bsock->jcr, M_ERROR, 0,
-                  _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
+                 _("Write error sending to %s:%s:%d: ERR=%s\n"), bsock->who,
                  bsock->host, bsock->port, bnet_strerror(bsock));
         }
       } else {
         Qmsg5(bsock->jcr, M_ERROR, 0,
-               _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"),
+              _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"),
               bsock->msglen, bsock->who, bsock->host, bsock->port, rc);
       }
       return false;
@@ -410,7 +426,7 @@ bool bnet_send(BSOCK * bsock)
 }
 
 /*
- * Establish an SSL connection -- server side        
+ * 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
@@ -424,7 +440,7 @@ int bnet_ssl_server(BSOCK * bsock, char *password, int ssl_need, int ssl_has)
 }
 
 /*
- * Establish an SSL connection -- client side  
+ * Establish an SSL connection -- client side
  */
 int bnet_ssl_client(BSOCK * bsock, char *password, int ssl_need)
 {
@@ -653,7 +669,7 @@ dlist *bnet_host2ipaddrs(const char *host, int family, const char **errstr)
    return addr_list;
 }
 
-/*     
+/*
  * Open a TCP connection to the UPS network server
  * Returns NULL
  * Returns BSOCK * pointer on success
@@ -670,7 +686,7 @@ static BSOCK *bnet_open(JCR * jcr, const char *name, char *host, char *service,
    const char *errstr;
    int save_errno = 0;
 
-   /* 
+   /*
     * Fill in the structure serv_addr with the address of
     * the server that we want to connect with.
     */
@@ -688,25 +704,24 @@ static BSOCK *bnet_open(JCR * jcr, const char *name, char *host, char *service,
       ipaddr->set_port_net(htons(port));
       char allbuf[256 * 10];
       char curbuf[256];
-      Dmsg2(100, "Current %sAll %s\n", 
-                  ipaddr->build_address_str(curbuf, sizeof(curbuf)), 
+      Dmsg2(100, "Current %sAll %s\n",
+                  ipaddr->build_address_str(curbuf, sizeof(curbuf)),
                   build_addresses_str(addr_list, allbuf, sizeof(allbuf)));
       /* Open a TCP socket */
       if ((sockfd = socket(ipaddr->get_family(), SOCK_STREAM, 0)) < 0) {
         berrno be;
         save_errno = errno;
         *fatal = 1;
-         Pmsg3(000, "Socket open error. proto=%d port=%d. ERR=%s\n", 
+        Pmsg3(000, "Socket open error. proto=%d port=%d. ERR=%s\n",
            ipaddr->get_family(), ipaddr->get_port_host_order(), be.strerror());
         continue;
       }
       /*
-       * Receive notification when connection dies.
+       * Keep socket from timing out from inactivity
        */
-      if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t) & turnon,
-          sizeof(turnon)) < 0) {
+      if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
         berrno be;
-         Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
+        Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
               be.strerror());
       }
       /* connect to server */
@@ -725,6 +740,15 @@ static BSOCK *bnet_open(JCR * jcr, const char *name, char *host, char *service,
       errno = save_errno;
       return NULL;
    }
+   /*
+    * Keep socket from timing out from inactivity
+    *  Do this a second time out of paranoia
+    */
+   if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
+      berrno be;
+      Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
+           be.strerror());
+   }
    BSOCK* ret =  init_bsock(jcr, sockfd, name, host, port, ipaddr->get_sockaddr());
    free_addresses(addr_list);
    return ret;
@@ -752,13 +776,13 @@ BSOCK *bnet_connect(JCR * jcr, int retry_interval, int max_retry_time,
       if (i < 0) {
         i = 60 * 5;               /* complain again in 5 minutes */
         if (verbose)
-            Qmsg4(jcr, M_WARNING, 0, "Could not connect to %s on %s:%d. ERR=%s\n\
-Retrying ...\n", name, host, port, be.strerror());
+           Qmsg4(jcr, M_WARNING, 0, "Could not connect to %s on %s:%d. ERR=%s\n"
+"Retrying ...\n", name, host, port, be.strerror());
       }
       bmicrosleep(retry_interval, 0);
       max_retry_time -= retry_interval;
       if (max_retry_time <= 0) {
-         Qmsg4(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
+        Qmsg4(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
               name, host, port, be.strerror());
         return NULL;
       }
@@ -771,9 +795,14 @@ Retrying ...\n", name, host, port, be.strerror());
  * Return the string for the error that occurred
  * on the socket. Only the first error is retained.
  */
-char *bnet_strerror(BSOCK * bsock)
+const char *bnet_strerror(BSOCK * bsock)
 {
-   return strerror(bsock->b_errno);
+   berrno be;
+   if (bsock->errmsg == NULL) {
+      bsock->errmsg = get_pool_memory(PM_MESSAGE);
+   }
+   pm_strcpy(bsock->errmsg, be.strerror(bsock->b_errno));
+   return bsock->errmsg;
 }
 
 /*
@@ -797,7 +826,7 @@ bool bnet_fsend(BSOCK * bs, const char *fmt, ...)
    for (;;) {
       maxlen = sizeof_pool_memory(bs->msg) - 1;
       va_start(arg_ptr, fmt);
-      bs->msglen = bvsnprintf(mp_chr(bs->msg), maxlen, fmt, arg_ptr);
+      bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
       va_end(arg_ptr);
       if (bs->msglen > 0 && bs->msglen < (maxlen - 5)) {
         break;
@@ -807,7 +836,7 @@ bool bnet_fsend(BSOCK * bs, const char *fmt, ...)
    return bnet_send(bs);
 }
 
-/* 
+/*
  * Set the network buffer size, suggested size is in size.
  *  Actual size obtained is returned in bs->msglen
  *
@@ -835,20 +864,20 @@ bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
       return false;
    }
    if (rw & BNET_SETBUF_READ) {
-      while ((dbuf_size > TAPE_BSIZE) && (setsockopt(bs->fd, SOL_SOCKET, 
+      while ((dbuf_size > TAPE_BSIZE) && (setsockopt(bs->fd, SOL_SOCKET,
              SO_RCVBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
         berrno be;
-         Qmsg1(bs->jcr, M_ERROR, 0, _("sockopt error: %s\n"), be.strerror());
+        Qmsg1(bs->jcr, M_ERROR, 0, _("sockopt error: %s\n"), be.strerror());
         dbuf_size -= TAPE_BSIZE;
       }
       Dmsg1(200, "set network buffer size=%d\n", dbuf_size);
       if (dbuf_size != start_size) {
         Qmsg1(bs->jcr, M_WARNING, 0,
-               _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
+              _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
       }
       if (dbuf_size % TAPE_BSIZE != 0) {
         Qmsg1(bs->jcr, M_ABORT, 0,
-               _("Network buffer size %d not multiple of tape block size.\n"),
+              _("Network buffer size %d not multiple of tape block size.\n"),
               dbuf_size);
       }
    }
@@ -859,20 +888,20 @@ bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
    }
    start_size = dbuf_size;
    if (rw & BNET_SETBUF_WRITE) {
-      while ((dbuf_size > TAPE_BSIZE) && (setsockopt(bs->fd, SOL_SOCKET, 
+      while ((dbuf_size > TAPE_BSIZE) && (setsockopt(bs->fd, SOL_SOCKET,
              SO_SNDBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
         berrno be;
-         Qmsg1(bs->jcr, M_ERROR, 0, _("sockopt error: %s\n"), be.strerror());
+        Qmsg1(bs->jcr, M_ERROR, 0, _("sockopt error: %s\n"), be.strerror());
         dbuf_size -= TAPE_BSIZE;
       }
       Dmsg1(200, "set network buffer size=%d\n", dbuf_size);
       if (dbuf_size != start_size) {
         Qmsg1(bs->jcr, M_WARNING, 0,
-               _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
+              _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
       }
       if (dbuf_size % TAPE_BSIZE != 0) {
         Qmsg1(bs->jcr, M_ABORT, 0,
-               _("Network buffer size %d not multiple of tape block size.\n"),
+              _("Network buffer size %d not multiple of tape block size.\n"),
               dbuf_size);
       }
    }
@@ -882,7 +911,7 @@ bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
 }
 
 /*
- * Send a network "signal" to the other end 
+ * Send a network "signal" to the other end
  *  This consists of sending a negative packet length
  *
  *  Returns: false on failure
@@ -925,7 +954,7 @@ const char *bnet_sig_to_ascii(BSOCK * bs)
 }
 
 
-/* Initialize internal socket structure.  
+/* Initialize internal socket structure.
  *  This probably should be done in net_open
  */
 BSOCK *init_bsock(JCR * jcr, int sockfd, const char *who, const char *host, int port,
@@ -943,7 +972,7 @@ BSOCK *init_bsock(JCR * jcr, int sockfd, const char *who, const char *host, int
    bsock->port = port;
    memcpy(&bsock->client_addr, client_addr, sizeof(bsock->client_addr));
    /*
-    * ****FIXME**** reduce this to a few hours once   
+    * ****FIXME**** reduce this to a few hours once
     *  heartbeats are implemented
     */
    bsock->timeout = 60 * 60 * 6 * 24;  /* 6 days timeout */