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>
21 #include "afs/sysincludes.h"
22 #include "afs/afsutil.h"
25 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
49 /* also parse a.b.c.d addresses */
51 hostutil_GetHostByName(register char *ahost)
54 static struct hostent thostent;
55 static char *addrp[2];
57 register char *ptr = ahost;
58 afs_uint32 tval, numeric = 0;
61 tc = *ahost; /* look at the first char */
62 if (tc >= '0' && tc <= '9') {
64 while ((tc = *ptr++)) {
71 } else if (tc > '9' || tc < '0') {
78 tc = *ahost; /* look at the first char */
79 /* decimal address, return fake hostent with only hostaddr field good */
82 memset(addr, 0, sizeof(addr));
83 while ((tc = *ahost++)) {
86 return NULL; /* too many dots */
89 } else if (tc > '9' || tc < '0')
101 thostent.h_addr_list = &addrp[0];
103 /* 4.2 and older systems */
104 thostent.h_addr = addr;
109 if (afs_winsockInit() < 0)
112 return gethostbyname(ahost);
116 /* Translate an internet address into a nice printable string. The
117 * variable addr is in network byte order.
120 hostutil_GetNameByINet(afs_uint32 addr)
123 static char tbuffer[256];
126 if (afs_winsockInit() < 0)
129 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
131 strcpy(tbuffer, th->h_name);
134 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
135 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
142 /* the parameter is a pointer to a buffer containing a string of
144 ** w.x.y.z # machineName
145 ** returns the network interface in network byte order
148 extractAddr(char *line, int 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) {
162 /* skip empty lines */
163 if (!maxSize || !*line)
164 return AFS_IPINVALIDIGNORE;
166 while ((*line != '.') && maxSize) { /* extract first byte */
168 return AFS_IPINVALID;
170 return AFS_IPINVALID; /* no space */
171 byte1[i++] = *line++;
175 return AFS_IPINVALID;
179 while ((*line != '.') && maxSize) { /* extract second byte */
181 return AFS_IPINVALID;
183 return AFS_IPINVALID; /* no space */
184 byte2[i++] = *line++;
188 return AFS_IPINVALID;
192 while ((*line != '.') && maxSize) {
194 return AFS_IPINVALID;
196 return AFS_IPINVALID; /* no space */
197 byte3[i++] = *line++;
201 return AFS_IPINVALID;
205 while (*line && !isspace(*line) && maxSize) {
207 return AFS_IPINVALID;
209 return AFS_IPINVALID; /* no space */
210 byte4[i++] = *line++;
214 return AFS_IPINVALID;
218 val1 = strtol(byte1, &endPtr, 10);
219 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
220 return AFS_IPINVALID;
223 val2 = strtol(byte2, &endPtr, 10);
224 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
225 return AFS_IPINVALID;
228 val3 = strtol(byte3, &endPtr, 10);
229 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
230 return AFS_IPINVALID;
233 val4 = strtol(byte4, &endPtr, 10);
234 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
235 return AFS_IPINVALID;
237 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
243 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
244 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
245 ** causes a memory fault.
248 afs_inet_ntoa(afs_uint32 addr)
252 return (char *)inet_ntoa(temp);
255 /* same as above, but to a non-static buffer, must be freed by called */
257 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
262 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
263 (temp >> 8) & 0xff, (temp) & 0xff);
268 * gettmpdir() -- Returns pointer to global temporary directory string.
269 * Always succeeds. Never attempt to deallocate directory string.
275 char *tmpdirp = NULL;
278 static char *saveTmpDir = NULL;
280 if (saveTmpDir == NULL) {
281 /* initialize global temporary directory string */
282 char *dirp = (char *)malloc(MAX_PATH);
286 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
288 if (pathLen == 0 || pathLen > MAX_PATH) {
289 /* can't get tmp path; get cur work dir */
290 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
291 if (pathLen == 0 || pathLen > MAX_PATH) {
298 /* Have a valid dir path; check that actually exists. */
299 DWORD fileAttr = GetFileAttributes(dirp);
301 if ((fileAttr == 0xFFFFFFFF)
302 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
310 FilepathNormalize(dirp);
312 /* most likely TMP or TEMP env vars specify a non-existent dir */
317 /* atomically initialize shared buffer pointer IF still null */
320 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
321 /* shared buffer pointer already initialized by another thread */
325 } /* interlock xchng */
328 /* Above is what we really want to do, but Windows 95 does not have
329 * InterlockedCompareExchange(). So we just atomically swap
330 * the buffer pointer values but we do NOT deallocate the
331 * previously installed buffer, if any, in case it is in use.
334 InterlockedExchange64((LONGLONG)(INT_PTR)&saveTmpDir, (LONGLONG) dirp);
336 InterlockedExchange((LONG) & saveTmpDir, (LONG) dirp);
340 /* if (!saveTmpDir) */
341 tmpdirp = saveTmpDir;
344 #endif /* AFS_NT40_ENV */