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