Support log proxy for the distributed engine.
[pachi.git] / network.c
bloba2290996d4f2506f5272ef22c9f186ab8d2314b7
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 if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
49 die("bind");
50 if (listen(sock, max_connections) == -1)
51 die("listen");
52 return sock;
55 /* Returns true if in private address range: 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 */
56 static bool
57 is_private(struct in_addr *in)
59 return (ntohl(in->s_addr) & 0xff000000) >> 24 == 10
60 || (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16
61 || (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168;
64 /* Waits for a connection on the given socket, and returns the file descriptor.
65 * Updates the client address if it is not null.
66 * WARNING: the connection is not authenticated. As a weak security measure,
67 * the connections are limited to a private network. */
68 int
69 open_server_connection(int socket, struct in_addr *client)
71 assert(socket >= 0);
72 for (;;) {
73 struct sockaddr_in client_addr;
74 int sin_size = sizeof(struct sockaddr_in);
75 int fd = accept(socket, (struct sockaddr *)&client_addr, (socklen_t *)&sin_size);
76 if (fd == -1) {
77 die("accept");
79 if (is_private(&client_addr.sin_addr)) {
80 if (client)
81 *client = client_addr.sin_addr;
82 return fd;
84 close(fd);
88 /* Opens a new connection to the given port name, which must
89 * contain a host name. Returns the open file descriptor,
90 * or -1 if the open fails. */
91 static int
92 open_client_connection(char *port_name)
94 char hostname[BSIZE];
95 strncpy(hostname, port_name, sizeof(hostname));
96 char *port = strchr(hostname, ':');
97 assert(port);
98 *port++ = '\0';
100 int sock = socket(AF_INET, SOCK_STREAM, 0);
101 if (sock == -1)
102 die("socket");
103 struct hostent *host = gethostbyname(hostname);
104 struct sockaddr_in sin;
105 memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
106 sin.sin_family = AF_INET;
107 sin.sin_port = htons(atoi(port));
109 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
110 close(sock);
111 return -1;
113 return sock;
116 /* Allow connexion queue > 1 to avoid race conditions. */
117 #define MAX_CONNEXIONS 5
119 struct port_info {
120 int socket;
121 char *port;
124 /* Wait at most 30s between connection attempts. */
125 #define MAX_WAIT 30
127 /* Open a connection on the given socket/port.
128 * Act as server if the port doesn't contain a hostname,
129 * as a client otherwise. If socket < 0 or in client mode,
130 * create the socket from the given port and update socket.
131 * Block until the connection succeeds.
132 * Return a file descriptor for the new connection. */
133 static int
134 open_connection(struct port_info *info)
136 int conn;
137 char *p = strchr(info->port, ':');
138 if (p) {
139 for (int try = 1;; ) {
140 conn = open_client_connection(info->port);
141 if (conn >= 0) break;
142 sleep(try);
143 if (try < MAX_WAIT) try++;
145 info->socket = conn;
146 } else {
147 if (info->socket < 0)
148 info->socket = port_listen(info->port, MAX_CONNEXIONS);
149 conn = open_server_connection(info->socket, NULL);
151 return conn;
154 /* Open the log connection on the given port, redirect stderr to it. */
155 static void
156 open_log_connection(struct port_info *info)
158 int log_conn = open_connection(info);
159 if (dup2(log_conn, STDERR) < 0)
160 die("dup2");
161 if (DEBUGL(0))
162 fprintf(stderr, "log connection opened\n");
165 /* Thread keeping the log connection open and redirecting stderr to it.
166 * It also echoes its input, which can be used to check if the
167 * program is alive. As a weak identity check, in server mode the input
168 * must start with "Pachi" (without the quotes). */
169 static void *
170 log_thread(void *arg)
172 struct port_info *info = arg;
173 assert(info && info->port);
174 for (;;) {
175 char buf[BSIZE];
176 int size;
177 bool check = !strchr(info->port, ':');
178 if (!check)
179 write(STDERR, "Pachi\n", 6);
180 while ((size = read(STDERR, buf, BSIZE)) > 0) {
181 if (check && strncasecmp(buf, "Pachi", 5)) break;
182 check = false;
183 write(STDERR, buf, size);
185 fflush(stderr);
186 open_log_connection(info);
190 /* Open the log connection on the given port, redirect stderr to it,
191 * and keep reopening it if the connection is closed. */
192 void
193 open_log_port(char *port)
195 pthread_t thread;
196 static struct port_info log_info = { .socket = -1 };
197 log_info.port = port;
198 open_log_connection(&log_info);
200 /* From now on, log_info may only be modified by the single
201 * log_thread so static allocation is ok and there is no race. */
202 pthread_create(&thread, NULL, log_thread, (void *)&log_info);
205 /* Open the gtp connection on the given port, redirect stdin & stdout to it. */
206 void
207 open_gtp_connection(int *socket, char *port)
209 static struct port_info gtp_info = { .socket = -1 };
210 gtp_info.port = port;
211 int gtp_conn = open_connection(&gtp_info);
212 for (int d = STDIN; d <= STDOUT; d++) {
213 if (dup2(gtp_conn, d) < 0)
214 die("dup2");
216 if (DEBUGL(0))
217 fprintf(stderr, "gtp connection opened\n");