]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
2 * Network interface functions for the CUPS scheduler.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include <cups/http-private.h>
17 #include <cups/getifaddrs-internal.h>
24 static void cupsdNetIFFree(void);
25 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
29 * 'cupsdNetIFFind()' - Find a network interface.
32 cupsd_netif_t
* /* O - Network interface data */
33 cupsdNetIFFind(const char *name
) /* I - Name of interface */
35 cupsd_netif_t key
; /* Search key */
39 * Update the interface list as needed...
46 * Search for the named interface...
49 strlcpy(key
.name
, name
, sizeof(key
.name
));
51 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
56 * 'cupsdNetIFFree()' - Free the current network interface list.
62 cupsd_netif_t
*current
; /* Current interface in array */
66 * Loop through the interface list and free all the records...
69 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
71 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
73 cupsArrayRemove(NetIFList
, current
);
80 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
84 cupsdNetIFUpdate(void)
86 int match
; /* Matching address? */
87 cupsd_listener_t
*lis
; /* Listen address */
88 cupsd_netif_t
*temp
; /* New interface */
89 struct ifaddrs
*addrs
, /* Interface address list */
90 *addr
; /* Current interface address */
91 char hostname
[1024]; /* Hostname for address */
92 size_t hostlen
; /* Length of hostname */
96 * Only update the list if we need to...
105 * Free the old interfaces...
111 * Make sure we have an array...
115 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
121 * Grab a new list of interfaces...
124 if (getifaddrs(&addrs
) < 0)
126 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno
));
130 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
133 * See if this interface address is IPv4 or IPv6...
136 if (addr
->ifa_addr
== NULL
||
137 (addr
->ifa_addr
->sa_family
!= AF_INET
139 && addr
->ifa_addr
->sa_family
!= AF_INET6
142 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
144 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Ignoring \"%s\".", addr
->ifa_name
);
149 * Try looking up the hostname for the address as needed...
153 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
158 * Map the default server address and localhost to the server name
159 * and localhost, respectively; for all other addresses, use the
163 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
164 strlcpy(hostname
, "localhost", sizeof(hostname
));
166 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
171 * Create a new address element...
174 hostlen
= strlen(hostname
);
175 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) + hostlen
)) == NULL
)
177 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
182 * Copy all of the information...
185 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
186 temp
->hostlen
= hostlen
;
187 memcpy(temp
->hostname
, hostname
, hostlen
+ 1);
189 if (addr
->ifa_addr
->sa_family
== AF_INET
)
192 * Copy IPv4 addresses...
195 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
196 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
198 if (addr
->ifa_dstaddr
)
199 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
200 sizeof(struct sockaddr_in
));
206 * Copy IPv6 addresses...
209 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
210 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
212 if (addr
->ifa_dstaddr
)
213 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
214 sizeof(struct sockaddr_in6
));
216 #endif /* AF_INET6 */
218 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
219 !httpAddrLocalhost(&(temp
->address
)))
223 * Determine which port to use when advertising printers...
226 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
228 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
232 if (httpAddrAny(&(lis
->address
)))
234 else if (addr
->ifa_addr
->sa_family
== AF_INET
&&
235 lis
->address
.addr
.sa_family
== AF_INET
&&
236 (lis
->address
.ipv4
.sin_addr
.s_addr
&
237 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
238 (temp
->address
.ipv4
.sin_addr
.s_addr
&
239 temp
->mask
.ipv4
.sin_addr
.s_addr
))
242 else if (addr
->ifa_addr
->sa_family
== AF_INET6
&&
243 lis
->address
.addr
.sa_family
== AF_INET6
&&
244 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
245 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) ==
246 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
247 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) &&
248 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
249 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) ==
250 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
251 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) &&
252 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
253 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) ==
254 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
255 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) &&
256 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
257 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]) ==
258 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
259 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]))
261 #endif /* AF_INET6 */
265 temp
->port
= httpAddrPort(&(lis
->address
));
271 * Add it to the array...
274 cupsArrayAdd(NetIFList
, temp
);
276 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s:%d",
277 temp
->name
, temp
->hostname
, temp
->port
);
285 * 'compare_netif()' - Compare two network interfaces.
288 static int /* O - Result of comparison */
289 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
290 cupsd_netif_t
*b
) /* I - Second network interface */
292 return (strcmp(a
->name
, b
->name
));