1 |
|
---|
2 | /*
|
---|
3 | * DNA (Delphi Network Access) implements the network layer for the DIM
|
---|
4 | * (Delphi Information Managment) System.
|
---|
5 | *
|
---|
6 | * Started date : 10-11-91
|
---|
7 | * Written by : C. Gaspar
|
---|
8 | * UNIX adjustment: G.C. Ballintijn
|
---|
9 | *
|
---|
10 | */
|
---|
11 |
|
---|
12 | /* include files */
|
---|
13 |
|
---|
14 | #include <errno.h>
|
---|
15 | #define DIMLIB
|
---|
16 | #define DNA
|
---|
17 | #include <dim.h>
|
---|
18 |
|
---|
19 | /* global definitions */
|
---|
20 |
|
---|
21 | #define READ_HEADER_SIZE 12
|
---|
22 |
|
---|
23 | /*
|
---|
24 | #define TO_DBG 1
|
---|
25 | */
|
---|
26 |
|
---|
27 | /* global variables */
|
---|
28 | typedef struct {
|
---|
29 | char node_name[MAX_NODE_NAME];
|
---|
30 | char task_name[MAX_TASK_NAME];
|
---|
31 | int port;
|
---|
32 | SRC_TYPES src_type;
|
---|
33 | } PENDING_OPEN;
|
---|
34 |
|
---|
35 | static PENDING_OPEN Pending_conns[MAX_CONNS];
|
---|
36 |
|
---|
37 | static int DNA_Initialized = FALSE;
|
---|
38 |
|
---|
39 | extern int Tcpip_max_io_data_write;
|
---|
40 | extern int Tcpip_max_io_data_read;
|
---|
41 |
|
---|
42 | _DIM_PROTO( static void ast_read_h, (int conn_id, int status, int size) );
|
---|
43 | _DIM_PROTO( static void ast_conn_h, (int handle, int svr_conn_id,
|
---|
44 | int protocol) );
|
---|
45 | _DIM_PROTO( static int dna_write_bytes, (int conn_id, void *buffer, int size,
|
---|
46 | int nowait) );
|
---|
47 | _DIM_PROTO( static void release_conn, (int conn_id) );
|
---|
48 | _DIM_PROTO( static void save_node_task, (int conn_id, DNA_NET *buffer) );
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Routines common to Server and Client
|
---|
52 | */
|
---|
53 | /*
|
---|
54 | static int Prev_packet[3];
|
---|
55 | static int Prev_buffer[3];
|
---|
56 | static int Prev_conn_id = 0;
|
---|
57 | */
|
---|
58 | static int is_header( int conn_id )
|
---|
59 | {
|
---|
60 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
|
---|
61 | register int ret;
|
---|
62 |
|
---|
63 | ret = 0;
|
---|
64 | if( (vtohl(dna_connp->buffer[2]) == TRP_MAGIC) &&
|
---|
65 | (vtohl(dna_connp->buffer[1]) == 0) &&
|
---|
66 | (vtohl(dna_connp->buffer[0]) == READ_HEADER_SIZE) )
|
---|
67 | {
|
---|
68 | dna_connp->state = RD_HDR;
|
---|
69 | ret = 1;
|
---|
70 | }
|
---|
71 | else if( (vtohl(dna_connp->buffer[2]) == TST_MAGIC) &&
|
---|
72 | (vtohl(dna_connp->buffer[1]) == 0) &&
|
---|
73 | (vtohl(dna_connp->buffer[0]) == READ_HEADER_SIZE) )
|
---|
74 | {
|
---|
75 | dna_connp->state = RD_HDR;
|
---|
76 | ret = 1;
|
---|
77 | }
|
---|
78 | else if( (vtohl(dna_connp->buffer[2]) == (int)HDR_MAGIC ) &&
|
---|
79 | (vtohl(dna_connp->buffer[0]) == (int)READ_HEADER_SIZE ) )
|
---|
80 | {
|
---|
81 | dna_connp->state = RD_DATA;
|
---|
82 | ret = 1;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | dim_print_date_time();
|
---|
88 | printf( " conn: %d to %s@%s, expecting header\n", conn_id,
|
---|
89 | Net_conns[conn_id].task, Net_conns[conn_id].node );
|
---|
90 | printf( "buffer[0]=%d\n", vtohl(dna_connp->buffer[0]));
|
---|
91 | printf( "buffer[1]=%d\n", vtohl(dna_connp->buffer[1]));
|
---|
92 | printf( "buffer[2]=%x\n", vtohl(dna_connp->buffer[2]));
|
---|
93 | printf( "closing the connection.\n" );
|
---|
94 | printf( " Previous conn: %d, Previous Packet\n", Prev_conn_id);
|
---|
95 | printf( "buffer[0]=%d\n", vtohl(Prev_packet[0]));
|
---|
96 | printf( "buffer[1]=%d\n", vtohl(Prev_packet[1]));
|
---|
97 | printf( "buffer[2]=%x\n", vtohl(Prev_packet[2]));
|
---|
98 | printf( " Previous Buffer\n");
|
---|
99 | printf( "buffer[0]=%d\n", vtohl(Prev_buffer[0]));
|
---|
100 | printf( "buffer[1]=%d\n", vtohl(Prev_buffer[1]));
|
---|
101 | printf( "buffer[2]=%x\n", vtohl(Prev_buffer[2]));
|
---|
102 | fflush(stdout);
|
---|
103 | */
|
---|
104 | dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
|
---|
105 | ret = 0;
|
---|
106 | }
|
---|
107 | return(ret);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void read_data( int conn_id)
|
---|
111 | {
|
---|
112 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
|
---|
113 |
|
---|
114 | if( !dna_connp->saw_init &&
|
---|
115 | vtohl(dna_connp->buffer[0]) == (int)OPN_MAGIC)
|
---|
116 | {
|
---|
117 | save_node_task(conn_id, (DNA_NET *) dna_connp->buffer);
|
---|
118 | dna_connp->saw_init = TRUE;
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | /*
|
---|
123 | printf("passing up %d bytes, conn_id %d\n",dna_connp->full_size, conn_id);
|
---|
124 | */
|
---|
125 | dna_connp->read_ast(conn_id, dna_connp->buffer,
|
---|
126 | dna_connp->full_size, STA_DATA);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | static void ast_read_h( int conn_id, int status, int size )
|
---|
131 | {
|
---|
132 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
|
---|
133 | int tcpip_code;
|
---|
134 | register int read_size, next_size;
|
---|
135 | register char *buff;
|
---|
136 | int max_io_data;
|
---|
137 |
|
---|
138 | if(!dna_connp->buffer) /* The connection has already been closed */
|
---|
139 | {
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | if(status == 1)
|
---|
143 | {
|
---|
144 | next_size = dna_connp->curr_size;
|
---|
145 | buff = (char *) dna_connp->curr_buffer;
|
---|
146 | if(size < next_size)
|
---|
147 | {
|
---|
148 | /*
|
---|
149 | Prev_conn_id = conn_id;
|
---|
150 | Prev_packet[0] = ((int *)dna_connp->curr_buffer)[0];
|
---|
151 | Prev_packet[1] = ((int *)dna_connp->curr_buffer)[1];
|
---|
152 | Prev_packet[2] = ((int *)dna_connp->curr_buffer)[2];
|
---|
153 | Prev_buffer[0] = dna_connp->buffer[0];
|
---|
154 | Prev_buffer[1] = dna_connp->buffer[1];
|
---|
155 | Prev_buffer[2] = dna_connp->buffer[2];
|
---|
156 | */
|
---|
157 | max_io_data = Tcpip_max_io_data_read;
|
---|
158 | read_size = ((next_size - size) > max_io_data) ?
|
---|
159 | max_io_data : next_size - size;
|
---|
160 | dna_connp->curr_size -= size;
|
---|
161 | dna_connp->curr_buffer += size;
|
---|
162 | tcpip_code = tcpip_start_read(conn_id, buff + size,
|
---|
163 | read_size, ast_read_h);
|
---|
164 | if(tcpip_failure(tcpip_code))
|
---|
165 | {
|
---|
166 | #ifndef WIN32
|
---|
167 | if(errno == ENOTSOCK)
|
---|
168 | {
|
---|
169 | if(dna_connp->read_ast)
|
---|
170 | dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
|
---|
171 | }
|
---|
172 | else
|
---|
173 | #endif
|
---|
174 | {
|
---|
175 | dna_report_error(conn_id, tcpip_code,
|
---|
176 | "Reading from", DIM_ERROR, DIMTCPRDERR);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | return;
|
---|
180 | }
|
---|
181 | switch(dna_connp->state)
|
---|
182 | {
|
---|
183 | case RD_HDR :
|
---|
184 | if(is_header(conn_id))
|
---|
185 | {
|
---|
186 | if( dna_connp->state == RD_DATA )
|
---|
187 | {
|
---|
188 | next_size = vtohl(dna_connp->buffer[1]);
|
---|
189 | dna_start_read(conn_id, next_size);
|
---|
190 | }
|
---|
191 | else
|
---|
192 | {
|
---|
193 | dna_connp->state = RD_HDR;
|
---|
194 | dna_start_read(conn_id, READ_HEADER_SIZE);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | break;
|
---|
198 | case RD_DATA :
|
---|
199 | read_data(conn_id);
|
---|
200 | dna_connp->state = RD_HDR;
|
---|
201 | dna_start_read(conn_id, READ_HEADER_SIZE);
|
---|
202 | break;
|
---|
203 | default:
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | /*
|
---|
207 | if(dna_connp->buffer)
|
---|
208 | {
|
---|
209 | Prev_conn_id = conn_id;
|
---|
210 | Prev_packet[0] = ((int *)dna_connp->curr_buffer)[0];
|
---|
211 | Prev_packet[1] = ((int *)dna_connp->curr_buffer)[1];
|
---|
212 | Prev_packet[2] = ((int *)dna_connp->curr_buffer)[2];
|
---|
213 | Prev_buffer[0] = dna_connp->buffer[0];
|
---|
214 | Prev_buffer[1] = dna_connp->buffer[1];
|
---|
215 | Prev_buffer[2] = dna_connp->buffer[2];
|
---|
216 | }
|
---|
217 | */
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | /*
|
---|
222 | printf("Connection lost. Signal upper layer\n");
|
---|
223 | */
|
---|
224 | if(dna_connp->read_ast)
|
---|
225 | dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | int dna_start_read(int conn_id, int size)
|
---|
231 | {
|
---|
232 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
|
---|
233 | register int tcpip_code, read_size;
|
---|
234 | int max_io_data;
|
---|
235 |
|
---|
236 | if(!dna_connp->busy)
|
---|
237 | {
|
---|
238 | return(0);
|
---|
239 | }
|
---|
240 |
|
---|
241 | dna_connp->curr_size = size;
|
---|
242 | dna_connp->full_size = size;
|
---|
243 | if(size > dna_connp->buffer_size)
|
---|
244 | {
|
---|
245 | dna_connp->buffer =
|
---|
246 | (int *) realloc(dna_connp->buffer, size);
|
---|
247 | dna_connp->buffer_size = size;
|
---|
248 | }
|
---|
249 | dna_connp->curr_buffer = (char *) dna_connp->buffer;
|
---|
250 | max_io_data = Tcpip_max_io_data_read;
|
---|
251 | read_size = (size > max_io_data) ? max_io_data : size ;
|
---|
252 |
|
---|
253 | tcpip_code = tcpip_start_read(conn_id, dna_connp->curr_buffer,
|
---|
254 | read_size, ast_read_h);
|
---|
255 | if(tcpip_failure(tcpip_code)) {
|
---|
256 | dna_report_error(conn_id, tcpip_code,
|
---|
257 | "Reading from", DIM_ERROR, DIMTCPRDERR);
|
---|
258 |
|
---|
259 | return(0);
|
---|
260 | }
|
---|
261 |
|
---|
262 | return(1);
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | static int dna_write_bytes( int conn_id, void *buffer, int size, int nowait )
|
---|
267 | {
|
---|
268 | register int size_left, wrote;
|
---|
269 | register char *p;
|
---|
270 | int max_io_data;
|
---|
271 | #ifdef VMS
|
---|
272 | int retries = WRITE_RETRIES, retrying = 0;
|
---|
273 | float wait_time = 0.01;
|
---|
274 | #endif
|
---|
275 | extern int tcpip_write_nowait(int, char *, int);
|
---|
276 |
|
---|
277 | max_io_data = Tcpip_max_io_data_write;
|
---|
278 | p = (char *) buffer;
|
---|
279 | size_left = size;
|
---|
280 | do {
|
---|
281 | size = (size_left > max_io_data) ? max_io_data : size_left ;
|
---|
282 | #ifdef VMS
|
---|
283 | if(nowait)
|
---|
284 | {
|
---|
285 | while(--retries)
|
---|
286 | {
|
---|
287 | if((wrote = tcpip_write_nowait(conn_id, p, size)) > 0)
|
---|
288 | break;
|
---|
289 | if(!tcpip_would_block(wrote))
|
---|
290 | return(0);
|
---|
291 | if(retries == WRITE_RETRIES_WNG)
|
---|
292 | {
|
---|
293 | dna_report_error(conn_id, tcpip_code,
|
---|
294 | "Writing to (retrying)", DIM_WARNING, DIMTCPWRRTY);
|
---|
295 | retrying = 1;
|
---|
296 | }
|
---|
297 | lib$wait(&wait_time);
|
---|
298 | }
|
---|
299 | if(!retries)
|
---|
300 | {
|
---|
301 | return(0);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | else
|
---|
305 | wrote = tcpip_write(conn_id, p, size);
|
---|
306 | #else
|
---|
307 | if(nowait)
|
---|
308 | {
|
---|
309 | wrote = tcpip_write_nowait(conn_id, p, size);
|
---|
310 | if(wrote == -1)
|
---|
311 | {
|
---|
312 | dna_report_error(conn_id, -1,
|
---|
313 | "Write timeout, disconnecting from", DIM_ERROR, DIMTCPWRTMO);
|
---|
314 | wrote = 0;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | else
|
---|
318 | {
|
---|
319 | wrote = tcpip_write(conn_id, p, size);
|
---|
320 | }
|
---|
321 | #endif
|
---|
322 |
|
---|
323 | if( tcpip_failure(wrote) )
|
---|
324 | return(0);
|
---|
325 | p += wrote;
|
---|
326 | size_left -= wrote;
|
---|
327 | } while(size_left > 0);
|
---|
328 | return(1);
|
---|
329 | }
|
---|
330 |
|
---|
331 | void dna_test_write(int conn_id)
|
---|
332 | {
|
---|
333 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
|
---|
334 | register int tcpip_code;
|
---|
335 | DNA_HEADER test_pkt;
|
---|
336 | register DNA_HEADER *test_p = &test_pkt;
|
---|
337 |
|
---|
338 | if(!dna_connp->busy)
|
---|
339 | {
|
---|
340 | return;
|
---|
341 | }
|
---|
342 | if(dna_connp->writing)
|
---|
343 | {
|
---|
344 | return;
|
---|
345 | }
|
---|
346 | test_p->header_size = htovl(READ_HEADER_SIZE);
|
---|
347 | test_p->data_size = 0;
|
---|
348 | test_p->header_magic = htovl(TST_MAGIC);
|
---|
349 | tcpip_code = dna_write_bytes(conn_id, &test_pkt, READ_HEADER_SIZE,0);
|
---|
350 | if(tcpip_failure(tcpip_code)) {
|
---|
351 | /* Connection lost. Signal upper layer ? */
|
---|
352 | if(dna_connp->read_ast)
|
---|
353 | dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
|
---|
354 | return;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | typedef struct
|
---|
359 | {
|
---|
360 | int conn_id;
|
---|
361 | void *buffer;
|
---|
362 | int size;
|
---|
363 | char dummy[MAX_NAME];
|
---|
364 | } WRITE_ITEM;
|
---|
365 |
|
---|
366 | static int do_dna_write(int id)
|
---|
367 | {
|
---|
368 | register DNA_CONNECTION *dna_connp;
|
---|
369 | int tcpip_code;
|
---|
370 | WRITE_ITEM *ptr;
|
---|
371 | int conn_id, size;
|
---|
372 | void *buffer;
|
---|
373 |
|
---|
374 | ptr = (WRITE_ITEM *)id_get_ptr(id, SRC_DNA);
|
---|
375 | if(!ptr)
|
---|
376 | return(2);
|
---|
377 | conn_id = ptr->conn_id;
|
---|
378 | buffer = ptr->buffer;
|
---|
379 | size = ptr->size;
|
---|
380 |
|
---|
381 | dna_connp = &Dna_conns[conn_id];
|
---|
382 | if(!dna_connp->busy)
|
---|
383 | {
|
---|
384 | id_free(id, SRC_DNA);
|
---|
385 | free(buffer);
|
---|
386 | free(ptr);
|
---|
387 | return(2);
|
---|
388 | }
|
---|
389 | dna_connp->writing = TRUE;
|
---|
390 | tcpip_code = dna_write_bytes(conn_id, buffer, size,0);
|
---|
391 | if(tcpip_failure(tcpip_code))
|
---|
392 | {
|
---|
393 | dna_connp->writing = FALSE;
|
---|
394 | id_free(id, SRC_DNA);
|
---|
395 | free(buffer);
|
---|
396 | free(ptr);
|
---|
397 | return(0);
|
---|
398 | }
|
---|
399 |
|
---|
400 | id_free(id, SRC_DNA);
|
---|
401 | free(buffer);
|
---|
402 | free(ptr);
|
---|
403 |
|
---|
404 | dna_connp->writing = FALSE;
|
---|
405 | return(1);
|
---|
406 | }
|
---|
407 |
|
---|
408 | int dna_write_nowait(int conn_id, void *buffer, int size)
|
---|
409 | {
|
---|
410 | register DNA_CONNECTION *dna_connp;
|
---|
411 | DNA_HEADER header_pkt;
|
---|
412 | register DNA_HEADER *header_p = &header_pkt;
|
---|
413 | int tcpip_code, ret = 1;
|
---|
414 |
|
---|
415 | DISABLE_AST
|
---|
416 | dna_connp = &Dna_conns[conn_id];
|
---|
417 | if(!dna_connp->busy)
|
---|
418 | {
|
---|
419 | ENABLE_AST
|
---|
420 | return(2);
|
---|
421 | }
|
---|
422 | dna_connp->writing = TRUE;
|
---|
423 |
|
---|
424 | header_p->header_size = htovl(READ_HEADER_SIZE);
|
---|
425 | header_p->data_size = htovl(size);
|
---|
426 | header_p->header_magic = htovl(HDR_MAGIC);
|
---|
427 | tcpip_code = dna_write_bytes(conn_id, &header_pkt, READ_HEADER_SIZE, 1);
|
---|
428 | if(tcpip_failure(tcpip_code))
|
---|
429 | {
|
---|
430 | dna_connp->writing = FALSE;
|
---|
431 | ENABLE_AST
|
---|
432 | return(0);
|
---|
433 | }
|
---|
434 | tcpip_code = dna_write_bytes(conn_id, buffer, size, 1);
|
---|
435 | if(tcpip_failure(tcpip_code))
|
---|
436 | {
|
---|
437 | ret = 0;
|
---|
438 | }
|
---|
439 | dna_connp->writing = FALSE;
|
---|
440 | ENABLE_AST
|
---|
441 | return(ret);
|
---|
442 | }
|
---|
443 |
|
---|
444 | typedef struct
|
---|
445 | {
|
---|
446 | DNA_HEADER header;
|
---|
447 | char data[1];
|
---|
448 |
|
---|
449 | }WRITE_DATA;
|
---|
450 |
|
---|
451 | int dna_write(int conn_id, void *buffer, int size)
|
---|
452 | {
|
---|
453 | WRITE_ITEM *newp;
|
---|
454 | int id;
|
---|
455 | WRITE_DATA *pktp;
|
---|
456 | DNA_HEADER *headerp;
|
---|
457 |
|
---|
458 | DISABLE_AST
|
---|
459 |
|
---|
460 | pktp = malloc(READ_HEADER_SIZE+size);
|
---|
461 | headerp = &(pktp->header);
|
---|
462 | headerp->header_size = htovl(READ_HEADER_SIZE);
|
---|
463 | headerp->data_size = htovl(size);
|
---|
464 | headerp->header_magic = htovl(HDR_MAGIC);
|
---|
465 |
|
---|
466 | memcpy(pktp->data, (char *)buffer, size);
|
---|
467 |
|
---|
468 | newp = malloc(sizeof(WRITE_ITEM));
|
---|
469 | newp->conn_id = conn_id;
|
---|
470 | newp->buffer = pktp;
|
---|
471 | newp->size = size+READ_HEADER_SIZE;
|
---|
472 | id = id_get((void *)newp, SRC_DNA);
|
---|
473 | dtq_start_timer(0, do_dna_write, id);
|
---|
474 | ENABLE_AST
|
---|
475 | return(1);
|
---|
476 | }
|
---|
477 |
|
---|
478 | /* Server Routines */
|
---|
479 |
|
---|
480 | static void ast_conn_h(int handle, int svr_conn_id, int protocol)
|
---|
481 | {
|
---|
482 | register DNA_CONNECTION *dna_connp;
|
---|
483 | register int tcpip_code;
|
---|
484 | register int conn_id;
|
---|
485 |
|
---|
486 | if(protocol){}
|
---|
487 | conn_id = conn_get();
|
---|
488 | /*
|
---|
489 | if(!conn_id)
|
---|
490 | dim_panic("In ast_conn_h: No more connections\n");
|
---|
491 | */
|
---|
492 | dna_connp = &Dna_conns[conn_id] ;
|
---|
493 | dna_connp->error_ast = Dna_conns[svr_conn_id].error_ast;
|
---|
494 | tcpip_code = tcpip_open_connection( conn_id, handle );
|
---|
495 |
|
---|
496 | if(tcpip_failure(tcpip_code))
|
---|
497 | {
|
---|
498 | dna_report_error(conn_id, tcpip_code,
|
---|
499 | "Connecting to", DIM_ERROR, DIMTCPCNERR);
|
---|
500 | conn_free(conn_id);
|
---|
501 | } else {
|
---|
502 | dna_connp->state = RD_HDR;
|
---|
503 | dna_connp->buffer = (int *)malloc(TCP_RCV_BUF_SIZE);
|
---|
504 | /*
|
---|
505 | if(!dna_connp->buffer)
|
---|
506 | {
|
---|
507 | printf("Error in DNA - handle_connection malloc returned 0\n");
|
---|
508 | fflush(stdout);
|
---|
509 | }
|
---|
510 | */
|
---|
511 | dna_connp->buffer_size = TCP_RCV_BUF_SIZE;
|
---|
512 | dna_connp->read_ast = Dna_conns[svr_conn_id].read_ast;
|
---|
513 | dna_connp->saw_init = FALSE;
|
---|
514 | dna_start_read(conn_id, READ_HEADER_SIZE); /* sizeof(DNA_NET) */
|
---|
515 | /* Connection arrived. Signal upper layer ? */
|
---|
516 | dna_connp->read_ast(conn_id, NULL, 0, STA_CONN);
|
---|
517 | }
|
---|
518 | tcpip_code = tcpip_start_listen(svr_conn_id, ast_conn_h);
|
---|
519 | if(tcpip_failure(tcpip_code))
|
---|
520 | {
|
---|
521 | dna_report_error(svr_conn_id, tcpip_code,
|
---|
522 | "Listening at", DIM_ERROR, DIMTCPLNERR);
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | int dna_init()
|
---|
527 | {
|
---|
528 | if(!DNA_Initialized)
|
---|
529 | {
|
---|
530 | conn_arr_create(SRC_DNA);
|
---|
531 | DNA_Initialized = TRUE;
|
---|
532 | }
|
---|
533 | return(1);
|
---|
534 | }
|
---|
535 |
|
---|
536 | int dna_open_server(char *task, void (*read_ast)(), int *protocol, int *port, void (*error_ast)())
|
---|
537 | {
|
---|
538 | register DNA_CONNECTION *dna_connp;
|
---|
539 | register int tcpip_code;
|
---|
540 | register int conn_id;
|
---|
541 |
|
---|
542 | if(!DNA_Initialized)
|
---|
543 | {
|
---|
544 | conn_arr_create(SRC_DNA);
|
---|
545 | DNA_Initialized = TRUE;
|
---|
546 | }
|
---|
547 | *protocol = PROTOCOL;
|
---|
548 | conn_id = conn_get();
|
---|
549 | dna_connp = &Dna_conns[conn_id];
|
---|
550 | /*
|
---|
551 | if(!conn_id)
|
---|
552 | dim_panic("In dna_open_server: No more connections\n");
|
---|
553 | */
|
---|
554 | dna_connp->protocol = TCPIP;
|
---|
555 | dna_connp->error_ast = error_ast;
|
---|
556 | tcpip_code = tcpip_open_server(conn_id, task, port);
|
---|
557 | if(tcpip_failure(tcpip_code))
|
---|
558 | {
|
---|
559 | dna_report_error(conn_id, tcpip_code,
|
---|
560 | "Opening server port", DIM_ERROR, DIMTCPOPERR);
|
---|
561 | conn_free(conn_id);
|
---|
562 | return(0);
|
---|
563 | }
|
---|
564 | dna_connp->writing = FALSE;
|
---|
565 | dna_connp->read_ast = read_ast;
|
---|
566 | tcpip_code = tcpip_start_listen(conn_id, ast_conn_h);
|
---|
567 | if(tcpip_failure(tcpip_code))
|
---|
568 | {
|
---|
569 | dna_report_error(conn_id, tcpip_code, "Listening at", DIM_ERROR, DIMTCPLNERR);
|
---|
570 | return(0);
|
---|
571 | }
|
---|
572 | return(conn_id);
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | int dna_get_node_task(int conn_id, char *node, char *task)
|
---|
577 | {
|
---|
578 | if(Dna_conns[conn_id].busy)
|
---|
579 | tcpip_get_node_task(conn_id, node, task);
|
---|
580 | else
|
---|
581 | node[0] = '\0';
|
---|
582 | return(1);
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | /* Client Routines */
|
---|
587 |
|
---|
588 | void dna_set_test_write(int conn_id, int time)
|
---|
589 | {
|
---|
590 | extern void tcpip_set_test_write(int, int);
|
---|
591 |
|
---|
592 | tcpip_set_test_write(conn_id, time);
|
---|
593 | }
|
---|
594 |
|
---|
595 | void dna_rem_test_write(int conn_id)
|
---|
596 | {
|
---|
597 | extern void tcpip_rem_test_write(int);
|
---|
598 |
|
---|
599 | tcpip_rem_test_write(conn_id);
|
---|
600 | }
|
---|
601 |
|
---|
602 | static int ins_pend_conn( char *node, char *task, int port, SRC_TYPES src_type )
|
---|
603 | {
|
---|
604 | register PENDING_OPEN *pending_connp;
|
---|
605 | register int i;
|
---|
606 |
|
---|
607 | for( i = 1, pending_connp = &Pending_conns[1]; i < MAX_CONNS;
|
---|
608 | i++, pending_connp++ )
|
---|
609 | {
|
---|
610 | if( pending_connp->task_name[0] == '\0' )
|
---|
611 | {
|
---|
612 | strcpy(pending_connp->node_name, node);
|
---|
613 | strcpy(pending_connp->task_name, task);
|
---|
614 | pending_connp->port = port;
|
---|
615 | pending_connp->src_type = src_type;
|
---|
616 | return(i);
|
---|
617 | }
|
---|
618 | }
|
---|
619 | return(0);
|
---|
620 | }
|
---|
621 |
|
---|
622 | static int find_pend_conn( char *node, char *task, int port, SRC_TYPES src_type )
|
---|
623 | {
|
---|
624 | register PENDING_OPEN *pending_connp;
|
---|
625 | register int i;
|
---|
626 |
|
---|
627 | for( i = 1, pending_connp = &Pending_conns[1]; i < MAX_CONNS;
|
---|
628 | i++, pending_connp++ )
|
---|
629 | {
|
---|
630 | if( (!strcmp(pending_connp->node_name, node)) &&
|
---|
631 | (!strcmp(pending_connp->task_name, task)) &&
|
---|
632 | (pending_connp->port == port) &&
|
---|
633 | (pending_connp->src_type == src_type))
|
---|
634 | {
|
---|
635 | return(i);
|
---|
636 | }
|
---|
637 | }
|
---|
638 | return(0);
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | static void rel_pend_conn( int conn_id )
|
---|
643 | {
|
---|
644 | Pending_conns[conn_id].task_name[0] = '\0';
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | int dna_open_client(char *server_node, char *server_task, int port, int server_protocol,
|
---|
649 | void (*read_ast)(), void (*error_ast)(), SRC_TYPES src_type)
|
---|
650 | {
|
---|
651 | register DNA_CONNECTION *dna_connp;
|
---|
652 | char str[256];
|
---|
653 | register int tcpip_code, conn_id, id;
|
---|
654 | DNA_NET local_buffer;
|
---|
655 | extern int get_proc_name(char *);
|
---|
656 | char src_type_str[64];
|
---|
657 |
|
---|
658 | if(server_protocol){}
|
---|
659 | if(!DNA_Initialized) {
|
---|
660 | conn_arr_create(SRC_DNA);
|
---|
661 | DNA_Initialized = TRUE;
|
---|
662 | }
|
---|
663 | conn_id = conn_get();
|
---|
664 | dna_connp = &Dna_conns[conn_id] ;
|
---|
665 | /*
|
---|
666 | if( !(conn_id = conn_get()) )
|
---|
667 | dim_panic("In dna_open_client: No more connections\n");
|
---|
668 | */
|
---|
669 | dna_connp->protocol = TCPIP;
|
---|
670 | dna_connp->error_ast = error_ast;
|
---|
671 | tcpip_code = tcpip_open_client(conn_id, server_node, server_task, port);
|
---|
672 | if( tcpip_failure(tcpip_code) )
|
---|
673 | {
|
---|
674 | #ifdef VMS
|
---|
675 | if(!strstr(server_node,"fidel"))
|
---|
676 | {
|
---|
677 | #endif
|
---|
678 | if(!find_pend_conn(server_node, server_task, port, src_type))
|
---|
679 | {
|
---|
680 | if(src_type == SRC_DIS)
|
---|
681 | strcpy(src_type_str,"Server");
|
---|
682 | else if(src_type == SRC_DIC)
|
---|
683 | strcpy(src_type_str,"Client");
|
---|
684 | else
|
---|
685 | strcpy(src_type_str,"Unknown type");
|
---|
686 | sprintf( str,"%s Connecting to %s on %s",
|
---|
687 | src_type_str, server_task, server_node );
|
---|
688 | if(!strcmp(server_task,"DIM_DNS"))
|
---|
689 | dna_report_error( conn_id, tcpip_code, str, DIM_ERROR, DIMDNSCNERR );
|
---|
690 | else
|
---|
691 | dna_report_error( conn_id, tcpip_code, str, DIM_ERROR, DIMTCPCNERR );
|
---|
692 | ins_pend_conn(server_node, server_task, port, src_type);
|
---|
693 | }
|
---|
694 | #ifdef VMS
|
---|
695 | }
|
---|
696 | #endif
|
---|
697 | tcpip_close(conn_id);
|
---|
698 | conn_free( conn_id );
|
---|
699 | return(0);
|
---|
700 | }
|
---|
701 | if( (id = find_pend_conn(server_node, server_task, port, src_type)) )
|
---|
702 | {
|
---|
703 | if(src_type == SRC_DIS)
|
---|
704 | strcpy(src_type_str,"Server");
|
---|
705 | else if(src_type == SRC_DIC)
|
---|
706 | strcpy(src_type_str,"Client");
|
---|
707 | else
|
---|
708 | strcpy(src_type_str,"Unknown type");
|
---|
709 | sprintf( str,"%s Connection established to", src_type_str);
|
---|
710 | if(!strcmp(server_task,"DIM_DNS"))
|
---|
711 | dna_report_error( conn_id, -1, str, DIM_INFO, DIMDNSCNEST );
|
---|
712 | else
|
---|
713 | dna_report_error( conn_id, -1, str, DIM_INFO, DIMTCPCNEST );
|
---|
714 | rel_pend_conn(id);
|
---|
715 | }
|
---|
716 | dna_connp->state = RD_HDR;
|
---|
717 | dna_connp->writing = FALSE;
|
---|
718 | dna_connp->buffer = (int *)malloc(TCP_RCV_BUF_SIZE);
|
---|
719 | /*
|
---|
720 | if(!dna_connp->buffer)
|
---|
721 | {
|
---|
722 | printf("Error in DNA - open_client malloc returned 0\n");
|
---|
723 | fflush(stdout);
|
---|
724 | }
|
---|
725 | */
|
---|
726 | dna_connp->buffer_size = TCP_RCV_BUF_SIZE;
|
---|
727 | dna_connp->read_ast = read_ast;
|
---|
728 | dna_connp->saw_init = TRUE; /* we send it! */
|
---|
729 | dna_start_read(conn_id, READ_HEADER_SIZE);
|
---|
730 | local_buffer.code = htovl(OPN_MAGIC);
|
---|
731 | get_node_name(local_buffer.node);
|
---|
732 | get_proc_name(local_buffer.task);
|
---|
733 | tcpip_code = dna_write_nowait(conn_id, &local_buffer, sizeof(local_buffer));
|
---|
734 | if (tcpip_failure(tcpip_code))
|
---|
735 | {
|
---|
736 | dna_close(conn_id);
|
---|
737 | return(0);
|
---|
738 | }
|
---|
739 | read_ast(conn_id, NULL, 0, STA_CONN);
|
---|
740 | return(conn_id);
|
---|
741 | }
|
---|
742 |
|
---|
743 | int dna_close(int conn_id)
|
---|
744 | {
|
---|
745 | if(conn_id > 0)
|
---|
746 | release_conn(conn_id);
|
---|
747 | return(1);
|
---|
748 | }
|
---|
749 |
|
---|
750 | /* connection managment routines */
|
---|
751 |
|
---|
752 | static void release_conn(int conn_id)
|
---|
753 | {
|
---|
754 | register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id] ;
|
---|
755 |
|
---|
756 | DISABLE_AST
|
---|
757 | if(dna_connp->busy)
|
---|
758 | {
|
---|
759 | tcpip_close(conn_id);
|
---|
760 | if(dna_connp->buffer)
|
---|
761 | {
|
---|
762 | free(dna_connp->buffer);
|
---|
763 | dna_connp->buffer = 0;
|
---|
764 | dna_connp->buffer_size = 0;
|
---|
765 | }
|
---|
766 | dna_connp->read_ast = NULL;
|
---|
767 | dna_connp->error_ast = NULL;
|
---|
768 | conn_free(conn_id);
|
---|
769 | }
|
---|
770 | ENABLE_AST
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 | void dna_report_error_old(int conn_id, int code, char *routine_name)
|
---|
775 | {
|
---|
776 | char str[128];
|
---|
777 | extern void tcpip_get_error(char *, int);
|
---|
778 | dim_print_date_time();
|
---|
779 | printf("%s", routine_name);
|
---|
780 | if(conn_id)
|
---|
781 | {
|
---|
782 | if(Net_conns[conn_id].node[0])
|
---|
783 | printf(" %s on node %s",
|
---|
784 | Net_conns[conn_id].task, Net_conns[conn_id].node);
|
---|
785 | /*
|
---|
786 | else
|
---|
787 | printf("\tConn %d :\n", conn_id);
|
---|
788 | */
|
---|
789 | }
|
---|
790 | if(code != -1)
|
---|
791 | {
|
---|
792 | /*
|
---|
793 | printf("\t");
|
---|
794 | tcpip_report_error(code);
|
---|
795 | */
|
---|
796 | tcpip_get_error(str, code);
|
---|
797 | printf(": %s\n",str);
|
---|
798 | }
|
---|
799 | fflush(stdout);
|
---|
800 | }
|
---|
801 |
|
---|
802 | void dna_report_error(int conn_id, int code, char *routine_name, int severity, int errcode)
|
---|
803 | {
|
---|
804 | char str[128], msg[1024];
|
---|
805 | extern void tcpip_get_error();
|
---|
806 |
|
---|
807 | sprintf(msg, "%s", routine_name);
|
---|
808 | if(conn_id)
|
---|
809 | {
|
---|
810 | if(Net_conns[conn_id].node[0])
|
---|
811 | {
|
---|
812 | sprintf(str," %s on node %s",
|
---|
813 | Net_conns[conn_id].task, Net_conns[conn_id].node);
|
---|
814 | strcat(msg, str);
|
---|
815 | }
|
---|
816 | }
|
---|
817 | if(code != -1)
|
---|
818 | {
|
---|
819 | tcpip_get_error(str, code);
|
---|
820 | strcat(msg,": ");
|
---|
821 | strcat(msg, str);
|
---|
822 | }
|
---|
823 | if(Dna_conns[conn_id].error_ast)
|
---|
824 | {
|
---|
825 | Dna_conns[conn_id].error_ast(conn_id, severity, errcode, msg);
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | static void save_node_task(int conn_id, DNA_NET *buffer)
|
---|
830 | {
|
---|
831 | strcpy(Net_conns[conn_id].node, buffer->node);
|
---|
832 | strcpy(Net_conns[conn_id].task, buffer->task);
|
---|
833 | }
|
---|
834 |
|
---|