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 /* decimal address, return fake hostent with only hostaddr field good */
59 memset(addr, 0, sizeof(addr));
60 while ((tc = *ahost++)) {
63 return NULL; /* too many dots */
66 } else if (tc > '9' || tc < '0')
78 thostent.h_addr_list = &addrp[0];
80 /* 4.2 and older systems */
81 thostent.h_addr = addr;
86 if (afs_winsockInit() < 0)
89 return gethostbyname(ahost);
93 /* Translate an internet address into a nice printable string. The
94 * variable addr is in network byte order.
97 hostutil_GetNameByINet(afs_uint32 addr)
100 static char tbuffer[256];
103 if (afs_winsockInit() < 0)
106 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
108 strcpy(tbuffer, th->h_name);
111 sprintf(tbuffer, "%d.%d.%d.%d", (int)((addr >> 24) & 0xff),
112 (int)((addr >> 16) & 0xff), (int)((addr >> 8) & 0xff),
119 /* the parameter is a pointer to a buffer containing a string of
121 ** w.x.y.z # machineName
122 ** returns the network interface in network byte order
125 extractAddr(char *line, int maxSize)
127 char byte1[32], byte2[32], byte3[32], byte4[32];
130 afs_uint32 val1, val2, val3, val4;
133 /* skip empty spaces */
134 while (isspace(*line) && maxSize) {
139 /* skip empty lines */
140 if (!maxSize || !*line)
141 return AFS_IPINVALIDIGNORE;
143 while ((*line != '.') && maxSize) { /* extract first byte */
145 return AFS_IPINVALID;
147 return AFS_IPINVALID; /* no space */
148 byte1[i++] = *line++;
152 return AFS_IPINVALID;
156 while ((*line != '.') && maxSize) { /* extract second byte */
158 return AFS_IPINVALID;
160 return AFS_IPINVALID; /* no space */
161 byte2[i++] = *line++;
165 return AFS_IPINVALID;
169 while ((*line != '.') && maxSize) {
171 return AFS_IPINVALID;
173 return AFS_IPINVALID; /* no space */
174 byte3[i++] = *line++;
178 return AFS_IPINVALID;
182 while (*line && !isspace(*line) && maxSize) {
184 return AFS_IPINVALID;
186 return AFS_IPINVALID; /* no space */
187 byte4[i++] = *line++;
191 return AFS_IPINVALID;
195 val1 = strtol(byte1, &endPtr, 10);
196 if ((val1 == 0) && (errno != 0 || byte1 == endPtr))
197 return AFS_IPINVALID;
200 val2 = strtol(byte2, &endPtr, 10);
201 if ((val2 == 0) && (errno != 0 || byte2 == endPtr)) /* no conversion */
202 return AFS_IPINVALID;
205 val3 = strtol(byte3, &endPtr, 10);
206 if ((val3 == 0) && (errno != 0 || byte3 == endPtr)) /* no conversion */
207 return AFS_IPINVALID;
210 val4 = strtol(byte4, &endPtr, 10);
211 if ((val4 == 0) && (errno != 0 || byte4 == endPtr)) /* no conversion */
212 return AFS_IPINVALID;
214 val = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4;
219 /* same as inet_ntoa, but to a non-static buffer, must be freed by called */
221 afs_inet_ntoa_r(afs_uint32 addr, char *buf)
226 sprintf(buf, "%d.%d.%d.%d", (temp >> 24) & 0xff, (temp >> 16) & 0xff,
227 (temp >> 8) & 0xff, (temp) & 0xff);
232 * gettmpdir() -- Returns pointer to global temporary directory string.
233 * Always succeeds. Never attempt to deallocate directory string.
239 char *tmpdirp = NULL;
242 static char *saveTmpDir = NULL;
244 if (saveTmpDir == NULL) {
245 /* initialize global temporary directory string */
246 char *dirp = malloc(MAX_PATH+1);
250 DWORD pathLen = GetTempPath(MAX_PATH+1, dirp);
252 if (pathLen == 0 || pathLen > MAX_PATH) {
253 /* can't get tmp path; get cur work dir */
254 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
255 if (pathLen == 0 || pathLen > MAX_PATH) {
262 /* Have a valid dir path; check that actually exists. */
263 DWORD fileAttr = GetFileAttributes(dirp);
265 if ((fileAttr == 0xFFFFFFFF)
266 || ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
274 FilepathNormalize(dirp);
276 /* most likely TMP or TEMP env vars specify a non-existent dir */
281 /* atomically initialize shared buffer pointer IF still null */
282 if (InterlockedCompareExchangePointer(&saveTmpDir, dirp, NULL) != NULL) {
283 /* shared buffer pointer already initialized by another thread */
288 /* if (!saveTmpDir) */
289 tmpdirp = saveTmpDir;
292 #endif /* AFS_NT40_ENV */