2 Copyright (C) 2000, 2001, 2002 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>
31 #include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
41 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 int allow_severity = LOG_NOTICE;
46 int deny_severity = LOG_WARNING;
49 /* Become Threaded Network Server */
51 bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq,
52 void *handle_client_request(void *bsock))
54 int newsockfd, sockfd, stat;
56 struct sockaddr_in cli_addr; /* client's address */
57 struct sockaddr_in serv_addr; /* our address */
58 struct in_addr bind_ip; /* address to bind to */
60 fd_set ready, sockset;
64 struct request_info request;
70 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
73 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s. Retrying ...\n"), strerror(errno));
81 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
82 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"), strerror(errno));
86 * Bind our local address so that the client can send to us.
88 bind_ip.s_addr = htonl(INADDR_ANY);
89 if (bind_addr && bind_addr[0]) {
91 if (inet_pton(AF_INET, bind_addr, &bind_ip) <= 0) {
93 if (inet_aton(bind_addr, &bind_ip) <= 0) {
95 Emsg1(M_WARNING, 0, _("Invalid bind address: %s, using INADDR_ANY\n"),
97 bind_ip.s_addr = htonl(INADDR_ANY);
100 memset((char *) &serv_addr, 0, sizeof(serv_addr));
101 serv_addr.sin_family = AF_INET;
102 serv_addr.sin_addr.s_addr = bind_ip.s_addr;
103 serv_addr.sin_port = htons(port);
105 int tmax = 30 * (60 / 5); /* wait 30 minutes max */
106 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
108 tlog = 2*60; /* Complain every 2 minutes */
109 Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s. Retrying ...\n"), port, strerror(errno));
113 Emsg2(M_ABORT, 0, _("Cannot bind port %d: %s.\n"), port, strerror(errno));
116 listen(sockfd, 5); /* tell system we are ready */
119 FD_SET(sockfd, &sockset);
121 /* Start work queue thread */
122 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
123 Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), strerror(stat));
128 * Wait for a connection from a client process.
131 if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
132 if (errno == EINTR || errno == EAGAIN) {
137 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
140 clilen = sizeof(cli_addr);
141 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
144 P(mutex); /* hosts_access is not thread safe */
145 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
147 if (!hosts_access(&request)) {
149 Jmsg2(NULL, M_WARNING, 0, _("Connection from %s:%d refused by hosts.access"),
150 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
158 * Receive notification when connection dies.
160 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
161 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n") , strerror(errno));
164 /* see who client is. i.e. who connected to us. */
166 caller = inet_ntoa(cli_addr.sin_addr); /* NOT thread safe, use mutex */
167 if (caller == NULL) {
168 caller = "unknown client";
171 /* Queue client to be served */
172 if ((stat = workq_add(client_wq,
173 (void *)init_bsock(NULL, newsockfd, "client", caller, port), NULL, 0)) != 0) {
175 Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"), strerror(stat));
184 * Bind an address so that we may accept connections
191 struct sockaddr_in serv_addr; /* our address */
198 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
201 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
209 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
210 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n") , strerror(errno));
214 * Bind our local address so that the client can send to us.
216 bzero((char *) &serv_addr, sizeof(serv_addr));
217 serv_addr.sin_family = AF_INET;
218 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
219 serv_addr.sin_port = htons(port);
221 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
224 Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s: retrying ...\n"), port, strerror(errno));
228 listen(sockfd, 1); /* tell system we are ready */
229 return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port);
233 * Accept a single connection
236 bnet_accept(BSOCK *bsock, char *who)
238 fd_set ready, sockset;
239 int newsockfd, stat, len;
241 struct sockaddr_in cli_addr; /* client's address */
246 struct request_info request;
250 * Wait for a connection from the client process.
253 FD_SET(bsock->fd, &sockset);
257 * Wait for a connection from a client process.
260 if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
261 if (errno == EINTR || errno == EAGAIN) {
265 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
269 clilen = sizeof(cli_addr);
270 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
276 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
278 if (!hosts_access(&request)) {
280 Emsg2(M_WARNING, 0, _("Connection from %s:%d refused by hosts.access"),
281 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
289 * Receive notification when connection dies.
291 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
292 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"), strerror(errno));
295 /* see who client is. I.e. who connected to us.
296 * return it in the input message buffer.
298 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
299 pm_strcpy(&bsock->msg, caller);
303 bsock->msglen = strlen(bsock->msg);
306 Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
310 if (caller == NULL) {
313 len = strlen(caller) + strlen(who) + 3;
314 buf = (char *) malloc(len);
318 bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port);
320 return bs; /* return new BSOCK */