2 Copyright (C) 2000-2004 Kern Sibbald and John Walker
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
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 GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * Originally written by Kern Sibbald for inclusion in apcupsd,
22 * but heavily modified for Bacula
29 #include <netinet/in.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #ifdef HAVE_ARPA_NAMESER_H
35 #include <arpa/nameser.h>
42 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
46 int allow_severity = LOG_NOTICE;
47 int deny_severity = LOG_WARNING;
50 static bool quit = false;
52 void bnet_stop_thread_server(pthread_t tid)
55 pthread_kill(tid, TIMEOUT_SIGNAL);
59 Become Threaded Network Server
60 This function is able to handle multiple server ips in
61 ipv4 and ipv6 style. The Addresse are give in a comma
62 seperated string in bind_addr
63 In the moment it is inpossible to bind different ports.
66 bnet_thread_server(dlist *addrs, int max_clients, workq_t *client_wq,
67 void *handle_client_request(void *bsock))
71 struct sockaddr cli_addr; /* client's address */
75 struct request_info request;
79 dlink link; /* this MUST be the first item */
86 char allbuf[256 * 10];
87 Dmsg1(100, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
89 foreach_dlist(p, addrs) {
90 /* Allocate on stack frame -- no need to free */
91 fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
92 fd_ptr->port = p->get_port_net_order();
96 for (tlog= 60; (fd_ptr->fd=socket(p->get_family(), SOCK_STREAM, 0)) < 0; tlog -= 10) {
100 Emsg3(M_ABORT, 0, _("Cannot open stream socket. ERR=%s. Current %s All %s\n"),
102 p->build_address_str(curbuf, sizeof(curbuf)),
103 build_addresses_str(addrs, allbuf, sizeof(allbuf)));
110 if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon,
111 sizeof(turnon)) < 0) {
113 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
117 int tmax = 30 * (60 / 5); /* wait 30 minutes max */
118 /* FIXME i can go to a endless loop i get a invalid address */
119 for (tlog = 0; bind(fd_ptr->fd, p->get_sockaddr(), p->get_sockaddr_len()) < 0; tlog -= 5) {
122 tlog = 2 * 60; /* Complain every 2 minutes */
123 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s. Retrying ...\n"),
124 ntohs(fd_ptr->port), be.strerror());
128 Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
132 listen(fd_ptr->fd, 5); /* tell system we are ready */
133 sockfds.append(fd_ptr);
135 /* Start work queue thread */
136 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
139 Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), be.strerror());
142 * Wait for a connection from the client process.
145 unsigned int maxfd = 0;
148 foreach_dlist(fd_ptr, &sockfds) {
149 FD_SET((unsigned)fd_ptr->fd, &sockset);
150 maxfd = maxfd > (unsigned)fd_ptr->fd ? maxfd : fd_ptr->fd;
153 if ((stat = select(maxfd + 1, &sockset, NULL, NULL, NULL)) < 0) {
154 berrno be; /* capture errno */
155 if (errno == EINTR || errno == EAGAIN) {
159 foreach_dlist(fd_ptr, &sockfds) {
161 free((void *)fd_ptr);
163 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.strerror());
167 foreach_dlist(fd_ptr, &sockfds) {
168 if (FD_ISSET(fd_ptr->fd, &sockset)) {
169 /* Got a connection, now accept it. */
171 clilen = sizeof(cli_addr);
172 newsockfd = accept(fd_ptr->fd, &cli_addr, &clilen);
173 } while (newsockfd < 0 && (errno == EINTR || errno == EAGAIN));
178 P(mutex); /* hosts_access is not thread safe */
179 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
181 if (!hosts_access(&request)) {
183 #ifndef HAVE_INET_NTOP
184 Jmsg2(NULL, M_SECURITY, 0,
185 _("Connection from %s:%d refused by hosts.access\n"),
186 inet_ntoa(((sockaddr_in *)&cli_addr)->sin_addr),
187 ntohs(((sockaddr_in *)&cli_addr)->sin_port));
189 Jmsg2(NULL, M_SECURITY, 0,
190 _("Connection from %s:%d refused by hosts.access\n"),
191 inet_ntop(clilen == sizeof(sockaddr_in) ? AF_INET : AF_INET6,
192 &clilen, buf, clilen),
193 ntohs(clilen == sizeof(sockaddr_in) ?
194 ((sockaddr_in *)&cli_addr)->sin_port :
195 ((sockaddr_in6 *)&cli_addr)->sin6_port));
204 * Receive notification when connection dies.
206 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon,
207 sizeof(turnon)) < 0) {
209 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
213 /* see who client is. i.e. who connected to us. */
215 #ifdef HAVE_INET_NTOP
216 inet_ntop(clilen == sizeof(sockaddr_in) ? AF_INET : AF_INET6, &clilen,
219 bstrncpy(buf, inet_ntoa(((sockaddr_in *)&cli_addr)->sin_addr), sizeof(buf)); /* NOT thread safe, use mutex */
223 bs = init_bsock(NULL, newsockfd, "client", buf, fd_ptr->port, &cli_addr);
225 Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
228 /* Queue client to be served */
229 if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
232 Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"),
239 /* Stop work queue thread */
240 if ((stat = workq_destroy(client_wq)) != 0) {
243 Emsg1(M_FATAL, 0, _("Could not destroy client queue: ERR=%s\n"),
251 * Bind an address so that we may accept connections
254 BSOCK *bnet_bind(int port)
257 struct sockaddr_in serv_addr; /* our address */
264 for (tlog = 0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10) {
265 if (errno == EINTR || errno == EAGAIN) {
270 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
278 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
279 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
284 * Bind our local address so that the client can send to us.
286 bzero((char *)&serv_addr, sizeof(serv_addr));
287 serv_addr.sin_family = AF_INET;
288 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
289 serv_addr.sin_port = htons(port);
291 for (tlog = 0; bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0;
294 if (errno == EINTR || errno == EAGAIN) {
299 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: retrying ...\n"), port,
304 listen(sockfd, 1); /* tell system we are ready */
305 return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port,
310 * Accept a single connection
312 BSOCK *bnet_accept(BSOCK * bsock, char *who)
314 fd_set ready, sockset;
315 int newsockfd, stat, len;
317 struct sockaddr_in cli_addr; /* client's address */
322 struct request_info request;
326 * Wait for a connection from the client process.
329 FD_SET((unsigned)bsock->fd, &sockset);
333 * Wait for a connection from a client process.
336 if ((stat = select(bsock->fd + 1, &ready, NULL, NULL, NULL)) < 0) {
337 if (errno == EINTR || errno = EAGAIN) {
341 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
346 clilen = sizeof(cli_addr);
347 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
348 } while (newsockfd < 0 && (errno == EINTR || errno = EAGAIN));
349 if (newsockfd >= 0) {
356 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
358 if (!hosts_access(&request)) {
360 Emsg2(M_SECURITY, 0, _("Connection from %s:%d refused by hosts.access\n"),
361 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
369 * Receive notification when connection dies.
371 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
372 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
376 /* see who client is. I.e. who connected to us.
377 * return it in the input message buffer.
379 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
380 pm_strcpy(&bsock->msg, caller);
384 bsock->msglen = strlen(bsock->msg);
387 Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
391 if (caller == NULL) {
394 len = strlen(caller) + strlen(who) + 3;
395 buf = (char *)malloc(len);
396 bstrncpy(buf, len, who);
397 bstrncat(buf, len, ": ");
398 bstrncat(buf, len, caller);
399 bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port, &cli_addr);
401 return bs; /* return new BSOCK */