Distributed engine: Wait for only 3/4 slaves and at most 0.5s for fast commands
[pachi/json.git] / network.c
blob8f4c3b847254ce7e761a7945f88d642ba375196a
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>
13 #include <sys/socket.h>
14 #include <netdb.h>
16 #include "debug.h"
17 #include "util.h"
19 #define STDIN 0
20 #define STDOUT 1
21 #define STDERR 2
23 #define BSIZE 4096
25 static inline void
26 die(char *msg)
28 perror(msg);
29 exit(42);
32 /* Create a socket, bind to it on the given port and listen.
33 * This function is restricted to server mode (port has
34 * no hostname). Returns the socket. */
35 int
36 port_listen(char *port, int max_connections)
38 int sock = socket(AF_INET, SOCK_STREAM, 0);
39 if (sock == -1)
40 die("socket");
42 struct sockaddr_in server_addr;
43 memset(&server_addr, 0, sizeof(server_addr));
44 server_addr.sin_family = AF_INET;
45 server_addr.sin_port = htons(atoi(port));
46 server_addr.sin_addr.s_addr = INADDR_ANY;
48 int val = 1;
49 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)))
50 die("setsockopt");
51 if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
52 die("bind");
53 if (listen(sock, max_connections) == -1)
54 die("listen");
55 return sock;
58 /* Returns true if in private address range: 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 */
59 static bool
60 is_private(struct in_addr *in)
62 return (ntohl(in->s_addr) & 0xff000000) >> 24 == 10
63 || (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16
64 || (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168;
67 /* Waits for a connection on the given socket, and returns the file descriptor.
68 * Updates the client address if it is not null.
69 * WARNING: the connection is not authenticated. As a weak security measure,
70 * the connections are limited to a private network. */
71 int
72 open_server_connection(int socket, struct in_addr *client)
74 assert(socket >= 0);
75 for (;;) {
76 struct sockaddr_in client_addr;
77 int sin_size = sizeof(struct sockaddr_in);
78 int fd = accept(socket, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size);
79 if (fd == -1) {
80 die("accept");
82 if (is_private(&client_addr.sin_addr)) {
83 if (client)
84 *client = client_addr.sin_addr;
85 return fd;
87 close(fd);
91 /* Opens a new connection to the given port name, which must
92 * contain a host name. Returns the open file descriptor,
93 * or -1 if the open fails. */
94 static int
95 open_client_connection(char *port_name)
97 char hostname[BSIZE];
98 strncpy(hostname, port_name, sizeof(hostname));
99 char *port = strchr(hostname, ':');
100 assert(port);
101 *port++ = '\0';
103 struct hostent *host = gethostbyname(hostname);
104 if (!host)
105 return -1;
106 int sock = socket(AF_INET, SOCK_STREAM, 0);
107 if (sock == -1)
108 die("socket");
109 struct sockaddr_in sin;
110 memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
111 sin.sin_family = AF_INET;
112 sin.sin_port = htons(atoi(port));
114 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
115 close(sock);
116 return -1;
118 return sock;
121 /* Allow connexion queue > 1 to avoid race conditions. */
122 #define MAX_CONNEXIONS 5
124 struct port_info {
125 int socket;
126 char *port;
129 /* Wait at most 30s between connection attempts. */
130 #define MAX_WAIT 30
132 /* Open a connection on the given socket/port.
133 * Act as server if the port doesn't contain a hostname,
134 * as a client otherwise. If socket < 0 or in client mode,
135 * create the socket from the given port and update socket.
136 * Block until the connection succeeds.
137 * Return a file descriptor for the new connection. */
138 static int
139 open_connection(struct port_info *info)
141 int conn;
142 char *p = strchr(info->port, ':');
143 if (p) {
144 for (int try = 1;; ) {
145 conn = open_client_connection(info->port);
146 if (conn >= 0) break;
147 sleep(try);
148 if (try < MAX_WAIT) try++;
150 info->socket = conn;
151 } else {
152 if (info->socket < 0)
153 info->socket = port_listen(info->port, MAX_CONNEXIONS);
154 conn = open_server_connection(info->socket, NULL);
156 return conn;
159 /* Open the log connection on the given port, redirect stderr to it. */
160 static void
161 open_log_connection(struct port_info *info)
163 int log_conn = open_connection(info);
164 if (dup2(log_conn, STDERR) < 0)
165 die("dup2");
166 if (DEBUGL(0))
167 fprintf(stderr, "log connection opened\n");
170 /* Thread keeping the log connection open and redirecting stderr to it.
171 * It also echoes its input, which can be used to check if the
172 * program is alive. As a weak identity check, in server mode the input
173 * must start with "Pachi" (without the quotes). */
174 static void *
175 log_thread(void *arg)
177 struct port_info *info = arg;
178 assert(info && info->port);
179 for (;;) {
180 char buf[BSIZE];
181 int size;
182 bool check = !strchr(info->port, ':');
183 if (!check)
184 write(STDERR, "Pachi\n", 6);
185 while ((size = read(STDERR, buf, BSIZE)) > 0) {
186 if (check && strncasecmp(buf, "Pachi", 5)) break;
187 check = false;
188 write(STDERR, buf, size);
190 fflush(stderr);
191 open_log_connection(info);
195 /* Open the log connection on the given port, redirect stderr to it,
196 * and keep reopening it if the connection is closed. */
197 void
198 open_log_port(char *port)
200 pthread_t thread;
201 static struct port_info log_info = { .socket = -1 };
202 log_info.port = port;
203 open_log_connection(&log_info);
205 /* From now on, log_info may only be modified by the single
206 * log_thread so static allocation is ok and there is no race. */
207 pthread_create(&thread, NULL, log_thread, (void *)&log_info);
210 /* Open the gtp connection on the given port, redirect stdin & stdout to it. */
211 void
212 open_gtp_connection(int *socket, char *port)
214 static struct port_info gtp_info = { .socket = -1 };
215 gtp_info.port = port;
216 int gtp_conn = open_connection(&gtp_info);
217 for (int d = STDIN; d <= STDOUT; d++) {
218 if (dup2(gtp_conn, d) < 0)
219 die("dup2");
221 if (DEBUGL(0))
222 fprintf(stderr, "gtp connection opened\n");