]> git.sesse.net Git - cubemap/blob - acceptor.cpp
Use in-class initialization for making it harder to forget to set a default.
[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         int server_sock;
25         if (socket_type == TCP_SOCKET) {
26                 server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
27         } else {
28                 assert(socket_type == UDP_SOCKET);
29                 server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
30         }
31         if (server_sock == -1) {
32                 log_perror("socket");
33                 exit(1);
34         }
35
36         int one = 1;
37         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
38                 log_perror("setsockopt(SO_REUSEADDR)");
39                 exit(1);
40         }
41
42         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
43         int zero = 0;
44         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
45                 log_perror("setsockopt(IPV6_V6ONLY)");
46                 exit(1);
47         }
48
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)");
52                 exit(1);
53         }
54
55         if (bind(server_sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) == -1) {
56                 log_perror("bind");
57                 exit(1);
58         }
59
60         if (socket_type == TCP_SOCKET) {
61                 if (listen(server_sock, 128) == -1) {
62                         log_perror("listen");
63                         exit(1);
64                 }
65         }
66
67         return server_sock;
68 }
69
70 sockaddr_in6 create_any_address(int port)
71 {
72         sockaddr_in6 sin6;
73         memset(&sin6, 0, sizeof(sin6));
74         sin6.sin6_family = AF_INET6;
75         sin6.sin6_port = htons(port);
76         return sin6;
77 }
78
79 sockaddr_in6 extract_address_from_acceptor_proto(const AcceptorProto &proto)
80 {
81         sockaddr_in6 sin6;
82         memset(&sin6, 0, sizeof(sin6));
83         sin6.sin6_family = AF_INET6;
84
85         if (!proto.addr().empty()) {
86                 int ret = inet_pton(AF_INET6, proto.addr().c_str(), &sin6.sin6_addr);
87                 assert(ret == 1);
88         }
89
90         sin6.sin6_port = htons(proto.port());
91         return sin6;
92 }
93         
94 Acceptor::Acceptor(int server_sock, const sockaddr_in6 &addr,
95                    const string &certificate_chain, const string &private_key)
96         : server_sock(server_sock),
97           addr(addr),
98           certificate_chain(certificate_chain),
99           private_key(private_key)
100 {
101 }
102
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())
108 {
109 }
110
111 AcceptorProto Acceptor::serialize() const
112 {
113         char buf[INET6_ADDRSTRLEN];
114         inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
115
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);
122         return serialized;
123 }
124
125 void Acceptor::close_socket()
126 {
127         safe_close(server_sock);
128 }
129
130 void Acceptor::do_work()
131 {
132         while (!should_stop()) {
133                 if (!wait_for_activity(server_sock, POLLIN, nullptr)) {
134                         continue;
135                 }
136
137                 sockaddr_in6 addr;
138                 socklen_t addrlen = sizeof(addr);
139
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) {
143                         continue;
144                 }
145                 if (sock == -1) {
146                         log_perror("accept");
147                         usleep(100000);
148                         continue;
149                 }
150
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.
154                 int one = 1;
155                 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
156                         log_perror("setsockopt(TCP_CORK)");
157                         // Can still continue.
158                 }
159
160                 // Pick a server, round-robin, and hand over the socket to it.
161                 servers->add_client(sock, this);
162         }
163 }