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 <afsconfig.h>
15 #include <afs/param.h>
20 #include "../afs/sysincludes.h"
21 #include "../afs/afsutil.h"
24 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
48 /* also parse a.b.c.d addresses */
49 struct hostent *hostutil_GetHostByName(ahost)
50 register char *ahost; {
52 static struct hostent thostent;
53 static char *addrp[2];
55 register char *ptr = ahost;
56 afs_uint32 tval, numeric=0;
59 tc = *ahost; /* look at the first char */
60 if (tc >= '0' && tc <= '9') {
62 while ((tc = *ptr++)) {
69 } else if (tc > '9' || tc < '0') {
76 tc = *ahost; /* look at the first char */
77 /* decimal address, return fake hostent with only hostaddr field good */
80 memset(addr, 0, sizeof(addr));
81 while ((tc = *ahost++)) {
83 if (dots >= 3) return (struct hostent *) 0; /* too many dots */
87 else if (tc > '9' || tc < '0') return (struct hostent *) 0;
97 addrp[1] = (char *) 0;
98 thostent.h_addr_list = &addrp[0];
100 /* 4.2 and older systems */
101 thostent.h_addr = addr;
107 if (afs_winsockInit()<0)
108 return (struct hostent *)0;
110 return gethostbyname(ahost);
114 /* Translate an internet address into a nice printable string. The
115 * variable addr is in network byte order.
117 char *hostutil_GetNameByINet(addr)
121 static char tbuffer[256];
124 if (afs_winsockInit()<0)
127 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
129 strcpy(tbuffer, th->h_name);
132 sprintf(tbuffer, "%d.%d.%d.%d",
133 (int)((addr>>24) & 0xff), (int)((addr>>16) & 0xff),
134 (int)((addr>>8) & 0xff), (int)( addr & 0xff));
140 /* the parameter is a pointer to a buffer containing a string of
142 ** w.x.y.z # machineName
143 ** returns the network interface in network byte order
146 extractAddr(line, maxSize)
150 char byte1[32], byte2[32], byte3[32], byte4[32];
153 afs_uint32 val1, val2, val3, val4;
156 /* skip empty spaces */
157 while ( isspace(*line) && maxSize )
163 /* skip empty lines */
164 if ( !maxSize || !*line ) return AFS_IPINVALIDIGNORE;
166 while ( ( *line != '.' ) && maxSize) /* extract first byte */
168 if ( !isdigit(*line) ) return AFS_IPINVALID;
169 if ( i > 31 ) return AFS_IPINVALID; /* no space */
170 byte1[i++] = *line++;
173 if ( !maxSize ) return AFS_IPINVALID;
177 while ( ( *line != '.' ) && maxSize) /* extract second byte */
179 if ( !isdigit(*line) ) return AFS_IPINVALID;
180 if ( i > 31 ) return AFS_IPINVALID; /* no space */
181 byte2[i++] = *line++;
184 if ( !maxSize ) return AFS_IPINVALID;
188 while ( ( *line != '.' ) && maxSize)
190 if ( !isdigit(*line) ) return AFS_IPINVALID;
191 if ( i > 31 ) return AFS_IPINVALID; /* no space */
192 byte3[i++] = *line++;
195 if ( !maxSize ) return AFS_IPINVALID;
199 while ( *line && !isspace(*line) && maxSize)
201 if ( !isdigit(*line) ) return AFS_IPINVALID;
202 if ( i > 31 ) return AFS_IPINVALID; /* no space */
203 byte4[i++] = *line++;
206 if ( !maxSize ) return AFS_IPINVALID;
210 val1 = strtol(byte1, &endPtr, 10);
211 if (( val1== 0 ) && ( errno != 0 || byte1 == endPtr))
212 return AFS_IPINVALID;
215 val2 = strtol(byte2, &endPtr, 10);
216 if (( val2== 0 ) && ( errno !=0 || byte2 == endPtr))/* no conversion */
217 return AFS_IPINVALID;
220 val3 = strtol(byte3, &endPtr, 10);
221 if (( val3== 0 ) && ( errno !=0 || byte3 == endPtr))/* no conversion */
222 return AFS_IPINVALID;
225 val4 = strtol(byte4, &endPtr, 10);
226 if (( val4== 0 ) && ( errno !=0 || byte4 == endPtr))/* no conversion */
227 return AFS_IPINVALID;
229 val = ( val1 << 24 ) | ( val2 << 16 ) | ( val3 << 8 ) | val4;
235 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
236 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
237 ** causes a memory fault.
239 char* afs_inet_ntoa(afs_uint32 addr)
243 return (char *) inet_ntoa(temp);
246 /* same as above, but to a non-static buffer, must be freed by called */
247 char* afs_inet_ntoa_r(afs_uint32 addr, char *buf)
252 sprintf(buf, "%d.%d.%d.%d",
253 (temp >> 24 ) & 0xff,
254 (temp >> 16 ) & 0xff,
261 * gettmpdir() -- Returns pointer to global temporary directory string.
262 * Always succeeds. Never attempt to deallocate directory string.
265 char *gettmpdir(void)
267 char *tmpdirp = NULL;
270 static char *saveTmpDir = NULL;
272 if (saveTmpDir == NULL) {
273 /* initialize global temporary directory string */
274 char *dirp = (char *)malloc(MAX_PATH);
278 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
280 if (pathLen == 0 || pathLen > MAX_PATH) {
281 /* can't get tmp path; get cur work dir */
282 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
283 if (pathLen == 0 || pathLen > MAX_PATH) {
290 /* Have a valid dir path; check that actually exists. */
291 DWORD fileAttr = GetFileAttributes(dirp);
293 if ((fileAttr == 0xFFFFFFFF) ||
294 ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
302 FilepathNormalize(dirp);
304 /* most likely TMP or TEMP env vars specify a non-existent dir */
309 /* atomically initialize shared buffer pointer IF still null */
312 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
313 /* shared buffer pointer already initialized by another thread */
317 } /* interlock xchng */
320 /* Above is what we really want to do, but Windows 95 does not have
321 * InterlockedCompareExchange(). So we just atomically swap
322 * the buffer pointer values but we do NOT deallocate the
323 * previously installed buffer, if any, in case it is in use.
325 InterlockedExchange((LPLONG)&saveTmpDir, (LONG)dirp);
327 } /* if (!saveTmpDir) */
329 tmpdirp = saveTmpDir;
332 #endif /* AFS_NT40_ENV */