UCT max_maintime_ratio: Raise back to 2.0
[pachi.git] / network.c
blob02c986e0aa9e1b0a91eed2256c6f1b50cca1064a
1 /* Utility functions to redirect stdin, stdout, stderr to sockets. */
3 #define DEBUG
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <assert.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <pthread.h>
12 #include <sys/types.h>
14 #ifdef _WIN32
15 #include <winsock2.h>
16 #include <ws2tcpip.h>
17 #else
18 #include <sys/socket.h>
19 #include <netdb.h>
20 #endif
22 #include "debug.h"
23 #include "util.h"
25 #define STDIN 0
26 #define STDOUT 1
27 #define STDERR 2
29 #define BSIZE 4096
31 static inline void
32 die(char *msg)
34 perror(msg);
35 exit(42);
38 /* Create a socket, bind to it on the given port and listen.
39 * This function is restricted to server mode (port has
40 * no hostname). Returns the socket. */
41 int
42 port_listen(char *port, int max_connections)
44 int sock = socket(AF_INET, SOCK_STREAM, 0);
45 if (sock == -1)
46 die("socket");
48 struct sockaddr_in server_addr;
49 memset(&server_addr, 0, sizeof(server_addr));
50 server_addr.sin_family = AF_INET;
51 server_addr.sin_port = htons(atoi(port));
52 server_addr.sin_addr.s_addr = INADDR_ANY;
54 const char val = 1;
55 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)))
56 die("setsockopt");
57 if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
58 die("bind");
59 if (listen(sock, max_connections) == -1)
60 die("listen");
61 return sock;
64 /* Returns true if in private address range: 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 */
65 static bool
66 is_private(struct in_addr *in)
68 return (ntohl(in->s_addr) & 0xff000000) >> 24 == 10
69 || (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16
70 || (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168;
73 /* Waits for a connection on the given socket, and returns the file descriptor.
74 * Updates the client address if it is not null.
75 * WARNING: the connection is not authenticated. As a weak security measure,
76 * the connections are limited to a private network. */
77 int
78 open_server_connection(int socket, struct in_addr *client)
80 assert(socket >= 0);
81 for (;;) {
82 struct sockaddr_in client_addr;
83 int sin_size = sizeof(struct sockaddr_in);
84 int fd = accept(socket, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size);
85 if (fd == -1) {
86 die("accept");
88 if (is_private(&client_addr.sin_addr)) {
89 if (client)
90 *client = client_addr.sin_addr;
91 return fd;
93 close(fd);
97 /* Opens a new connection to the given port name, which must
98 * contain a host name. Returns the open file descriptor,
99 * or -1 if the open fails. */
100 static int
101 open_client_connection(char *port_name)
103 char hostname[BSIZE];
104 strncpy(hostname, port_name, sizeof(hostname));
105 char *port = strchr(hostname, ':');
106 assert(port);
107 *port++ = '\0';
109 struct hostent *host = gethostbyname(hostname);
110 if (!host)
111 return -1;
112 int sock = socket(AF_INET, SOCK_STREAM, 0);
113 if (sock == -1)
114 die("socket");
115 struct sockaddr_in sin;
116 memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
117 sin.sin_family = AF_INET;
118 sin.sin_port = htons(atoi(port));
120 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
121 close(sock);
122 return -1;
124 return sock;
127 /* Allow connexion queue > 1 to avoid race conditions. */
128 #define MAX_CONNEXIONS 5
130 struct port_info {
131 int socket;
132 char *port;
135 /* Wait at most 30s between connection attempts. */
136 #define MAX_WAIT 30
138 /* Open a connection on the given socket/port.
139 * Act as server if the port doesn't contain a hostname,
140 * as a client otherwise. If socket < 0 or in client mode,
141 * create the socket from the given port and update socket.
142 * Block until the connection succeeds.
143 * Return a file descriptor for the new connection. */
144 static int
145 open_connection(struct port_info *info)
147 int conn;
148 char *p = strchr(info->port, ':');
149 if (p) {
150 for (int try = 1;; ) {
151 conn = open_client_connection(info->port);
152 if (conn >= 0) break;
153 sleep(try);
154 if (try < MAX_WAIT) try++;
156 info->socket = conn;
157 } else {
158 if (info->socket < 0)
159 info->socket = port_listen(info->port, MAX_CONNEXIONS);
160 conn = open_server_connection(info->socket, NULL);
162 return conn;
165 /* Open the log connection on the given port, redirect stderr to it. */
166 static void
167 open_log_connection(struct port_info *info)
169 int log_conn = open_connection(info);
170 if (dup2(log_conn, STDERR) < 0)
171 die("dup2");
172 if (DEBUGL(0))
173 fprintf(stderr, "log connection opened\n");
176 /* Thread keeping the log connection open and redirecting stderr to it.
177 * It also echoes its input, which can be used to check if the
178 * program is alive. As a weak identity check, in server mode the input
179 * must start with "Pachi" (without the quotes). */
180 static void *
181 log_thread(void *arg)
183 struct port_info *info = arg;
184 assert(info && info->port);
185 for (;;) {
186 char buf[BSIZE];
187 int size;
188 bool check = !strchr(info->port, ':');
189 if (!check)
190 write(STDERR, "Pachi\n", 6);
191 while ((size = read(STDERR, buf, BSIZE)) > 0) {
192 if (check && strncasecmp(buf, "Pachi", 5)) break;
193 check = false;
194 write(STDERR, buf, size);
196 fflush(stderr);
197 open_log_connection(info);
201 /* Open the log connection on the given port, redirect stderr to it,
202 * and keep reopening it if the connection is closed. */
203 void
204 open_log_port(char *port)
206 pthread_t thread;
207 static struct port_info log_info = { .socket = -1 };
208 log_info.port = port;
209 open_log_connection(&log_info);
211 /* From now on, log_info may only be modified by the single
212 * log_thread so static allocation is ok and there is no race. */
213 pthread_create(&thread, NULL, log_thread, (void *)&log_info);
216 /* Open the gtp connection on the given port, redirect stdin & stdout to it. */
217 void
218 open_gtp_connection(int *socket, char *port)
220 static struct port_info gtp_info = { .socket = -1 };
221 gtp_info.port = port;
222 int gtp_conn = open_connection(&gtp_info);
223 for (int d = STDIN; d <= STDOUT; d++) {
224 if (dup2(gtp_conn, d) < 0)
225 die("dup2");
227 if (DEBUGL(0))
228 fprintf(stderr, "gtp connection opened\n");