3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
9 #include <sys/socket.h>
14 #include "serverpool.h"
20 extern ServerPool *servers;
22 int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type)
25 if (socket_type == TCP_SOCKET) {
26 server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
28 assert(socket_type == UDP_SOCKET);
29 server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
31 if (server_sock == -1) {
37 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
38 log_perror("setsockopt(SO_REUSEADDR)");
42 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
44 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
45 log_perror("setsockopt(IPV6_V6ONLY)");
49 // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
50 if (ioctl(server_sock, FIONBIO, &one) == -1) {
51 log_perror("ioctl(FIONBIO)");
55 if (bind(server_sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) == -1) {
60 if (socket_type == TCP_SOCKET) {
61 if (listen(server_sock, 128) == -1) {
70 sockaddr_in6 create_any_address(int port)
73 memset(&sin6, 0, sizeof(sin6));
74 sin6.sin6_family = AF_INET6;
75 sin6.sin6_port = htons(port);
79 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto)
82 memset(&sin6, 0, sizeof(sin6));
83 sin6.sin6_family = AF_INET6;
85 if (!proto.addr().empty()) {
86 int ret = inet_pton(AF_INET6, proto.addr().c_str(), &sin6.sin6_addr);
90 sin6.sin6_port = htons(proto.port());
94 Acceptor::Acceptor(int server_sock, const sockaddr_in6 &addr,
95 const string &certificate_chain, const string &private_key)
96 : server_sock(server_sock),
98 certificate_chain(certificate_chain),
99 private_key(private_key)
103 Acceptor::Acceptor(const AcceptorProto &serialized)
104 : server_sock(serialized.server_sock()),
105 addr(extract_address_from_acceptor_proto(serialized)),
106 certificate_chain(serialized.certificate_chain()),
107 private_key(serialized.private_key())
111 AcceptorProto Acceptor::serialize() const
113 char buf[INET6_ADDRSTRLEN];
114 inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
116 AcceptorProto serialized;
117 serialized.set_server_sock(server_sock);
118 serialized.set_addr(buf);
119 serialized.set_port(ntohs(addr.sin6_port));
120 serialized.set_certificate_chain(certificate_chain);
121 serialized.set_private_key(private_key);
125 void Acceptor::close_socket()
127 safe_close(server_sock);
130 void Acceptor::do_work()
132 while (!should_stop()) {
133 if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
138 socklen_t addrlen = sizeof(addr);
140 // Get a new socket, and set it as nonblocking.
141 int sock = accept4(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen, SOCK_NONBLOCK);
142 if (sock == -1 && errno == EINTR) {
146 log_perror("accept");
151 // Enable TCP_CORK for maximum throughput. In the rare case that the
152 // stream stops entirely, this will cause a small delay (~200 ms)
153 // before the last part is sent out, but that should be fine.
155 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
156 log_perror("setsockopt(TCP_CORK)");
157 // Can still continue.
160 // Pick a server, round-robin, and hand over the socket to it.
161 servers->add_client(sock, this);