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

Last change on this file since 14561 was 13135, checked in by tbretz, 13 years ago
Updated to v19r30
File size: 21.1 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);
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 = 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(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 = htovl(HDR_MAGIC);
469
470 memcpy(pktp->data, (char *)buffer, 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(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;
626
627 if(type == 0)
628 {
629 pending_connp = &Pending_conns[1];
630 size = MAX_CONNS;
631 oldest = 0;
632 }
633 else
634 {
635 pending_connp = &Pending_conns_tmout[1];
636 size = MAX_TMOUT_PENDING_CONNS;
637 oldest = time(NULL);
638 oldesti = 1;
639 }
640
641 for( i = 1; i < size; i++, pending_connp++ )
642 {
643 if( pending_connp->task_name[0] == '\0' )
644 {
645 strcpy(pending_connp->node_name, node);
646 strcpy(pending_connp->task_name, task);
647 pending_connp->port = port;
648 pending_connp->src_type = src_type;
649 pending_connp->last_used = last_used;
650 return(i);
651 }
652 else
653 {
654 if(pending_connp->last_used < oldest)
655 {
656 oldest = pending_connp->last_used;
657 oldesti = i;
658 }
659 }
660 }
661 if(type != 0)
662 {
663 pending_connp = &Pending_conns_tmout[oldesti];
664 strcpy(pending_connp->node_name, node);
665 strcpy(pending_connp->task_name, task);
666 pending_connp->port = port;
667 pending_connp->src_type = src_type;
668 pending_connp->last_used = last_used;
669 return(oldesti);
670 }
671 return(0);
672}
673
674static int find_pend_conn( char *node, char *task, int port, SRC_TYPES src_type, int type )
675{
676 register PENDING_OPEN *pending_connp;
677 register int i, size;
678 time_t curr_time;
679
680 if(type == 0)
681 {
682 pending_connp = &Pending_conns[1];
683 size = MAX_CONNS;
684 }
685 else
686 {
687 pending_connp = &Pending_conns_tmout[1];
688 size = MAX_TMOUT_PENDING_CONNS;
689 curr_time = time(NULL);
690 for( i = 1; i < size; i++, pending_connp++ )
691 {
692 if( pending_connp->task_name[0] != '\0' )
693 {
694 if( curr_time - pending_connp->last_used > TMOUT_PENDING_CONN_TMOUT )
695 {
696 pending_connp->task_name[0] = '\0';
697 }
698 }
699 }
700 pending_connp = &Pending_conns_tmout[1];
701 }
702 for( i = 1; i < size; i++, pending_connp++ )
703 {
704 if( (!strcmp(pending_connp->node_name, node)) &&
705 (!strcmp(pending_connp->task_name, task)) &&
706 (pending_connp->port == port) &&
707 (pending_connp->src_type == src_type))
708 {
709 return(i);
710 }
711 }
712 return(0);
713}
714
715
716static void rel_pend_conn( int id, int type )
717{
718 register PENDING_OPEN *pending_connp;
719
720 if(type == 0)
721 {
722 pending_connp = &Pending_conns[id];
723 }
724 else
725 {
726 pending_connp = &Pending_conns_tmout[id];
727 }
728 pending_connp->task_name[0] = '\0';
729}
730
731
732int dna_open_client(char *server_node, char *server_task, int port, int server_protocol,
733 void (*read_ast)(), void (*error_ast)(), SRC_TYPES src_type)
734{
735 register DNA_CONNECTION *dna_connp;
736 char str[256];
737 register int tcpip_code, conn_id, id;
738 DNA_NET local_buffer;
739 extern int get_proc_name(char *);
740 char src_type_str[64];
741
742 if(server_protocol){}
743 dna_init();
744/*
745 if(!DNA_Initialized) {
746 conn_arr_create(SRC_DNA);
747 DNA_Initialized = TRUE;
748 }
749*/
750 conn_id = conn_get();
751 dna_connp = &Dna_conns[conn_id] ;
752/*
753 if( !(conn_id = conn_get()) )
754 dim_panic("In dna_open_client: No more connections\n");
755*/
756 dna_connp->protocol = TCPIP;
757 dna_connp->error_ast = error_ast;
758 tcpip_code = tcpip_open_client(conn_id, server_node, server_task, port);
759 if( tcpip_failure(tcpip_code) )
760 {
761#ifdef VMS
762 if(!strstr(server_node,"fidel"))
763 {
764#endif
765 if(!find_pend_conn(server_node, server_task, port, src_type, 0))
766 {
767 if(src_type == SRC_DIS)
768 strcpy(src_type_str,"Server");
769 else if(src_type == SRC_DIC)
770 strcpy(src_type_str,"Client");
771 else
772 strcpy(src_type_str,"Unknown type");
773 sprintf( str,"%s Connecting to %s on %s",
774 src_type_str, server_task, server_node );
775 if(!strcmp(server_task,"DIM_DNS"))
776 dna_report_error( conn_id, tcpip_code, str, DIM_ERROR, DIMDNSCNERR );
777 else
778 dna_report_error( conn_id, tcpip_code, str, DIM_ERROR, DIMTCPCNERR );
779 ins_pend_conn(server_node, server_task, port, src_type, 0, 0);
780 }
781#ifdef VMS
782 }
783#endif
784 tcpip_close(conn_id);
785 conn_free( conn_id );
786 return(0);
787 }
788 if( (id = find_pend_conn(server_node, server_task, port, src_type, 0)) )
789 {
790 if(src_type == SRC_DIS)
791 strcpy(src_type_str,"Server");
792 else if(src_type == SRC_DIC)
793 strcpy(src_type_str,"Client");
794 else
795 strcpy(src_type_str,"Unknown type");
796 sprintf( str,"%s Connection established to", src_type_str);
797 if(!strcmp(server_task,"DIM_DNS"))
798 dna_report_error( conn_id, -1, str, DIM_INFO, DIMDNSCNEST );
799 else
800 dna_report_error( conn_id, -1, str, DIM_INFO, DIMTCPCNEST );
801 rel_pend_conn(id, 0);
802 }
803 dna_connp->state = RD_HDR;
804 dna_connp->writing = FALSE;
805 dna_connp->buffer = (int *)malloc(TCP_RCV_BUF_SIZE);
806/*
807 if(!dna_connp->buffer)
808 {
809 printf("Error in DNA - open_client malloc returned 0\n");
810 fflush(stdout);
811 }
812*/
813 dna_connp->buffer_size = TCP_RCV_BUF_SIZE;
814 dna_connp->read_ast = read_ast;
815 dna_connp->saw_init = TRUE; /* we send it! */
816 dna_start_read(conn_id, READ_HEADER_SIZE);
817 local_buffer.code = htovl(OPN_MAGIC);
818 get_node_name(local_buffer.node);
819 get_proc_name(local_buffer.task);
820 tcpip_code = dna_write_nowait(conn_id, &local_buffer, sizeof(local_buffer));
821 if (tcpip_failure(tcpip_code))
822 {
823 dim_print_date_time();
824 printf(" Client Establishing Connection: Couldn't write to Conn %3d : Server %s@%s\n",conn_id,
825 Net_conns[conn_id].task, Net_conns[conn_id].node);
826 fflush(stdout);
827 dna_close(conn_id);
828 return(0);
829 }
830 read_ast(conn_id, NULL, 0, STA_CONN);
831 return(conn_id);
832}
833
834int dna_close(int conn_id)
835{
836 if(conn_id > 0)
837 {
838 if(Net_conns[conn_id].write_timedout)
839 {
840 dna_report_error(conn_id, -1,
841 "Write timeout, disconnecting from", DIM_ERROR, DIMTCPWRTMO);
842 if(!find_pend_conn(Net_conns[conn_id].node, Net_conns[conn_id].task, 0, 0, 1))
843 ins_pend_conn(Net_conns[conn_id].node, Net_conns[conn_id].task, 0, 0, 1, time(NULL));
844 }
845 release_conn(conn_id);
846 }
847 return(1);
848}
849
850/* connection managment routines */
851
852static void release_conn(int conn_id)
853{
854 register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id] ;
855
856 DISABLE_AST
857 if(dna_connp->busy)
858 {
859 tcpip_close(conn_id);
860 if(dna_connp->buffer)
861 {
862 free(dna_connp->buffer);
863 dna_connp->buffer = 0;
864 dna_connp->buffer_size = 0;
865 }
866 dna_connp->read_ast = NULL;
867 dna_connp->error_ast = NULL;
868 conn_free(conn_id);
869 }
870 ENABLE_AST
871}
872
873
874void dna_report_error_old(int conn_id, int code, char *routine_name)
875{
876 char str[128];
877 extern void tcpip_get_error(char *, int);
878 dim_print_date_time();
879 printf("%s", routine_name);
880 if(conn_id)
881 {
882 if(Net_conns[conn_id].node[0])
883 printf(" %s on node %s",
884 Net_conns[conn_id].task, Net_conns[conn_id].node);
885/*
886 else
887 printf("\tConn %d :\n", conn_id);
888*/
889 }
890 if(code != -1)
891 {
892/*
893 printf("\t");
894 tcpip_report_error(code);
895*/
896 tcpip_get_error(str, code);
897 printf(": %s\n",str);
898 }
899 fflush(stdout);
900}
901
902void dna_report_error(int conn_id, int code, char *routine_name, int severity, int errcode)
903{
904 char str[128], msg[1024];
905 extern void tcpip_get_error();
906
907 sprintf(msg, "%s", routine_name);
908 if(conn_id)
909 {
910 if(Net_conns[conn_id].node[0])
911 {
912 sprintf(str," %s@%s",
913 Net_conns[conn_id].task, Net_conns[conn_id].node);
914 strcat(msg, str);
915 }
916 }
917 if(code != -1)
918 {
919 tcpip_get_error(str, code);
920 strcat(msg,": ");
921 strcat(msg, str);
922 }
923 if(Dna_conns[conn_id].error_ast)
924 {
925 Dna_conns[conn_id].error_ast(conn_id, severity, errcode, msg);
926 }
927}
928
929static void save_node_task(int conn_id, DNA_NET *buffer)
930{
931 int id;
932 strcpy(Net_conns[conn_id].node, buffer->node);
933 strcpy(Net_conns[conn_id].task, buffer->task);
934 if((id = find_pend_conn(Net_conns[conn_id].node, Net_conns[conn_id].task, 0, 0, 1)))
935 {
936 dna_report_error( conn_id, -1, "Re-connected to", DIM_INFO, DIMDNSCNEST );
937 rel_pend_conn(id, 1);
938 }
939}
Note: See TracBrowser for help on using the repository browser.