]> git.sesse.net Git - cubemap/blob - acceptor.cpp
Simplify setting the non-blocking flag when creating HTTP sockets.
[cubemap] / acceptor.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
5 #include <poll.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/ioctl.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "acceptor.h"
13 #include "log.h"
14 #include "serverpool.h"
15 #include "state.pb.h"
16 #include "util.h"
17
18 using namespace std;
19
20 extern ServerPool *servers;
21
22 int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type)
23 {
24         // NOTE: We set as non-blocking, so the acceptor thread can notice that we want to shut it down.
25         int server_sock;
26         if (socket_type == TCP_SOCKET) {
27                 server_sock = socket(PF_INET6, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
28         } else {
29                 assert(socket_type == UDP_SOCKET);
30                 server_sock = socket(PF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
31         }
32         if (server_sock == -1) {
33                 log_perror("socket");
34                 exit(1);
35         }
36
37         int one = 1;
38         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
39                 log_perror("setsockopt(SO_REUSEADDR)");
40                 exit(1);
41         }
42
43         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
44         int zero = 0;
45         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
46                 log_perror("setsockopt(IPV6_V6ONLY)");
47                 exit(1);
48         }
49
50         if (bind(server_sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) == -1) {
51                 log_perror("bind");
52                 exit(1);
53         }
54
55         if (socket_type == TCP_SOCKET) {
56                 if (listen(server_sock, 128) == -1) {
57                         log_perror("listen");
58                         exit(1);
59                 }
60         }
61
62         return server_sock;
63 }
64
65 sockaddr_in6 create_any_address(int port)
66 {
67         sockaddr_in6 sin6;
68         memset(&sin6, 0, sizeof(sin6));
69         sin6.sin6_family = AF_INET6;
70         sin6.sin6_port = htons(port);
71         return sin6;
72 }
73
74 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto)
75 {
76         sockaddr_in6 sin6;
77         memset(&sin6, 0, sizeof(sin6));
78         sin6.sin6_family = AF_INET6;
79
80         if (!proto.addr().empty()) {
81                 int ret = inet_pton(AF_INET6, proto.addr().c_str(), &sin6.sin6_addr);
82                 assert(ret == 1);
83         }
84
85         sin6.sin6_port = htons(proto.port());
86         return sin6;
87 }
88         
89 Acceptor::Acceptor(int server_sock, const sockaddr_in6 &addr,
90                    const string &certificate_chain, const string &private_key)
91         : server_sock(server_sock),
92           addr(addr),
93           certificate_chain(certificate_chain),
94           private_key(private_key)
95 {
96 }
97
98 Acceptor::Acceptor(const AcceptorProto &serialized)
99         : server_sock(serialized.server_sock()),
100           addr(extract_address_from_acceptor_proto(serialized)),
101           certificate_chain(serialized.certificate_chain()),
102           private_key(serialized.private_key())
103 {
104 }
105
106 AcceptorProto Acceptor::serialize() const
107 {
108         char buf[INET6_ADDRSTRLEN];
109         inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
110
111         AcceptorProto serialized;
112         serialized.set_server_sock(server_sock);
113         serialized.set_addr(buf);
114         serialized.set_port(ntohs(addr.sin6_port));
115         serialized.set_certificate_chain(certificate_chain);
116         serialized.set_private_key(private_key);
117         return serialized;
118 }
119
120 void Acceptor::close_socket()
121 {
122         safe_close(server_sock);
123 }
124
125 void Acceptor::do_work()
126 {
127         while (!should_stop()) {
128                 if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
129                         continue;
130                 }
131
132                 sockaddr_in6 addr;
133                 socklen_t addrlen = sizeof(addr);
134
135                 // Get a new socket, and set it as nonblocking.
136                 int sock = accept4(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen, SOCK_NONBLOCK);
137                 if (sock == -1 && errno == EINTR) {
138                         continue;
139                 }
140                 if (sock == -1) {
141                         log_perror("accept");
142                         usleep(100000);
143                         continue;
144                 }
145
146                 // Enable TCP_CORK for maximum throughput. In the rare case that the
147                 // stream stops entirely, this will cause a small delay (~200 ms)
148                 // before the last part is sent out, but that should be fine.
149                 int one = 1;
150                 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
151                         log_perror("setsockopt(TCP_CORK)");
152                         // Can still continue.
153                 }
154
155                 // Pick a server, round-robin, and hand over the socket to it.
156                 servers->add_client(sock, this);
157         }
158 }