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>
27 /* also parse a.b.c.d addresses */
29 hostutil_GetHostByName(char *ahost)
32 static struct hostent thostent;
33 static char *addrp[2];
36 afs_uint32 tval, numeric = 0;
39 tc = *ahost; /* look at the first char */
40 if (tc >= '0' && tc <= '9') {
42 while ((tc = *ptr++)) {
49 } else if (tc > '9' || tc < '0') {
56 tc = *ahost; /* look at the first char */
57 /* decimal address, return fake hostent with only hostaddr field good */
60 memset(addr, 0, sizeof(addr));
61 while ((tc = *ahost++)) {
64 return NULL; /* too many dots */
67 } else if (tc > '9' || tc < '0')
79 thostent.h_addr_list = &addrp[0];
81 /* 4.2 and older systems */
82 thostent.h_addr = addr;
87 if (afs_winsockInit() < 0)
90 return gethostbyname(ahost);
94 /* Translate an internet address into a nice printable string. The
95 * variable addr is in network byte order.
98 hostutil_GetNameByINet(afs_uint32 addr)
101 static char tbuffer[256];
104 if (afs_winsockInit() < 0)
107 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
109 strcpy(tbuffer, th->h_name);
112 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
113 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
120 /* the parameter is a pointer to a buffer containing a string of
122 ** w.x.y.z # machineName
123 ** returns the network interface in network byte order
126 extractAddr(char *line, int maxSize)
128 char byte1[32], byte2[32], byte3[32], byte4[32];
131 afs_uint32 val1, val2, val3, val4;
134 /* skip empty spaces */
135 while (isspace(*line) && maxSize) {
140 /* skip empty lines */
141 if (!maxSize || !*line)
142 return AFS_IPINVALIDIGNORE;
144 while ((*line != '.') && maxSize) { /* extract first byte */
146 return AFS_IPINVALID;
148 return AFS_IPINVALID; /* no space */
149 byte1[i++] = *line++;
153 return AFS_IPINVALID;
157 while ((*line != '.') && maxSize) { /* extract second byte */
159 return AFS_IPINVALID;
161 return AFS_IPINVALID; /* no space */
162 byte2[i++] = *line++;
166 return AFS_IPINVALID;
170 while ((*line != '.') && maxSize) {
172 return AFS_IPINVALID;
174 return AFS_IPINVALID; /* no space */
175 byte3[i++] = *line++;
179 return AFS_IPINVALID;
183 while (*line && !isspace(*line) && maxSize) {
185 return AFS_IPINVALID;
187 return AFS_IPINVALID; /* no space */
188 byte4[i++] = *line++;
192 return AFS_IPINVALID;
196 val1 = strtol(byte1, &endPtr, 10);
197 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
198 return AFS_IPINVALID;
201 val2 = strtol(byte2, &endPtr, 10);
202 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
203 return AFS_IPINVALID;
206 val3 = strtol(byte3, &endPtr, 10);
207 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
208 return AFS_IPINVALID;
211 val4 = strtol(byte4, &endPtr, 10);
212 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
213 return AFS_IPINVALID;
215 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
220 /* same as inet_ntoa, but to a non-static buffer, must be freed by called */
222 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
227 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
228 (temp >> 8) & 0xff, (temp) & 0xff);
233 * gettmpdir() -- Returns pointer to global temporary directory string.
234 * Always succeeds. Never attempt to deallocate directory string.
240 char *tmpdirp = NULL;
243 static char *saveTmpDir = NULL;
245 if (saveTmpDir == NULL) {
246 /* initialize global temporary directory string */
247 char *dirp = (char *)malloc(MAX_PATH+1);
251 DWORD pathLen = GetTempPath(MAX_PATH+1, dirp);
253 if (pathLen == 0 || pathLen > MAX_PATH) {
254 /* can't get tmp path; get cur work dir */
255 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
256 if (pathLen == 0 || pathLen > MAX_PATH) {
263 /* Have a valid dir path; check that actually exists. */
264 DWORD fileAttr = GetFileAttributes(dirp);
266 if ((fileAttr == 0xFFFFFFFF)
267 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
275 FilepathNormalize(dirp);
277 /* most likely TMP or TEMP env vars specify a non-existent dir */
282 /* atomically initialize shared buffer pointer IF still null */
283 if (InterlockedCompareExchangePointer(&saveTmpDir, dirp, NULL) != NULL) {
284 /* shared buffer pointer already initialized by another thread */
289 /* if (!saveTmpDir) */
290 tmpdirp = saveTmpDir;
293 #endif /* AFS_NT40_ENV */