MA 02111-1307, USA.
*/
- /*
+ /*
* Originally written by Kern Sibbald for inclusion in apcupsd,
* but heavily modified for Bacula
*
*/
#include "bacula.h"
-#undef DEV_BSIZE
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
void bnet_stop_thread_server(pthread_t tid)
{
quit = true;
- pthread_kill(tid, TIMEOUT_SIGNAL);
+ if (!pthread_equal(tid, pthread_self())) {
+ pthread_kill(tid, TIMEOUT_SIGNAL);
+ }
}
-/*
- Become Threaded Network Server
+/*
+ Become Threaded Network Server
This function is able to handle multiple server ips in
ipv4 and ipv6 style. The Addresse are give in a comma
seperated string in bind_addr
char buf[128];
dlist sockfds;
- char allbuf[256 * addrs->size()];
+ char allbuf[256 * 10];
Dmsg1(100, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
foreach_dlist(p, addrs) {
/* Allocate on stack frame -- no need to free */
fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
- fd_ptr->port = p->get_port();
+ fd_ptr->port = p->get_port_net_order();
/*
- * Open a TCP socket
+ * Open a TCP socket
*/
for (tlog= 60; (fd_ptr->fd=socket(p->get_family(), SOCK_STREAM, 0)) < 0; tlog -= 10) {
if (tlog <= 0) {
char curbuf[256];
Emsg3(M_ABORT, 0, _("Cannot open stream socket. ERR=%s. Current %s All %s\n"),
be.strerror(),
- p->build_address_str(curbuf, sizeof(curbuf)),
+ p->build_address_str(curbuf, sizeof(curbuf)),
build_addresses_str(addrs, allbuf, sizeof(allbuf)));
}
bmicrosleep(10, 0);
}
/*
- * Reuse old sockets
+ * Reuse old sockets
*/
- if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t) & turnon,
+ if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon,
sizeof(turnon)) < 0) {
berrno be;
Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
if (tlog <= 0) {
tlog = 2 * 60; /* Complain every 2 minutes */
Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s. Retrying ...\n"),
- fd_ptr->port, be.strerror());
+ ntohs(fd_ptr->port), be.strerror());
}
bmicrosleep(5, 0);
if (--tmax <= 0) {
- Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), fd_ptr->port,
+ Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
be.strerror());
}
}
be.set_errno(stat);
Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), be.strerror());
}
- /*
+ /*
* Wait for a connection from the client process.
*/
for (; !quit;) {
fromhost(&request);
if (!hosts_access(&request)) {
V(mutex);
-#ifndef HAVE_INET_NTOP
Jmsg2(NULL, M_SECURITY, 0,
_("Connection from %s:%d refused by hosts.access\n"),
- inet_ntoa(((sockaddr_in *)&cli_addr)->sin_addr),
- ntohs(((sockaddr_in *)&cli_addr)->sin_port));
-#else
- Jmsg2(NULL, M_SECURITY, 0,
- _("Connection from %s:%d refused by hosts.access\n"),
- inet_ntop(clilen == sizeof(sockaddr_in) ? AF_INET : AF_INET6,
- &clilen, buf, clilen),
- ntohs(clilen == sizeof(sockaddr_in) ?
- ((sockaddr_in *)&cli_addr)->sin_port :
- ((sockaddr_in6 *)&cli_addr)->sin6_port));
-#endif
+ sockaddr_to_ascii(&cli_addr, buf, sizeof(buf)),
+ sockaddr_get_port(&cli_addr));
close(newsockfd);
continue;
}
/*
* Receive notification when connection dies.
*/
- if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t) & turnon,
+ if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon,
sizeof(turnon)) < 0) {
berrno be;
Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
/* see who client is. i.e. who connected to us. */
P(mutex);
-#ifndef HAVE_INET_NTOP
- bstrncpy(buf, inet_ntoa(((sockaddr_in *) & cli_addr)->sin_addr), sizeof(buf)); /* NOT thread safe, use mutex */
-#else
- inet_ntop(clilen == sizeof(sockaddr_in) ? AF_INET : AF_INET6, &clilen,
- buf, clilen);
-#endif
- /* possible release of the mutex */
-
- BSOCK *bs =
- init_bsock(NULL, newsockfd, "client", buf, fd_ptr->port, &cli_addr);
+ sockaddr_to_ascii(&cli_addr, buf, sizeof(buf));
+ V(mutex);
+ BSOCK *bs;
+ bs = init_bsock(NULL, newsockfd, "client", buf, fd_ptr->port, &cli_addr);
if (bs == NULL) {
Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
}
/* Queue client to be served */
if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
berrno be;
- V(mutex);
be.set_errno(stat);
- Jmsg1(NULL, M_ABORT, 0,
- _("Could not add job to client queue: ERR=%s\n"),
+ Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"),
be.strerror());
}
- V(mutex);
}
}
}
int turnon = 1;
/*
- * Open a TCP socket
+ * Open a TCP socket
*/
for (tlog = 0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10) {
if (errno == EINTR || errno == EAGAIN) {
}
/*
- * Reuse old sockets
+ * Reuse old sockets
*/
- if (setsockopt
- (sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t) & turnon,
- sizeof(turnon)) < 0) {
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
strerror(errno));
}
- /*
+ /*
* Bind our local address so that the client can send to us.
*/
bzero((char *)&serv_addr, sizeof(serv_addr));
}
/*
- * Accept a single connection
+ * Accept a single connection
*/
BSOCK *bnet_accept(BSOCK * bsock, char *who)
{
struct request_info request;
#endif
- /*
+ /*
* Wait for a connection from the client process.
*/
FD_ZERO(&sockset);
FD_SET((unsigned)bsock->fd, &sockset);
for (;;) {
- /*
+ /*
* Wait for a connection from a client process.
*/
ready = sockset;
/*
* Receive notification when connection dies.
*/
- if (setsockopt
- (newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t) & turnon,
- sizeof(turnon)) < 0) {
+ if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
strerror(errno));
}