]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
2 * "$Id: network.c 11500 2014-01-06 22:21:15Z msweet $"
4 * Network interface functions for the CUPS scheduler.
6 * Copyright 2007-2012 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 * cupsdNetIFFind() - Find a network interface.
18 * cupsdNetIFFree() - Free the current network interface list.
19 * cupsdNetIFUpdate() - Update the network interface list as needed...
20 * compare_netif() - Compare two network interfaces.
24 * Include necessary headers.
27 #include <cups/http-private.h>
35 static void cupsdNetIFFree(void);
36 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
40 * 'cupsdNetIFFind()' - Find a network interface.
43 cupsd_netif_t
* /* O - Network interface data */
44 cupsdNetIFFind(const char *name
) /* I - Name of interface */
46 cupsd_netif_t key
; /* Search key */
50 * Update the interface list as needed...
57 * Search for the named interface...
60 strlcpy(key
.name
, name
, sizeof(key
.name
));
62 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
67 * 'cupsdNetIFFree()' - Free the current network interface list.
73 cupsd_netif_t
*current
; /* Current interface in array */
77 * Loop through the interface list and free all the records...
80 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
82 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
84 cupsArrayRemove(NetIFList
, current
);
91 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
95 cupsdNetIFUpdate(void)
97 int match
; /* Matching address? */
98 cupsd_listener_t
*lis
; /* Listen address */
99 cupsd_netif_t
*temp
; /* New interface */
100 struct ifaddrs
*addrs
, /* Interface address list */
101 *addr
; /* Current interface address */
102 char hostname
[1024]; /* Hostname for address */
103 size_t hostlen
; /* Length of hostname */
107 * Only update the list if we need to...
116 * Free the old interfaces...
122 * Make sure we have an array...
126 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
132 * Grab a new list of interfaces...
135 if (getifaddrs(&addrs
) < 0)
137 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno
));
141 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
144 * See if this interface address is IPv4 or IPv6...
147 if (addr
->ifa_addr
== NULL
||
148 (addr
->ifa_addr
->sa_family
!= AF_INET
150 && addr
->ifa_addr
->sa_family
!= AF_INET6
153 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
155 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Ignoring \"%s\".", addr
->ifa_name
);
160 * Try looking up the hostname for the address as needed...
164 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
169 * Map the default server address and localhost to the server name
170 * and localhost, respectively; for all other addresses, use the
174 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
175 strlcpy(hostname
, "localhost", sizeof(hostname
));
177 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
182 * Create a new address element...
185 hostlen
= strlen(hostname
);
186 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) + hostlen
)) == NULL
)
188 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
193 * Copy all of the information...
196 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
197 temp
->hostlen
= hostlen
;
198 memcpy(temp
->hostname
, hostname
, hostlen
+ 1);
200 if (addr
->ifa_addr
->sa_family
== AF_INET
)
203 * Copy IPv4 addresses...
206 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
207 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
209 if (addr
->ifa_dstaddr
)
210 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
211 sizeof(struct sockaddr_in
));
217 * Copy IPv6 addresses...
220 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
221 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
223 if (addr
->ifa_dstaddr
)
224 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
225 sizeof(struct sockaddr_in6
));
227 #endif /* AF_INET6 */
229 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
230 !httpAddrLocalhost(&(temp
->address
)))
234 * Determine which port to use when advertising printers...
237 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
239 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
243 if (httpAddrAny(&(lis
->address
)))
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
&
248 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
249 (temp
->address
.ipv4
.sin_addr
.s_addr
&
250 temp
->mask
.ipv4
.sin_addr
.s_addr
))
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] &
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]) &&
259 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
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]) &&
263 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
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]) &&
267 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
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]))
272 #endif /* AF_INET6 */
276 temp
->port
= httpAddrPort(&(lis
->address
));
282 * Add it to the array...
285 cupsArrayAdd(NetIFList
, temp
);
287 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s:%d",
288 temp
->name
, temp
->hostname
, temp
->port
);
296 * 'compare_netif()' - Compare two network interfaces.
299 static int /* O - Result of comparison */
300 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
301 cupsd_netif_t
*b
) /* I - Second network interface */
303 return (strcmp(a
->name
, b
->name
));
308 * End of "$Id: network.c 11500 2014-01-06 22:21:15Z msweet $".