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 = (char *) 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 atleast 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) {
411 i = 60 * 5; /* complain again in 5 minutes */
413 Jmsg(jcr, M_WARNING, 0, "Could not connect to %s on %s:%d. ERR=%s\n\
414 Retrying ...\n", name, host, port, strerror(errno));
416 sleep(retry_interval);
417 max_retry_time -= retry_interval;
418 if (max_retry_time <= 0) {
419 Jmsg(jcr, M_FATAL, 0, "Unable to connect to %s on %s:%d.\n",
429 * Return the string for the error that occurred
430 * on the socket. Only the first error is retained.
432 char *bnet_strerror(BSOCK *bsock)
434 /* ***FIXME*** not thread safe */
435 return strerror(bsock->b_errno);
439 * Format and send a message
440 * Returns: 0 on failure
444 bnet_fsend(BSOCK *bs, char *fmt, ...)
449 /* This probably won't work, but we vsnprintf, then if we
450 * get a negative length or a length greater than our buffer
451 * (depending on which library is used), the printf was truncated, so
452 * get a biger buffer and try again.
455 maxlen = sizeof_pool_memory(bs->msg) - 1;
456 va_start(arg_ptr, fmt);
457 bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
459 if (bs->msglen < 0 || bs->msglen >= maxlen) {
460 bs->msg = (POOLMEM *)realloc_pool_memory(bs->msg, maxlen + 200);
463 return bnet_send(bs) < 0 ? 0 : 1;
467 * Set the network buffer size, suggested size is in size.
468 * Actual size obtained is returned in bs->msglen
470 * Returns: 0 on failure
473 int bnet_set_buffer_size(BSOCK *bs, uint32_t size, int rw)
476 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
479 opt = IPTOS_THROUGHPUT;
480 setsockopt(bs->fd, IPPROTO_IP, IP_TOS, (char *)&opt, sizeof(opt));
484 if ((bs->msg = (char *) realloc_pool_memory(bs->msg, dbuf_size+100)) == NULL) {
485 Emsg0(M_FATAL, 0, "Could not malloc 32K BSOCK data buffer\n");
488 if (rw & BNET_SETBUF_READ) {
489 while ((dbuf_size > TAPE_BSIZE) &&
490 (setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (char *)&dbuf_size, sizeof(dbuf_size)) < 0)) {
491 Emsg1(M_ERROR, 0, "sockopt error: %s\n", strerror(errno));
492 dbuf_size -= TAPE_BSIZE;
494 Dmsg1(20, "set network buffer size=%d\n", dbuf_size);
495 if (dbuf_size != MAX_NETWORK_BUFFER_SIZE)
496 Emsg1(M_WARNING, 0, "Warning network buffer = %d bytes not max size.\n", dbuf_size);
497 if (dbuf_size % TAPE_BSIZE != 0) {
498 Emsg1(M_ABORT, 0, "Network buffer size %d not multiple of tape block size.\n",
503 if (rw & BNET_SETBUF_WRITE) {
504 while ((dbuf_size > TAPE_BSIZE) &&
505 (setsockopt(bs->fd, SOL_SOCKET, SO_SNDBUF, (char *)&dbuf_size, sizeof(dbuf_size)) < 0)) {
506 Emsg1(M_ERROR, 0, "sockopt error: %s\n", strerror(errno));
507 dbuf_size -= TAPE_BSIZE;
509 Dmsg1(20, "set network buffer size=%d\n", dbuf_size);
510 if (dbuf_size != MAX_NETWORK_BUFFER_SIZE)
511 Emsg1(M_WARNING, 0, "Warning network buffer = %d bytes not max size.\n", dbuf_size);
512 if (dbuf_size % TAPE_BSIZE != 0) {
513 Emsg1(M_ABORT, 0, "Network buffer size %d not multiple of tape block size.\n",
518 bs->msglen = dbuf_size;
523 * Send a network "signal" to the other end
524 * This consists of sending a negative packet length
526 * Returns: 0 on failure
529 int bnet_sig(BSOCK *bs, int sig)
532 return bnet_send(bs);
536 * Convert a network "signal" code into
537 * human readable ASCII.
539 char *bnet_sig_to_ascii(BSOCK *bs)
542 switch (bs->msglen) {
548 return "BNET_EOD_POLL";
550 return "BNET_STATUS";
552 return "BNET_TERMINATE";
556 return "BNET_HEARTBEAT";
557 case BNET_HB_RESPONSE:
558 return "BNET_HB_RESPONSE";
560 return "BNET_PROMPT";
562 sprintf(buf, "Unknown sig %d", bs->msglen);
568 /* Initialize internal socket structure.
569 * This probably should be done in net_open
572 init_bsock(int sockfd, char *who, char *host, int port)
574 BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
576 Emsg0(M_ABORT, 0, "Out of memory in init_bsock.\n");
578 memset(bsock, 0, sizeof(BSOCK));
581 bsock->msg = (POOLMEM *)get_pool_memory(PM_MESSAGE);
582 bsock->errmsg = (POOLMEM *)get_pool_memory(PM_MESSAGE);
583 bsock->who = bstrdup(who);
584 bsock->host = bstrdup(host);
587 * ****FIXME**** reduce this to a few hours once
588 * heartbeats are implemented
590 bsock->timeout = 60 * 60 * 6 * 24; /* 6 days timeout */
595 dup_bsock(BSOCK *osock)
597 BSOCK *bsock = (BSOCK *) malloc(sizeof(BSOCK));
599 Emsg0(M_ABORT, 0, "Out of memory in dup_bsock.\n");
601 memcpy(bsock, osock, sizeof(BSOCK));
602 bsock->msg = (POOLMEM *)get_pool_memory(PM_MESSAGE);
603 bsock->errmsg = (POOLMEM *)get_pool_memory(PM_MESSAGE);
608 /* Close the network connection */
610 bnet_close(BSOCK *bsock)
614 for ( ; bsock != NULL; bsock = next) {
617 shutdown(bsock->fd, SHUT_RDWR);
628 term_bsock(BSOCK *bsock)
630 free_pool_memory(bsock->msg);
631 free_pool_memory(bsock->errmsg);