source: trunk/FACT++/dim_v19r15/src/dis.c@ 10305

Last change on this file since 10305 was 10183, checked in by tbretz, 14 years ago
New import.
File size: 69.5 KB
Line 
1/*
2 * DIS (Delphi Information Server) Package implements a library of
3 * routines to be used by servers.
4 *
5 * Started on : 10-11-91
6 * Last modification : 28-07-94
7 * Written by : C. Gaspar
8 * Adjusted by : G.C. Ballintijn
9 *
10 */
11
12#ifdef VMS
13# include <lnmdef.h>
14# include <ssdef.h>
15# include <descrip.h>
16# include <cfortran.h>
17#endif
18/*
19#define DEBUG
20*/
21#include <time.h>
22#ifdef VAX
23#include <timeb.h>
24#else
25#include <sys/timeb.h>
26#endif
27
28#define DIMLIB
29#include <dim.h>
30#include <dis.h>
31
32#define ALL 0
33#define MORE 1
34#define NONE 2
35
36typedef struct dis_dns_ent {
37 struct dis_dns_ent *next;
38 struct dis_dns_ent *prev;
39 long dnsid;
40 char task_name[MAX_NAME];
41 TIMR_ENT *dns_timr_ent;
42 DIS_DNS_PACKET dis_dns_packet;
43 int dis_n_services;
44 int dns_dis_conn_id;
45 int dis_first_time;
46 int serving;
47 unsigned int dis_service_id;
48 unsigned int dis_client_id;
49 int updating_service_list;
50} DIS_DNS_CONN;
51
52typedef struct req_ent {
53 struct req_ent *next;
54 struct req_ent *prev;
55 int conn_id;
56 int service_id;
57 int req_id;
58 int type;
59 struct serv *service_ptr;
60 int timeout;
61 int format;
62 int first_time;
63 int delay_delete;
64 int to_delete;
65 TIMR_ENT *timr_ent;
66 struct reqp_ent *reqpp;
67} REQUEST;
68
69typedef struct serv {
70 struct serv *next;
71 struct serv *prev;
72 char name[MAX_NAME];
73 int id;
74 int type;
75 char def[MAX_NAME];
76 FORMAT_STR format_data[MAX_NAME/4];
77 int *address;
78 int size;
79 void (*user_routine)();
80 long tag;
81 int registered;
82 int quality;
83 int user_secs;
84 int user_millisecs;
85 int tid;
86 REQUEST *request_head;
87 DIS_DNS_CONN *dnsp;
88 int delay_delete;
89 int to_delete;
90} SERVICE;
91
92typedef struct reqp_ent {
93 struct reqp_ent *next;
94 struct reqp_ent *prev;
95 REQUEST *reqp;
96} REQUEST_PTR;
97
98typedef struct cli_ent {
99 struct cli_ent *next;
100 struct cli_ent *prev;
101 int conn_id;
102 REQUEST_PTR *requestp_head;
103 DIS_DNS_CONN *dnsp;
104} CLIENT;
105
106static CLIENT *Client_head = (CLIENT *)0;
107
108static DIS_DNS_CONN *DNS_head = (DIS_DNS_CONN *)0;
109
110/*
111static char Task_name[MAX_NAME];
112static TIMR_ENT *Dns_timr_ent = (TIMR_ENT *)0;
113static DIS_DNS_PACKET Dis_dns_packet = {0, 0, {0}};
114static int Dis_n_services = 0;
115*/
116static int Dis_first_time = 1;
117/*
118static int Dns_dis_conn_id = 0;
119*/
120static int Protocol;
121static int Port_number;
122static int Dis_conn_id = 0;
123static int Curr_conn_id = 0;
124static int Serving = 0;
125static void (*Client_exit_user_routine)() = 0;
126static void (*Exit_user_routine)() = 0;
127static void (*Error_user_routine)() = 0;
128static int Error_conn_id = 0;
129DIS_DNS_CONN *Default_DNS = 0;
130
131typedef struct exit_ent {
132 struct exit_ent *next;
133 int conn_id;
134 int exit_id;
135} EXIT_H;
136
137static EXIT_H *Exit_h_head = (EXIT_H *)0;
138
139/* Do not forget to increase when this file is modified */
140static int Version_number = DIM_VERSION_NUMBER;
141static int Dis_timer_q = 0;
142static int Threads_off = 0;
143/*
144static unsigned int Dis_service_id, Dis_client_id;
145static int Updating_service_list = 0;
146*/
147static int Last_client;
148
149#ifdef DEBUG
150static int Debug_on = 1;
151#else
152static int Debug_on = 0;
153#endif
154
155_DIM_PROTO( static void dis_insert_request, (int conn_id, DIC_PACKET *dic_packet,
156 int size, int status ) );
157_DIM_PROTO( int execute_service, (int req_id) );
158_DIM_PROTO( void execute_command, (SERVICE *servp, DIC_PACKET *packet) );
159_DIM_PROTO( void register_dns_services, (int flag) );
160_DIM_PROTO( void register_services, (DIS_DNS_CONN *dnsp, int flag, int dns_flag) );
161_DIM_PROTO( void std_cmnd_handler, (long *tag, int *cmnd_buff, int *size) );
162_DIM_PROTO( void client_info, (long *tag, int **bufp, int *size) );
163_DIM_PROTO( void service_info, (long *tag, int **bufp, int *size) );
164_DIM_PROTO( void add_exit_handler, (int *tag, int *bufp, int *size) );
165_DIM_PROTO( static void exit_handler, (int *tag, int *bufp, int *size) );
166_DIM_PROTO( static void error_handler, (int conn_id, int severity, int errcode, char *reason) );
167_DIM_PROTO( SERVICE *find_service, (char *name) );
168_DIM_PROTO( CLIENT *find_client, (int conn_id) );
169_DIM_PROTO( static int get_format_data, (FORMAT_STR *format_data, char *def) );
170_DIM_PROTO( static int release_conn, (int conn_id, int print_flag, int dns_flag) );
171_DIM_PROTO( SERVICE *dis_hash_service_exists, (char *name) );
172_DIM_PROTO( SERVICE *dis_hash_service_get_next, (int *start, SERVICE *prev, int flag) );
173_DIM_PROTO( static unsigned do_dis_add_service_dns, (char *name, char *type, void *address, int size,
174 void (*user_routine)(), long tag, long dnsid ) );
175_DIM_PROTO( static DIS_DNS_CONN *create_dns, (long dnsid) );
176
177void dis_set_debug_on()
178{
179 Debug_on = 1;
180}
181
182void dis_set_debug_off()
183{
184 Debug_on = 0;
185}
186
187void dis_no_threads()
188{
189 Threads_off = 1;
190}
191
192static DIS_STAMPED_PACKET *Dis_packet = 0;
193static int Dis_packet_size = 0;
194
195int dis_set_buffer_size(int size)
196{
197 if(Dis_packet_size)
198 free(Dis_packet);
199 Dis_packet = (DIS_STAMPED_PACKET *)malloc(DIS_STAMPED_HEADER + size);
200 if(Dis_packet)
201 {
202 Dis_packet_size = DIS_STAMPED_HEADER + size;
203 return(1);
204 }
205 else
206 return(0);
207}
208
209static int check_service_name(char *name)
210{
211 if(strlen(name) > (MAX_NAME - 1))
212 return(0);
213 return(1);
214}
215
216static void dis_init()
217{
218 int dis_hash_service_init();
219 void dis_dns_init();
220
221 dis_dns_init();
222 {
223 DISABLE_AST
224 dis_hash_service_init();
225 ENABLE_AST
226 }
227}
228
229static unsigned do_dis_add_service_dns( char *name, char *type, void *address, int size,
230 void (*user_routine)(), long tag, long dnsid )
231{
232 register SERVICE *new_serv;
233 register int service_id;
234 char str[512];
235 int dis_hash_service_insert();
236 DIS_DNS_CONN *dnsp;
237 extern DIS_DNS_CONN *dis_find_dns(long);
238
239 dis_init();
240 {
241 DISABLE_AST
242 if(!check_service_name(name))
243 {
244 strcpy(str,"Service name too long: ");
245 strcat(str,name);
246 error_handler(0, DIM_ERROR, DIMSVCTOOLG, str);
247 ENABLE_AST
248 return((unsigned) 0);
249 }
250 if( find_service(name) )
251 {
252 strcpy(str,"Duplicate Service: ");
253 strcat(str,name);
254 error_handler(0, DIM_ERROR, DIMSVCDUPLC, str);
255 ENABLE_AST
256 return((unsigned) 0);
257 }
258 new_serv = (SERVICE *)malloc( sizeof(SERVICE) );
259 strncpy( new_serv->name, name, MAX_NAME );
260 if(type != (char *)0)
261 {
262 if (strlen(type) >= MAX_NAME)
263 {
264 strcpy(str,"Format String Too Long: ");
265 strcat(str,name);
266 error_handler(0, DIM_ERROR, DIMSVCFORMT, str);
267 free(new_serv);
268 ENABLE_AST
269 return((unsigned) 0);
270 }
271 if (! get_format_data(new_serv->format_data, type))
272 {
273 strcpy(str,"Bad Format String: ");
274 strcat(str,name);
275 error_handler(0, DIM_ERROR, DIMSVCFORMT, str);
276 free(new_serv);
277 ENABLE_AST
278 return((unsigned) 0);
279 }
280 strcpy(new_serv->def,type);
281 }
282 else
283 {
284 new_serv->format_data[0].par_bytes = 0;
285 new_serv->def[0] = '\0';
286 }
287 new_serv->type = 0;
288 new_serv->address = (int *)address;
289 new_serv->size = size;
290 new_serv->user_routine = user_routine;
291 new_serv->tag = tag;
292 new_serv->registered = 0;
293 new_serv->quality = 0;
294 new_serv->user_secs = 0;
295 new_serv->tid = 0;
296 new_serv->delay_delete = 0;
297 new_serv->to_delete = 0;
298 dnsp = dis_find_dns(dnsid);
299 if(!dnsp)
300 dnsp = create_dns(dnsid);
301 new_serv->dnsp = dnsp;
302 service_id = id_get((void *)new_serv, SRC_DIS);
303 new_serv->id = service_id;
304 new_serv->request_head = (REQUEST *)malloc(sizeof(REQUEST));
305 dll_init( (DLL *) (new_serv->request_head) );
306 dis_hash_service_insert(new_serv);
307/*
308 Dis_n_services++;
309*/
310 dnsp->dis_n_services++;
311 ENABLE_AST
312 }
313 return((unsigned)service_id);
314}
315
316static unsigned do_dis_add_service( char *name, char *type, void *address, int size,
317 void (*user_routine)(), long tag )
318{
319 return do_dis_add_service_dns( name, type, address, size,
320 user_routine, tag, 0 );
321}
322
323#ifdef VxWorks
324void dis_destroy(int tid)
325{
326register SERVICE *servp, *prevp;
327int n_left = 0;
328
329 prevp = 0;
330 while( servp = dis_hash_service_get_next(prevp))
331 {
332 if(servp->tid == tid)
333 {
334 dis_remove_service(servp->id);
335 }
336 else
337 {
338 prevp = servp;
339 n_left++;
340 }
341 }
342 if(n_left == 5)
343 {
344 prevp = 0;
345 while( servp = dis_hash_service_get_next(prevp))
346 {
347 dis_remove_service(servp->id);
348 }
349 dna_close(Dis_conn_id);
350 dna_close(Dns_dis_conn_id);
351 Dns_dis_conn_id = 0;
352 Dis_first_time = 1;
353 dtq_rem_entry(Dis_timer_q, Dns_timr_ent);
354 Dns_timr_ent = NULL;
355 }
356}
357
358
359#endif
360
361unsigned dis_add_service( char *name, char *type, void *address, int size,
362 void (*user_routine)(), long tag)
363{
364 unsigned ret;
365#ifdef VxWorks
366 register SERVICE *servp;
367#endif
368/*
369 DISABLE_AST
370*/
371 ret = do_dis_add_service( name, type, address, size, user_routine, tag);
372#ifdef VxWorks
373 servp = (SERVICE *)id_get_ptr(ret, SRC_DIS);
374 servp->tid = taskIdSelf();
375#endif
376/*
377 ENABLE_AST
378*/
379 return(ret);
380}
381
382unsigned dis_add_service_dns( long dnsid, char *name, char *type, void *address, int size,
383 void (*user_routine)(), long tag)
384{
385 unsigned ret;
386#ifdef VxWorks
387 register SERVICE *servp;
388#endif
389/*
390 DISABLE_AST
391*/
392 ret = do_dis_add_service_dns( name, type, address, size, user_routine, tag, dnsid);
393#ifdef VxWorks
394 servp = (SERVICE *)id_get_ptr(ret, SRC_DIS);
395 servp->tid = taskIdSelf();
396#endif
397/*
398 ENABLE_AST
399*/
400 return(ret);
401}
402
403static unsigned do_dis_add_cmnd_dns( char *name, char *type, void (*user_routine)(), long tag, long dnsid )
404{
405 register SERVICE *new_serv;
406 register int service_id;
407 char str[512];
408 int dis_hash_service_insert();
409 DIS_DNS_CONN *dnsp;
410 extern DIS_DNS_CONN *dis_find_dns(long);
411
412 dis_init();
413 {
414 DISABLE_AST
415 if(!check_service_name(name))
416 {
417 strcpy(str,"Command name too long: ");
418 strcat(str,name);
419 error_handler(0, DIM_ERROR, DIMSVCTOOLG, str);
420 ENABLE_AST
421 return((unsigned) 0);
422 }
423 if( find_service(name) )
424 {
425 ENABLE_AST
426 return((unsigned) 0);
427 }
428 new_serv = (SERVICE *)malloc(sizeof(SERVICE));
429 strncpy(new_serv->name, name, MAX_NAME);
430 if(type != (char *)0)
431 {
432 if( !get_format_data(new_serv->format_data, type))
433 {
434 ENABLE_AST
435 return((unsigned) 0);
436 }
437 strcpy(new_serv->def,type);
438 }
439 else
440 {
441 new_serv->format_data[0].par_bytes = 0;
442 new_serv->def[0] = '\0';
443 }
444 new_serv->type = COMMAND;
445 new_serv->address = 0;
446 new_serv->size = 0;
447 if(user_routine)
448 new_serv->user_routine = user_routine;
449 else
450 new_serv->user_routine = std_cmnd_handler;
451 new_serv->tag = tag;
452 new_serv->tid = 0;
453 new_serv->registered = 0;
454 new_serv->quality = 0;
455 new_serv->user_secs = 0;
456 new_serv->delay_delete = 0;
457 new_serv->to_delete = 0;
458 service_id = id_get((void *)new_serv, SRC_DIS);
459 new_serv->id = service_id;
460 dnsp = dis_find_dns(dnsid);
461 if(!dnsp)
462 dnsp = create_dns(dnsid);
463 new_serv->dnsp = dnsp;
464 new_serv->request_head = (REQUEST *)malloc(sizeof(REQUEST));
465 dll_init( (DLL *) (new_serv->request_head) );
466 dis_hash_service_insert(new_serv);
467/*
468 Dis_n_services++;
469*/
470 dnsp->dis_n_services++;
471 ENABLE_AST
472 }
473 return((unsigned) service_id);
474}
475
476static unsigned do_dis_add_cmnd( char *name, char *type, void (*user_routine)(), long tag)
477{
478 return do_dis_add_cmnd_dns(name, type, user_routine, tag, 0);
479}
480
481unsigned dis_add_cmnd( char *name, char *type, void (*user_routine)(), long tag )
482{
483 unsigned ret;
484
485/*
486 DISABLE_AST
487*/
488 ret = do_dis_add_cmnd( name, type, user_routine, tag );
489/*
490 ENABLE_AST
491*/
492 return(ret);
493}
494
495unsigned dis_add_cmnd_dns( long dnsid, char *name, char *type, void (*user_routine)(), long tag )
496{
497 unsigned ret;
498
499 /*
500 DISABLE_AST
501 */
502 ret = do_dis_add_cmnd_dns( name, type, user_routine, tag, dnsid );
503 /*
504 ENABLE_AST
505 */
506 return(ret);
507}
508
509void dis_add_client_exit_handler( void (*user_routine)())
510{
511
512 DISABLE_AST
513 Client_exit_user_routine = user_routine;
514 ENABLE_AST
515}
516
517void dis_add_exit_handler( void (*user_routine)())
518{
519
520 DISABLE_AST
521 Exit_user_routine = user_routine;
522 ENABLE_AST
523}
524
525void dis_add_error_handler( void (*user_routine)())
526{
527
528 DISABLE_AST
529 Error_user_routine = user_routine;
530 ENABLE_AST
531}
532
533static int get_format_data(FORMAT_STR *format_data, char *def)
534{
535 register char code, last_code = 0;
536 int num;
537
538 code = *def;
539 while(*def)
540 {
541 if(code != last_code)
542 {
543 format_data->par_num = 0;
544 format_data->flags = 0;
545 switch(code)
546 {
547 case 'i':
548 case 'I':
549 case 'l':
550 case 'L':
551 format_data->par_bytes = SIZEOF_LONG;
552 format_data->flags |= SWAPL;
553 break;
554 case 'x':
555 case 'X':
556 format_data->par_bytes = SIZEOF_DOUBLE;
557 format_data->flags |= SWAPD;
558 break;
559 case 's':
560 case 'S':
561 format_data->par_bytes = SIZEOF_SHORT;
562 format_data->flags |= SWAPS;
563 break;
564 case 'f':
565 case 'F':
566 format_data->par_bytes = SIZEOF_FLOAT;
567 format_data->flags |= SWAPL;
568#ifdef vms
569 format_data->flags |= IT_IS_FLOAT;
570#endif
571 break;
572 case 'd':
573 case 'D':
574 format_data->par_bytes = SIZEOF_DOUBLE;
575 format_data->flags |= SWAPD;
576#ifdef vms
577 format_data->flags |= IT_IS_FLOAT;
578#endif
579 break;
580 case 'c':
581 case 'C':
582 format_data->par_bytes = SIZEOF_CHAR;
583 format_data->flags |= NOSWAP;
584 break;
585 }
586 }
587 def++;
588 if(*def != ':')
589 {
590 if(*def)
591 {
592/*
593 printf("Bad service definition parsing\n");
594 fflush(stdout);
595
596 error_handler("Bad service definition parsing",2);
597*/
598 return(0);
599 }
600 else
601 format_data->par_num = 0;
602 }
603 else
604 {
605 def++;
606 sscanf(def,"%d",&num);
607 format_data->par_num += num;
608 while((*def != ';') && (*def != '\0'))
609 def++;
610 if(*def)
611 def++;
612 }
613 last_code = code;
614 code = *def;
615 if(code != last_code)
616 format_data++;
617 }
618 format_data->par_bytes = 0;
619 return(1);
620}
621
622void recv_dns_dis_rout( int conn_id, DNS_DIS_PACKET *packet, int size, int status )
623{
624 char str[128];
625 int dns_timr_time;
626 extern int rand_tmout(int, int);
627 extern int open_dns(long, void (*)(), void (*)(), int, int, int);
628 extern DIS_DNS_CONN *find_dns_by_conn_id(int);
629 extern void do_register_services(DIS_DNS_CONN *);
630 extern void do_dis_stop_serving_dns(DIS_DNS_CONN *);
631 DIS_DNS_CONN *dnsp;
632
633 if(size){}
634 dnsp = find_dns_by_conn_id(conn_id);
635 if(!dnsp)
636 {
637 return;
638 }
639 switch(status)
640 {
641 case STA_DISC: /* connection broken */
642 if( dnsp->dns_timr_ent ) {
643 dtq_rem_entry( Dis_timer_q, dnsp->dns_timr_ent );
644 dnsp->dns_timr_ent = NULL;
645 }
646
647 if(dnsp->dns_dis_conn_id > 0)
648 dna_close(dnsp->dns_dis_conn_id);
649 if(dnsp->serving)
650 {
651 dnsp->dns_dis_conn_id = open_dns(dnsp->dnsid, recv_dns_dis_rout, error_handler,
652 DIS_DNS_TMOUT_MIN, DIS_DNS_TMOUT_MAX, SRC_DIS );
653 if(dnsp->dns_dis_conn_id == -2)
654 error_handler(0, DIM_FATAL, DIMDNSUNDEF, "DIM_DNS_NODE undefined");
655 }
656 break;
657 case STA_CONN: /* connection received */
658 if(dnsp->serving)
659 {
660 dnsp->dns_dis_conn_id = conn_id;
661 register_services(dnsp, ALL, 0);
662 dns_timr_time = rand_tmout(WATCHDOG_TMOUT_MIN,
663 WATCHDOG_TMOUT_MAX);
664 dnsp->dns_timr_ent = dtq_add_entry( Dis_timer_q,
665 dns_timr_time,
666 do_register_services, dnsp );
667 }
668 else
669 {
670 dna_close(conn_id);
671 }
672 break;
673 default : /* normal packet */
674 if(vtohl(packet->size) != DNS_DIS_HEADER)
675 break;
676 switch( vtohl(packet->type) )
677 {
678 case DNS_DIS_REGISTER :
679 sprintf(str,
680 "%s: Watchdog Timeout, DNS requests registration",
681 dnsp->task_name);
682 error_handler(0, DIM_WARNING, DIMDNSTMOUT, str);
683 register_services(dnsp, ALL, 0);
684 break;
685 case DNS_DIS_KILL :
686 sprintf(str,
687 "%s: Some Services already known to DNS",
688 dnsp->task_name);
689 /*
690 exit(2);
691 */
692 error_handler(0, DIM_FATAL, DIMDNSDUPLC, str);
693 do_dis_stop_serving_dns(dnsp);
694 dis_stop_serving();
695/*
696 exit_tag = 0;
697 exit_code = 2;
698 exit_size = sizeof(int);
699 exit_handler(&exit_tag, &exit_code, &exit_size);
700*/
701 break;
702 case DNS_DIS_STOP :
703 sprintf(str,
704 "%s: DNS refuses connection",dnsp->task_name);
705/*
706 exit(2);
707*/
708 error_handler(0, DIM_FATAL, DIMDNSREFUS, str);
709 do_dis_stop_serving_dns(dnsp);
710 dis_stop_serving();
711/*
712 exit_tag = 0;
713 exit_code = 2;
714 exit_size = sizeof(int);
715 exit_handler(&exit_tag, &exit_code, &exit_size);
716*/
717 break;
718 case DNS_DIS_EXIT :
719 sprintf(str,
720 "%s: DNS requests Exit",dnsp->task_name);
721 error_handler(0, DIM_FATAL, DIMDNSEXIT, str);
722 break;
723 }
724 break;
725 }
726}
727
728
729/* register services within the name server
730 *
731 * Send services uses the DNA package. services is a linked list of services
732 * stored by add_service.
733 */
734
735int send_dns_update_packet(DIS_DNS_CONN *dnsp)
736{
737 DIS_DNS_PACKET *dis_dns_p = &(dnsp->dis_dns_packet);
738 int n_services;
739 SERVICE_REG *serv_regp;
740
741 n_services = 1;
742 dis_dns_p->n_services = htovl(n_services);
743 dis_dns_p->size = htovl(DIS_DNS_HEADER +
744 n_services * sizeof(SERVICE_REG));
745 serv_regp = dis_dns_p->services;
746 strcpy( serv_regp->service_name, "DUMMY_UPDATE_PACKET" );
747 if(dnsp->dns_dis_conn_id > 0)
748 {
749if(Debug_on)
750{
751dim_print_date_time();
752 printf("Sending UpdatePacket to dns %d as %s@%s, %d services\n",
753 dnsp->dns_dis_conn_id,
754 (&(dnsp->dis_dns_packet))->task_name, (&(dnsp->dis_dns_packet))->node_name, n_services);
755}
756 if( !dna_write(dnsp->dns_dis_conn_id, &(dnsp->dis_dns_packet),
757 DIS_DNS_HEADER + n_services * sizeof(SERVICE_REG)))
758 {
759 release_conn(dnsp->dns_dis_conn_id, 0, 1);
760 }
761 }
762 return(1);
763}
764
765void do_register_services(DIS_DNS_CONN *dnsp)
766{
767 register_services(dnsp, NONE, 0);
768}
769
770void register_services(DIS_DNS_CONN *dnsp, int flag, int dns_flag)
771{
772 register DIS_DNS_PACKET *dis_dns_p = &(dnsp->dis_dns_packet);
773 register int n_services, tot_n_services;
774 register SERVICE *servp;
775 register SERVICE_REG *serv_regp;
776 int hash_index, new_entries;
777 extern int get_node_addr();
778 int dis_hash_service_registered();
779
780 if(!dis_dns_p->src_type)
781 {
782 get_node_name( dis_dns_p->node_name );
783/*
784 strcpy( dis_dns_p->task_name, Task_name );
785*/
786 strncpy( dis_dns_p->task_name, dnsp->task_name,
787 MAX_TASK_NAME-4 );
788 dis_dns_p->task_name[MAX_TASK_NAME-4-1] = '\0';
789 get_node_addr( dis_dns_p->node_addr );
790/*
791 dis_dns_p->port = htovl(Port_number);
792*/
793 dis_dns_p->pid = htovl(getpid());
794 dis_dns_p->protocol = htovl(Protocol);
795 dis_dns_p->src_type = htovl(SRC_DIS);
796 dis_dns_p->format = htovl(MY_FORMAT);
797if(Debug_on)
798{
799dim_print_date_time();
800 printf("Registering as %d %s@%s\n",
801 dis_dns_p->pid, dis_dns_p->task_name, dis_dns_p->node_name);
802}
803
804 }
805
806 dis_dns_p->port = htovl(Port_number);
807 serv_regp = dis_dns_p->services;
808 n_services = 0;
809 tot_n_services = 0;
810 if( flag == NONE ) {
811 dis_dns_p->n_services = htovl(n_services);
812 dis_dns_p->size = htovl( DIS_DNS_HEADER +
813 (n_services*sizeof(SERVICE_REG)));
814 if(dnsp->dns_dis_conn_id > 0)
815 {
816if(Debug_on)
817{
818dim_print_date_time();
819 printf("Sending NONE to dns %d as %s@%s, %d services\n",
820 dnsp->dns_dis_conn_id,
821 (&(dnsp->dis_dns_packet))->task_name, (&(dnsp->dis_dns_packet))->node_name, n_services);
822}
823 if(!dna_write(dnsp->dns_dis_conn_id, &(dnsp->dis_dns_packet),
824 DIS_DNS_HEADER + n_services*sizeof(SERVICE_REG)))
825 {
826 release_conn(dnsp->dns_dis_conn_id, 0, 1);
827 }
828 }
829 return;
830 }
831 if(flag == ALL)
832 {
833 servp = 0;
834 hash_index = -1;
835 while( (servp = dis_hash_service_get_next(&hash_index, servp, 0)))
836 {
837 if(servp->dnsp == dnsp)
838 servp->registered = 0;
839 }
840 }
841 servp = 0;
842 hash_index = -1;
843 new_entries = 0;
844 if(flag == MORE)
845 new_entries = 1;
846 while( (servp = dis_hash_service_get_next(&hash_index, servp, new_entries)))
847 {
848 if( flag == MORE )
849 {
850 if( servp->registered )
851 {
852 continue;
853 }
854 }
855
856 if(servp->dnsp != dnsp)
857 continue;
858
859if(Debug_on)
860{
861dim_print_date_time();
862 printf("Registering %s\n",
863 servp->name);
864}
865 strcpy( serv_regp->service_name, servp->name );
866 strcpy( serv_regp->service_def, servp->def );
867 if(servp->type == COMMAND)
868 serv_regp->service_id = htovl( servp->id | 0x10000000);
869 else
870 serv_regp->service_id = htovl( servp->id );
871
872 serv_regp++;
873 n_services++;
874 dis_hash_service_registered(hash_index, servp);
875 if( n_services == MAX_SERVICE_UNIT )
876 {
877 dis_dns_p->n_services = htovl(n_services);
878 dis_dns_p->size = htovl(DIS_DNS_HEADER +
879 n_services * sizeof(SERVICE_REG));
880 if(dnsp->dns_dis_conn_id > 0)
881 {
882if(Debug_on)
883{
884dim_print_date_time();
885 printf("Sending MAX_SERVICE_UNIT to dns %d as %s@%s, %d services\n",
886 dnsp->dns_dis_conn_id,
887 (&(dnsp->dis_dns_packet))->task_name, (&(dnsp->dis_dns_packet))->node_name, n_services);
888}
889 if( !dna_write(dnsp->dns_dis_conn_id,
890 &(dnsp->dis_dns_packet),
891 DIS_DNS_HEADER + n_services *
892 sizeof(SERVICE_REG)) )
893 {
894 release_conn(dnsp->dns_dis_conn_id, 0, 1);
895 }
896 }
897 serv_regp = dis_dns_p->services;
898 tot_n_services += MAX_SERVICE_UNIT;
899 n_services = 0;
900 continue;
901 }
902 }
903 if( n_services )
904 {
905 dis_dns_p->n_services = htovl(n_services);
906 dis_dns_p->size = htovl(DIS_DNS_HEADER +
907 n_services * sizeof(SERVICE_REG));
908 if(dnsp->dns_dis_conn_id > 0)
909 {
910if(Debug_on)
911{
912dim_print_date_time();
913 printf("Sending to dns %d as %s@%s, %d services\n",
914 dnsp->dns_dis_conn_id,
915 (&(dnsp->dis_dns_packet))->task_name, (&(dnsp->dis_dns_packet))->node_name, n_services);
916}
917 if( !dna_write(dnsp->dns_dis_conn_id, &(dnsp->dis_dns_packet),
918 DIS_DNS_HEADER + n_services * sizeof(SERVICE_REG)))
919 {
920 release_conn(dnsp->dns_dis_conn_id, 0, 1);
921 }
922
923 }
924 tot_n_services += n_services;
925 }
926 if(!dns_flag)
927 {
928 if(tot_n_services >= MAX_REGISTRATION_UNIT)
929 {
930 send_dns_update_packet(dnsp);
931 }
932 }
933}
934
935void unregister_service(DIS_DNS_CONN *dnsp, SERVICE *servp)
936{
937 register DIS_DNS_PACKET *dis_dns_p = &(dnsp->dis_dns_packet);
938 register int n_services;
939 register SERVICE_REG *serv_regp;
940 extern int get_node_addr();
941
942 if(dnsp->dns_dis_conn_id > 0)
943 {
944 if(!dis_dns_p->src_type)
945 {
946 get_node_name( dis_dns_p->node_name );
947/*
948 strcpy( dis_dns_p->task_name, Task_name );
949*/
950 strncpy( dis_dns_p->task_name, dnsp->task_name,
951 MAX_TASK_NAME-4 );
952 dis_dns_p->task_name[MAX_TASK_NAME-4-1] = '\0';
953 get_node_addr( dis_dns_p->node_addr );
954 dis_dns_p->port = htovl(Port_number);
955 dis_dns_p->protocol = htovl(Protocol);
956 dis_dns_p->src_type = htovl(SRC_DIS);
957 dis_dns_p->format = htovl(MY_FORMAT);
958 }
959 serv_regp = dis_dns_p->services;
960 strcpy( serv_regp->service_name, servp->name );
961 strcpy( serv_regp->service_def, servp->def );
962 serv_regp->service_id = htovl( servp->id | 0x80000000);
963 serv_regp++;
964 n_services = 1;
965 servp->registered = 0;
966 dis_dns_p->n_services = htovl(n_services);
967 dis_dns_p->size = htovl(DIS_DNS_HEADER +
968 n_services * sizeof(SERVICE_REG));
969
970if(Debug_on)
971{
972dim_print_date_time();
973 printf("Sending UNREGISTER to dns %d as %s@%s, %d services\n",
974 dnsp->dns_dis_conn_id,
975 (&(dnsp->dis_dns_packet))->task_name, (&(dnsp->dis_dns_packet))->node_name, n_services);
976}
977 if( !dna_write(dnsp->dns_dis_conn_id, &(dnsp->dis_dns_packet),
978 DIS_DNS_HEADER + n_services * sizeof(SERVICE_REG)) )
979 {
980 release_conn(dnsp->dns_dis_conn_id, 0, 1);
981 }
982 if(dnsp->dis_service_id)
983 dis_update_service(dnsp->dis_service_id);
984 }
985}
986
987void do_update_service_list(DIS_DNS_CONN *dnsp)
988{
989 dnsp->updating_service_list = 0;
990 dis_update_service(dnsp->dis_service_id);
991}
992
993/* start serving client requests
994 *
995 * Using the DNA package start accepting requests from clients.
996 * When a request arrives the routine "dis_insert_request" will be executed.
997 */
998
999int dis_start_serving(char *task)
1000{
1001 return dis_start_serving_dns(0, task);
1002}
1003
1004static DIS_DNS_CONN *create_dns(long dnsid)
1005{
1006 DIS_DNS_CONN *dnsp;
1007
1008 dnsp = malloc(sizeof(DIS_DNS_CONN));
1009 dnsp->dns_timr_ent = (TIMR_ENT *)0;
1010 dnsp->dis_n_services = 0;
1011 dnsp->dns_dis_conn_id = 0;
1012 dnsp->dis_first_time = 1;
1013 dnsp->serving = 0;
1014 dnsp->dis_dns_packet.size = 0;
1015 dnsp->dis_dns_packet.src_type = 0;
1016 dnsp->dis_dns_packet.node_name[0] = 0;
1017 dnsp->updating_service_list = 0;
1018 dnsp->dnsid = dnsid;
1019 dll_insert_queue( (DLL *) DNS_head, (DLL *) dnsp );
1020 return dnsp;
1021}
1022
1023void dis_dns_init()
1024{
1025 static int done = 0;
1026 DIS_DNS_CONN *dnsp;
1027 void dim_init_threads(void);
1028
1029 if(!done)
1030 {
1031 if(!Threads_off)
1032 {
1033 dim_init_threads();
1034 }
1035 {
1036 DISABLE_AST
1037 if(!DNS_head)
1038 {
1039 DNS_head = (DIS_DNS_CONN *)malloc(sizeof(DIS_DNS_CONN));
1040 dll_init( (DLL *) DNS_head );
1041 }
1042 dnsp = create_dns(0);
1043 Default_DNS = dnsp;
1044 done = 1;
1045 ENABLE_AST
1046 }
1047 }
1048}
1049
1050int dis_start_serving_dns(long dnsid, char *task/*, int *idlist*/)
1051{
1052 char str0[MAX_NAME], str1[MAX_NAME],str2[MAX_NAME],
1053 str3[MAX_NAME],str4[MAX_NAME];
1054 char task_name_aux[MAX_TASK_NAME];
1055 extern int open_dns();
1056 extern DIS_DNS_CONN *dis_find_dns(long);
1057 DIS_DNS_CONN *dnsp;
1058 int more_ids[10] = {0};
1059
1060 dis_init();
1061 {
1062 DISABLE_AST
1063 /*
1064#ifdef VxWorks
1065 taskDeleteHookAdd(remove_all_services);
1066 printf("Adding delete hook\n");
1067#endif
1068*/
1069
1070 if(!Client_head)
1071 {
1072 Client_head = (CLIENT *)malloc(sizeof(CLIENT));
1073 dll_init( (DLL *) Client_head );
1074 }
1075 if(dnsid == 0)
1076 {
1077 dnsp = Default_DNS;
1078 }
1079 else if(!(dnsp = dis_find_dns(dnsid)))
1080 {
1081 dnsp = create_dns(dnsid);
1082 }
1083 dnsp->serving = 1;
1084 Serving = 1;
1085 if(Dis_first_time)
1086 {
1087 strncpy( task_name_aux, task, MAX_TASK_NAME );
1088 task_name_aux[MAX_TASK_NAME-1] = '\0';
1089 Port_number = SEEK_PORT;
1090if(Debug_on)
1091{
1092dim_print_date_time();
1093 printf("Opening Server Connection %s\n",task_name_aux);
1094}
1095 if( !(Dis_conn_id = dna_open_server( task_name_aux, dis_insert_request,
1096 &Protocol, &Port_number, error_handler) ))
1097 {
1098 ENABLE_AST
1099 return(0);
1100 }
1101 Dis_first_time = 0;
1102 }
1103 if(dnsp->dis_first_time)
1104 {
1105 dnsp->dis_first_time = 0;
1106
1107 sprintf(str0, "%s/VERSION_NUMBER", task);
1108 sprintf(str1, "%s/CLIENT_LIST", task);
1109 sprintf(str2, "%s/SERVICE_LIST", task);
1110 sprintf(str3, "%s/SET_EXIT_HANDLER", task);
1111 sprintf(str4, "%s/EXIT", task);
1112
1113 more_ids[0] = do_dis_add_service_dns( str0, "L", &Version_number,
1114 sizeof(Version_number), 0, 0, dnsid );
1115
1116 more_ids[1] = do_dis_add_service_dns( str1, "C", 0, 0, client_info, (long)dnsp, dnsid );
1117 dnsp->dis_client_id = more_ids[1];
1118 more_ids[2] = do_dis_add_service_dns( str2, "C", 0, 0, service_info, (long)dnsp, dnsid );
1119 dnsp->dis_service_id = more_ids[2];
1120 more_ids[3] = do_dis_add_cmnd_dns( str3, "L:1", add_exit_handler, 0, dnsid );
1121 more_ids[4] = do_dis_add_cmnd_dns( str4, "L:1", exit_handler, 0, dnsid );
1122 more_ids[5] = 0;
1123 strcpy( dnsp->task_name, task );
1124if(Debug_on)
1125{
1126dim_print_date_time();
1127 printf("start serving %s\n",task);
1128}
1129 }
1130/*
1131 if(idlist)
1132 {
1133 for(i = 0; idlist[i]; i++)
1134 {
1135 servp = (SERVICE *)id_get_ptr(idlist[i], SRC_DIS);
1136 if(servp)
1137 {
1138 servp->dnsp = dnsp;
1139 n_services++;
1140 }
1141 }
1142 }
1143 if(dnsp != Default_DNS)
1144 {
1145 for(i = 0; more_ids[i]; i++)
1146 {
1147 servp = (SERVICE *)id_get_ptr(more_ids[i], SRC_DIS);
1148 if(servp)
1149 {
1150 servp->dnsp = dnsp;
1151 n_services++;
1152 }
1153 }
1154 dnsp->dis_n_services += n_services;
1155 Dis_n_services -= n_services;
1156 }
1157*/
1158 if(!Dis_timer_q)
1159 Dis_timer_q = dtq_create();
1160 if( !dnsp->dns_dis_conn_id )
1161 {
1162 if(!strcmp(task,"DIS_DNS"))
1163 {
1164 register_services(dnsp, ALL, 1);
1165 ENABLE_AST
1166 return(id_get(&(dnsp->dis_dns_packet), SRC_DIS));
1167 }
1168 else
1169 {
1170
1171 dnsp->dns_dis_conn_id = open_dns(dnsid, recv_dns_dis_rout, error_handler,
1172 DIS_DNS_TMOUT_MIN, DIS_DNS_TMOUT_MAX, SRC_DIS );
1173 if(dnsp->dns_dis_conn_id == -2)
1174 error_handler(0, DIM_FATAL, DIMDNSUNDEF, "DIM_DNS_NODE undefined");
1175 }
1176 }
1177 else
1178 {
1179 register_services(dnsp, MORE, 0);
1180 if(dnsp->dis_service_id)
1181 {
1182/*
1183 dis_update_service(Dis_service_id);
1184*/
1185 if(!dnsp->updating_service_list)
1186 {
1187 dtq_start_timer(1, do_update_service_list, dnsp);
1188 dnsp->updating_service_list = 1;
1189 }
1190 }
1191 }
1192 ENABLE_AST
1193 }
1194 return(1);
1195}
1196
1197
1198/* asynchrounous reception of requests */
1199/*
1200 Called by DNA package.
1201 A request has arrived, queue it to process later - dis_ins_request
1202*/
1203static void dis_insert_request(int conn_id, DIC_PACKET *dic_packet, int size, int status)
1204{
1205 register SERVICE *servp;
1206 register REQUEST *newp, *reqp;
1207 CLIENT *clip, *create_client();
1208 REQUEST_PTR *reqpp;
1209 int type, new_client = 0, found = 0;
1210 int find_release_request();
1211 DIS_DNS_CONN *dnsp;
1212
1213 if(size){}
1214 /* status = 1 => new connection, status = -1 => conn. lost */
1215 if(!Client_head)
1216 {
1217 Client_head = (CLIENT *)malloc(sizeof(CLIENT));
1218 dll_init( (DLL *) Client_head );
1219 }
1220 if(status != 0)
1221 {
1222 if(status == -1) /* release all requests from conn_id */
1223 {
1224if(Debug_on)
1225{
1226dim_print_date_time();
1227printf("Received Disconnection %d, from %s@%s\n",
1228 conn_id,
1229 Net_conns[conn_id].task, Net_conns[conn_id].node);
1230}
1231 release_conn(conn_id, 0, 0);
1232 }
1233 else
1234 {
1235if(Debug_on)
1236{
1237dim_print_date_time();
1238printf("Received Connection %d, from %s@%s\n",
1239 conn_id,
1240 Net_conns[conn_id].task, Net_conns[conn_id].node);
1241}
1242 }
1243 }
1244 else
1245 {
1246if(Debug_on)
1247{
1248dim_print_date_time();
1249printf("Received Request for %s, from %d %s@%s\n",
1250 dic_packet->service_name, conn_id,
1251 Net_conns[conn_id].task, Net_conns[conn_id].node);
1252}
1253 if(!(servp = find_service(dic_packet->service_name)))
1254 {
1255 release_conn(conn_id, 0, 0);
1256 return;
1257 }
1258 dic_packet->type = vtohl(dic_packet->type);
1259 type = dic_packet->type & 0xFFF;
1260 /*
1261 if(type == COMMAND)
1262 {
1263 Curr_conn_id = conn_id;
1264 execute_command(servp, dic_packet);
1265 Curr_conn_id = 0;
1266 return;
1267 }
1268 */
1269 if(type == DIM_DELETE)
1270 {
1271 find_release_request(conn_id, vtohl(dic_packet->service_id));
1272 return;
1273 }
1274 newp = (REQUEST *)/*my_*/malloc(sizeof(REQUEST));
1275 newp->service_ptr = servp;
1276 newp->service_id = vtohl(dic_packet->service_id);
1277 newp->type = dic_packet->type;
1278 newp->timeout = vtohl(dic_packet->timeout);
1279 newp->format = vtohl(dic_packet->format);
1280 newp->conn_id = conn_id;
1281 newp->first_time = 1;
1282 newp->delay_delete = 0;
1283 newp->to_delete = 0;
1284 newp->timr_ent = 0;
1285 newp->req_id = id_get((void *)newp, SRC_DIS);
1286 newp->reqpp = 0;
1287 if(type == ONCE_ONLY)
1288 {
1289 execute_service(newp->req_id);
1290 id_free(newp->req_id, SRC_DIS);
1291 free(newp);
1292 clip = create_client(conn_id, servp, &new_client);
1293 return;
1294 }
1295 if(type == COMMAND)
1296 {
1297 Curr_conn_id = conn_id;
1298 execute_command(servp, dic_packet);
1299 Curr_conn_id = 0;
1300 reqp = servp->request_head;
1301 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1302 (DLL *) reqp)) )
1303 {
1304 if(reqp->conn_id == conn_id)
1305 {
1306 id_free(newp->req_id, SRC_DIS);
1307 free(newp);
1308 found = 1;
1309 break;
1310 }
1311 }
1312 if(!found)
1313 dll_insert_queue( (DLL *) servp->request_head, (DLL *) newp );
1314 clip = create_client(conn_id, servp, &new_client);
1315 return;
1316 }
1317 dll_insert_queue( (DLL *) servp->request_head, (DLL *) newp );
1318 clip = create_client(conn_id, servp, &new_client);
1319 reqpp = (REQUEST_PTR *)malloc(sizeof(REQUEST_PTR));
1320 reqpp->reqp = newp;
1321 dll_insert_queue( (DLL *) clip->requestp_head, (DLL *) reqpp );
1322 newp->reqpp = reqpp;
1323 if((type != MONIT_ONLY) && (type != UPDATE))
1324 {
1325 execute_service(newp->req_id);
1326 }
1327 if((type != MONIT_ONLY) && (type != MONIT_FIRST))
1328 {
1329 if(newp->timeout != 0)
1330 {
1331 newp->timr_ent = dtq_add_entry( Dis_timer_q,
1332 newp->timeout,
1333 execute_service,
1334 newp->req_id );
1335 }
1336 }
1337 if(new_client)
1338 {
1339 Last_client = conn_id;
1340 dnsp = clip->dnsp;
1341 if(dnsp->dis_client_id)
1342 dis_update_service(dnsp->dis_client_id);
1343 }
1344 }
1345}
1346
1347/* A timeout for a timed or monitored service occured, serve it. */
1348
1349int execute_service( int req_id )
1350{
1351 int *buffp, size;
1352 register REQUEST *reqp;
1353 register SERVICE *servp;
1354 char str[80], def[MAX_NAME];
1355 register char *ptr;
1356 int last_conn_id;
1357 int *pkt_buffer, header_size, aux;
1358#ifdef WIN32
1359 struct timeb timebuf;
1360#else
1361 struct timeval tv;
1362 struct timezone *tz;
1363#endif
1364 FORMAT_STR format_data_cp[MAX_NAME/4];
1365
1366 reqp = (REQUEST *)id_get_ptr(req_id, SRC_DIS);
1367 if(!reqp)
1368 return(0);
1369 if(reqp->to_delete)
1370 return(0);
1371 reqp->delay_delete++;
1372 servp = reqp->service_ptr;
1373 last_conn_id = Curr_conn_id;
1374 Curr_conn_id = reqp->conn_id;
1375 ptr = servp->def;
1376 if(servp->type == COMMAND)
1377 {
1378 sprintf(str,"This is a COMMAND Service");
1379 buffp = (int *)str;
1380 size = 26;
1381 sprintf(def,"c:26");
1382 ptr = def;
1383 }
1384 else if( servp->user_routine != 0 )
1385 {
1386 (servp->user_routine)( &servp->tag, &buffp, &size,
1387 &reqp->first_time );
1388 reqp->first_time = 0;
1389
1390 }
1391 else
1392 {
1393 buffp = servp->address;
1394 size = servp->size;
1395 }
1396 Curr_conn_id = last_conn_id;
1397/* send even if no data but not if negative */
1398 if( size < 0)
1399 {
1400 reqp->delay_delete--;
1401 return(0);
1402 }
1403 if( DIS_STAMPED_HEADER + size > Dis_packet_size )
1404 {
1405 if( Dis_packet_size )
1406 free( Dis_packet );
1407 Dis_packet = (DIS_STAMPED_PACKET *)malloc(DIS_STAMPED_HEADER + size);
1408 if(!Dis_packet)
1409 {
1410 reqp->delay_delete--;
1411 return(0);
1412 }
1413 Dis_packet_size = DIS_STAMPED_HEADER + size;
1414 }
1415 Dis_packet->service_id = htovl(reqp->service_id);
1416 if((reqp->type & 0xFF000) == STAMPED)
1417 {
1418 pkt_buffer = ((DIS_STAMPED_PACKET *)Dis_packet)->buffer;
1419 header_size = DIS_STAMPED_HEADER;
1420 if(!servp->user_secs)
1421 {
1422#ifdef WIN32
1423 ftime(&timebuf);
1424 aux = timebuf.millitm;
1425 Dis_packet->time_stamp[0] = htovl(aux);
1426 Dis_packet->time_stamp[1] = htovl((int)timebuf.time);
1427#else
1428 tz = 0;
1429 gettimeofday(&tv, tz);
1430 aux = tv.tv_usec / 1000;
1431 Dis_packet->time_stamp[0] = htovl(aux);
1432 Dis_packet->time_stamp[1] = htovl(tv.tv_sec);
1433#endif
1434 }
1435 else
1436 {
1437 aux = /*0xc0de0000 |*/ servp->user_millisecs;
1438 Dis_packet->time_stamp[0] = htovl(aux);
1439 Dis_packet->time_stamp[1] = htovl(servp->user_secs);
1440 }
1441 Dis_packet->reserved[0] = htovl(0xc0dec0de);
1442 Dis_packet->quality = htovl(servp->quality);
1443 }
1444 else
1445 {
1446 pkt_buffer = ((DIS_PACKET *)Dis_packet)->buffer;
1447 header_size = DIS_HEADER;
1448 }
1449 memcpy(format_data_cp, servp->format_data, sizeof(format_data_cp));
1450 size = copy_swap_buffer_out(reqp->format, format_data_cp,
1451 pkt_buffer,
1452 buffp, size);
1453 Dis_packet->size = htovl(header_size + size);
1454 if( !dna_write_nowait(reqp->conn_id, Dis_packet, header_size + size) )
1455 {
1456 reqp->to_delete = 1;
1457 }
1458/*
1459 else
1460 {
1461 if((reqp->type & 0xFFF) == MONITORED)
1462 {
1463 if(reqp->timr_ent)
1464 dtq_clear_entry(reqp->timr_ent);
1465 }
1466 }
1467*/
1468 reqp->delay_delete--;
1469 return(1);
1470}
1471
1472void remove_service( int req_id )
1473{
1474 register REQUEST *reqp;
1475 register SERVICE *servp;
1476 static DIS_PACKET *dis_packet;
1477 static int packet_size = 0;
1478 int service_id;
1479
1480 reqp = (REQUEST *)id_get_ptr(req_id, SRC_DIS);
1481 if(!reqp)
1482 return;
1483 servp = reqp->service_ptr;
1484 if( !packet_size ) {
1485 dis_packet = (DIS_PACKET *)malloc(DIS_HEADER);
1486 packet_size = DIS_HEADER;
1487 }
1488 service_id = (reqp->service_id | 0x80000000);
1489 dis_packet->service_id = htovl(service_id);
1490 dis_packet->size = htovl(DIS_HEADER);
1491 if( !dna_write(reqp->conn_id, dis_packet, DIS_HEADER) )
1492 {
1493 release_conn(reqp->conn_id, 0, 0);
1494 }
1495}
1496
1497void execute_command(SERVICE *servp, DIC_PACKET *packet)
1498{
1499 int size;
1500 int format;
1501 FORMAT_STR format_data_cp[MAX_NAME/4], *formatp;
1502 static int *buffer;
1503 static int buffer_size = 0;
1504 int add_size;
1505
1506 size = vtohl(packet->size) - DIC_HEADER;
1507 add_size = size + (size/2);
1508 if(!buffer_size)
1509 {
1510 buffer = (int *)malloc(add_size);
1511 buffer_size = add_size;
1512 }
1513 else
1514 {
1515 if( add_size > buffer_size )
1516 {
1517 free(buffer);
1518 buffer = (int *)malloc(add_size);
1519 buffer_size = add_size;
1520 }
1521 }
1522
1523 dis_set_timestamp(servp->id, 0, 0);
1524 if(servp->user_routine != 0)
1525 {
1526 format = vtohl(packet->format);
1527 memcpy(format_data_cp, servp->format_data, sizeof(format_data_cp));
1528 if((format & 0xF) == ((MY_FORMAT) & 0xF))
1529 {
1530 for(formatp = format_data_cp; formatp->par_bytes; formatp++)
1531 {
1532 if(formatp->flags & IT_IS_FLOAT)
1533 formatp->flags |= (format & 0xf0);
1534 formatp->flags &= 0xFFF0; /* NOSWAP */
1535 }
1536 }
1537 else
1538 {
1539 for(formatp = format_data_cp; formatp->par_bytes; formatp++)
1540 {
1541 if(formatp->flags & IT_IS_FLOAT)
1542 formatp->flags |= (format & 0xf0);
1543 }
1544 }
1545 size = copy_swap_buffer_in(format_data_cp,
1546 buffer,
1547 packet->buffer, size);
1548 (servp->user_routine)(&servp->tag, buffer, &size);
1549 }
1550}
1551
1552void dis_report_service(char *serv_name)
1553{
1554 register SERVICE *servp;
1555 register REQUEST *reqp;
1556 int to_delete = 0, more;
1557
1558
1559 DISABLE_AST
1560 servp = find_service(serv_name);
1561 reqp = servp->request_head;
1562 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1563 (DLL *) reqp)) )
1564 {
1565 if((reqp->type & 0xFFF) != TIMED_ONLY)
1566 {
1567 execute_service(reqp->req_id);
1568 if(reqp->to_delete)
1569 to_delete = 1;
1570 }
1571 }
1572 if(to_delete)
1573 {
1574 do
1575 {
1576 more = 0;
1577 reqp = servp->request_head;
1578 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1579 (DLL *) reqp)) )
1580 {
1581 if(reqp->to_delete)
1582 {
1583 more = 1;
1584 release_conn(reqp->conn_id, 1, 0);
1585 break;
1586 }
1587 }
1588 }while(more);
1589 }
1590 ENABLE_AST
1591}
1592
1593int dis_update_service(unsigned service_id)
1594{
1595int do_update_service();
1596
1597 return(do_update_service(service_id,0));
1598}
1599
1600int dis_selective_update_service(unsigned service_id, int *client_ids)
1601{
1602int do_update_service();
1603
1604 return(do_update_service(service_id, client_ids));
1605}
1606
1607int check_client(REQUEST *reqp, int *client_ids)
1608{
1609 if(!client_ids)
1610 return(1);
1611 while(*client_ids)
1612 {
1613 if(reqp->conn_id == *client_ids)
1614 {
1615 return(1);
1616 }
1617 client_ids++;
1618 }
1619 return(0);
1620}
1621
1622int do_update_service(unsigned service_id, int *client_ids)
1623{
1624 register REQUEST *reqp;
1625 register SERVICE *servp;
1626 REQUEST_PTR *reqpp;
1627 CLIENT *clip;
1628 register int found = 0;
1629 int to_delete = 0, more, conn_id;
1630 char str[128];
1631 int release_request();
1632
1633 DISABLE_AST
1634 if(!service_id)
1635 {
1636 sprintf(str, "Update Service - Invalid service id");
1637 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1638 ENABLE_AST
1639 return(found);
1640 }
1641 servp = (SERVICE *)id_get_ptr(service_id, SRC_DIS);
1642 if(!servp)
1643 {
1644 ENABLE_AST
1645 return(found);
1646 }
1647 if(servp->id != (int)service_id)
1648 {
1649 ENABLE_AST
1650 return(found);
1651 }
1652 servp->delay_delete = 1;
1653 reqp = servp->request_head;
1654 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1655 (DLL *) reqp)) )
1656 {
1657if(Debug_on)
1658{
1659dim_print_date_time();
1660printf("Updating %s (id = %d, ptr = %08lX) for %s@%s (req_id = %d, req_ptr = %08lX)\n",
1661 servp->name, (int)service_id, (unsigned long)servp,
1662 Net_conns[reqp->conn_id].task, Net_conns[reqp->conn_id].node, reqp->req_id, (unsigned long)reqp);
1663}
1664 if(check_client(reqp, client_ids))
1665 reqp->delay_delete = 1;
1666 }
1667 ENABLE_AST
1668 {
1669 DISABLE_AST
1670 reqp = servp->request_head;
1671 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1672 (DLL *) reqp)) )
1673 {
1674 if(reqp->delay_delete && ((reqp->type & 0xFFF) != COMMAND))
1675 {
1676 if(check_client(reqp, client_ids))
1677 {
1678 if( (reqp->type & 0xFFF) != TIMED_ONLY )
1679 {
1680/*
1681 DISABLE_AST
1682*/
1683 execute_service(reqp->req_id);
1684 found++;
1685 ENABLE_AST
1686 {
1687 DISABLE_AST
1688 }
1689 }
1690 }
1691 }
1692 }
1693 ENABLE_AST
1694 }
1695 {
1696 DISABLE_AST
1697 reqp = servp->request_head;
1698 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1699 (DLL *) reqp)) )
1700 {
1701 if(check_client(reqp, client_ids))
1702 {
1703 reqp->delay_delete = 0;
1704 if(reqp->to_delete)
1705 to_delete = 1;
1706 }
1707 }
1708 ENABLE_AST
1709 }
1710 if(to_delete)
1711 {
1712 DISABLE_AST
1713 do
1714 {
1715 more = 0;
1716 reqp = servp->request_head;
1717 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1718 (DLL *) reqp)) )
1719 {
1720 if(reqp->to_delete & 0x1)
1721 {
1722 more = 1;
1723 reqp->to_delete = 0;
1724 release_conn(reqp->conn_id, 1, 0);
1725 break;
1726 }
1727 else if(reqp->to_delete & 0x2)
1728 {
1729 more = 1;
1730 reqp->to_delete = 0;
1731 reqpp = reqp->reqpp;
1732 conn_id = reqp->conn_id;
1733 release_request(reqp, reqpp, 1);
1734 clip = find_client(conn_id);
1735 if(clip)
1736 {
1737 if( dll_empty((DLL *)clip->requestp_head) )
1738 {
1739 release_conn( conn_id, 0, 0);
1740 }
1741 }
1742 break;
1743 }
1744 }
1745 }while(more);
1746 ENABLE_AST
1747 }
1748 {
1749 DISABLE_AST
1750 servp->delay_delete = 0;
1751 if(servp->to_delete)
1752 {
1753 dis_remove_service(servp->id);
1754 }
1755 ENABLE_AST
1756 }
1757
1758 return(found);
1759}
1760
1761int dis_get_n_clients(unsigned service_id)
1762{
1763 register REQUEST *reqp;
1764 register SERVICE *servp;
1765 register int found = 0;
1766 char str[128];
1767
1768 DISABLE_AST
1769 if(!service_id)
1770 {
1771 sprintf(str, "Service Has Clients- Invalid service id");
1772 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1773 ENABLE_AST
1774 return(found);
1775 }
1776 servp = (SERVICE *)id_get_ptr(service_id, SRC_DIS);
1777 if(!servp)
1778 {
1779 ENABLE_AST
1780 return(found);
1781 }
1782 if(servp->id != (int)service_id)
1783 {
1784 ENABLE_AST
1785 return(found);
1786 }
1787 reqp = servp->request_head;
1788 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1789 (DLL *) reqp)) )
1790 {
1791 found++;
1792 }
1793 ENABLE_AST
1794 return found;
1795}
1796
1797int dis_get_timeout(unsigned service_id, int client_id)
1798{
1799 register REQUEST *reqp;
1800 register SERVICE *servp;
1801 char str[128];
1802
1803 if(!service_id)
1804 {
1805 sprintf(str,"Get Timeout - Invalid service id");
1806 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1807 return(-1);
1808 }
1809 servp = (SERVICE *)id_get_ptr(service_id, SRC_DIS);
1810 if(!servp)
1811 {
1812 return(-1);
1813 }
1814 reqp = servp->request_head;
1815 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1816 (DLL *) reqp)) )
1817 {
1818 if(reqp->conn_id == client_id)
1819 return(reqp->timeout);
1820 }
1821 return(-1);
1822}
1823
1824void dis_set_quality( unsigned serv_id, int quality )
1825{
1826 register SERVICE *servp;
1827 char str[128];
1828
1829 DISABLE_AST
1830 if(!serv_id)
1831 {
1832 sprintf(str,"Set Quality - Invalid service id");
1833 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1834 ENABLE_AST
1835 return;
1836 }
1837 servp = (SERVICE *)id_get_ptr(serv_id, SRC_DIS);
1838 if(!servp)
1839 {
1840 ENABLE_AST
1841 return;
1842 }
1843 if(servp->id != (int)serv_id)
1844 {
1845 ENABLE_AST
1846 return;
1847 }
1848 servp->quality = quality;
1849 ENABLE_AST
1850}
1851
1852int dis_set_timestamp( unsigned serv_id, int secs, int millisecs )
1853{
1854 register SERVICE *servp;
1855 char str[128];
1856#ifdef WIN32
1857 struct timeb timebuf;
1858#else
1859 struct timeval tv;
1860 struct timezone *tz;
1861#endif
1862
1863 DISABLE_AST
1864 if(!serv_id)
1865 {
1866 sprintf(str,"Set Timestamp - Invalid service id");
1867 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1868 ENABLE_AST
1869 return(0);
1870 }
1871 servp = (SERVICE *)id_get_ptr(serv_id, SRC_DIS);
1872 if(!servp)
1873 {
1874 ENABLE_AST
1875 return(0);
1876 }
1877 if(servp->id != (int)serv_id)
1878 {
1879 ENABLE_AST
1880 return(0);
1881 }
1882 if(secs == 0)
1883 {
1884#ifdef WIN32
1885 ftime(&timebuf);
1886 servp->user_secs = (int)timebuf.time;
1887 servp->user_millisecs = timebuf.millitm;
1888#else
1889 tz = 0;
1890 gettimeofday(&tv, tz);
1891 servp->user_secs = tv.tv_sec;
1892 servp->user_millisecs = tv.tv_usec / 1000;
1893#endif
1894 }
1895 else
1896 {
1897 servp->user_secs = secs;
1898/*
1899 servp->user_millisecs = (millisecs & 0xffff);
1900*/
1901 servp->user_millisecs = millisecs;
1902 }
1903 ENABLE_AST
1904 return(1);
1905}
1906
1907int dis_get_timestamp( unsigned serv_id, int *secs, int *millisecs )
1908{
1909 register SERVICE *servp;
1910 char str[128];
1911
1912 DISABLE_AST
1913 if(!serv_id)
1914 {
1915 sprintf(str,"Get Timestamp - Invalid service id");
1916 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1917 ENABLE_AST
1918 return(0);
1919 }
1920 servp = (SERVICE *)id_get_ptr(serv_id, SRC_DIS);
1921 if(!servp)
1922 {
1923 ENABLE_AST
1924 return(0);
1925 }
1926 if(servp->id != (int)serv_id)
1927 {
1928 ENABLE_AST
1929 return(0);
1930 }
1931 if(servp->user_secs)
1932 {
1933 *secs = servp->user_secs;
1934 *millisecs = servp->user_millisecs;
1935 }
1936 else
1937 {
1938 *secs = 0;
1939 *millisecs = 0;
1940 }
1941 ENABLE_AST
1942 return(1);
1943}
1944
1945void dis_send_service(unsigned service_id, int *buffer, int size)
1946{
1947 register REQUEST *reqp, *prevp;
1948 register SERVICE *servp;
1949 static DIS_PACKET *dis_packet;
1950 static int packet_size = 0;
1951 int conn_id;
1952 char str[128];
1953
1954 DISABLE_AST
1955 if( !service_id ) {
1956 sprintf(str,"Send Service - Invalid service id");
1957 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
1958 ENABLE_AST
1959 return;
1960 }
1961 servp = (SERVICE *)id_get_ptr(service_id, SRC_DIS);
1962 if(!servp)
1963 {
1964 ENABLE_AST
1965 return;
1966 }
1967 if(!packet_size)
1968 {
1969 dis_packet = (DIS_PACKET *)malloc(DIS_HEADER+size);
1970 packet_size = DIS_HEADER + size;
1971 }
1972 else
1973 {
1974 if( DIS_HEADER+size > packet_size )
1975 {
1976 free(dis_packet);
1977 dis_packet = (DIS_PACKET *)malloc(DIS_HEADER+size);
1978 packet_size = DIS_HEADER+size;
1979 }
1980 }
1981 prevp = servp->request_head;
1982 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
1983 (DLL *) prevp)) )
1984 {
1985 dis_packet->service_id = htovl(reqp->service_id);
1986 memcpy(dis_packet->buffer, buffer, size);
1987 dis_packet->size = htovl(DIS_HEADER + size);
1988
1989 conn_id = reqp->conn_id;
1990 if( !dna_write_nowait(conn_id, dis_packet, size + DIS_HEADER) )
1991 {
1992 release_conn(conn_id, 1, 0);
1993 }
1994 else
1995 prevp = reqp;
1996 }
1997 ENABLE_AST
1998}
1999
2000int dis_remove_service(unsigned service_id)
2001{
2002 register REQUEST *reqp, *auxp;
2003 register SERVICE *servp;
2004 REQUEST_PTR *reqpp;
2005 int found = 0;
2006 char str[128];
2007 int release_request();
2008 int dis_hash_service_remove();
2009 DIS_DNS_CONN *dnsp;
2010 int n_services;
2011 void do_dis_stop_serving_dns(DIS_DNS_CONN *);
2012
2013 DISABLE_AST
2014 if(!service_id)
2015 {
2016 sprintf(str,"Remove Service - Invalid service id");
2017 error_handler(0, DIM_ERROR, DIMSVCINVAL, str);
2018 ENABLE_AST
2019 return(found);
2020 }
2021 servp = (SERVICE *)id_get_ptr(service_id, SRC_DIS);
2022 if(!servp)
2023 {
2024 ENABLE_AST
2025 return(found);
2026 }
2027 if(servp->id != (int)service_id)
2028 {
2029 ENABLE_AST
2030 return(found);
2031 }
2032if(Debug_on)
2033{
2034dim_print_date_time();
2035 printf("Removing service %s, delay_delete = %d\n",
2036 servp->name, servp->delay_delete);
2037}
2038 if(servp->delay_delete)
2039 {
2040 servp->to_delete = 1;
2041 ENABLE_AST
2042 return(found);
2043 }
2044 /* remove from name server */
2045
2046 dnsp = servp->dnsp;
2047 unregister_service(dnsp, servp);
2048 /* Release client requests and remove from actual clients */
2049 reqp = servp->request_head;
2050 while( (reqp = (REQUEST *) dll_get_next((DLL *)servp->request_head,
2051 (DLL *) reqp)) )
2052 {
2053 remove_service(reqp->req_id);
2054 auxp = reqp->prev;
2055 reqpp = (REQUEST_PTR *) reqp->reqpp;
2056 release_request(reqp, reqpp, 1);
2057 found = 1;
2058 reqp = auxp;
2059 }
2060 if(servp->id == (int)dnsp->dis_service_id)
2061 dnsp->dis_service_id = 0;
2062 if(servp->id == (int)dnsp->dis_client_id)
2063 dnsp->dis_client_id = 0;
2064 dis_hash_service_remove(servp);
2065 id_free(servp->id, SRC_DIS);
2066 free(servp->request_head);
2067 free(servp);
2068/*
2069 if(dnsp != Default_DNS)
2070 {
2071 dnsp->dis_n_services--;
2072 n_services = dnsp->dis_n_services;
2073 }
2074 else
2075 {
2076 Dis_n_services--;
2077 n_services = Dis_n_services;
2078 }
2079*/
2080 dnsp->dis_n_services--;
2081 n_services = dnsp->dis_n_services;
2082
2083 ENABLE_AST
2084 if(dnsp->serving)
2085 {
2086 if(n_services == 5)
2087 {
2088/*
2089 dis_stop_serving();
2090*/
2091 do_dis_stop_serving_dns(dnsp);
2092 }
2093 }
2094 return(found);
2095}
2096
2097void do_dis_stop_serving_dns(DIS_DNS_CONN *dnsp)
2098{
2099register SERVICE *servp, *prevp;
2100void dim_stop_threads(void);
2101int dis_no_dns();
2102int hash_index, old_index;
2103extern int close_dns(long, int);
2104CLIENT *clip, *cprevp;
2105
2106 dnsp->serving = 0;
2107 dis_init();
2108/*
2109 dis_hash_service_init();
2110 prevp = 0;
2111 if(Dis_conn_id)
2112 {
2113 dna_close(Dis_conn_id);
2114 Dis_conn_id = 0;
2115 }
2116*/
2117 {
2118 DISABLE_AST
2119 if(dnsp->dns_timr_ent)
2120 {
2121 dtq_rem_entry(Dis_timer_q, dnsp->dns_timr_ent);
2122 dnsp->dns_timr_ent = NULL;
2123 }
2124 if(dnsp->dns_dis_conn_id)
2125 {
2126 dna_close(dnsp->dns_dis_conn_id);
2127 dnsp->dns_dis_conn_id = 0;
2128 }
2129 ENABLE_AST
2130 }
2131 {
2132 DISABLE_AST
2133 prevp = 0;
2134 hash_index = -1;
2135 old_index = -1;
2136 while( (servp = dis_hash_service_get_next(&hash_index, prevp, 0)) )
2137 {
2138 if(servp->dnsp == dnsp)
2139 {
2140 ENABLE_AST
2141 dis_remove_service(servp->id);
2142 {
2143 DISABLE_AST
2144 if(old_index != hash_index)
2145 prevp = 0;
2146 }
2147 }
2148 else
2149 {
2150 prevp = servp;
2151 old_index = hash_index;
2152 }
2153 }
2154 ENABLE_AST
2155 }
2156 {
2157 DISABLE_AST
2158 cprevp = Client_head;
2159 while( (clip = (CLIENT *)dll_get_next( (DLL *) Client_head,
2160 (DLL*) cprevp)) )
2161 {
2162 if(clip->dnsp != dnsp)
2163 {
2164 cprevp = clip;
2165 continue;
2166 }
2167 if( dll_empty((DLL *)clip->requestp_head) )
2168 {
2169if(Debug_on)
2170{
2171dim_print_date_time();
2172printf("Releasing conn %d, to %s@%s\n",
2173 clip->conn_id,
2174 Net_conns[clip->conn_id].task, Net_conns[clip->conn_id].node);
2175}
2176 release_conn( clip->conn_id, 0, 0);
2177 }
2178 else
2179 {
2180 cprevp = clip;
2181 }
2182 }
2183 ENABLE_AST
2184 }
2185if(Debug_on)
2186{
2187dim_print_date_time();
2188printf("Cleaning dnsp variables\n");
2189}
2190
2191 dnsp->dis_first_time = 1;
2192 dnsp->dis_n_services = 0;
2193 dnsp->dis_dns_packet.size = 0;
2194 dnsp->dis_dns_packet.src_type = 0;
2195 close_dns(dnsp->dnsid, SRC_DIS);
2196/*
2197 if(dnsp != Default_DNS)
2198 {
2199 dll_remove(dnsp);
2200 free(dnsp);
2201 }
2202*/
2203/*
2204 if(dll_empty(DNS_head))
2205*/
2206 if(dis_no_dns())
2207 dis_stop_serving();
2208}
2209
2210void dis_stop_serving_dns(long dnsid)
2211{
2212 DIS_DNS_CONN *dnsp, *dis_find_dns();
2213
2214 dnsp = dis_find_dns(dnsid);
2215 do_dis_stop_serving_dns(dnsp);
2216}
2217
2218void dis_stop_serving()
2219{
2220register SERVICE *servp, *prevp;
2221void dim_stop_threads(void);
2222int hash_index;
2223
2224 Serving = 0;
2225 dis_init();
2226 if(Dis_conn_id)
2227 {
2228 dna_close(Dis_conn_id);
2229 Dis_conn_id = 0;
2230 }
2231/*
2232 if(Dns_dis_conn_id)
2233 {
2234 dna_close(Dns_dis_conn_id);
2235 Dns_dis_conn_id = 0;
2236 }
2237*/
2238 {
2239 DISABLE_AST
2240 prevp = 0;
2241 hash_index = -1;
2242 while( (servp = dis_hash_service_get_next(&hash_index, prevp, 0)) )
2243 {
2244 ENABLE_AST
2245 dis_remove_service(servp->id);
2246 {
2247 DISABLE_AST
2248 prevp = 0;
2249 }
2250 }
2251 ENABLE_AST
2252 }
2253/*
2254 if(Dis_conn_id)
2255 dna_close(Dis_conn_id);
2256 if(Dns_dis_conn_id)
2257 dna_close(Dns_dis_conn_id);
2258 Dns_dis_conn_id = 0;
2259*/
2260 Dis_first_time = 1;
2261/*
2262 if(Dns_timr_ent)
2263 {
2264 dtq_rem_entry(Dis_timer_q, Dns_timr_ent);
2265 Dns_timr_ent = NULL;
2266 }
2267*/
2268 dtq_delete(Dis_timer_q);
2269 Dis_timer_q = 0;
2270 dim_stop_threads();
2271}
2272
2273/* find service by name */
2274SERVICE *find_service(char *name)
2275{
2276 return(dis_hash_service_exists(name));
2277}
2278
2279CLIENT *create_client(int conn_id, SERVICE *servp, int *new_client)
2280{
2281 CLIENT *clip;
2282
2283 *new_client = 0;
2284 if(!(clip = find_client(conn_id)))
2285 {
2286 clip = (CLIENT *)malloc(sizeof(CLIENT));
2287 clip->conn_id = conn_id;
2288 clip->dnsp = servp->dnsp;
2289 clip->requestp_head = (REQUEST_PTR *)malloc(sizeof(REQUEST_PTR));
2290 dll_init( (DLL *) clip->requestp_head );
2291 dll_insert_queue( (DLL *) Client_head, (DLL *) clip );
2292 *new_client = 1;
2293 }
2294 return clip;
2295}
2296
2297CLIENT *find_client(int conn_id)
2298{
2299 register CLIENT *clip;
2300
2301 clip = (CLIENT *)
2302 dll_search( (DLL *) Client_head, &conn_id, sizeof(conn_id));
2303 return(clip);
2304}
2305
2306void release_all_requests(int conn_id, CLIENT *clip)
2307{
2308 register REQUEST_PTR *reqpp, *auxp;
2309 register REQUEST *reqp;
2310 int found = 0;
2311 int release_request();
2312 DIS_DNS_CONN *dnsp;
2313
2314 DISABLE_AST;
2315 if(clip)
2316 {
2317 reqpp = clip->requestp_head;
2318 while( (reqpp = (REQUEST_PTR *) dll_get_next((DLL *)clip->requestp_head,
2319 (DLL *) reqpp)) )
2320 {
2321 auxp = reqpp->prev;
2322 reqp = (REQUEST *) reqpp->reqp;
2323 release_request(reqp, reqpp, 0);
2324 found = 1;
2325 reqpp = auxp;
2326 }
2327 dnsp = clip->dnsp;
2328 dll_remove(clip);
2329 free(clip->requestp_head);
2330 free(clip);
2331 }
2332 if(found)
2333 {
2334 Last_client = -conn_id;
2335 if(dnsp->dis_client_id)
2336 dis_update_service(dnsp->dis_client_id);
2337 }
2338 dna_close(conn_id);
2339 ENABLE_AST;
2340}
2341
2342CLIENT *check_delay_delete(int conn_id)
2343{
2344 register REQUEST_PTR *reqpp;
2345 register CLIENT *clip;
2346 register REQUEST *reqp;
2347 int found = 0;
2348
2349 DISABLE_AST;
2350 clip = find_client(conn_id);
2351 if(clip)
2352 {
2353 reqpp = clip->requestp_head;
2354 while( (reqpp = (REQUEST_PTR *) dll_get_next((DLL *)clip->requestp_head,
2355 (DLL *) reqpp)) )
2356 {
2357 reqp = (REQUEST *) reqpp->reqp;
2358 if(reqp->delay_delete)
2359 {
2360 reqp->to_delete = 1;
2361 found = 1;
2362 }
2363 }
2364 }
2365 ENABLE_AST;
2366 if(found)
2367 {
2368 return((CLIENT *)-1);
2369 }
2370 return(clip);
2371}
2372
2373char *dis_get_error_services()
2374{
2375 return(dis_get_client_services(Error_conn_id));
2376}
2377
2378char *dis_get_client_services(int conn_id)
2379{
2380 register REQUEST_PTR *reqpp;
2381 register CLIENT *clip;
2382 register REQUEST *reqp;
2383 register SERVICE *servp;
2384
2385 int n_services = 0;
2386 int max_size;
2387 static int curr_allocated_size = 0;
2388 static char *service_info_buffer;
2389 char *buff_ptr;
2390
2391
2392 if(!conn_id)
2393 return((char *)0);
2394 {
2395 DISABLE_AST;
2396 clip = find_client(conn_id);
2397 if(clip)
2398 {
2399 reqpp = clip->requestp_head;
2400 while( (reqpp = (REQUEST_PTR *) dll_get_next((DLL *)clip->requestp_head,
2401 (DLL *) reqpp)))
2402 {
2403 n_services++;
2404 }
2405 if(!n_services)
2406 {
2407 ENABLE_AST
2408 return((char *)0);
2409 }
2410 max_size = n_services * MAX_NAME;
2411 if(!curr_allocated_size)
2412 {
2413 service_info_buffer = (char *)malloc(max_size);
2414 curr_allocated_size = max_size;
2415 }
2416 else if (max_size > curr_allocated_size)
2417 {
2418 free(service_info_buffer);
2419 service_info_buffer = (char *)malloc(max_size);
2420 curr_allocated_size = max_size;
2421 }
2422 service_info_buffer[0] = '\0';
2423 buff_ptr = service_info_buffer;
2424 reqpp = clip->requestp_head;
2425 while( (reqpp = (REQUEST_PTR *) dll_get_next((DLL *)clip->requestp_head,
2426 (DLL *) reqpp)) )
2427 {
2428 reqp = (REQUEST *) reqpp->reqp;
2429 servp = reqp->service_ptr;
2430 strcat(buff_ptr, servp->name);
2431 strcat(buff_ptr, "\n");
2432 buff_ptr += strlen(buff_ptr);
2433 }
2434 }
2435 else
2436 {
2437 ENABLE_AST
2438 return((char *)0);
2439 }
2440 ENABLE_AST;
2441 }
2442/*
2443 dim_print_date_time();
2444 dna_get_node_task(conn_id, node, task);
2445 printf("Client %s@%s uses services: \n", task, node);
2446 printf("%s\n",service_info_buffer);
2447*/
2448 return(service_info_buffer);
2449}
2450
2451int find_release_request(int conn_id, int service_id)
2452{
2453 register REQUEST_PTR *reqpp, *auxp;
2454 register CLIENT *clip;
2455 register REQUEST *reqp;
2456 int release_request();
2457
2458 DISABLE_AST
2459 clip = find_client(conn_id);
2460 if(clip)
2461 {
2462 reqpp = clip->requestp_head;
2463 while( (reqpp = (REQUEST_PTR *) dll_get_next((DLL *)clip->requestp_head,
2464 (DLL *) reqpp)) )
2465 {
2466 reqp = (REQUEST *) reqpp->reqp;
2467 if(reqp->service_id == service_id)
2468 {
2469 if(reqp->delay_delete)
2470 {
2471 reqp->to_delete += 0x2;
2472 }
2473 else
2474 {
2475 auxp = reqpp->prev;
2476 release_request(reqp, reqpp, 0);
2477 reqpp = auxp;
2478 }
2479 }
2480 }
2481 if( dll_empty((DLL *)clip->requestp_head) )
2482 {
2483 release_conn( conn_id, 0, 0 );
2484 }
2485 }
2486 ENABLE_AST
2487 return(1);
2488}
2489
2490int release_request(REQUEST *reqp, REQUEST_PTR *reqpp, int remove)
2491{
2492 int conn_id;
2493 CLIENT *clip;
2494
2495 DISABLE_AST
2496 conn_id = reqp->conn_id;
2497 if(reqpp)
2498 dll_remove((DLL *)reqpp);
2499 dll_remove((DLL *)reqp);
2500 if(reqp->timr_ent)
2501 dtq_rem_entry(Dis_timer_q, reqp->timr_ent);
2502 id_free(reqp->req_id, SRC_DIS);
2503 free(reqp);
2504 if(reqpp)
2505 free(reqpp);
2506/* Would do it too early, the client will disconnect anyway
2507*/
2508 if((remove) && (!Serving))
2509 {
2510 clip = find_client(conn_id);
2511 if(clip)
2512 {
2513 if( dll_empty((DLL *)clip->requestp_head) )
2514 {
2515 release_conn( conn_id, 0, 0);
2516 }
2517 }
2518 }
2519
2520 ENABLE_AST
2521 return(1);
2522}
2523
2524static int release_conn(int conn_id, int print_flg, int dns_flag)
2525{
2526 static int releasing = 0;
2527 CLIENT *clip;
2528 int do_exit_handler();
2529
2530 DISABLE_AST
2531 if(print_flg){}
2532 if(dns_flag)
2533 {
2534 recv_dns_dis_rout( conn_id, 0, 0, STA_DISC );
2535 ENABLE_AST
2536 return(0);
2537 }
2538#ifdef VMS
2539 if(print_flg)
2540 {
2541 dim_print_date_time();
2542 dna_get_node_task(conn_id, node, task);
2543 printf(" Couldn't write to client %s@%s, releasing connection %d\n",
2544 task, node, conn_id);
2545 fflush(stdout);
2546 }
2547#endif
2548 clip = check_delay_delete(conn_id);
2549 if(clip != (CLIENT *)-1)
2550 {
2551 if( Client_exit_user_routine != 0 )
2552 {
2553 releasing++;
2554 Curr_conn_id = conn_id;
2555 do_exit_handler(conn_id);
2556 releasing--;
2557 }
2558 if(!releasing)
2559 {
2560 release_all_requests(conn_id, clip);
2561 }
2562 }
2563 ENABLE_AST
2564 return(1);
2565}
2566
2567typedef struct cmnds{
2568 struct cmnds *next;
2569 long tag;
2570 int size;
2571 int buffer[1];
2572} DIS_CMND;
2573
2574static DIS_CMND *Cmnds_head = (DIS_CMND *)0;
2575
2576void std_cmnd_handler(long *tag, int *cmnd_buff, int *size)
2577{
2578 register DIS_CMND *new_cmnd;
2579/* queue the command */
2580
2581 if(!Cmnds_head)
2582 {
2583 Cmnds_head = (DIS_CMND *)malloc(sizeof(DIS_CMND));
2584 sll_init((SLL *) Cmnds_head);
2585 }
2586 new_cmnd = (DIS_CMND *)malloc((*size)+12);
2587 new_cmnd->next = 0;
2588 new_cmnd->tag = *tag;
2589 new_cmnd->size = *size;
2590 memcpy(new_cmnd->buffer, cmnd_buff, *size);
2591 sll_insert_queue((SLL *) Cmnds_head, (SLL *) new_cmnd);
2592}
2593
2594int dis_get_next_cmnd(long *tag, int *buffer, int *size)
2595{
2596 register DIS_CMND *cmndp;
2597 register int ret_val = -1;
2598
2599 DISABLE_AST
2600 if(!Cmnds_head)
2601 {
2602 Cmnds_head = (DIS_CMND *)malloc(sizeof(DIS_CMND));
2603 sll_init((SLL *) Cmnds_head);
2604 }
2605 if(*size == 0)
2606 {
2607 if( (cmndp = (DIS_CMND *) sll_get_head((SLL *) Cmnds_head)))
2608 {
2609 if(cmndp->size > 0)
2610 {
2611 *size = cmndp->size;
2612 *tag = cmndp->tag;
2613 ENABLE_AST
2614 return(-1);
2615 }
2616 }
2617 }
2618 if( (cmndp = (DIS_CMND *) sll_remove_head((SLL *) Cmnds_head)) )
2619 {
2620 if (*size >= cmndp->size)
2621 {
2622 *size = cmndp->size;
2623 ret_val = 1;
2624 }
2625 memcpy(buffer, cmndp->buffer, *size);
2626 *tag = cmndp->tag;
2627 free(cmndp);
2628 ENABLE_AST
2629 return(ret_val);
2630 }
2631 ENABLE_AST
2632 return(0);
2633}
2634
2635int dis_get_conn_id()
2636{
2637 return(Curr_conn_id);
2638}
2639
2640int dis_get_client(char *name)
2641{
2642 int ret = 0;
2643 char node[MAX_NODE_NAME], task[MAX_TASK_NAME];
2644
2645 DISABLE_AST
2646
2647 if(Curr_conn_id)
2648 {
2649 dna_get_node_task(Curr_conn_id, node, task);
2650 strcpy(name,task);
2651 strcat(name,"@");
2652 strcat(name,node);
2653 ret = Curr_conn_id;
2654 }
2655 ENABLE_AST
2656 return(ret);
2657}
2658
2659#ifdef VMS
2660dis_convert_str(c_str, for_str)
2661char *c_str;
2662struct dsc$descriptor_s *for_str;
2663{
2664 int i;
2665
2666 strcpy(for_str->dsc$a_pointer, c_str);
2667 for(i = strlen(c_str); i< for_str->dsc$w_length; i++)
2668 for_str->dsc$a_pointer[i] = ' ';
2669}
2670#endif
2671
2672void client_info(long *tag, int **bufp, int *size, int *first_time)
2673{
2674 register CLIENT *clip;
2675 int curr_conns[MAX_CONNS];
2676 int i, index, max_size;
2677 static int curr_allocated_size = 0;
2678 static char *dns_info_buffer;
2679 register char *dns_client_info;
2680 char node[MAX_NODE_NAME], task[MAX_TASK_NAME];
2681 DIS_DNS_CONN *dnsp = (DIS_DNS_CONN *)*tag;
2682
2683 max_size = sizeof(DNS_CLIENT_INFO);
2684 if(!curr_allocated_size)
2685 {
2686 dns_info_buffer = malloc(max_size);
2687 curr_allocated_size = max_size;
2688 }
2689 dns_client_info = dns_info_buffer;
2690 dns_client_info[0] = '\0';
2691 index = 0;
2692 if(*first_time)
2693 {
2694 clip = Client_head;
2695 while( (clip = (CLIENT *)dll_get_next( (DLL *) Client_head,
2696 (DLL*) clip)) )
2697 {
2698 if(clip->dnsp != dnsp)
2699 continue;
2700 curr_conns[index++] = clip->conn_id;
2701 }
2702 max_size = (index+1)*sizeof(DNS_CLIENT_INFO);
2703 if (max_size > curr_allocated_size)
2704 {
2705 free(dns_info_buffer);
2706 dns_info_buffer = malloc(max_size);
2707 curr_allocated_size = max_size;
2708 }
2709 dns_client_info = dns_info_buffer;
2710 dns_client_info[0] = '\0';
2711 }
2712 else
2713 {
2714 if(Last_client > 0)
2715 {
2716 strcat(dns_client_info,"+");
2717 curr_conns[index++] = Last_client;
2718 }
2719 else
2720 {
2721 strcat(dns_client_info,"-");
2722 curr_conns[index++] = -Last_client;
2723 }
2724 }
2725
2726 for(i=0; i<index;i++)
2727 {
2728 dna_get_node_task(curr_conns[i], node, task);
2729 strcat(dns_client_info,task);
2730 strcat(dns_client_info,"@");
2731 strcat(dns_client_info,node);
2732 strcat(dns_client_info,"|");
2733 }
2734 if(index)
2735 dns_client_info[strlen(dns_client_info)-1] = '\0';
2736 *bufp = (int *)dns_info_buffer;
2737 *size = strlen(dns_info_buffer)+1;
2738}
2739
2740void append_service(char *service_info_buffer, SERVICE *servp)
2741{
2742 char name[MAX_NAME], *ptr;
2743
2744 if(strstr(servp->name,"/RpcIn"))
2745 {
2746 strcpy(name,servp->name);
2747 ptr = (char *)strstr(name,"/RpcIn");
2748 *ptr = 0;
2749 strcat(service_info_buffer, name);
2750 strcat(service_info_buffer, "|");
2751 if(servp->def[0])
2752 {
2753 strcat(service_info_buffer, servp->def);
2754 }
2755 strcat(name,"/RpcOut");
2756 if( (servp = find_service(name)) )
2757 {
2758 strcat(service_info_buffer, ",");
2759 if(servp->def[0])
2760 {
2761 strcat(service_info_buffer, servp->def);
2762 }
2763 }
2764 strcat(service_info_buffer, "|RPC");
2765 strcat(service_info_buffer, "\n");
2766 }
2767 else if(strstr(servp->name,"/RpcOut"))
2768 {
2769/*
2770 if(servp->def[0])
2771 {
2772 strcat(service_info_buffer, servp->def);
2773 }
2774 strcat(service_info_buffer, "|RPC");
2775 strcat(service_info_buffer, "\n");
2776
2777*/
2778 }
2779 else
2780 {
2781 strcat(service_info_buffer, servp->name);
2782 strcat(service_info_buffer, "|");
2783 if(servp->def[0])
2784 {
2785 strcat(service_info_buffer, servp->def);
2786 }
2787 strcat(service_info_buffer, "|");
2788 if(servp->type == COMMAND)
2789 {
2790 strcat(service_info_buffer, "CMD");
2791 }
2792 strcat(service_info_buffer, "\n");
2793 }
2794}
2795
2796void service_info(long *tag, int **bufp, int *size, int *first_time)
2797{
2798 register SERVICE *servp;
2799 int max_size, done = 0;
2800 static int curr_allocated_size = 0;
2801 static char *service_info_buffer;
2802 char *buff_ptr;
2803 DIS_DNS_CONN *dnsp = (DIS_DNS_CONN *)*tag;
2804 int hash_index;
2805
2806 DISABLE_AST
2807 max_size = (dnsp->dis_n_services+10) * (MAX_NAME*2 + 4);
2808 if(!curr_allocated_size)
2809 {
2810 service_info_buffer = (char *)malloc(max_size);
2811 curr_allocated_size = max_size;
2812 }
2813 else if (max_size > curr_allocated_size)
2814 {
2815 free(service_info_buffer);
2816 service_info_buffer = (char *)malloc(max_size);
2817 curr_allocated_size = max_size;
2818 }
2819 service_info_buffer[0] = '\0';
2820 buff_ptr = service_info_buffer;
2821 servp = 0;
2822 hash_index = -1;
2823 if(*first_time)
2824 {
2825 while( (servp = dis_hash_service_get_next(&hash_index, servp, 0)) )
2826 {
2827 if(servp->dnsp != dnsp)
2828 continue;
2829 if(servp->registered)
2830 {
2831 servp->registered = 2;
2832 append_service(buff_ptr, servp);
2833 buff_ptr += strlen(buff_ptr);
2834 }
2835 }
2836 }
2837 else
2838 {
2839 while( (servp = dis_hash_service_get_next(&hash_index, servp, 0)) )
2840 {
2841 if(servp->dnsp != dnsp)
2842 continue;
2843 if(servp->registered == 1)
2844 {
2845 if(!done)
2846 {
2847 strcat(buff_ptr, "+");
2848 buff_ptr += strlen(buff_ptr);
2849 done = 1;
2850 }
2851 append_service(buff_ptr, servp);
2852 buff_ptr += strlen(buff_ptr);
2853 servp->registered = 2;
2854 }
2855 else if(servp->registered == 0)
2856 {
2857 strcat(buff_ptr, "-");
2858 buff_ptr += strlen(buff_ptr);
2859 append_service(buff_ptr, servp);
2860 buff_ptr += strlen(buff_ptr);
2861 }
2862 }
2863 }
2864 *bufp = (int *)service_info_buffer;
2865 *size = buff_ptr - service_info_buffer+1;
2866 ENABLE_AST
2867}
2868
2869void add_exit_handler(int *tag, int *bufp, int *size)
2870{
2871 EXIT_H *newp;
2872
2873 if(size){}
2874 if(tag){}
2875 if(*bufp)
2876 {
2877 if(!Exit_h_head)
2878 {
2879 Exit_h_head = (EXIT_H *)malloc(sizeof(EXIT_H));
2880 sll_init( (SLL *) Exit_h_head );
2881 }
2882 newp = (EXIT_H *)malloc(sizeof(EXIT_H));
2883 newp->conn_id = Curr_conn_id;
2884 newp->exit_id = *bufp;
2885 sll_insert_queue( (SLL *) Exit_h_head, (SLL *) newp );
2886 }
2887 else
2888 {
2889 if(!Exit_h_head)
2890 return;
2891 if((newp = (EXIT_H *)sll_search((SLL *) Exit_h_head,
2892 (char *)&Curr_conn_id, 4)) )
2893 {
2894 sll_remove( (SLL *) Exit_h_head, (SLL *) newp );
2895 }
2896 }
2897}
2898
2899void dis_set_client_exit_handler(int conn_id, int tag)
2900{
2901 EXIT_H *newp;
2902
2903 DISABLE_AST
2904 if(tag)
2905 {
2906 if(!Exit_h_head)
2907 {
2908 Exit_h_head = (EXIT_H *)malloc(sizeof(EXIT_H));
2909 sll_init( (SLL *) Exit_h_head );
2910 }
2911 if( (newp = (EXIT_H *)sll_search((SLL *) Exit_h_head,
2912 (char *)&conn_id, 4)) )
2913 {
2914 newp->conn_id = conn_id;
2915 newp->exit_id = tag;
2916 }
2917 else
2918 {
2919 newp = (EXIT_H *)malloc(sizeof(EXIT_H));
2920 newp->conn_id = conn_id;
2921 newp->exit_id = tag;
2922 sll_insert_queue( (SLL *) Exit_h_head, (SLL *) newp );
2923 }
2924 }
2925 else
2926 {
2927 if(!Exit_h_head)
2928 {
2929 ENABLE_AST
2930 return;
2931 }
2932 if( (newp = (EXIT_H *)sll_search((SLL *) Exit_h_head,
2933 (char *)&conn_id, 4)) )
2934 {
2935 sll_remove( (SLL *) Exit_h_head, (SLL *) newp );
2936 }
2937 }
2938 ENABLE_AST
2939}
2940
2941int do_exit_handler(int conn_id)
2942{
2943 register EXIT_H *exitp;
2944
2945 DISABLE_AST;
2946 if(!Exit_h_head)
2947 {
2948 ENABLE_AST;
2949 return(0);
2950 }
2951 while( (exitp = (EXIT_H *) sll_search_next_remove((SLL *) Exit_h_head,
2952 0, (char *) &conn_id, 4)) )
2953 {
2954 (Client_exit_user_routine)( &exitp->exit_id );
2955 free(exitp);
2956 }
2957 ENABLE_AST
2958 return(1);
2959}
2960
2961static void exit_handler(int *tag, int *bufp, int *size)
2962{
2963
2964 if(size){}
2965 if(tag){}
2966 if(Exit_user_routine)
2967 (Exit_user_routine)( bufp );
2968 else
2969 {
2970/*
2971 printf("%s PID %d Exiting!\n", Task_name, getpid());
2972*/
2973 exit(*bufp);
2974 }
2975}
2976
2977static void error_handler(int conn_id, int severity, int errcode, char *reason)
2978{
2979 int exit_tag, exit_code, exit_size;
2980 int last_conn_id;
2981
2982 if(Error_user_routine)
2983 {
2984 Error_conn_id = conn_id;
2985 last_conn_id = Curr_conn_id;
2986 Curr_conn_id = conn_id;
2987 (Error_user_routine)( severity, errcode, reason);
2988 Error_conn_id = 0;
2989 Curr_conn_id = last_conn_id;
2990 }
2991 else
2992 {
2993 dim_print_msg(reason, severity);
2994 }
2995 if(severity == DIM_FATAL)
2996 {
2997 exit_tag = 0;
2998 exit_code = errcode;
2999 exit_size = sizeof(int);
3000 exit_handler(&exit_tag, &exit_code, &exit_size);
3001 }
3002}
3003/*
3004#define MAX_HASH_ENTRIES 2000
3005*/
3006#define MAX_HASH_ENTRIES 5000
3007
3008static SERVICE *Service_hash_table[MAX_HASH_ENTRIES];
3009static int Service_new_entries[MAX_HASH_ENTRIES];
3010
3011int dis_hash_service_init()
3012{
3013 int i;
3014 static int done = 0;
3015
3016 if(!done)
3017 {
3018 for( i = 0; i < MAX_HASH_ENTRIES; i++ )
3019 {
3020 Service_hash_table[i] = (SERVICE *) malloc(8);
3021 dll_init((DLL *) Service_hash_table[i]);
3022 Service_new_entries[i] = 0;
3023 }
3024 done = 1;
3025 }
3026 return(1);
3027}
3028
3029int dis_hash_service_insert(SERVICE *servp)
3030{
3031 int index;
3032 index = HashFunction(servp->name, MAX_HASH_ENTRIES);
3033 Service_new_entries[index]++;
3034 dll_insert_queue((DLL *) Service_hash_table[index],
3035 (DLL *) servp);
3036 return(1);
3037}
3038
3039int dis_hash_service_registered(int index, SERVICE *servp)
3040{
3041 servp->registered = 1;
3042 Service_new_entries[index]--;
3043 if(Service_new_entries[index] < 0)
3044 Service_new_entries[index] = 0;
3045 return 1;
3046}
3047
3048int dis_hash_service_remove(SERVICE *servp)
3049{
3050 dll_remove( (DLL *) servp );
3051 return(1);
3052}
3053
3054
3055SERVICE *dis_hash_service_exists(char *name)
3056{
3057 int index;
3058 SERVICE *servp;
3059
3060 index = HashFunction(name, MAX_HASH_ENTRIES);
3061 if( (servp = (SERVICE *) dll_search(
3062 (DLL *) Service_hash_table[index],
3063 name, strlen(name)+1)) )
3064 {
3065 return(servp);
3066 }
3067 return((SERVICE *)0);
3068}
3069
3070SERVICE *dis_hash_service_get_next(int *curr_index, SERVICE *prevp, int new_entries)
3071{
3072 int index;
3073 SERVICE *servp = 0;
3074/*
3075 if(!prevp)
3076 {
3077 index = -1;
3078 }
3079*/
3080 index = *curr_index;
3081 if(index == -1)
3082 {
3083 index++;
3084 prevp = Service_hash_table[index];
3085 }
3086 if(!prevp)
3087 {
3088 prevp = Service_hash_table[index];
3089 }
3090 do
3091 {
3092 if((!new_entries) || (Service_new_entries[index] > 0))
3093 {
3094 servp = (SERVICE *) dll_get_next(
3095 (DLL *) Service_hash_table[index],
3096 (DLL *) prevp);
3097 if(servp)
3098 break;
3099 }
3100 index++;
3101 if(index == MAX_HASH_ENTRIES)
3102 {
3103 *curr_index = -1;
3104 return((SERVICE *) 0);
3105 }
3106 prevp = Service_hash_table[index];
3107 } while(!servp);
3108 *curr_index = index;
3109 return(servp);
3110}
3111
3112DIS_DNS_CONN *dis_find_dns(long dnsid)
3113{
3114 DIS_DNS_CONN *dnsp;
3115
3116 dnsp = (DIS_DNS_CONN *)
3117 dll_search( (DLL *) DNS_head, &dnsid, sizeof(dnsid));
3118/*
3119 if(!dnsp)
3120 {
3121 dnsp = create_dns(dnsid);
3122 }
3123*/
3124 return dnsp;
3125}
3126
3127int dis_no_dns()
3128{
3129 DIS_DNS_CONN *dnsp;
3130
3131 dnsp = (DIS_DNS_CONN *) DNS_head;
3132 while ( (dnsp = (DIS_DNS_CONN *) dll_get_next( (DLL *) DNS_head, (DLL *) dnsp)))
3133 {
3134/*
3135 if(dnsp != Default_DNS)
3136 return 0;
3137*/
3138 if(dnsp->serving)
3139 return 0;
3140 }
3141 return 1;
3142}
3143
3144DIS_DNS_CONN *find_dns_by_conn_id(int conn_id)
3145{
3146 DIS_DNS_CONN *dnsp;
3147 extern long dns_get_dnsid();
3148 long dnsid;
3149
3150 dnsid = dns_get_dnsid(conn_id, SRC_DIS);
3151 dnsp = dis_find_dns(dnsid);
3152 if(!dnsp)
3153 dnsp = Default_DNS;
3154 return (DIS_DNS_CONN *)dnsp;
3155}
3156
3157void dis_print_hash_table()
3158{
3159 SERVICE *servp;
3160 int i;
3161 int n_entries, max_entry_index = 0;
3162 int max_entries = 0;
3163
3164 for( i = 0; i < MAX_HASH_ENTRIES; i++ )
3165 {
3166 n_entries = 0;
3167 servp = Service_hash_table[i];
3168 while( (servp = (SERVICE *) dll_get_next(
3169 (DLL *) Service_hash_table[i],
3170 (DLL *) servp)) )
3171 {
3172 n_entries++;
3173 if(n_entries == 1)
3174 printf(" Name = %s\n",servp->name);
3175 }
3176 if(n_entries != 0)
3177 printf("HASH[%d] - %d entries\n", i, n_entries);
3178 if(n_entries > max_entries)
3179 {
3180 max_entries = n_entries;
3181 max_entry_index = i;
3182 }
3183 }
3184 printf("Maximum : HASH[%d] - %d entries\n", max_entry_index, max_entries);
3185 fflush(stdout);
3186}
3187
3188void dis_hash_print()
3189{
3190 SERVICE *servp;
3191 int hash_index;
3192
3193 servp = 0;
3194 hash_index = -1;
3195 while( (servp = dis_hash_service_get_next(&hash_index, servp, 0)) )
3196 {
3197 printf("Name = %s\n",servp->name);
3198 }
3199}
3200
3201#ifdef VMS
3202/* CFORTRAN WRAPPERS */
3203FCALLSCFUN1(INT, dis_start_serving, DIS_START_SERVING, dis_start_serving,
3204 STRING)
3205FCALLSCFUN3(INT, dis_get_next_cmnd, DIS_GET_NEXT_CMND, dis_get_next_cmnd,
3206 PINT, PVOID, PINT)
3207FCALLSCFUN1(INT, dis_get_client, DIS_GET_CLIENT, dis_get_client,
3208 PSTRING)
3209FCALLSCFUN6(INT, dis_add_service, DIS_ADD_SERVICE, dis_add_service,
3210 STRING, PVOID, PVOID, INT, PVOID, INT)
3211FCALLSCSUB4( dis_add_cmnd, DIS_ADD_CMND, dis_add_cmnd,
3212 STRING, PVOID, PVOID, INT)
3213FCALLSCSUB1( dis_add_client_exit_handler, DIS_ADD_CLIENT_EXIT_HANDLER,
3214 dis_add_client_exit_handler,
3215 PVOID)
3216FCALLSCSUB2( dis_set_client_exit_handler, DIS_SET_CLIENT_EXIT_HANDLER,
3217 dis_set_client_exit_handler,
3218 INT, INT)
3219FCALLSCSUB1( dis_add_exit_handler, DIS_ADD_EXIT_HANDLER,
3220 dis_add_exit_handler,
3221 PVOID)
3222FCALLSCSUB1( dis_report_service, DIS_REPORT_SERVICE, dis_report_service,
3223 STRING)
3224FCALLSCSUB2( dis_convert_str, DIS_CONVERT_STR, dis_convert_str,
3225 PVOID, PVOID)
3226FCALLSCFUN1(INT, dis_update_service, DIS_UPDATE_SERVICE, dis_update_service,
3227 INT)
3228FCALLSCFUN1(INT, dis_remove_service, DIS_REMOVE_SERVICE, dis_remove_service,
3229 INT)
3230FCALLSCSUB3( dis_send_service, DIS_SEND_SERVICE, dis_send_service,
3231 INT, PVOID, INT)
3232FCALLSCSUB2( dis_set_quality, DIS_SET_QUALITY, dis_set_quality,
3233 INT, INT)
3234FCALLSCSUB3(INT, dis_set_timestamp, DIS_SET_TIMESTAMP, dis_set_timestamp,
3235 INT, INT, INT)
3236FCALLSCFUN2(INT, dis_selective_update_service, DIS_SELECTIVE_UPDATE_SERVICE,
3237 dis_selective_update_service,
3238 INT, PINT)
3239FCALLSCSUB3(INT, dis_get_timestamp, DIS_GET_TIMESTAMP, dis_get_timestamp,
3240 INT, PINT, PINT)
3241#endif
Note: See TracBrowser for help on using the repository browser.