]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
Merge changes from CUPS 1.5svn-r9037.
[thirdparty/cups.git] / scheduler / network.c
1 /*
2 * "$Id: network.c 7861 2008-08-26 03:15:14Z mike $"
3 *
4 * Network interface functions for the Common UNIX Printing System
5 * (CUPS) scheduler.
6 *
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * "LICENSE" which should have been included with this file. If this
14 * file is missing or damaged, see the license at "http://www.cups.org/".
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...
21 * compare_netif() - Compare two network interfaces.
22 */
23
24 /*
25 * Include necessary headers.
26 */
27
28 #include <cups/http-private.h>
29 #include "cupsd.h"
30
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
40 /*
41 * 'cupsdNetIFFind()' - Find a network interface.
42 */
43
44 cupsd_netif_t * /* O - Network interface data */
45 cupsdNetIFFind(const char *name) /* I - Name of interface */
46 {
47 cupsd_netif_t key; /* Search key */
48
49
50 /*
51 * Update the interface list as needed...
52 */
53
54 if (NetIFUpdate)
55 cupsdNetIFUpdate();
56
57 /*
58 * Search for the named interface...
59 */
60
61 strlcpy(key.name, name, sizeof(key.name));
62
63 return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key));
64 }
65
66
67 /*
68 * 'cupsdNetIFFree()' - Free the current network interface list.
69 */
70
71 static void
72 cupsdNetIFFree(void)
73 {
74 cupsd_netif_t *current; /* Current interface in array */
75
76
77 /*
78 * Loop through the interface list and free all the records...
79 */
80
81 for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
82 current;
83 current = (cupsd_netif_t *)cupsArrayNext(NetIFList))
84 {
85 cupsArrayRemove(NetIFList, current);
86 free(current);
87 }
88 }
89
90
91 /*
92 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
93 */
94
95 void
96 cupsdNetIFUpdate(void)
97 {
98 int match; /* Matching address? */
99 cupsd_listener_t *lis; /* Listen address */
100 cupsd_netif_t *temp; /* New interface */
101 struct ifaddrs *addrs, /* Interface address list */
102 *addr; /* Current interface address */
103 char hostname[1024]; /* Hostname for address */
104 size_t hostlen; /* Length of hostname */
105
106
107 /*
108 * Only update the list if we need to...
109 */
110
111 if (!NetIFUpdate)
112 return;
113
114 NetIFUpdate = 0;
115
116 /*
117 * Free the old interfaces...
118 */
119
120 cupsdNetIFFree();
121
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
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 /*
142 * See if this interface address is IPv4 or IPv6...
143 */
144
145 if (addr->ifa_addr == NULL ||
146 (addr->ifa_addr->sa_family != AF_INET
147 #ifdef AF_INET6
148 && addr->ifa_addr->sa_family != AF_INET6
149 #endif
150 ) ||
151 addr->ifa_netmask == NULL || addr->ifa_name == NULL)
152 continue;
153
154 /*
155 * Try looking up the hostname for the address as needed...
156 */
157
158 #ifdef __APPLE__
159 if (HostNameLookups)
160 #else
161 if (HostNameLookups || RemotePort)
162 #endif /* __APPLE__ */
163 httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname,
164 sizeof(hostname));
165 else
166 {
167 /*
168 * Map the default server address and localhost to the server name
169 * and localhost, respectively; for all other addresses, use the
170 * dotted notation...
171 */
172
173 if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr)))
174 strcpy(hostname, "localhost");
175 else
176 httpAddrString((http_addr_t *)(addr->ifa_addr), hostname,
177 sizeof(hostname));
178 }
179
180 /*
181 * Create a new address element...
182 */
183
184 hostlen = strlen(hostname);
185 if ((temp = calloc(1, sizeof(cupsd_netif_t) + hostlen)) == NULL)
186 break;
187
188 /*
189 * Copy all of the information...
190 */
191
192 strlcpy(temp->name, addr->ifa_name, sizeof(temp->name));
193 temp->hostlen = hostlen;
194 strcpy(temp->hostname, hostname); /* Safe because hostname is allocated */
195
196 if (addr->ifa_addr->sa_family == AF_INET)
197 {
198 /*
199 * Copy IPv4 addresses...
200 */
201
202 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in));
203 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in));
204
205 if (addr->ifa_dstaddr)
206 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
207 sizeof(struct sockaddr_in));
208 }
209 #ifdef AF_INET6
210 else
211 {
212 /*
213 * Copy IPv6 addresses...
214 */
215
216 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in6));
217 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in6));
218
219 if (addr->ifa_dstaddr)
220 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
221 sizeof(struct sockaddr_in6));
222 }
223 #endif /* AF_INET6 */
224
225 if (!(addr->ifa_flags & IFF_POINTOPOINT) &&
226 !httpAddrLocalhost(&(temp->address)))
227 temp->is_local = 1;
228
229 /*
230 * Determine which port to use when advertising printers...
231 */
232
233 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
234 lis;
235 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
236 {
237 match = 0;
238
239 if (httpAddrAny(&(lis->address)))
240 match = 1;
241 else if (addr->ifa_addr->sa_family == AF_INET &&
242 lis->address.addr.sa_family == AF_INET &&
243 (lis->address.ipv4.sin_addr.s_addr &
244 temp->mask.ipv4.sin_addr.s_addr) ==
245 (temp->address.ipv4.sin_addr.s_addr &
246 temp->mask.ipv4.sin_addr.s_addr))
247 match = 1;
248 #ifdef AF_INET6
249 else if (addr->ifa_addr->sa_family == AF_INET6 &&
250 lis->address.addr.sa_family == AF_INET6 &&
251 (lis->address.ipv6.sin6_addr.s6_addr[0] &
252 temp->mask.ipv6.sin6_addr.s6_addr[0]) ==
253 (temp->address.ipv6.sin6_addr.s6_addr[0] &
254 temp->mask.ipv6.sin6_addr.s6_addr[0]) &&
255 (lis->address.ipv6.sin6_addr.s6_addr[1] &
256 temp->mask.ipv6.sin6_addr.s6_addr[1]) ==
257 (temp->address.ipv6.sin6_addr.s6_addr[1] &
258 temp->mask.ipv6.sin6_addr.s6_addr[1]) &&
259 (lis->address.ipv6.sin6_addr.s6_addr[2] &
260 temp->mask.ipv6.sin6_addr.s6_addr[2]) ==
261 (temp->address.ipv6.sin6_addr.s6_addr[2] &
262 temp->mask.ipv6.sin6_addr.s6_addr[2]) &&
263 (lis->address.ipv6.sin6_addr.s6_addr[3] &
264 temp->mask.ipv6.sin6_addr.s6_addr[3]) ==
265 (temp->address.ipv6.sin6_addr.s6_addr[3] &
266 temp->mask.ipv6.sin6_addr.s6_addr[3]))
267 match = 1;
268 #endif /* AF_INET6 */
269
270 if (match)
271 {
272 if (lis->address.addr.sa_family == AF_INET)
273 temp->port = ntohs(lis->address.ipv4.sin_port);
274 #ifdef AF_INET6
275 else if (lis->address.addr.sa_family == AF_INET6)
276 temp->port = ntohs(lis->address.ipv6.sin6_port);
277 #endif /* AF_INET6 */
278 break;
279 }
280 }
281
282 /*
283 * Add it to the array...
284 */
285
286 cupsArrayAdd(NetIFList, temp);
287
288 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d",
289 temp->name, temp->hostname, temp->port);
290 }
291
292 freeifaddrs(addrs);
293 }
294
295
296 /*
297 * 'compare_netif()' - Compare two network interfaces.
298 */
299
300 static int /* O - Result of comparison */
301 compare_netif(cupsd_netif_t *a, /* I - First network interface */
302 cupsd_netif_t *b) /* I - Second network interface */
303 {
304 return (strcmp(a->name, b->name));
305 }
306
307
308 /*
309 * End of "$Id: network.c 7861 2008-08-26 03:15:14Z mike $".
310 */