source: trunk/FACT++/dim/src/utilities.c@ 18522

Last change on this file since 18522 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 4.7 KB
Line 
1/*
2 * DNA (Delphi Network Access) implements the network layer for the DIM
3 * (Delphi Information Managment) System.
4 *
5 * Started date : 10-11-91
6 * Written by : C. Gaspar
7 * UNIX adjustment: G.C. Ballintijn
8 *
9 */
10
11#include <sys/types.h>
12#ifndef WIN32
13#include <netinet/in.h>
14#include <netdb.h>
15#endif
16#include <string.h>
17#include <time.h>
18#include <sys/timeb.h>
19#define DIMLIB
20#include <dim.h>
21
22int get_proc_name(char *proc_name)
23{
24#ifndef VxWorks
25 sprintf( proc_name, "%d", getpid() );
26#else
27 sprintf( proc_name, "%d", taskIdSelf() );
28#endif
29 return(1);
30}
31
32
33int get_node_name(char *node_name)
34{
35#ifndef VxWorks
36struct hostent *host;
37#endif
38char *p;
39int i;
40#ifdef WIN32
41extern void init_sock();
42#endif
43
44 DISABLE_AST
45#ifdef WIN32
46 init_sock();
47#endif
48 if( (p = getenv("DIM_HOST_NODE")) != NULL )
49 {
50 strcpy( node_name, p );
51 ENABLE_AST
52 return(1);
53 }
54 if((gethostname(node_name, MAX_NODE_NAME)) == -1)
55 {
56 ENABLE_AST
57 return(0);
58 }
59#ifndef VxWorks
60#ifndef RAID
61 if(!strchr(node_name,'.'))
62 {
63 if ((host = gethostbyname(node_name)) != (struct hostent *)0)
64 {
65 strcpy(node_name,host->h_name);
66 if(!strchr(node_name,'.'))
67 {
68 if(host->h_aliases)
69 {
70 if(host->h_aliases[0])
71 {
72 for(i = 0; host->h_aliases[i]; i++)
73 {
74 p = host->h_aliases[i];
75 if(strchr(p,'.'))
76 {
77 strcpy(node_name,p);
78 break;
79 }
80 }
81 }
82 }
83 }
84 }
85 }
86#endif
87#endif
88 ENABLE_AST
89 return(1);
90}
91
92/*
93Bug or Feature?
94get_node_addr returns the "default" interface address, not the one chosen by
95DIM_HOST_NODE. This makes the DNS or a DIM server respond to both interfaces
96*/
97
98int get_node_addr(char *node_addr)
99{
100#ifndef VxWorks
101struct hostent *host;
102#endif
103char node_name[MAX_NODE_NAME];
104char *ptr;
105
106#ifdef WIN32
107 init_sock();
108#endif
109 gethostname(node_name, MAX_NODE_NAME);
110#ifndef VxWorks
111 if ((host = (struct hostent *)gethostbyname(node_name)) == (struct hostent *)0)
112 {
113 node_addr[0] = 0;
114 node_addr[1] = 0;
115 node_addr[2] = 0;
116 node_addr[3] = 0;
117 return(0);
118 }
119 ptr = (char *)host->h_addr;
120 node_addr[0] = *ptr++;
121 node_addr[1] = *ptr++;
122 node_addr[2] = *ptr++;
123 node_addr[3] = *ptr++;
124 return(1);
125#else
126 node_addr[0] = 0;
127 node_addr[1] = 0;
128 node_addr[2] = 0;
129 node_addr[3] = 0;
130 return(0);
131#endif
132}
133
134void dim_print_date_time()
135{
136 time_t t;
137 char str[128];
138
139 t = time((time_t *)0);
140/*
141#ifdef WIN32
142 strcpy(str, ctime(&t));
143#else
144#ifdef LYNXOS
145 ctime_r(&t, str, 128);
146#else
147 ctime_r(&t, str);
148#endif
149#endif
150*/
151 my_ctime(&t, str, 128);
152 str[(int)strlen(str)-1] = '\0';
153 printf("PID %d - ",getpid());
154 printf("%s - ",str );
155}
156
157void dim_print_date_time_millis()
158{
159 int millies;
160
161#ifdef WIN32
162 struct timeb timebuf;
163#else
164 struct timeval tv;
165 struct timezone *tz;
166#endif
167
168#ifdef WIN32
169 ftime(&timebuf);
170 millies = timebuf.millitm;
171#else
172 tz = 0;
173 gettimeofday(&tv, tz);
174 millies = (int)tv.tv_usec / 1000;
175#endif
176 dim_print_date_time();
177 printf("milliseconds: %d ", millies);
178}
179
180void dim_print_msg(char *msg, int severity)
181{
182 dim_print_date_time();
183 switch(severity)
184 {
185 case 0: printf("(INFO) ");
186 break;
187 case 1: printf("(WARNING) ");
188 break;
189 case 2: printf("(ERROR) ");
190 break;
191 case 3: printf("(FATAL) ");
192 break;
193 }
194 printf("%s\n",msg);
195 fflush(stdout);
196}
197
198void dim_panic( char *s )
199{
200 printf( "\n\nDNA library panic: %s\n\n", s );
201 exit(0);
202}
203
204int get_dns_node_name( char *node_name )
205{
206 char *p;
207
208 if( (p = getenv("DIM_DNS_NODE")) == NULL )
209 return(0);
210 else {
211 strcpy( node_name, p );
212 return(1);
213 }
214}
215
216int get_dns_port_number()
217{
218 char *p;
219
220 if( (p = getenv("DIM_DNS_PORT")) == NULL )
221 return(DNS_PORT);
222 else {
223 return(atoi(p));
224 }
225}
226
227int dim_get_env_var( char *env_var, char *value, int len )
228{
229 char *p;
230 int tot, sz;
231
232 if( (p = getenv(env_var)) == NULL )
233 return(0);
234 else {
235 tot = (int)strlen(p)+1;
236 if(value != 0)
237 {
238 sz = tot;
239 if(sz > len)
240 sz = len;
241 strncpy(value, p, (size_t)sz);
242 if((sz == len) && (len > 0))
243 value[sz-1] = '\0';
244 }
245 return(tot);
246 }
247}
248
249int get_dns_accepted_domains( char *domains )
250{
251 char *p;
252 int append = 0;
253
254 if(get_dns_accepted_nodes(domains))
255 append = 1;
256 if( (p = getenv("DIM_DNS_ACCEPTED_DOMAINS")) == NULL )
257 {
258 if(!append)
259 return(0);
260 else
261 return(1);
262 }
263 else {
264 if(!append)
265 strcpy( domains, p );
266 else
267 {
268 strcat( domains, ",");
269 strcat( domains, p);
270 }
271 return(1);
272 }
273}
274
275int get_dns_accepted_nodes( char *nodes )
276{
277 char *p;
278
279 if( (p = getenv("DIM_DNS_ACCEPTED_NODES")) == NULL )
280 return(0);
281 else {
282 strcpy( nodes, p );
283 return(1);
284 }
285}
286
287int get_keepalive_tmout()
288{
289 char *p;
290
291 if( (p = getenv("DIM_KEEPALIVE_TMOUT")) == NULL )
292 return(TEST_TIME_OSK);
293 else {
294 return(atoi(p));
295 }
296}
297
298int get_write_tmout()
299{
300 char *p;
301
302 if( (p = getenv("DIM_WRITE_TMOUT")) == NULL )
303 return(0);
304 else {
305 return(atoi(p));
306 }
307}
Note: See TracBrowser for help on using the repository browser.