Exemple #1
0
static int do_accept(int sock, struct sockaddr *addr, socklen_t *len)
{
    int c = -1;
    while ( true ) {
        c = accept4(sock, addr, len, SOCK_NONBLOCK);
        if ( c >= 0 ) {
            break;
        }
        if ( errno == EAGAIN || errno == EINTR || errno == ECONNABORTED) {
            continue;
        }
        break;
    }
    return c;
}
Exemple #2
0
/**
 * @brief Accept connections on a passive UNIX socket
 *
 * @param sfd The server socket
 * @param flags Flags for `accept4(3)` (therefore useless on any other system than Linux)
 *
 * @retval >0 Return value is a socket connected to the client
 * @retval <0 Error at `accept[4]()`
 */
int accept_unix_stream_socket(int sfd, int flags)
{
    int cfd;

    if ( sfd < 0 )
	return -1;
# ifdef __linux__
    if ( -1 == check_error(cfd = accept4(sfd,0,0,flags)) )
	return -1;
# else
    if ( -1 == check_error(cfd = accept(sfd,0,0)) )
	return -1;
# endif
    return cfd;
}
int target_accept(struct target *t, int sockfd)
{
	int sock;

	sock = accept4(sockfd, NULL, NULL, SOCK_CLOEXEC);
	if (sock == UNKNOWN_FD)
		return 1;

	/* accept succeed */
	fd_setup_attributes(sock);

	t->socket = sock;

	return 0;
}
int TCPServer::acceptClientConnection()
{
	int nClients = 0;
	while(1) {
		struct sockaddr_in client_address;
		unsigned int sockaddr_in_len = (sizeof(struct sockaddr_in));
		int clientId = accept4(descriptor, (struct sockaddr *) &client_address, &sockaddr_in_len, SOCK_CLOEXEC);
		if( clientId > 0 )
			createNewClient( clientId );
		else
			break;
		nClients++;
	}
	return nClients;
}
Exemple #5
0
static void accept_loop() {
  while (!stopping) {
    int client_fd;
    struct pollfd pfd;
    int bufsize;
    w_stm_t stm;

#ifdef HAVE_LIBGIMLI_H
    if (hb) {
      gimli_heartbeat_set(hb, GIMLI_HB_RUNNING);
    }
#endif

    pfd.events = POLLIN;
    pfd.fd = listener_fd;
    if (poll(&pfd, 1, 60000) < 1 || (pfd.revents & POLLIN) == 0) {
      if (stopping) {
        break;
      }
      // Timed out, or error.
      // Arrange to sanity check that we're working
      w_check_my_sock();
      continue;
    }

#ifdef HAVE_ACCEPT4
    client_fd = accept4(listener_fd, NULL, 0, SOCK_CLOEXEC);
#else
    client_fd = accept(listener_fd, NULL, 0);
#endif
    if (client_fd == -1) {
      continue;
    }
    w_set_cloexec(client_fd);
    bufsize = WATCHMAN_IO_BUF_SIZE;
    setsockopt(client_fd, SOL_SOCKET, SO_SNDBUF,
        (void*)&bufsize, sizeof(bufsize));

    stm = w_stm_fdopen(client_fd);
    if (!stm) {
      w_log(W_LOG_ERR, "Failed to allocate stm for fd: %s\n",
          strerror(errno));
      close(client_fd);
      continue;
    }
    make_new_client(stm);
  }
}
Exemple #6
0
static void
manage_server_event (struct item_s *it, uint32_t evt)
{
    struct sockaddr_storage ss;
    int cli, rc, opt;

    ASSERT (evt & EPOLLIN);
    (void) evt;
    socklen_t sslen = sizeof (ss);

    cli = accept4 (it->fd, SA (&ss), &sslen, O_NONBLOCK | O_CLOEXEC);
    if (cli >= 0) {

        opt = PIPE_SIZE / 2;
        setsockopt (cli, SOL_SOCKET, SO_RCVBUF, &opt, sizeof (opt));
        opt = PIPE_SIZE;
        setsockopt (cli, SOL_SOCKET, SO_SNDBUF, &opt, sizeof (opt));

        sock_set_chatty (cli, 1);

        struct item_s *c = malloc (sizeof (struct item_s));

        c->loaded = 0;
        c->pfd[0] = c->pfd[1] = -1;
        if (0 > (rc = pipe2 (c->pfd, O_NONBLOCK | O_CLOEXEC))) {
            (void) close (cli);
            free (c);
            return;
        }
        c->fd = cli;
        c->events = EPOLLIN;
        c->type = CLIENT;
        c->shut = 0;
        fcntl (c->pfd[1], F_SETPIPE_SZ, PIPE_SIZE);

        struct epoll_event evt;

retry_add:
        evt.data.ptr = c;
        evt.events = EPOLLIN;
        rc = epoll_ctl (fd_epoll, EPOLL_CTL_ADD, cli, &evt);
        if (rc < 0) {
            if (rc == EINTR)
                goto retry_add;
            abort ();
        }
    }
}
Exemple #7
0
void WebSocket::acceptConnections() {
    int ConnectFD = accept4(socketFD, NULL, NULL, SOCK_NONBLOCK);

    if(0 > ConnectFD && EAGAIN != errno && EWOULDBLOCK != errno) {
        perror("error accept failed");
    }
    if(ConnectFD > 0) {
        //std::cout << "connection: " << ConnectFD << "\n";
        Connection* connection = new Connection(ConnectFD);
        //call socketlistener to pass connection
        for(unsigned int i = 0; i < socketListeners.size(); i++) {
            socketListeners[i]->onConnect(connection);
        }
        conn.push_back(connection);
    }
}
Exemple #8
0
int
wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
	int fd;

