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