]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
61515785 | 2 | * "$Id: http-addr.c 11374 2013-11-04 23:49:10Z msweet $" |
ef416fc2 | 3 | * |
8eabd901 | 4 | * HTTP address routines for CUPS. |
ef416fc2 | 5 | * |
8eabd901 MS |
6 | * Copyright 2007-2013 by Apple Inc. |
7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
ef416fc2 | 8 | * |
8eabd901 MS |
9 | * These coded instructions, statements, and computer programs are the |
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 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
49d87452 MS |
21 | #ifdef HAVE_RESOLV_H |
22 | # include <resolv.h> | |
23 | #endif /* HAVE_RESOLV_H */ | |
a29fd7dd | 24 | #ifdef __APPLE__ |
1106b00e | 25 | # include <CoreFoundation/CoreFoundation.h> |
1106b00e | 26 | # include <SystemConfiguration/SystemConfiguration.h> |
a29fd7dd | 27 | #endif /* __APPLE__ */ |
ef416fc2 | 28 | |
29 | ||
30 | /* | |
31 | * 'httpAddrAny()' - Check for the "any" address. | |
32 | * | |
f3c17241 | 33 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 34 | */ |
35 | ||
36 | int /* O - 1 if "any", 0 otherwise */ | |
37 | httpAddrAny(const http_addr_t *addr) /* I - Address to check */ | |
38 | { | |
89d46774 | 39 | if (!addr) |
40 | return (0); | |
41 | ||
ef416fc2 | 42 | #ifdef AF_INET6 |
43 | if (addr->addr.sa_family == AF_INET6 && | |
44 | IN6_IS_ADDR_UNSPECIFIED(&(addr->ipv6.sin6_addr))) | |
45 | return (1); | |
46 | #endif /* AF_INET6 */ | |
47 | ||
48 | if (addr->addr.sa_family == AF_INET && | |
49 | ntohl(addr->ipv4.sin_addr.s_addr) == 0x00000000) | |
50 | return (1); | |
51 | ||
52 | return (0); | |
53 | } | |
54 | ||
55 | ||
56 | /* | |
57 | * 'httpAddrEqual()' - Compare two addresses. | |
58 | * | |
f3c17241 | 59 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 60 | */ |
61 | ||
ecdc0628 | 62 | int /* O - 1 if equal, 0 if not */ |
ef416fc2 | 63 | httpAddrEqual(const http_addr_t *addr1, /* I - First address */ |
64 | const http_addr_t *addr2) /* I - Second address */ | |
65 | { | |
89d46774 | 66 | if (!addr1 && !addr2) |
67 | return (1); | |
68 | ||
69 | if (!addr1 || !addr2) | |
70 | return (0); | |
71 | ||
ef416fc2 | 72 | if (addr1->addr.sa_family != addr2->addr.sa_family) |
73 | return (0); | |
74 | ||
75 | #ifdef AF_LOCAL | |
76 | if (addr1->addr.sa_family == AF_LOCAL) | |
77 | return (!strcmp(addr1->un.sun_path, addr2->un.sun_path)); | |
78 | #endif /* AF_LOCAL */ | |
79 | ||
80 | #ifdef AF_INET6 | |
81 | if (addr1->addr.sa_family == AF_INET6) | |
82 | return (!memcmp(&(addr1->ipv6.sin6_addr), &(addr2->ipv6.sin6_addr), 16)); | |
83 | #endif /* AF_INET6 */ | |
84 | ||
85 | return (addr1->ipv4.sin_addr.s_addr == addr2->ipv4.sin_addr.s_addr); | |
86 | } | |
87 | ||
88 | ||
89 | /* | |
90 | * 'httpAddrLength()' - Return the length of the address in bytes. | |
91 | * | |
f3c17241 | 92 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 93 | */ |
94 | ||
95 | int /* O - Length in bytes */ | |
96 | httpAddrLength(const http_addr_t *addr) /* I - Address */ | |
97 | { | |
89d46774 | 98 | if (!addr) |
99 | return (0); | |
100 | ||
ef416fc2 | 101 | #ifdef AF_INET6 |
102 | if (addr->addr.sa_family == AF_INET6) | |
103 | return (sizeof(addr->ipv6)); | |
104 | else | |
105 | #endif /* AF_INET6 */ | |
106 | #ifdef AF_LOCAL | |
107 | if (addr->addr.sa_family == AF_LOCAL) | |
108 | return (offsetof(struct sockaddr_un, sun_path) + | |
109 | strlen(addr->un.sun_path) + 1); | |
110 | else | |
111 | #endif /* AF_LOCAL */ | |
112 | if (addr->addr.sa_family == AF_INET) | |
113 | return (sizeof(addr->ipv4)); | |
114 | else | |
115 | return (0); | |
116 | ||
117 | } | |
118 | ||
119 | ||
a469f8a5 MS |
120 | /* |
121 | * 'httpAddrListen()' - Create a listening socket bound to the specified | |
122 | * address and port. | |
123 | * | |
9c0e8e5d | 124 | * @since CUPS 1.7/OS X 10.9@ |
a469f8a5 MS |
125 | */ |
126 | ||
127 | int /* O - Socket or -1 on error */ | |
128 | httpAddrListen(http_addr_t *addr, /* I - Address to bind to */ | |
129 | int port) /* I - Port number to bind to */ | |
130 | { | |
131 | int fd = -1, /* Socket */ | |
132 | val; /* Socket value */ | |
133 | ||
134 | ||
135 | /* | |
136 | * Range check input... | |
137 | */ | |
138 | ||
139 | if (!addr || port <= 0) | |
140 | return (-1); | |
141 | ||
142 | if ((fd = socket(addr->addr.sa_family, SOCK_STREAM, 0)) < 0) | |
143 | { | |
144 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
145 | return (-1); | |
146 | } | |
147 | ||
148 | val = 1; | |
db8b865d | 149 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
150 | |
151 | #ifdef IPV6_V6ONLY | |
152 | if (addr->addr.sa_family == AF_INET6) | |
db8b865d | 153 | setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
154 | #endif /* IPV6_V6ONLY */ |
155 | ||
156 | _httpAddrSetPort(addr, port); | |
157 | ||
158 | if (bind(fd, (struct sockaddr *)addr, httpAddrLength(addr))) | |
159 | { | |
160 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
161 | ||
162 | close(fd); | |
163 | ||
164 | return (-1); | |
165 | } | |
166 | ||
167 | if (listen(fd, 5)) | |
168 | { | |
169 | _cupsSetHTTPError(HTTP_STATUS_ERROR); | |
170 | ||
171 | close(fd); | |
172 | ||
173 | return (-1); | |
174 | } | |
175 | ||
176 | #ifdef SO_NOSIGPIPE | |
177 | /* | |
178 | * Disable SIGPIPE for this socket. | |
179 | */ | |
180 | ||
181 | val = 1; | |
db8b865d | 182 | setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val)); |
a469f8a5 MS |
183 | #endif /* SO_NOSIGPIPE */ |
184 | ||
185 | return (fd); | |
186 | } | |
187 | ||
188 | ||
ef416fc2 | 189 | /* |
190 | * 'httpAddrLocalhost()' - Check for the local loopback address. | |
191 | * | |
f3c17241 | 192 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 193 | */ |
194 | ||
195 | int /* O - 1 if local host, 0 otherwise */ | |
196 | httpAddrLocalhost( | |
197 | const http_addr_t *addr) /* I - Address to check */ | |
198 | { | |
89d46774 | 199 | if (!addr) |
200 | return (1); | |
201 | ||
ef416fc2 | 202 | #ifdef AF_INET6 |
203 | if (addr->addr.sa_family == AF_INET6 && | |
fa73b229 | 204 | IN6_IS_ADDR_LOOPBACK(&(addr->ipv6.sin6_addr))) |
ef416fc2 | 205 | return (1); |
206 | #endif /* AF_INET6 */ | |
207 | ||
208 | #ifdef AF_LOCAL | |
209 | if (addr->addr.sa_family == AF_LOCAL) | |
210 | return (1); | |
211 | #endif /* AF_LOCAL */ | |
212 | ||
213 | if (addr->addr.sa_family == AF_INET && | |
e07d4801 | 214 | (ntohl(addr->ipv4.sin_addr.s_addr) & 0xff000000) == 0x7f000000) |
ef416fc2 | 215 | return (1); |
216 | ||
217 | return (0); | |
218 | } | |
219 | ||
220 | ||
ef416fc2 | 221 | /* |
222 | * 'httpAddrLookup()' - Lookup the hostname associated with the address. | |
223 | * | |
f3c17241 | 224 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 225 | */ |
226 | ||
ecdc0628 | 227 | char * /* O - Host name */ |
228 | httpAddrLookup( | |
229 | const http_addr_t *addr, /* I - Address to lookup */ | |
230 | char *name, /* I - Host name buffer */ | |
231 | int namelen) /* I - Size of name buffer */ | |
ef416fc2 | 232 | { |
49d87452 MS |
233 | _cups_globals_t *cg = _cupsGlobals(); |
234 | /* Global data */ | |
235 | ||
236 | ||
e07d4801 MS |
237 | DEBUG_printf(("httpAddrLookup(addr=%p, name=%p, namelen=%d)", addr, name, |
238 | namelen)); | |
ef416fc2 | 239 | |
240 | /* | |
241 | * Range check input... | |
242 | */ | |
243 | ||
244 | if (!addr || !name || namelen <= 2) | |
245 | { | |
246 | if (name && namelen >= 1) | |
247 | *name = '\0'; | |
248 | ||
249 | return (NULL); | |
250 | } | |
251 | ||
252 | #ifdef AF_LOCAL | |
253 | if (addr->addr.sa_family == AF_LOCAL) | |
49d87452 | 254 | { |
ef416fc2 | 255 | strlcpy(name, addr->un.sun_path, namelen); |
49d87452 MS |
256 | return (name); |
257 | } | |
ef416fc2 | 258 | #endif /* AF_LOCAL */ |
49d87452 | 259 | |
d2354e63 MS |
260 | /* |
261 | * Optimize lookups for localhost/loopback addresses... | |
262 | */ | |
263 | ||
264 | if (httpAddrLocalhost(addr)) | |
265 | { | |
266 | strlcpy(name, "localhost", namelen); | |
267 | return (name); | |
268 | } | |
269 | ||
49d87452 MS |
270 | #ifdef HAVE_RES_INIT |
271 | /* | |
272 | * STR #2920: Initialize resolver after failure in cups-polld | |
273 | * | |
274 | * If the previous lookup failed, re-initialize the resolver to prevent | |
275 | * temporary network errors from persisting. This *should* be handled by | |
276 | * the resolver libraries, but apparently the glibc folks do not agree. | |
277 | * | |
278 | * We set a flag at the end of this function if we encounter an error that | |
279 | * requires reinitialization of the resolver functions. We then call | |
280 | * res_init() if the flag is set on the next call here or in httpAddrLookup(). | |
281 | */ | |
282 | ||
283 | if (cg->need_res_init) | |
284 | { | |
285 | res_init(); | |
286 | ||
287 | cg->need_res_init = 0; | |
288 | } | |
289 | #endif /* HAVE_RES_INIT */ | |
290 | ||
ef416fc2 | 291 | #ifdef HAVE_GETNAMEINFO |
292 | { | |
db1f069b MS |
293 | /* |
294 | * STR #2486: httpAddrLookup() fails when getnameinfo() returns EAI_AGAIN | |
295 | * | |
296 | * FWIW, I think this is really a bug in the implementation of | |
297 | * getnameinfo(), but falling back on httpAddrString() is easy to | |
298 | * do... | |
299 | */ | |
ef416fc2 | 300 | |
49d87452 MS |
301 | int error = getnameinfo(&addr->addr, httpAddrLength(addr), name, namelen, |
302 | NULL, 0, 0); | |
303 | ||
304 | if (error) | |
305 | { | |
306 | if (error == EAI_FAIL) | |
307 | cg->need_res_init = 1; | |
308 | ||
db1f069b | 309 | return (httpAddrString(addr, name, namelen)); |
49d87452 | 310 | } |
ef416fc2 | 311 | } |
312 | #else | |
313 | { | |
314 | struct hostent *host; /* Host from name service */ | |
315 | ||
316 | ||
317 | # ifdef AF_INET6 | |
318 | if (addr->addr.sa_family == AF_INET6) | |
3dd9c340 | 319 | host = gethostbyaddr((char *)&(addr->ipv6.sin6_addr), |
ef416fc2 | 320 | sizeof(struct in_addr), AF_INET6); |
321 | else | |
322 | # endif /* AF_INET6 */ | |
3dd9c340 | 323 | host = gethostbyaddr((char *)&(addr->ipv4.sin_addr), |
ef416fc2 | 324 | sizeof(struct in_addr), AF_INET); |
325 | ||
326 | if (host == NULL) | |
327 | { | |
328 | /* | |
329 | * No hostname, so return the raw address... | |
330 | */ | |
331 | ||
49d87452 MS |
332 | if (h_errno == NO_RECOVERY) |
333 | cg->need_res_init = 1; | |
334 | ||
335 | return (httpAddrString(addr, name, namelen)); | |
ef416fc2 | 336 | } |
337 | ||
338 | strlcpy(name, host->h_name, namelen); | |
339 | } | |
340 | #endif /* HAVE_GETNAMEINFO */ | |
341 | ||
e07d4801 MS |
342 | DEBUG_printf(("1httpAddrLookup: returning \"%s\"...", name)); |
343 | ||
ef416fc2 | 344 | return (name); |
345 | } | |
346 | ||
347 | ||
1ff0402e | 348 | /* |
a469f8a5 MS |
349 | * 'httpAddrPort()' - Get the port number associated with an address. |
350 | * | |
9c0e8e5d | 351 | * @since CUPS 1.7/OS X 10.9@ |
1ff0402e MS |
352 | */ |
353 | ||
354 | int /* O - Port number */ | |
a469f8a5 | 355 | httpAddrPort(http_addr_t *addr) /* I - Address */ |
1ff0402e | 356 | { |
5f64df29 MS |
357 | if (!addr) |
358 | return (ippPort()); | |
1ff0402e | 359 | #ifdef AF_INET6 |
5f64df29 | 360 | else if (addr->addr.sa_family == AF_INET6) |
1ff0402e | 361 | return (ntohs(addr->ipv6.sin6_port)); |
1ff0402e | 362 | #endif /* AF_INET6 */ |
5f64df29 | 363 | else if (addr->addr.sa_family == AF_INET) |
1ff0402e MS |
364 | return (ntohs(addr->ipv4.sin_port)); |
365 | else | |
366 | return (ippPort()); | |
367 | } | |
368 | ||
c1420c87 MS |
369 | /* For OS X 10.8 and earlier */ |
370 | int _httpAddrPort(http_addr_t *addr) { return (httpAddrPort(addr)); } | |
371 | ||
1ff0402e | 372 | |
22c9029b MS |
373 | /* |
374 | * '_httpAddrSetPort()' - Set the port number associated with an address. | |
375 | */ | |
376 | ||
377 | void | |
378 | _httpAddrSetPort(http_addr_t *addr, /* I - Address */ | |
379 | int port) /* I - Port */ | |
380 | { | |
381 | if (!addr || port <= 0) | |
382 | return; | |
383 | ||
384 | #ifdef AF_INET6 | |
385 | if (addr->addr.sa_family == AF_INET6) | |
386 | addr->ipv6.sin6_port = htons(port); | |
387 | else | |
388 | #endif /* AF_INET6 */ | |
389 | if (addr->addr.sa_family == AF_INET) | |
390 | addr->ipv4.sin_port = htons(port); | |
391 | } | |
392 | ||
393 | ||
ef416fc2 | 394 | /* |
ecdc0628 | 395 | * 'httpAddrString()' - Convert an address to a numeric string. |
ef416fc2 | 396 | * |
f3c17241 | 397 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 398 | */ |
399 | ||
ecdc0628 | 400 | char * /* O - Numeric address string */ |
401 | httpAddrString(const http_addr_t *addr, /* I - Address to convert */ | |
402 | char *s, /* I - String buffer */ | |
403 | int slen) /* I - Length of string */ | |
ef416fc2 | 404 | { |
e07d4801 | 405 | DEBUG_printf(("httpAddrString(addr=%p, s=%p, slen=%d)", addr, s, slen)); |
ef416fc2 | 406 | |
407 | /* | |
408 | * Range check input... | |
409 | */ | |
410 | ||
411 | if (!addr || !s || slen <= 2) | |
412 | { | |
413 | if (s && slen >= 1) | |
414 | *s = '\0'; | |
415 | ||
416 | return (NULL); | |
417 | } | |
418 | ||
419 | #ifdef AF_LOCAL | |
420 | if (addr->addr.sa_family == AF_LOCAL) | |
771bd8cb MS |
421 | { |
422 | if (addr->un.sun_path[0] == '/') | |
423 | strlcpy(s, addr->un.sun_path, slen); | |
424 | else | |
425 | strlcpy(s, "localhost", slen); | |
426 | } | |
ef416fc2 | 427 | else |
428 | #endif /* AF_LOCAL */ | |
b423cd4c | 429 | if (addr->addr.sa_family == AF_INET) |
ef416fc2 | 430 | { |
b423cd4c | 431 | unsigned temp; /* Temporary address */ |
ef416fc2 | 432 | |
433 | ||
b423cd4c | 434 | temp = ntohl(addr->ipv4.sin_addr.s_addr); |
435 | ||
436 | snprintf(s, slen, "%d.%d.%d.%d", (temp >> 24) & 255, | |
437 | (temp >> 16) & 255, (temp >> 8) & 255, temp & 255); | |
438 | } | |
439 | #ifdef AF_INET6 | |
440 | else if (addr->addr.sa_family == AF_INET6) | |
441 | { | |
82f97232 MS |
442 | char *sptr, /* Pointer into string */ |
443 | temps[64]; /* Temporary string for address */ | |
444 | ||
b423cd4c | 445 | # ifdef HAVE_GETNAMEINFO |
82f97232 | 446 | if (getnameinfo(&addr->addr, httpAddrLength(addr), temps, sizeof(temps), |
b423cd4c | 447 | NULL, 0, NI_NUMERICHOST)) |
ef416fc2 | 448 | { |
449 | /* | |
450 | * If we get an error back, then the address type is not supported | |
451 | * and we should zero out the buffer... | |
452 | */ | |
453 | ||
454 | s[0] = '\0'; | |
455 | ||
456 | return (NULL); | |
457 | } | |
82f97232 MS |
458 | else if ((sptr = strchr(temps, '%')) != NULL) |
459 | { | |
460 | /* | |
461 | * Convert "%zone" to "+zone" to match URI form... | |
462 | */ | |
463 | ||
464 | *sptr = '+'; | |
465 | } | |
466 | ||
b423cd4c | 467 | # else |
b423cd4c | 468 | int i; /* Looping var */ |
469 | unsigned temp; /* Current value */ | |
470 | const char *prefix; /* Prefix for address */ | |
471 | ||
472 | ||
473 | prefix = ""; | |
82f97232 | 474 | for (sptr = temps, i = 0; i < 4 && addr->ipv6.sin6_addr.s6_addr32[i]; i ++) |
ef416fc2 | 475 | { |
b423cd4c | 476 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 477 | |
82f97232 MS |
478 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
479 | (temp >> 16) & 0xffff); | |
b423cd4c | 480 | prefix = ":"; |
b423cd4c | 481 | sptr += strlen(sptr); |
ef416fc2 | 482 | |
b423cd4c | 483 | temp &= 0xffff; |
ef416fc2 | 484 | |
b423cd4c | 485 | if (temp || i == 3 || addr->ipv6.sin6_addr.s6_addr32[i + 1]) |
486 | { | |
82f97232 | 487 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, temp); |
ef416fc2 | 488 | sptr += strlen(sptr); |
ef416fc2 | 489 | } |
b423cd4c | 490 | } |
491 | ||
492 | if (i < 4) | |
493 | { | |
494 | while (i < 4 && !addr->ipv6.sin6_addr.s6_addr32[i]) | |
495 | i ++; | |
ef416fc2 | 496 | |
497 | if (i < 4) | |
498 | { | |
82f97232 | 499 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s:", prefix); |
b423cd4c | 500 | prefix = ":"; |
b423cd4c | 501 | sptr += strlen(sptr); |
ef416fc2 | 502 | |
b423cd4c | 503 | for (; i < 4; i ++) |
ef416fc2 | 504 | { |
b423cd4c | 505 | temp = ntohl(addr->ipv6.sin6_addr.s6_addr32[i]); |
ef416fc2 | 506 | |
82f97232 MS |
507 | if ((temp & 0xffff0000) || |
508 | (i > 0 && addr->ipv6.sin6_addr.s6_addr32[i - 1])) | |
ef416fc2 | 509 | { |
82f97232 MS |
510 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
511 | (temp >> 16) & 0xffff); | |
ef416fc2 | 512 | sptr += strlen(sptr); |
b423cd4c | 513 | } |
ef416fc2 | 514 | |
82f97232 MS |
515 | snprintf(sptr, sizeof(temps) - (sptr - temps), "%s%x", prefix, |
516 | temp & 0xffff); | |
b423cd4c | 517 | sptr += strlen(sptr); |
ef416fc2 | 518 | } |
519 | } | |
b423cd4c | 520 | else if (sptr == s) |
521 | { | |
522 | /* | |
523 | * Empty address... | |
524 | */ | |
ef416fc2 | 525 | |
82f97232 | 526 | strlcpy(temps, "::", sizeof(temps)); |
b423cd4c | 527 | } |
528 | else | |
529 | { | |
530 | /* | |
531 | * Empty at end... | |
532 | */ | |
ef416fc2 | 533 | |
82f97232 | 534 | strlcpy(sptr, "::", sizeof(temps) - (sptr - temps)); |
b423cd4c | 535 | } |
ef416fc2 | 536 | } |
b423cd4c | 537 | # endif /* HAVE_GETNAMEINFO */ |
82f97232 MS |
538 | |
539 | /* | |
540 | * Add "[v1." and "]" around IPv6 address to convert to URI form. | |
541 | */ | |
542 | ||
543 | snprintf(s, slen, "[v1.%s]", temps); | |
ef416fc2 | 544 | } |
b423cd4c | 545 | #endif /* AF_INET6 */ |
546 | else | |
547 | strlcpy(s, "UNKNOWN", slen); | |
ef416fc2 | 548 | |
e07d4801 | 549 | DEBUG_printf(("1httpAddrString: returning \"%s\"...", s)); |
ef416fc2 | 550 | |
551 | return (s); | |
552 | } | |
553 | ||
554 | ||
555 | /* | |
556 | * 'httpGetHostByName()' - Lookup a hostname or IPv4 address, and return | |
557 | * address records for the specified name. | |
558 | * | |
559 | * @deprecated@ | |
560 | */ | |
561 | ||
562 | struct hostent * /* O - Host entry */ | |
563 | httpGetHostByName(const char *name) /* I - Hostname or IP address */ | |
564 | { | |
565 | const char *nameptr; /* Pointer into name */ | |
566 | unsigned ip[4]; /* IP address components */ | |
567 | _cups_globals_t *cg = _cupsGlobals(); | |
568 | /* Pointer to library globals */ | |
569 | ||
570 | ||
e07d4801 | 571 | DEBUG_printf(("httpGetHostByName(name=\"%s\")", name)); |
ef416fc2 | 572 | |
573 | /* | |
574 | * Avoid lookup delays and configuration problems when connecting | |
575 | * to the localhost address... | |
576 | */ | |
577 | ||
578 | if (!strcmp(name, "localhost")) | |
579 | name = "127.0.0.1"; | |
580 | ||
581 | /* | |
582 | * This function is needed because some operating systems have a | |
583 | * buggy implementation of gethostbyname() that does not support | |
584 | * IP addresses. If the first character of the name string is a | |
585 | * number, then sscanf() is used to extract the IP components. | |
586 | * We then pack the components into an IPv4 address manually, | |
587 | * since the inet_aton() function is deprecated. We use the | |
588 | * htonl() macro to get the right byte order for the address. | |
589 | * | |
590 | * We also support domain sockets when supported by the underlying | |
591 | * OS... | |
592 | */ | |
593 | ||
594 | #ifdef AF_LOCAL | |
595 | if (name[0] == '/') | |
596 | { | |
597 | /* | |
598 | * A domain socket address, so make an AF_LOCAL entry and return it... | |
599 | */ | |
600 | ||
601 | cg->hostent.h_name = (char *)name; | |
602 | cg->hostent.h_aliases = NULL; | |
603 | cg->hostent.h_addrtype = AF_LOCAL; | |
604 | cg->hostent.h_length = strlen(name) + 1; | |
605 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
606 | cg->ip_ptrs[0] = (char *)name; | |
607 | cg->ip_ptrs[1] = NULL; | |
608 | ||
e07d4801 | 609 | DEBUG_puts("1httpGetHostByName: returning domain socket address..."); |
ef416fc2 | 610 | |
611 | return (&cg->hostent); | |
612 | } | |
613 | #endif /* AF_LOCAL */ | |
614 | ||
615 | for (nameptr = name; isdigit(*nameptr & 255) || *nameptr == '.'; nameptr ++); | |
616 | ||
617 | if (!*nameptr) | |
618 | { | |
619 | /* | |
620 | * We have an IPv4 address; break it up and provide the host entry | |
621 | * to the caller. | |
622 | */ | |
623 | ||
624 | if (sscanf(name, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) | |
625 | return (NULL); /* Must have 4 numbers */ | |
626 | ||
627 | if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255) | |
628 | return (NULL); /* Invalid byte ranges! */ | |
629 | ||
8eabd901 MS |
630 | cg->ip_addr = htonl((((((((unsigned)ip[0] << 8) | (unsigned)ip[1]) << 8) | |
631 | (unsigned)ip[2]) << 8) | | |
632 | (unsigned)ip[3])); | |
ef416fc2 | 633 | |
634 | /* | |
635 | * Fill in the host entry and return it... | |
636 | */ | |
637 | ||
638 | cg->hostent.h_name = (char *)name; | |
639 | cg->hostent.h_aliases = NULL; | |
640 | cg->hostent.h_addrtype = AF_INET; | |
641 | cg->hostent.h_length = 4; | |
642 | cg->hostent.h_addr_list = cg->ip_ptrs; | |
643 | cg->ip_ptrs[0] = (char *)&(cg->ip_addr); | |
644 | cg->ip_ptrs[1] = NULL; | |
645 | ||
e07d4801 | 646 | DEBUG_puts("1httpGetHostByName: returning IPv4 address..."); |
ef416fc2 | 647 | |
648 | return (&cg->hostent); | |
649 | } | |
650 | else | |
651 | { | |
652 | /* | |
653 | * Use the gethostbyname() function to get the IPv4 address for | |
654 | * the name... | |
655 | */ | |
656 | ||
e07d4801 | 657 | DEBUG_puts("1httpGetHostByName: returning domain lookup address(es)..."); |
ef416fc2 | 658 | |
659 | return (gethostbyname(name)); | |
660 | } | |
661 | } | |
662 | ||
663 | ||
664 | /* | |
757d2cad | 665 | * 'httpGetHostname()' - Get the FQDN for the connection or local system. |
ef416fc2 | 666 | * |
757d2cad | 667 | * When "http" points to a connected socket, return the hostname or |
668 | * address that was used in the call to httpConnect() or httpConnectEncrypt(). | |
669 | * Otherwise, return the FQDN for the local system using both gethostname() | |
670 | * and gethostbyname() to get the local hostname with domain. | |
ef416fc2 | 671 | * |
f3c17241 | 672 | * @since CUPS 1.2/OS X 10.5@ |
ef416fc2 | 673 | */ |
674 | ||
757d2cad | 675 | const char * /* O - FQDN for connection or system */ |
676 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ | |
677 | char *s, /* I - String buffer for name */ | |
678 | int slen) /* I - Size of buffer */ | |
ef416fc2 | 679 | { |
89d46774 | 680 | if (!s || slen <= 1) |
681 | return (NULL); | |
682 | ||
757d2cad | 683 | if (http) |
480ef0fe | 684 | { |
685 | if (http->hostname[0] == '/') | |
686 | strlcpy(s, "localhost", slen); | |
687 | else | |
688 | strlcpy(s, http->hostname, slen); | |
689 | } | |
757d2cad | 690 | else |
ef416fc2 | 691 | { |
692 | /* | |
757d2cad | 693 | * Get the hostname... |
ef416fc2 | 694 | */ |
695 | ||
89d46774 | 696 | if (gethostname(s, slen) < 0) |
697 | strlcpy(s, "localhost", slen); | |
757d2cad | 698 | |
699 | if (!strchr(s, '.')) | |
700 | { | |
0837b7e8 | 701 | #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME |
1106b00e MS |
702 | /* |
703 | * The hostname is not a FQDN, so use the local hostname from the | |
704 | * SystemConfiguration framework... | |
705 | */ | |
706 | ||
707 | SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault, | |
708 | CFSTR("libcups"), NULL, NULL); | |
709 | /* System configuration data */ | |
710 | CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL; | |
711 | /* Local host name */ | |
712 | char localStr[1024]; /* Local host name C string */ | |
713 | ||
714 | if (local && CFStringGetCString(local, localStr, sizeof(localStr), | |
715 | kCFStringEncodingUTF8)) | |
716 | { | |
717 | /* | |
718 | * Append ".local." to the hostname we get... | |
719 | */ | |
720 | ||
721 | snprintf(s, slen, "%s.local.", localStr); | |
722 | } | |
723 | ||
724 | if (local) | |
725 | CFRelease(local); | |
726 | if (sc) | |
727 | CFRelease(sc); | |
728 | ||
729 | #else | |
757d2cad | 730 | /* |
731 | * The hostname is not a FQDN, so look it up... | |
732 | */ | |
733 | ||
1106b00e MS |
734 | struct hostent *host; /* Host entry to get FQDN */ |
735 | ||
89d46774 | 736 | if ((host = gethostbyname(s)) != NULL && host->h_name) |
1106b00e MS |
737 | { |
738 | /* | |
739 | * Use the resolved hostname... | |
740 | */ | |
741 | ||
757d2cad | 742 | strlcpy(s, host->h_name, slen); |
1106b00e | 743 | } |
0837b7e8 | 744 | #endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */ |
757d2cad | 745 | } |
ef416fc2 | 746 | } |
747 | ||
748 | /* | |
749 | * Return the hostname with as much domain info as we have... | |
750 | */ | |
751 | ||
752 | return (s); | |
753 | } | |
754 | ||
755 | ||
756 | /* | |
61515785 | 757 | * End of "$Id: http-addr.c 11374 2013-11-04 23:49:10Z msweet $". |
ef416fc2 | 758 | */ |