#ifdef HAVE_ACCEPT4
	fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
	if (fd >= 0)
		return fd;
	if (errno != ENOSYS)
		return -1;
#endif

	fd = accept(sockfd, addr, addrlen);
	return set_cloexec_or_close(fd);
}
Exemple #9
0
/* ARGSUSED */
void
control_accept(int listenfd, short event, void *arg)
{
	int			 connfd;
	socklen_t		 len;
	struct sockaddr_un	 sun;
	struct ctl_conn		*c;
	struct control_sock	*cs = arg;

	event_add(&cs->cs_ev, NULL);
	if ((event & EV_TIMEOUT))
		return;

	len = sizeof(sun);
	if ((connfd = accept4(listenfd,
	    (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) {
		/*
		 * Pause accept if we are out of file descriptors, or
		 * libevent will haunt us here too.
		 */
		if (errno == ENFILE || errno == EMFILE) {
			struct timeval evtpause = { 1, 0 };

			event_del(&cs->cs_ev);
			evtimer_add(&cs->cs_evt, &evtpause);
		} else if (errno != EWOULDBLOCK && errno != EINTR &&
		    errno != ECONNABORTED)
			log_warn("%s: accept", __func__);
		return;
	}

	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
		close(connfd);
		log_warn("%s: calloc", __func__);
		return;
	}

	imsg_init(&c->iev.ibuf, connfd);
	c->iev.handler = control_dispatch_imsg;
	c->iev.events = EV_READ;
	c->iev.data = cs;	/* proc.c cheats (reuses the handler) */
	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
	    c->iev.handler, cs);
	event_add(&c->iev.ev, NULL);

	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
}
Exemple #10
0
/******************************************************

int proxy: listens for connection requests

******************************************************/
int proxy(uint16_t port)
{
	/* Create server socket*/
	int sock, accepted_client;
	unsigned int client_addr_len;
	struct sockaddr_in socket_addr, client_addr;

	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Create socket error:");
		return 1;
	}

	printf("Socket created (proxy)\n");

	socket_addr.sin_addr.s_addr = htonl(INADDR_ANY); //should this be changed to localhost?
	socket_addr.sin_family = AF_INET;
	socket_addr.sin_port = htons(port);
	
	/*Bind socket to an address*/
	if (bind(sock, (struct sockaddr *) &socket_addr, sizeof(socket_addr)) < 0) {
		perror("Bind error");
		return 1;
	}
	
	
	printf("Listening for connections...\n");
	/*Listen for connections */
	listen(sock, 0);
	
	while (1){
		/* Accept the connection */	
	   	accepted_client = accept4(sock, (struct sockaddr *) &client_addr, &client_addr_len,0);
		if (accepted_client < 0) {
	      		perror("Accept error");
	      		return 1;
	      	}
		printf("Accepted client: %i \n", accepted_client);
		
		pthread_t tid;
		int result;
		if((result = pthread_create(&tid, NULL, (void *)receive_func, (void *)&accepted_client))){ 
			perror("Creation of thread failed"); 
			return 1; 
		}
	}
	return 0;
}
Exemple #11
0
/* ARGSUSED */
void
control_accept(int listenfd, short event, void *bula)
{
	int			 connfd;
	socklen_t		 len;
	struct sockaddr_un	 sun;
	struct ctl_conn		*c;

	event_add(&control_state.ev, NULL);
	if ((event & EV_TIMEOUT))
		return;

	len = sizeof(sun);
	if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
	    SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
		/*
		 * Pause accept if we are out of file descriptors, or
		 * libevent will haunt us here too.
		 */
		if (errno == ENFILE || errno == EMFILE) {
			struct timeval evtpause = { 1, 0 };

			event_del(&control_state.ev);
			evtimer_add(&control_state.evt, &evtpause);
		} else if (errno != EWOULDBLOCK && errno != EINTR &&
		    errno != ECONNABORTED)
			log_warn("control_accept: accept");
		return;
	}

	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
		log_warn("control_accept");
		close(connfd);
		return;
	}

	imsg_init(&c->iev.ibuf, connfd);
	c->iev.handler = control_dispatch_imsg;
	c->iev.events = EV_READ;
	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
	    c->iev.handler, &c->iev);
	event_add(&c->iev.ev, NULL);

	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
}
Exemple #12
0
/**
 * Process all incoming connections.
 */
