]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
84ec3a84 | 4 | * Network interface functions for the CUPS scheduler. |
ef416fc2 | 5 | * |
84ec3a84 MS |
6 | * Copyright 2007-2014 by Apple Inc. |
7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
ef416fc2 | 8 | * |
84ec3a84 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 | * "LICENSE" which should have been included with this file. If this | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers. | |
18 | */ | |
19 | ||
89d46774 | 20 | #include <cups/http-private.h> |
ef416fc2 | 21 | #include "cupsd.h" |
22 | ||
e00b005a | 23 | |
24 | /* | |
25 | * Local functions... | |
26 | */ | |
27 | ||
28 | static void cupsdNetIFFree(void); | |
29 | static int compare_netif(cupsd_netif_t *a, cupsd_netif_t *b); | |
30 | ||
31 | ||
ef416fc2 | 32 | /* |
33 | * 'cupsdNetIFFind()' - Find a network interface. | |
34 | */ | |
35 | ||
36 | cupsd_netif_t * /* O - Network interface data */ | |
37 | cupsdNetIFFind(const char *name) /* I - Name of interface */ | |
38 | { | |
e00b005a | 39 | cupsd_netif_t key; /* Search key */ |
ef416fc2 | 40 | |
41 | ||
42 | /* | |
43 | * Update the interface list as needed... | |
44 | */ | |
45 | ||
411affcf | 46 | if (NetIFUpdate) |
47 | cupsdNetIFUpdate(); | |
ef416fc2 | 48 | |
49 | /* | |
50 | * Search for the named interface... | |
51 | */ | |
52 | ||
e00b005a | 53 | strlcpy(key.name, name, sizeof(key.name)); |
ef416fc2 | 54 | |
e00b005a | 55 | return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key)); |
ef416fc2 | 56 | } |
57 | ||
58 | ||
59 | /* | |
60 | * 'cupsdNetIFFree()' - Free the current network interface list. | |
61 | */ | |
62 | ||
e00b005a | 63 | static void |
ef416fc2 | 64 | cupsdNetIFFree(void) |
65 | { | |
e00b005a | 66 | cupsd_netif_t *current; /* Current interface in array */ |
ef416fc2 | 67 | |
68 | ||
69 | /* | |
70 | * Loop through the interface list and free all the records... | |
71 | */ | |
72 | ||
e00b005a | 73 | for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList); |
74 | current; | |
75 | current = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
ef416fc2 | 76 | { |
e00b005a | 77 | cupsArrayRemove(NetIFList, current); |
78 | free(current); | |
ef416fc2 | 79 | } |
80 | } | |
81 | ||
82 | ||
83 | /* | |
84 | * 'cupsdNetIFUpdate()' - Update the network interface list as needed... | |
85 | */ | |
86 | ||
87 | void | |
88 | cupsdNetIFUpdate(void) | |
89 | { | |
bd7854cb | 90 | int match; /* Matching address? */ |
ef416fc2 | 91 | cupsd_listener_t *lis; /* Listen address */ |
e00b005a | 92 | cupsd_netif_t *temp; /* New interface */ |
ef416fc2 | 93 | struct ifaddrs *addrs, /* Interface address list */ |
94 | *addr; /* Current interface address */ | |
e00b005a | 95 | char hostname[1024]; /* Hostname for address */ |
e07d4801 | 96 | size_t hostlen; /* Length of hostname */ |
ef416fc2 | 97 | |
98 | ||
99 | /* | |
411affcf | 100 | * Only update the list if we need to... |
ef416fc2 | 101 | */ |
102 | ||
411affcf | 103 | if (!NetIFUpdate) |
ef416fc2 | 104 | return; |
105 | ||
411affcf | 106 | NetIFUpdate = 0; |
ef416fc2 | 107 | |
108 | /* | |
109 | * Free the old interfaces... | |
110 | */ | |
111 | ||
112 | cupsdNetIFFree(); | |
113 | ||
e00b005a | 114 | /* |
115 | * Make sure we have an array... | |
116 | */ | |
117 | ||
118 | if (!NetIFList) | |
119 | NetIFList = cupsArrayNew((cups_array_func_t)compare_netif, NULL); | |
120 | ||
121 | if (!NetIFList) | |
122 | return; | |
123 | ||
ef416fc2 | 124 | /* |
125 | * Grab a new list of interfaces... | |
126 | */ | |
127 | ||
128 | if (getifaddrs(&addrs) < 0) | |
84ec3a84 MS |
129 | { |
130 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno)); | |
ef416fc2 | 131 | return; |
84ec3a84 | 132 | } |
ef416fc2 | 133 | |
134 | for (addr = addrs; addr != NULL; addr = addr->ifa_next) | |
135 | { | |
136 | /* | |
137 | * See if this interface address is IPv4 or IPv6... | |
138 | */ | |
139 | ||
140 | if (addr->ifa_addr == NULL || | |
141 | (addr->ifa_addr->sa_family != AF_INET | |
142 | #ifdef AF_INET6 | |
143 | && addr->ifa_addr->sa_family != AF_INET6 | |
144 | #endif | |
145 | ) || | |
146 | addr->ifa_netmask == NULL || addr->ifa_name == NULL) | |
84ec3a84 MS |
147 | { |
148 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Ignoring \"%s\".", addr->ifa_name); | |
ef416fc2 | 149 | continue; |
84ec3a84 | 150 | } |
ef416fc2 | 151 | |
152 | /* | |
e00b005a | 153 | * Try looking up the hostname for the address as needed... |
ef416fc2 | 154 | */ |
155 | ||
ba55dc12 | 156 | if (HostNameLookups) |
e00b005a | 157 | httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname, |
158 | sizeof(hostname)); | |
159 | else | |
160 | { | |
161 | /* | |
162 | * Map the default server address and localhost to the server name | |
163 | * and localhost, respectively; for all other addresses, use the | |
cc754834 | 164 | * numeric address... |
e00b005a | 165 | */ |
ef416fc2 | 166 | |
e00b005a | 167 | if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr))) |
82cc1f9a | 168 | strlcpy(hostname, "localhost", sizeof(hostname)); |
e00b005a | 169 | else |
d1c13e16 MS |
170 | httpAddrString((http_addr_t *)(addr->ifa_addr), hostname, |
171 | sizeof(hostname)); | |
e00b005a | 172 | } |
ef416fc2 | 173 | |
e00b005a | 174 | /* |
175 | * Create a new address element... | |
176 | */ | |
ef416fc2 | 177 | |
e07d4801 MS |
178 | hostlen = strlen(hostname); |
179 | if ((temp = calloc(1, sizeof(cupsd_netif_t) + hostlen)) == NULL) | |
84ec3a84 MS |
180 | { |
181 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to allocate memory for interface."); | |
e00b005a | 182 | break; |
84ec3a84 | 183 | } |
ef416fc2 | 184 | |
185 | /* | |
e00b005a | 186 | * Copy all of the information... |
ef416fc2 | 187 | */ |
188 | ||
189 | strlcpy(temp->name, addr->ifa_name, sizeof(temp->name)); | |
e07d4801 | 190 | temp->hostlen = hostlen; |
82cc1f9a | 191 | memcpy(temp->hostname, hostname, hostlen + 1); |
ef416fc2 | 192 | |
193 | if (addr->ifa_addr->sa_family == AF_INET) | |
194 | { | |
195 | /* | |
196 | * Copy IPv4 addresses... | |
197 | */ | |
198 | ||
199 | memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in)); | |
200 | memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in)); | |
201 | ||
202 | if (addr->ifa_dstaddr) | |
203 | memcpy(&(temp->broadcast), addr->ifa_dstaddr, | |
204 | sizeof(struct sockaddr_in)); | |
205 | } | |
206 | #ifdef AF_INET6 | |
207 | else | |
208 | { | |
209 | /* | |
210 | * Copy IPv6 addresses... | |
211 | */ | |
212 | ||
213 | memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in6)); | |
214 | memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in6)); | |
215 | ||
216 | if (addr->ifa_dstaddr) | |
217 | memcpy(&(temp->broadcast), addr->ifa_dstaddr, | |
218 | sizeof(struct sockaddr_in6)); | |
219 | } | |
220 | #endif /* AF_INET6 */ | |
221 | ||
222 | if (!(addr->ifa_flags & IFF_POINTOPOINT) && | |
223 | !httpAddrLocalhost(&(temp->address))) | |
224 | temp->is_local = 1; | |
225 | ||
226 | /* | |
227 | * Determine which port to use when advertising printers... | |
228 | */ | |
229 | ||
bd7854cb | 230 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); |
231 | lis; | |
232 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
ef416fc2 | 233 | { |
234 | match = 0; | |
235 | ||
236 | if (httpAddrAny(&(lis->address))) | |
237 | match = 1; | |
238 | else if (addr->ifa_addr->sa_family == AF_INET && | |
239 | lis->address.addr.sa_family == AF_INET && | |
240 | (lis->address.ipv4.sin_addr.s_addr & | |
06d4e77b MS |
241 | temp->mask.ipv4.sin_addr.s_addr) == |
242 | (temp->address.ipv4.sin_addr.s_addr & | |
243 | temp->mask.ipv4.sin_addr.s_addr)) | |
ef416fc2 | 244 | match = 1; |
245 | #ifdef AF_INET6 | |
246 | else if (addr->ifa_addr->sa_family == AF_INET6 && | |
247 | lis->address.addr.sa_family == AF_INET6 && | |
248 | (lis->address.ipv6.sin6_addr.s6_addr[0] & | |
06d4e77b MS |
249 | temp->mask.ipv6.sin6_addr.s6_addr[0]) == |
250 | (temp->address.ipv6.sin6_addr.s6_addr[0] & | |
251 | temp->mask.ipv6.sin6_addr.s6_addr[0]) && | |
ef416fc2 | 252 | (lis->address.ipv6.sin6_addr.s6_addr[1] & |
06d4e77b MS |
253 | temp->mask.ipv6.sin6_addr.s6_addr[1]) == |
254 | (temp->address.ipv6.sin6_addr.s6_addr[1] & | |
255 | temp->mask.ipv6.sin6_addr.s6_addr[1]) && | |
ef416fc2 | 256 | (lis->address.ipv6.sin6_addr.s6_addr[2] & |
06d4e77b MS |
257 | temp->mask.ipv6.sin6_addr.s6_addr[2]) == |
258 | (temp->address.ipv6.sin6_addr.s6_addr[2] & | |
259 | temp->mask.ipv6.sin6_addr.s6_addr[2]) && | |
ef416fc2 | 260 | (lis->address.ipv6.sin6_addr.s6_addr[3] & |
06d4e77b MS |
261 | temp->mask.ipv6.sin6_addr.s6_addr[3]) == |
262 | (temp->address.ipv6.sin6_addr.s6_addr[3] & | |
263 | temp->mask.ipv6.sin6_addr.s6_addr[3])) | |
ef416fc2 | 264 | match = 1; |
265 | #endif /* AF_INET6 */ | |
266 | ||
267 | if (match) | |
268 | { | |
a469f8a5 | 269 | temp->port = httpAddrPort(&(lis->address)); |
ef416fc2 | 270 | break; |
271 | } | |
272 | } | |
273 | ||
274 | /* | |
e00b005a | 275 | * Add it to the array... |
ef416fc2 | 276 | */ |
277 | ||
e00b005a | 278 | cupsArrayAdd(NetIFList, temp); |
ef416fc2 | 279 | |
06d4e77b MS |
280 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d", |
281 | temp->name, temp->hostname, temp->port); | |
ef416fc2 | 282 | } |
283 | ||
284 | freeifaddrs(addrs); | |
285 | } | |
286 | ||
287 | ||
e00b005a | 288 | /* |
289 | * 'compare_netif()' - Compare two network interfaces. | |
290 | */ | |
291 | ||
292 | static int /* O - Result of comparison */ | |
293 | compare_netif(cupsd_netif_t *a, /* I - First network interface */ | |
294 | cupsd_netif_t *b) /* I - Second network interface */ | |
295 | { | |
296 | return (strcmp(a->name, b->name)); | |
297 | } | |
298 | ||
299 | ||
ef416fc2 | 300 | /* |
f2d18633 | 301 | * End of "$Id$". |
ef416fc2 | 302 | */ |