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
);
107 if (th
&& strlen(th
->h_name
) < sizeof(tbuffer
)) {
108 strlcpy(tbuffer
, th
->h_name
, sizeof(tbuffer
));
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 #define MAXBYTELEN 32
127 extractAddr(char *line
, int maxSize
)
129 char byte1
[MAXBYTELEN
], byte2
[MAXBYTELEN
];
130 char byte3
[MAXBYTELEN
], byte4
[MAXBYTELEN
];
133 afs_uint32 val1
, val2
, val3
, val4
;
136 /* skip empty spaces */
137 while (isspace(*line
) && maxSize
) {
142 /* skip empty lines */
143 if (!maxSize
|| !*line
)
144 return AFS_IPINVALIDIGNORE
;
146 while ((*line
!= '.') && maxSize
) { /* extract first byte */
148 return AFS_IPINVALID
;
149 if (i
>= MAXBYTELEN
-1)
150 return AFS_IPINVALID
; /* no space */
151 byte1
[i
++] = *line
++;
155 return AFS_IPINVALID
;
159 while ((*line
!= '.') && maxSize
) { /* extract second byte */
161 return AFS_IPINVALID
;
162 if (i
>= MAXBYTELEN
-1)
163 return AFS_IPINVALID
; /* no space */
164 byte2
[i
++] = *line
++;
168 return AFS_IPINVALID
;
172 while ((*line
!= '.') && maxSize
) {
174 return AFS_IPINVALID
;
175 if (i
>= MAXBYTELEN
-1)
176 return AFS_IPINVALID
; /* no space */
177 byte3
[i
++] = *line
++;
181 return AFS_IPINVALID
;
185 while (*line
&& !isspace(*line
) && maxSize
) {
187 return AFS_IPINVALID
;
188 if (i
>= MAXBYTELEN
-1)
189 return AFS_IPINVALID
; /* no space */
190 byte4
[i
++] = *line
++;
194 return AFS_IPINVALID
;
198 val1
= strtol(byte1
, &endPtr
, 10);
199 if ((val1
== 0) && (errno
!= 0 || byte1
== endPtr
))
200 return AFS_IPINVALID
;
203 val2
= strtol(byte2
, &endPtr
, 10);
204 if ((val2
== 0) && (errno
!= 0 || byte2
== endPtr
)) /* no conversion */
205 return AFS_IPINVALID
;
208 val3
= strtol(byte3
, &endPtr
, 10);
209 if ((val3
== 0) && (errno
!= 0 || byte3
== endPtr
)) /* no conversion */
210 return AFS_IPINVALID
;
213 val4
= strtol(byte4
, &endPtr
, 10);
214 if ((val4
== 0) && (errno
!= 0 || byte4
== endPtr
)) /* no conversion */
215 return AFS_IPINVALID
;
217 val
= (val1
<< 24) | (val2
<< 16) | (val3
<< 8) | val4
;
222 /* same as inet_ntoa, but to a non-static buffer, must be freed by called */
224 afs_inet_ntoa_r(afs_uint32 addr
, char *buf
)
229 sprintf(buf
, "%d.%d.%d.%d", (temp
>> 24) & 0xff, (temp
>> 16) & 0xff,
230 (temp
>> 8) & 0xff, (temp
) & 0xff);
235 * gettmpdir() -- Returns pointer to global temporary directory string.
236 * Always succeeds. Never attempt to deallocate directory string.
242 char *tmpdirp
= NULL
;
245 static char *saveTmpDir
= NULL
;
247 if (saveTmpDir
== NULL
) {
248 /* initialize global temporary directory string */
249 char *dirp
= malloc(MAX_PATH
+1);
253 DWORD pathLen
= GetTempPath(MAX_PATH
+1, dirp
);
255 if (pathLen
== 0 || pathLen
> MAX_PATH
) {
256 /* can't get tmp path; get cur work dir */
257 pathLen
= GetCurrentDirectory(MAX_PATH
, dirp
);
258 if (pathLen
== 0 || pathLen
> MAX_PATH
) {
265 /* Have a valid dir path; check that actually exists. */
266 DWORD fileAttr
= GetFileAttributes(dirp
);
268 if ((fileAttr
== 0xFFFFFFFF)
269 || ((fileAttr
& FILE_ATTRIBUTE_DIRECTORY
) == 0)) {
277 FilepathNormalize(dirp
);
279 /* most likely TMP or TEMP env vars specify a non-existent dir */
284 /* atomically initialize shared buffer pointer IF still null */
285 if (InterlockedCompareExchangePointer(&saveTmpDir
, dirp
, NULL
) != NULL
) {
286 /* shared buffer pointer already initialized by another thread */
291 /* if (!saveTmpDir) */
292 tmpdirp
= saveTmpDir
;
295 #endif /* AFS_NT40_ENV */