static void
do_accept (const int listensock, const int epollfd)
{
    for (;;) {
        int                         client_socket;
        struct sockaddr_storage     peer_addr;
        socklen_t                   peer_addr_len;
        struct command              *command;


        peer_addr_len = sizeof (peer_addr);
        /* TODO use accept4() instead of accept and mk_nonblock, saves calls to
         * fcntl and userspace flag bit twiddling. */
#if 0
        client_socket = accept (listensock, (struct sockaddr *) &peer_addr,
                                &peer_addr_len);
#endif
        client_socket = accept4 (listensock, (struct sockaddr *) &peer_addr,
                                 &peer_addr_len, SOCK_NONBLOCK);

        if (client_socket < 0) {
            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                /* processed all incoming connections */
                break;
            } else {
                perror ("do_accept");
                break;
            }
        }

#if 0
        if (mk_nonblock (client_socket) < 0) {
            err (1, "Could not make client socket non-blocking");
        }
#endif

        /* create read client command */
        command = calloc (1, sizeof (struct command));
        command->cmd = READ_CMD;
        command->cfd = client_socket;

        /* add command to epoll event queue */
        epoll_add (epollfd, command);
    }
}
Exemple #13
0
static void *proxy_thread(void *data)
{
    int lfd = (intptr_t)data;

    for (;;)
    {
        int cfd = accept4(lfd, NULL, NULL, SOCK_CLOEXEC);
        if (cfd == -1)
            continue;

        int canc = vlc_savecancel();
        proxy_client_process(cfd);
        close(cfd);
        connection_count++;
        vlc_restorecancel(canc);
    }
    vlc_assert_unreachable();
}
Exemple #14
0
void __fcgi_runServer(struct fcgi_server* server) {
	while (1) {
		struct sockaddr* addr = __fcgi_xmalloc(sizeof(struct sockaddr_in6));
		socklen_t len = sizeof(struct sockaddr_in6);
		int fd = accept4(server->fd, addr, &len, SOCK_CLOEXEC);
		if (fd < 0) {
			__fcgi_xfree(addr);
			continue;
		}
		struct fcgi_conn* fc = __fcgi_xmalloc(sizeof(struct fcgi_conn));
		fc->fd = fd;
		fc->addr = addr;
		fc->addrlen = len;
		fc->server = server;
		pthread_t pt;
		pthread_create(&pt, NULL, (void*) __fcgi_runWork, fc);
	}
}
Exemple #15
0
int se_socket_accept(int sock, int *pnewsock)
{
	int newsock;
	int err;

	newsock = accept4(sock, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
	if (newsock < 0)
		return errno;

	err = se_socket_config(newsock);
	if (err) {
		close(newsock);
		return err;
	}

	*pnewsock = newsock;
	return 0;
}
Exemple #16
0
static void http_server_accept_connections(void) {
    struct http_server **server_ref = (struct http_server **)current_ctx->reserved;
    struct http_server *server = *server_ref;
    for (;; yield()) {
        struct sockaddr_in new_addr;
        socklen_t new_addr_size = sizeof(struct sockaddr_in);
        int fd = accept4(server->fd, (struct sockaddr *)&new_addr, &new_addr_size, SOCK_CLOEXEC | SOCK_NONBLOCK);
        if (0 > fd)
            continue;

        if (0 > ribs_epoll_add(fd, EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP, server->idle_ctx)) {
            close(fd);
            continue;
        }

        timeout_handler_add_fd_data(&server->timeout_handler, epoll_worker_fd_map + fd);
    }
}
ClientSocket ServerSocket::next(int flags) {
  struct sockaddr_storage peer_addr;
  socklen_t peer_addr_size = sizeof(sockaddr_storage);
  memset(&peer_addr, 0, sizeof(peer_addr));

  //Poll the socket to see if a new connection has arrived
  //Wait until data is ready
  pollfd ufd;
  ufd.fd = sock_fd;
  ufd.events = POLLIN;
  //Wait up to 10 milliseconds for the poll to finish
  int result = poll(&ufd, 1, 10);
  //Accept a new socket is an input event is polled
  if (result == 1 and (ufd.revents & (POLLERR | POLLHUP | POLLNVAL)) == 0) {
    int in_sock = accept4(sock_fd, (struct sockaddr*)&peer_addr, &peer_addr_size, flags);

    std::string ip = "";

    if (0 <= in_sock) {
      //Get the ip address of this new connection
      char addr_buf[1000] = {0};
      int err = getnameinfo((struct sockaddr*)&peer_addr, peer_addr_size,
          addr_buf, sizeof(addr_buf) - 1, NULL, 0, NI_NUMERICHOST);
      if (err != 0) {
        std::cerr<<"Error getting client address: "<<gai_strerror(errno)<<'\n';
      }

      ip = std::string(addr_buf);
      std::cerr<<"Found ip address "<<addr_buf<<'\n';
    }
    //Don't print an error if the socket is simply non-blocking
    else if (errno != EAGAIN and errno != EWOULDBLOCK) {
      std::cerr<<"Socket failure: "<<strerror(errno)<<"\n";
    }

    //Control of the socket is handed over to the ClientSocket class.
    //The socket may be invalid if no new connection was available
    return ClientSocket(_port, ip, in_sock);
  }
  else {
    //Return an invalid socket
    return ClientSocket(_port, "", -1);
  }
}
Exemple #18
0
F_NONNULL
static css_conn_t* css_accept(css_t* css, ev_io* w)
{
    const int fd = accept4(w->fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);

    if (unlikely(fd < 0)) {
        switch (errno) {
        case EAGAIN:
#if EWOULDBLOCK != EAGAIN
        case EWOULDBLOCK:
#endif
        case EINTR:
            break;
        default:
            log_err("control socket early connection failure: %s", logf_errno());
            break;
        }
        return NULL;
    }

    // set up the per-connection state and start reading requests...
    css_conn_t* c = xcalloc(sizeof(*c));
    c->css = css;
    c->fd = fd;
    ev_io* w_read = &c->w_read;
    ev_io_init(w_read, css_conn_read, fd, EV_READ);
    ev_io* w_write = &c->w_write;
    ev_io_init(w_write, css_conn_write, fd, EV_WRITE);
    w_read->data = c;
    w_write->data = c;

    // set up buffer/watcher state to read input length
    c->state = READING_REQ;
    ev_io_start(css->loop, w_read);

    // insert into front of linked list
    if (css->clients) {
        c->next = css->clients;
        css->clients->prev = c;
    }
    css->clients = c;

    return c;
}
Exemple #19
0
static void server(int use_accept4, int pass_addr) {
  struct sockaddr_un addr;
  int listenfd;
  pid_t child;
  int servefd;
  struct sockaddr_un peer_addr;
  socklen_t len = sizeof(peer_addr);
  int status;
  struct sockaddr* peer_addr_ptr =
      pass_addr ? (struct sockaddr*)&peer_addr : NULL;
  socklen_t* len_ptr = pass_addr ? &len : NULL;

  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, "socket.unix", sizeof(addr.sun_path) - 1);

  test_assert(0 <= (listenfd = socket(AF_UNIX, SOCK_STREAM, 0)));
  test_assert(0 == bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)));
  test_assert(0 == listen(listenfd, 1));

  if (0 == (child = fork())) {
    client(&addr);
    test_assert("Not reached" && 0);
  }

  if (use_accept4) {
    test_assert(0 <= (servefd = accept4(listenfd, peer_addr_ptr, len_ptr, 0)));
  } else {
    test_assert(0 <= (servefd = accept(listenfd, peer_addr_ptr, len_ptr)));
  }
  if (pass_addr) {
    test_assert(AF_UNIX == peer_addr.sun_family);
  }

  test_assert(1 == send(servefd, "!", 1, 0));

  test_assert(child == waitpid(child, &status, 0));
  test_assert(WIFEXITED(status) && 0 == WEXITSTATUS(status));

  unlink(addr.sun_path);
  close(servefd);
  close(listenfd);
}
Exemple #20
0
/*
 * We have a new incoming connection.  Accept the connection and start polling
 * for incoming packets.
 */
