]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $" |
ef416fc2 | 3 | * |
4 | * HTTP address routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
1ff0402e | 6 | * Copyright 2007-2008 by Apple Inc. |
ecdc0628 | 7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * httpAddrAny() - Check for the "any" address. | |
18 | * httpAddrEqual() - Compare two addresses. | |
19 | * httpAddrLocalhost() - Check for the local loopback address. | |
20 | * httpAddrLookup() - Lookup the hostname associated with the address. | |
1ff0402e | 21 | * _httpAddrPort() - Get the port number associated with an address. |
ef416fc2 | 22 | * httpAddrString() - Convert an IP address to a dotted string. |
23 | * httpGetHostByName() - Lookup a hostname or IP address, and return | |
24 | * address records for the specified name. | |
25 | * httpGetHostname() - Get the FQDN for the local system. | |
26 | */ | |
27 | ||
28 | /* | |
29 | * Include necessary headers... | |
30 | */ | |
31 | ||
49d87452 | 32 | #include "http-private.h" |
ef416fc2 | 33 | #include "globals.h" |
34 | #include "debug.h" | |
35 | #include <stdlib.h> | |
36 | #include <stddef.h> | |
49d87452 MS |
37 | #ifdef HAVE_RESOLV_H |
38 | # include <resolv.h> | |
39 | #endif /* HAVE_RESOLV_H */ | |
ef416fc2 | 40 | |
41 | ||
42 | /* | |
43 | * 'httpAddrAny()' - Check for the "any" address. | |
44 | * | |
426c6a59 | 45 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 46 | */ |
47 | ||
48 | int /* O - 1 if "any", 0 otherwise */ | |
49 | httpAddrAny(const http_addr_t *addr) /* I - Address to check */ | |
50 | { | |
89d46774 | 51 | if (!addr) |
52 | return (0); | |
53 | ||
ef416fc2 | 54 | #ifdef AF_INET6 |
55 | if (addr->addr.sa_family == AF_INET6 && | |
56 | IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr))) | |
57 | return (1); | |
58 | #endif /* AF_INET6 */ | |
59 | ||
60 | if (addr->addr.sa_family == AF_INET && | |
61 | ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000) | |
62 | return (1); | |
63 | ||
64 | return (0); | |
65 | } | |
66 | ||
67 | ||
68 | /* | |
69 | * 'httpAddrEqual()' - Compare two addresses. | |
70 | * | |
426c6a59 | 71 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 72 | */ |
73 | ||
ecdc0628 | 74 | int /* O - 1 if equal, 0 if not */ |
ef416fc2 | 75 | httpAddrEqual(const http_addr_t *addr1, /* I - First address */ |
76 | const http_addr_t *addr2) /* I - Second address */ | |
77 | { | |
89d46774 | 78 | if (!addr1 && !addr2) |
79 | return (1); | |
80 | ||
81 | if (!addr1 || !addr2) | |
82 | return (0); | |
83 | ||
ef416fc2 | 84 | if (addr1->addr.sa_family != addr2->addr.sa_family) |
85 | return (0); | |
86 | ||
87 | #ifdef AF_LOCAL | |
88 | if (addr1->addr.sa_family == AF_LOCAL) | |
89 | return (!strcmp(addr1->un.sun_path, addr2->un.sun_path)); | |
90 | #endif /* AF_LOCAL */ | |
91 | ||
92 | #ifdef AF_INET6 | |
93 | if (addr1->addr.sa_family == AF_INET6) | |
94 | return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16)); | |
95 | #endif /* AF_INET6 */ | |
96 | ||
97 | return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr); | |
98 | } | |
99 | ||
100 | ||
101 | /* | |
102 | * 'httpAddrLength()' - Return the length of the address in bytes. | |
103 | * | |
426c6a59 | 104 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 105 | */ |
106 | ||
107 | int /* O - Length in bytes */ | |
108 | httpAddrLength(const http_addr_t *addr) /* I - Address */ | |
109 | { | |
89d46774 | 110 | if (!addr) |
111 | return (0); | |
112 | ||
ef416fc2 | 113 | #ifdef AF_INET6 |
114 | if (addr->addr.sa_family == AF_INET6) | |
115 | return (sizeof(addr->ipv6)); | |
116 | else | |
117 | #endif /* AF_INET6 */ | |
118 | #ifdef AF_LOCAL | |
119 | if (addr->addr.sa_family == AF_LOCAL) | |
120 | return (offsetof(struct sockaddr_un, sun_path) + | |
121 | strlen(addr->un.sun_path) + 1); | |
122 | else | |
123 | #endif /* AF_LOCAL */ | |
124 | if (addr->addr.sa_family == AF_INET) | |
125 | return (sizeof(addr->ipv4)); | |
126 | else | |
127 | return (0); | |
128 | ||
129 | } | |
130 | ||
131 | ||
132 | /* | |
133 | * 'httpAddrLocalhost()' - Check for the local loopback address. | |
134 | * | |
426c6a59 | 135 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 136 | */ |
137 | ||
138 | int /* O - 1 if local host, 0 otherwise */ | |
139 | httpAddrLocalhost( | |
140 | const http_addr_t *addr) /* I - Address to check */ | |
141 | { | |
89d46774 | 142 | if (!addr) |
143 | return (1); | |
144 | ||
ef416fc2 | 145 | #ifdef AF_INET6 |
146 | if (addr->addr.sa_family == AF_INET6 && | |
fa73b229 | 147 | IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr))) |
ef416fc2 | 148 | return (1); |
149 | #endif /* AF_INET6 */ | |
150 | ||
151 | #ifdef AF_LOCAL | |
152 | if (addr->addr.sa_family == AF_LOCAL) | |
153 | return (1); | |
154 | #endif /* AF_LOCAL */ | |
155 | ||
156 | if (addr->addr.sa_family == AF_INET && | |
157 | ntohl(addr->ipv4.sin_addr.s_addr) == 0x7f000001) | |
158 | return (1); | |
159 | ||
160 | return (0); | |
161 | } | |
162 | ||
163 | ||
164 | #ifdef __sgi | |
165 | # define ADDR_CAST (struct sockaddr *) | |
166 | #else | |
167 | # define ADDR_CAST (char *) | |
168 | #endif /* __sgi */ | |
169 | ||
170 | ||
171 | /* | |
172 | * 'httpAddrLookup()' - Lookup the hostname associated with the address. | |
173 | * | |
426c6a59 | 174 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 175 | */ |
176 | ||
ecdc0628 | 177 | char * /* O - Host name */ |
178 | httpAddrLookup( | |
179 | const http_addr_t *addr, /* I - Address to lookup */ | |
180 | char *name, /* I - Host name buffer */ | |
181 | int namelen) /* I - Size of name buffer */ | |
ef416fc2 | 182 | { |
49d87452 MS |
183 | _cups_globals_t *cg = _cupsGlobals(); |
184 | /* Global data */ | |
185 | ||
186 | ||
ef416fc2 | 187 | DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)\n", |
188 | addr, name, namelen)); | |
189 | ||
190 | /* | |
191 | * Range check input... | |
192 | */ | |
193 | ||
194 | if (!addr || !name || namelen <= 2) | |
195 | { | |
196 | if (name && namelen >= 1) | |
197 | *name = '\0'; | |
198 | ||
199 | return (NULL); | |
200 | } | |
201 | ||
202 | #ifdef AF_LOCAL | |
203 | if (addr->addr.sa_family == AF_LOCAL) | |
49d87452 | 204 | { |
ef416fc2 | 205 | strlcpy(name, addr->un.sun_path, namelen); |
49d87452 MS |
206 | return (name); |
207 | } | |
ef416fc2 | 208 | #endif /* AF_LOCAL */ |
49d87452 MS |
209 | |
210 | #ifdef HAVE_RES_INIT | |
211 | /* | |
212 | * STR #2920: Initialize resolver after failure in cups-polld | |
213 | * | |
214 | * If the previous lookup failed, re-initialize the resolver to prevent | |
215 | * temporary network errors from persisting. This *should* be handled by | |
216 | * the resolver libraries, but apparently the glibc folks do not agree. | |
217 | * | |
218 | * We set a flag at the end of this function if we encounter an error that | |
219 | * requires reinitialization of the resolver functions. We then call | |
220 | * res_init() if the flag is set on the next call here or in httpAddrLookup(). | |
221 | */ | |
222 | ||
223 | if (cg->need_res_init) | |
224 | { | |
225 | res_init(); | |
226 | ||
227 | cg->need_res_init = 0; | |
228 | } | |
229 | #endif /* HAVE_RES_INIT */ | |
230 | ||
ef416fc2 | 231 | #ifdef HAVE_GETNAMEINFO |
232 | { | |
db1f069b MS |
233 | /* |
234 | * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN | |
235 | * | |
236 | * FWIW, I think this is really a bug in the implementation of | |
237 | * getnameinfo(), but falling back on httpAddrString() is easy to | |
238 | * do... | |
239 | */ | |
ef416fc2 | 240 | |
49d87452 MS |
241 | int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen, |
242 | NULL, 0, 0); | |
243 | ||
244 | if (error) | |
245 | { | |
246 | if (error == EAI_FAIL) | |
247 | cg->need_res_init = 1; | |
248 | ||
db1f069b | 249 | return (httpAddrString(addr, name, namelen)); |
49d87452 | 250 | } |
ef416fc2 | 251 | } |
252 | #else | |
253 | { | |
254 | struct hostent *host; /* Host from name service */ | |
255 | ||
256 | ||
257 | # ifdef AF_INET6 | |
258 | if (addr->addr.sa_family == AF_INET6) | |
259 | host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr), | |
260 | sizeof(struct in_addr), AF_INET6); | |
261 | else | |
262 | # endif /* AF_INET6 */ | |
263 | host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr), | |
264 | sizeof(struct in_addr), AF_INET); | |
265 | ||
266 | if (host == NULL) | |
267 | { | |
268 | /* | |
269 | * No hostname, so return the raw address... | |
270 | */ | |
271 | ||
49d87452 MS |
272 | if (h_errno == NO_RECOVERY) |
273 | cg->need_res_init = 1; | |
274 | ||
275 | return (httpAddrString(addr, name, namelen)); | |
ef416fc2 | 276 | } |
277 | ||
278 | strlcpy(name, host->h_name, namelen); | |
279 | } | |
280 | #endif /* HAVE_GETNAMEINFO */ | |
281 | ||
282 | return (name); | |
283 | } | |
284 | ||
285 | ||
1ff0402e MS |
286 | /* |
287 | * '_httpAddrPort()' - Get the port number associated with an address. | |
288 | */ | |
289 | ||
290 | int /* O - Port number */ | |
291 | _httpAddrPort(http_addr_t *addr) /* I - Address */ | |
292 | { | |
5f64df29 MS |
293 | if (!addr) |
294 | return (ippPort()); | |
1ff0402e | 295 | #ifdef AF_INET6 |
5f64df29 | 296 | else if (addr->addr.sa_family == AF_INET6) |
1ff0402e | 297 | return (ntohs(addr->ipv6.sin6_port)); |
1ff0402e | 298 | #endif /* AF_INET6 */ |
5f64df29 | 299 | else if (addr->addr.sa_family == AF_INET) |
1ff0402e MS |
300 | return (ntohs(addr->ipv4.sin_port)); |
301 | else | |
302 | return (ippPort()); | |
303 | } | |
304 | ||
305 | ||
ef416fc2 | 306 | /* |
ecdc0628 | 307 | * 'httpAddrString()' - Convert an address to a numeric string. |
ef416fc2 | 308 | * |
426c6a59 | 309 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 310 | */ |
311 | ||
ecdc0628 | 312 | char * /* O - Numeric address string */ |
313 | httpAddrString(const http_addr_t *addr, /* I - Address to convert */ | |
314 | char *s, /* I - String buffer */ | |
315 | int slen) /* I - Length of string */ | |
ef416fc2 | 316 | { |
317 | DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n", | |
318 | addr, s, slen)); | |
319 | ||
320 | /* | |
321 | * Range check input... | |
322 | */ | |
323 | ||
324 | if (!addr || !s || slen <= 2) | |
325 | { | |
326 | if (s && slen >= 1) | |
327 | *s = '\0'; | |
328 | ||
329 | return (NULL); | |
330 | } | |
331 | ||
332 | #ifdef AF_LOCAL | |
333 | if (addr->addr.sa_family == AF_LOCAL) | |
334 | strlcpy(s, addr->un.sun_path, slen); | |
335 | else | |
336 | #endif /* AF_LOCAL */ | |
b423cd4c | 337 | if (addr->addr.sa_family == AF_INET) |
ef416fc2 | 338 | { |
b423cd4c | 339 | unsigned temp; /* Temporary address */ |
ef416fc2 | 340 | |
341 | ||
b423cd4c | 342 | temp = ntohl(addr->ipv4.sin_addr.s_addr); |
343 | ||
344 | snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, | |
345 | (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); | |
346 | } | |
347 | #ifdef AF_INET6 | |
348 | else if (addr->addr.sa_family == AF_INET6) | |
349 | { | |
350 | # ifdef HAVE_GETNAMEINFO | |
ef416fc2 | 351 | if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen, |
b423cd4c | 352 | NULL, 0, NI_NUMERICHOST)) |
ef416fc2 | 353 | { |
354 | /* | |
355 | * If we get an error back, then the address type is not supported | |
356 | * and we should zero out the buffer... | |
357 | */ | |
358 | ||
359 | s[0] = '\0'; | |
360 | ||
361 | return (NULL); | |
362 | } | |
b423cd4c | 363 | # else |
364 | char *sptr; /* Pointer into string */ | |
365 | int i; /* Looping var */ | |
366 | unsigned temp; /* Current value */ | |
367 | const char *prefix; /* Prefix for address */ | |
368 | ||
369 | ||
370 | prefix = ""; | |
371 | for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) | |
ef416fc2 | 372 | { |
b423cd4c | 373 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 374 | |
b423cd4c | 375 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
376 | prefix = ":"; | |
377 | slen -= strlen(sptr); | |
378 | sptr += strlen(sptr); | |
ef416fc2 | 379 | |
b423cd4c | 380 | temp &= 0xffff; |
ef416fc2 | 381 | |
b423cd4c | 382 | if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) |
383 | { | |
384 | snprintf(sptr, slen, "%s%x", prefix, temp); | |
ef416fc2 | 385 | slen -= strlen(sptr); |
386 | sptr += strlen(sptr); | |
ef416fc2 | 387 | } |
b423cd4c | 388 | } |
389 | ||
390 | if (i < 4) | |
391 | { | |
392 | while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) | |
393 | i ++; | |
ef416fc2 | 394 | |
395 | if (i < 4) | |
396 | { | |
b423cd4c | 397 | snprintf(sptr, slen, "%s:", prefix); |
398 | prefix = ":"; | |
399 | slen -= strlen(sptr); | |
400 | sptr += strlen(sptr); | |
ef416fc2 | 401 | |
b423cd4c | 402 | for (; i < 4; i ++) |
ef416fc2 | 403 | { |
b423cd4c | 404 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 405 | |
b423cd4c | 406 | if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1]) |
ef416fc2 | 407 | { |
b423cd4c | 408 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
ef416fc2 | 409 | slen -= strlen(sptr); |
410 | sptr += strlen(sptr); | |
b423cd4c | 411 | } |
ef416fc2 | 412 | |
b423cd4c | 413 | snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff); |
414 | slen -= strlen(sptr); | |
415 | sptr += strlen(sptr); | |
ef416fc2 | 416 | } |
417 | } | |
b423cd4c | 418 | else if (sptr == s) |
419 | { | |
420 | /* | |
421 | * Empty address... | |
422 | */ | |
ef416fc2 | 423 | |
b423cd4c | 424 | strlcpy(s, "::", slen); |
425 | sptr = s + 2; | |
426 | slen -= 2; | |
427 | } | |
428 | else | |
429 | { | |
430 | /* | |
431 | * Empty at end... | |
432 | */ | |
ef416fc2 | 433 | |
b423cd4c | 434 | strlcpy(sptr, "::", slen); |
435 | sptr += 2; | |
436 | slen -= 2; | |
437 | } | |
ef416fc2 | 438 | } |
b423cd4c | 439 | # endif /* HAVE_GETNAMEINFO */ |
ef416fc2 | 440 | } |
b423cd4c | 441 | #endif /* AF_INET6 */ |
442 | else | |
443 | strlcpy(s, "UNKNOWN", slen); | |
ef416fc2 | 444 | |
445 | DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s)); | |
446 | ||
447 | return (s); | |
448 | } | |
449 | ||
450 | ||
451 | /* | |
452 | * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return | |
453 | * address records for the specified name. | |
454 | * | |
455 | * @deprecated@ | |
456 | */ | |
457 | ||
458 | struct hostent * /* O - Host entry */ | |
459 | httpGetHostByName(const char *name) /* I - Hostname or IP address */ | |
460 | { | |
461 | const char *nameptr; /* Pointer into name */ | |
462 | unsigned ip[4]; /* IP address components */ | |
463 | _cups_globals_t *cg = _cupsGlobals(); | |
464 | /* Pointer to library globals */ | |
465 | ||
466 | ||
467 | DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name)); | |
468 | ||
469 | /* | |
470 | * Avoid lookup delays and configuration problems when connecting | |
471 | * to the localhost address... | |
472 | */ | |
473 | ||
474 | if (!strcmp(name, "localhost")) | |
475 | name = "127.0.0.1"; | |
476 | ||
477 | /* | |
478 | * This function is needed because some operating systems have a | |
479 | * buggy implementation of gethostbyname() that does not support | |
480 | * IP addresses. If the first character of the name string is a | |
481 | * number, then sscanf() is used to extract the IP components. | |
482 | * We then pack the components into an IPv4 address manually, | |
483 | * since the inet_aton() function is deprecated. We use the | |
484 | * htonl() macro to get the right byte order for the address. | |
485 | * | |
486 | * We also support domain sockets when supported by the underlying | |
487 | * OS... | |
488 | */ | |
489 | ||
490 | #ifdef AF_LOCAL | |
491 | if (name[0] == '/') | |
492 | { | |
493 | /* | |
494 | * A domain socket address, so make an AF_LOCAL entry and return it... | |
495 | */ | |
496 | ||
497 | cg->hostent.h_name = (char *)name; | |
498 | cg->hostent.h_aliases = NULL; | |
499 | cg->hostent.h_addrtype = AF_LOCAL; | |
500 | cg->hostent.h_length = strlen(name) + 1; | |
501 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
502 | cg->ip_ptrs[0] = (char *)name; | |
503 | cg->ip_ptrs[1] = NULL; | |
504 | ||
505 | DEBUG_puts("httpGetHostByName: returning domain socket address..."); | |
506 | ||
507 | return (&cg->hostent); | |
508 | } | |
509 | #endif /* AF_LOCAL */ | |
510 | ||
511 | for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); | |
512 | ||
513 | if (!*nameptr) | |
514 | { | |
515 | /* | |
516 | * We have an IPv4 address; break it up and provide the host entry | |
517 | * to the caller. | |
518 | */ | |
519 | ||
520 | if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) | |
521 | return (NULL); /* Must have 4 numbers */ | |
522 | ||
523 | if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) | |
524 | return (NULL); /* Invalid byte ranges! */ | |
525 | ||
526 | cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | | |
527 | ip[3])); | |
528 | ||
529 | /* | |
530 | * Fill in the host entry and return it... | |
531 | */ | |
532 | ||
533 | cg->hostent.h_name = (char *)name; | |
534 | cg->hostent.h_aliases = NULL; | |
535 | cg->hostent.h_addrtype = AF_INET; | |
536 | cg->hostent.h_length = 4; | |
537 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
538 | cg->ip_ptrs[0] = (char *)&(cg->ip_addr); | |
539 | cg->ip_ptrs[1] = NULL; | |
540 | ||
541 | DEBUG_puts("httpGetHostByName: returning IPv4 address..."); | |
542 | ||
543 | return (&cg->hostent); | |
544 | } | |
545 | else | |
546 | { | |
547 | /* | |
548 | * Use the gethostbyname() function to get the IPv4 address for | |
549 | * the name... | |
550 | */ | |
551 | ||
552 | DEBUG_puts("httpGetHostByName: returning domain lookup address(es)..."); | |
553 | ||
554 | return (gethostbyname(name)); | |
555 | } | |
556 | } | |
557 | ||
558 | ||
559 | /* | |
757d2cad | 560 | * 'httpGetHostname()' - Get the FQDN for the connection or local system. |
ef416fc2 | 561 | * |
757d2cad | 562 | * When "http" points to a connected socket, return the hostname or |
563 | * address that was used in the call to httpConnect() or httpConnectEncrypt(). | |
564 | * Otherwise, return the FQDN for the local system using both gethostname() | |
565 | * and gethostbyname() to get the local hostname with domain. | |
ef416fc2 | 566 | * |
426c6a59 | 567 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 568 | */ |
569 | ||
757d2cad | 570 | const char * /* O - FQDN for connection or system */ |
571 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ | |
572 | char *s, /* I - String buffer for name */ | |
573 | int slen) /* I - Size of buffer */ | |
ef416fc2 | 574 | { |
575 | struct hostent *host; /* Host entry to get FQDN */ | |
576 | ||
577 | ||
89d46774 | 578 | if (!s || slen <= 1) |
579 | return (NULL); | |
580 | ||
757d2cad | 581 | if (http) |
480ef0fe | 582 | { |
583 | if (http->hostname[0] == '/') | |
584 | strlcpy(s, "localhost", slen); | |
585 | else | |
586 | strlcpy(s, http->hostname, slen); | |
587 | } | |
757d2cad | 588 | else |
ef416fc2 | 589 | { |
590 | /* | |
757d2cad | 591 | * Get the hostname... |
ef416fc2 | 592 | */ |
593 | ||
89d46774 | 594 | if (gethostname(s, slen) < 0) |
595 | strlcpy(s, "localhost", slen); | |
757d2cad | 596 | |
597 | if (!strchr(s, '.')) | |
598 | { | |
599 | /* | |
600 | * The hostname is not a FQDN, so look it up... | |
601 | */ | |
602 | ||
89d46774 | 603 | if ((host = gethostbyname(s)) != NULL && host->h_name) |
757d2cad | 604 | strlcpy(s, host->h_name, slen); |
605 | } | |
ef416fc2 | 606 | } |
607 | ||
608 | /* | |
609 | * Return the hostname with as much domain info as we have... | |
610 | */ | |
611 | ||
612 | return (s); | |
613 | } | |
614 | ||
615 | ||
616 | /* | |
b19ccc9e | 617 | * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $". |
ef416fc2 | 618 | */ |