source: trunk/FACT++/dim/src/util/dimbridge.cxx@ 20115

Last change on this file since 20115 was 19184, checked in by tbretz, 6 years ago
Updated to v20r23
File size: 30.0 KB
Line 
1#include <dic.hxx>
2#include <dis.hxx>
3#include <dim.h>
4#include <iostream>
5using namespace std;
6
7static int no_link = 0xdeaddead;
8static char from_node[128], to_node[128], bridge_name[128];
9static int from_port, to_port;
10static char *services = 0, *eservices = 0;
11static char *sstrings = 0, *rstrings = 0, *myown = 0;
12int n_replaces = 0;
13int max_replaces = 10;
14int poll_interval = 30;
15
16void get_srv_name_pub(char *srv_name, char *srv_name_pub)
17{
18 char *p1, *p2, *ptrs, *ptrr;
19 int i;
20
21 strcpy(srv_name_pub, srv_name);
22 if (n_replaces)
23 {
24 ptrs = sstrings;
25 ptrr = rstrings;
26 for (i = 0; i < n_replaces; i++)
27 {
28 p1 = strstr(srv_name_pub, ptrs);
29 if (p1)
30 {
31 p2 = srv_name + (p1 - srv_name_pub);
32 p2 += strlen(ptrs);
33 *p1 = '\0';
34 strcat(p1, ptrr);
35 strcat(p1, p2);
36 }
37 ptrs += strlen(ptrs) + 1;
38 ptrr += strlen(ptrr) + 1;
39 }
40 }
41}
42
43class BridgeService: public DimStampedInfo
44{
45 char srv_name[256];
46 char srv_name_pub[256];
47 char srv_format[256];
48 int declared;
49 DimService *srv;
50 void *srv_data, *data;
51 int srv_size;
52 int cmnd;
53 int found;
54 int copyFlag;
55 time_t timestamp;
56 int milliseconds;
57 int quality;
58
59 void infoHandler() {
60 char *server;
61 data = DimStampedInfo::getData();
62 srv_size = DimStampedInfo::getSize();
63 timestamp = getTimestamp();
64 milliseconds = getTimestampMillisecs();
65 quality = getQuality();
66// Sometimes gets a packet from DIS_DNS not understood why!!!"
67 server = DimClient::getServerName();
68 if(strstr(server,"DIS_DNS") != 0)
69 {
70//dim_print_date_time();
71//cout << "received from " << server << " size = " << srv_size << endl;
72 if(strstr(srv_name,"DIS_DNS") == 0)
73 return;
74 }
75 srv_data = data;
76 if(*(int *)srv_data == no_link)
77 {
78 if(srv)
79 {
80// dim_print_date_time();
81// cout << "Deleting Service (no link)" << srv_name << endl;
82 delete(srv);
83 srv = 0;
84 }
85 declared = 0;
86// cout << "Disconnecting bridge for: " << srv_name << endl;
87 poll_interval = 5;
88 }
89 else if(!declared)
90 {
91// DimServer::setDnsNode(to_node);
92 srv = new DimService(srv_name_pub, srv_format, srv_data, srv_size);
93 if(copyFlag)
94 srv->setData(srv_data, srv_size);
95 srv->setQuality(quality);
96 srv->setTimestamp(timestamp, milliseconds);
97 DimServer::start(bridge_name);
98 declared = 1;
99// DimClient::setDnsNode(from_node);
100 }
101 else
102 {
103 if(srv)
104 {
105 srv->setQuality(quality);
106 srv->setTimestamp(timestamp, milliseconds);
107 if (copyFlag)
108 {
109 srv->setData(srv_data, srv_size);
110 srv->updateService();
111 }
112 else
113 {
114 srv->updateService(srv_data, srv_size);
115 }
116 }
117 }
118 }
119
120 public:
121 BridgeService(char *name, char *format, int copy):
122 DimStampedInfo(name, &no_link, sizeof(no_link)), declared(0), srv(0), copyFlag(copy)
123 {
124 strcpy(srv_format, format);
125 strcpy(srv_name, name);
126 get_srv_name_pub(srv_name, srv_name_pub);
127 cmnd = 0;
128 found = 1;
129// cout << "Bridging Service: " << name << endl;
130 }
131 BridgeService(char *name, char *format, int rate, int copy):
132 DimStampedInfo(name, rate, &no_link, sizeof(no_link)), declared(0), srv(0), copyFlag(copy)
133 {
134 strcpy(srv_format, format);
135 strcpy(srv_name, name);
136 get_srv_name_pub(srv_name, srv_name_pub);
137 cmnd = 0;
138 found = 1;
139// cout << "Bridging Service: " << name << ", rate = " << rate << " seconds" << endl;
140 }
141 ~BridgeService()
142 {
143 if(declared)
144 {
145 if(srv)
146 {
147 delete(srv);
148 srv = 0;
149 }
150 }
151 declared = 0;
152// cout << "Stopped bridge for: " << srv_name << endl;
153 }
154 char *getName() { return srv_name; };
155 char *getNamePub() { return srv_name_pub; };
156 void clear() { found = 0; };
157 void set() {found = 1;};
158 int find() {return found;};
159 int isCmnd() { return cmnd;};
160};
161
162class BridgeCommand: public DimCommandHandler
163{
164 char srv_name[256];
165 char srv_name_pub[256];
166 char srv_format[256];
167 int declared;
168// DimService *srv;
169 DimCommand *srv;
170 void *srv_data;
171 int srv_size;
172 int cmnd;
173 int found;
174
175 void commandHandler() {
176// srv_data = DimCommand::getData();
177// srv_size = DimCommand::getSize();
178 srv_data = srv->getData();
179 srv_size = srv->getSize();
180 DimClient::sendCommandNB(srv_name, srv_data, srv_size);
181 }
182
183 public:
184 BridgeCommand(char *name, char *format)/*:*/
185// DimCommand(name, format)
186 {
187 cmnd = 1;
188 found = 1;
189 strcpy(srv_name, name);
190 get_srv_name_pub(srv_name, srv_name_pub);
191 srv = new DimCommand(srv_name_pub, format, this);
192 DimServer::start(bridge_name);
193 // cout << "Bridging Command: " << name << endl;
194 }
195 ~BridgeCommand()
196 {
197// if (declared)
198// {
199 if (srv)
200 {
201// dim_print_date_time();
202// cout << "Deleting Command " << srv_name << endl;
203 delete(srv);
204 srv = 0;
205 }
206// }
207// declared = 0;
208 // cout << "Stopped bridge for: " << srv_name << endl;
209 }
210 char *getName() { return srv_name; };
211 char *getNamePub() { return srv_name_pub; };
212 void clear() { found = 0; };
213 void set() {found = 1;};
214 int find() {return found;};
215 int isCmnd() { return cmnd;};
216};
217
218void print_usage_old()
219{
220 cout << "Usage: DimBridge [from_node] to_node services [time_interval] [-copy]" << endl;
221 cout << " from_node - by default DIM_DNS_NODE" << endl;
222 cout << " to_node - the complete node name of the new DNS" << endl;
223 cout << " services - the list of service names (wildcards allowed)" << endl;
224 cout << " time_interval - the interval in seconds to be used for updating the services" << endl;
225 cout << " -copy - copy internally the service data" << endl;
226}
227
228void print_usage()
229{
230 cout << "Usage: DimBridge [-f(rom) <from_node>[:<port_number>]] -t(o) <to_node>[:<port_number>] -s <services> [-e <services>] [-i(nterval) <time_interval>] [-c(opy)] ([-r(replace) <search_string> <replace_string>])* " << endl;
231 cout << " -f, -from\t\tfrom_node - the DNS node name (optionally a port number), by default DIM_DNS_NODE (DIM_DNS_PORT)" << endl;
232 cout << " -t, -to\t\tto_node - the complete node name of the new DNS (optionally a port number)" << endl;
233 cout << " -s, -services\tservices - the list of service names to bridge as a comma separated list of patterns" << endl;
234 cout << " -e, -exclude\tservices - a list of service names to be excluded as a comma separated list of patterns" << endl;
235 cout << " -i, -interval\ttime_interval - the interval in seconds to be used for updating the services" << endl;
236 cout << " -c, -copy\t\tcopy internally the service data" << endl;
237 cout << " -r, -replace\tsearch_string, replace_string - replace a part of the service name when bridging, can be repeated" << endl;
238}
239
240int parse_cmd_line(int argc, char **argv, int *rate, int *copyFlag)
241{
242 int index = 1;
243 char *ptrs, *ptrr;
244 int size;
245 char *ptr, *ptr1;
246 int i;
247
248 strcpy(from_node, DimClient::getDnsNode());
249 strcpy(to_node, "");
250 n_replaces = 0;
251 *rate = 0;
252 *copyFlag = 0;
253 if (argc < 2)
254 return 0;
255 if (argv[1][0] != '-') // old style command line
256 return -1;
257 while (index < argc)
258 {
259//cout << "arg" << index << " " << argv[index] << endl;
260 if (argv[index][0] != '-') // bad command line
261 return 0;
262 if (argv[index][1] == 'f')
263 {
264 index++;
265 if (index >= argc)
266 return 0;
267 strcpy(from_node, argv[index]);
268 }
269 else if (argv[index][1] == 't')
270 {
271 index++;
272 if (index >= argc)
273 return 0;
274 strcpy(to_node, argv[index]);
275 }
276 else if (argv[index][1] == 's')
277 {
278 index++;
279 if (index >= argc)
280 return 0;
281 services = (char *)malloc(strlen(argv[index])+1);
282 strcpy(services, argv[index]);
283 }
284 else if (argv[index][1] == 'e')
285 {
286 index++;
287 if (index >= argc)
288 return 0;
289 eservices = (char *)malloc(strlen(argv[index]) + 1);
290 strcpy(eservices, argv[index]);
291 }
292 else if (argv[index][1] == 'i')
293 {
294 index++;
295 if (index >= argc)
296 return 0;
297 sscanf(argv[index], "%d", rate);
298 }
299 else if (argv[index][1] == 'c')
300 {
301 *copyFlag = 1;
302 }
303 else if (argv[index][1] == 'r')
304 {
305 if (n_replaces == 0)
306 {
307 sstrings = (char *)malloc(max_replaces * MAX_NAME);
308 rstrings = (char *)malloc(max_replaces * MAX_NAME);
309 strcpy(sstrings, "");
310 strcpy(rstrings, "");
311 ptrs = sstrings;
312 ptrr = rstrings;
313 }
314 index++;
315 if (index >= argc)
316 return 0;
317 strcpy(ptrs, argv[index]);
318 index++;
319 if (index >= argc)
320 return 0;
321 strcpy(ptrr, argv[index]);
322 ptrs += strlen(ptrs) + 1;
323 ptrr += strlen(ptrr) + 1;
324 n_replaces++;
325 if (n_replaces == max_replaces)
326 {
327 max_replaces += 10;
328 sstrings = (char *)realloc(sstrings, max_replaces * MAX_NAME);
329 rstrings = (char *)realloc(rstrings, max_replaces * MAX_NAME);
330 }
331 }
332 index++;
333 }
334 if (n_replaces)
335 {
336 size = n_replaces * MAX_NAME;
337 myown = (char *)malloc(size);
338 ptr = myown;
339 ptr1 = rstrings;
340 for (i = 0; i < (n_replaces - 1); i++)
341 {
342 strcpy(ptr, "*");
343 strcat(ptr, ptr1);
344 strcat(ptr, "*,");
345 ptr += strlen(ptr);
346 ptr1 += strlen(ptr1) + 1;
347 }
348 strcpy(ptr, "*");
349 strcat(ptr, ptr1);
350 strcat(ptr, "*");
351 }
352 if (!services)
353 return 0;
354 if ((to_node[0] == '\0') || (services[0] == '\0'))
355 return 0;
356 return 1;
357}
358
359int parse_cmd_line_old(int argc, char **argv, int *rate, int *copyFlag)
360{
361 if (argc < 3)
362 {
363 return 0;
364 }
365 else if (argc == 3)
366 {
367 strcpy(from_node, DimClient::getDnsNode());
368 strcpy(to_node, argv[1]);
369 services = (char *)malloc(strlen(argv[2]) + 1);
370 strcpy(services, argv[2]);
371 }
372 else if (argc == 4)
373 {
374 // if(sscanf(argv[3],"%d", &rate))
375 if (isdigit(argv[3][0]))
376 {
377 sscanf(argv[3], "%d", rate);
378 strcpy(from_node, DimClient::getDnsNode());
379 strcpy(to_node, argv[1]);
380 services = (char *)malloc(strlen(argv[2]) + 1);
381 strcpy(services, argv[2]);
382 }
383 else if (argv[3][0] == '-')
384 {
385 *rate = 0;
386 strcpy(from_node, DimClient::getDnsNode());
387 strcpy(to_node, argv[1]);
388 services = (char *)malloc(strlen(argv[2]) + 1);
389 strcpy(services, argv[2]);
390 *copyFlag = 1;
391 }
392 else
393 {
394 *rate = 0;
395 strcpy(from_node, argv[1]);
396 strcpy(to_node, argv[2]);
397 services = (char *)malloc(strlen(argv[3]) + 1);
398 strcpy(services, argv[3]);
399 }
400 }
401 else if (argc == 5)
402 {
403 // if(sscanf(argv[4],"%d", &rate))
404 if (isdigit(argv[4][0]))
405 {
406 sscanf(argv[4], "%d", rate);
407 strcpy(from_node, argv[1]);
408 strcpy(to_node, argv[2]);
409 services = (char *)malloc(strlen(argv[3]) + 1);
410 strcpy(services, argv[3]);
411 }
412 else if (argv[4][0] == '-')
413 {
414 *copyFlag = 1;
415 // if(sscanf(argv[3],"%d", &rate))
416 if (isdigit(argv[3][0]))
417 {
418 sscanf(argv[3], "%d", rate);
419 strcpy(from_node, DimClient::getDnsNode());
420 strcpy(to_node, argv[1]);
421 services = (char *)malloc(strlen(argv[2]) + 1);
422 strcpy(services, argv[2]);
423 }
424 else
425 {
426 *rate = 0;
427 strcpy(from_node, argv[1]);
428 strcpy(to_node, argv[2]);
429 services = (char *)malloc(strlen(argv[3]) + 1);
430 strcpy(services, argv[3]);
431 }
432 }
433 }
434 else if (argc == 6)
435 {
436 strcpy(from_node, argv[1]);
437 strcpy(to_node, argv[2]);
438 services = (char *)malloc(strlen(argv[3]) + 1);
439 strcpy(services, argv[3]);
440 sscanf(argv[4], "%d", rate);
441 *copyFlag = 1;
442 }
443 else
444 {
445 return 0;
446 }
447 return 1;
448}
449
450char ServerList_no_link[10] = "";
451char ServiceList_no_link[10] = "";
452
453typedef struct serv {
454 struct serv *next;
455 struct serv *prev;
456 char name[MAX_NAME];
457 int type;
458 char format[MAX_NAME];
459 int registered;
460 void *serverPtr;
461 void *browserServicePtr;
462} SERVICE;
463
464int multiple_match(const char*, const char*, int case_sensitive);
465int bridge_hash_service_init();
466int bridge_hash_service_insert(SERVICE *);
467int bridge_hash_service_registered(int, SERVICE *);
468int bridge_hash_service_remove(SERVICE *);
469SERVICE *bridge_hash_service_exists(char *);
470SERVICE *bridge_hash_service_get_next(int *, SERVICE *, int);
471int bridge_hash_service_remove_many(int);
472/*
473class ServerInfo : public DimInfo, public SLLItem
474{
475 char name[256];
476
477 void infoHandler() {
478 char *data, *list, *format;
479 char *ptr, *ptr1, *ptr2, *ptr3;
480 int mode = 0, type;
481 SERVICE *servp;
482
483 data = DimInfo::getString();
484// cout << "***** Received from Server : " << data << endl;
485 list = new char[(int)strlen(data) + 1];
486 strcpy(list, data);
487 ptr = list;
488 if (*ptr == '\0')
489 {
490 }
491 else if (*ptr == '+')
492 {
493 mode = 1;
494 ptr++;
495 }
496 else if (*ptr == '-')
497 {
498 mode = -1;
499 ptr++;
500 }
501 else
502 {
503 mode = 1;
504 }
505 while (*ptr)
506 {
507 if ((ptr1 = strchr(ptr, '\n')))
508 {
509 *ptr1 = '\0';
510 ptr1++;
511 }
512 else
513 {
514 ptr1 = ptr;
515 ptr1 += strlen(ptr);
516 }
517 if ((ptr2 = strchr(ptr, '|')))
518 {
519 *ptr2 = '\0';
520 ptr2++;
521 if ((ptr3 = strchr(ptr2, '|')))
522 {
523 *ptr3 = '\0';
524 format = ptr2;
525 ptr3++;
526 if (*ptr3 == '\0')
527 type = DimSERVICE;
528 else if (*ptr3 == 'C')
529 type = DimCOMMAND;
530 else if (*ptr3 == 'R')
531 type = DimRPC;
532 }
533// cout << "***** Checking Service " << ptr << endl;
534 if (!strcmp(ptr, bridge_name))
535 {
536 ptr = ptr1;
537 continue;
538 }
539 if (!strcmp(ptr, "DIS_DNS"))
540 {
541 ptr = ptr1;
542 continue;
543 }
544 if (mode == 1)
545 {
546 if (type != DimRPC)
547 {
548 if (multiple_match(ptr, services, 1))
549 {
550 poll_interval = 3;
551 servp = (SERVICE *)malloc(sizeof(SERVICE));
552 strncpy(servp->name, ptr, (size_t)MAX_NAME);
553 servp->type = type;
554 strcpy(servp->format, format);
555 servp->serverPtr = (void *)this;
556 servp->registered = 0;
557 bridge_hash_service_insert(servp);
558 }
559 }
560 }
561 else if (mode == -1)
562 {
563 poll_interval = 3;
564 servp = bridge_hash_service_exists(ptr);
565 if (servp)
566 {
567// bridge_hash_service_remove(servp);
568// free(servp);
569 servp->registered = -1;
570 }
571 }
572 }
573 ptr = ptr1;
574 }
575 }
576public:
577 ServerInfo(char *server_name, char *service_name) :
578 DimInfo(service_name, ServiceList_no_link)
579 {
580 strcpy(name, server_name);
581 }
582 ~ServerInfo()
583 {
584 SERVICE *servp;
585 int hash_index;
586
587 hash_index = -1;
588 servp = 0;
589 while ((servp = bridge_hash_service_get_next(&hash_index, servp, 0)))
590 {
591 if (servp->serverPtr == (void *)this)
592 {
593 servp->registered = -1;
594 }
595 }
596 }
597 char *getName(){ return name; }
598};
599*/
600//int GotInitialList = 0;
601
602
603class ServerList : public DimInfo
604{
605// ServerInfo *srv, *aux_srv;
606 char name[256], srv_name[256];
607 SLList servers;
608// char *serverList;
609// int serverListSize;
610
611 void infoHandler() {
612 char *data;
613// int size;
614// char *ptr;
615
616 data = DimInfo::getString();
617// dim_print_date_time();
618// cout << "***** Received from DNS " << data << endl;
619/*
620 size = (int)strlen(data) + 1;
621 if (!serverListSize)
622 {
623 serverList = new char[size];
624 }
625 else if (size > serverListSize)
626 {
627 delete serverList;
628 serverList = new char[size];
629 serverListSize = size;
630 }
631 strcpy(serverList, data);
632 if ((serverList[0] == '+') || (serverList[0] == '-'))
633*/
634 if ((data[0] == '+') || (data[0] == '-'))
635 {
636 poll_interval = 3;
637// parseServerList();
638 }
639 else if (data[0] == '\0')
640 {
641 poll_interval = 0;
642 dim_print_date_time();
643 cout << "Disconnected from " << DimClient::getServerName() << endl;
644 }
645 else
646 {
647 poll_interval = 30;
648 dim_print_date_time();
649 cout << "Connected to " << DimClient::getServerName() << endl;
650 }
651// GotInitialList = 1;
652 }
653
654public:
655 ServerList(char *name) :
656 DimInfo(name, ServerList_no_link)
657 {
658 }
659 ~ServerList()
660 {
661 }
662// int parseServerList();
663};
664/*
665int ServerList::parseServerList()
666{
667 char *ptr, *ptr1, *ptr2;
668 int mode = 0;
669
670 ptr = serverList;
671 if (*ptr == '\0')
672 {
673 }
674 else if (*ptr == '+')
675 {
676 mode = 1;
677 ptr++;
678 }
679 else if (*ptr == '-')
680 {
681 mode = -1;
682 ptr++;
683 }
684 else
685 {
686 mode = 1;
687 }
688 while (*ptr)
689 {
690 if ((ptr1 = strchr(ptr, '|')))
691 {
692 *ptr1 = '\0';
693 ptr1++;
694 }
695 else
696 {
697 ptr1 = ptr;
698 ptr1 += strlen(ptr);
699 }
700 if ((ptr2 = strchr(ptr, '@')))
701 {
702 *ptr2 = '\0';
703// dim_print_date_time();
704// cout << "***** Checking Server " << ptr << endl;
705 if (!strcmp(ptr, bridge_name))
706 {
707 ptr = ptr1;
708 continue;
709 }
710 if (!strcmp(ptr, "DIS_DNS"))
711 {
712 ptr = ptr1;
713 continue;
714 }
715 if (mode == 1)
716 {
717 strcpy(srv_name, ptr);
718 strcat(srv_name, "/SERVICE_LIST");
719 strcpy(name, ptr);
720 dim_print_date_time();
721 cout << "***** Subscribing Server " << ptr << " " << srv_name << endl;
722 srv = new ServerInfo(name, srv_name);
723 servers.add(srv);
724 }
725 else if (mode == -1)
726 {
727 srv = (ServerInfo *)servers.getHead();
728 while (srv)
729 {
730 aux_srv = 0;
731 // cout << "Checking remove " << ptr << " " << srv->getName() << endl;
732 if (!(strcmp(srv->getName(), ptr)))
733 {
734 dim_print_date_time();
735 cout << "***** Removing Server " << srv->getName() << endl;
736 servers.remove(srv);
737 aux_srv = srv;
738 break;
739 }
740 srv = (ServerInfo *)servers.getNext();
741 }
742 if (aux_srv)
743 {
744 delete aux_srv;
745 }
746 }
747 }
748 ptr = ptr1;
749 }
750 return 1;
751}
752*/
753int checkBridgeServices()
754{
755 SERVICE *servp;
756 int hash_index;
757
758 servp = 0;
759 hash_index = -1;
760 // dbr.getServices(services);
761 // while ((type = dbr.getNextService(service, format)))
762 while ((servp = bridge_hash_service_get_next(&hash_index, servp, 0)))
763 {
764// if (servp->registered == 2)
765 servp->registered = 1;
766 }
767 return 1;
768}
769
770int doBridgeService(char *service, char *format, int type)
771{
772 SERVICE *servp;
773 int ret = 0;
774
775 if (!(servp = bridge_hash_service_exists(service)))
776 {
777 if (multiple_match(service, services, 1))
778 {
779 if ((multiple_match(service, myown, 1)) ||
780 (multiple_match(service, eservices, 1)))
781 {
782// poll_interval = 3;
783 servp = (SERVICE *)malloc(sizeof(SERVICE));
784 strncpy(servp->name, service, (size_t)MAX_NAME);
785 servp->type = type;
786 strcpy(servp->format, format);
787// servp->serverPtr = (void *)this;
788 servp->registered = 2;
789 servp->browserServicePtr = (void *)0;
790 bridge_hash_service_insert(servp);
791// cout << "Scanning " << service << "registered " << servp->registered << endl;
792 ret = 1;
793 }
794 else
795 {
796 poll_interval = 3;
797 servp = (SERVICE *)malloc(sizeof(SERVICE));
798 strncpy(servp->name, service, (size_t)MAX_NAME);
799 servp->type = type;
800 strcpy(servp->format, format);
801 servp->browserServicePtr = (void *)0;
802 servp->registered = 0;
803 bridge_hash_service_insert(servp);
804 // cout << "Scanning " << service << " registered " << servp->registered << endl;
805 ret = 1;
806 }
807 }
808 }
809 else
810 {
811 servp->registered = 2;
812// cout << "Scanning " << service << "registered " << servp->registered << endl;
813 ret = 2;
814 }
815 return ret;
816}
817
818int dontBridgeServices()
819{
820 SERVICE *servp;
821 int hash_index;
822
823 servp = 0;
824 hash_index = -1;
825 // dbr.getServices(services);
826 // while ((type = dbr.getNextService(service, format)))
827 while ((servp = bridge_hash_service_get_next(&hash_index, servp, 0)))
828 {
829 if (servp->registered == 1)
830 servp->registered = -1;
831 }
832 return 1;
833}
834
835int main(int argc, char **argv)
836{
837DimBrowser dbr;
838char *service, *format, *p;
839int type, known;
840BridgeService *ptrs;
841BridgeCommand *ptrc;
842int rate = 0;
843int copyFlag = 0;
844int i, ret, done;
845ServerList *serverList;
846SERVICE *servp;
847int hash_index;
848char *sptr, *rptr;
849
850//dic_set_debug_on();
851// cout << "n pars " << argc << endl;
852// for (i = 0; i < argc; i++)
853// cout << "arg " << i << " " << argv[i] << endl;
854 bridge_hash_service_init();
855 ret = parse_cmd_line(argc, argv, &rate, &copyFlag);
856 if (ret == -1)
857 {
858 ret = parse_cmd_line_old(argc, argv, &rate, &copyFlag);
859 if (!ret)
860 {
861 print_usage_old();
862 return 0;
863 }
864 }
865 else if (!ret)
866 {
867 print_usage();
868 return 0;
869 }
870 cout << "Starting DimBridge from " << from_node << " to " << to_node << endl;
871 cout << "for " << services << endl;
872 if (eservices)
873 {
874 cout << "excluding " << eservices << endl;
875 }
876 if (n_replaces)
877 {
878 sptr = sstrings;
879 rptr = rstrings;
880 for (i = 0; i < n_replaces; i++)
881 {
882 cout << "replacing " << sptr << " by " << rptr << endl;
883 sptr += strlen(sptr) + 1;
884 rptr += strlen(rptr) + 1;
885 }
886 }
887 if ((rate) || (copyFlag))
888 cout << "option(s):";
889 if (rate)
890 cout << " update interval=" << rate;
891 if ((rate) && (copyFlag))
892 cout << ",";
893 if (copyFlag)
894 cout << " internal data copy";
895 if ((rate) || (copyFlag))
896 cout << endl;
897
898 strcpy(bridge_name,"Bridge_");
899 strcat(bridge_name, from_node);
900 if ((p = strchr(bridge_name, ':')))
901 *p = '\0';
902 if ((p = strchr(bridge_name, ',')))
903 *p = '\0';
904 if ((p = strchr(bridge_name, '.')))
905 *p = '\0';
906/*
907 strcat(bridge_name, "_");
908 strcat(bridge_name, to_node);
909 if ((p = strchr(bridge_name, ':')))
910 *p = '\0';
911 if ((p = strchr(bridge_name, ',')))
912 *p = '\0';
913 if ((p = strchr(bridge_name, '.')))
914 *p = '\0';
915*/
916 p = bridge_name + strlen(bridge_name);
917#ifndef WIN32
918 sprintf(p,"_%d",getpid());
919#else
920 sprintf(p,"_%d",_getpid());
921#endif
922 from_port = 0;
923 to_port = 0;
924 if ((p = strchr(from_node, ':')))
925 {
926 *p = '\0';
927 p++;
928 sscanf(p, "%d", &from_port);
929 }
930 if ((p = strchr(to_node, ':')))
931 {
932 *p = '\0';
933 p++;
934 sscanf(p, "%d", &to_port);
935 }
936// cout << "Starting1 DimBridge from " << from_node << ":" << from_port <<" to " << to_node << ":" << to_port <<" for " << services << endl;
937 if (!from_port)
938 DimClient::setDnsNode(from_node);
939 else
940 DimClient::setDnsNode(from_node, from_port);
941 if(!to_port)
942 DimServer::setDnsNode(to_node);
943 else
944 DimServer::setDnsNode(to_node, to_port);
945 serverList = new ServerList((char *)"DIS_DNS/SERVER_LIST");
946 if (serverList){} // to avoid a warning
947 // while (!GotInitialList)
948// sleep(1);
949// serverList->parseServerList();
950
951 while (1)
952 {
953 done = 0;
954 checkBridgeServices();
955 dbr.getServices("*");
956 while ((type = dbr.getNextService(service, format)))
957 {
958 doBridgeService(service, format, type);
959 }
960 dontBridgeServices();
961 servp = 0;
962 hash_index = -1;
963 while ((servp = bridge_hash_service_get_next(&hash_index, servp, 0)))
964 {
965 service = servp->name;
966 type = servp->type;
967 format = servp->format;
968// dim_print_date_time();
969// cout << "Checking " << service << " reg "<< servp->registered << " type " << type << endl;
970 known = servp->registered;
971 if (known == 0)
972 {
973 done = 1;
974 if(type == DimSERVICE)
975 {
976 dim_print_date_time();
977 cout << "Start Bridging Service: " << service << endl;
978 if (!rate)
979 ptrs = new BridgeService(service, format, copyFlag);
980 else
981 ptrs = new BridgeService(service, format, rate, copyFlag);
982 servp->browserServicePtr = (void *)ptrs;
983 }
984 else if (type == DimCOMMAND)
985 {
986 dim_print_date_time();
987 cout << "Start Bridging Command: " << service << endl;
988 // DimClient::setDnsNode(to_node);
989 ptrc = new BridgeCommand(service, format);
990 servp->browserServicePtr = (void *)ptrc;
991// DimClient::setDnsNode(from_node);
992 }
993 servp->registered = 1;
994 }
995 else if (known == -1)
996 {
997 done = -1;
998 if (type == DimSERVICE)
999 {
1000 ptrs = (BridgeService *)servp->browserServicePtr;
1001 if (ptrs)
1002 {
1003 dim_print_date_time();
1004 cout << "Stop Bridging Service: " << ptrs->getName() << endl;
1005 delete ptrs;
1006 }
1007 }
1008 else if (type == DimCOMMAND)
1009 {
1010 ptrc = (BridgeCommand *)servp->browserServicePtr;
1011 if (ptrc)
1012 {
1013 dim_print_date_time();
1014 cout << "Stop Bridging Command: " << ptrc->getName() << endl;
1015 delete ptrc;
1016 }
1017 }
1018 servp->registered = -2;
1019 }
1020 }
1021 if (poll_interval == 2)
1022 {
1023// sleep(2);
1024// dbr.getServices("*");
1025 poll_interval = 5;
1026// continue;
1027 }
1028 if (!done)
1029 {
1030 poll_interval = 30;
1031 }
1032 else if (done < 0)
1033 {
1034 bridge_hash_service_remove_many(-2);
1035 poll_interval = 2;
1036 }
1037 else
1038 {
1039 poll_interval = 5;
1040 }
1041// cout << "done " << done << ", poll_interval " << poll_interval << endl;
1042 for (i = 0; i < poll_interval; i++)
1043 {
1044 sleep(1);
1045 if (poll_interval == 0)
1046 i = -2;
1047 }
1048// cout << "out of sleep, poll_interval " << poll_interval << endl;
1049 }
1050 return 1;
1051}
1052
1053
1054
1055
1056
1057
1058/////////////////////////////
1059//////// Function for glob pattern matching. See http://www.cse.yorku.ca/~oz/
1060/*
1061* robust glob pattern matcher
1062* ozan s. yigit/dec 1994
1063* public domain
1064*
1065* glob patterns:
1066* * matches zero or more characters
1067* ? matches any single character
1068* [set] matches any character in the set
1069* [^set] matches any character NOT in the set
1070* where a set is a group of characters or ranges. a range
1071* is written as two characters seperated with a hyphen: a-z denotes
1072* all characters between a to z inclusive.
1073* [-set] set matches a literal hypen and any character in the set
1074* []set] matches a literal close bracket and any character in the set
1075*
1076* char matches itself except where char is '*' or '?' or '['
1077* \char matches char, including any pattern character
1078*
1079* examples:
1080* a*c ac abc abbc ...
1081* a?c acc abc aXc ...
1082* a[a-z]c aac abc acc ...
1083* a[-a-z]c a-c aac abc ...
1084*
1085* $Log: glob.c,v $
1086* Revision 1.3 1995/09/14 23:24:23 oz
1087* removed boring test/main code.
1088*
1089* Revision 1.2 94/12/11 10:38:15 oz
1090* cset code fixed. it is now robust and interprets all
1091* variations of cset [i think] correctly, including [z-a] etc.
1092*
1093* Revision 1.1 94/12/08 12:45:23 oz
1094* Initial revision
1095*/
1096
1097#ifndef NEGATE
1098#define NEGATE '^' /* std cset negation char */
1099#endif
1100
1101#define TRUE 1
1102#define FALSE 0
1103
1104int
1105amatch(char *str, char *p)
1106{
1107 int negate;
1108 int match;
1109 int c;
1110
1111 while (*p) {
1112 if (!*str && *p != '*')
1113 return FALSE;
1114
1115 switch (c = *p++) {
1116
1117 case '*':
1118 while (*p == '*')
1119 p++;
1120
1121 if (!*p)
1122 return TRUE;
1123
1124 if (*p != '?' && *p != '[' && *p != '\\')
1125 while (*str && *p != *str)
1126 str++;
1127
1128 while (*str) {
1129 if (amatch(str, p))
1130 return TRUE;
1131 str++;
1132 }
1133 return FALSE;
1134
1135 case '?':
1136 if (*str)
1137 break;
1138 return FALSE;
1139 /*
1140 * set specification is inclusive, that is [a-z] is a, z and
1141 * everything in between. this means [z-a] may be interpreted
1142 * as a set that contains z, a and nothing in between.
1143 */
1144 case '[':
1145 if (*p != NEGATE)
1146 negate = FALSE;
1147 else {
1148 negate = TRUE;
1149 p++;
1150 }
1151
1152 match = FALSE;
1153
1154 while (!match && (c = *p++)) {
1155 if (!*p)
1156 return FALSE;
1157 if (*p == '-') { /* c-c */
1158 if (!*++p)
1159 return FALSE;
1160 if (*p != ']') {
1161 if (*str == c || *str == *p ||
1162 (*str > c && *str < *p))
1163 match = TRUE;
1164 }
1165 else { /* c-] */
1166 if (*str >= c)
1167 match = TRUE;
1168 break;
1169 }
1170 }
1171 else { /* cc or c] */
1172 if (c == *str)
1173 match = TRUE;
1174 if (*p != ']') {
1175 if (*p == *str)
1176 match = TRUE;
1177 }
1178 else
1179 break;
1180 }
1181 }
1182
1183 if (negate == match)
1184 return FALSE;
1185 /*
1186 * if there is a match, skip past the cset and continue on
1187 */
1188 while (*p && *p != ']')
1189 p++;
1190 if (!*p++) /* oops! */
1191 return FALSE;
1192 break;
1193
1194 case '\\':
1195 if (*p)
1196 c = *p++;
1197 default:
1198 if (c != *str)
1199 return FALSE;
1200 break;
1201
1202 }
1203 str++;
1204 }
1205
1206 return !*str;
1207}
1208
1209
1210
1211/////////////////////////////// END OF CODE FROM http://www.cse.yorku.ca/~oz/
1212
1213char *strlwr(char *str)
1214{
1215 unsigned char *p = (unsigned char *)str;
1216
1217 while (*p) {
1218 *p = tolower((unsigned char)*p);
1219 p++;
1220 }
1221
1222 return str;
1223}
1224
1225// ps is a comma separated list of patterns. The function returns 1 as soon as one pattern matches
1226int multiple_match(const char* str, const char* ps, int case_sensitive) {
1227 if (ps == 0) return 0;
1228 if (strlen(ps) == 0) return 0;
1229 char* str2 = new char[strlen(str) + 1];
1230 strcpy(str2, str);
1231
1232 char* p;
1233 // the pattern must be copied in new memory because strtok modifies the string
1234 char* p2 = new char[strlen(ps) + 1];
1235 strcpy(p2, ps);
1236
1237 if (case_sensitive == 0) {
1238 strlwr(str2);
1239 strlwr(p2);
1240 }
1241
1242 p = strtok(p2, ",");
1243 int result = 0;
1244 while (p != NULL)
1245 {
1246 if (amatch(str2, p) == 1) {
1247 result = 1;
1248 break;
1249 }
1250 p = strtok(NULL, ",");
1251 }
1252 delete(p2);
1253 p2 = NULL;
1254 delete(str2);
1255 str2 = NULL;
1256 return result;
1257}
1258
1259#define MAX_HASH_ENTRIES 5000
1260
1261static SERVICE *BridgeService_hash_table[MAX_HASH_ENTRIES];
1262static int Service_new_entries[MAX_HASH_ENTRIES];
1263
1264int bridge_hash_service_init()
1265{
1266
1267 int i;
1268 static int done = 0;
1269
1270 dim_lock();
1271 if (!done)
1272 {
1273 for (i = 0; i < MAX_HASH_ENTRIES; i++)
1274 {
1275 /*
1276 BridgeService_hash_table[i] = (SERVICE *) malloc(sizeof(SERVICE));
1277 dll_init((DLL *) BridgeService_hash_table[i]);
1278 */
1279 BridgeService_hash_table[i] = 0;
1280 Service_new_entries[i] = 0;
1281 }
1282 done = 1;
1283 }
1284 dim_unlock();
1285 return(1);
1286}
1287
1288int bridge_hash_service_insert(SERVICE *servp)
1289{
1290 int index;
1291
1292 dim_lock();
1293 index = HashFunction(servp->name, MAX_HASH_ENTRIES);
1294 if (!BridgeService_hash_table[index])
1295 {
1296 BridgeService_hash_table[index] = (SERVICE *)malloc(sizeof(SERVICE));
1297 dll_init((DLL *)BridgeService_hash_table[index]);
1298 }
1299 Service_new_entries[index]++;
1300 dll_insert_queue((DLL *)BridgeService_hash_table[index],
1301 (DLL *)servp);
1302 dim_unlock();
1303 return(1);
1304}
1305
1306int bridge_hash_service_registered(int index, SERVICE *servp)
1307{
1308 dim_lock();
1309 servp->registered = 1;
1310 Service_new_entries[index]--;
1311 if (Service_new_entries[index] < 0)
1312 Service_new_entries[index] = 0;
1313 dim_unlock();
1314 return 1;
1315}
1316
1317int bridge_hash_service_remove(SERVICE *servp)
1318{
1319 int index;
1320
1321 dim_lock();
1322 index = HashFunction(servp->name, MAX_HASH_ENTRIES);
1323 if (!BridgeService_hash_table[index])
1324 {
1325 dim_unlock();
1326 return(0);
1327 }
1328 dll_remove((DLL *)servp);
1329 dim_unlock();
1330 return(1);
1331}
1332
1333
1334SERVICE *bridge_hash_service_exists(char *name)
1335{
1336 int index;
1337 SERVICE *servp;
1338
1339 dim_lock();
1340 index = HashFunction(name, MAX_HASH_ENTRIES);
1341 if (!BridgeService_hash_table[index])
1342 {
1343 dim_unlock();
1344 return((SERVICE *)0);
1345 }
1346 if ((servp = (SERVICE *)dll_search(
1347 (DLL *)BridgeService_hash_table[index],
1348 name, (int)strlen(name) + 1)))
1349 {
1350 dim_unlock();
1351 return(servp);
1352 }
1353 dim_unlock();
1354 return((SERVICE *)0);
1355}
1356
1357SERVICE *bridge_hash_service_get_next(int *curr_index, SERVICE *prevp, int new_entries)
1358{
1359 int index;
1360 SERVICE *servp = 0;
1361 /*
1362 if(!prevp)
1363 {
1364 index = -1;
1365 }
1366 */
1367 dim_lock();
1368 index = *curr_index;
1369 if (index == -1)
1370 {
1371 index++;
1372 prevp = BridgeService_hash_table[index];
1373 }
1374 if (!prevp)
1375 {
1376 prevp = BridgeService_hash_table[index];
1377 }
1378 do
1379 {
1380 if (prevp)
1381 {
1382 if ((!new_entries) || (Service_new_entries[index] > 0))
1383 {
1384 servp = (SERVICE *)dll_get_next(
1385 (DLL *)BridgeService_hash_table[index],
1386 (DLL *)prevp);
1387 if (servp)
1388 break;
1389 }
1390 }
1391 index++;
1392 if (index == MAX_HASH_ENTRIES)
1393 {
1394 *curr_index = -1;
1395 dim_unlock();
1396 return((SERVICE *)0);
1397 }
1398 prevp = BridgeService_hash_table[index];
1399 } while (!servp);
1400 *curr_index = index;
1401 dim_unlock();
1402 return(servp);
1403}
1404
1405int bridge_hash_service_remove_many(int registered)
1406{
1407 SERVICE *servp, *prevp;
1408 int hash_index, old_index;
1409 int n = 0;
1410
1411 dim_lock();
1412 prevp = 0;
1413 hash_index = -1;
1414 old_index = -1;
1415 while ((servp = bridge_hash_service_get_next(&hash_index, prevp, 0)))
1416 {
1417 if (servp->registered == registered)
1418 {
1419 servp->registered = 0;
1420 bridge_hash_service_remove(servp);
1421 free(servp);
1422 n++;
1423 if (old_index != hash_index)
1424 prevp = 0;
1425 }
1426 else
1427 {
1428 prevp = servp;
1429 old_index = hash_index;
1430 }
1431 }
1432 dim_unlock();
1433 return(n);
1434}
Note: See TracBrowser for help on using the repository browser.