2 * (C) COPYRIGHT TRANSARC CORPORATION 1989
3 * LICENSED MATERIALS - PROPERTY OF TRANSARC
9 #include "../afs/sysincludes.h"
10 #include "../afs/afsutil.h"
13 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <sys/socket.h>
21 #include <arpa/inet.h>
29 /* also parse a.b.c.d addresses */
30 struct hostent *hostutil_GetHostByName(ahost)
31 register char *ahost; {
33 static struct hostent thostent;
34 static char *addrp[2];
36 register char *ptr = ahost;
37 afs_int32 tval, numeric=0;
40 tc = *ahost; /* look at the first char */
41 if (tc >= '0' && tc <= '9') {
50 } else if (tc > '9' || tc < '0') {
57 tc = *ahost; /* look at the first char */
58 /* decimal address, return fake hostent with only hostaddr field good */
61 bzero(addr, sizeof(addr));
62 while (tc = *ahost++) {
64 if (dots >= 3) return (struct hostent *) 0; /* too many dots */
68 else if (tc > '9' || tc < '0') return (struct hostent *) 0;
78 addrp[1] = (char *) 0;
79 thostent.h_addr_list = &addrp[0];
81 /* 4.2 and older systems */
82 thostent.h_addr = addr;
88 if (afs_winsockInit()<0)
89 return (struct hostent *)0;
91 return gethostbyname(ahost);
95 /* Translate an internet address into a nice printable string. The
96 * variable addr is in network byte order.
98 char *hostutil_GetNameByINet(addr)
102 static char tbuffer[256];
105 if (afs_winsockInit()<0)
108 th = gethostbyaddr((void *)&addr, sizeof(addr), AF_INET);
110 strcpy(tbuffer, th->h_name);
113 sprintf(tbuffer, "%d.%d.%d.%d",
114 ((addr>>24) & 0xff), ((addr>>16) & 0xff),
115 ((addr>>8) & 0xff), ( addr & 0xff));
121 /* the parameter is a pointer to a buffer containing a string of
123 ** w.x.y.z # machineName
124 ** returns the network interface in network byte order
127 extractAddr(line, maxSize)
131 char byte1[32], byte2[32], byte3[32], byte4[32];
134 afs_int32 val1, val2, val3, val4;
137 /* skip empty spaces */
138 while ( isspace(*line) && maxSize )
144 /* skip empty lines */
145 if ( !maxSize || !*line ) return AFS_IPINVALIDIGNORE;
147 while ( ( *line != '.' ) && maxSize) /* extract first byte */
149 if ( !isdigit(*line) ) return AFS_IPINVALID;
150 if ( i > 31 ) return AFS_IPINVALID; /* no space */
151 byte1[i++] = *line++;
154 if ( !maxSize ) return AFS_IPINVALID;
158 while ( ( *line != '.' ) && maxSize) /* extract second byte */
160 if ( !isdigit(*line) ) return AFS_IPINVALID;
161 if ( i > 31 ) return AFS_IPINVALID; /* no space */
162 byte2[i++] = *line++;
165 if ( !maxSize ) return AFS_IPINVALID;
169 while ( ( *line != '.' ) && maxSize)
171 if ( !isdigit(*line) ) return AFS_IPINVALID;
172 if ( i > 31 ) return AFS_IPINVALID; /* no space */
173 byte3[i++] = *line++;
176 if ( !maxSize ) return AFS_IPINVALID;
180 while ( *line && !isspace(*line) && maxSize)
182 if ( !isdigit(*line) ) return AFS_IPINVALID;
183 if ( i > 31 ) return AFS_IPINVALID; /* no space */
184 byte4[i++] = *line++;
187 if ( !maxSize ) return AFS_IPINVALID;
191 val1 = strtol(byte1, &endPtr, 10);
192 if (( val1== 0 ) && ( errno != 0 || byte1 == endPtr))
193 return AFS_IPINVALID;
196 val2 = strtol(byte2, &endPtr, 10);
197 if (( val2== 0 ) && ( errno !=0 || byte2 == endPtr))/* no conversion */
198 return AFS_IPINVALID;
201 val3 = strtol(byte3, &endPtr, 10);
202 if (( val3== 0 ) && ( errno !=0 || byte3 == endPtr))/* no conversion */
203 return AFS_IPINVALID;
206 val4 = strtol(byte4, &endPtr, 10);
207 if (( val4== 0 ) && ( errno !=0 || byte4 == endPtr))/* no conversion */
208 return AFS_IPINVALID;
210 val = ( val1 << 24 ) | ( val2 << 16 ) | ( val3 << 8 ) | val4;
216 ** converts a 4byte IP address into a static string (e.g. w.x.y.z)
217 ** On Solaris, if we pass a 4 byte integer directly into inet_ntoa(), it
218 ** causes a memory fault.
220 char* afs_inet_ntoa(afs_int32 addr)
224 return (char *) inet_ntoa(temp);
228 * gettmpdir() -- Returns pointer to global temporary directory string.
229 * Always succeeds. Never attempt to deallocate directory string.
232 char *gettmpdir(void)
234 char *tmpdirp = NULL;
237 static char *saveTmpDir = NULL;
239 if (saveTmpDir == NULL) {
240 /* initialize global temporary directory string */
241 char *dirp = (char *)malloc(MAX_PATH);
245 DWORD pathLen = GetTempPath(MAX_PATH, dirp);
247 if (pathLen == 0 || pathLen > MAX_PATH) {
248 /* can't get tmp path; get cur work dir */
249 pathLen = GetCurrentDirectory(MAX_PATH, dirp);
250 if (pathLen == 0 || pathLen > MAX_PATH) {
257 /* Have a valid dir path; check that actually exists. */
258 DWORD fileAttr = GetFileAttributes(dirp);
260 if ((fileAttr == 0xFFFFFFFF) ||
261 ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
269 FilepathNormalize(dirp);
271 /* most likely TMP or TEMP env vars specify a non-existent dir */
276 /* atomically initialize shared buffer pointer IF still null */
279 if (InterlockedCompareExchange(&saveTmpDir, dirp, NULL) != NULL) {
280 /* shared buffer pointer already initialized by another thread */
284 } /* interlock xchng */
287 /* Above is what we really want to do, but Windows 95 does not have
288 * InterlockedCompareExchange(). So we just atomically swap
289 * the buffer pointer values but we do NOT deallocate the
290 * previously installed buffer, if any, in case it is in use.
292 InterlockedExchange((LPLONG)&saveTmpDir, (LONG)dirp);
294 } /* if (!saveTmpDir) */
296 tmpdirp = saveTmpDir;
299 #endif /* AFS_NT40_ENV */