]>
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 | * | |
d2354e63 | 6 | * Copyright 2007-2009 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 | 209 | |
d2354e63 MS |
210 | /* |
211 | * Optimize lookups for localhost/loopback addresses... | |
212 | */ | |
213 | ||
214 | if (httpAddrLocalhost(addr)) | |
215 | { | |
216 | strlcpy(name, "localhost", namelen); | |
217 | return (name); | |
218 | } | |
219 | ||
49d87452 MS |
220 | #ifdef HAVE_RES_INIT |
221 | /* | |
222 | * STR #2920: Initialize resolver after failure in cups-polld | |
223 | * | |
224 | * If the previous lookup failed, re-initialize the resolver to prevent | |
225 | * temporary network errors from persisting. This *should* be handled by | |
226 | * the resolver libraries, but apparently the glibc folks do not agree. | |
227 | * | |
228 | * We set a flag at the end of this function if we encounter an error that | |
229 | * requires reinitialization of the resolver functions. We then call | |
230 | * res_init() if the flag is set on the next call here or in httpAddrLookup(). | |
231 | */ | |
232 | ||
233 | if (cg->need_res_init) | |
234 | { | |
235 | res_init(); | |
236 | ||
237 | cg->need_res_init = 0; | |
238 | } | |
239 | #endif /* HAVE_RES_INIT */ | |
240 | ||
ef416fc2 | 241 | #ifdef HAVE_GETNAMEINFO |
242 | { | |
db1f069b MS |
243 | /* |
244 | * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN | |
245 | * | |
246 | * FWIW, I think this is really a bug in the implementation of | |
247 | * getnameinfo(), but falling back on httpAddrString() is easy to | |
248 | * do... | |
249 | */ | |
ef416fc2 | 250 | |
49d87452 MS |
251 | int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen, |
252 | NULL, 0, 0); | |
253 | ||
254 | if (error) | |
255 | { | |
256 | if (error == EAI_FAIL) | |
257 | cg->need_res_init = 1; | |
258 | ||
db1f069b | 259 | return (httpAddrString(addr, name, namelen)); |
49d87452 | 260 | } |
ef416fc2 | 261 | } |
262 | #else | |
263 | { | |
264 | struct hostent *host; /* Host from name service */ | |
265 | ||
266 | ||
267 | # ifdef AF_INET6 | |
268 | if (addr->addr.sa_family == AF_INET6) | |
269 | host = gethostbyaddr(ADDR_CAST &(addr->ipv6.sin6_addr), | |
270 | sizeof(struct in_addr), AF_INET6); | |
271 | else | |
272 | # endif /* AF_INET6 */ | |
273 | host = gethostbyaddr(ADDR_CAST &(addr->ipv4.sin_addr), | |
274 | sizeof(struct in_addr), AF_INET); | |
275 | ||
276 | if (host == NULL) | |
277 | { | |
278 | /* | |
279 | * No hostname, so return the raw address... | |
280 | */ | |
281 | ||
49d87452 MS |
282 | if (h_errno == NO_RECOVERY) |
283 | cg->need_res_init = 1; | |
284 | ||
285 | return (httpAddrString(addr, name, namelen)); | |
ef416fc2 | 286 | } |
287 | ||
288 | strlcpy(name, host->h_name, namelen); | |
289 | } | |
290 | #endif /* HAVE_GETNAMEINFO */ | |
291 | ||
292 | return (name); | |
293 | } | |
294 | ||
295 | ||
1ff0402e MS |
296 | /* |
297 | * '_httpAddrPort()' - Get the port number associated with an address. | |
298 | */ | |
299 | ||
300 | int /* O - Port number */ | |
301 | _httpAddrPort(http_addr_t *addr) /* I - Address */ | |
302 | { | |
5f64df29 MS |
303 | if (!addr) |
304 | return (ippPort()); | |
1ff0402e | 305 | #ifdef AF_INET6 |
5f64df29 | 306 | else if (addr->addr.sa_family == AF_INET6) |
1ff0402e | 307 | return (ntohs(addr->ipv6.sin6_port)); |
1ff0402e | 308 | #endif /* AF_INET6 */ |
5f64df29 | 309 | else if (addr->addr.sa_family == AF_INET) |
1ff0402e MS |
310 | return (ntohs(addr->ipv4.sin_port)); |
311 | else | |
312 | return (ippPort()); | |
313 | } | |
314 | ||
315 | ||
ef416fc2 | 316 | /* |
ecdc0628 | 317 | * 'httpAddrString()' - Convert an address to a numeric string. |
ef416fc2 | 318 | * |
426c6a59 | 319 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 320 | */ |
321 | ||
ecdc0628 | 322 | char * /* O - Numeric address string */ |
323 | httpAddrString(const http_addr_t *addr, /* I - Address to convert */ | |
324 | char *s, /* I - String buffer */ | |
325 | int slen) /* I - Length of string */ | |
ef416fc2 | 326 | { |
327 | DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)\n", | |
328 | addr, s, slen)); | |
329 | ||
330 | /* | |
331 | * Range check input... | |
332 | */ | |
333 | ||
334 | if (!addr || !s || slen <= 2) | |
335 | { | |
336 | if (s && slen >= 1) | |
337 | *s = '\0'; | |
338 | ||
339 | return (NULL); | |
340 | } | |
341 | ||
342 | #ifdef AF_LOCAL | |
343 | if (addr->addr.sa_family == AF_LOCAL) | |
344 | strlcpy(s, addr->un.sun_path, slen); | |
345 | else | |
346 | #endif /* AF_LOCAL */ | |
b423cd4c | 347 | if (addr->addr.sa_family == AF_INET) |
ef416fc2 | 348 | { |
b423cd4c | 349 | unsigned temp; /* Temporary address */ |
ef416fc2 | 350 | |
351 | ||
b423cd4c | 352 | temp = ntohl(addr->ipv4.sin_addr.s_addr); |
353 | ||
354 | snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, | |
355 | (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); | |
356 | } | |
357 | #ifdef AF_INET6 | |
358 | else if (addr->addr.sa_family == AF_INET6) | |
359 | { | |
360 | # ifdef HAVE_GETNAMEINFO | |
ef416fc2 | 361 | if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen, |
b423cd4c | 362 | NULL, 0, NI_NUMERICHOST)) |
ef416fc2 | 363 | { |
364 | /* | |
365 | * If we get an error back, then the address type is not supported | |
366 | * and we should zero out the buffer... | |
367 | */ | |
368 | ||
369 | s[0] = '\0'; | |
370 | ||
371 | return (NULL); | |
372 | } | |
b423cd4c | 373 | # else |
374 | char *sptr; /* Pointer into string */ | |
375 | int i; /* Looping var */ | |
376 | unsigned temp; /* Current value */ | |
377 | const char *prefix; /* Prefix for address */ | |
378 | ||
379 | ||
380 | prefix = ""; | |
381 | for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) | |
ef416fc2 | 382 | { |
b423cd4c | 383 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 384 | |
b423cd4c | 385 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
386 | prefix = ":"; | |
387 | slen -= strlen(sptr); | |
388 | sptr += strlen(sptr); | |
ef416fc2 | 389 | |
b423cd4c | 390 | temp &= 0xffff; |
ef416fc2 | 391 | |
b423cd4c | 392 | if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) |
393 | { | |
394 | snprintf(sptr, slen, "%s%x", prefix, temp); | |
ef416fc2 | 395 | slen -= strlen(sptr); |
396 | sptr += strlen(sptr); | |
ef416fc2 | 397 | } |
b423cd4c | 398 | } |
399 | ||
400 | if (i < 4) | |
401 | { | |
402 | while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) | |
403 | i ++; | |
ef416fc2 | 404 | |
405 | if (i < 4) | |
406 | { | |
b423cd4c | 407 | snprintf(sptr, slen, "%s:", prefix); |
408 | prefix = ":"; | |
409 | slen -= strlen(sptr); | |
410 | sptr += strlen(sptr); | |
ef416fc2 | 411 | |
b423cd4c | 412 | for (; i < 4; i ++) |
ef416fc2 | 413 | { |
b423cd4c | 414 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 415 | |
b423cd4c | 416 | if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1]) |
ef416fc2 | 417 | { |
b423cd4c | 418 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
ef416fc2 | 419 | slen -= strlen(sptr); |
420 | sptr += strlen(sptr); | |
b423cd4c | 421 | } |
ef416fc2 | 422 | |
b423cd4c | 423 | snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff); |
424 | slen -= strlen(sptr); | |
425 | sptr += strlen(sptr); | |
ef416fc2 | 426 | } |
427 | } | |
b423cd4c | 428 | else if (sptr == s) |
429 | { | |
430 | /* | |
431 | * Empty address... | |
432 | */ | |
ef416fc2 | 433 | |
b423cd4c | 434 | strlcpy(s, "::", slen); |
435 | sptr = s + 2; | |
436 | slen -= 2; | |
437 | } | |
438 | else | |
439 | { | |
440 | /* | |
441 | * Empty at end... | |
442 | */ | |
ef416fc2 | 443 | |
b423cd4c | 444 | strlcpy(sptr, "::", slen); |
445 | sptr += 2; | |
446 | slen -= 2; | |
447 | } | |
ef416fc2 | 448 | } |
b423cd4c | 449 | # endif /* HAVE_GETNAMEINFO */ |
ef416fc2 | 450 | } |
b423cd4c | 451 | #endif /* AF_INET6 */ |
452 | else | |
453 | strlcpy(s, "UNKNOWN", slen); | |
ef416fc2 | 454 | |
455 | DEBUG_printf(("httpAddrString: returning \"%s\"...\n", s)); | |
456 | ||
457 | return (s); | |
458 | } | |
459 | ||
460 | ||
461 | /* | |
462 | * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return | |
463 | * address records for the specified name. | |
464 | * | |
465 | * @deprecated@ | |
466 | */ | |
467 | ||
468 | struct hostent * /* O - Host entry */ | |
469 | httpGetHostByName(const char *name) /* I - Hostname or IP address */ | |
470 | { | |
471 | const char *nameptr; /* Pointer into name */ | |
472 | unsigned ip[4]; /* IP address components */ | |
473 | _cups_globals_t *cg = _cupsGlobals(); | |
474 | /* Pointer to library globals */ | |
475 | ||
476 | ||
477 | DEBUG_printf(("httpGetHostByName(name=\"%s\")\n", name)); | |
478 | ||
479 | /* | |
480 | * Avoid lookup delays and configuration problems when connecting | |
481 | * to the localhost address... | |
482 | */ | |
483 | ||
484 | if (!strcmp(name, "localhost")) | |
485 | name = "127.0.0.1"; | |
486 | ||
487 | /* | |
488 | * This function is needed because some operating systems have a | |
489 | * buggy implementation of gethostbyname() that does not support | |
490 | * IP addresses. If the first character of the name string is a | |
491 | * number, then sscanf() is used to extract the IP components. | |
492 | * We then pack the components into an IPv4 address manually, | |
493 | * since the inet_aton() function is deprecated. We use the | |
494 | * htonl() macro to get the right byte order for the address. | |
495 | * | |
496 | * We also support domain sockets when supported by the underlying | |
497 | * OS... | |
498 | */ | |
499 | ||
500 | #ifdef AF_LOCAL | |
501 | if (name[0] == '/') | |
502 | { | |
503 | /* | |
504 | * A domain socket address, so make an AF_LOCAL entry and return it... | |
505 | */ | |
506 | ||
507 | cg->hostent.h_name = (char *)name; | |
508 | cg->hostent.h_aliases = NULL; | |
509 | cg->hostent.h_addrtype = AF_LOCAL; | |
510 | cg->hostent.h_length = strlen(name) + 1; | |
511 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
512 | cg->ip_ptrs[0] = (char *)name; | |
513 | cg->ip_ptrs[1] = NULL; | |
514 | ||
515 | DEBUG_puts("httpGetHostByName: returning domain socket address..."); | |
516 | ||
517 | return (&cg->hostent); | |
518 | } | |
519 | #endif /* AF_LOCAL */ | |
520 | ||
521 | for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); | |
522 | ||
523 | if (!*nameptr) | |
524 | { | |
525 | /* | |
526 | * We have an IPv4 address; break it up and provide the host entry | |
527 | * to the caller. | |
528 | */ | |
529 | ||
530 | if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) | |
531 | return (NULL); /* Must have 4 numbers */ | |
532 | ||
533 | if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) | |
534 | return (NULL); /* Invalid byte ranges! */ | |
535 | ||
536 | cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | | |
537 | ip[3])); | |
538 | ||
539 | /* | |
540 | * Fill in the host entry and return it... | |
541 | */ | |
542 | ||
543 | cg->hostent.h_name = (char *)name; | |
544 | cg->hostent.h_aliases = NULL; | |
545 | cg->hostent.h_addrtype = AF_INET; | |
546 | cg->hostent.h_length = 4; | |
547 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
548 | cg->ip_ptrs[0] = (char *)&(cg->ip_addr); | |
549 | cg->ip_ptrs[1] = NULL; | |
550 | ||
551 | DEBUG_puts("httpGetHostByName: returning IPv4 address..."); | |
552 | ||
553 | return (&cg->hostent); | |
554 | } | |
555 | else | |
556 | { | |
557 | /* | |
558 | * Use the gethostbyname() function to get the IPv4 address for | |
559 | * the name... | |
560 | */ | |
561 | ||
562 | DEBUG_puts("httpGetHostByName: returning domain lookup address(es)..."); | |
563 | ||
564 | return (gethostbyname(name)); | |
565 | } | |
566 | } | |
567 | ||
568 | ||
569 | /* | |
757d2cad | 570 | * 'httpGetHostname()' - Get the FQDN for the connection or local system. |
ef416fc2 | 571 | * |
757d2cad | 572 | * When "http" points to a connected socket, return the hostname or |
573 | * address that was used in the call to httpConnect() or httpConnectEncrypt(). | |
574 | * Otherwise, return the FQDN for the local system using both gethostname() | |
575 | * and gethostbyname() to get the local hostname with domain. | |
ef416fc2 | 576 | * |
426c6a59 | 577 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 578 | */ |
579 | ||
757d2cad | 580 | const char * /* O - FQDN for connection or system */ |
581 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ | |
582 | char *s, /* I - String buffer for name */ | |
583 | int slen) /* I - Size of buffer */ | |
ef416fc2 | 584 | { |
585 | struct hostent *host; /* Host entry to get FQDN */ | |
586 | ||
587 | ||
89d46774 | 588 | if (!s || slen <= 1) |
589 | return (NULL); | |
590 | ||
757d2cad | 591 | if (http) |
480ef0fe | 592 | { |
593 | if (http->hostname[0] == '/') | |
594 | strlcpy(s, "localhost", slen); | |
595 | else | |
596 | strlcpy(s, http->hostname, slen); | |
597 | } | |
757d2cad | 598 | else |
ef416fc2 | 599 | { |
600 | /* | |
757d2cad | 601 | * Get the hostname... |
ef416fc2 | 602 | */ |
603 | ||
89d46774 | 604 | if (gethostname(s, slen) < 0) |
605 | strlcpy(s, "localhost", slen); | |
757d2cad | 606 | |
607 | if (!strchr(s, '.')) | |
608 | { | |
609 | /* | |
610 | * The hostname is not a FQDN, so look it up... | |
611 | */ | |
612 | ||
89d46774 | 613 | if ((host = gethostbyname(s)) != NULL && host->h_name) |
757d2cad | 614 | strlcpy(s, host->h_name, slen); |
615 | } | |
ef416fc2 | 616 | } |
617 | ||
618 | /* | |
619 | * Return the hostname with as much domain info as we have... | |
620 | */ | |
621 | ||
622 | return (s); | |
623 | } | |
624 | ||
625 | ||
626 | /* | |
b19ccc9e | 627 | * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $". |
ef416fc2 | 628 | */ |