]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
2 * "$Id: network.c 6086 2006-11-14 15:43:55Z mike $"
4 * Network interface functions for the Common UNIX Printing System
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
27 * cupsdNetIFFind() - Find a network interface.
28 * cupsdNetIFFree() - Free the current network interface list.
29 * cupsdNetIFUpdate() - Update the network interface list as needed...
30 * compare_netif() - Compare two network interfaces.
34 * Include necessary headers.
37 #include <cups/http-private.h>
45 static void cupsdNetIFFree(void);
46 static int compare_netif(cupsd_netif_t
*a
, cupsd_netif_t
*b
);
50 * 'cupsdNetIFFind()' - Find a network interface.
53 cupsd_netif_t
* /* O - Network interface data */
54 cupsdNetIFFind(const char *name
) /* I - Name of interface */
56 cupsd_netif_t key
; /* Search key */
60 * Update the interface list as needed...
67 * Search for the named interface...
70 strlcpy(key
.name
, name
, sizeof(key
.name
));
72 return ((cupsd_netif_t
*)cupsArrayFind(NetIFList
, &key
));
77 * 'cupsdNetIFFree()' - Free the current network interface list.
83 cupsd_netif_t
*current
; /* Current interface in array */
87 * Loop through the interface list and free all the records...
90 for (current
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
92 current
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
94 cupsArrayRemove(NetIFList
, current
);
101 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
105 cupsdNetIFUpdate(void)
107 int match
; /* Matching address? */
108 cupsd_listener_t
*lis
; /* Listen address */
109 cupsd_netif_t
*temp
; /* New interface */
110 struct ifaddrs
*addrs
, /* Interface address list */
111 *addr
; /* Current interface address */
112 http_addrlist_t
*saddr
; /* Current server address */
113 char hostname
[1024]; /* Hostname for address */
117 * Only update the list if we need to...
126 * Free the old interfaces...
132 * Make sure we have an array...
136 NetIFList
= cupsArrayNew((cups_array_func_t
)compare_netif
, NULL
);
142 * Grab a new list of interfaces...
145 if (getifaddrs(&addrs
) < 0)
148 for (addr
= addrs
; addr
!= NULL
; addr
= addr
->ifa_next
)
151 * See if this interface address is IPv4 or IPv6...
154 if (addr
->ifa_addr
== NULL
||
155 (addr
->ifa_addr
->sa_family
!= AF_INET
157 && addr
->ifa_addr
->sa_family
!= AF_INET6
160 addr
->ifa_netmask
== NULL
|| addr
->ifa_name
== NULL
)
164 * Try looking up the hostname for the address as needed...
168 httpAddrLookup((http_addr_t
*)(addr
->ifa_addr
), hostname
,
173 * Map the default server address and localhost to the server name
174 * and localhost, respectively; for all other addresses, use the
178 if (httpAddrLocalhost((http_addr_t
*)(addr
->ifa_addr
)))
179 strcpy(hostname
, "localhost");
182 for (saddr
= ServerAddrs
; saddr
; saddr
= saddr
->next
)
183 if (httpAddrEqual((http_addr_t
*)(addr
->ifa_addr
), &(saddr
->addr
)))
187 strlcpy(hostname
, ServerName
, sizeof(hostname
));
189 httpAddrString((http_addr_t
*)(addr
->ifa_addr
), hostname
,
195 * Create a new address element...
198 if ((temp
= calloc(1, sizeof(cupsd_netif_t
) +
199 strlen(hostname
))) == NULL
)
203 * Copy all of the information...
206 strlcpy(temp
->name
, addr
->ifa_name
, sizeof(temp
->name
));
207 strcpy(temp
->hostname
, hostname
); /* Safe because hostname is allocated */
209 if (addr
->ifa_addr
->sa_family
== AF_INET
)
212 * Copy IPv4 addresses...
215 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in
));
216 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in
));
218 if (addr
->ifa_dstaddr
)
219 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
220 sizeof(struct sockaddr_in
));
226 * Copy IPv6 addresses...
229 memcpy(&(temp
->address
), addr
->ifa_addr
, sizeof(struct sockaddr_in6
));
230 memcpy(&(temp
->mask
), addr
->ifa_netmask
, sizeof(struct sockaddr_in6
));
232 if (addr
->ifa_dstaddr
)
233 memcpy(&(temp
->broadcast
), addr
->ifa_dstaddr
,
234 sizeof(struct sockaddr_in6
));
236 #endif /* AF_INET6 */
238 if (!(addr
->ifa_flags
& IFF_POINTOPOINT
) &&
239 !httpAddrLocalhost(&(temp
->address
)))
243 * Determine which port to use when advertising printers...
246 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
248 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
252 if (httpAddrAny(&(lis
->address
)))
254 else if (addr
->ifa_addr
->sa_family
== AF_INET
&&
255 lis
->address
.addr
.sa_family
== AF_INET
&&
256 (lis
->address
.ipv4
.sin_addr
.s_addr
&
257 temp
->mask
.ipv4
.sin_addr
.s_addr
) ==
258 temp
->address
.ipv4
.sin_addr
.s_addr
)
261 else if (addr
->ifa_addr
->sa_family
== AF_INET6
&&
262 lis
->address
.addr
.sa_family
== AF_INET6
&&
263 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[0] &
264 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[0]) ==
265 temp
->address
.ipv6
.sin6_addr
.s6_addr
[0] &&
266 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[1] &
267 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[1]) ==
268 temp
->address
.ipv6
.sin6_addr
.s6_addr
[1] &&
269 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[2] &
270 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[2]) ==
271 temp
->address
.ipv6
.sin6_addr
.s6_addr
[2] &&
272 (lis
->address
.ipv6
.sin6_addr
.s6_addr
[3] &
273 temp
->mask
.ipv6
.sin6_addr
.s6_addr
[3]) ==
274 temp
->address
.ipv6
.sin6_addr
.s6_addr
[3])
276 #endif /* AF_INET6 */
280 if (lis
->address
.addr
.sa_family
== AF_INET
)
281 temp
->port
= ntohs(lis
->address
.ipv4
.sin_port
);
283 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
284 temp
->port
= ntohs(lis
->address
.ipv6
.sin6_port
);
285 #endif /* AF_INET6 */
291 * Add it to the array...
294 cupsArrayAdd(NetIFList
, temp
);
296 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdNetIFUpdate: \"%s\" = %s...",
297 temp
->name
, temp
->hostname
);
305 * 'compare_netif()' - Compare two network interfaces.
308 static int /* O - Result of comparison */
309 compare_netif(cupsd_netif_t
*a
, /* I - First network interface */
310 cupsd_netif_t
*b
) /* I - Second network interface */
312 return (strcmp(a
->name
, b
->name
));
317 * End of "$Id: network.c 6086 2006-11-14 15:43:55Z mike $".