2 * Copyright 2000, International Business Machines Corporation and others.
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
14 #include <afs/param.h>
16 #include "../afs/sysincludes.h"
17 #include "../afs/afsutil.h"
20 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
36 /* also parse a.b.c.d addresses */
37 struct hostent *hostutil_GetHostByName(ahost)
38 register char *ahost; {
40 static struct hostent thostent;
41 static char *addrp[2];
43 register char *ptr = ahost;
44 afs_int32 tval, numeric=0;
47 tc = *ahost; /* look at the first char */
48 if (tc >= '0' && tc <= '9') {
57 } else if (tc > '9' || tc < '0') {
64 tc = *ahost; /* look at the first char */
65 /* decimal address, return fake hostent with only hostaddr field good */
68 bzero(addr, sizeof(addr));
69 while (tc = *ahost++) {
71 if (dots >= 3) return (struct hostent *) 0; /* too many dots */
75 else if (tc > '9' || tc < '0') return (struct hostent *) 0;
85 addrp[1] = (char *) 0;
86 thostent.h_addr_list = &addrp[0];
88 /* 4.2 and older systems */
89 thostent.h_addr = addr;
95 if (afs_winsockInit()<0)
96 return (struct hostent *)0;
98 return gethostbyname(ahost);
102 /* Translate an internet address into a nice printable string. The
103 * variable addr is in network byte order.
105 char *hostutil_GetNameByINet(addr)
109 static char tbuffer[256];
112 if (afs_winsockInit()<0)
115 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
117 strcpy(tbuffer, th->h_name);
120 sprintf(tbuffer, "%d.%d.%d.%d",
121 ((addr>>24) & 0xff), ((addr>>16) & 0xff),
122 ((addr>>8) & 0xff), ( addr & 0xff));
128 /* the parameter is a pointer to a buffer containing a string of
130 ** w.x.y.z # machineName
131 ** returns the network interface in network byte order
134 extractAddr(line, maxSize)
138 char byte1[32], byte2[32], byte3[32], byte4[32];
141 afs_int32 val1, val2, val3, val4;
144 /* skip empty spaces */
145 while ( isspace(*line) && maxSize )
151 /* skip empty lines */
152 if ( !maxSize || !*line ) return AFS_IPINVALIDIGNORE;
154 while ( ( *line != '.' ) && maxSize) /* extract first byte */
156 if ( !isdigit(*line) ) return AFS_IPINVALID;
157 if ( i > 31 ) return AFS_IPINVALID; /* no space */
158 byte1[i++] = *line++;
161 if ( !maxSize ) return AFS_IPINVALID;
165 while ( ( *line != '.' ) && maxSize) /* extract second byte */
167 if ( !isdigit(*line) ) return AFS_IPINVALID;
168 if ( i > 31 ) return AFS_IPINVALID; /* no space */
169 byte2[i++] = *line++;
172 if ( !maxSize ) return AFS_IPINVALID;
176 while ( ( *line != '.' ) && maxSize)
178 if ( !isdigit(*line) ) return AFS_IPINVALID;
179 if ( i > 31 ) return AFS_IPINVALID; /* no space */
180 byte3[i++] = *line++;
183 if ( !maxSize ) return AFS_IPINVALID;
187 while ( *line && !isspace(*line) && maxSize)
189 if ( !isdigit(*line) ) return AFS_IPINVALID;
190 if ( i > 31 ) return AFS_IPINVALID; /* no space */
191 byte4[i++] = *line++;
194 if ( !maxSize ) return AFS_IPINVALID;
198 val1 = strtol(byte1, &endPtr, 10);
199 if (( val1== 0 ) && ( errno != 0 || byte1 == endPtr))
200 return AFS_IPINVALID;
203 val2 = strtol(byte2, &endPtr, 10);
204 if (( val2== 0 ) && ( errno !=0 || byte2 == endPtr))/* no conversion */
205 return AFS_IPINVALID;
208 val3 = strtol(byte3, &endPtr, 10);
209 if (( val3== 0 ) && ( errno !=0 || byte3 == endPtr))/* no conversion */
210 return AFS_IPINVALID;
213 val4 = strtol(byte4, &endPtr, 10);
214 if (( val4== 0 ) && ( errno !=0 || byte4 == endPtr))/* no conversion */
215 return AFS_IPINVALID;
217 val = ( val1 << 24 ) | ( val2 << 16 ) | ( val3 << 8 ) | val4;
223 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
224 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
225 ** causes a memory fault.
227 char* afs_inet_ntoa(afs_int32 addr)
231 return (char *) inet_ntoa(temp);
235 * gettmpdir() -- Returns pointer to global temporary directory string.
236 * Always succeeds. Never attempt to deallocate directory string.
239 char *gettmpdir(void)
241 char *tmpdirp = NULL;
244 static char *saveTmpDir = NULL;
246 if (saveTmpDir == NULL) {
247 /* initialize global temporary directory string */
248 char *dirp = (char *)malloc(MAX_PATH);
252 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
254 if (pathLen == 0 || pathLen > MAX_PATH) {
255 /* can't get tmp path; get cur work dir */
256 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
257 if (pathLen == 0 || pathLen > MAX_PATH) {
264 /* Have a valid dir path; check that actually exists. */
265 DWORD fileAttr = GetFileAttributes(dirp);
267 if ((fileAttr == 0xFFFFFFFF) ||
268 ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
276 FilepathNormalize(dirp);
278 /* most likely TMP or TEMP env vars specify a non-existent dir */
283 /* atomically initialize shared buffer pointer IF still null */
286 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
287 /* shared buffer pointer already initialized by another thread */
291 } /* interlock xchng */
294 /* Above is what we really want to do, but Windows 95 does not have
295 * InterlockedCompareExchange(). So we just atomically swap
296 * the buffer pointer values but we do NOT deallocate the
297 * previously installed buffer, if any, in case it is in use.
299 InterlockedExchange((LPLONG)&saveTmpDir, (LONG)dirp);
301 } /* if (!saveTmpDir) */
303 tmpdirp = saveTmpDir;
306 #endif /* AFS_NT40_ENV */