]>
Commit | Line | Data |
---|---|---|
2123c23a | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
2123c23a | 3 | * |
4 | * Network interface functions for the Common UNIX Printing System | |
5 | * (CUPS) scheduler. | |
6 | * | |
7f1e45d9 | 7 | * Copyright 2007-2009 by Apple Inc. |
71fe79e8 | 8 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
2123c23a | 9 | * |
10 | * These coded instructions, statements, and computer programs are the | |
4e8d321f | 11 | * property of Apple Inc. and are protected by Federal copyright |
12 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
2123c23a | 13 | * "LICENSE" which should have been included with this file. If this |
4e8d321f | 14 | * file is missing or damaged, see the license at "http://www.cups.org/". |
f3e786fc | 15 | * |
16 | * Contents: | |
17 | * | |
18 | * cupsdNetIFFind() - Find a network interface. | |
19 | * cupsdNetIFFree() - Free the current network interface list. | |
20 | * cupsdNetIFUpdate() - Update the network interface list as needed... | |
71fe79e8 | 21 | * compare_netif() - Compare two network interfaces. |
2123c23a | 22 | */ |
23 | ||
24 | /* | |
25 | * Include necessary headers. | |
26 | */ | |
27 | ||
846f45a0 | 28 | #include <cups/http-private.h> |
2123c23a | 29 | #include "cupsd.h" |
30 | ||
71fe79e8 | 31 | |
32 | /* | |
33 | * Local functions... | |
34 | */ | |
35 | ||
36 | static void cupsdNetIFFree(void); | |
37 | static int compare_netif(cupsd_netif_t *a, cupsd_netif_t *b); | |
38 | ||
39 | ||
2123c23a | 40 | /* |
f3e786fc | 41 | * 'cupsdNetIFFind()' - Find a network interface. |
2123c23a | 42 | */ |
43 | ||
f3e786fc | 44 | cupsd_netif_t * /* O - Network interface data */ |
45 | cupsdNetIFFind(const char *name) /* I - Name of interface */ | |
2123c23a | 46 | { |
71fe79e8 | 47 | cupsd_netif_t key; /* Search key */ |
2123c23a | 48 | |
49 | ||
50 | /* | |
51 | * Update the interface list as needed... | |
52 | */ | |
53 | ||
87c85a78 | 54 | if (NetIFUpdate) |
55 | cupsdNetIFUpdate(); | |
2123c23a | 56 | |
57 | /* | |
58 | * Search for the named interface... | |
59 | */ | |
60 | ||
71fe79e8 | 61 | strlcpy(key.name, name, sizeof(key.name)); |
2123c23a | 62 | |
71fe79e8 | 63 | return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key)); |
2123c23a | 64 | } |
65 | ||
66 | ||
67 | /* | |
589eb420 | 68 | * 'cupsdNetIFFree()' - Free the current network interface list. |
2123c23a | 69 | */ |
70 | ||
71fe79e8 | 71 | static void |
589eb420 | 72 | cupsdNetIFFree(void) |
2123c23a | 73 | { |
71fe79e8 | 74 | cupsd_netif_t *current; /* Current interface in array */ |
2123c23a | 75 | |
76 | ||
77 | /* | |
78 | * Loop through the interface list and free all the records... | |
79 | */ | |
80 | ||
71fe79e8 | 81 | for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList); |
82 | current; | |
83 | current = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
2123c23a | 84 | { |
71fe79e8 | 85 | cupsArrayRemove(NetIFList, current); |
86 | free(current); | |
2123c23a | 87 | } |
88 | } | |
89 | ||
90 | ||
91 | /* | |
589eb420 | 92 | * 'cupsdNetIFUpdate()' - Update the network interface list as needed... |
2123c23a | 93 | */ |
94 | ||
95 | void | |
589eb420 | 96 | cupsdNetIFUpdate(void) |
2123c23a | 97 | { |
c7445b9f | 98 | int match; /* Matching address? */ |
589eb420 | 99 | cupsd_listener_t *lis; /* Listen address */ |
71fe79e8 | 100 | cupsd_netif_t *temp; /* New interface */ |
f3e786fc | 101 | struct ifaddrs *addrs, /* Interface address list */ |
102 | *addr; /* Current interface address */ | |
086c584d | 103 | http_addrlist_t *saddr; /* Current server address */ |
71fe79e8 | 104 | char hostname[1024]; /* Hostname for address */ |
2123c23a | 105 | |
106 | ||
107 | /* | |
87c85a78 | 108 | * Only update the list if we need to... |
2123c23a | 109 | */ |
110 | ||
87c85a78 | 111 | if (!NetIFUpdate) |
2123c23a | 112 | return; |
113 | ||
87c85a78 | 114 | NetIFUpdate = 0; |
5fe35f41 | 115 | |
2123c23a | 116 | /* |
117 | * Free the old interfaces... | |
118 | */ | |
119 | ||
589eb420 | 120 | cupsdNetIFFree(); |
2123c23a | 121 | |
71fe79e8 | 122 | /* |
123 | * Make sure we have an array... | |
124 | */ | |
125 | ||
126 | if (!NetIFList) | |
127 | NetIFList = cupsArrayNew((cups_array_func_t)compare_netif, NULL); | |
128 | ||
129 | if (!NetIFList) | |
130 | return; | |
131 | ||
2123c23a | 132 | /* |
133 | * Grab a new list of interfaces... | |
134 | */ | |
135 | ||
136 | if (getifaddrs(&addrs) < 0) | |
137 | return; | |
138 | ||
139 | for (addr = addrs; addr != NULL; addr = addr->ifa_next) | |
140 | { | |
141 | /* | |
20264b99 | 142 | * See if this interface address is IPv4 or IPv6... |
2123c23a | 143 | */ |
144 | ||
20264b99 | 145 | if (addr->ifa_addr == NULL || |
a09840c5 | 146 | (addr->ifa_addr->sa_family != AF_INET |
147 | #ifdef AF_INET6 | |
148 | && addr->ifa_addr->sa_family != AF_INET6 | |
149 | #endif | |
150 | ) || | |
2123c23a | 151 | addr->ifa_netmask == NULL || addr->ifa_name == NULL) |
152 | continue; | |
153 | ||
154 | /* | |
71fe79e8 | 155 | * Try looking up the hostname for the address as needed... |
2123c23a | 156 | */ |
157 | ||
71fe79e8 | 158 | if (HostNameLookups) |
159 | httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname, | |
160 | sizeof(hostname)); | |
161 | else | |
162 | { | |
163 | /* | |
164 | * Map the default server address and localhost to the server name | |
165 | * and localhost, respectively; for all other addresses, use the | |
166 | * dotted notation... | |
167 | */ | |
2123c23a | 168 | |
71fe79e8 | 169 | if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr))) |
170 | strcpy(hostname, "localhost"); | |
171 | else | |
7f1e45d9 | 172 | httpAddrString((http_addr_t *)(addr->ifa_addr), hostname, |
173 | sizeof(hostname)); | |
71fe79e8 | 174 | } |
7776142f | 175 | |
71fe79e8 | 176 | /* |
177 | * Create a new address element... | |
178 | */ | |
7776142f | 179 | |
71fe79e8 | 180 | if ((temp = calloc(1, sizeof(cupsd_netif_t) + |
181 | strlen(hostname))) == NULL) | |
182 | break; | |
2123c23a | 183 | |
184 | /* | |
71fe79e8 | 185 | * Copy all of the information... |
2123c23a | 186 | */ |
187 | ||
def978d5 | 188 | strlcpy(temp->name, addr->ifa_name, sizeof(temp->name)); |
71fe79e8 | 189 | strcpy(temp->hostname, hostname); /* Safe because hostname is allocated */ |
2123c23a | 190 | |
20264b99 | 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) | |
f3e786fc | 201 | memcpy(&(temp->broadcast), addr->ifa_dstaddr, |
202 | sizeof(struct sockaddr_in)); | |
20264b99 | 203 | } |
a09840c5 | 204 | #ifdef AF_INET6 |
20264b99 | 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) | |
f3e786fc | 215 | memcpy(&(temp->broadcast), addr->ifa_dstaddr, |
216 | sizeof(struct sockaddr_in6)); | |
20264b99 | 217 | } |
a09840c5 | 218 | #endif /* AF_INET6 */ |
2123c23a | 219 | |
d7a9de63 | 220 | if (!(addr->ifa_flags & IFF_POINTOPOINT) && |
221 | !httpAddrLocalhost(&(temp->address))) | |
2123c23a | 222 | temp->is_local = 1; |
223 | ||
edd6ee99 | 224 | /* |
225 | * Determine which port to use when advertising printers... | |
226 | */ | |
227 | ||
c7445b9f | 228 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); |
229 | lis; | |
230 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
edd6ee99 | 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 & | |
24bf49a5 | 239 | temp->mask.ipv4.sin_addr.s_addr) == |
240 | (temp->address.ipv4.sin_addr.s_addr & | |
241 | temp->mask.ipv4.sin_addr.s_addr)) | |
edd6ee99 | 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] & | |
24bf49a5 | 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]) && | |
edd6ee99 | 250 | (lis->address.ipv6.sin6_addr.s6_addr[1] & |
24bf49a5 | 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]) && | |
edd6ee99 | 254 | (lis->address.ipv6.sin6_addr.s6_addr[2] & |
24bf49a5 | 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]) && | |
edd6ee99 | 258 | (lis->address.ipv6.sin6_addr.s6_addr[3] & |
24bf49a5 | 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])) | |
edd6ee99 | 262 | match = 1; |
263 | #endif /* AF_INET6 */ | |
264 | ||
265 | if (match) | |
266 | { | |
267 | if (lis->address.addr.sa_family == AF_INET) | |
268 | temp->port = ntohs(lis->address.ipv4.sin_port); | |
269 | #ifdef AF_INET6 | |
270 | else if (lis->address.addr.sa_family == AF_INET6) | |
271 | temp->port = ntohs(lis->address.ipv6.sin6_port); | |
272 | #endif /* AF_INET6 */ | |
273 | break; | |
274 | } | |
275 | } | |
276 | ||
2123c23a | 277 | /* |
71fe79e8 | 278 | * Add it to the array... |
2123c23a | 279 | */ |
280 | ||
71fe79e8 | 281 | cupsArrayAdd(NetIFList, temp); |
40e67cda | 282 | |
24bf49a5 | 283 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d", |
284 | temp->name, temp->hostname, temp->port); | |
2123c23a | 285 | } |
286 | ||
287 | freeifaddrs(addrs); | |
288 | } | |
289 | ||
290 | ||
71fe79e8 | 291 | /* |
292 | * 'compare_netif()' - Compare two network interfaces. | |
293 | */ | |
294 | ||
295 | static int /* O - Result of comparison */ | |
296 | compare_netif(cupsd_netif_t *a, /* I - First network interface */ | |
297 | cupsd_netif_t *b) /* I - Second network interface */ | |
298 | { | |
299 | return (strcmp(a->name, b->name)); | |
300 | } | |
301 | ||
302 | ||
2123c23a | 303 | /* |
c9d3f842 | 304 | * End of "$Id$". |
2123c23a | 305 | */ |