FACT++  1.0
open_dns.c
Go to the documentation of this file.
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 
15 typedef struct {
16  char node_name[MAX_NODE_NAME];
17  char task_name[MAX_TASK_NAME];
18  void (*recv_rout)();
19  void (*error_rout)();
22 } PENDING_CONN;
23 
24 typedef struct dns_ent{
25  struct dns_ent *next;
26  struct dns_ent *prev;
30  void (*recv_rout)();
31  void (*error_rout)();
34  int conn_id;
35  int pending;
37 } DNS_CONN;
38 
39 static int Timer_q = 0;
40 static DNS_CONN *DNS_ids[3] = {0, 0, 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 
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 
59 {
60  init_dns_list();
61  set_dns_pars(DNS_ids[SRC_DIC], node, 0);
62  return(1);
63 }
64 
66 {
67  init_dns_list();
68  set_dns_pars(DNS_ids[SRC_DIS], node, 0);
69  return(1);
70 }
71 
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 
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 
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 
99 int 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 
107 int 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 
114 int 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 
122 {
123 int port;
124 char node[MAX_DNS_NODE];
125  init_dns_list();
126  get_dns_pars(DNS_ids[SRC_DIS], node, &port);
127  return(port);
128 }
129 
131 {
132 int port;
133 char node[MAX_DNS_NODE];
134  init_dns_list();
135  get_dns_pars(DNS_ids[SRC_DIC], node, &port);
136  return(port);
137 }
138 
140 {
141 int port;
142 char node[MAX_DNS_NODE];
143  init_dns_list();
144  get_dns_pars(DNS_ids[SRC_DIS], node, &port);
145  return(port);
146 }
147 
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 
161 int 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 
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 
194 void 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 
206 int 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 
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 
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 
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 
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 
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 
313 int 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 
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 
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 }
int close_dns(dim_long dnsid, SRC_TYPES src_type)
Definition: open_dns.c:298
void retry_dns_connection(DNS_CONN *connp)
Definition: open_dns.c:377
Definition: dns.c:26
int dis_get_dns_port()
Definition: open_dns.c:139
struct dns_ent DNS_CONN
int dis_set_dns_port(int port)
Definition: open_dns.c:114
dim_long dis_add_dns(char *node_name, int port_number)
Definition: open_dns.c:237
int dim_set_dns_node(char *node)
Definition: open_dns.c:50
DNS_CONN * find_dns(char *node_name, int port_number, SRC_TYPES src_type)
Definition: open_dns.c:219
int dna_open_client(char *server_node, char *server_task, int port, int server_protocol, void(*read_ast)(), void(*error_ast)(), SRC_TYPES src_type)
Definition: dna.c:733
int i
Definition: db_dim_client.c:21
#define TCPIP
Definition: dim.h:194
char task_name[MAX_TASK_NAME]
Definition: open_dns.c:28
void dll_insert_queue(DLL *head, DLL *item)
Definition: dll.c:23
#define DNS_TASK
Definition: dim.h:138
int get_dns_pars(DNS_CONN *connp, char *node, int *port)
Definition: open_dns.c:206
int rand_tmout(int min, int max)
Definition: open_dns.c:161
int dtq_create()
Definition: dtq.c:353
int dic_set_dns_node(char *node)
Definition: open_dns.c:58
void * malloc()
Definition: EventBuilder.cc:99
SRC_TYPES
Definition: dim_common.h:19
Definition: dim.h:370
_DIM_PROTO(void retry_dns_connection,(int conn_pend_id))
int dic_get_dns_port()
Definition: open_dns.c:130
TIMR_ENT * timr_ent
Definition: open_dns.c:32
void dll_init(DLL *head)
Definition: dll.c:14
void init_dns_list()
Definition: open_dns.c:171
static void recv_rout(int conn_id, DIS_PACKET *packet, int size, int status)
Definition: dic.c:143
static int Timer_q
Definition: open_dns.c:39
int get_node_addr(char *node_addr)
Definition: utilities.c:98
struct dns_ent * prev
Definition: open_dns.c:26
int dic_set_dns_port(int port)
Definition: open_dns.c:107
#define MAX_DNS_NODE
Definition: open_dns.c:13
int get_dns_node_name(char *node_name)
Definition: utilities.c:204
int connecting
Definition: open_dns.c:36
int dim_get_dns_node(char *node)
Definition: open_dns.c:72
void(* error_rout)()
Definition: open_dns.c:31
SRC_TYPES src_type
Definition: open_dns.c:33
int open_dns(dim_long dnsid, void(*recv_rout)(), void(*error_rout)(), int tmout_min, int tmout_max, SRC_TYPES src_type)
Definition: open_dns.c:313
Definition: dim.h:530
int conn_id
Definition: open_dns.c:34
int dic_get_dns_node(char *node)
Definition: open_dns.c:81
int dtq_rem_entry(int queue_id, TIMR_ENT *entry)
Definition: dtq.c:503
#define MAX_TASK_NAME
Definition: dim.h:181
SRC_TYPES src_type
Definition: open_dns.c:21
long dim_long
Definition: dim_common.h:57
dim_long dic_add_dns(char *node_name, int port_number)
Definition: open_dns.c:258
dim_long dns_get_dnsid(int conn_id, SRC_TYPES src_type)
Definition: open_dns.c:426
int dim_get_dns_port()
Definition: open_dns.c:121
int dis_get_dns_node(char *node)
Definition: open_dns.c:90
void rand_tmout_init()
Definition: open_dns.c:148
int get_dns_port_number()
Definition: utilities.c:216
int port_number
Definition: open_dns.c:29
void set_dns_pars(DNS_CONN *connp, char *node, int port)
Definition: open_dns.c:194
char node_name[MAX_DNS_NODE]
Definition: open_dns.c:27
int dis_set_dns_node(char *node)
Definition: open_dns.c:65
TIMR_ENT * dtq_add_entry(int queue_id, int time, void(*user_routine)(), dim_long tag)
Definition: dtq.c:399
struct dns_ent * next
Definition: open_dns.c:25
int dim_set_dns_port(int port)
Definition: open_dns.c:99
DLL * dll_get_next(DLL *head, DLL *item)
Definition: dll.c:66
#define MAX_NODE_NAME
Definition: dim.h:180
static DNS_CONN * DNS_ids[3]
Definition: open_dns.c:40
int pending
Definition: open_dns.c:35
int get_proc_name(char *proc_name)
Definition: utilities.c:22
dnsid
Definition: README_v19.txt:372
static DNS_CONN * DNS_conn_head
Definition: open_dns.c:41
void(* recv_rout)()
Definition: open_dns.c:30
DNS_CONN * get_dns(DNS_CONN *connp, SRC_TYPES src_type)
Definition: open_dns.c:279
#define DNS_PORT
Definition: dim.h:139
TIMR_ENT * timr_ent
Definition: open_dns.c:20