static void
incoming_connection(int fd, short revents, void *arg)
{
	struct sockaddr sa;
	socklen_t sa_len;
	int client_fd;
	struct node *node;

	for(;;) {
		sa_len = sizeof(sa);
		client_fd = accept4(fd, &sa, &sa_len, SOCK_NONBLOCK);
		if (client_fd == -1) {
			if (errno == EAGAIN || errno == EWOULDBLOCK)
				return;
			fail(NULL);
		}

		node = sockaddr_to_node(&sa, sa_len);
		if (!node) {
			char hbuf[NI_MAXHOST];
			int g;

			g = getnameinfo(&sa, sa_len, hbuf, sizeof(hbuf), NULL, 0,
					NI_NUMERICHOST);
			if (g) {
				fprintf(stderr, "%s\n", gai_strerror(g));
				exit(1);
			}
			fprintf(stderr, "Could not determine node-id for node at %s\n",
				hbuf);
			close(client_fd);
		} else {
			if (node->connecting_fd != -1) {
				close(node->connecting_fd);
				remove_poll_callback(&cbs, node->connecting_fd);
				node->connecting_fd = -1;
			}
			add_poll_callback(&cbs, client_fd, POLLIN, proto_read, node);
			add_connection(client_fd, node);
		}
	}
}
Exemple #21
0
static void
accept_callback(picoev_loop* loop, int fd, int events, void* cb_arg)
{
  int client_fd;
  client_t *client;
  struct sockaddr_in client_addr;
  if ((events & PICOEV_TIMEOUT) != 0) {
    // time out
    // next turn or other process
    return;
  }else if ((events & PICOEV_READ) != 0) {
    socklen_t client_len = sizeof(client_addr);
#ifdef linux
    client_fd = accept4(fd, (struct sockaddr *)&client_addr, &client_len, SOCK_NONBLOCK | SOCK_CLOEXEC);
#else
    client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
#endif
    if (client_fd != -1) {
#ifdef DEBUG
      printf("accept fd %d \n", client_fd);
#endif
      setup_sock(client_fd);
      client = new_client_t(client_fd, client_addr);

      client->environ = Qnil;
      rb_gc_register_address(&client->environ);

      init_parser(client, server_name, server_port);

      picoev_add(loop, client_fd, PICOEV_READ, READ_LONG_TIMEOUT_SECS, r_callback, (void *)client);
    }else{
      if (errno != EAGAIN && errno != EWOULDBLOCK) {
	// TODO:
	// raise exception from errno
	/* rb_raise(); */
	/* write_error_log(__FILE__, __LINE__); */
	// die
	loop_done = 0;
      }
    }
  }
}
Exemple #22
0
static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) {
        _cleanup_close_ int fd = -1;
        Server *s = userdata;
        int r;

        assert(s);

        if (revents != EPOLLIN) {
                log_error("Got invalid event from epoll for stdout server fd: %"PRIx32, revents);
                return -EIO;
        }

        fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
        if (fd < 0) {
                if (errno == EAGAIN)
                        return 0;

                return log_error_errno(errno, "Failed to accept stdout connection: %m");
        }

        if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
                struct ucred u;

                r = getpeercred(fd, &u);

                /* By closing fd here we make sure that the client won't wait too long for journald to
                 * gather all the data it adds to the error message to find out that the connection has
                 * just been refused.
                 */
                fd = safe_close(fd);

                server_driver_message(s, r < 0 ? 0 : u.pid, NULL, LOG_MESSAGE("Too many stdout streams, refusing connection."), NULL);
                return 0;
        }

        r = stdout_stream_install(s, fd, NULL);
        if (r < 0)
                return r;

        fd = -1;
        return 0;
}
Exemple #23
0
int flb_net_accept(int server_fd)
{
    int remote_fd;
    struct sockaddr sock_addr;
    socklen_t socket_size = sizeof(struct sockaddr);

#ifdef HAVE_ACCEPT4
    remote_fd = accept4(server_fd, &sock_addr, &socket_size,
                        SOCK_NONBLOCK | SOCK_CLOEXEC);
#else
    remote_fd = accept(server_fd, &sock_addr, &socket_size);
    flb_net_socket_nonblocking(remote_fd);
#endif

    if (remote_fd == -1) {
        perror("accept4");
    }

    return remote_fd;
}
Exemple #24
0
static
int _accept(int fileno, struct sockaddr *addr, unsigned int addrlen) {
  int fd;
#ifdef SOCK_NONBLOCK
  fd = accept4(fileno, addr, &addrlen, SOCK_CLOEXEC|SOCK_NONBLOCK);
#else
  fd = accept(fileno, addr, &addrlen);
#endif
  if (fd < 0) {
    if ( errno == EINTR ) {
      rb_thread_sleep(1);
    }
    return fd;
  }
#ifndef SOCK_NONBLOCK
  fcntl(fd, F_SETFD, FD_CLOEXEC);
  fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
#endif
  return fd;
}
Exemple #25
0
int
ud_accept(int listenfd, struct ucred *cred)
{
	while (1) {
		int newsock = 0;
		struct sockaddr_un cliaddr;
		socklen_t len = sizeof(struct sockaddr_un);

		newsock = TEMP_FAILURE_RETRY (accept4(listenfd, (struct sockaddr *)&cliaddr, &len, SOCK_CLOEXEC|SOCK_NONBLOCK));
		if (newsock < 0) {
			return newsock;
		}
		if (cred) {
			len = sizeof(struct ucred);
			getsockopt(newsock,SOL_SOCKET,SO_PEERCRED,cred,&len);
		}

		return newsock;
	}
}
Exemple #26
0
struct basic_epoll_event *acceptor::on_accept()
{
    struct sockaddr_in new_addr;
    socklen_t new_addr_size = sizeof(struct sockaddr_in);
    int acceptfd = accept4(this->fd, (sockaddr *)&new_addr, &new_addr_size, SOCK_CLOEXEC | SOCK_NONBLOCK);
    if (0 > acceptfd)
        return NULL;
    
