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

Last change on this file since 12410 was 11071, checked in by tbretz, 13 years ago
Replaced v19r21 by a version extracted with 'unzip -a' to get proper unix text format.
File size: 8.3 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
148int rand_tmout( int min, int max )
149{
150 int aux;
151
152 aux = rand();
153 aux %= (max - min);
154 aux += min;
155 return(aux);
156}
157
158void init_dns_list()
159{
160 char node[MAX_DNS_NODE];
161 int port;
162 long sid, cid;
163
164 DISABLE_AST
165 if(!DNS_conn_head)
166 {
167 DNS_conn_head = (DNS_CONN *)malloc(sizeof(DNS_CONN));
168 dll_init( (DLL *) DNS_conn_head );
169 node[0] = '\0';
170 get_dns_node_name(node);
171 port = get_dns_port_number();
172 sid = dis_add_dns(node, port);
173 cid = dic_add_dns(node, port);
174 DNS_ids[SRC_DIS] = (DNS_CONN *)sid;
175 DNS_ids[SRC_DIC] = (DNS_CONN *)cid;
176 }
177 ENABLE_AST
178}
179
180void set_dns_pars(DNS_CONN *connp, char *node, int port)
181{
182 if(node != 0)
183 {
184 strcpy(connp->node_name, node);
185 }
186 if(port != 0)
187 {
188 connp->port_number = port;
189 }
190}
191
192int get_dns_pars(DNS_CONN *connp, char *node, int *port)
193{
194 int exists = 0;
195
196 if(connp->node_name[0])
197 {
198 strcpy(node, connp->node_name);
199 exists = 1;
200 }
201 *port = connp->port_number;
202 return exists;
203}
204
205DNS_CONN *find_dns(char *node_name, int port_number, SRC_TYPES src_type)
206{
207 DNS_CONN *connp;
208
209 connp = DNS_conn_head;
210 while( (connp = (DNS_CONN *)dll_get_next( (DLL *) DNS_conn_head,
211 (DLL*) connp)) )
212 {
213 if(connp->src_type == src_type)
214 {
215 if((!strcmp(connp->node_name, node_name)) &&
216 (connp->port_number == port_number))
217 return connp;
218 }
219 }
220 return (DNS_CONN *)0;
221}
222
223long dis_add_dns(char *node_name, int port_number)
224{
225 DNS_CONN *connp;
226
227 init_dns_list();
228 if(!(connp = find_dns(node_name, port_number, SRC_DIS)))
229 {
230 connp = (DNS_CONN *)malloc(sizeof(DNS_CONN));
231 strcpy(connp->node_name, node_name);
232 connp->port_number = DNS_PORT;
233 if(port_number != 0)
234 connp->port_number = port_number;
235 connp->src_type = SRC_DIS;
236 connp->pending = 0;
237 connp->conn_id = 0;
238 connp->connecting = 0;
239 dll_insert_queue( (DLL *) DNS_conn_head, (DLL *) connp );
240 }
241 return (long)connp;
242}
243
244long dic_add_dns(char *node_name, int port_number)
245{
246 DNS_CONN *connp;
247
248 init_dns_list();
249 if(!(connp = find_dns(node_name, port_number, SRC_DIC)))
250 {
251 connp = (DNS_CONN *)malloc(sizeof(DNS_CONN));
252 strcpy(connp->node_name, node_name);
253 connp->port_number = DNS_PORT;
254 if(port_number != 0)
255 connp->port_number = port_number;
256 connp->src_type = SRC_DIC;
257 connp->pending = 0;
258 connp->conn_id = 0;
259 connp->connecting = 0;
260 dll_insert_queue( (DLL *) DNS_conn_head, (DLL *) connp );
261 }
262 return (long)connp;
263}
264
265DNS_CONN *get_dns(DNS_CONN *connp, SRC_TYPES src_type)
266{
267 DNS_CONN *p = 0;
268
269 init_dns_list();
270 if(connp)
271 {
272 if(connp->src_type == src_type)
273 {
274 p = connp;
275 }
276 }
277 else
278 {
279 p = DNS_ids[src_type];
280 }
281 return p;
282}
283
284int close_dns(long dnsid, SRC_TYPES src_type)
285{
286 DNS_CONN *connp;
287
288 connp = get_dns((DNS_CONN *)dnsid, src_type);
289 if( !Timer_q )
290 Timer_q = dtq_create();
291 if( connp->pending )
292 {
293 connp->pending = 0;
294 dtq_rem_entry( Timer_q, connp->timr_ent );
295 }
296 return 1;
297}
298
299int open_dns(long dnsid, void (*recv_rout)(), void (*error_rout)(), int tmout_min, int tmout_max, SRC_TYPES src_type )
300{
301 char nodes[MAX_DNS_NODE];
302 char node_info[MAX_NODE_NAME+4];
303 register char *dns_node, *ptr;
304 register int conn_id;
305 register int timeout, node_exists;
306 int i, dns_port;
307 int rand_tmout();
308 DNS_CONN *connp;
309
310 conn_id = 0;
311 if( !Timer_q )
312 Timer_q = dtq_create();
313
314 connp = get_dns((DNS_CONN *)dnsid, src_type);
315 node_exists = get_dns_pars(connp, nodes, &dns_port);
316 if( !(connp->pending) )
317 {
318 if(!node_exists)
319 {
320 return(-2);
321 }
322 ptr = nodes;
323 while(1)
324 {
325 dns_node = ptr;
326 if( (ptr = (char *)strchr(ptr,',')) )
327 {
328 *ptr = '\0';
329 ptr++;
330 }
331 strcpy(node_info,dns_node);
332 for(i = 0; i < 4; i ++)
333 node_info[strlen(node_info)+i+1] = (char)0xff;
334 connp->conn_id = 0;
335 connp->connecting = 1;
336 conn_id = dna_open_client( node_info, DNS_TASK, dns_port,
337 TCPIP, recv_rout, error_rout, src_type );
338 connp->connecting = 0;
339 if(conn_id)
340 break;
341 if( !ptr )
342 break;
343 }
344 connp->conn_id = conn_id;
345 if(!conn_id)
346 {
347 strncpy(connp->task_name, DNS_TASK, MAX_TASK_NAME);
348 connp->recv_rout = recv_rout;
349 connp->error_rout = error_rout;
350 connp->pending = 1;
351 timeout = rand_tmout( tmout_min, tmout_max );
352 connp->timr_ent = dtq_add_entry( Timer_q, timeout,
353 retry_dns_connection,
354 connp );
355 return( -1);
356 }
357 }
358 else
359 return(-1);
360 return(conn_id);
361}
362
363void retry_dns_connection( DNS_CONN *connp )
364{
365 char nodes[MAX_DNS_NODE];
366 char node_info[MAX_NODE_NAME+4];
367 register char *dns_node, *ptr;
368 register int conn_id, node_exists;
369 static int retrying = 0;
370 int i, dns_port;
371
372 if( retrying ) return;
373 retrying = 1;
374
375 conn_id = 0;
376 node_exists = get_dns_pars(connp, nodes, &dns_port);
377 if(node_exists)
378 {
379 ptr = nodes;
380 while(1)
381 {
382 dns_node = ptr;
383 if( (ptr = (char *)strchr(ptr,',')) )
384 {
385 *ptr = '\0';
386 ptr++;
387 }
388 strcpy(node_info,dns_node);
389 for(i = 0; i < 4; i ++)
390 node_info[strlen(node_info)+i+1] = (char)0xff;
391 connp->conn_id = 0;
392 connp->connecting = 1;
393 conn_id = dna_open_client( node_info, connp->task_name,
394 dns_port, TCPIP,
395 connp->recv_rout, connp->error_rout, connp->src_type );
396 connp->connecting = 0;
397 if( conn_id )
398 break;
399 if( !ptr )
400 break;
401 }
402 }
403 connp->conn_id = conn_id;
404 if(conn_id)
405 {
406 connp->pending = 0;
407 dtq_rem_entry( Timer_q, connp->timr_ent );
408 }
409 retrying = 0;
410}
411
412long dns_get_dnsid(int conn_id, SRC_TYPES src_type)
413{
414 DNS_CONN *connp;
415 int found = 0;
416
417 connp = DNS_conn_head;
418 while( (connp = (DNS_CONN *)dll_get_next( (DLL *) DNS_conn_head,
419 (DLL*) connp)) )
420 {
421 if(connp->conn_id == conn_id)
422 {
423 found = 1;
424 break;
425 }
426 else if((connp->conn_id == 0) && (connp->connecting))
427 {
428 connp->conn_id = conn_id;
429 found = 1;
430 break;
431 }
432 }
433 if(found)
434 {
435 if(connp == DNS_ids[src_type])
436 {
437 return (long)0;
438 }
439 else
440 {
441 return (long)connp;
442 }
443 }
444 return (long)-1;
445}
Note: See TracBrowser for help on using the repository browser.