]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
2 * "$Id: network.c 7861 2008-08-26 03:15:14Z mike $"
4 * Network interface functions for the Common UNIX Printing System
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * "LICENSE" which should have been included with this file. If this
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * cupsdNetIFFind() - Find a network interface.
19 * cupsdNetIFFree() - Free the current network interface list.
20 * cupsdNetIFUpdate() - Update the network interface list as needed...
21 * compare_netif() - Compare two network interfaces.
25 * Include necessary headers.
28 #include <cups/http-private.h>
36 static void cupsdNetIFFree(void);
37 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
41 * 'cupsdNetIFFind()' - Find a network interface.
44 cupsd_netif_t
* /* O - Network interface data */
45 cupsdNetIFFind(const char *name
) /* I - Name of interface */
47 cupsd_netif_t key
; /* Search key */
51 * Update the interface list as needed...
58 * Search for the named interface...
61 strlcpy(key
.name
, name
, sizeof(key
.name
));
63 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
68 * 'cupsdNetIFFree()' - Free the current network interface list.
74 cupsd_netif_t
*current
; /* Current interface in array */
78 * Loop through the interface list and free all the records...
81 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
83 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
85 cupsArrayRemove(NetIFList
, current
);
92 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
96 cupsdNetIFUpdate(void)
98 int match
; /* Matching address? */
99 cupsd_listener_t
*lis
; /* Listen address */
100 cupsd_netif_t
*temp
; /* New interface */
101 struct ifaddrs
*addrs
, /* Interface address list */
102 *addr
; /* Current interface address */
103 char hostname
[1024]; /* Hostname for address */
104 size_t hostlen
; /* Length of hostname */
108 * Only update the list if we need to...
117 * Free the old interfaces...
123 * Make sure we have an array...
127 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
133 * Grab a new list of interfaces...
136 if (getifaddrs(&addrs
) < 0)
139 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
142 * See if this interface address is IPv4 or IPv6...
145 if (addr
->ifa_addr
== NULL
||
146 (addr
->ifa_addr
->sa_family
!= AF_INET
148 && addr
->ifa_addr
->sa_family
!= AF_INET6
151 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
155 * Try looking up the hostname for the address as needed...
158 if (HostNameLookups
|| RemotePort
)
159 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
164 * Map the default server address and localhost to the server name
165 * and localhost, respectively; for all other addresses, use the
169 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
170 strcpy(hostname
, "localhost");
172 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
177 * Create a new address element...
180 hostlen
= strlen(hostname
);
181 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) + hostlen
)) == NULL
)
185 * Copy all of the information...
188 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
189 temp
->hostlen
= hostlen
;
190 strcpy(temp
->hostname
, hostname
); /* Safe because hostname is allocated */
192 if (addr
->ifa_addr
->sa_family
== AF_INET
)
195 * Copy IPv4 addresses...
198 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
199 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
201 if (addr
->ifa_dstaddr
)
202 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
203 sizeof(struct sockaddr_in
));
209 * Copy IPv6 addresses...
212 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
213 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
215 if (addr
->ifa_dstaddr
)
216 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
217 sizeof(struct sockaddr_in6
));
219 #endif /* AF_INET6 */
221 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
222 !httpAddrLocalhost(&(temp
->address
)))
226 * Determine which port to use when advertising printers...
229 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
231 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
235 if (httpAddrAny(&(lis
->address
)))
237 else if (addr
->ifa_addr
->sa_family
== AF_INET
&&
238 lis
->address
.addr
.sa_family
== AF_INET
&&
239 (lis
->address
.ipv4
.sin_addr
.s_addr
&
240 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
241 (temp
->address
.ipv4
.sin_addr
.s_addr
&
242 temp
->mask
.ipv4
.sin_addr
.s_addr
))
245 else if (addr
->ifa_addr
->sa_family
== AF_INET6
&&
246 lis
->address
.addr
.sa_family
== AF_INET6
&&
247 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
248 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) ==
249 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
250 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) &&
251 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
252 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) ==
253 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
254 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) &&
255 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
256 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) ==
257 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
258 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) &&
259 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
260 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]) ==
261 (temp
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
262 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]))
264 #endif /* AF_INET6 */
268 if (lis
->address
.addr
.sa_family
== AF_INET
)
269 temp
->port
= ntohs(lis
->address
.ipv4
.sin_port
);
271 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
272 temp
->port
= ntohs(lis
->address
.ipv6
.sin6_port
);
273 #endif /* AF_INET6 */
279 * Add it to the array...
282 cupsArrayAdd(NetIFList
, temp
);
284 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s:%d",
285 temp
->name
, temp
->hostname
, temp
->port
);
293 * 'compare_netif()' - Compare two network interfaces.
296 static int /* O - Result of comparison */
297 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
298 cupsd_netif_t
*b
) /* I - Second network interface */
300 return (strcmp(a
->name
, b
->name
));
305 * End of "$Id: network.c 7861 2008-08-26 03:15:14Z mike $".