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 <sys/types.h>
25 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
36 /* also parse a.b.c.d addresses */
38 hostutil_GetHostByName(char *ahost)
41 static struct hostent thostent;
42 static char *addrp[2];
45 afs_uint32 tval, numeric = 0;
48 tc = *ahost; /* look at the first char */
49 if (tc >= '0' && tc <= '9') {
51 while ((tc = *ptr++)) {
58 } else if (tc > '9' || tc < '0') {
65 tc = *ahost; /* look at the first char */
66 /* decimal address, return fake hostent with only hostaddr field good */
69 memset(addr, 0, sizeof(addr));
70 while ((tc = *ahost++)) {
73 return NULL; /* too many dots */
76 } else if (tc > '9' || tc < '0')
88 thostent.h_addr_list = &addrp[0];
90 /* 4.2 and older systems */
91 thostent.h_addr = addr;
96 if (afs_winsockInit() < 0)
99 return gethostbyname(ahost);
103 /* Translate an internet address into a nice printable string. The
104 * variable addr is in network byte order.
107 hostutil_GetNameByINet(afs_uint32 addr)
110 static char tbuffer[256];
113 if (afs_winsockInit() < 0)
116 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
118 strcpy(tbuffer, th->h_name);
121 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
122 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
129 /* the parameter is a pointer to a buffer containing a string of
131 ** w.x.y.z # machineName
132 ** returns the network interface in network byte order
135 extractAddr(char *line, int maxSize)
137 char byte1[32], byte2[32], byte3[32], byte4[32];
140 afs_uint32 val1, val2, val3, val4;
143 /* skip empty spaces */
144 while (isspace(*line) && maxSize) {
149 /* skip empty lines */
150 if (!maxSize || !*line)
151 return AFS_IPINVALIDIGNORE;
153 while ((*line != '.') && maxSize) { /* extract first byte */
155 return AFS_IPINVALID;
157 return AFS_IPINVALID; /* no space */
158 byte1[i++] = *line++;
162 return AFS_IPINVALID;
166 while ((*line != '.') && maxSize) { /* extract second byte */
168 return AFS_IPINVALID;
170 return AFS_IPINVALID; /* no space */
171 byte2[i++] = *line++;
175 return AFS_IPINVALID;
179 while ((*line != '.') && maxSize) {
181 return AFS_IPINVALID;
183 return AFS_IPINVALID; /* no space */
184 byte3[i++] = *line++;
188 return AFS_IPINVALID;
192 while (*line && !isspace(*line) && maxSize) {
194 return AFS_IPINVALID;
196 return AFS_IPINVALID; /* no space */
197 byte4[i++] = *line++;
201 return AFS_IPINVALID;
205 val1 = strtol(byte1, &endPtr, 10);
206 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
207 return AFS_IPINVALID;
210 val2 = strtol(byte2, &endPtr, 10);
211 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
212 return AFS_IPINVALID;
215 val3 = strtol(byte3, &endPtr, 10);
216 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
217 return AFS_IPINVALID;
220 val4 = strtol(byte4, &endPtr, 10);
221 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
222 return AFS_IPINVALID;
224 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
229 /* same as inet_ntoa, but to a non-static buffer, must be freed by called */
231 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
236 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
237 (temp >> 8) & 0xff, (temp) & 0xff);
242 * gettmpdir() -- Returns pointer to global temporary directory string.
243 * Always succeeds. Never attempt to deallocate directory string.
249 char *tmpdirp = NULL;
252 static char *saveTmpDir = NULL;
254 if (saveTmpDir == NULL) {
255 /* initialize global temporary directory string */
256 char *dirp = (char *)malloc(MAX_PATH);
260 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
262 if (pathLen == 0 || pathLen > MAX_PATH) {
263 /* can't get tmp path; get cur work dir */
264 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
265 if (pathLen == 0 || pathLen > MAX_PATH) {
272 /* Have a valid dir path; check that actually exists. */
273 DWORD fileAttr = GetFileAttributes(dirp);
275 if ((fileAttr == 0xFFFFFFFF)
276 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
284 FilepathNormalize(dirp);
286 /* most likely TMP or TEMP env vars specify a non-existent dir */
291 /* atomically initialize shared buffer pointer IF still null */
294 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
295 /* shared buffer pointer already initialized by another thread */
299 } /* interlock xchng */
302 /* Above is what we really want to do, but Windows 95 does not have
303 * InterlockedCompareExchange(). So we just atomically swap
304 * the buffer pointer values but we do NOT deallocate the
305 * previously installed buffer, if any, in case it is in use.
308 InterlockedExchange64((LONGLONG)(INT_PTR)&saveTmpDir, (LONGLONG) dirp);
310 InterlockedExchange((LONG) & saveTmpDir, (LONG) dirp);
314 /* if (!saveTmpDir) */
315 tmpdirp = saveTmpDir;
318 #endif /* AFS_NT40_ENV */