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

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