]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
2 * Network interface functions for the CUPS scheduler.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers.
18 #include <cups/http-private.h>
26 static void cupsdNetIFFree(void);
27 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
31 * 'cupsdNetIFFind()' - Find a network interface.
34 cupsd_netif_t
* /* O - Network interface data */
35 cupsdNetIFFind(const char *name
) /* I - Name of interface */
37 cupsd_netif_t key
; /* Search key */
41 * Update the interface list as needed...
48 * Search for the named interface...
51 strlcpy(key
.name
, name
, sizeof(key
.name
));
53 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
58 * 'cupsdNetIFFree()' - Free the current network interface list.
64 cupsd_netif_t
*current
; /* Current interface in array */
68 * Loop through the interface list and free all the records...
71 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
73 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
75 cupsArrayRemove(NetIFList
, current
);
82 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
86 cupsdNetIFUpdate(void)
88 int match
; /* Matching address? */
89 cupsd_listener_t
*lis
; /* Listen address */
90 cupsd_netif_t
*temp
; /* New interface */
91 struct ifaddrs
*addrs
, /* Interface address list */
92 *addr
; /* Current interface address */
93 char hostname
[1024]; /* Hostname for address */
94 size_t hostlen
; /* Length of hostname */
98 * Only update the list if we need to...
107 * Free the old interfaces...
113 * Make sure we have an array...
117 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
123 * Grab a new list of interfaces...
126 if (getifaddrs(&addrs
) < 0)
128 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno
));
132 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
135 * See if this interface address is IPv4 or IPv6...
138 if (addr
->ifa_addr
== NULL
||
139 (addr
->ifa_addr
->sa_family
!= AF_INET
141 && addr
->ifa_addr
->sa_family
!= AF_INET6
144 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
146 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Ignoring \"%s\".", addr
->ifa_name
);
151 * Try looking up the hostname for the address as needed...
155 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
160 * Map the default server address and localhost to the server name
161 * and localhost, respectively; for all other addresses, use the
165 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
166 strlcpy(hostname
, "localhost", sizeof(hostname
));
168 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
173 * Create a new address element...
176 hostlen
= strlen(hostname
);
177 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) + hostlen
)) == NULL
)
179 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
184 * Copy all of the information...
187 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
188 temp
->hostlen
= hostlen
;
189 memcpy(temp
->hostname
, hostname
, hostlen
+ 1);
191 if (addr
->ifa_addr
->sa_family
== AF_INET
)
194 * Copy IPv4 addresses...
197 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
198 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
200 if (addr
->ifa_dstaddr
)
201 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
202 sizeof(struct sockaddr_in
));
208 * Copy IPv6 addresses...
211 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
212 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
214 if (addr
->ifa_dstaddr
)
215 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
216 sizeof(struct sockaddr_in6
));
218 #endif /* AF_INET6 */
220 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
221 !httpAddrLocalhost(&(temp
->address
)))
225 * Determine which port to use when advertising printers...
228 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
230 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
234 if (httpAddrAny(&(lis
->address
)))
236 else if (addr
->ifa_addr
->sa_family
== AF_INET
&&
237 lis
->address
.addr
.sa_family
== AF_INET
&&
238 (lis
->address
.ipv4
.sin_addr
.s_addr
&
239 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
240 (temp
->address
.ipv4
.sin_addr
.s_addr
&
241 temp
->mask
.ipv4
.sin_addr
.s_addr
))
244 else if (addr
->ifa_addr
->sa_family
== AF_INET6
&&
245 lis
->address
.addr
.sa_family
== AF_INET6
&&
246 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
247 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) ==
248 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
249 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) &&
250 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
251 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) ==
252 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
253 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) &&
254 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
255 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) ==
256 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
257 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) &&
258 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
259 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]) ==
260 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
261 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]))
263 #endif /* AF_INET6 */
267 temp
->port
= httpAddrPort(&(lis
->address
));
273 * Add it to the array...
276 cupsArrayAdd(NetIFList
, temp
);
278 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s:%d",
279 temp
->name
, temp
->hostname
, temp
->port
);
287 * 'compare_netif()' - Compare two network interfaces.
290 static int /* O - Result of comparison */
291 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
292 cupsd_netif_t
*b
) /* I - Second network interface */
294 return (strcmp(a
->name
, b
->name
));