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 CreateAnyAddress(int port)
73 memset(&sin6, 0, sizeof(sin6));
74 sin6.sin6_family = AF_INET6;
75 sin6.sin6_port = htons(port);
79 sockaddr_in6 ExtractAddressFromAcceptorProto(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 : server_sock(server_sock),
100 Acceptor::Acceptor(const AcceptorProto &serialized)
101 : server_sock(serialized.server_sock()),
102 addr(ExtractAddressFromAcceptorProto(serialized))
106 AcceptorProto Acceptor::serialize() const
108 char buf[INET6_ADDRSTRLEN];
109 inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf));
111 AcceptorProto serialized;
112 serialized.set_server_sock(server_sock);
113 serialized.set_addr(buf);
114 serialized.set_port(ntohs(addr.sin6_port));
118 void Acceptor::close_socket()
120 safe_close(server_sock);
123 void Acceptor::do_work()
125 while (!should_stop()) {
126 if (!wait_for_activity(server_sock, POLLIN, NULL)) {
131 socklen_t addrlen = sizeof(addr);
134 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
135 if (sock == -1 && errno == EINTR) {
139 log_perror("accept");
144 // Set the socket as nonblocking.
146 if (ioctl(sock, FIONBIO, &one) == -1) {
147 log_perror("ioctl(FIONBIO)");
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 if (setsockopt(sock, SOL_TCP, TCP_CORK, &one, sizeof(one)) == -1) {
155 log_perror("setsockopt(TCP_CORK)");
156 // Can still continue.
159 // Pick a server, round-robin, and hand over the socket to it.
160 servers->add_client(sock);