]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
4 * Network interface functions for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
17 * Include necessary headers.
20 #include <cups/http-private.h>
28 static void cupsdNetIFFree(void);
29 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
33 * 'cupsdNetIFFind()' - Find a network interface.
36 cupsd_netif_t
* /* O - Network interface data */
37 cupsdNetIFFind(const char *name
) /* I - Name of interface */
39 cupsd_netif_t key
; /* Search key */
43 * Update the interface list as needed...
50 * Search for the named interface...
53 strlcpy(key
.name
, name
, sizeof(key
.name
));
55 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
60 * 'cupsdNetIFFree()' - Free the current network interface list.
66 cupsd_netif_t
*current
; /* Current interface in array */
70 * Loop through the interface list and free all the records...
73 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
75 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
77 cupsArrayRemove(NetIFList
, current
);
84 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
88 cupsdNetIFUpdate(void)
90 int match
; /* Matching address? */
91 cupsd_listener_t
*lis
; /* Listen address */
92 cupsd_netif_t
*temp
; /* New interface */
93 struct ifaddrs
*addrs
, /* Interface address list */
94 *addr
; /* Current interface address */
95 char hostname
[1024]; /* Hostname for address */
96 size_t hostlen
; /* Length of hostname */
100 * Only update the list if we need to...
109 * Free the old interfaces...
115 * Make sure we have an array...
119 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
125 * Grab a new list of interfaces...
128 if (getifaddrs(&addrs
) < 0)
130 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno
));
134 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
137 * See if this interface address is IPv4 or IPv6...
140 if (addr
->ifa_addr
== NULL
||
141 (addr
->ifa_addr
->sa_family
!= AF_INET
143 && addr
->ifa_addr
->sa_family
!= AF_INET6
146 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
148 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Ignoring \"%s\".", addr
->ifa_name
);
153 * Try looking up the hostname for the address as needed...
157 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
162 * Map the default server address and localhost to the server name
163 * and localhost, respectively; for all other addresses, use the
167 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
168 strlcpy(hostname
, "localhost", sizeof(hostname
));
170 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
175 * Create a new address element...
178 hostlen
= strlen(hostname
);
179 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) + hostlen
)) == NULL
)
181 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
186 * Copy all of the information...
189 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
190 temp
->hostlen
= hostlen
;
191 memcpy(temp
->hostname
, hostname
, hostlen
+ 1);
193 if (addr
->ifa_addr
->sa_family
== AF_INET
)
196 * Copy IPv4 addresses...
199 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
200 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
202 if (addr
->ifa_dstaddr
)
203 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
204 sizeof(struct sockaddr_in
));
210 * Copy IPv6 addresses...
213 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
214 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
216 if (addr
->ifa_dstaddr
)
217 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
218 sizeof(struct sockaddr_in6
));
220 #endif /* AF_INET6 */
222 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
223 !httpAddrLocalhost(&(temp
->address
)))
227 * Determine which port to use when advertising printers...
230 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
232 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
236 if (httpAddrAny(&(lis
->address
)))
238 else if (addr
->ifa_addr
->sa_family
== AF_INET
&&
239 lis
->address
.addr
.sa_family
== AF_INET
&&
240 (lis
->address
.ipv4
.sin_addr
.s_addr
&
241 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
242 (temp
->address
.ipv4
.sin_addr
.s_addr
&
243 temp
->mask
.ipv4
.sin_addr
.s_addr
))
246 else if (addr
->ifa_addr
->sa_family
== AF_INET6
&&
247 lis
->address
.addr
.sa_family
== AF_INET6
&&
248 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
249 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) ==
250 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
251 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) &&
252 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
253 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) ==
254 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
255 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) &&
256 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
257 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) ==
258 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
259 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) &&
260 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
261 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]) ==
262 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
263 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]))
265 #endif /* AF_INET6 */
269 temp
->port
= httpAddrPort(&(lis
->address
));
275 * Add it to the array...
278 cupsArrayAdd(NetIFList
, temp
);
280 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s:%d",
281 temp
->name
, temp
->hostname
, temp
->port
);
289 * 'compare_netif()' - Compare two network interfaces.
292 static int /* O - Result of comparison */
293 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
294 cupsd_netif_t
*b
) /* I - Second network interface */
296 return (strcmp(a
->name
, b
->name
));