2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Network Utility Routines
19 * Written by Kern Sibbald
26 #include <netinet/tcp.h>
28 #ifndef ENODATA /* not defined on BSD systems */
33 #define SOL_TCP IPPROTO_TCP
38 #define socketRead(fd, buf, len) ::recv(fd, buf, len, 0)
39 #define socketWrite(fd, buf, len) ::send(fd, buf, len, 0)
40 #define socketClose(fd) ::closesocket(fd)
41 static void win_close_wait(int fd);
43 #define socketRead(fd, buf, len) ::read(fd, buf, len)
44 #define socketWrite(fd, buf, len) ::write(fd, buf, len)
45 #define socketClose(fd) ::close(fd)
50 * This is a non-class BSOCK "constructor" because we want to
51 * call the Bacula smartalloc routines instead of new.
55 BSOCK *bsock = (BSOCK *)malloc(sizeof(BSOCK));
62 memset(this, 0, sizeof(BSOCK));
66 msg = get_pool_memory(PM_BSOCK);
67 errmsg = get_pool_memory(PM_MESSAGE);
68 timeout = BSOCK_TIMEOUT;
71 void BSOCK::free_tls()
73 free_tls_connection(this->tls);
78 * Try to connect to host for max_retry_time at retry_time intervals.
79 * Note, you must have called the constructor prior to calling
82 bool BSOCK::connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
84 const char *name, char *host, char *service, int port,
90 time_t begin_time = time(NULL);
94 /* Try to trap out of OS call when time expires */
96 tid = start_thread_timer(jcr, pthread_self(), (uint32_t)max_retry_time);
99 for (i = 0; !open(jcr, name, host, service, port, heart_beat, &fatal);
100 i -= retry_interval) {
102 if (fatal || (jcr && job_canceled(jcr))) {
105 Dmsg4(50, "Unable to connect to %s on %s:%d. ERR=%s\n",
106 name, host, port, be.bstrerror());
108 i = 60 * 5; /* complain again in 5 minutes */
110 Qmsg4(jcr, M_WARNING, 0, _(
111 "Could not connect to %s on %s:%d. ERR=%s\n"
112 "Retrying ...\n"), name, host, port, be.bstrerror());
114 bmicrosleep(retry_interval, 0);
116 if (begin_time + max_retry_time <= now) {
117 Qmsg4(jcr, M_FATAL, 0, _("Unable to connect to %s on %s:%d. ERR=%s\n"),
118 name, host, port, be.bstrerror());
126 stop_thread_timer(tid);
132 * Finish initialization of the packet structure.
134 void BSOCK::fin_init(JCR * jcr, int sockfd, const char *who, const char *host, int port,
135 struct sockaddr *lclient_addr)
137 Dmsg3(100, "who=%s host=%s port=%d\n", who, host, port);
145 set_who(bstrdup(who));
146 set_host(bstrdup(host));
148 memcpy(&client_addr, lclient_addr, sizeof(client_addr));
153 * Copy the address from the configuration dlist that gets passed in
155 void BSOCK::set_source_address(dlist *src_addr_list)
159 // delete the object we already have, if it's allocated
166 addr = (IPADDR*) src_addr_list->first();
167 src_addr = New( IPADDR(*addr));
172 * Open a TCP connection to the server
174 * Returns BSOCK * pointer on success
176 bool BSOCK::open(JCR *jcr, const char *name, char *host, char *service,
177 int port, utime_t heart_beat, int *fatal)
182 bool connected = false;
188 * Fill in the structure serv_addr with the address of
189 * the server that we want to connect with.
191 if ((addr_list = bnet_host2ipaddrs(host, 0, &errstr)) == NULL) {
192 /* Note errstr is not malloc'ed */
193 Qmsg2(jcr, M_ERROR, 0, _("gethostbyname() for host \"%s\" failed: ERR=%s\n"),
195 Dmsg2(100, "bnet_host2ipaddrs() for host %s failed: ERR=%s\n",
201 remove_duplicate_addresses(addr_list);
202 foreach_dlist(ipaddr, addr_list) {
203 ipaddr->set_port_net(htons(port));
204 char allbuf[256 * 10];
206 Dmsg2(100, "Current %sAll %s\n",
207 ipaddr->build_address_str(curbuf, sizeof(curbuf)),
208 build_addresses_str(addr_list, allbuf, sizeof(allbuf)));
209 /* Open a TCP socket */
210 if ((sockfd = socket(ipaddr->get_family(), SOCK_STREAM, 0)) < 0) {
217 * The name lookup of the host returned an address in a protocol family
218 * we don't support. Suppress the error and try the next address.
224 Qmsg3(jcr, M_ERROR, 0, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
225 ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
226 Pmsg3(000, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
227 ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
233 /* Bind to the source address if it is set */
235 if (bind(sockfd, src_addr->get_sockaddr(), src_addr->get_sockaddr_len()) < 0) {
239 Qmsg2(jcr, M_ERROR, 0, _("Source address bind error. proto=%d. ERR=%s\n"),
240 src_addr->get_family(), be.bstrerror() );
241 Pmsg2(000, _("Source address bind error. proto=%d. ERR=%s\n"),
242 src_addr->get_family(), be.bstrerror() );
243 if (sockfd >= 0) socketClose(sockfd);
249 * Keep socket from timing out from inactivity
251 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
253 Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
256 #if defined(TCP_KEEPIDLE)
258 int opt = heart_beat;
259 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
261 Qmsg1(jcr, M_WARNING, 0, _("Cannot set TCP_KEEPIDLE on socket: %s\n"),
267 /* connect to server */
268 if (::connect(sockfd, ipaddr->get_sockaddr(), ipaddr->get_sockaddr_len()) < 0) {
270 if (sockfd >= 0) socketClose(sockfd);
280 free_addresses(addr_list);
281 errno = save_errno | b_errno_win32;
282 Dmsg4(50, "Could not connect to server %s %s:%d. ERR=%s\n",
283 name, host, port, be.bstrerror());
287 * Keep socket from timing out from inactivity
288 * Do this a second time out of paranoia
290 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
292 Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
295 fin_init(jcr, sockfd, name, host, port, ipaddr->get_sockaddr());
296 free_addresses(addr_list);
298 /* Clean the packet a bit */
302 m_use_locking = false;
304 m_terminated = false;
305 m_suppress_error_msgs = false;
309 Dmsg3(50, "OK connected to server %s %s:%d.\n",
316 * Force read/write to use locking
318 bool BSOCK::set_locking()
322 return true; /* already set */
324 if ((stat = pthread_mutex_init(&m_mutex, NULL)) != 0) {
326 Qmsg(m_jcr, M_FATAL, 0, _("Could not init bsock mutex. ERR=%s\n"),
330 m_use_locking = true;
334 void BSOCK::clear_locking()
336 if (!m_use_locking) {
339 m_use_locking = false;
340 pthread_mutex_destroy(&m_mutex);
345 * Send a message over the network. The send consists of
346 * two network packets. The first is sends a 32 bit integer containing
347 * the length of the data packet which follows.
349 * Returns: false on failure
362 if (!m_suppress_error_msgs) {
363 Qmsg0(m_jcr, M_ERROR, 0, _("Socket is closed\n"));
368 if (!m_suppress_error_msgs) {
369 Qmsg4(m_jcr, M_ERROR, 0, _("Socket has errors=%d on call to %s:%s:%d\n"),
370 errors, m_who, m_host, m_port);
374 if (is_terminated()) {
375 if (!m_suppress_error_msgs) {
376 Qmsg4(m_jcr, M_ERROR, 0, _("Socket is terminated=%d on call to %s:%s:%d\n"),
377 is_terminated(), m_who, m_host, m_port);
381 if (msglen > 4000000) {
382 if (!m_suppress_error_msgs) {
383 Qmsg4(m_jcr, M_ERROR, 0,
384 _("Socket has insane msglen=%d on call to %s:%s:%d\n"),
385 msglen, m_who, m_host, m_port);
390 if (m_use_locking) P(m_mutex);
391 save_msglen = msglen;
393 /* Compute total packet length */
395 pktsiz = sizeof(pktsiz); /* signal, no data */
397 pktsiz = msglen + sizeof(pktsiz); /* data */
400 * Store packet length at head of message -- note, we
401 * have reserved an int32_t just before msg, so we can
404 hdr = (int32_t *)(msg - (int)sizeof(pktsiz));
405 *hdr = htonl(msglen); /* store signal/length */
407 out_msg_no++; /* increment message number */
409 /* send data packet */
410 timer_start = watchdog_time; /* start timer */
412 /* Full I/O done in one write */
413 rc = write_nbytes(this, (char *)hdr, pktsiz);
414 timer_start = 0; /* clear timer */
423 if (!m_suppress_error_msgs) {
424 Qmsg5(m_jcr, M_ERROR, 0,
425 _("Write error sending %d bytes to %s:%s:%d: ERR=%s\n"),
427 m_host, m_port, this->bstrerror());
430 Qmsg5(m_jcr, M_ERROR, 0,
431 _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"),
432 pktsiz, m_who, m_host, m_port, rc);
436 msglen = save_msglen;
438 if (m_use_locking) V(m_mutex);
443 * Format and send a message
444 * Returns: false on error
447 bool BSOCK::fsend(const char *fmt, ...)
452 if (errors || is_terminated() || is_closed()) {
455 /* This probably won't work, but we vsnprintf, then if we
456 * get a negative length or a length greater than our buffer
457 * (depending on which library is used), the printf was truncated, so
458 * get a bigger buffer and try again.
461 maxlen = sizeof_pool_memory(msg) - 1;
462 va_start(arg_ptr, fmt);
463 msglen = bvsnprintf(msg, maxlen, fmt, arg_ptr);
465 if (msglen > 0 && msglen < (maxlen - 5)) {
468 msg = realloc_pool_memory(msg, maxlen + maxlen / 2);
474 * Receive a message from the other end. Each message consists of
475 * two packets. The first is a header that contains the size
476 * of the data that follows in the second packet.
477 * Returns number of bytes read (may return zero)
478 * Returns -1 on signal (BNET_SIGNAL)
479 * Returns -2 on hard end of file (BNET_HARDEOF)
480 * Returns -3 on error (BNET_ERROR)
482 * Unfortunately, it is a bit complicated because we have these
485 * 2. Signal including end of data stream
486 * 3. Hard end of file
488 * Using bsock->is_stop() and bsock->is_error() you can figure this all out.
490 int32_t BSOCK::recv()
498 if (errors || is_terminated() || is_closed()) {
506 read_seqno++; /* bump sequence number */
507 timer_start = watchdog_time; /* set start wait time */
509 /* get data size -- in int32_t */
510 if ((nbytes = read_nbytes(this, (char *)&pktsiz, sizeof(int32_t))) <= 0) {
511 timer_start = 0; /* clear timer */
512 /* probably pipe broken because client died */
519 nbytes = BNET_HARDEOF; /* assume hard EOF received */
522 timer_start = 0; /* clear timer */
523 if (nbytes != sizeof(int32_t)) {
526 Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
527 sizeof(int32_t), nbytes, m_who, m_host, m_port);
532 pktsiz = ntohl(pktsiz); /* decode no. of bytes that follow */
534 if (pktsiz == 0) { /* No data transferred */
535 timer_start = 0; /* clear timer */
538 nbytes = 0; /* zero bytes read */
542 /* If signal or packet size too big */
543 if (pktsiz < 0 || pktsiz > 1000000) {
544 if (pktsiz > 0) { /* if packet too big */
545 Qmsg4(m_jcr, M_FATAL, 0,
546 _("Packet size=%d too big from \"%s:%s:%d. Terminating connection.\n"),
547 pktsiz, m_who, m_host, m_port);
548 pktsiz = BNET_TERMINATE; /* hang up */
550 if (pktsiz == BNET_TERMINATE) {
553 timer_start = 0; /* clear timer */
555 msglen = pktsiz; /* signal code */
556 nbytes = BNET_SIGNAL; /* signal */
560 /* Make sure the buffer is big enough + one byte for EOS */
561 if (pktsiz >= (int32_t) sizeof_pool_memory(msg)) {
562 msg = realloc_pool_memory(msg, pktsiz + 100);
565 timer_start = watchdog_time; /* set start wait time */
567 /* now read the actual data */
568 if ((nbytes = read_nbytes(this, msg, pktsiz)) <= 0) {
569 timer_start = 0; /* clear timer */
576 Qmsg4(m_jcr, M_ERROR, 0, _("Read error from %s:%s:%d: ERR=%s\n"),
577 m_who, m_host, m_port, this->bstrerror());
581 timer_start = 0; /* clear timer */
584 if (nbytes != pktsiz) {
587 Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
588 pktsiz, nbytes, m_who, m_host, m_port);
593 /* always add a zero by to properly terminate any
594 * string that was send to us. Note, we ensured above that the
595 * buffer is at least one byte longer than the message length.
597 msg[nbytes] = 0; /* terminate in case it is a string */
599 * The following uses *lots* of resources so turn it on only for
605 if (locked) V(m_mutex);
606 return nbytes; /* return actual length of message */
612 bool BSOCK::signal(int signal)
615 if (signal == BNET_TERMINATE) {
616 m_suppress_error_msgs = true;
622 * Despool spooled attributes
624 bool BSOCK::despool(void update_attr_spool_size(ssize_t size), ssize_t tsize)
628 ssize_t last = 0, size = 0;
630 JCR *jcr = get_jcr();
634 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
635 posix_fadvise(fileno(m_spool_fd), 0, 0, POSIX_FADV_WILLNEED);
638 while (fread((char *)&pktsiz, 1, sizeof(int32_t), m_spool_fd) ==
640 size += sizeof(int32_t);
641 msglen = ntohl(pktsiz);
643 if (msglen > (int32_t)sizeof_pool_memory(msg)) {
644 msg = realloc_pool_memory(msg, msglen + 1);
646 nbytes = fread(msg, 1, msglen, m_spool_fd);
647 if (nbytes != (size_t)msglen) {
649 Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, msglen);
650 Qmsg3(get_jcr(), M_FATAL, 0, _("fread attr spool error. Wanted=%d got=%d bytes. ERR=%s\n"),
651 msglen, nbytes, be.bstrerror());
652 update_attr_spool_size(tsize - last);
656 if ((++count & 0x3F) == 0) {
657 update_attr_spool_size(size - last);
662 if (jcr && job_canceled(jcr)) {
666 update_attr_spool_size(tsize - last);
667 if (ferror(m_spool_fd)) {
668 Qmsg(jcr, M_FATAL, 0, _("fread attr spool I/O error.\n"));
675 * Return the string for the error that occurred
676 * on the socket. Only the first error is retained.
678 const char *BSOCK::bstrerror()
681 if (errmsg == NULL) {
682 errmsg = get_pool_memory(PM_MESSAGE);
684 pm_strcpy(errmsg, be.bstrerror(b_errno));
688 int BSOCK::get_peer(char *buf, socklen_t buflen)
690 #if !defined(HAVE_WIN32)
691 if (peer_addr.sin_family == 0) {
692 socklen_t salen = sizeof(peer_addr);
693 int rval = (getpeername)(m_fd, (struct sockaddr *)&peer_addr, &salen);
694 if (rval < 0) return rval;
696 if (!inet_ntop(peer_addr.sin_family, &peer_addr.sin_addr, buf, buflen))
706 * Set the network buffer size, suggested size is in size.
707 * Actual size obtained is returned in bs->msglen
709 * Returns: false on failure
712 bool BSOCK::set_buffer_size(uint32_t size, int rw)
714 uint32_t dbuf_size, start_size;
716 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
718 opt = IPTOS_THROUGHPUT;
719 setsockopt(m_fd, IPPROTO_IP, IP_TOS, (sockopt_val_t)&opt, sizeof(opt));
725 dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
727 start_size = dbuf_size;
728 if ((msg = realloc_pool_memory(msg, dbuf_size + 100)) == NULL) {
729 Qmsg0(get_jcr(), M_FATAL, 0, _("Could not malloc BSOCK data buffer\n"));
734 * If user has not set the size, use the OS default -- i.e. do not
735 * try to set it. This allows sys admins to set the size they
736 * want in the OS, and Bacula will comply. See bug #1493
743 if (rw & BNET_SETBUF_READ) {
744 while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
745 SO_RCVBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
747 Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
748 dbuf_size -= TAPE_BSIZE;
750 Dmsg1(200, "set network buffer size=%d\n", dbuf_size);
751 if (dbuf_size != start_size) {
752 Qmsg1(get_jcr(), M_WARNING, 0,
753 _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
759 dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
761 start_size = dbuf_size;
762 if (rw & BNET_SETBUF_WRITE) {
763 while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
764 SO_SNDBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
766 Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
767 dbuf_size -= TAPE_BSIZE;
769 Dmsg1(900, "set network buffer size=%d\n", dbuf_size);
770 if (dbuf_size != start_size) {
771 Qmsg1(get_jcr(), M_WARNING, 0,
772 _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
781 * Set socket non-blocking
782 * Returns previous socket flag
784 int BSOCK::set_nonblocking()
789 /* Get current flags */
790 if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
792 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
795 /* Set O_NONBLOCK flag */
796 if ((fcntl(m_fd, F_SETFL, oflags|O_NONBLOCK)) < 0) {
798 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
808 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
816 * Set socket blocking
817 * Returns previous socket flags
819 int BSOCK::set_blocking()
823 /* Get current flags */
824 if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
826 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
829 /* Set O_NONBLOCK flag */
830 if ((fcntl(m_fd, F_SETFL, oflags & ~O_NONBLOCK)) < 0) {
832 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
842 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
849 void BSOCK::set_killable(bool killable)
852 m_jcr->set_killable(killable);
857 * Restores socket flags
859 void BSOCK::restore_blocking (int flags)
862 if ((fcntl(m_fd, F_SETFL, flags)) < 0) {
864 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
867 m_blocking = (flags & O_NONBLOCK) ? true : false;
869 u_long ioctlArg = flags;
871 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
877 * Wait for a specified time for data to appear on
878 * the BSOCK connection.
880 * Returns: 1 if data available
884 int BSOCK::wait_data(int sec, int usec)
890 FD_SET((unsigned)m_fd, &fdset);
894 switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
895 case 0: /* timeout */
900 if (errno == EINTR) {
903 return -1; /* error return */
907 if (this->tls && !tls_bsock_probe(this)) {
908 continue; /* false alarm, maybe a session key negotiation in progress on the socket */
917 * As above, but returns on interrupt
919 int BSOCK::wait_data_intr(int sec, int usec)
928 FD_SET((unsigned)m_fd, &fdset);
931 switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
932 case 0: /* timeout */
937 return -1; /* error return */
943 if (this->tls && !tls_bsock_probe(this)) {
944 /* maybe a session key negotiation waked up the socket */
948 >>>>>>> 625e1f1... Fix compilation of bsock.c when TLS is not available
955 * This routine closes the current BSOCK.
956 * It does not delete the socket packet
957 * resources, which are released int
965 * Note, this routine closes the socket, but leaves the
966 * bsock memory in place.
973 if (bsock->is_closed()) {
979 for (; bsock; bsock = next) {
980 next = bsock->m_next; /* get possible pointer to next before destoryed */
982 bsock->set_terminated();
983 if (!bsock->m_duped) {
984 /* Shutdown tls cleanly. */
986 tls_bsock_shutdown(bsock);
987 free_tls_connection(bsock->tls);
992 if (!bsock->is_timed_out()) {
993 win_close_wait(bsock->m_fd); /* Ensure that data is not discarded */
996 if (bsock->is_timed_out()) {
997 shutdown(bsock->m_fd, SHUT_RDWR); /* discard any pending I/O */
1000 /* On Windows this discards data if we did not do a close_wait() */
1001 socketClose(bsock->m_fd); /* normal close */
1008 * Destroy the socket (i.e. release all resources)
1009 * including and duped sockets.
1011 void BSOCK::destroy()
1013 this->close(); /* Ensure that socket is closed */
1016 free_pool_memory(msg);
1019 ASSERT2(1 == 0, "Two calls to destroy socket"); /* double destroy */
1022 free_pool_memory(errmsg);
1043 /* Commands sent to Director */
1044 static char hello[] = "Hello %s calling\n";
1046 /* Response from Director */
1047 static char OKhello[] = "1000 OK:";
1050 * Authenticate Director
1052 bool BSOCK::authenticate_director(const char *name, const char *password,
1053 TLS_CONTEXT *tls_ctx, char *errmsg, int errmsg_len)
1055 int tls_local_need = BNET_TLS_NONE;
1056 int tls_remote_need = BNET_TLS_NONE;
1057 int compatible = true;
1058 char bashed_name[MAX_NAME_LENGTH];
1059 BSOCK *dir = this; /* for readability */
1063 * Send my name to the Director then do authentication
1066 /* Timeout Hello after 15 secs */
1067 dir->start_timer(15);
1068 dir->fsend(hello, bashed_name);
1070 if (get_tls_enable(tls_ctx)) {
1071 tls_local_need = get_tls_enable(tls_ctx) ? BNET_TLS_REQUIRED : BNET_TLS_OK;
1074 /* respond to Dir challenge */
1075 if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
1076 /* Now challenge dir */
1077 !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
1078 bsnprintf(errmsg, errmsg_len, _("Director authorization error at \"%s:%d\"\n"),
1079 dir->host(), dir->port());
1083 /* Verify that the remote host is willing to meet our TLS requirements */
1084 if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
1085 bsnprintf(errmsg, errmsg_len, _("Authorization error:"
1086 " Remote server at \"%s:%d\" did not advertise required TLS support.\n"),
1087 dir->host(), dir->port());
1091 /* Verify that we are willing to meet the remote host's requirements */
1092 if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
1093 bsnprintf(errmsg, errmsg_len, _("Authorization error with Director at \"%s:%d\":"
1094 " Remote server requires TLS.\n"),
1095 dir->host(), dir->port());
1100 /* Is TLS Enabled? */
1102 if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
1103 /* Engage TLS! Full Speed Ahead! */
1104 if (!bnet_tls_client(tls_ctx, dir, NULL)) {
1105 bsnprintf(errmsg, errmsg_len, _("TLS negotiation failed with Director at \"%s:%d\"\n"),
1106 dir->host(), dir->port());
1112 Dmsg1(6, ">dird: %s", dir->msg);
1113 if (dir->recv() <= 0) {
1115 bsnprintf(errmsg, errmsg_len, _("Bad errmsg to Hello command: ERR=%s\n"
1116 "The Director at \"%s:%d\" may not be running.\n"),
1117 dir->bstrerror(), dir->host(), dir->port());
1122 Dmsg1(10, "<dird: %s", dir->msg);
1123 if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
1124 bsnprintf(errmsg, errmsg_len, _("Director at \"%s:%d\" rejected Hello command\n"),
1125 dir->host(), dir->port());
1128 bsnprintf(errmsg, errmsg_len, "%s", dir->msg);
1134 bsnprintf(errmsg, errmsg_len, _("Authorization error with Director at \"%s:%d\"\n"
1135 "Most likely the passwords do not agree.\n"
1136 "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
1137 "Please see " MANUAL_AUTH_URL " for help.\n"),
1138 dir->host(), dir->port());
1142 /* Try to limit the bandwidth of a network connection
1144 void BSOCK::control_bwlimit(int bytes)
1151 now = get_current_btime(); /* microseconds */
1152 temp = now - m_last_tick; /* microseconds */
1154 m_nb_bytes += bytes;
1156 /* Less than 0.1ms since the last call, see the next time */
1161 if (temp > 10000000) { /* Take care of clock problems (>10s) */
1167 /* Remove what was authorised to be written in temp us */
1168 m_nb_bytes -= (int64_t)(temp * ((double)m_bwlimit / 1000000.0));
1170 if (m_nb_bytes < 0) {
1174 /* What exceed should be converted in sleep time */
1175 int64_t usec_sleep = (int64_t)(m_nb_bytes /((double)m_bwlimit / 1000000.0));
1176 if (usec_sleep > 100) {
1177 bmicrosleep(0, usec_sleep); /* TODO: Check that bmicrosleep slept enough or sleep again */
1178 m_last_tick = get_current_btime();
1187 * closesocket is supposed to do a graceful disconnect under Window
1188 * but it doesn't. Comments on http://msdn.microsoft.com/en-us/li
1189 * confirm this behaviour. DisconnectEx is required instead, but
1190 * that function needs to be retrieved via WS IOCTL
1193 win_close_wait(int fd)
1196 GUID disconnectex_guid = WSAID_DISCONNECTEX;
1197 DWORD bytes_returned;
1198 LPFN_DISCONNECTEX DisconnectEx;
1199 ret = WSAIoctl(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectex_guid, sizeof(disconnectex_guid), &DisconnectEx, sizeof(DisconnectEx), &bytes_returned, NULL, NULL);
1200 Dmsg1(100, "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER, WSAID_DISCONNECTEX) ret = %d\n", ret);
1202 DisconnectEx(fd, NULL, 0, 0);