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