2 * Network Utility Routines
6 * Adapted and enhanced for Bacula, originally written
7 * for inclusion in the Apcupsd package
12 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public
25 License along with this program; if not, write to the Free
26 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 extern time_t watchdog_time;
37 #define INADDR_NONE -1
40 #ifndef ENODATA /* not defined on BSD systems */
46 * Read a nbytes from the network.
47 * It is possible that the total bytes require in several
51 static int32_t read_nbytes(BSOCK *bsock, char *ptr, int32_t nbytes)
59 nread = read(bsock->fd, ptr, nleft);
60 } while (!bsock->timed_out && nread == -1 && (errno == EINTR || errno == EAGAIN));
62 return nread; /* error, or EOF */
67 return nbytes - nleft; /* return >= 0 */
71 * Write nbytes to the network.
72 * It may require several writes.
75 static int32_t write_nbytes(BSOCK *bsock, char *ptr, int32_t nbytes)
77 int32_t nleft, nwritten;
83 nwritten = write(bsock->fd, ptr, nleft);
84 } while (!bsock->timed_out && nwritten == -1 && (errno == EINTR || errno == EAGAIN));
86 return nwritten; /* error */
95 * Receive a message from the other end. Each message consists of
96 * two packets. The first is a header that contains the size
97 * of the data that follows in the second packet.
98 * Returns number of bytes read
99 * Returns 0 on end of file
100 * Returns -1 on hard end of file (i.e. network connection close)
101 * Returns -2 on error
105 bnet_recv(BSOCK *bsock)
114 bsock->read_seqno++; /* bump sequence number */
115 bsock->timer_start = watchdog_time; /* set start wait time */
116 bsock->timed_out = 0;
117 /* get data size -- in int32_t */
118 if ((nbytes = read_nbytes(bsock, (char *)&pktsiz, sizeof(int32_t))) <= 0) {
119 bsock->timer_start = 0; /* clear timer */
120 /* probably pipe broken because client died */
122 bsock->b_errno = ENODATA;
124 bsock->b_errno = errno;
127 return -1; /* assume hard EOF received */
129 bsock->timer_start = 0; /* clear timer */
130 if (nbytes != sizeof(int32_t)) {
132 bsock->b_errno = EIO;
133 Emsg3(M_ERROR, 0, "Read %d expected %d from %s\n", nbytes, sizeof(int32_t),
138 pktsiz = ntohl(pktsiz); /* decode no. of bytes that follow */
141 bsock->b_errno = ENODATA;
142 bsock->msglen = pktsiz; /* return size */
143 return 0; /* soft EOF */
145 /* For big packet size, something went wrong */
146 if (pktsiz > 10000000) {
147 bsock->b_errno = ENODATA;
148 bsock->msglen = pktsiz; /* return size */
149 return 0; /* soft EOF */
152 /* Make sure the buffer is big enough + one byte for EOS */
153 if (pktsiz >= (int32_t)sizeof_pool_memory(bsock->msg)) {
154 bsock->msg = realloc_pool_memory(bsock->msg, pktsiz + 100);
157 bsock->timer_start = watchdog_time; /* set start wait time */
158 bsock->timed_out = 0;
159 /* now read the actual data */
160 if ((nbytes = read_nbytes(bsock, bsock->msg, pktsiz)) <= 0) {
161 bsock->timer_start = 0; /* clear timer */
163 bsock->b_errno = ENODATA;
165 bsock->b_errno = errno;
168 Emsg4(M_ERROR, 0, "Read error from %s:%s:%d: ERR=%s\n",
169 bsock->who, bsock->host, bsock->port, bnet_strerror(bsock));
172 bsock->timer_start = 0; /* clear timer */
174 bsock->msglen = nbytes;
175 if (nbytes != pktsiz) {
176 bsock->b_errno = EIO;
178 Emsg5(M_ERROR, 0, "Read expected %d got %d from %s:%s:%d\n", pktsiz, nbytes,
179 bsock->who, bsock->host, bsock->port);
182 /* always add a zero by to properly terminate any
183 * string that was send to us. Note, we ensured above that the
184 * buffer is at least one byte longer than the message length.
186 bsock->msg[nbytes] = 0; /* terminate in case it is a string */
187 sm_check(__FILE__, __LINE__, False);
188 return nbytes; /* return actual length of message */
192 * Send a message over the network. The send consists of
193 * two network packets. The first is sends a 32 bit integer containing
194 * the length of the data packet which follows.
196 * Returns: 0 on failure
200 bnet_send(BSOCK *bsock)
208 pktsiz = htonl((int32_t)bsock->msglen);
209 /* send int32_t containing size of data packet */
210 bsock->timer_start = watchdog_time; /* start timer */
211 bsock->timed_out = 0;
212 rc = write_nbytes(bsock, (char *)&pktsiz, sizeof(int32_t));
213 bsock->timer_start = 0; /* clear timer */
214 if (rc != sizeof(int32_t)) {
217 bsock->b_errno = EIO;
219 bsock->b_errno = errno;
222 /****FIXME***** use Mmsg */
223 Emsg4(M_ERROR, 0, "Write error sending to %s:%s:%d: ERR=%s\n",
224 bsock->who, bsock->host, bsock->port, bnet_strerror(bsock));
226 Emsg5(M_ERROR, 0, "Wrote %d bytes to %s:%s:%d, but only %d accepted.\n",
227 bsock->who, bsock->host, bsock->port, bsock->msglen, rc);
232 bsock->out_msg_no++; /* increment message number */
233 if (bsock->msglen <= 0) { /* length only? */
234 return 1; /* yes, no data */
237 /* send data packet */
238 bsock->timer_start = watchdog_time; /* start timer */
239 bsock->timed_out = 0;
240 rc = write_nbytes(bsock, bsock->msg, bsock->msglen);
241 bsock->timer_start = 0; /* clear timer */
242 if (rc != bsock->msglen) {
245 bsock->b_errno = EIO;
247 bsock->b_errno = errno;
250 /************FIXME********* use Pmsg() **/
251 Emsg4(M_ERROR, 0, "Write error sending to %s:%s:%d: ERR=%s\n",
252 bsock->who, bsock->host, bsock->port, bnet_strerror(bsock));
254 Emsg5(M_ERROR, 0, "Wrote %d bytes to %s:%s:%d, but only %d accepted.\n",
255 bsock->who, bsock->host, bsock->port, bsock->msglen, rc);
263 * Wait for a specified time for data to appear on
264 * the BSOCK connection.
266 * Returns: 1 if data available
271 bnet_wait_data(BSOCK *bsock, int sec)
277 FD_SET(bsock->fd, &fdset);
281 switch(select(bsock->fd + 1, &fdset, NULL, NULL, &tv)) {
282 case 0: /* timeout */
286 bsock->b_errno = errno;
287 if (errno == EINTR || errno == EAGAIN) {
290 return -1; /* error return */
299 * Convert a hostname or dotted IP address into
300 * a s_addr. We handle only IPv4.
302 static uint32_t *bget_host_ip(char *host)
304 struct in_addr inaddr;
305 uint32_t *addr_list; /* this really should be struct in_addr */
310 if ((inaddr.s_addr = inet_addr(host)) != INADDR_NONE) {
311 addr_list = (uint32_t *) malloc(sizeof(uint32_t) * 2);
312 addr_list[0] = inaddr.s_addr;
313 addr_list[1] = (uint32_t) -1;
315 /******FIXME***** use gethostbyname_r or mutex ****/
316 if ((hp = gethostbyname(host)) == NULL) {
317 Dmsg2(0, "gethostbyname() for %s failed: ERR=%s\n", host, strerror(errno));
320 if (hp->h_length != sizeof(inaddr.s_addr) || hp->h_addrtype != AF_INET) {
321 Emsg2(M_WARNING, 0, "gethostbyname() network address length error.\n\
322 Wanted %d got %d bytes for s_addr.\n", sizeof(inaddr.s_addr), hp->h_length);
326 for (p = hp->h_addr_list; *p != 0; p++) {
330 addr_list = (uint32_t *) malloc(sizeof(uint32_t) * i);
332 for (p = hp->h_addr_list; *p != 0; p++) {
333 addr_list[i++] = (*(struct in_addr **)p)->s_addr;
335 addr_list[i] = (uint32_t) -1;
341 * Open a TCP connection to the UPS network server
343 * Returns BSOCK * pointer on success
345 * ***FIXME*** implement service from /etc/services
348 bnet_open(char *name, char *host, char *service, int port)
351 struct sockaddr_in tcp_serv_addr; /* socket information */
353 int i, connected = 0;
357 * Fill in the structure serv_addr with the address of
358 * the server that we want to connect with.
360 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
361 tcp_serv_addr.sin_family = AF_INET;
362 tcp_serv_addr.sin_port = htons(port);
364 if ((addr_list=bget_host_ip(host)) == NULL) {
368 /* Open a TCP socket */
369 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
375 * Receive notification when connection dies.
377 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
378 Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
381 for (i = 0; addr_list[i] != ((uint32_t) -1); i++) {
382 /* connect to server */
383 tcp_serv_addr.sin_addr.s_addr = addr_list[i];
384 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
396 return init_bsock(sockfd, name, host, port);
400 * Try to connect to host for max_retry_time at retry_time intervals.
403 bnet_connect(void *jcr, int retry_interval, int max_retry_time, char *name,
404 char *host, char *service, int port, int verbose)
409 for (i=0; (bsock = bnet_open(name, host, service, port)) == NULL; i -= retry_interval) {
410 Dmsg4(100, "Unable to connect to %s on %s:%d. ERR=%s\n",
411 name, host, port, strerror(errno));
413 i = 60 * 5; /* complain again in 5 minutes */
415 Jmsg(jcr, M_WARNING, 0, "Could not connect to %s on %s:%d. ERR=%s\n\
416 Retrying ...\n", name, host, port, strerror(errno));
418 sleep(retry_interval);
419 max_retry_time -= retry_interval;
420 if (max_retry_time <= 0) {
421 Jmsg(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
422 name, host, port, strerror(errno));
431 * Return the string for the error that occurred
432 * on the socket. Only the first error is retained.
434 char *bnet_strerror(BSOCK *bsock)
436 /* ***FIXME*** not thread safe */
437 return strerror(bsock->b_errno);
441 * Format and send a message
442 * Returns: 0 on failure
446 bnet_fsend(BSOCK *bs, char *fmt, ...)
451 /* This probably won't work, but we vsnprintf, then if we
452 * get a negative length or a length greater than our buffer
453 * (depending on which library is used), the printf was truncated, so
454 * get a biger buffer and try again.
457 maxlen = sizeof_pool_memory(bs->msg) - 1;
458 va_start(arg_ptr, fmt);
459 bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
461 if (bs->msglen < 0 || bs->msglen >= maxlen) {
462 bs->msg = realloc_pool_memory(bs->msg, maxlen + 200);
465 return bnet_send(bs) < 0 ? 0 : 1;
469 * Set the network buffer size, suggested size is in size.
470 * Actual size obtained is returned in bs->msglen
472 * Returns: 0 on failure
475 int bnet_set_buffer_size(BSOCK *bs, uint32_t size, int rw)
478 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
481 opt = IPTOS_THROUGHPUT;
482 setsockopt(bs->fd, IPPROTO_IP, IP_TOS, (char *)&opt, sizeof(opt));
486 if ((bs->msg = realloc_pool_memory(bs->msg, dbuf_size+100)) == NULL) {
487 Emsg0(M_FATAL, 0, "Could not malloc 32K BSOCK data buffer\n");
490 if (rw & BNET_SETBUF_READ) {
491 while ((dbuf_size > TAPE_BSIZE) &&
492 (setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (char *)&dbuf_size, sizeof(dbuf_size)) < 0)) {
493 Emsg1(M_ERROR, 0, "sockopt error: %s\n", strerror(errno));
494 dbuf_size -= TAPE_BSIZE;
496 Dmsg1(20, "set network buffer size=%d\n", dbuf_size);
497 if (dbuf_size != MAX_NETWORK_BUFFER_SIZE)
498 Emsg1(M_WARNING, 0, "Warning network buffer = %d bytes not max size.\n", dbuf_size);
499 if (dbuf_size % TAPE_BSIZE != 0) {
500 Emsg1(M_ABORT, 0, "Network buffer size %d not multiple of tape block size.\n",
505 if (rw & BNET_SETBUF_WRITE) {
506 while ((dbuf_size > TAPE_BSIZE) &&
507 (setsockopt(bs->fd, SOL_SOCKET, SO_SNDBUF, (char *)&dbuf_size, sizeof(dbuf_size)) < 0)) {
508 Emsg1(M_ERROR, 0, "sockopt error: %s\n", strerror(errno));
509 dbuf_size -= TAPE_BSIZE;
511 Dmsg1(20, "set network buffer size=%d\n", dbuf_size);
512 if (dbuf_size != MAX_NETWORK_BUFFER_SIZE)
513 Emsg1(M_WARNING, 0, "Warning network buffer = %d bytes not max size.\n", dbuf_size);
514 if (dbuf_size % TAPE_BSIZE != 0) {
515 Emsg1(M_ABORT, 0, "Network buffer size %d not multiple of tape block size.\n",
520 bs->msglen = dbuf_size;
525 * Send a network "signal" to the other end
526 * This consists of sending a negative packet length
528 * Returns: 0 on failure
531 int bnet_sig(BSOCK *bs, int sig)
534 return bnet_send(bs);
538 * Convert a network "signal" code into
539 * human readable ASCII.
541 char *bnet_sig_to_ascii(BSOCK *bs)
544 switch (bs->msglen) {
550 return "BNET_EOD_POLL";
552 return "BNET_STATUS";
554 return "BNET_TERMINATE";
558 return "BNET_HEARTBEAT";
559 case BNET_HB_RESPONSE:
560 return "BNET_HB_RESPONSE";
562 return "BNET_PROMPT";
564 sprintf(buf, "Unknown sig %d", bs->msglen);
570 /* Initialize internal socket structure.
571 * This probably should be done in net_open
574 init_bsock(int sockfd, char *who, char *host, int port)
576 BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
578 Emsg0(M_ABORT, 0, "Out of memory in init_bsock.\n");
580 memset(bsock, 0, sizeof(BSOCK));
583 bsock->msg = get_pool_memory(PM_MESSAGE);
584 bsock->errmsg = get_pool_memory(PM_MESSAGE);
585 bsock->who = bstrdup(who);
586 bsock->host = bstrdup(host);
589 * ****FIXME**** reduce this to a few hours once
590 * heartbeats are implemented
592 bsock->timeout = 60 * 60 * 6 * 24; /* 6 days timeout */
597 dup_bsock(BSOCK *osock)
599 BSOCK *bsock = (BSOCK *) malloc(sizeof(BSOCK));
601 Emsg0(M_ABORT, 0, "Out of memory in dup_bsock.\n");
603 memcpy(bsock, osock, sizeof(BSOCK));
604 bsock->msg = get_pool_memory(PM_MESSAGE);
605 bsock->errmsg = get_pool_memory(PM_MESSAGE);
610 /* Close the network connection */
612 bnet_close(BSOCK *bsock)
616 for ( ; bsock != NULL; bsock = next) {
619 shutdown(bsock->fd, SHUT_RDWR);
630 term_bsock(BSOCK *bsock)
632 free_pool_memory(bsock->msg);
633 free_pool_memory(bsock->errmsg);