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