1
/* ========================================================================
2
* Copyright 1988-2006 University of Washington
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
11
* ========================================================================
15
* Program: TOPS-20 TCP/IP routines
17
* Author: Mark Crispin
18
* Networks and Distributed Computing
19
* Computing & Communications
20
* University of Washington
21
* Administration Building, AG-44
23
* Internet: MRC@CAC.Washington.EDU
26
* Last Edited: 30 August 2006
31
* This file is dedicated with affection to the TOPS-20 operating system, which
32
* set standards for user and programmer friendliness that have still not been
33
* equaled by more `modern' operating systems.
37
/* TCP/IP manipulate parameters
38
* Accepts: function code
39
* function-dependent value
40
* Returns: function-dependent return value
43
void *tcp_parameters (long function,void *value)
50
* contact service name
52
* Returns: TCP stream if success else NIL
55
TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
57
char *s,tmp[MAILTMPLEN];
58
TCPSTREAM *stream = NIL;
60
unsigned long i,j,k,l;
61
char file[MAILTMPLEN];
62
port &= 0xffff; /* erase flags */
64
if (host[0] == '[' && host[strlen (host)-1] == ']') {
65
if (((i = strtoul (s = host+1,&s,10)) <= 255) && *s++ == '.' &&
66
((j = strtoul (s,&s,10)) <= 255) && *s++ == '.' &&
67
((k = strtoul (s,&s,10)) <= 255) && *s++ == '.' &&
68
((l = strtoul (s,&s,10)) <= 255) && *s++ == ']' && !*s) {
69
argblk[3] = (i << 24) + (j << 16) + (k << 8) + l;
70
sprintf (tmp,"[%lu.%lu.%lu.%lu]",i,j,k,l);
73
sprintf (tmp,"Bad format domain-literal: %.80s",host);
78
else { /* host name */
79
argblk[1] = _GTHPN; /* get IP address and primary name */
80
argblk[2] = (int) (host-1); /* pointer to host */
81
argblk[4] = (int) (tmp-1);
82
if (!jsys (GTHST,argblk)) { /* first try DEC's domain way */
83
argblk[1] = _GTHPN; /* get IP address and primary name */
84
argblk[2] = (int) (host-1);
85
argblk[4] = (int) (tmp-1);
86
if (!jsys (GTDOM,argblk)){/* try the CHIVES domain way */
87
argblk[1] = _GTHSN; /* failed, do the host table then */
88
if (!jsys (GTHST,argblk)) {
89
sprintf (tmp,"No such host as %s",host);
93
argblk[1] = _GTHNS; /* convert number to string */
94
argblk[2] = (int) (tmp-1);
95
/* get the official name */
96
if (!jsys (GTHST,argblk)) strcpy (tmp,host);
101
sprintf (file,"TCP:.%o-%d;PERSIST:30;CONNECTION:ACTIVE",argblk[3],port);
102
argblk[1] = GJ_SHT; /* short form GTJFN% */
103
argblk[2] = (int) (file-1); /* pointer to file name */
104
/* get JFN for TCP: file */
105
if (!jsys (GTJFN,argblk)) fatal ("Unable to create TCP JFN");
106
jfn = argblk[1]; /* note JFN for later */
107
/* want 8-bit bidirectional I/O */
108
argblk[2] = OF_RD|OF_WR|(FLD (8,monsym("OF%BSZ")));
109
if (!jsys (OPENF,argblk)) {
110
sprintf (file,"Can't connect to %s,%d server",tmp,port);
114
/* create TCP/IP stream */
115
stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
116
stream->host = cpystr (tmp); /* copy official host name */
117
argblk[1] = _GTHNS; /* convert number to string */
118
argblk[2] = (int) (tmp-1);
119
argblk[3] = -1; /* want local host */
120
if (!jsys (GTHST,argblk)) strcpy (tmp,"LOCAL");
121
stream->localhost = cpystr (tmp);
122
stream->port = port; /* save port number */
123
stream->jfn = jfn; /* init JFN */
127
/* TCP/IP authenticated open
128
* Accepts: NETMBX specifier
130
* returned user name buffer
131
* Returns: TCP/IP stream if success else NIL
134
TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
139
/* TCP/IP receive line
140
* Accepts: TCP/IP stream
141
* Returns: text line string or NIL if failure
144
char *tcp_getline (TCPSTREAM *stream)
149
argblk[1] = stream->jfn; /* read from TCP */
150
/* pointer to buffer */
151
argblk[2] = (int) (stream->ibuf-1);
152
argblk[3] = BUFLEN; /* max number of bytes to read */
153
argblk[4] = '\012'; /* terminate on LF */
154
if (!jsys (SIN,argblk)) return NIL;
155
n = BUFLEN - argblk[3]; /* number of bytes read */
156
/* got a complete line? */
157
if ((stream->ibuf[n - 2] == '\015') && (stream->ibuf[n - 1] == '\012')) {
158
memcpy ((ret = (char *) fs_get (n)),stream->ibuf,n - 2);
159
ret[n - 2] = '\0'; /* tie off string with null */
162
/* copy partial string */
163
memcpy ((stp = ret = (char *) fs_get (n)),stream->ibuf,n);
164
/* special case of newline broken by buffer */
165
if ((stream->ibuf[n - 1] == '\015') && jsys (BIN,argblk) &&
166
(argblk[2] == '\012')) { /* was it? */
167
ret[n - 1] = '\0'; /* tie off string with null */
169
/* recurse to get remainder */
170
else if (jsys (BKJFN,argblk) && (st = tcp_getline (stream))) {
171
ret = (char *) fs_get (n + 1 + (m = strlen (st)));
172
memcpy (ret,stp,n); /* copy first part */
173
memcpy (ret + n,st,m); /* and second part */
174
fs_give ((void **) &stp); /* flush first part */
175
fs_give ((void **) &st); /* flush second part */
176
ret[n + m] = '\0'; /* tie off string with null */
181
/* TCP/IP receive buffer
182
* Accepts: TCP/IP stream
184
* buffer to read into
185
* Returns: T if success, NIL otherwise
188
long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
191
argblk[1] = stream->jfn; /* read from TCP */
192
argblk[2] = (int) (buffer-1); /* pointer to buffer */
193
argblk[3] = -size; /* number of bytes to read */
194
if (!jsys (SIN,argblk)) return NIL;
195
buffer[size] = '\0'; /* tie off text */
200
/* TCP/IP send string as record
201
* Accepts: TCP/IP stream
203
* Returns: T if success else NIL
206
long tcp_soutr (TCPSTREAM *stream,char *string)
209
argblk[1] = stream->jfn; /* write to TCP */
210
argblk[2] = (int) (string-1); /* pointer to buffer */
211
argblk[3] = 0; /* write until NUL */
212
if (!jsys (SOUTR,argblk)) return NIL;
217
/* TCP/IP send string
218
* Accepts: TCP/IP stream
221
* Returns: T if success else NIL
224
long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
227
argblk[1] = stream->jfn; /* write to TCP */
228
argblk[2] = (int) (string-1); /* pointer to buffer */
229
argblk[3] = -size; /* write this many bytes */
230
if (!jsys (SOUTR,argblk)) return NIL;
235
* Accepts: TCP/IP stream
238
void tcp_close (TCPSTREAM *stream)
241
argblk[1] = stream->jfn; /* close TCP */
243
/* flush host names */
244
fs_give ((void **) &stream->host);
245
fs_give ((void **) &stream->localhost);
246
fs_give ((void **) &stream); /* flush the stream */
250
/* TCP/IP return host for this stream
251
* Accepts: TCP/IP stream
252
* Returns: host name for this stream
255
char *tcp_host (TCPSTREAM *stream)
257
return stream->host; /* return host name */
261
/* TCP/IP return remote host for this stream
262
* Accepts: TCP/IP stream
263
* Returns: host name for this stream
266
char *tcp_remotehost (TCPSTREAM *stream)
268
return stream->host; /* return host name */
272
/* TCP/IP return port for this stream
273
* Accepts: TCP/IP stream
274
* Returns: port number for this stream
277
unsigned long tcp_port (TCPSTREAM *stream)
279
return stream->port; /* return port number */
283
/* TCP/IP return local host for this stream
284
* Accepts: TCP/IP stream
285
* Returns: local host name for this stream
288
char *tcp_localhost (TCPSTREAM *stream)
290
return stream->localhost; /* return local host name */
293
/* TCP/IP return canonical form of host name
295
* Returns: canonical form of host name
298
char *tcp_canonical (char *name)
301
static char tmp[MAILTMPLEN];
302
/* look like domain literal? */
303
if (name[0] == '[' && name[strlen (name) - 1] == ']') return name;
304
argblk[1] = _GTHPN; /* get IP address and primary name */
305
argblk[2] = (int) (name-1); /* pointer to host */
306
argblk[4] = (int) (tmp-1); /* pointer to return destination */
307
if (!jsys (GTHST,argblk)) { /* first try DEC's domain way */
308
argblk[1] = _GTHPN; /* get IP address and primary name */
309
argblk[2] = (int) (name-1);
310
argblk[4] = (int) (tmp-1);
311
if (!jsys (GTDOM,argblk)) { /* try the CHIVES domain way */
312
argblk[1] = _GTHSN; /* failed, do the host table then */
313
if (!jsys (GTHST,argblk)) return name;
314
argblk[1] = _GTHNS; /* convert number to string */
315
argblk[2] = (int) (tmp-1);
316
/* get the official name */
317
if (!jsys (GTHST,argblk)) return name;
324
/* TCP/IP get client host name (server calls only)
325
* Returns: client host name
328
char *tcp_clienthost ()