]> git.sesse.net Git - cubemap/blob - acceptor.cpp
Fix a crash when a HTTP input connected to an UDP output goes unavailable.
[cubemap] / acceptor.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <netinet/in.h>
5 #include <netinet/tcp.h>
6 #include <poll.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 #include "acceptor.h"
14 #include "log.h"
15 #include "serverpool.h"
16 #include "state.pb.h"
17 #include "util.h"
18
19 using namespace std;
20
21 extern ServerPool *servers;
22
23 int create_server_socket(const sockaddr_in6 &addr, SocketType socket_type)
24 {
25         // NOTE: We set as non-blocking, so the acceptor thread can notice that we want to shut it down.
26         int server_sock;
27         if (socket_type == TCP_SOCKET) {
28                 server_sock = socket(PF_INET6, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
29         } else {
30                 assert(socket_type == UDP_SOCKET);
31                 server_sock = socket(PF_INET6, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_UDP);
32         }
33         if (server_sock == -1) {
34                 log_perror("socket");
35                 exit(1);
36         }
37
38         int one = 1;
39         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
40                 log_perror("setsockopt(SO_REUSEADDR)");
41                 exit(1);
42         }
43
44         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
45         int zero = 0;
46         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
47                 log_perror("setsockopt(IPV6_V6ONLY)");
48                 exit(1);
49         }
50
51         if (::bind(server_sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) == -1) {
52                 log_perror("bind");
53                 exit(1);
54         }
55
56         if (socket_type == TCP_SOCKET) {
57                 if (listen(server_sock, 128) == -1) {
58                         log_perror("listen");
59                         exit(1);
60                 }
61         }
62
63         return server_sock;
64 }
65
66 sockaddr_in6 create_any_address(int port)
67 {
68         sockaddr_in6 sin6;
69         memset(&sin6, 0, sizeof(sin6));
70         sin6.sin6_family = AF_INET6;
71         sin6.sin6_port = htons(port);
72         return sin6;
73 }
74
75 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto)
76 {
77         sockaddr_in6 sin6;
78         memset(&sin6, 0, sizeof(sin6));
79         sin6.sin6_family = AF_INET6;
80
81         if (!proto.addr().empty()) {
82                 int ret = inet_pton(AF_INET6, proto.addr().c_str(), &sin6.sin6_addr);
83                 assert(ret == 1);
84         }
85
86         sin6.sin6_port = htons(proto.port());
87         return sin6;
88 }
89         
90 Acceptor::Acceptor(int server_sock, const sockaddr_in6 &addr,
91                    const string &certificate_chain, const string &private_key)
92         : server_sock(server_sock),
93           addr(addr),
94           certificate_chain(certificate_chain),
95           private_key(private_key)
96 {
97 }
98
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())
104 {
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, O_CLOEXEC);
108 }
109
110 AcceptorProto Acceptor::serialize() const
111 {
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);
115
116         char buf[INET6_ADDRSTRLEN];
117         inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
118
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);
125         return serialized;
126 }
127
128 void Acceptor::close_socket()
129 {
130         safe_close(server_sock);
131 }
132
133 void Acceptor::do_work()
134 {
135         while (!should_stop()) {
136                 if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
137                         continue;
138                 }
139
140                 sockaddr_in6 addr;
141                 socklen_t addrlen = sizeof(addr);
142
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) {
146                         continue;
147                 }
148                 if (sock == -1) {
149                         log_perror("accept");
150                         usleep(100000);
151                         continue;
152                 }
153
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.
157                 int one = 1;
158                 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
159                         log_perror("setsockopt(TCP_CORK)");
160                         // Can still continue.
161                 }
162
163                 // Pick a server, round-robin, and hand over the socket to it.
164                 servers->add_client(sock, this);
165         }
166 }