pull-prototypes-to-head-20020821
[openafs.git] / src / util / hostparse.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * ALL RIGHTS RESERVED
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID("$Header$");
18
19 #ifdef UKERNEL
20 #include "../afs/sysincludes.h"
21 #include "../afs/afsutil.h"
22 #else /* UKERNEL */
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <stdlib.h>
26 #ifdef AFS_NT40_ENV
27 #include <winsock2.h>
28 #include <direct.h>
29 #else
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <ctype.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #else
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42 #endif
43 #include <errno.h>
44 #include "afsutil.h"
45 #endif /* UKERNEL */
46
47
48 /* also parse a.b.c.d addresses */
49 struct hostent *hostutil_GetHostByName(register char *ahost)
50 {
51     register int tc;
52     static struct hostent thostent;
53     static char *addrp[2];
54     static char addr[4];
55     register char *ptr = ahost;
56     afs_uint32 tval, numeric=0;
57     int dots=0;
58
59     tc = *ahost;    /* look at the first char */
60     if (tc >= '0' && tc <= '9') {
61         numeric = 1;
62         while ((tc = *ptr++)) {
63             if (tc == '.') {
64                 if (dots >= 3) {
65                     numeric = 0;
66                     break;
67                 }
68                 dots++;
69             } else if (tc > '9' || tc < '0') {
70                 numeric = 0;
71                 break;
72             } 
73         }
74     }
75     if (numeric) {
76         tc = *ahost;    /* look at the first char */
77         /* decimal address, return fake hostent with only hostaddr field good */
78         tval = 0;
79         dots = 0;
80         memset(addr, 0, sizeof(addr));
81         while ((tc = *ahost++)) {
82             if (tc == '.') {
83                 if (dots >= 3) return NULL; /* too many dots */
84                 addr[dots++] = tval;
85                 tval = 0;
86             }
87             else if (tc > '9' || tc < '0') return NULL;
88             else {
89                 tval *= 10;
90                 tval += tc - '0';
91             }
92         }
93         addr[dots] = tval;
94 #ifdef h_addr
95         /* 4.3 system */
96         addrp[0] = addr;
97         addrp[1] = NULL;
98         thostent.h_addr_list = &addrp[0];
99 #else /* h_addr */
100         /* 4.2 and older systems */
101         thostent.h_addr = addr;
102 #endif /* h_addr */
103         return &thostent;
104     }
105     else {
106 #ifdef AFS_NT40_ENV
107         if (afs_winsockInit()<0) 
108             return NULL;
109 #endif
110         return gethostbyname(ahost);
111     }
112 }
113
114 /* Translate an internet address into a nice printable string. The
115  * variable addr is in network byte order.
116  */
117 char *hostutil_GetNameByINet(afs_uint32 addr)
118 {
119   struct hostent *th;
120   static char    tbuffer[256];
121
122 #ifdef AFS_NT40_ENV
123         if (afs_winsockInit()<0) 
124             return NULL;
125 #endif
126   th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
127   if (th) {
128      strcpy(tbuffer, th->h_name);
129   } else {
130      addr = ntohl(addr);
131      sprintf(tbuffer, "%d.%d.%d.%d", 
132              (int)((addr>>24) & 0xff), (int)((addr>>16) & 0xff),
133              (int)((addr>>8)  & 0xff), (int)( addr      & 0xff));
134   }
135   
136     return tbuffer;
137 }
138
139 /* the parameter is a pointer to a buffer containing a string of 
140 ** bytes of the form 
141 ** w.x.y.z      # machineName
142 ** returns the network interface in network byte order 
143 */
144 afs_uint32 extractAddr(char *line, int maxSize)
145 {
146         char byte1[32], byte2[32], byte3[32], byte4[32];
147         int i=0;
148         char*   endPtr;
149         afs_uint32 val1, val2, val3, val4;
150         afs_uint32 val=0;
151
152         /* skip empty spaces */
153         while ( isspace(*line) && maxSize ) 
154         {
155                 line++;
156                 maxSize--;
157         }
158         
159         /* skip empty lines */
160         if ( !maxSize || !*line ) return  AFS_IPINVALIDIGNORE;
161         
162         while ( ( *line != '.' ) && maxSize) /* extract first byte */
163         {
164                 if ( !isdigit(*line) ) return AFS_IPINVALID;
165                 if ( i > 31 ) return AFS_IPINVALID; /* no space */
166                 byte1[i++] = *line++;
167                 maxSize--;
168         }
169         if ( !maxSize ) return AFS_IPINVALID;
170         byte1[i] = 0;
171         
172         i=0, line++;
173         while ( ( *line != '.' ) && maxSize) /* extract second byte */
174         {
175                 if ( !isdigit(*line) ) return AFS_IPINVALID;
176                 if ( i > 31 ) return AFS_IPINVALID; /* no space */
177                 byte2[i++] = *line++;
178                 maxSize--;
179         }
180         if ( !maxSize ) return AFS_IPINVALID;
181         byte2[i] = 0;
182
183         i=0, line++;
184         while ( ( *line != '.' ) && maxSize)
185         {
186                 if ( !isdigit(*line) ) return AFS_IPINVALID;
187                 if ( i > 31 ) return AFS_IPINVALID; /* no space */
188                 byte3[i++] = *line++;
189                 maxSize--;
190         }
191         if ( !maxSize ) return AFS_IPINVALID;
192         byte3[i] = 0;
193
194         i=0, line++;
195         while ( *line && !isspace(*line) && maxSize)
196         {
197                 if ( !isdigit(*line) ) return AFS_IPINVALID;
198                 if ( i > 31 ) return AFS_IPINVALID; /* no space */
199                 byte4[i++] = *line++;
200                 maxSize--;
201         }
202         if ( !maxSize ) return AFS_IPINVALID;
203         byte4[i] = 0;
204         
205         errno=0;
206         val1 = strtol(byte1, &endPtr, 10);
207         if (( val1== 0 ) && ( errno != 0 || byte1 == endPtr)) 
208                 return AFS_IPINVALID;
209         
210         errno=0;
211         val2 = strtol(byte2, &endPtr, 10);
212         if (( val2== 0 ) && ( errno !=0 || byte2 == endPtr))/* no conversion */
213                 return AFS_IPINVALID;
214
215         errno=0;
216         val3 = strtol(byte3, &endPtr, 10);
217         if (( val3== 0 ) && ( errno !=0 || byte3 == endPtr))/* no conversion */
218                 return AFS_IPINVALID;
219
220         errno=0;
221         val4 = strtol(byte4, &endPtr, 10);
222         if (( val4== 0 ) && ( errno !=0 || byte4 == endPtr))/* no conversion */
223                 return AFS_IPINVALID;
224
225         val = ( val1 << 24 ) | ( val2 << 16 ) | ( val3 << 8 ) | val4;
226         val = htonl(val);
227         return val;
228 }
229
230 /* 
231 ** converts a 4byte IP address into a static string (e.g. w.x.y.z) 
232 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
233 ** causes a memory fault. 
234 */
235 char *afs_inet_ntoa(afs_uint32 addr)
236 {
237     struct in_addr temp;
238     temp.s_addr = addr;
239     return (char *) inet_ntoa(temp);
240 }
241
242 /* same as above, but to a non-static buffer, must be freed by called */
243 char *afs_inet_ntoa_r(afs_uint32 addr, char *buf)
244 {
245     int temp;
246
247     temp = ntohl(addr);
248     sprintf(buf, "%d.%d.%d.%d", 
249             (temp >> 24 ) & 0xff,
250             (temp >> 16 ) & 0xff, 
251             (temp >> 8  ) & 0xff,
252             (temp       ) & 0xff);
253     return buf;
254 }
255
256 /*
257  * gettmpdir() -- Returns pointer to global temporary directory string.
258  *     Always succeeds.  Never attempt to deallocate directory string.
259  */
260
261 char *gettmpdir(void)
262 {
263     char *tmpdirp = NULL;
264     
265 #ifdef AFS_NT40_ENV
266     static char *saveTmpDir = NULL;
267
268     if (saveTmpDir == NULL) {
269         /* initialize global temporary directory string */
270         char *dirp = (char *)malloc(MAX_PATH);
271         int freeDirp = 1;
272
273         if (dirp != NULL) {
274             DWORD pathLen = GetTempPath(MAX_PATH, dirp);
275
276             if (pathLen == 0 || pathLen > MAX_PATH) {
277                 /* can't get tmp path; get cur work dir */
278                 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
279                 if (pathLen == 0 || pathLen > MAX_PATH) {
280                     free(dirp);
281                     dirp = NULL;
282                 }
283             }
284
285             if (dirp != NULL) {
286                 /* Have a valid dir path; check that actually exists. */
287                 DWORD fileAttr = GetFileAttributes(dirp);
288
289                 if ((fileAttr == 0xFFFFFFFF) ||
290                     ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
291                     free(dirp);
292                     dirp = NULL;
293                 }
294             }
295         } /* dirp != NULL */
296         
297         if (dirp != NULL) {
298             FilepathNormalize(dirp);
299         } else {
300             /* most likely TMP or TEMP env vars specify a non-existent dir */
301             dirp = "/";
302             freeDirp = 0;
303         }
304
305         /* atomically initialize shared buffer pointer IF still null */
306
307 #if 0
308         if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
309             /* shared buffer pointer already initialized by another thread */
310             if (freeDirp) {
311                 free(dirp);
312             }
313         } /* interlock xchng */
314 #endif
315
316         /* Above is what we really want to do, but Windows 95 does not have
317          * InterlockedCompareExchange().  So we just atomically swap
318          * the buffer pointer values but we do NOT deallocate the
319          * previously installed buffer, if any, in case it is in use.
320          */
321         InterlockedExchange((LPLONG)&saveTmpDir, (LONG)dirp);
322
323     } /* if (!saveTmpDir) */
324     
325     tmpdirp = saveTmpDir;
326 #else
327     tmpdirp = "/tmp";
328 #endif /* AFS_NT40_ENV */
329     
330     return tmpdirp;
331 }
332
333