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>
43 /* also parse a.b.c.d addresses */
45 hostutil_GetHostByName(register char *ahost)
48 static struct hostent thostent;
49 static char *addrp[2];
51 register char *ptr = ahost;
52 afs_uint32 tval, numeric = 0;
55 tc = *ahost; /* look at the first char */
56 if (tc >= '0' && tc <= '9') {
58 while ((tc = *ptr++)) {
65 } else if (tc > '9' || tc < '0') {
72 tc = *ahost; /* look at the first char */
73 /* decimal address, return fake hostent with only hostaddr field good */
76 memset(addr, 0, sizeof(addr));
77 while ((tc = *ahost++)) {
80 return NULL; /* too many dots */
83 } else if (tc > '9' || tc < '0')
95 thostent.h_addr_list = &addrp[0];
97 /* 4.2 and older systems */
98 thostent.h_addr = addr;
103 if (afs_winsockInit() < 0)
106 return gethostbyname(ahost);
110 /* Translate an internet address into a nice printable string. The
111 * variable addr is in network byte order.
114 hostutil_GetNameByINet(afs_uint32 addr)
117 static char tbuffer[256];
120 if (afs_winsockInit() < 0)
123 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
125 strcpy(tbuffer, th->h_name);
128 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
129 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
136 /* the parameter is a pointer to a buffer containing a string of
138 ** w.x.y.z # machineName
139 ** returns the network interface in network byte order
142 extractAddr(char *line, int maxSize)
144 char byte1[32], byte2[32], byte3[32], byte4[32];
147 afs_uint32 val1, val2, val3, val4;
150 /* skip empty spaces */
151 while (isspace(*line) && maxSize) {
156 /* skip empty lines */
157 if (!maxSize || !*line)
158 return AFS_IPINVALIDIGNORE;
160 while ((*line != '.') && maxSize) { /* extract first byte */
162 return AFS_IPINVALID;
164 return AFS_IPINVALID; /* no space */
165 byte1[i++] = *line++;
169 return AFS_IPINVALID;
173 while ((*line != '.') && maxSize) { /* extract second byte */
175 return AFS_IPINVALID;
177 return AFS_IPINVALID; /* no space */
178 byte2[i++] = *line++;
182 return AFS_IPINVALID;
186 while ((*line != '.') && maxSize) {
188 return AFS_IPINVALID;
190 return AFS_IPINVALID; /* no space */
191 byte3[i++] = *line++;
195 return AFS_IPINVALID;
199 while (*line && !isspace(*line) && maxSize) {
201 return AFS_IPINVALID;
203 return AFS_IPINVALID; /* no space */
204 byte4[i++] = *line++;
208 return AFS_IPINVALID;
212 val1 = strtol(byte1, &endPtr, 10);
213 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
214 return AFS_IPINVALID;
217 val2 = strtol(byte2, &endPtr, 10);
218 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
219 return AFS_IPINVALID;
222 val3 = strtol(byte3, &endPtr, 10);
223 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
224 return AFS_IPINVALID;
227 val4 = strtol(byte4, &endPtr, 10);
228 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
229 return AFS_IPINVALID;
231 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
237 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
238 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
239 ** causes a memory fault.
242 afs_inet_ntoa(afs_uint32 addr)
246 return (char *)inet_ntoa(temp);
249 /* same as above, but to a non-static buffer, must be freed by called */
251 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
256 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
257 (temp >> 8) & 0xff, (temp) & 0xff);
262 * gettmpdir() -- Returns pointer to global temporary directory string.
263 * Always succeeds. Never attempt to deallocate directory string.
269 char *tmpdirp = NULL;
272 static char *saveTmpDir = NULL;
274 if (saveTmpDir == NULL) {
275 /* initialize global temporary directory string */
276 char *dirp = (char *)malloc(MAX_PATH);
280 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
282 if (pathLen == 0 || pathLen > MAX_PATH) {
283 /* can't get tmp path; get cur work dir */
284 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
285 if (pathLen == 0 || pathLen > MAX_PATH) {
292 /* Have a valid dir path; check that actually exists. */
293 DWORD fileAttr = GetFileAttributes(dirp);
295 if ((fileAttr == 0xFFFFFFFF)
296 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
304 FilepathNormalize(dirp);
306 /* most likely TMP or TEMP env vars specify a non-existent dir */
311 /* atomically initialize shared buffer pointer IF still null */
314 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
315 /* shared buffer pointer already initialized by another thread */
319 } /* interlock xchng */
322 /* Above is what we really want to do, but Windows 95 does not have
323 * InterlockedCompareExchange(). So we just atomically swap
324 * the buffer pointer values but we do NOT deallocate the
325 * previously installed buffer, if any, in case it is in use.
328 InterlockedExchange64((LONGLONG)(INT_PTR)&saveTmpDir, (LONGLONG) dirp);
330 InterlockedExchange((LONG) & saveTmpDir, (LONG) dirp);
334 /* if (!saveTmpDir) */
335 tmpdirp = saveTmpDir;
338 #endif /* AFS_NT40_ENV */