    /*
    struct server_epoll_event *event = pool.get();
    event->fd = acceptfd;
    */
    struct server_epoll_event *event = events->get(acceptfd);
    
    epoll::add(event, EPOLLET | EPOLLIN | EPOLLOUT);
    event->callback = callback;
    //event->pool = &pool;
    this->accept_callback.invoke(event);
    return event; 
}
Exemple #27
0
void
lwan_main_loop(lwan_t *l)
{
    if (setjmp(cleanup_jmp_buf))
        return;

    signal(SIGINT, _signal_handler);

    lwan_status_info("Ready to serve");

    for (;;) {
        int child_fd = accept4(l->main_socket, NULL, NULL, SOCK_NONBLOCK);
        if (UNLIKELY(child_fd < 0)) {
            lwan_status_perror("accept");
            continue;
        }

        _push_request_fd(l, child_fd);
    }
}
Exemple #28
0
int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) {
  int peerfd;

  assert(sockfd >= 0);

  do {
#if defined(HAVE_ACCEPT4)
    peerfd = accept4(sockfd, saddr, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC);
#else
    if ((peerfd = accept(sockfd, saddr, &slen)) != -1) {
      if (uv__cloexec(peerfd, 1) == -1 || uv__nonblock(peerfd, 1) == -1) {
        uv__close(peerfd);
        return -1;
      }
    }
#endif
  }
  while (peerfd == -1 && errno == EINTR);

  return peerfd;
}
Exemple #29
0
static void
listen_cb (EV_P_ ev_io *w, int revents)
{
    int cfd;
    int r;
    struct pollfd pfd;
    struct sockaddr_storage peer_addr;
    socklen_t peer_addr_size;
    ev_io* client_watcher;

    //printf("listen_cb: w->fd = %d\n", w->fd);

    /* Without this block the accept call often fails with EINVAL */
    bzero(&pfd, sizeof pfd);
    pfd.fd = w->fd;
    pfd.events |= POLLIN;
    r = poll(&pfd, 1, /* timeout in ms */ 10);
    if (r == -1)
        handle_error("poll");
    else if (r == 0) {
        /* no events to read */
        fprintf(stderr, "poll: no events to read\n");
        return;
    }

    cfd = accept4(w->fd, (struct sockaddr*) &peer_addr,
                  &peer_addr_size, SOCK_NONBLOCK);
    //printf("cfd=%d\terrno=%d\n", cfd, errno);
    if (cfd == -1)
        handle_error("accept");

    client_watcher = (ev_io*) malloc (sizeof(ev_io));
    if (! client_watcher)
        handle_error("malloc");

    ev_io_init (client_watcher, client_cb, cfd, EV_READ);
    ev_io_start (EV_A_ client_watcher);

    fprintf(stderr, "accepted connection\n");
}
Exemple #30
0
int srv_tcp_accept(int fd, char *ip, int *port, int flags) {
    int fd_new;
    struct sockaddr_storage addr;
    socklen_t addrlen = sizeof(addr);

#ifdef linux
    fd_new = accept4(fd, (struct sockaddr *) &addr, &addrlen, flags);
#else
    /* Fallback to accept if accept4 is not implemented */
    fd_new = accept(fd, (struct sockaddr *) &addr, &addrlen);
#endif

    if(fd_new == -1) {
        return -1;
    }
    else {
#ifndef linux
        /* accept() doesn't accept flags so we need to set non-blocking mode manually */
        if(flags == SOCK_NONBLOCK)
            srv_setnoblock(fd_new);
#endif
        /* Fill in address info buffers */
        if(addr.ss_family == AF_INET) {
            /* IPv4 */
            struct sockaddr_in *s = (struct sockaddr_in *) &addr;

            if(ip) inet_ntop(AF_INET, &s->sin_addr, ip, INET6_ADDRSTRLEN);
            if(port) *port = ntohs(s->sin_port);
        }
        else {
            /* IPv6 */
            struct sockaddr_in6 *s = (struct sockaddr_in6 *) &addr;

            if(ip) inet_ntop(AF_INET6, &s->sin6_addr, ip, INET6_ADDRSTRLEN);
            if(port) *port = ntohs(s->sin6_port);
        }

        return fd_new;
    }
}