source: trunk/FACT++/dim/src/dna.c@ 19167

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