forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpsvc.c
463 lines (394 loc) · 9.97 KB
/
tcpsvc.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*-
* Copyright (c) 1997 Dave Richards <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* and exceptions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. The code can not be used by Gary Random, Random Communications, Inc.,
* the employees of Random Communications, Inc. or its subsidiaries,
* including Defiance MUD, without prior written permission from the
* authors.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#include "lint.h"
#include "main.h"
#include "interpret.h"
#include "simulate.h"
#include "ndesc.h"
#include "nqueue.h"
#include "net.h"
#include "backend.h"
#include "tcpsvc.h"
#ifndef EPROTO
#define EPROTO EPROTOTYPE
#endif
#ifdef SERVICE_PORT
/*
* TCP Service
*/
/*
* TCP Service Control Block.
*/
typedef struct {
u_char ts_flags;
nqueue_t * ts_canq;
nqueue_t * ts_rawq;
ndesc_t * ts_nd;
struct task *task;
} tcpsvc_t;
/*
* TCP Service Flags.
*/
#define TF_CLOSE 0x01
/*
* Queue Sizes.
*/
#define TCPSVC_RAWQ_SIZE 32768
#define TCPSVC_CANQ_SIZE 32768
/*
* Maximum # of concurrent TCP Service.
*/
#define TCPSVC_MAX 32
// static ndesc_t *tcpsvc_nd = NULL;
static int tcpsvc_count = 0;
/*
* Allocate a TCP Service control block.
*/
static tcpsvc_t *
tcpsvc_alloc(void)
{
tcpsvc_t *tsp;
tsp = xalloc(sizeof (tcpsvc_t));
tsp->ts_flags = 0;
tsp->ts_rawq = nq_alloc(TCPSVC_RAWQ_SIZE);
tsp->ts_canq = nq_alloc(TCPSVC_CANQ_SIZE);
tsp->ts_nd = 0;
tsp->task = 0;
return tsp;
}
/*
* Free a TCP Service control block.
*/
static void
tcpsvc_free(tcpsvc_t *tsp)
{
if (tsp->ts_rawq)
nq_free(tsp->ts_rawq);
tsp->ts_rawq = NULL;
if (tsp->ts_canq)
nq_free(tsp->ts_canq);
tsp->ts_canq = NULL;
remove_task(tsp->task);
tsp->task = NULL;
free(tsp);
}
/*
* Process a disconnect indication.
*/
static void
tcpsvc_disconnect(ndesc_t *nd, tcpsvc_t *tsp)
{
tsp->ts_flags |= TF_CLOSE;
nd_enable(tsp->ts_nd, ND_W);
nd_disable(tsp->ts_nd, ND_R);
}
/*
* Close a TCP Service connection and free the associated resources.
*/
static void
tcpsvc_shutdown(ndesc_t *nd, tcpsvc_t *tsp)
{
if (tsp)
tsp->ts_flags |= TF_CLOSE;
}
static void
tcpsvc_process(void *vp)
{
struct svalue *svp;
struct gdexception exception_frame;
tcpsvc_t *tsp = vp;
nd_enable(tsp->ts_nd, ND_W);
tsp->task = 0;
if (tsp->ts_flags & TF_CLOSE)
{
close(nd_fd(tsp->ts_nd));
nd_detach(tsp->ts_nd);
tcpsvc_free(tsp);
tcpsvc_count--;
return;
}
update_tcp_av();
if (nq_full(tsp->ts_canq))
{
nq_init(tsp->ts_canq);
nq_puts(tsp->ts_canq, (u_char *)"ERROR Service request too long.\n");
tcpsvc_disconnect(tsp->ts_nd, tsp);
return;
}
exception_frame.e_exception = exception;
exception_frame.e_catch = 0;
exception = &exception_frame;
if (setjmp(exception_frame.e_context) == 0)
{
push_string((char *)nq_rptr(tsp->ts_canq), STRING_MSTRING);
svp = apply_master_ob(M_INCOMING_SERVICE, 1);
}
else
{
svp = NULL;
}
exception = exception->e_exception;
nq_init(tsp->ts_canq);
if (svp == NULL || svp->type != T_STRING)
{
nq_puts(tsp->ts_canq, (u_char *)"ERROR Service calls not supported.\n");
tcpsvc_disconnect(tsp->ts_nd, tsp);
return;
}
if (strlen(svp->u.string) > nq_size(tsp->ts_canq))
{
nq_puts(tsp->ts_canq, (u_char *)"ERROR Service response too long.\n");
tcpsvc_disconnect(tsp->ts_nd, tsp);
return;
}
nq_puts(tsp->ts_canq, (u_char *)svp->u.string);
}
/*
* Read the network into the raw input queue.
*/
static void
tcpsvc_read(ndesc_t *nd, tcpsvc_t *tsp)
{
int cc;
char c;
if (!nq_full(tsp->ts_rawq))
{
cc = nq_recv(tsp->ts_rawq, nd_fd(nd), NULL);
if (cc == -1)
{
switch (errno)
{
case EWOULDBLOCK:
case EINTR:
case EPROTO:
break;
default:
tcpsvc_disconnect(nd, tsp);
nd_disable(nd, ND_W);
tsp->task = create_task(tcpsvc_process, tsp);
return;
}
}
if (cc == 0)
{
tcpsvc_disconnect(nd, tsp);
nd_disable(nd, ND_W);
tsp->task = create_task(tcpsvc_process, tsp);
return;
}
}
for (;;)
{
if (nq_empty(tsp->ts_rawq))
{
nq_init(tsp->ts_rawq);
return;
}
c = nq_getc(tsp->ts_rawq);
if (c == '\n')
break;
if (!nq_full(tsp->ts_canq))
nq_putc(tsp->ts_canq, c);
}
nd_disable(tsp->ts_nd, ND_R);
if (!nq_full(tsp->ts_canq))
nq_putc(tsp->ts_canq, '\0');
tsp->task = create_task(tcpsvc_process, tsp);
}
/*
* Write the contents of the canonical queue to the network.
*/
static void
tcpsvc_write(ndesc_t *nd, tcpsvc_t *tsp)
{
if (!nq_empty(tsp->ts_canq))
{
if (nq_send(tsp->ts_canq, nd_fd(nd), NULL) == -1)
{
switch (errno)
{
case EWOULDBLOCK:
case EINTR:
case EPROTO:
break;
default:
tcpsvc_disconnect(nd, tsp);
nd_disable(nd, ND_W);
tsp->task = create_task(tcpsvc_process, tsp);
return;
}
}
if (!nq_empty(tsp->ts_canq))
return;
}
nq_init(tsp->ts_canq);
nd_disable(nd, ND_W);
/* If set to close close soon/now */
if (tsp->ts_flags & TF_CLOSE)
{
tsp->task = create_task(tcpsvc_process, tsp);
return;
}
nd_enable(nd, ND_R);
}
/*
* Accept a TCP Service connection.
*/
static void
tcpsvc_accept(void *vp)
{
int s;
char host[NI_MAXHOST], port[NI_MAXSERV];
struct sockaddr_storage addr;
socklen_t addrlen;
tcpsvc_t *tsp;
ndesc_t *nd = vp;
struct svalue *svp;
struct gdexception exception_frame;
nd_enable(nd, ND_R);
addrlen = sizeof (addr);
s = accept(nd_fd(nd), (struct sockaddr *)&addr, &addrlen);
if (s == -1)
{
switch (errno)
{
default:
fatal("svc_server: accept() errno = %d.\n", errno);
case EWOULDBLOCK:
case EINTR:
case EPROTO:
return;
}
}
getnameinfo((struct sockaddr *)&addr, addrlen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
exception_frame.e_exception = exception;
exception_frame.e_catch = 0;
exception = &exception_frame;
if (setjmp(exception_frame.e_context) == 0)
{
push_string(host, STRING_MSTRING);
push_number(atoi(port));
svp = apply_master_ob(M_VALID_INCOMING_SERVICE, 2);
}
else
{
svp = NULL;
}
exception = exception->e_exception;
if (svp == NULL || svp->type != T_NUMBER || svp->u.number == 0)
{
fprintf(stderr, "SERVICE PORT ACCESS DENIED FROM [%s]:%s\n", host, port);
close(s);
return;
}
enable_nbio(s);
tsp = tcpsvc_alloc();
tsp->ts_nd = nd_attach(s, tcpsvc_read, tcpsvc_write, NULL, NULL,
tcpsvc_shutdown, tsp);
if (++tcpsvc_count > TCPSVC_MAX)
{
nq_puts(tsp->ts_canq, (u_char *)"ERROR Too many services in use.\n");
nd_enable(tsp->ts_nd, ND_W);
tcpsvc_disconnect(tsp->ts_nd, tsp);
return;
}
nd_enable(tsp->ts_nd, ND_R);
}
static void
tcpsvc_ready(ndesc_t *nd, void *vp)
{
nd_disable(nd, ND_R);
create_task(tcpsvc_accept, nd);
}
/*
* Initialize the TCP Service Manager.
*/
void
tcpsvc_init(u_short port_nr)
{
int s, e;
struct addrinfo hints;
struct addrinfo *res, *rp;
char host[NI_MAXHOST], port[NI_MAXSERV];
ndesc_t *nd;
if (service_port < 0)
return;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
snprintf(port, sizeof(port), "%d", port_nr);
e = getaddrinfo(NULL, port, &hints, &res);
if (e)
{
perror(gai_strerror(e));
exit(1);
}
s = -1;
for (rp = res; rp != NULL; rp = rp->ai_next)
{
s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (s == -1)
continue;
enable_reuseaddr(s);
if (rp->ai_family == AF_INET6)
enable_v6only(s);
getnameinfo(rp->ai_addr, rp->ai_addrlen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
if (bind(s, rp->ai_addr, rp->ai_addrlen) == 0)
{
/* Success */
printf("Listening to tcp service port: %s:%s\n", host, port);
enable_reuseaddr(s);
enable_nbio(s);
if (listen(s, 5) == -1)
{
close(s);
return;
}
nd = nd_attach(s, tcpsvc_ready, NULL, NULL, NULL, tcpsvc_shutdown, NULL);
nd_enable(nd, ND_R);
} else {
fatal("Failed to bind tcp service port: %s:%s\n", host, port);
close(s);
}
}
}
#endif