9 #include <sys/socket.h>
12 #include "serverpool.h"
17 extern ServerPool *servers;
18 extern volatile bool hupped;
20 int create_server_socket(int port)
22 int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
23 if (server_sock == -1) {
29 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
30 perror("setsockopt(SO_REUSEADDR)");
34 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
36 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
37 perror("setsockopt(IPV6_V6ONLY)");
41 // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
42 if (ioctl(server_sock, FIONBIO, &one) == -1) {
43 perror("ioctl(FIONBIO)");
48 memset(&addr, 0, sizeof(addr));
49 addr.sin6_family = AF_INET6;
50 addr.sin6_port = htons(port);
52 if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
57 if (listen(server_sock, 128) == -1) {
65 Acceptor::Acceptor(int server_sock, int port)
66 : server_sock(server_sock),
71 Acceptor::Acceptor(const AcceptorProto &serialized)
72 : server_sock(serialized.server_sock()),
73 port(serialized.port())
77 AcceptorProto Acceptor::serialize() const
79 AcceptorProto serialized;
80 serialized.set_server_sock(server_sock);
81 serialized.set_port(port);
85 void Acceptor::close_socket()
89 ret = close(server_sock);
90 } while (ret == -1 && errno == EINTR);
97 void Acceptor::do_work()
100 // Since we are non-blocking, we need to wait for the right state first.
101 // Wait up to 50 ms, then check hupped.
103 pfd.fd = server_sock;
106 int nfds = poll(&pfd, 1, 50);
107 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
117 socklen_t addrlen = sizeof(addr);
120 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
121 if (sock == -1 && errno == EINTR) {
130 // Set the socket as nonblocking.
132 if (ioctl(sock, FIONBIO, &one) == -1) {
137 // Pick a server, round-robin, and hand over the socket to it.
138 servers->add_client(sock);