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