~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to imap/src/osdep/tops-20/tcp_t20.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ========================================================================
 
2
 * Copyright 1988-2006 University of Washington
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * 
 
11
 * ========================================================================
 
12
 */
 
13
 
 
14
/*
 
15
 * Program:     TOPS-20 TCP/IP routines
 
16
 *
 
17
 * Author:      Mark Crispin
 
18
 *              Networks and Distributed Computing
 
19
 *              Computing & Communications
 
20
 *              University of Washington
 
21
 *              Administration Building, AG-44
 
22
 *              Seattle, WA  98195
 
23
 *              Internet: MRC@CAC.Washington.EDU
 
24
 *
 
25
 * Date:        1 August 1988
 
26
 * Last Edited: 30 August 2006
 
27
 */
 
28
 
 
29
 
 
30
/* Dedication:
 
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.
 
34
 * Wasureru mon ka!!!!
 
35
 */
 
36
 
 
37
/* TCP/IP manipulate parameters
 
38
 * Accepts: function code
 
39
 *          function-dependent value
 
40
 * Returns: function-dependent return value
 
41
 */
 
42
 
 
43
void *tcp_parameters (long function,void *value)
 
44
{
 
45
  return NIL;
 
46
}
 
47
 
 
48
/* TCP/IP open
 
49
 * Accepts: host name
 
50
 *          contact service name
 
51
 *          contact port number
 
52
 * Returns: TCP stream if success else NIL
 
53
 */
 
54
 
 
55
TCPSTREAM *tcp_open (char *host,char *service,unsigned long port)
 
56
{
 
57
  char *s,tmp[MAILTMPLEN];
 
58
  TCPSTREAM *stream = NIL;
 
59
  int argblk[5],jfn;
 
60
  unsigned long i,j,k,l;
 
61
  char file[MAILTMPLEN];
 
62
  port &= 0xffff;               /* erase flags */
 
63
                                /* domain literal? */
 
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);
 
71
    }
 
72
    else {
 
73
      sprintf (tmp,"Bad format domain-literal: %.80s",host);
 
74
      mm_log (tmp,ERROR);
 
75
      return NIL;
 
76
    }
 
77
  }
 
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);
 
90
          mm_log (tmp,ERROR);
 
91
          return NIL;
 
92
        }
 
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);
 
97
      }
 
98
    }
 
99
  }
 
100
 
 
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);
 
111
    mm_log (file,ERROR);
 
112
    return NIL;
 
113
  }
 
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 */
 
124
  return stream;
 
125
}
 
126
 
 
127
/* TCP/IP authenticated open
 
128
 * Accepts: NETMBX specifier
 
129
 *          service name
 
130
 *          returned user name buffer
 
131
 * Returns: TCP/IP stream if success else NIL
 
132
 */
 
133
 
 
134
TCPSTREAM *tcp_aopen (NETMBX *mb,char *service,char *usrbuf)
 
135
{
 
136
  return NIL;
 
137
}
 
138
 
 
139
/* TCP/IP receive line
 
140
 * Accepts: TCP/IP stream
 
141
 * Returns: text line string or NIL if failure
 
142
 */
 
143
 
 
144
char *tcp_getline (TCPSTREAM *stream)
 
145
{
 
146
  int argblk[5];
 
147
  int n,m;
 
148
  char *ret,*stp,*st;
 
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 */
 
160
    return ret;
 
161
  }
 
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 */
 
168
  }
 
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 */
 
177
  }
 
178
  return ret;
 
179
}
 
180
 
 
181
/* TCP/IP receive buffer
 
182
 * Accepts: TCP/IP stream
 
183
 *          size in bytes
 
184
 *          buffer to read into
 
185
 * Returns: T if success, NIL otherwise
 
186
 */
 
187
 
 
188
long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
 
189
{
 
190
  int argblk[5];
 
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 */
 
196
  return T;
 
197
}
 
198
 
 
199
 
 
200
/* TCP/IP send string as record
 
201
 * Accepts: TCP/IP stream
 
202
 *          string pointer
 
203
 * Returns: T if success else NIL
 
204
 */
 
205
 
 
206
long tcp_soutr (TCPSTREAM *stream,char *string)
 
207
{
 
208
  int argblk[5];
 
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;
 
213
  return T;
 
214
}
 
215
 
 
216
 
 
217
/* TCP/IP send string
 
218
 * Accepts: TCP/IP stream
 
219
 *          string pointer
 
220
 *          byte count
 
221
 * Returns: T if success else NIL
 
222
 */
 
223
 
 
224
long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
 
225
{
 
226
  int argblk[5];
 
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;
 
231
  return T;
 
232
}
 
233
 
 
234
/* TCP/IP close
 
235
 * Accepts: TCP/IP stream
 
236
 */
 
237
 
 
238
void tcp_close (TCPSTREAM *stream)
 
239
{
 
240
  int argblk[5];
 
241
  argblk[1] = stream->jfn;      /* close TCP */
 
242
  jsys (CLOSF,argblk);
 
243
                                /* flush host names */
 
244
  fs_give ((void **) &stream->host);
 
245
  fs_give ((void **) &stream->localhost);
 
246
  fs_give ((void **) &stream);  /* flush the stream */
 
247
}
 
248
 
 
249
 
 
250
/* TCP/IP return host for this stream
 
251
 * Accepts: TCP/IP stream
 
252
 * Returns: host name for this stream
 
253
 */
 
254
 
 
255
char *tcp_host (TCPSTREAM *stream)
 
256
{
 
257
  return stream->host;          /* return host name */
 
258
}
 
259
 
 
260
 
 
261
/* TCP/IP return remote host for this stream
 
262
 * Accepts: TCP/IP stream
 
263
 * Returns: host name for this stream
 
264
 */
 
265
 
 
266
char *tcp_remotehost (TCPSTREAM *stream)
 
267
{
 
268
  return stream->host;          /* return host name */
 
269
}
 
270
 
 
271
 
 
272
/* TCP/IP return port for this stream
 
273
 * Accepts: TCP/IP stream
 
274
 * Returns: port number for this stream
 
275
 */
 
276
 
 
277
unsigned long tcp_port (TCPSTREAM *stream)
 
278
{
 
279
  return stream->port;          /* return port number */
 
280
}
 
281
 
 
282
 
 
283
/* TCP/IP return local host for this stream
 
284
 * Accepts: TCP/IP stream
 
285
 * Returns: local host name for this stream
 
286
 */
 
287
 
 
288
char *tcp_localhost (TCPSTREAM *stream)
 
289
{
 
290
  return stream->localhost;     /* return local host name */
 
291
}
 
292
 
 
293
/* TCP/IP return canonical form of host name
 
294
 * Accepts: host name
 
295
 * Returns: canonical form of host name
 
296
 */
 
297
 
 
298
char *tcp_canonical (char *name)
 
299
{
 
300
  int argblk[5];
 
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;
 
318
    }
 
319
  }
 
320
  return tmp;
 
321
}
 
322
 
 
323
 
 
324
/* TCP/IP get client host name (server calls only)
 
325
 * Returns: client host name
 
326
 */
 
327
 
 
328
char *tcp_clienthost ()
 
329
{
 
330
  return "UNKNOWN";
 
331
}