]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/network.c
Import CUPS v1.7.1
[thirdparty/cups.git] / scheduler / network.c
CommitLineData
ef416fc2 1/*
61515785 2 * "$Id: network.c 11500 2014-01-06 22:21:15Z msweet $"
ef416fc2 3 *
39ff2fe7 4 * Network interface functions for the CUPS scheduler.
ef416fc2 5 *
82cc1f9a 6 * Copyright 2007-2012 by Apple Inc.
e00b005a 7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
ef416fc2 12 * "LICENSE" which should have been included with this file. If this
bc44d920 13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 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...
e00b005a 20 * compare_netif() - Compare two network interfaces.
ef416fc2 21 */
22
23/*
24 * Include necessary headers.
25 */
26
89d46774 27#include <cups/http-private.h>
ef416fc2 28#include "cupsd.h"
29
e00b005a 30
31/*
32 * Local functions...
33 */
34
35static void cupsdNetIFFree(void);
36static int compare_netif(cupsd_netif_t *a, cupsd_netif_t *b);
37
38
ef416fc2 39/*
40 * 'cupsdNetIFFind()' - Find a network interface.
41 */
42
43cupsd_netif_t * /* O - Network interface data */
44cupsdNetIFFind(const char *name) /* I - Name of interface */
45{
e00b005a 46 cupsd_netif_t key; /* Search key */
ef416fc2 47
48
49 /*
50 * Update the interface list as needed...
51 */
52
411affcf 53 if (NetIFUpdate)
54 cupsdNetIFUpdate();
ef416fc2 55
56 /*
57 * Search for the named interface...
58 */
59
e00b005a 60 strlcpy(key.name, name, sizeof(key.name));
ef416fc2 61
e00b005a 62 return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key));
ef416fc2 63}
64
65
66/*
67 * 'cupsdNetIFFree()' - Free the current network interface list.
68 */
69
e00b005a 70static void
ef416fc2 71cupsdNetIFFree(void)
72{
e00b005a 73 cupsd_netif_t *current; /* Current interface in array */
ef416fc2 74
75
76 /*
77 * Loop through the interface list and free all the records...
78 */
79
e00b005a 80 for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
81 current;
82 current = (cupsd_netif_t *)cupsArrayNext(NetIFList))
ef416fc2 83 {
e00b005a 84 cupsArrayRemove(NetIFList, current);
85 free(current);
ef416fc2 86 }
87}
88
89
90/*
91 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
92 */
93
94void
95cupsdNetIFUpdate(void)
96{
bd7854cb 97 int match; /* Matching address? */
ef416fc2 98 cupsd_listener_t *lis; /* Listen address */
e00b005a 99 cupsd_netif_t *temp; /* New interface */
ef416fc2 100 struct ifaddrs *addrs, /* Interface address list */
101 *addr; /* Current interface address */
e00b005a 102 char hostname[1024]; /* Hostname for address */
e07d4801 103 size_t hostlen; /* Length of hostname */
ef416fc2 104
105
106 /*
411affcf 107 * Only update the list if we need to...
ef416fc2 108 */
109
411affcf 110 if (!NetIFUpdate)
ef416fc2 111 return;
112
411affcf 113 NetIFUpdate = 0;
ef416fc2 114
115 /*
116 * Free the old interfaces...
117 */
118
119 cupsdNetIFFree();
120
e00b005a 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
ef416fc2 131 /*
132 * Grab a new list of interfaces...
133 */
134
135 if (getifaddrs(&addrs) < 0)
61515785
MS
136 {
137 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno));
ef416fc2 138 return;
61515785 139 }
ef416fc2 140
141 for (addr = addrs; addr != NULL; addr = addr->ifa_next)
142 {
143 /*
144 * See if this interface address is IPv4 or IPv6...
145 */
146
147 if (addr->ifa_addr == NULL ||
148 (addr->ifa_addr->sa_family != AF_INET
149#ifdef AF_INET6
150 && addr->ifa_addr->sa_family != AF_INET6
151#endif
152 ) ||
153 addr->ifa_netmask == NULL || addr->ifa_name == NULL)
61515785
MS
154 {
155 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Ignoring \"%s\".", addr->ifa_name);
ef416fc2 156 continue;
61515785 157 }
ef416fc2 158
159 /*
e00b005a 160 * Try looking up the hostname for the address as needed...
ef416fc2 161 */
162
ba55dc12 163 if (HostNameLookups)
e00b005a 164 httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname,
165 sizeof(hostname));
166 else
167 {
168 /*
169 * Map the default server address and localhost to the server name
170 * and localhost, respectively; for all other addresses, use the
cc754834 171 * numeric address...
e00b005a 172 */
ef416fc2 173
e00b005a 174 if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr)))
82cc1f9a 175 strlcpy(hostname, "localhost", sizeof(hostname));
e00b005a 176 else
d1c13e16
MS
177 httpAddrString((http_addr_t *)(addr->ifa_addr), hostname,
178 sizeof(hostname));
e00b005a 179 }
ef416fc2 180
e00b005a 181 /*
182 * Create a new address element...
183 */
ef416fc2 184
e07d4801
MS
185 hostlen = strlen(hostname);
186 if ((temp = calloc(1, sizeof(cupsd_netif_t) + hostlen)) == NULL)
61515785
MS
187 {
188 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
e00b005a 189 break;
61515785 190 }
ef416fc2 191
192 /*
e00b005a 193 * Copy all of the information...
ef416fc2 194 */
195
196 strlcpy(temp->name, addr->ifa_name, sizeof(temp->name));
e07d4801 197 temp->hostlen = hostlen;
82cc1f9a 198 memcpy(temp->hostname, hostname, hostlen + 1);
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 {
a469f8a5 276 temp->port = httpAddrPort(&(lis->address));
ef416fc2 277 break;
278 }
279 }
280
281 /*
e00b005a 282 * Add it to the array...
ef416fc2 283 */
284
e00b005a 285 cupsArrayAdd(NetIFList, temp);
ef416fc2 286
06d4e77b
MS
287 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d",
288 temp->name, temp->hostname, temp->port);
ef416fc2 289 }
290
291 freeifaddrs(addrs);
292}
293
294
e00b005a 295/*
296 * 'compare_netif()' - Compare two network interfaces.
297 */
298
299static int /* O - Result of comparison */
300compare_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
ef416fc2 307/*
61515785 308 * End of "$Id: network.c 11500 2014-01-06 22:21:15Z msweet $".
ef416fc2 309 */