-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_sysv_tcp.c
424 lines (377 loc) · 11.9 KB
/
net_sysv_tcp.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
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
/* Multi-user networking protocol implementation for TCP/IP on System V */
/* It appears that there may never be a use for this particular protocol
* implementation because every vendor that provides a TLI interface to TCP
* also provides a complete BSD-style sockets implementation, so one could
* simply use net_bsd_tcp.c instead. I went ahead and provided this code
* anyway, for two reasons: (a) it gave the server code a kind of pleasing
* symmetry and completeness (and me a sense of closure), and (b) it can,
* perhaps, serve as a model for other TLI-based protocol implementations in
* the future. For example, there are vendors supplying TLI interfaces to the
* XNS protocol family without providing a sockets-based interface. In any
* case, it was interesting to learn about TLI and may prove useful in the
* future; I have been given to understand that TLI will probably form the
* basis for the eventual POSIX standard networking interface.
*/
#include "my-inet.h" /* inet_addr() */
#include <errno.h> /* EMFILE */
#include "my-fcntl.h" /* O_RDWR */
#include "my-in.h" /* struct sockaddr_in, INADDR_ANY, htonl(),
* htons(), ntohl(), struct in_addr */
#include "my-ioctl.h" /* ioctl() */
#include "my-socket.h" /* AF_INET */
#include "my-stdlib.h" /* strtoul() */
#include "my-string.h" /* memcpy() */
#include "my-stropts.h" /* I_POP, I_PUSH */
#include <sys/conf.h> /* FMNAMESZ */
#include "my-tiuser.h" /* t_open(), t_bind(), t_alloc(), t_accept() */
#include "my-unistd.h" /* close() */
#include "config.h"
#include "log.h"
#include "name_lookup.h"
#include "net_proto.h"
#include "options.h"
#include "server.h"
#include "streams.h"
#include "structures.h"
#include "timers.h"
static struct t_call *call = 0;
static void
log_ti_error(const char *msg)
{
if (t_errno == TSYSERR)
log_perror(msg);
else
errlog("%s: %s\n", msg, t_errlist[t_errno]);
}
const char *
proto_name(void)
{
return "SysV/TCP";
}
const char *
proto_usage_string(void)
{
return "[port]";
}
int
proto_initialize(struct proto *proto, Var * desc, int argc, char **argv)
{
int port = DEFAULT_PORT;
char *p;
initialize_name_lookup();
proto->pocket_size = 1;
proto->believe_eof = 1;
proto->eol_out_string = "\r\n";
if (argc > 1)
return 0;
else if (argc == 1) {
port = strtoul(argv[0], &p, 10);
if (*p != '\0')
return 0;
}
desc->type = TYPE_INT;
desc->v.num = port;
return 1;
}
enum error
proto_make_listener(Var desc, int *fd, Var * canon, const char **name)
{
struct sockaddr_in req_addr, rec_addr;
struct t_bind requested, received;
int s, port;
static Stream *st = 0;
if (!st)
st = new_stream(20);
if (desc.type != TYPE_INT)
return E_TYPE;
port = desc.v.num;
s = t_open((void *) "/dev/tcp", O_RDWR, 0);
if (s < 0) {
log_ti_error("Creating listening endpoint");
return E_QUOTA;
}
req_addr.sin_family = AF_INET;
req_addr.sin_addr.s_addr = htonl(INADDR_ANY);
req_addr.sin_port = htons(port);
requested.addr.maxlen = sizeof(req_addr);
requested.addr.len = sizeof(req_addr);
requested.addr.buf = (void *) &req_addr;
requested.qlen = 5;
received.addr.maxlen = sizeof(rec_addr);
received.addr.len = sizeof(rec_addr);
received.addr.buf = (void *) &rec_addr;
if (t_bind(s, &requested, &received) < 0) {
enum error e = E_QUOTA;
log_ti_error("Binding to listening address");
t_close(s);
if (t_errno == TACCES || (t_errno == TSYSERR && errno == EACCES))
e = E_PERM;
return e;
} else if (port != 0 && rec_addr.sin_port != port) {
errlog("Can't bind to requested port!\n");
t_close(s);
return E_QUOTA;
}
if (!call)
call = (struct t_call *) t_alloc(s, T_CALL, T_ADDR);
if (!call) {
log_ti_error("Allocating T_CALL structure");
t_close(s);
return E_QUOTA;
}
canon->type = TYPE_INT;
canon->v.num = ntohs(rec_addr.sin_port);
stream_printf(st, "port %d", canon->v.num);
*name = reset_stream(st);
*fd = s;
return E_NONE;
}
int
proto_listen(int fd)
{
/* Nothing to do. */
return 1;
}
static int
set_rw_able(int fd)
{
char buffer[FMNAMESZ + 1];
if (ioctl(fd, I_LOOK, buffer) < 0
|| strcmp(buffer, "timod") != 0
|| ioctl(fd, I_POP, 0) < 0
|| ioctl(fd, I_PUSH, (void *) "tirdwr") < 0) {
log_perror("Setting up read/write behavior for connection");
return 0;
} else
return 1;
}
enum proto_accept_error
proto_accept_connection(int listener_fd, int *read_fd, int *write_fd,
const char **name)
{
int timeout = server_int_option("name_lookup_timeout", 5);
int fd;
struct sockaddr_in *addr = (struct sockaddr_in *) call->addr.buf;
static Stream *s = 0;
if (!s)
s = new_stream(100);
fd = t_open((void *) "/dev/tcp", O_RDWR, 0);
if (fd < 0) {
if (t_errno == TSYSERR && errno == EMFILE)
return PA_FULL;
else {
log_ti_error("Opening endpoint for new connection");
return PA_OTHER;
}
}
if (t_bind(fd, 0, 0) < 0) {
log_ti_error("Binding endpoint for new connection");
t_close(fd);
return PA_OTHER;
}
if (t_listen(listener_fd, call) < 0) {
log_ti_error("Accepting new network connection");
t_close(fd);
return PA_OTHER;
}
if (t_accept(listener_fd, fd, call) < 0) {
log_ti_error("Accepting new network connection");
t_close(fd);
return PA_OTHER;
}
if (!set_rw_able(fd)) {
t_close(fd);
return PA_OTHER;
}
*read_fd = *write_fd = fd;
stream_printf(s, "%s, port %d",
lookup_name_from_addr(addr, timeout),
(int) ntohs(addr->sin_port));
*name = reset_stream(s);
return PA_OKAY;
}
void
proto_close_connection(int read_fd, int write_fd)
{
/* read_fd and write_fd are the same, so we only need to deal with one. */
t_close(read_fd);
}
void
proto_close_listener(int fd)
{
t_close(fd);
}
#ifdef OUTBOUND_NETWORK
#include "exceptions.h"
static Exception timeout_exception;
static void
timeout_proc(Timer_ID id, Timer_Data data)
{
RAISE(timeout_exception, 0);
}
enum error
proto_open_connection(Var arglist, int *read_fd, int *write_fd,
const char **local_name, const char **remote_name)
{
/* These are `static' rather than `volatile' because I can't cope with
* getting all those nasty little parameter-passing rules right. This
* function isn't recursive anyway, so it doesn't matter.
*/
struct sockaddr_in rec_addr;
struct t_bind received;
static const char *host_name;
static int port;
static Timer_ID id;
int fd, result;
int timeout = server_int_option("name_lookup_timeout", 5);
static struct sockaddr_in addr;
static Stream *st1 = 0, *st2 = 0;
if (!st1) {
st1 = new_stream(20);
st2 = new_stream(50);
}
if (arglist.v.list[0].v.num != 2)
return E_ARGS;
else if (arglist.v.list[1].type != TYPE_STR ||
arglist.v.list[2].type != TYPE_INT)
return E_TYPE;
host_name = arglist.v.list[1].v.str;
port = arglist.v.list[2].v.num;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = lookup_addr_from_name(host_name, timeout);
if (addr.sin_addr.s_addr == 0)
return E_INVARG;
/* Cast to (void *) here to workaround const-less decls on some systems. */
fd = t_open((void *) "/dev/tcp", O_RDWR, 0);
if (fd < 0) {
if (t_errno != TSYSERR || errno != EMFILE)
log_ti_error("Making endpoint in proto_open_connection");
return E_QUOTA;
}
received.addr.maxlen = sizeof(rec_addr);
received.addr.len = sizeof(rec_addr);
received.addr.buf = (void *) &rec_addr;
if (t_bind(fd, 0, &received) < 0) {
log_ti_error("Binding outbound endpoint");
t_close(fd);
return E_QUOTA;
}
call->addr.maxlen = sizeof(addr);
call->addr.len = sizeof(addr);
call->addr.buf = (void *) &addr;
TRY
id = set_timer(server_int_option("outbound_connect_timeout", 5),
timeout_proc, 0);
result = t_connect(fd, call, 0);
cancel_timer(id);
EXCEPT(timeout_exception)
result = -1;
errno = ETIMEDOUT;
t_errno = TSYSERR;
reenable_timers();
ENDTRY
if (result < 0) {
t_close(fd);
log_ti_error("Connecting in proto_open_connection");
return E_QUOTA;
}
if (!set_rw_able(fd)) {
t_close(fd);
return E_QUOTA;
}
*read_fd = *write_fd = fd;
stream_printf(st1, "port %d", (int) ntohs(rec_addr.sin_port));
*local_name = reset_stream(st1);
stream_printf(st2, "%s, port %d", host_name, port);
*remote_name = reset_stream(st2);
return E_NONE;
}
#endif /* OUTBOUND_NETWORK */
char rcsid_net_sysv_tcp[] = "$Id: net_sysv_tcp.c,v 1.2 1997/03/03 04:19:09 nop Exp $";
/* $Log: net_sysv_tcp.c,v $
/* Revision 1.2 1997/03/03 04:19:09 nop
/* GNU Indent normalization
/*
* Revision 1.1.1.1 1997/03/03 03:45:02 nop
* LambdaMOO 1.8.0p5
*
* Revision 2.5 1996/03/10 01:13:36 pavel
* Moved definition of DEFAULT_PORT to options.h. Release 1.8.0.
*
* Revision 2.4 1996/02/08 06:35:06 pavel
* Renamed err/logf() to errlog/oklog() and TYPE_NUM to TYPE_INT. Updated
* copyright notice for 1996. Release 1.8.0beta1.
*
* Revision 2.3 1995/12/31 00:00:56 pavel
* Added support for the new multiple-listening-points interface.
* Release 1.8.0alpha4.
*
* Revision 2.2 1995/12/28 00:34:06 pavel
* Removed old support for protocol-specific input EOL conventions.
* Release 1.8.0alpha3.
*
* Revision 2.1 1995/12/11 08:12:48 pavel
* Added support for $server_options.name_lookup_timeout and
* $server_options.outbound_connect_timeout. Release 1.8.0alpha2.
*
* Revision 2.0 1995/11/30 04:47:03 pavel
* New baseline version, corresponding to release 1.8.0alpha1.
*
* Revision 1.11 1993/10/11 19:02:14 pavel
* Worked around problem with a mis-declared t_open() on Solaris, where they
* neglected to specify `const' on the first argument.
*
* Revision 1.10 1993/08/11 03:15:44 pavel
* -- Worked around a problem in some systems' declaration of gethostbyname().
* -- Changed some bogus %d's to %u's in calls to *scanf() and *printf(),
* guided by warnings from GCC...
*
* Revision 1.9 1993/08/05 19:01:03 pavel
* Made another local variable in proto_open_connection `static' to avoid
* volatility warnings.
*
* Revision 1.8 1993/08/04 02:41:03 pavel
* Sigh. I never will learn not to check anything in until *after* it
* compiles...
*
* Revision 1.7 1993/08/04 02:19:58 pavel
* -- Added support for better logging of outbound connections.
*
* Revision 1.6 1993/08/04 00:25:10 pavel
* Use strtoul() instead of strtol() for the port argument.
*
* Revision 1.5 1993/08/04 00:22:12 pavel
* -- Fixed declaration of variables holding TCP ports to be unsigned.
* -- Support new `network_listen()' protocol.
* -- Set a timeout on outbound network connections.
*
* Revision 1.4 1992/10/23 23:03:47 pavel
* Added copyright notice.
*
* Revision 1.3 1992/10/21 03:02:35 pavel
* Converted to use new automatic configuration system.
*
* Revision 1.2 1992/10/17 21:02:51 pavel
* Changed to use "my-inet.h" rather than <arpa/inet.h>.
* Fixed typo in OUTBOUND_NETWORK support.
*
* Revision 1.1 1992/10/06 01:38:46 pavel
* Initial RCS-controlled version.
*/