1 /* Utility functions to redirect stdin, stdout, stderr to sockets. */
12 #include <sys/types.h>
13 #include <sys/socket.h>
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. */
36 port_listen(char *port
, int max_connections
)
38 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
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)
50 if (listen(sock
, max_connections
) == -1)
55 /* Returns true if in private address range: 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 */
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. */
69 open_server_connection(int socket
, struct in_addr
*client
)
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
);
79 if (is_private(&client_addr
.sin_addr
)) {
81 *client
= client_addr
.sin_addr
;
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. */
92 open_client_connection(char *port_name
)
95 strncpy(hostname
, port_name
, sizeof(hostname
));
96 char *port
= strchr(hostname
, ':');
100 struct hostent
*host
= gethostbyname(hostname
);
103 int sock
= socket(AF_INET
, SOCK_STREAM
, 0);
106 struct sockaddr_in sin
;
107 memcpy(&sin
.sin_addr
.s_addr
, host
->h_addr
, host
->h_length
);
108 sin
.sin_family
= AF_INET
;
109 sin
.sin_port
= htons(atoi(port
));
111 if (connect(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0) {
118 /* Allow connexion queue > 1 to avoid race conditions. */
119 #define MAX_CONNEXIONS 5
126 /* Wait at most 30s between connection attempts. */
129 /* Open a connection on the given socket/port.
130 * Act as server if the port doesn't contain a hostname,
131 * as a client otherwise. If socket < 0 or in client mode,
132 * create the socket from the given port and update socket.
133 * Block until the connection succeeds.
134 * Return a file descriptor for the new connection. */
136 open_connection(struct port_info
*info
)
139 char *p
= strchr(info
->port
, ':');
141 for (int try = 1;; ) {
142 conn
= open_client_connection(info
->port
);
143 if (conn
>= 0) break;
145 if (try < MAX_WAIT
) try++;
149 if (info
->socket
< 0)
150 info
->socket
= port_listen(info
->port
, MAX_CONNEXIONS
);
151 conn
= open_server_connection(info
->socket
, NULL
);
156 /* Open the log connection on the given port, redirect stderr to it. */
158 open_log_connection(struct port_info
*info
)
160 int log_conn
= open_connection(info
);
161 if (dup2(log_conn
, STDERR
) < 0)
164 fprintf(stderr
, "log connection opened\n");
167 /* Thread keeping the log connection open and redirecting stderr to it.
168 * It also echoes its input, which can be used to check if the
169 * program is alive. As a weak identity check, in server mode the input
170 * must start with "Pachi" (without the quotes). */
172 log_thread(void *arg
)
174 struct port_info
*info
= arg
;
175 assert(info
&& info
->port
);
179 bool check
= !strchr(info
->port
, ':');
181 write(STDERR
, "Pachi\n", 6);
182 while ((size
= read(STDERR
, buf
, BSIZE
)) > 0) {
183 if (check
&& strncasecmp(buf
, "Pachi", 5)) break;
185 write(STDERR
, buf
, size
);
188 open_log_connection(info
);
192 /* Open the log connection on the given port, redirect stderr to it,
193 * and keep reopening it if the connection is closed. */
195 open_log_port(char *port
)
198 static struct port_info log_info
= { .socket
= -1 };
199 log_info
.port
= port
;
200 open_log_connection(&log_info
);
202 /* From now on, log_info may only be modified by the single
203 * log_thread so static allocation is ok and there is no race. */
204 pthread_create(&thread
, NULL
, log_thread
, (void *)&log_info
);
207 /* Open the gtp connection on the given port, redirect stdin & stdout to it. */
209 open_gtp_connection(int *socket
, char *port
)
211 static struct port_info gtp_info
= { .socket
= -1 };
212 gtp_info
.port
= port
;
213 int gtp_conn
= open_connection(>p_info
);
214 for (int d
= STDIN
; d
<= STDOUT
; d
++) {
215 if (dup2(gtp_conn
, d
) < 0)
219 fprintf(stderr
, "gtp connection opened\n");