2 Copyright (C) 2000-2005 Kern Sibbald
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as amended with additional clauses defined in the
7 file LICENSE in the main source directory.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 the file LICENSE for additional details.
16 * Originally written by Kern Sibbald for inclusion in apcupsd,
17 * but heavily modified for Bacula
23 #include <netinet/in.h>
24 #include <sys/socket.h>
26 #include <arpa/inet.h>
28 #ifdef HAVE_ARPA_NAMESER_H
29 #include <arpa/nameser.h>
36 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40 int allow_severity = LOG_NOTICE;
41 int deny_severity = LOG_WARNING;
44 static bool quit = false;
46 void bnet_stop_thread_server(pthread_t tid)
49 if (!pthread_equal(tid, pthread_self())) {
50 pthread_kill(tid, TIMEOUT_SIGNAL);
55 Become Threaded Network Server
56 This function is able to handle multiple server ips in
57 ipv4 and ipv6 style. The Addresse are give in a comma
58 seperated string in bind_addr
59 In the moment it is inpossible to bind different ports.
62 bnet_thread_server(dlist *addrs, int max_clients, workq_t *client_wq,
63 void *handle_client_request(void *bsock))
67 struct sockaddr cli_addr; /* client's address */
71 struct request_info request;
75 dlink link; /* this MUST be the first item */
82 char allbuf[256 * 10];
83 Dmsg1(100, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
85 foreach_dlist(p, addrs) {
86 /* Allocate on stack frame -- no need to free */
87 fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
88 fd_ptr->port = p->get_port_net_order();
92 for (tlog= 60; (fd_ptr->fd=socket(p->get_family(), SOCK_STREAM, 0)) < 0; tlog -= 10) {
96 Emsg3(M_ABORT, 0, _("Cannot open stream socket. ERR=%s. Current %s All %s\n"),
98 p->build_address_str(curbuf, sizeof(curbuf)),
99 build_addresses_str(addrs, allbuf, sizeof(allbuf)));
106 if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon,
107 sizeof(turnon)) < 0) {
109 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
113 int tmax = 30 * (60 / 5); /* wait 30 minutes max */
114 for (tlog = 0; bind(fd_ptr->fd, p->get_sockaddr(), p->get_sockaddr_len()) < 0; tlog -= 5) {
117 tlog = 2 * 60; /* Complain every 2 minutes */
118 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: Retrying ...\n"),
119 ntohs(fd_ptr->port), be.strerror());
123 Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
127 listen(fd_ptr->fd, 5); /* tell system we are ready */
128 sockfds.append(fd_ptr);
130 /* Start work queue thread */
131 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
134 Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), be.strerror());
137 * Wait for a connection from the client process.
140 unsigned int maxfd = 0;
143 foreach_dlist(fd_ptr, &sockfds) {
144 FD_SET((unsigned)fd_ptr->fd, &sockset);
145 maxfd = maxfd > (unsigned)fd_ptr->fd ? maxfd : fd_ptr->fd;
148 if ((stat = select(maxfd + 1, &sockset, NULL, NULL, NULL)) < 0) {
149 berrno be; /* capture errno */
150 if (errno == EINTR) {
154 foreach_dlist(fd_ptr, &sockfds) {
157 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.strerror());
161 foreach_dlist(fd_ptr, &sockfds) {
162 if (FD_ISSET(fd_ptr->fd, &sockset)) {
163 /* Got a connection, now accept it. */
165 clilen = sizeof(cli_addr);
166 newsockfd = accept(fd_ptr->fd, &cli_addr, &clilen);
167 } while (newsockfd < 0 && errno == EINTR);
172 P(mutex); /* hosts_access is not thread safe */
173 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
175 if (!hosts_access(&request)) {
177 Jmsg2(NULL, M_SECURITY, 0,
178 _("Connection from %s:%d refused by hosts.access\n"),
179 sockaddr_to_ascii(&cli_addr, buf, sizeof(buf)),
180 sockaddr_get_port(&cli_addr));
188 * Receive notification when connection dies.
190 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon,
191 sizeof(turnon)) < 0) {
193 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
197 /* see who client is. i.e. who connected to us. */
199 sockaddr_to_ascii(&cli_addr, buf, sizeof(buf));
202 bs = init_bsock(NULL, newsockfd, "client", buf, fd_ptr->port, &cli_addr);
204 Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
207 /* Queue client to be served */
208 if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
211 Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"),
218 /* Stop work queue thread */
219 if ((stat = workq_destroy(client_wq)) != 0) {
222 Emsg1(M_FATAL, 0, _("Could not destroy client queue: ERR=%s\n"),
230 * Bind an address so that we may accept connections
233 BSOCK *bnet_bind(int port)
236 struct sockaddr_in serv_addr; /* our address */
243 for (tlog = 0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10) {
244 if (errno == EINTR || errno == EAGAIN) {
249 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
257 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
258 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
263 * Bind our local address so that the client can send to us.
265 bzero((char *)&serv_addr, sizeof(serv_addr));
266 serv_addr.sin_family = AF_INET;
267 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
268 serv_addr.sin_port = htons(port);
270 for (tlog = 0; bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0;
273 if (errno == EINTR || errno == EAGAIN) {
278 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: retrying ...\n"), port,
283 listen(sockfd, 1); /* tell system we are ready */
284 return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port,
289 * Accept a single connection
291 BSOCK *bnet_accept(BSOCK * bsock, char *who)
293 fd_set ready, sockset;
294 int newsockfd, stat, len;
296 struct sockaddr_in cli_addr; /* client's address */
301 struct request_info request;
305 * Wait for a connection from the client process.
308 FD_SET((unsigned)bsock->fd, &sockset);
312 * Wait for a connection from a client process.
315 if ((stat = select(bsock->fd + 1, &ready, NULL, NULL, NULL)) < 0) {
316 if (errno == EINTR || errno = EAGAIN) {
320 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
325 clilen = sizeof(cli_addr);
326 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
327 } while (newsockfd < 0 && (errno == EINTR || errno = EAGAIN));
328 if (newsockfd >= 0) {
335 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
337 if (!hosts_access(&request)) {
339 Emsg2(M_SECURITY, 0, _("Connection from %s:%d refused by hosts.access\n"),
340 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
348 * Receive notification when connection dies.
350 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
351 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
355 /* see who client is. I.e. who connected to us.
356 * return it in the input message buffer.
358 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
359 pm_strcpy(&bsock->msg, caller);
363 bsock->msglen = strlen(bsock->msg);
366 Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
370 if (caller == NULL) {
371 caller = _("unknown");
373 len = strlen(caller) + strlen(who) + 3;
374 buf = (char *)malloc(len);
375 bstrncpy(buf, len, who);
376 bstrncat(buf, len, ": ");
377 bstrncat(buf, len, caller);
378 bs = init_bsock(NULL, newsockfd, _("client"), buf, bsock->port, &cli_addr);
380 return bs; /* return new BSOCK */