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

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