-
- /*
- * Open a TCP socket
- */
- for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
- if (tlog <= 0) {
- tlog = 60;
- Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s. Retrying ...\n"), strerror(errno));
- }
- sleep(10);
- }
-
- /*
- * Reuse old sockets
- */
- 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.
- */
- bind_ip.s_addr = htonl(INADDR_ANY);
- if (bind_addr && bind_addr[0]) {
-#ifdef HAVE_INET_PTON
- if (inet_pton(AF_INET, bind_addr, &bind_ip) <= 0) {
-#else
- if (inet_aton(bind_addr, &bind_ip) <= 0) {
-#endif
- Emsg1(M_WARNING, 0, _("Invalid bind address: %s, using INADDR_ANY\n"),
- bind_addr);
- bind_ip.s_addr = htonl(INADDR_ANY);
- }
- }
- memset((char *) &serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = bind_ip.s_addr;
- serv_addr.sin_port = htons(port);
-
- int tmax = 30 * (60 / 5); /* wait 30 minutes max */
- for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
- if (tlog <= 0) {
- tlog = 2*60; /* Complain every 2 minutes */
- Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s. Retrying ...\n"), port, strerror(errno));
- }
- sleep(5);
- if (--tmax <= 0) {
- Emsg2(M_ABORT, 0, _("Cannot bind port %d: %s.\n"), port, strerror(errno));
- }
- }
- listen(sockfd, 5); /* tell system we are ready */
-
- FD_ZERO(&sockset);
- FD_SET(sockfd, &sockset);
-
- /* Start work queue thread */
- if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
- Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), strerror(stat));
- }
-
- for (;;) {
- /*
- * Wait for a connection from a client process.
+ IPADDR *p;
+ struct s_sockfd {
+ dlink link; /* this MUST be the first item */
+ int fd;
+ int port;
+ } *fd_ptr = NULL;
+ char buf[128];
+ dlist sockfds;
+
+ char allbuf[256 * 10];
+ Dmsg1(100, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
+
+ foreach_dlist(p, addrs) {
+ /* Allocate on stack from -- no need to free */
+ fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
+ fd_ptr->port = p->get_port_net_order();
+ /*
+ * Open a TCP socket