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>
19 #include "afs/sysincludes.h"
20 #include "afs/afsutil.h"
24 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
42 /* also parse a.b.c.d addresses */
44 hostutil_GetHostByName(register char *ahost)
47 static struct hostent thostent;
48 static char *addrp[2];
50 register char *ptr = ahost;
51 afs_uint32 tval, numeric = 0;
54 tc = *ahost; /* look at the first char */
55 if (tc >= '0' && tc <= '9') {
57 while ((tc = *ptr++)) {
64 } else if (tc > '9' || tc < '0') {
71 tc = *ahost; /* look at the first char */
72 /* decimal address, return fake hostent with only hostaddr field good */
75 memset(addr, 0, sizeof(addr));
76 while ((tc = *ahost++)) {
79 return NULL; /* too many dots */
82 } else if (tc > '9' || tc < '0')
94 thostent.h_addr_list = &addrp[0];
96 /* 4.2 and older systems */
97 thostent.h_addr = addr;
102 if (afs_winsockInit() < 0)
105 return gethostbyname(ahost);
109 /* Translate an internet address into a nice printable string. The
110 * variable addr is in network byte order.
113 hostutil_GetNameByINet(afs_uint32 addr)
116 static char tbuffer[256];
119 if (afs_winsockInit() < 0)
122 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
124 strcpy(tbuffer, th->h_name);
127 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
128 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
135 /* the parameter is a pointer to a buffer containing a string of
137 ** w.x.y.z # machineName
138 ** returns the network interface in network byte order
141 extractAddr(char *line, int maxSize)
143 char byte1[32], byte2[32], byte3[32], byte4[32];
146 afs_uint32 val1, val2, val3, val4;
149 /* skip empty spaces */
150 while (isspace(*line) && maxSize) {
155 /* skip empty lines */
156 if (!maxSize || !*line)
157 return AFS_IPINVALIDIGNORE;
159 while ((*line != '.') && maxSize) { /* extract first byte */
161 return AFS_IPINVALID;
163 return AFS_IPINVALID; /* no space */
164 byte1[i++] = *line++;
168 return AFS_IPINVALID;
172 while ((*line != '.') && maxSize) { /* extract second byte */
174 return AFS_IPINVALID;
176 return AFS_IPINVALID; /* no space */
177 byte2[i++] = *line++;
181 return AFS_IPINVALID;
185 while ((*line != '.') && maxSize) {
187 return AFS_IPINVALID;
189 return AFS_IPINVALID; /* no space */
190 byte3[i++] = *line++;
194 return AFS_IPINVALID;
198 while (*line && !isspace(*line) && maxSize) {
200 return AFS_IPINVALID;
202 return AFS_IPINVALID; /* no space */
203 byte4[i++] = *line++;
207 return AFS_IPINVALID;
211 val1 = strtol(byte1, &endPtr, 10);
212 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
213 return AFS_IPINVALID;
216 val2 = strtol(byte2, &endPtr, 10);
217 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
218 return AFS_IPINVALID;
221 val3 = strtol(byte3, &endPtr, 10);
222 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
223 return AFS_IPINVALID;
226 val4 = strtol(byte4, &endPtr, 10);
227 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
228 return AFS_IPINVALID;
230 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
236 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
237 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
238 ** causes a memory fault.
241 afs_inet_ntoa(afs_uint32 addr)
245 return (char *)inet_ntoa(temp);
248 /* same as above, but to a non-static buffer, must be freed by called */
250 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
255 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
256 (temp >> 8) & 0xff, (temp) & 0xff);
261 * gettmpdir() -- Returns pointer to global temporary directory string.
262 * Always succeeds. Never attempt to deallocate directory string.
268 char *tmpdirp = NULL;
271 static char *saveTmpDir = NULL;
273 if (saveTmpDir == NULL) {
274 /* initialize global temporary directory string */
275 char *dirp = (char *)malloc(MAX_PATH);
279 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
281 if (pathLen == 0 || pathLen > MAX_PATH) {
282 /* can't get tmp path; get cur work dir */
283 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
284 if (pathLen == 0 || pathLen > MAX_PATH) {
291 /* Have a valid dir path; check that actually exists. */
292 DWORD fileAttr = GetFileAttributes(dirp);
294 if ((fileAttr == 0xFFFFFFFF)
295 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
303 FilepathNormalize(dirp);
305 /* most likely TMP or TEMP env vars specify a non-existent dir */
310 /* atomically initialize shared buffer pointer IF still null */
313 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
314 /* shared buffer pointer already initialized by another thread */
318 } /* interlock xchng */
321 /* Above is what we really want to do, but Windows 95 does not have
322 * InterlockedCompareExchange(). So we just atomically swap
323 * the buffer pointer values but we do NOT deallocate the
324 * previously installed buffer, if any, in case it is in use.
327 InterlockedExchange64((LONGLONG)(INT_PTR)&saveTmpDir, (LONGLONG) dirp);
329 InterlockedExchange((LONG) & saveTmpDir, (LONG) dirp);
333 /* if (!saveTmpDir) */
334 tmpdirp = saveTmpDir;
337 #endif /* AFS_NT40_ENV */