source: trunk/FACT++/dim/src/open_dns.c@ 16336

Last change on this file since 16336 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 8.7 KB
Line 
1/*
2 * A utility file.
3 *
4 * Started date : 10-11-91
5 * Written by : C. Gaspar
6 * UNIX adjustment: G.C. Ballintijn
7 *
8 */
9
10#define DIMLIB
11#include <dim.h>
12
13#define MAX_DNS_NODE 256
14
15typedef struct {
16 char node_name[MAX_NODE_NAME];
17 char task_name[MAX_TASK_NAME];
18 void (*recv_rout)();
19 void (*error_rout)();
20 TIMR_ENT *timr_ent;
21 SRC_TYPES src_type;
22} PENDING_CONN;
23
24typedef struct dns_ent{
25 struct dns_ent *next;
26 struct dns_ent *prev;
27 char node_name[MAX_DNS_NODE];
28 char task_name[MAX_TASK_NAME];
29 int port_number;
30 void (*recv_rout)();
31 void (*error_rout)();
32 TIMR_ENT *timr_ent;
33 SRC_TYPES src_type;
34 int conn_id;
35 int pending;
36 int connecting;
37} DNS_CONN;
38
39static int Timer_q = 0;
40static DNS_CONN *DNS_ids[3] = {0, 0, 0};
41static DNS_CONN *DNS_conn_head = (DNS_CONN *)0;
42
43
44_DIM_PROTO( void retry_dns_connection, ( int conn_pend_id ) );
45_DIM_PROTO( void init_dns_list, ( ) );
46_DIM_PROTO( void set_dns_pars, ( DNS_CONN *connp, char *node, int port ) );
47_DIM_PROTO( int get_dns_pars, ( DNS_CONN *connp, char *node, int *port ) );
48
49
50int dim_set_dns_node(char *node)
51{
52 init_dns_list();
53 set_dns_pars(DNS_ids[SRC_DIS], node, 0);
54 set_dns_pars(DNS_ids[SRC_DIC], node, 0);
55 return(1);
56}
57
58int dic_set_dns_node(char *node)
59{
60 init_dns_list();
61 set_dns_pars(DNS_ids[SRC_DIC], node, 0);
62 return(1);
63}
64
65int dis_set_dns_node(char *node)
66{
67 init_dns_list();
68 set_dns_pars(DNS_ids[SRC_DIS], node, 0);
69 return(1);
70}
71
72int dim_get_dns_node(char *node)
73{
74 register int node_exists;
75 int port;
76 init_dns_list();
77 node_exists = get_dns_pars(DNS_ids[SRC_DIS], node, &port);
78 return(node_exists);
79}
80
81int dic_get_dns_node(char *node)
82{
83 register int node_exists;
84 int port;
85 init_dns_list();
86 node_exists = get_dns_pars(DNS_ids[SRC_DIC], node, &port);
87 return(node_exists);
88}
89
90int dis_get_dns_node(char *node)
91{
92 register int node_exists;
93 int port;
94 init_dns_list();
95 node_exists = get_dns_pars(DNS_ids[SRC_DIS], node, &port);
96 return(node_exists);
97}
98
99int dim_set_dns_port(int port)
100{
101 init_dns_list();
102 set_dns_pars(DNS_ids[SRC_DIS], 0, port);
103 set_dns_pars(DNS_ids[SRC_DIC], 0, port);
104 return(1);
105}
106
107int dic_set_dns_port(int port)
108{
109 init_dns_list();
110 set_dns_pars(DNS_ids[SRC_DIC], 0, port);
111 return(1);
112}
113
114int dis_set_dns_port(int port)
115{
116 init_dns_list();
117 set_dns_pars(DNS_ids[SRC_DIS], 0, port);
118 return(1);
119}
120
121int dim_get_dns_port()
122{
123int port;
124char node[MAX_DNS_NODE];
125 init_dns_list();
126 get_dns_pars(DNS_ids[SRC_DIS], node, &port);
127 return(port);
128}
129
130int dic_get_dns_port()
131{
132int port;
133char node[MAX_DNS_NODE];
134 init_dns_list();
135 get_dns_pars(DNS_ids[SRC_DIC], node, &port);
136 return(port);
137}
138
139int dis_get_dns_port()
140{
141int port;
142char node[MAX_DNS_NODE];
143 init_dns_list();
144 get_dns_pars(DNS_ids[SRC_DIS], node, &port);
145 return(port);
146}
147
148void rand_tmout_init()
149{
150 char pid_str[MAX_TASK_NAME];
151 int ip, pid;
152 extern int get_node_addr();
153 extern int get_proc_name();
154
155 get_node_addr((char *)&ip);
156 get_proc_name(pid_str);
157 sscanf(pid_str,"%d",&pid);
158 srand((unsigned)(ip+pid));
159}
160
161int rand_tmout( int min, int max )
162{
163 int aux;
164
165 aux = rand();
166 aux %= (max - min);
167 aux += min;
168 return(aux);
169}
170
171void init_dns_list()
172{
173 char node[MAX_DNS_NODE];
174 int port;
175 dim_long sid, cid;
176
177 DISABLE_AST
178 if(!DNS_conn_head)
179 {
180 DNS_conn_head = (DNS_CONN *)malloc(sizeof(DNS_CONN));
181 dll_init( (DLL *) DNS_conn_head );
182 node[0] = '\0';
183 get_dns_node_name(node);
184 port = get_dns_port_number();
185 sid = dis_add_dns(node, port);
186 cid = dic_add_dns(node, port);
187 DNS_ids[SRC_DIS] = (DNS_CONN *)sid;
188 DNS_ids[SRC_DIC] = (DNS_CONN *)cid;
189 rand_tmout_init();
190 }
191 ENABLE_AST
192}
193
194void set_dns_pars(DNS_CONN *connp, char *node, int port)
195{
196 if(node != 0)
197 {
198 strcpy(connp->node_name, node);
199 }
200 if(port != 0)
201 {
202 connp->port_number = port;
203 }
204}
205
206int get_dns_pars(DNS_CONN *connp, char *node, int *port)
207{
208 int exists = 0;
209
210 if(connp->node_name[0])
211 {
212 strcpy(node, connp->node_name);
213 exists = 1;
214 }
215 *port = connp->port_number;
216 return exists;
217}
218
219DNS_CONN *find_dns(char *node_name, int port_number, SRC_TYPES src_type)
220{
221 DNS_CONN *connp;
222
223 connp = DNS_conn_head;
224 while( (connp = (DNS_CONN *)dll_get_next( (DLL *) DNS_conn_head,
225 (DLL*) connp)) )
226 {
227 if(connp->src_type == src_type)
228 {
229 if((!strcmp(connp->node_name, node_name)) &&
230 (connp->port_number == port_number))
231 return connp;
232 }
233 }
234 return (DNS_CONN *)0;
235}
236
237dim_long dis_add_dns(char *node_name, int port_number)
238{
239 DNS_CONN *connp;
240
241 init_dns_list();
242 if(!(connp = find_dns(node_name, port_number, SRC_DIS)))
243 {
244 connp = (DNS_CONN *)malloc(sizeof(DNS_CONN));
245 strcpy(connp->node_name, node_name);
246 connp->port_number = DNS_PORT;
247 if(port_number != 0)
248 connp->port_number = port_number;
249 connp->src_type = SRC_DIS;
250 connp->pending = 0;
251 connp->conn_id = 0;
252 connp->connecting = 0;
253 dll_insert_queue( (DLL *) DNS_conn_head, (DLL *) connp );
254 }
255 return (dim_long)connp;
256}
257
258dim_long dic_add_dns(char *node_name, int port_number)
259{
260 DNS_CONN *connp;
261
262 init_dns_list();
263 if(!(connp = find_dns(node_name, port_number, SRC_DIC)))
264 {
265 connp = (DNS_CONN *)malloc(sizeof(DNS_CONN));
266 strcpy(connp->node_name, node_name);
267 connp->port_number = DNS_PORT;
268 if(port_number != 0)
269 connp->port_number = port_number;
270 connp->src_type = SRC_DIC;
271 connp->pending = 0;
272 connp->conn_id = 0;
273 connp->connecting = 0;
274 dll_insert_queue( (DLL *) DNS_conn_head, (DLL *) connp );
275 }
276 return (dim_long)connp;
277}
278
279DNS_CONN *get_dns(DNS_CONN *connp, SRC_TYPES src_type)
280{
281 DNS_CONN *p = 0;
282
283 init_dns_list();
284 if(connp)
285 {
286 if(connp->src_type == src_type)
287 {
288 p = connp;
289 }
290 }
291 else
292 {
293 p = DNS_ids[src_type];
294 }
295 return p;
296}
297
298int close_dns(dim_long dnsid, SRC_TYPES src_type)
299{
300 DNS_CONN *connp;
301
302 connp = get_dns((DNS_CONN *)dnsid, src_type);
303 if( !Timer_q )
304 Timer_q = dtq_create();
305 if( connp->pending )
306 {
307 connp->pending = 0;
308 dtq_rem_entry( Timer_q, connp->timr_ent );
309 }
310 return 1;
311}
312
313int open_dns(dim_long dnsid, void (*recv_rout)(), void (*error_rout)(), int tmout_min, int tmout_max, SRC_TYPES src_type )
314{
315 char nodes[MAX_DNS_NODE];
316 char node_info[MAX_NODE_NAME+4];
317 register char *dns_node, *ptr;
318 register int conn_id;
319 register int timeout, node_exists;
320 int i, dns_port;
321 int rand_tmout();
322 DNS_CONN *connp;
323
324 conn_id = 0;
325 if( !Timer_q )
326 Timer_q = dtq_create();
327
328 connp = get_dns((DNS_CONN *)dnsid, src_type);
329 node_exists = get_dns_pars(connp, nodes, &dns_port);
330 if( !(connp->pending) )
331 {
332 if(!node_exists)
333 {
334 return(-2);
335 }
336 ptr = nodes;
337 while(1)
338 {
339 dns_node = ptr;
340 if( (ptr = (char *)strchr(ptr,',')) )
341 {
342 *ptr = '\0';
343 ptr++;
344 }
345 strcpy(node_info,dns_node);
346 for(i = 0; i < 4; i ++)
347 node_info[(int)strlen(node_info)+i+1] = (char)0xff;
348 connp->conn_id = 0;
349 connp->connecting = 1;
350 conn_id = dna_open_client( node_info, DNS_TASK, dns_port,
351 TCPIP, recv_rout, error_rout, src_type );
352 connp->connecting = 0;
353 if(conn_id)
354 break;
355 if( !ptr )
356 break;
357 }
358 connp->conn_id = conn_id;
359 if(!conn_id)
360 {
361 strncpy(connp->task_name, DNS_TASK, (size_t)MAX_TASK_NAME);
362 connp->recv_rout = recv_rout;
363 connp->error_rout = error_rout;
364 connp->pending = 1;
365 timeout = rand_tmout( tmout_min, tmout_max );
366 connp->timr_ent = dtq_add_entry( Timer_q, timeout,
367 retry_dns_connection,
368 connp );
369 return( -1);
370 }
371 }
372 else
373 return(-1);
374 return(conn_id);
375}
376
377void retry_dns_connection( DNS_CONN *connp )
378{
379 char nodes[MAX_DNS_NODE];
380 char node_info[MAX_NODE_NAME+4];
381 register char *dns_node, *ptr;
382 register int conn_id, node_exists;
383 static int retrying = 0;
384 int i, dns_port;
385
386 if( retrying ) return;
387 retrying = 1;
388
389 conn_id = 0;
390 node_exists = get_dns_pars(connp, nodes, &dns_port);
391 if(node_exists)
392 {
393 ptr = nodes;
394 while(1)
395 {
396 dns_node = ptr;
397 if( (ptr = (char *)strchr(ptr,',')) )
398 {
399 *ptr = '\0';
400 ptr++;
401 }
402 strcpy(node_info,dns_node);
403 for(i = 0; i < 4; i ++)
404 node_info[(int)strlen(node_info)+i+1] = (char)0xff;
405 connp->conn_id = 0;
406 connp->connecting = 1;
407 conn_id = dna_open_client( node_info, connp->task_name,
408 dns_port, TCPIP,
409 connp->recv_rout, connp->error_rout, connp->src_type );
410 connp->connecting = 0;
411 if( conn_id )
412 break;
413 if( !ptr )
414 break;
415 }
416 }
417 connp->conn_id = conn_id;
418 if(conn_id)
419 {
420 connp->pending = 0;
421 dtq_rem_entry( Timer_q, connp->timr_ent );
422 }
423 retrying = 0;
424}
425
426dim_long dns_get_dnsid(int conn_id, SRC_TYPES src_type)
427{
428 DNS_CONN *connp;
429 int found = 0;
430
431 connp = DNS_conn_head;
432 while( (connp = (DNS_CONN *)dll_get_next( (DLL *) DNS_conn_head,
433 (DLL*) connp)) )
434 {
435 if(connp->conn_id == conn_id)
436 {
437 found = 1;
438 break;
439 }
440 else if((connp->conn_id == 0) && (connp->connecting))
441 {
442 connp->conn_id = conn_id;
443 found = 1;
444 break;
445 }
446 }
447 if(found)
448 {
449 if(connp == DNS_ids[src_type])
450 {
451 return (dim_long)0;
452 }
453 else
454 {
455 return (dim_long)connp;
456 }
457 }
458 return (dim_long)-1;
459}
Note: See TracBrowser for help on using the repository browser.