source: branches/testFACT++branch/dim/src/dna.c@ 19366

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