]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/network.c
Merge trademark/logo changes from CUPS 1.4svn-r8418.
[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 *
d1c13e16 7 * Copyright 2007-2009 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 */
e00b005a 103 char hostname[1024]; /* Hostname for address */
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)
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 /*
e00b005a 154 * Try looking up the hostname for the address as needed...
ef416fc2 155 */
156
d1c13e16 157 if (HostNameLookups || RemoteAccessEnabled)
e00b005a 158 httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname,
159 sizeof(hostname));
160 else
161 {
162 /*
163 * Map the default server address and localhost to the server name
164 * and localhost, respectively; for all other addresses, use the
165 * dotted notation...
166 */
ef416fc2 167
e00b005a 168 if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr)))
169 strcpy(hostname, "localhost");
170 else
d1c13e16
MS
171 httpAddrString((http_addr_t *)(addr->ifa_addr), hostname,
172 sizeof(hostname));
e00b005a 173 }
ef416fc2 174
e00b005a 175 /*
176 * Create a new address element...
177 */
ef416fc2 178
e00b005a 179 if ((temp = calloc(1, sizeof(cupsd_netif_t) +
180 strlen(hostname))) == NULL)
181 break;
ef416fc2 182
183 /*
e00b005a 184 * Copy all of the information...
ef416fc2 185 */
186
187 strlcpy(temp->name, addr->ifa_name, sizeof(temp->name));
e00b005a 188 strcpy(temp->hostname, hostname); /* Safe because hostname is allocated */
ef416fc2 189
190 if (addr->ifa_addr->sa_family == AF_INET)
191 {
192 /*
193 * Copy IPv4 addresses...
194 */
195
196 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in));
197 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in));
198
199 if (addr->ifa_dstaddr)
200 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
201 sizeof(struct sockaddr_in));
202 }
203#ifdef AF_INET6
204 else
205 {
206 /*
207 * Copy IPv6 addresses...
208 */
209
210 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in6));
211 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in6));
212
213 if (addr->ifa_dstaddr)
214 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
215 sizeof(struct sockaddr_in6));
216 }
217#endif /* AF_INET6 */
218
219 if (!(addr->ifa_flags & IFF_POINTOPOINT) &&
220 !httpAddrLocalhost(&(temp->address)))
221 temp->is_local = 1;
222
223 /*
224 * Determine which port to use when advertising printers...
225 */
226
bd7854cb 227 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
228 lis;
229 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
ef416fc2 230 {
231 match = 0;
232
233 if (httpAddrAny(&(lis->address)))
234 match = 1;
235 else if (addr->ifa_addr->sa_family == AF_INET &&
236 lis->address.addr.sa_family == AF_INET &&
237 (lis->address.ipv4.sin_addr.s_addr &
06d4e77b
MS
238 temp->mask.ipv4.sin_addr.s_addr) ==
239 (temp->address.ipv4.sin_addr.s_addr &
240 temp->mask.ipv4.sin_addr.s_addr))
ef416fc2 241 match = 1;
242#ifdef AF_INET6
243 else if (addr->ifa_addr->sa_family == AF_INET6 &&
244 lis->address.addr.sa_family == AF_INET6 &&
245 (lis->address.ipv6.sin6_addr.s6_addr[0] &
06d4e77b
MS
246 temp->mask.ipv6.sin6_addr.s6_addr[0]) ==
247 (temp->address.ipv6.sin6_addr.s6_addr[0] &
248 temp->mask.ipv6.sin6_addr.s6_addr[0]) &&
ef416fc2 249 (lis->address.ipv6.sin6_addr.s6_addr[1] &
06d4e77b
MS
250 temp->mask.ipv6.sin6_addr.s6_addr[1]) ==
251 (temp->address.ipv6.sin6_addr.s6_addr[1] &
252 temp->mask.ipv6.sin6_addr.s6_addr[1]) &&
ef416fc2 253 (lis->address.ipv6.sin6_addr.s6_addr[2] &
06d4e77b
MS
254 temp->mask.ipv6.sin6_addr.s6_addr[2]) ==
255 (temp->address.ipv6.sin6_addr.s6_addr[2] &
256 temp->mask.ipv6.sin6_addr.s6_addr[2]) &&
ef416fc2 257 (lis->address.ipv6.sin6_addr.s6_addr[3] &
06d4e77b
MS
258 temp->mask.ipv6.sin6_addr.s6_addr[3]) ==
259 (temp->address.ipv6.sin6_addr.s6_addr[3] &
260 temp->mask.ipv6.sin6_addr.s6_addr[3]))
ef416fc2 261 match = 1;
262#endif /* AF_INET6 */
263
264 if (match)
265 {
266 if (lis->address.addr.sa_family == AF_INET)
267 temp->port = ntohs(lis->address.ipv4.sin_port);
268#ifdef AF_INET6
269 else if (lis->address.addr.sa_family == AF_INET6)
270 temp->port = ntohs(lis->address.ipv6.sin6_port);
271#endif /* AF_INET6 */
272 break;
273 }
274 }
275
276 /*
e00b005a 277 * Add it to the array...
ef416fc2 278 */
279
e00b005a 280 cupsArrayAdd(NetIFList, temp);
ef416fc2 281
06d4e77b
MS
282 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d",
283 temp->name, temp->hostname, temp->port);
ef416fc2 284 }
285
286 freeifaddrs(addrs);
287}
288
289
e00b005a 290/*
291 * 'compare_netif()' - Compare two network interfaces.
292 */
293
294static int /* O - Result of comparison */
295compare_netif(cupsd_netif_t *a, /* I - First network interface */
296 cupsd_netif_t *b) /* I - Second network interface */
297{
298 return (strcmp(a->name, b->name));
299}
300
301
ef416fc2 302/*
b19ccc9e 303 * End of "$Id: network.c 7861 2008-08-26 03:15:14Z mike $".
ef416fc2 304 */