]>
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 && | |
e07d4801 | 157 | (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000) |
ef416fc2 | 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 | ||
e07d4801 MS |
187 | DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name, |
188 | namelen)); | |
ef416fc2 | 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 | ||
e07d4801 MS |
292 | DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name)); |
293 | ||
ef416fc2 | 294 | return (name); |
295 | } | |
296 | ||
297 | ||
1ff0402e MS |
298 | /* |
299 | * '_httpAddrPort()' - Get the port number associated with an address. | |
300 | */ | |
301 | ||
302 | int /* O - Port number */ | |
303 | _httpAddrPort(http_addr_t *addr) /* I - Address */ | |
304 | { | |
5f64df29 MS |
305 | if (!addr) |
306 | return (ippPort()); | |
1ff0402e | 307 | #ifdef AF_INET6 |
5f64df29 | 308 | else if (addr->addr.sa_family == AF_INET6) |
1ff0402e | 309 | return (ntohs(addr->ipv6.sin6_port)); |
1ff0402e | 310 | #endif /* AF_INET6 */ |
5f64df29 | 311 | else if (addr->addr.sa_family == AF_INET) |
1ff0402e MS |
312 | return (ntohs(addr->ipv4.sin_port)); |
313 | else | |
314 | return (ippPort()); | |
315 | } | |
316 | ||
317 | ||
ef416fc2 | 318 | /* |
ecdc0628 | 319 | * 'httpAddrString()' - Convert an address to a numeric string. |
ef416fc2 | 320 | * |
426c6a59 | 321 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 322 | */ |
323 | ||
ecdc0628 | 324 | char * /* O - Numeric address string */ |
325 | httpAddrString(const http_addr_t *addr, /* I - Address to convert */ | |
326 | char *s, /* I - String buffer */ | |
327 | int slen) /* I - Length of string */ | |
ef416fc2 | 328 | { |
e07d4801 | 329 | DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen)); |
ef416fc2 | 330 | |
331 | /* | |
332 | * Range check input... | |
333 | */ | |
334 | ||
335 | if (!addr || !s || slen <= 2) | |
336 | { | |
337 | if (s && slen >= 1) | |
338 | *s = '\0'; | |
339 | ||
340 | return (NULL); | |
341 | } | |
342 | ||
343 | #ifdef AF_LOCAL | |
344 | if (addr->addr.sa_family == AF_LOCAL) | |
345 | strlcpy(s, addr->un.sun_path, slen); | |
346 | else | |
347 | #endif /* AF_LOCAL */ | |
b423cd4c | 348 | if (addr->addr.sa_family == AF_INET) |
ef416fc2 | 349 | { |
b423cd4c | 350 | unsigned temp; /* Temporary address */ |
ef416fc2 | 351 | |
352 | ||
b423cd4c | 353 | temp = ntohl(addr->ipv4.sin_addr.s_addr); |
354 | ||
355 | snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, | |
356 | (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); | |
357 | } | |
358 | #ifdef AF_INET6 | |
359 | else if (addr->addr.sa_family == AF_INET6) | |
360 | { | |
361 | # ifdef HAVE_GETNAMEINFO | |
ef416fc2 | 362 | if (getnameinfo(&addr->addr, httpAddrLength(addr), s, slen, |
b423cd4c | 363 | NULL, 0, NI_NUMERICHOST)) |
ef416fc2 | 364 | { |
365 | /* | |
366 | * If we get an error back, then the address type is not supported | |
367 | * and we should zero out the buffer... | |
368 | */ | |
369 | ||
370 | s[0] = '\0'; | |
371 | ||
372 | return (NULL); | |
373 | } | |
b423cd4c | 374 | # else |
375 | char *sptr; /* Pointer into string */ | |
376 | int i; /* Looping var */ | |
377 | unsigned temp; /* Current value */ | |
378 | const char *prefix; /* Prefix for address */ | |
379 | ||
380 | ||
381 | prefix = ""; | |
382 | for (sptr = s, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) | |
ef416fc2 | 383 | { |
b423cd4c | 384 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 385 | |
b423cd4c | 386 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
387 | prefix = ":"; | |
388 | slen -= strlen(sptr); | |
389 | sptr += strlen(sptr); | |
ef416fc2 | 390 | |
b423cd4c | 391 | temp &= 0xffff; |
ef416fc2 | 392 | |
b423cd4c | 393 | if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) |
394 | { | |
395 | snprintf(sptr, slen, "%s%x", prefix, temp); | |
ef416fc2 | 396 | slen -= strlen(sptr); |
397 | sptr += strlen(sptr); | |
ef416fc2 | 398 | } |
b423cd4c | 399 | } |
400 | ||
401 | if (i < 4) | |
402 | { | |
403 | while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) | |
404 | i ++; | |
ef416fc2 | 405 | |
406 | if (i < 4) | |
407 | { | |
b423cd4c | 408 | snprintf(sptr, slen, "%s:", prefix); |
409 | prefix = ":"; | |
410 | slen -= strlen(sptr); | |
411 | sptr += strlen(sptr); | |
ef416fc2 | 412 | |
b423cd4c | 413 | for (; i < 4; i ++) |
ef416fc2 | 414 | { |
b423cd4c | 415 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 416 | |
b423cd4c | 417 | if ((temp & 0xffff0000) || addr->ipv6.sin6_addr.s6_addr32[i - 1]) |
ef416fc2 | 418 | { |
b423cd4c | 419 | snprintf(sptr, slen, "%s%x", prefix, (temp >> 16) & 0xffff); |
ef416fc2 | 420 | slen -= strlen(sptr); |
421 | sptr += strlen(sptr); | |
b423cd4c | 422 | } |
ef416fc2 | 423 | |
b423cd4c | 424 | snprintf(sptr, slen, "%s%x", prefix, temp & 0xffff); |
425 | slen -= strlen(sptr); | |
426 | sptr += strlen(sptr); | |
ef416fc2 | 427 | } |
428 | } | |
b423cd4c | 429 | else if (sptr == s) |
430 | { | |
431 | /* | |
432 | * Empty address... | |
433 | */ | |
ef416fc2 | 434 | |
b423cd4c | 435 | strlcpy(s, "::", slen); |
436 | sptr = s + 2; | |
437 | slen -= 2; | |
438 | } | |
439 | else | |
440 | { | |
441 | /* | |
442 | * Empty at end... | |
443 | */ | |
ef416fc2 | 444 | |
b423cd4c | 445 | strlcpy(sptr, "::", slen); |
446 | sptr += 2; | |
447 | slen -= 2; | |
448 | } | |
ef416fc2 | 449 | } |
b423cd4c | 450 | # endif /* HAVE_GETNAMEINFO */ |
ef416fc2 | 451 | } |
b423cd4c | 452 | #endif /* AF_INET6 */ |
453 | else | |
454 | strlcpy(s, "UNKNOWN", slen); | |
ef416fc2 | 455 | |
e07d4801 | 456 | DEBUG_printf(("1httpAddrString: returning \"%s\"...", s)); |
ef416fc2 | 457 | |
458 | return (s); | |
459 | } | |
460 | ||
461 | ||
462 | /* | |
463 | * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return | |
464 | * address records for the specified name. | |
465 | * | |
466 | * @deprecated@ | |
467 | */ | |
468 | ||
469 | struct hostent * /* O - Host entry */ | |
470 | httpGetHostByName(const char *name) /* I - Hostname or IP address */ | |
471 | { | |
472 | const char *nameptr; /* Pointer into name */ | |
473 | unsigned ip[4]; /* IP address components */ | |
474 | _cups_globals_t *cg = _cupsGlobals(); | |
475 | /* Pointer to library globals */ | |
476 | ||
477 | ||
e07d4801 | 478 | DEBUG_printf(("httpGetHostByName(name=\"%s\")", name)); |
ef416fc2 | 479 | |
480 | /* | |
481 | * Avoid lookup delays and configuration problems when connecting | |
482 | * to the localhost address... | |
483 | */ | |
484 | ||
485 | if (!strcmp(name, "localhost")) | |
486 | name = "127.0.0.1"; | |
487 | ||
488 | /* | |
489 | * This function is needed because some operating systems have a | |
490 | * buggy implementation of gethostbyname() that does not support | |
491 | * IP addresses. If the first character of the name string is a | |
492 | * number, then sscanf() is used to extract the IP components. | |
493 | * We then pack the components into an IPv4 address manually, | |
494 | * since the inet_aton() function is deprecated. We use the | |
495 | * htonl() macro to get the right byte order for the address. | |
496 | * | |
497 | * We also support domain sockets when supported by the underlying | |
498 | * OS... | |
499 | */ | |
500 | ||
501 | #ifdef AF_LOCAL | |
502 | if (name[0] == '/') | |
503 | { | |
504 | /* | |
505 | * A domain socket address, so make an AF_LOCAL entry and return it... | |
506 | */ | |
507 | ||
508 | cg->hostent.h_name = (char *)name; | |
509 | cg->hostent.h_aliases = NULL; | |
510 | cg->hostent.h_addrtype = AF_LOCAL; | |
511 | cg->hostent.h_length = strlen(name) + 1; | |
512 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
513 | cg->ip_ptrs[0] = (char *)name; | |
514 | cg->ip_ptrs[1] = NULL; | |
515 | ||
e07d4801 | 516 | DEBUG_puts("1httpGetHostByName: returning domain socket address..."); |
ef416fc2 | 517 | |
518 | return (&cg->hostent); | |
519 | } | |
520 | #endif /* AF_LOCAL */ | |
521 | ||
522 | for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); | |
523 | ||
524 | if (!*nameptr) | |
525 | { | |
526 | /* | |
527 | * We have an IPv4 address; break it up and provide the host entry | |
528 | * to the caller. | |
529 | */ | |
530 | ||
531 | if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) | |
532 | return (NULL); /* Must have 4 numbers */ | |
533 | ||
534 | if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) | |
535 | return (NULL); /* Invalid byte ranges! */ | |
536 | ||
537 | cg->ip_addr = htonl(((((((ip[0] << 8) | ip[1]) << 8) | ip[2]) << 8) | | |
538 | ip[3])); | |
539 | ||
540 | /* | |
541 | * Fill in the host entry and return it... | |
542 | */ | |
543 | ||
544 | cg->hostent.h_name = (char *)name; | |
545 | cg->hostent.h_aliases = NULL; | |
546 | cg->hostent.h_addrtype = AF_INET; | |
547 | cg->hostent.h_length = 4; | |
548 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
549 | cg->ip_ptrs[0] = (char *)&(cg->ip_addr); | |
550 | cg->ip_ptrs[1] = NULL; | |
551 | ||
e07d4801 | 552 | DEBUG_puts("1httpGetHostByName: returning IPv4 address..."); |
ef416fc2 | 553 | |
554 | return (&cg->hostent); | |
555 | } | |
556 | else | |
557 | { | |
558 | /* | |
559 | * Use the gethostbyname() function to get the IPv4 address for | |
560 | * the name... | |
561 | */ | |
562 | ||
e07d4801 | 563 | DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)..."); |
ef416fc2 | 564 | |
565 | return (gethostbyname(name)); | |
566 | } | |
567 | } | |
568 | ||
569 | ||
570 | /* | |
757d2cad | 571 | * 'httpGetHostname()' - Get the FQDN for the connection or local system. |
ef416fc2 | 572 | * |
757d2cad | 573 | * When "http" points to a connected socket, return the hostname or |
574 | * address that was used in the call to httpConnect() or httpConnectEncrypt(). | |
575 | * Otherwise, return the FQDN for the local system using both gethostname() | |
576 | * and gethostbyname() to get the local hostname with domain. | |
ef416fc2 | 577 | * |
426c6a59 | 578 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 579 | */ |
580 | ||
757d2cad | 581 | const char * /* O - FQDN for connection or system */ |
582 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ | |
583 | char *s, /* I - String buffer for name */ | |
584 | int slen) /* I - Size of buffer */ | |
ef416fc2 | 585 | { |
586 | struct hostent *host; /* Host entry to get FQDN */ | |
587 | ||
588 | ||
89d46774 | 589 | if (!s || slen <= 1) |
590 | return (NULL); | |
591 | ||
757d2cad | 592 | if (http) |
480ef0fe | 593 | { |
594 | if (http->hostname[0] == '/') | |
595 | strlcpy(s, "localhost", slen); | |
596 | else | |
597 | strlcpy(s, http->hostname, slen); | |
598 | } | |
757d2cad | 599 | else |
ef416fc2 | 600 | { |
601 | /* | |
757d2cad | 602 | * Get the hostname... |
ef416fc2 | 603 | */ |
604 | ||
89d46774 | 605 | if (gethostname(s, slen) < 0) |
606 | strlcpy(s, "localhost", slen); | |
757d2cad | 607 | |
608 | if (!strchr(s, '.')) | |
609 | { | |
610 | /* | |
611 | * The hostname is not a FQDN, so look it up... | |
612 | */ | |
613 | ||
89d46774 | 614 | if ((host = gethostbyname(s)) != NULL && host->h_name) |
757d2cad | 615 | strlcpy(s, host->h_name, slen); |
616 | } | |
ef416fc2 | 617 | } |
618 | ||
619 | /* | |
620 | * Return the hostname with as much domain info as we have... | |
621 | */ | |
622 | ||
623 | return (s); | |
624 | } | |
625 | ||
626 | ||
627 | /* | |
b19ccc9e | 628 | * End of "$Id: http-addr.c 7910 2008-09-06 00:25:17Z mike $". |
ef416fc2 | 629 | */ |