-
Notifications
You must be signed in to change notification settings - Fork 7
/
thin_server.c
310 lines (289 loc) · 8.84 KB
/
thin_server.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_WINSOCK2_H
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#include <winsock2.h>
#include <windows.h>
#define drand48() (((double)rand())/((double)RAND_MAX))
#define lrand48() rand()
#define srand48(x)
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "evpath.h"
#include "cm_internal.h"
#if defined (__INTEL_COMPILER)
// Allow int conversions
# pragma warning (disable: 2259)
#endif
static void socket_accept_thin_client(void *cmv, void * sockv);
extern void CMget_qual_hostname(CManager cm, char *buf, int len);
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
extern void
CMget_port_range(CManager cm, int *high_bound, int *low_bound);
extern int
EVthin_socket_listen(CManager cm, char **hostname_p, int *port_p)
{
unsigned int length;
struct sockaddr_in sock_addr;
int sock_opt_val = 1;
SOCKET conn_sock;
int int_port_num = 0;
u_short port_num = 0;
char host_name[256];
int high_bound, low_bound;
CMget_port_range(cm, &high_bound, &low_bound);
conn_sock = socket(AF_INET, SOCK_STREAM, 0);
if (conn_sock == SOCKET_ERROR) {
fprintf(stderr, "Cannot open INET socket\n");
return 0;
}
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = INADDR_ANY;
sock_addr.sin_port = (unsigned short) (htons(port_num));
if (setsockopt(conn_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &sock_opt_val,
sizeof(sock_opt_val)) != 0) {
fprintf(stderr, "Failed to set 1REUSEADDR on INET socket\n");
return 0;
}
{
long seedval = (long)time(NULL) + (long)getpid();
/* port num is free. Constrain to range to standards */
int size = high_bound - low_bound;
int tries = 30;
int result = SOCKET_ERROR;
srand48(seedval);
while (tries > 0) {
int target = low_bound + (int)(size * drand48());
sock_addr.sin_port = htons(target);
CMtrace_out(cm, CMConnectionVerbose, "CMSocket trying to bind port %d", target);
result = bind(conn_sock, (struct sockaddr *) &sock_addr,
sizeof sock_addr);
int_port_num = target;
tries--;
if (result != SOCKET_ERROR) tries = 0;
if (tries%5 == 4) {
/* try reseeding in case we're in sync with another process */
srand48(time(NULL) + getpid());
}
if (tries == 20) {
/* damn, tried a lot, increase the range (This might violate specified range) */
size *= 10;
}
if (tries == 10) {
/* damn, tried a lot more, increase the range (This might violate specified range) */
size *= 10;
}
}
}
/* if (bind(conn_sock, (struct sockaddr *) &sock_addr,
sizeof sock_addr) == SOCKET_ERROR) {
fprintf(stderr, "Cannot bind INET socket\n");
return 0;
}*/
sock_opt_val = 1;
if (setsockopt(conn_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &sock_opt_val,
sizeof(sock_opt_val)) != 0) {
perror("Failed to set 2REUSEADDR on INET socket");
return 0;
}
length = sizeof sock_addr;
if (getsockname(conn_sock, (struct sockaddr *) &sock_addr, &length) < 0) {
fprintf(stderr, "Cannot get socket name\n");
return 0;
}
/* begin listening for conns and set the backlog */
if (listen(conn_sock, FD_SETSIZE)) {
fprintf(stderr, "listen failed\n");
return 0;
}
/* set the port num as one we can be contacted at */
CM_fd_add_select(cm, conn_sock, socket_accept_thin_client,
(void *) cm, (void *) (intptr_t)conn_sock);
// int_port_num = (unsigned short)(ntohs(sock_addr.sin_port));
CMget_qual_hostname(cm, host_name, sizeof(host_name));
*hostname_p = strdup(host_name);
*port_p = int_port_num;
return 1;
}
typedef struct thin_conn {
FFSFile ffsfile;
SOCKET fd;
int target_stone;
int format_count;
FMStructDescList *format_list;
int max_src_list;
EVsource *src_list;
} *thin_conn_data;
static void thin_free_func(void *event_data, void *client_data)
{
(void) client_data;
free(event_data);
}
static void
thin_data_available(void *cmv, void * conn_datav)
{
thin_conn_data cd = conn_datav;
CManager cm = (CManager) cmv;
int i;
CManager_unlock(cm);
switch(FFSnext_record_type(cd->ffsfile)) {
case FFSindex:
break;
case FFSend:
case FFSerror:
close_FFSfile(cd->ffsfile);
free_FFSfile(cd->ffsfile);
for (i=0; i < cd->format_count; i++) {
int j = 0;
if (cd->format_list[i] == NULL) continue;
while((cd->format_list[i])[j].format_name != NULL) {
int k = 0;
free((void*)cd->format_list[i][j].format_name);
while (cd->format_list[i][j].field_list[k].field_name != NULL) {
free((char*)cd->format_list[i][j].field_list[k].field_name);
free((char*)cd->format_list[i][j].field_list[k].field_type);
k++;
}
free(cd->format_list[i][j].field_list);
j++;
}
free(cd->format_list[i]);
}
free(cd->format_list);
for (i=0; i <= cd->max_src_list; i++) {
if (cd->src_list[i] != NULL) {
EVfree_source(cd->src_list[i]);
}
}
free(cd->src_list);
CM_fd_remove_select(cm, cd->fd);
free(cd);
break;
case FFSformat: {
FFSTypeHandle next_format = FFSread_format(cd->ffsfile);
FMStructDescList formats =
get_localized_formats(FMFormat_of_original(next_format));
FFSTypeHandle target =
FFSset_fixed_target(FFSContext_of_file(cd->ffsfile),
formats);
int format_num = FMformat_index(FMFormat_of_original(target));
if (cd->format_list == NULL) {
cd->format_list = malloc(sizeof(cd->format_list[0]));
cd->format_count = 1;
}
if (cd->format_count < format_num) {
cd->format_list =
realloc(cd->format_list,
(format_num + 1) * sizeof(cd->format_list[0]));
memset(cd->format_list + cd->format_count, 0,
sizeof(cd->format_list[0]) * (format_num -cd->format_count +1));
cd->format_count = format_num + 1;
}
cd->format_list[format_num] = formats;
break;
}
case FFSdata: {
FFSTypeHandle next_format = FFSnext_type_handle(cd->ffsfile);
size_t len = FFSnext_data_length(cd->ffsfile);
int format_num = FMformat_index(FMFormat_of_original(next_format));
void *data = malloc(len);
FFSread(cd->ffsfile, data);
if (cd->max_src_list < format_num) {
cd->src_list = realloc(cd->src_list,
(format_num+1) * sizeof(cd->src_list[0]));
memset(&cd->src_list[cd->max_src_list], 0,
(format_num - cd->max_src_list + 1) * sizeof(cd->src_list[0]));
cd->max_src_list = format_num;
}
if (cd->src_list[format_num] == NULL) {
cd->src_list[format_num] =
EVcreate_submit_handle_free(cm, cd->target_stone,
cd->format_list[format_num],
thin_free_func, cd);
}
EVsubmit(cd->src_list[format_num], data, NULL);
break;
}
case FFScomment: {
char *comment = FFSread_comment(cd->ffsfile);
if (strncmp(comment, "Stone ", 6) == 0) {
int tmp_stone;
if (sscanf(comment, "Stone %d", &tmp_stone) == 1) {
cd->target_stone = tmp_stone;
}
}
break;
}
}
CManager_lock(cm);
}
static void
socket_accept_thin_client(void *cmv, void * sockv)
{
CManager cm = (CManager) cmv;
SOCKET conn_sock = (int) (intptr_t)sockv;
SOCKET sock;
struct sockaddr sock_addr;
unsigned int sock_len = sizeof(sock_addr);
int int_port_num;
struct linger linger_val;
int sock_opt_val = 1;
thin_conn_data cd;
#ifdef TCP_NODELAY
int delay_value = 1;
#endif
linger_val.l_onoff = 1;
linger_val.l_linger = 60;
if ((sock = accept(conn_sock, (struct sockaddr *) 0, (unsigned int *) 0)) == SOCKET_ERROR) {
perror("Cannot accept socket connection");
CM_fd_remove_select(cm, conn_sock);
fprintf(stderr, "failure in CMsockets removing socket connection\n");
return;
}
sock_opt_val = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *) &sock_opt_val,
sizeof(sock_opt_val));
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *) &linger_val,
sizeof(struct linger)) != 0) {
perror("set SO_LINGER");
return;
}
#ifdef TCP_NODELAY
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &delay_value,
sizeof(delay_value));
#endif
sock_len = sizeof(sock_addr);
memset(&sock_addr, 0, sock_len);
getsockname(sock, (struct sockaddr *) &sock_addr, &sock_len);
int_port_num = (unsigned short) ntohs(((struct sockaddr_in *) &sock_addr)->sin_port);
memset(&sock_addr, 0, sizeof(sock_addr));
sock_len = sizeof(sock_addr);
if (getpeername(sock, &sock_addr,
&sock_len) == 0) {
int_port_num = (unsigned short) ntohs(((struct sockaddr_in *) &sock_addr)->sin_port);
}
(void) int_port_num;
cd = malloc(sizeof(*cd));
memset(cd, 0, sizeof(*cd));
cd->ffsfile = open_FFSfd((void*)(intptr_t)sock, "r");
cd->fd = sock;
cd->src_list = malloc(sizeof(cd->src_list[0]));
cd->src_list[0] = NULL;
INT_CM_fd_add_select(cm, sock,
(void (*)(void *, void *)) thin_data_available,
(void *) cm, (void *) cd);
}