-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconnect.c
More file actions
executable file
·315 lines (289 loc) · 8.8 KB
/
connect.c
File metadata and controls
executable file
·315 lines (289 loc) · 8.8 KB
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
/*[[ CMS REPLACEMENT HISTORY, Element CONNECT.C]]*/
/*[[ *2 1-JAN-1999 11:38:30 MADISON "Fix return of status code in do_connect"]]*/
/*[[ *1 12-MAR-1998 16:40:56 MADISON "Initial CMS population"]]*/
/*[[ CMS REPLACEMENT HISTORY, Element CONNECT.C]]*/
/*
**++
** FACILITY: NETLIB
**
** ABSTRACT: Connect by name.
**
** MODULE DESCRIPTION:
**
** This module contains the NETLIB_CONNECT_BY_NAME routine,
** which does establishes an active TCP connect to a remote host and
** port by name, rather than address.
**
** AUTHOR: M. Madison
** COPYRIGHT © 1994,1995,1998 MADGOAT SOFTWARE. ALL RIGHTS RESERVED.
**
** CREATION DATE: 25-OCT-1994
**
** MODIFICATION HISTORY:
**
** 25-OCT-1994 V1.0 Madison Initial coding.
** 17-NOV-1994 V1.0-1 Madison Don't use _both_ DNS & host tables!
** 19-JAN-1994 V1.0-2 Madison Fix connect() call completion args.
** 28-DEC-1998 V1.0-3 Madison Fix connect() return in case 1.
**--
*/
#include "netlib.h"
/*
** Context structure used to track our progress
*/
struct Connect_Context {
struct NETLIBIOSBDEF iosb;
struct dsc$descriptor dsc;
unsigned int ctxsize;
char *name;
struct CTX *ctx;
int state;
unsigned int adrcnt, htadrcnt, nsadrcnt;
int curadr;
void (*astadr)();
void *astprm;
struct NETLIBIOSBDEF *user_iosb;
struct SINDEF sin;
struct INADDRDEF *adrlst;
struct INADDRDEF htadrlst[8], nsadrlst[8];
};
/*
** Forward declarations
*/
unsigned int netlib_connect_by_name(struct CTX **xctx,
struct dsc$descriptor *dsc, unsigned short *port,
struct NETLIBIOSBDEF *iosb, void (*astadr)(), void *astprm);
static unsigned int do_connect(struct Connect_Context *con);
/*
** OWN storage
*/
static unsigned int usedns = NETLIB_K_LOOKUP_DNS;
static unsigned int useht = NETLIB_K_LOOKUP_HOST_TABLE;
static unsigned int sinsize = sizeof(struct SINDEF);
/*
** External references
*/
unsigned int netlib_name_to_address(struct CTX **, unsigned int *,
struct dsc$descriptor *, struct INADDRDEF *,
unsigned int *, ...);
unsigned int netlib_connect(struct CTX **, struct SINDEF *,
unsigned int *, ...);
unsigned int netlib_strtoaddr(struct dsc$descriptor *, struct INADDRDEF *);
/*
**++
** ROUTINE: netlib_connect_by_name
**
** FUNCTIONAL DESCRIPTION:
**
** Connects to a remote host/port by name. The name is looked up,
** then a connection is tried to each address until a connection is
** established, or we run out of addresses.
**
** RETURNS: cond_value, longword (unsigned), write only, by value
**
** PROTOTYPE:
**
** NETLIB_CONNECT_BY_NAME ctxptr, namdsc, port [,iosb] [,astadr] [,astprm]
**
** ctxptr: NETLIB context, longword (unsigned), read only, by reference
** namdsc: char_string, character string, read only, by descriptor
** port: word_unsigned, word (unsigned), read only, by reference
** iosb: io_status_block, quadword (unsigned), write only, by reference
** astadr: ast_procedure, procedure value, call, by reference
** astprm: user_arg, longword (unsigned), read only, by value
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** SS$_NORMAL: normal successful completion
** SS$_INSFARG: not enough arguments
** SS$_BADPARAM: invalid argument
** Codes from LIB$GET_VM, LIB$ANALYZE_SDESC, and
** other NETLIB network status codes.
**
** SIDE EFFECTS: None.
**
**--
*/
unsigned int netlib_connect_by_name (struct CTX **xctx,
struct dsc$descriptor *dsc, unsigned short *port,
struct NETLIBIOSBDEF *iosb, void (*astadr)(), void *astprm) {
struct Connect_Context *con;
struct CTX *ctx;
struct INADDRDEF addr;
unsigned int status, size;
unsigned short namlen;
char *namp;
int argc;
/*
** Verify the arguments
*/
VERIFY_CTX(xctx, ctx);
SETARGCOUNT(argc);
if (argc < 3) return SS$_INSFARG;
if (dsc == 0 || port == 0) return SS$_BADPARAM;
/*
** Allocate and fill in the connection context
*/
status = lib$analyze_sdesc(dsc, &namlen, &namp);
if (!OK(status)) return status;
size = namlen + sizeof(struct Connect_Context);
status = lib$get_vm(&size, &con);
if (!OK(status)) return status;
memset(con, 0, size);
con->name = (char *) (con + 1);
memcpy(con->name, namp, namlen);
INIT_SDESC(con->dsc, namlen, con->name);
con->ctx = ctx;
con->sin.sin_w_family = NETLIB_K_AF_INET;
con->sin.sin_w_port = netlib_word_swap(*port);
con->ctxsize = size;
size = sizeof(con->htadrlst)/sizeof(con->htadrlst[0]);
if (argc > 3 && iosb != 0) con->user_iosb = iosb;
if (argc > 4 && astadr != 0) {
con->astadr = astadr;
if (argc > 5) con->astprm = astprm;
}
/*
** If they provided us with a dotted-decimal IP address, fake
** out do_connect to make it look like we looked up the address
** via DNS.
*/
if (OK(netlib_strtoaddr(&con->dsc, &addr))) {
con->iosb.iosb_w_status = SS$_NORMAL;
con->nsadrcnt = 1;
con->nsadrlst[0] = addr;
if (con->astadr != 0) return sys$dclast(do_connect, con, 0);
return do_connect(con);
}
/*
** Make lookup via host table synchronous and in main-line thread
** because we can't call it from AST level for all packages
*/
status = netlib_name_to_address(&con->ctx, &useht, &con->dsc,
con->htadrlst, &size, &con->htadrcnt, &con->iosb);
if (!OK(status)) con->htadrcnt = 0;
size = sizeof(con->nsadrlst)/sizeof(con->nsadrlst[0]);
/*
** For an asynch call, do the DNS lookup and have DO_CONNECT invoked
** as the AST routine
*/
if (argc > 4 && astadr != 0) {
status = netlib_name_to_address(&con->ctx, &usedns, &con->dsc,
con->nsadrlst, &size, &con->nsadrcnt, &con->iosb,
do_connect, con);
if (!OK(status)) lib$free_vm(&con->ctxsize, &con);
return status;
}
/*
** Synchronous call: do the DNS lookup...
*/
status = netlib_name_to_address(&con->ctx, &usedns, &con->dsc,
con->nsadrlst, &size, &con->nsadrcnt, &con->iosb);
/*
** ... if it failed, fall back on the host table lookup info we got
*/
if (!OK(status)) {
con->iosb.iosb_w_status = SS$_ENDOFFILE;
con->nsadrcnt = 0;
}
/*
** Just call DO_CONNECT to complete this for us
*/
return do_connect(con);
} /* netlib_connect_by_name */
/*
**++
** ROUTINE: do_connect
**
** FUNCTIONAL DESCRIPTION:
**
** Completion routine for NETLIB_CONNECT_BY_NAME. Can be
** invoked as a regular main-line routine or an AST completion.
**
** RETURNS: cond_value, longword (unsigned), write only, by value
**
** PROTOTYPE:
**
** DO_CONNECT connection-context
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: Any NETLIB network status code.
**
** SIDE EFFECTS: None.
**
**--
*/
static unsigned int do_connect (struct Connect_Context *con) {
unsigned int status;
int done;
/*
** We implement our FSM as a loop for the synchronous case
*/
done = 0;
while (!done) {
status = con->iosb.iosb_w_status;
switch (con->state) {
/*
** Initial state -- if the DNS lookup failed, fall back on host table
** entry. Otherwise, start trying the connections.
*/
case 0:
if (con->nsadrcnt == 0) {
if (con->htadrcnt == 0) {
con->iosb.iosb_w_status = SS$_ENDOFFILE;
done = 1;
break;
}
con->adrlst = con->htadrlst;
con->adrcnt = con->htadrcnt;
} else {
con->adrlst = con->nsadrlst;
con->adrcnt = con->nsadrcnt;
}
con->state = 1;
/* and fall through */
/*
** State 1: Attempt a connection
*/
case 1:
con->sin.sin_x_addr = con->adrlst[con->curadr++];
con->state = 2;
status = netlib_connect(&con->ctx, &con->sin, &sinsize,
&con->iosb, (con->astadr == 0) ? 0 : do_connect,
(con->astadr == 0) ? 0 : con);
if (!OK(status)) done = 1;
else if (con->astadr != 0) return status;
break;
/*
** State 2: connect() completion status check. If we're successful
** or we've run out of addresses, we're done. Otherwise, we loop
** back up and try again.
*/
case 2:
if (OK(status) || con->curadr >= con->adrcnt) done = 1;
con->state = 1;
break;
}
}
/*
** We're done, one way or another. Fill in the caller's IOSB and
** call back the AST, if there was one.
*/
if (con->user_iosb != 0)
memcpy(con->user_iosb, &con->iosb, sizeof(con->iosb));
if (con->astadr != 0) (*con->astadr)(con->astprm);
/*
** We're done with this context -- free it
*/
lib$free_vm(&con->ctxsize, &con);
/*
** Synchronous completion occurs here.
*/
return status;
} /* do_connect */