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);
203 foreach_dlist(ipaddr, addr_list) {
204 ipaddr->set_port_net(htons(port));
205 char allbuf[256 * 10];
207 Dmsg2(100, "Current %sAll %s\n",
208 ipaddr->build_address_str(curbuf, sizeof(curbuf)),
209 build_addresses_str(addr_list, allbuf, sizeof(allbuf)));
210 /* Open a TCP socket */
211 if ((sockfd = socket(ipaddr->get_family(), SOCK_STREAM, 0)) < 0) {
218 * The name lookup of the host returned an address in a protocol family
219 * we don't support. Suppress the error and try the next address.
225 Qmsg3(jcr, M_ERROR, 0, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
226 ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
227 Pmsg3(000, _("Socket open error. proto=%d port=%d. ERR=%s\n"),
228 ipaddr->get_family(), ipaddr->get_port_host_order(), be.bstrerror());
234 /* Bind to the source address if it is set */
236 if (bind(sockfd, src_addr->get_sockaddr(), src_addr->get_sockaddr_len()) < 0) {
240 Qmsg2(jcr, M_ERROR, 0, _("Source address bind error. proto=%d. ERR=%s\n"),
241 src_addr->get_family(), be.bstrerror() );
242 Pmsg2(000, _("Source address bind error. proto=%d. ERR=%s\n"),
243 src_addr->get_family(), be.bstrerror() );
244 if (sockfd >= 0) socketClose(sockfd);
250 * Keep socket from timing out from inactivity
252 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
254 Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
257 #if defined(TCP_KEEPIDLE)
259 int opt = heart_beat;
260 if (setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
262 Qmsg1(jcr, M_WARNING, 0, _("Cannot set TCP_KEEPIDLE on socket: %s\n"),
268 /* connect to server */
269 if (::connect(sockfd, ipaddr->get_sockaddr(), ipaddr->get_sockaddr_len()) < 0) {
271 if (sockfd >= 0) socketClose(sockfd);
281 free_addresses(addr_list);
282 errno = save_errno | b_errno_win32;
283 Dmsg4(50, "Could not connect to server %s %s:%d. ERR=%s\n",
284 name, host, port, be.bstrerror());
288 * Keep socket from timing out from inactivity
289 * Do this a second time out of paranoia
291 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
293 Qmsg1(jcr, M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
296 fin_init(jcr, sockfd, name, host, port, ipaddr->get_sockaddr());
297 free_addresses(addr_list);
299 /* Clean the packet a bit */
303 m_use_locking = false;
305 m_terminated = false;
306 m_suppress_error_msgs = false;
310 Dmsg3(50, "OK connected to server %s %s:%d.\n",
317 * Force read/write to use locking
319 bool BSOCK::set_locking()
323 return true; /* already set */
325 if ((stat = pthread_mutex_init(&m_mutex, NULL)) != 0) {
327 Qmsg(m_jcr, M_FATAL, 0, _("Could not init bsock mutex. ERR=%s\n"),
331 m_use_locking = true;
335 void BSOCK::clear_locking()
337 if (!m_use_locking) {
340 m_use_locking = false;
341 pthread_mutex_destroy(&m_mutex);
346 * Send a message over the network. The send consists of
347 * two network packets. The first is sends a 32 bit integer containing
348 * the length of the data packet which follows.
350 * Returns: false on failure
363 if (!m_suppress_error_msgs) {
364 Qmsg0(m_jcr, M_ERROR, 0, _("Socket is closed\n"));
369 if (!m_suppress_error_msgs) {
370 Qmsg4(m_jcr, M_ERROR, 0, _("Socket has errors=%d on call to %s:%s:%d\n"),
371 errors, m_who, m_host, m_port);
375 if (is_terminated()) {
376 if (!m_suppress_error_msgs) {
377 Qmsg4(m_jcr, M_ERROR, 0, _("Socket is terminated=%d on call to %s:%s:%d\n"),
378 is_terminated(), m_who, m_host, m_port);
382 if (msglen > 4000000) {
383 if (!m_suppress_error_msgs) {
384 Qmsg4(m_jcr, M_ERROR, 0,
385 _("Socket has insane msglen=%d on call to %s:%s:%d\n"),
386 msglen, m_who, m_host, m_port);
391 if (m_use_locking) P(m_mutex);
392 save_msglen = msglen;
394 /* Compute total packet length */
396 pktsiz = sizeof(pktsiz); /* signal, no data */
398 pktsiz = msglen + sizeof(pktsiz); /* data */
401 * Store packet length at head of message -- note, we
402 * have reserved an int32_t just before msg, so we can
405 hdr = (int32_t *)(msg - (int)sizeof(pktsiz));
406 *hdr = htonl(msglen); /* store signal/length */
408 out_msg_no++; /* increment message number */
410 /* send data packet */
411 timer_start = watchdog_time; /* start timer */
413 /* Full I/O done in one write */
414 rc = write_nbytes(this, (char *)hdr, pktsiz);
415 timer_start = 0; /* clear timer */
424 if (!m_suppress_error_msgs) {
425 Qmsg5(m_jcr, M_ERROR, 0,
426 _("Write error sending %d bytes to %s:%s:%d: ERR=%s\n"),
428 m_host, m_port, this->bstrerror());
431 Qmsg5(m_jcr, M_ERROR, 0,
432 _("Wrote %d bytes to %s:%s:%d, but only %d accepted.\n"),
433 pktsiz, m_who, m_host, m_port, rc);
437 msglen = save_msglen;
439 if (m_use_locking) V(m_mutex);
444 * Format and send a message
445 * Returns: false on error
448 bool BSOCK::fsend(const char *fmt, ...)
453 if (errors || is_terminated() || is_closed()) {
456 /* This probably won't work, but we vsnprintf, then if we
457 * get a negative length or a length greater than our buffer
458 * (depending on which library is used), the printf was truncated, so
459 * get a bigger buffer and try again.
462 maxlen = sizeof_pool_memory(msg) - 1;
463 va_start(arg_ptr, fmt);
464 msglen = bvsnprintf(msg, maxlen, fmt, arg_ptr);
466 if (msglen > 0 && msglen < (maxlen - 5)) {
469 msg = realloc_pool_memory(msg, maxlen + maxlen / 2);
475 * Receive a message from the other end. Each message consists of
476 * two packets. The first is a header that contains the size
477 * of the data that follows in the second packet.
478 * Returns number of bytes read (may return zero)
479 * Returns -1 on signal (BNET_SIGNAL)
480 * Returns -2 on hard end of file (BNET_HARDEOF)
481 * Returns -3 on error (BNET_ERROR)
483 * Unfortunately, it is a bit complicated because we have these
486 * 2. Signal including end of data stream
487 * 3. Hard end of file
489 * Using bsock->is_stop() and bsock->is_error() you can figure this all out.
491 int32_t BSOCK::recv()
499 if (errors || is_terminated() || is_closed()) {
507 read_seqno++; /* bump sequence number */
508 timer_start = watchdog_time; /* set start wait time */
510 /* get data size -- in int32_t */
511 if ((nbytes = read_nbytes(this, (char *)&pktsiz, sizeof(int32_t))) <= 0) {
512 timer_start = 0; /* clear timer */
513 /* probably pipe broken because client died */
520 nbytes = BNET_HARDEOF; /* assume hard EOF received */
523 timer_start = 0; /* clear timer */
524 if (nbytes != sizeof(int32_t)) {
527 Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
528 sizeof(int32_t), nbytes, m_who, m_host, m_port);
533 pktsiz = ntohl(pktsiz); /* decode no. of bytes that follow */
535 if (pktsiz == 0) { /* No data transferred */
536 timer_start = 0; /* clear timer */
539 nbytes = 0; /* zero bytes read */
543 /* If signal or packet size too big */
544 if (pktsiz < 0 || pktsiz > 1000000) {
545 if (pktsiz > 0) { /* if packet too big */
546 Qmsg4(m_jcr, M_FATAL, 0,
547 _("Packet size=%d too big from \"%s:%s:%d. Terminating connection.\n"),
548 pktsiz, m_who, m_host, m_port);
549 pktsiz = BNET_TERMINATE; /* hang up */
551 if (pktsiz == BNET_TERMINATE) {
554 timer_start = 0; /* clear timer */
556 msglen = pktsiz; /* signal code */
557 nbytes = BNET_SIGNAL; /* signal */
561 /* Make sure the buffer is big enough + one byte for EOS */
562 if (pktsiz >= (int32_t) sizeof_pool_memory(msg)) {
563 msg = realloc_pool_memory(msg, pktsiz + 100);
566 timer_start = watchdog_time; /* set start wait time */
568 /* now read the actual data */
569 if ((nbytes = read_nbytes(this, msg, pktsiz)) <= 0) {
570 timer_start = 0; /* clear timer */
577 Qmsg4(m_jcr, M_ERROR, 0, _("Read error from %s:%s:%d: ERR=%s\n"),
578 m_who, m_host, m_port, this->bstrerror());
582 timer_start = 0; /* clear timer */
585 if (nbytes != pktsiz) {
588 Qmsg5(m_jcr, M_ERROR, 0, _("Read expected %d got %d from %s:%s:%d\n"),
589 pktsiz, nbytes, m_who, m_host, m_port);
594 /* always add a zero by to properly terminate any
595 * string that was send to us. Note, we ensured above that the
596 * buffer is at least one byte longer than the message length.
598 msg[nbytes] = 0; /* terminate in case it is a string */
600 * The following uses *lots* of resources so turn it on only for
606 if (locked) V(m_mutex);
607 return nbytes; /* return actual length of message */
613 bool BSOCK::signal(int signal)
616 if (signal == BNET_TERMINATE) {
617 m_suppress_error_msgs = true;
623 * Despool spooled attributes
625 bool BSOCK::despool(void update_attr_spool_size(ssize_t size), ssize_t tsize)
629 ssize_t last = 0, size = 0;
631 JCR *jcr = get_jcr();
635 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
636 posix_fadvise(fileno(m_spool_fd), 0, 0, POSIX_FADV_WILLNEED);
639 while (fread((char *)&pktsiz, 1, sizeof(int32_t), m_spool_fd) ==
641 size += sizeof(int32_t);
642 msglen = ntohl(pktsiz);
644 if (msglen > (int32_t)sizeof_pool_memory(msg)) {
645 msg = realloc_pool_memory(msg, msglen + 1);
647 nbytes = fread(msg, 1, msglen, m_spool_fd);
648 if (nbytes != (size_t)msglen) {
650 Dmsg2(400, "nbytes=%d msglen=%d\n", nbytes, msglen);
651 Qmsg3(get_jcr(), M_FATAL, 0, _("fread attr spool error. Wanted=%d got=%d bytes. ERR=%s\n"),
652 msglen, nbytes, be.bstrerror());
653 update_attr_spool_size(tsize - last);
657 if ((++count & 0x3F) == 0) {
658 update_attr_spool_size(size - last);
663 if (jcr && job_canceled(jcr)) {
667 update_attr_spool_size(tsize - last);
668 if (ferror(m_spool_fd)) {
669 Qmsg(jcr, M_FATAL, 0, _("fread attr spool I/O error.\n"));
676 * Return the string for the error that occurred
677 * on the socket. Only the first error is retained.
679 const char *BSOCK::bstrerror()
682 if (errmsg == NULL) {
683 errmsg = get_pool_memory(PM_MESSAGE);
685 pm_strcpy(errmsg, be.bstrerror(b_errno));
689 int BSOCK::get_peer(char *buf, socklen_t buflen)
691 #if !defined(HAVE_WIN32)
692 if (peer_addr.sin_family == 0) {
693 socklen_t salen = sizeof(peer_addr);
694 int rval = (getpeername)(m_fd, (struct sockaddr *)&peer_addr, &salen);
695 if (rval < 0) return rval;
697 if (!inet_ntop(peer_addr.sin_family, &peer_addr.sin_addr, buf, buflen))
707 * Set the network buffer size, suggested size is in size.
708 * Actual size obtained is returned in bs->msglen
710 * Returns: false on failure
713 bool BSOCK::set_buffer_size(uint32_t size, int rw)
715 uint32_t dbuf_size, start_size;
717 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
719 opt = IPTOS_THROUGHPUT;
720 setsockopt(m_fd, IPPROTO_IP, IP_TOS, (sockopt_val_t)&opt, sizeof(opt));
726 dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
728 start_size = dbuf_size;
729 if ((msg = realloc_pool_memory(msg, dbuf_size + 100)) == NULL) {
730 Qmsg0(get_jcr(), M_FATAL, 0, _("Could not malloc BSOCK data buffer\n"));
735 * If user has not set the size, use the OS default -- i.e. do not
736 * try to set it. This allows sys admins to set the size they
737 * want in the OS, and Bacula will comply. See bug #1493
744 if (rw & BNET_SETBUF_READ) {
745 while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
746 SO_RCVBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
748 Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
749 dbuf_size -= TAPE_BSIZE;
751 Dmsg1(200, "set network buffer size=%d\n", dbuf_size);
752 if (dbuf_size != start_size) {
753 Qmsg1(get_jcr(), M_WARNING, 0,
754 _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
760 dbuf_size = DEFAULT_NETWORK_BUFFER_SIZE;
762 start_size = dbuf_size;
763 if (rw & BNET_SETBUF_WRITE) {
764 while ((dbuf_size > TAPE_BSIZE) && (setsockopt(m_fd, SOL_SOCKET,
765 SO_SNDBUF, (sockopt_val_t) & dbuf_size, sizeof(dbuf_size)) < 0)) {
767 Qmsg1(get_jcr(), M_ERROR, 0, _("sockopt error: %s\n"), be.bstrerror());
768 dbuf_size -= TAPE_BSIZE;
770 Dmsg1(900, "set network buffer size=%d\n", dbuf_size);
771 if (dbuf_size != start_size) {
772 Qmsg1(get_jcr(), M_WARNING, 0,
773 _("Warning network buffer = %d bytes not max size.\n"), dbuf_size);
782 * Set socket non-blocking
783 * Returns previous socket flag
785 int BSOCK::set_nonblocking()
790 /* Get current flags */
791 if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
793 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
796 /* Set O_NONBLOCK flag */
797 if ((fcntl(m_fd, F_SETFL, oflags|O_NONBLOCK)) < 0) {
799 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
809 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
817 * Set socket blocking
818 * Returns previous socket flags
820 int BSOCK::set_blocking()
824 /* Get current flags */
825 if ((oflags = fcntl(m_fd, F_GETFL, 0)) < 0) {
827 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_GETFL error. ERR=%s\n"), be.bstrerror());
830 /* Set O_NONBLOCK flag */
831 if ((fcntl(m_fd, F_SETFL, oflags & ~O_NONBLOCK)) < 0) {
833 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
843 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
850 void BSOCK::set_killable(bool killable)
853 m_jcr->set_killable(killable);
858 * Restores socket flags
860 void BSOCK::restore_blocking (int flags)
863 if ((fcntl(m_fd, F_SETFL, flags)) < 0) {
865 Qmsg1(get_jcr(), M_ABORT, 0, _("fcntl F_SETFL error. ERR=%s\n"), be.bstrerror());
868 m_blocking = (flags & O_NONBLOCK) ? true : false;
870 u_long ioctlArg = flags;
872 ioctlsocket(m_fd, FIONBIO, &ioctlArg);
878 * Wait for a specified time for data to appear on
879 * the BSOCK connection.
881 * Returns: 1 if data available
885 int BSOCK::wait_data(int sec, int usec)
891 FD_SET((unsigned)m_fd, &fdset);
895 switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
896 case 0: /* timeout */
901 if (errno == EINTR) {
904 return -1; /* error return */
908 if (this->tls && !tls_bsock_probe(this)) {
909 continue; /* false alarm, maybe a session key negotiation in progress on the socket */
918 * As above, but returns on interrupt
920 int BSOCK::wait_data_intr(int sec, int usec)
929 FD_SET((unsigned)m_fd, &fdset);
932 switch (select(m_fd + 1, &fdset, NULL, NULL, &tv)) {
933 case 0: /* timeout */
938 return -1; /* error return */
944 if (this->tls && !tls_bsock_probe(this)) {
945 /* maybe a session key negotiation waked up the socket */
949 >>>>>>> 625e1f1... Fix compilation of bsock.c when TLS is not available
956 * This routine closes the current BSOCK.
957 * It does not delete the socket packet
958 * resources, which are released int
966 * Note, this routine closes the socket, but leaves the
967 * bsock memory in place.
974 if (bsock->is_closed()) {
980 for (; bsock; bsock = next) {
981 next = bsock->m_next; /* get possible pointer to next before destoryed */
983 bsock->set_terminated();
984 if (!bsock->m_duped) {
985 /* Shutdown tls cleanly. */
987 tls_bsock_shutdown(bsock);
988 free_tls_connection(bsock->tls);
993 if (!bsock->is_timed_out()) {
994 win_close_wait(bsock->m_fd); /* Ensure that data is not discarded */
997 if (bsock->is_timed_out()) {
998 shutdown(bsock->m_fd, SHUT_RDWR); /* discard any pending I/O */
1001 /* On Windows this discards data if we did not do a close_wait() */
1002 socketClose(bsock->m_fd); /* normal close */
1009 * Destroy the socket (i.e. release all resources)
1010 * including and duped sockets.
1012 void BSOCK::destroy()
1014 this->close(); /* Ensure that socket is closed */
1017 free_pool_memory(msg);
1020 ASSERT2(1 == 0, "Two calls to destroy socket"); /* double destroy */
1023 free_pool_memory(errmsg);
1044 /* Commands sent to Director */
1045 static char hello[] = "Hello %s calling\n";
1047 /* Response from Director */
1048 static char OKhello[] = "1000 OK:";
1051 * Authenticate Director
1053 bool BSOCK::authenticate_director(const char *name, const char *password,
1054 TLS_CONTEXT *tls_ctx, char *response, int response_len)
1056 int tls_local_need = BNET_TLS_NONE;
1057 int tls_remote_need = BNET_TLS_NONE;
1058 int compatible = true;
1059 char bashed_name[MAX_NAME_LENGTH];
1060 BSOCK *dir = this; /* for readability */
1064 * Send my name to the Director then do authentication
1067 /* Timeout Hello after 15 secs */
1068 dir->start_timer(15);
1069 dir->fsend(hello, bashed_name);
1071 if (get_tls_enable(tls_ctx)) {
1072 tls_local_need = get_tls_enable(tls_ctx) ? BNET_TLS_REQUIRED : BNET_TLS_OK;
1075 /* respond to Dir challenge */
1076 if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
1077 /* Now challenge dir */
1078 !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
1079 bsnprintf(response, response_len, _("Director authorization problem at \"%s:%d\"\n"),
1080 dir->host(), dir->port());
1084 /* Verify that the remote host is willing to meet our TLS requirements */
1085 if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
1086 bsnprintf(response, response_len, _("Authorization problem:"
1087 " Remote server at \"%s:%d\" did not advertise required TLS support.\n"),
1088 dir->host(), dir->port());
1092 /* Verify that we are willing to meet the remote host's requirements */
1093 if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
1094 bsnprintf(response, response_len, _("Authorization problem with Director at \"%s:%d\":"
1095 " Remote server requires TLS.\n"),
1096 dir->host(), dir->port());
1101 /* Is TLS Enabled? */
1103 if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
1104 /* Engage TLS! Full Speed Ahead! */
1105 if (!bnet_tls_client(tls_ctx, dir, NULL)) {
1106 bsnprintf(response, response_len, _("TLS negotiation failed with Director at \"%s:%d\"\n"),
1107 dir->host(), dir->port());
1113 Dmsg1(6, ">dird: %s", dir->msg);
1114 if (dir->recv() <= 0) {
1116 bsnprintf(response, response_len, _("Bad response to Hello command: ERR=%s\n"
1117 "The Director at \"%s:%d\" is probably not running.\n"),
1118 dir->bstrerror(), dir->host(), dir->port());
1123 Dmsg1(10, "<dird: %s", dir->msg);
1124 if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
1125 bsnprintf(response, response_len, _("Director at \"%s:%d\" rejected Hello command\n"),
1126 dir->host(), dir->port());
1129 bsnprintf(response, response_len, "%s", dir->msg);
1135 bsnprintf(response, response_len, _("Authorization problem with Director at \"%s:%d\"\n"
1136 "Most likely the passwords do not agree.\n"
1137 "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
1138 "Please see " MANUAL_AUTH_URL " for help.\n"),
1139 dir->host(), dir->port());
1143 /* Try to limit the bandwidth of a network connection
1145 void BSOCK::control_bwlimit(int bytes)
1152 now = get_current_btime(); /* microseconds */
1153 temp = now - m_last_tick; /* microseconds */
1155 m_nb_bytes += bytes;
1157 /* Less than 0.1ms since the last call, see the next time */
1162 if (temp > 10000000) { /* Take care of clock problems (>10s) */
1168 /* Remove what was authorised to be written in temp us */
1169 m_nb_bytes -= (int64_t)(temp * ((double)m_bwlimit / 1000000.0));
1171 if (m_nb_bytes < 0) {
1175 /* What exceed should be converted in sleep time */
1176 int64_t usec_sleep = (int64_t)(m_nb_bytes /((double)m_bwlimit / 1000000.0));
1177 if (usec_sleep > 100) {
1178 bmicrosleep(0, usec_sleep); /* TODO: Check that bmicrosleep slept enough or sleep again */
1179 m_last_tick = get_current_btime();
1188 * closesocket is supposed to do a graceful disconnect under Window
1189 * but it doesn't. Comments on http://msdn.microsoft.com/en-us/li
1190 * confirm this behaviour. DisconnectEx is required instead, but
1191 * that function needs to be retrieved via WS IOCTL
1194 win_close_wait(int fd)
1197 GUID disconnectex_guid = WSAID_DISCONNECTEX;
1198 DWORD bytes_returned;
1199 LPFN_DISCONNECTEX DisconnectEx;
1200 ret = WSAIoctl(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &disconnectex_guid, sizeof(disconnectex_guid), &DisconnectEx, sizeof(DisconnectEx), &bytes_returned, NULL, NULL);
1201 Dmsg1(100, "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER, WSAID_DISCONNECTEX) ret = %d\n", ret);
1203 DisconnectEx(fd, NULL, 0, 0);