-
-/*
- * Accept a single connection
- */
-BSOCK *
-bnet_accept(BSOCK *bsock, char *who)
-{
- fd_set ready, sockset;
- int newsockfd, stat, len;
- socklen_t clilen;
- struct sockaddr_in cli_addr; /* client's address */
- char *caller, *buf;
- BSOCK *bs;
- int turnon = 1;
-#ifdef HAVE_LIBWRAP
- 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;
- if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
- if (errno == EINTR || errno = EAGAIN) {
- errno = 0;
- continue;
- }
- Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
- newsockfd = -1;
- break;
- }
- do {
- clilen = sizeof(cli_addr);
- newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
- } while (newsockfd < 0 && (errno == EINTR || errno = EAGAIN));
- if (newsockfd >= 0) {
- break;
- }
- }
-
-#ifdef HAVE_LIBWRAP
- P(mutex);
- request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
- fromhost(&request);
- if (!hosts_access(&request)) {
- V(mutex);
- Emsg2(M_SECURITY, 0, _("Connection from %s:%d refused by hosts.access\n"),
- inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
- close(newsockfd);
- return NULL;
- }
- V(mutex);
-#endif
-
- /*
- * Receive notification when connection dies.
- */
- 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));
- }
-
- /* see who client is. I.e. who connected to us.
- * return it in the input message buffer.
- */
- if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
- pm_strcpy(&bsock->msg, caller);
- } else {
- bsock->msg[0] = 0;
- }
- bsock->msglen = strlen(bsock->msg);
-
- if (newsockfd < 0) {
- Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
- strerror(errno));
- return NULL;
- } else {
- if (caller == NULL) {
- caller = "unknown";
- }
- len = strlen(caller) + strlen(who) + 3;
- buf = (char *) malloc(len);
- strcpy(buf, who);
- strcat(buf, ": ");
- strcat(buf, caller);
- bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port, &cli_addr);
- free(buf);
- return bs; /* return new BSOCK */
- }
-}
-
-#endif