4 #include <netinet/in.h>
5 #include <netinet/tcp.h>
10 #include <sys/socket.h>
15 #include "serverpool.h"
21 extern ServerPool *servers;
23 int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type)
25 // NOTE: We set as non-blocking, so the acceptor thread can notice that we want to shut it down.
27 if (socket_type == TCP_SOCKET) {
28 server_sock = socket(PF_INET6, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
30 assert(socket_type == UDP_SOCKET);
31 server_sock = socket(PF_INET6, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_UDP);
33 if (server_sock == -1) {
39 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
40 log_perror("setsockopt(SO_REUSEADDR)");
44 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
46 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
47 log_perror("setsockopt(IPV6_V6ONLY)");
51 if (::bind(server_sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) == -1) {
56 if (socket_type == TCP_SOCKET) {
57 if (listen(server_sock, 128) == -1) {
66 sockaddr_in6 create_any_address(int port)
69 memset(&sin6, 0, sizeof(sin6));
70 sin6.sin6_family = AF_INET6;
71 sin6.sin6_port = htons(port);
75 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto)
78 memset(&sin6, 0, sizeof(sin6));
79 sin6.sin6_family = AF_INET6;
81 if (!proto.addr().empty()) {
82 int ret = inet_pton(AF_INET6, proto.addr().c_str(), &sin6.sin6_addr);
86 sin6.sin6_port = htons(proto.port());
90 Acceptor::Acceptor(int server_sock, const sockaddr_in6 &addr,
91 const string &certificate_chain, const string &private_key)
92 : server_sock(server_sock),
94 certificate_chain(certificate_chain),
95 private_key(private_key)
99 Acceptor::Acceptor(const AcceptorProto &serialized)
100 : server_sock(serialized.server_sock()),
101 addr(extract_address_from_acceptor_proto(serialized)),
102 certificate_chain(serialized.certificate_chain()),
103 private_key(serialized.private_key())
105 // Set back the close-on-exec flag for the socket.
106 // (This can't leak into a child, since we haven't been started yet.)
107 fcntl(server_sock, F_SETFD, FD_CLOEXEC);
110 AcceptorProto Acceptor::serialize() const
112 // Unset the close-on-exec flag for the socket.
113 // (This can't leak into a child, since there's only one thread left.)
114 fcntl(server_sock, F_SETFD, 0);
116 char buf[INET6_ADDRSTRLEN];
117 inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
119 AcceptorProto serialized;
120 serialized.set_server_sock(server_sock);
121 serialized.set_addr(buf);
122 serialized.set_port(ntohs(addr.sin6_port));
123 serialized.set_certificate_chain(certificate_chain);
124 serialized.set_private_key(private_key);
128 void Acceptor::close_socket()
130 safe_close(server_sock);
133 void Acceptor::do_work()
135 while (!should_stop()) {
136 if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
141 socklen_t addrlen = sizeof(addr);
143 // Get a new socket, and set it as nonblocking.
144 int sock = accept4(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
145 if (sock == -1 && errno == EINTR) {
149 log_perror("accept");
154 // Enable TCP_CORK for maximum throughput. In the rare case that the
155 // stream stops entirely, this will cause a small delay (~200 ms)
156 // before the last part is sent out, but that should be fine.
158 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
159 log_perror("setsockopt(TCP_CORK)");
160 // Can still continue.
163 // Pick a server, round-robin, and hand over the socket to it.
164 servers->add_client(sock, this);