]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/network.c
Do some code reorganization so that all of the PPD code is separate from the rest.
[thirdparty/cups.git] / scheduler / network.c
1 /*
2 * "$Id$"
3 *
4 * Network interface functions for the CUPS scheduler.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
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/".
14 */
15
16 /*
17 * Include necessary headers.
18 */
19
20 #include <cups/http-private.h>
21 #include "cupsd.h"
22
23
24 /*
25 * Local functions...
26 */
27
28 static void cupsdNetIFFree(void);
29 static int compare_netif(cupsd_netif_t *a, cupsd_netif_t *b);
30
31
32 /*
33 * 'cupsdNetIFFind()' - Find a network interface.
34 */
35
36 cupsd_netif_t * /* O - Network interface data */
37 cupsdNetIFFind(const char *name) /* I - Name of interface */
38 {
39 cupsd_netif_t key; /* Search key */
40
41
42 /*
43 * Update the interface list as needed...
44 */
45
46 if (NetIFUpdate)
47 cupsdNetIFUpdate();
48
49 /*
50 * Search for the named interface...
51 */
52
53 strlcpy(key.name, name, sizeof(key.name));
54
55 return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key));
56 }
57
58
59 /*
60 * 'cupsdNetIFFree()' - Free the current network interface list.
61 */
62
63 static void
64 cupsdNetIFFree(void)
65 {
66 cupsd_netif_t *current; /* Current interface in array */
67
68
69 /*
70 * Loop through the interface list and free all the records...
71 */
72
73 for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
74 current;
75 current = (cupsd_netif_t *)cupsArrayNext(NetIFList))
76 {
77 cupsArrayRemove(NetIFList, current);
78 free(current);
79 }
80 }
81
82
83 /*
84 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
85 */
86
87 void
88 cupsdNetIFUpdate(void)
89 {
90 int match; /* Matching address? */
91 cupsd_listener_t *lis; /* Listen address */
92 cupsd_netif_t *temp; /* New interface */
93 struct ifaddrs *addrs, /* Interface address list */
94 *addr; /* Current interface address */
95 char hostname[1024]; /* Hostname for address */
96 size_t hostlen; /* Length of hostname */
97
98
99 /*
100 * Only update the list if we need to...
101 */
102
103 if (!NetIFUpdate)
104 return;
105
106 NetIFUpdate = 0;
107
108 /*
109 * Free the old interfaces...
110 */
111
112 cupsdNetIFFree();
113
114 /*
115 * Make sure we have an array...
116 */
117
118 if (!NetIFList)
119 NetIFList = cupsArrayNew((cups_array_func_t)compare_netif, NULL);
120
121 if (!NetIFList)
122 return;
123
124 /*
125 * Grab a new list of interfaces...
126 */
127
128 if (getifaddrs(&addrs) < 0)
129 {
130 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to get interface list - %s", strerror(errno));
131 return;
132 }
133
134 for (addr = addrs; addr != NULL; addr = addr->ifa_next)
135 {
136 /*
137 * See if this interface address is IPv4 or IPv6...
138 */
139
140 if (addr->ifa_addr == NULL ||
141 (addr->ifa_addr->sa_family != AF_INET
142 #ifdef AF_INET6
143 && addr->ifa_addr->sa_family != AF_INET6
144 #endif
145 ) ||
146 addr->ifa_netmask == NULL || addr->ifa_name == NULL)
147 {
148 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Ignoring \"%s\".", addr->ifa_name);
149 continue;
150 }
151
152 /*
153 * Try looking up the hostname for the address as needed...
154 */
155
156 if (HostNameLookups)
157 httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname,
158 sizeof(hostname));
159 else
160 {
161 /*
162 * Map the default server address and localhost to the server name
163 * and localhost, respectively; for all other addresses, use the
164 * numeric address...
165 */
166
167 if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr)))
168 strlcpy(hostname, "localhost", sizeof(hostname));
169 else
170 httpAddrString((http_addr_t *)(addr->ifa_addr), hostname,
171 sizeof(hostname));
172 }
173
174 /*
175 * Create a new address element...
176 */
177
178 hostlen = strlen(hostname);
179 if ((temp = calloc(1, sizeof(cupsd_netif_t) + hostlen)) == NULL)
180 {
181 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: Unable to allocate memory for interface.");
182 break;
183 }
184
185 /*
186 * Copy all of the information...
187 */
188
189 strlcpy(temp->name, addr->ifa_name, sizeof(temp->name));
190 temp->hostlen = hostlen;
191 memcpy(temp->hostname, hostname, hostlen + 1);
192
193 if (addr->ifa_addr->sa_family == AF_INET)
194 {
195 /*
196 * Copy IPv4 addresses...
197 */
198
199 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in));
200 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in));
201
202 if (addr->ifa_dstaddr)
203 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
204 sizeof(struct sockaddr_in));
205 }
206 #ifdef AF_INET6
207 else
208 {
209 /*
210 * Copy IPv6 addresses...
211 */
212
213 memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in6));
214 memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in6));
215
216 if (addr->ifa_dstaddr)
217 memcpy(&(temp->broadcast), addr->ifa_dstaddr,
218 sizeof(struct sockaddr_in6));
219 }
220 #endif /* AF_INET6 */
221
222 if (!(addr->ifa_flags & IFF_POINTOPOINT) &&
223 !httpAddrLocalhost(&(temp->address)))
224 temp->is_local = 1;
225
226 /*
227 * Determine which port to use when advertising printers...
228 */
229
230 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
231 lis;
232 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
233 {
234 match = 0;
235
236 if (httpAddrAny(&(lis->address)))
237 match = 1;
238 else if (addr->ifa_addr->sa_family == AF_INET &&
239 lis->address.addr.sa_family == AF_INET &&
240 (lis->address.ipv4.sin_addr.s_addr &
241 temp->mask.ipv4.sin_addr.s_addr) ==
242 (temp->address.ipv4.sin_addr.s_addr &
243 temp->mask.ipv4.sin_addr.s_addr))
244 match = 1;
245 #ifdef AF_INET6
246 else if (addr->ifa_addr->sa_family == AF_INET6 &&
247 lis->address.addr.sa_family == AF_INET6 &&
248 (lis->address.ipv6.sin6_addr.s6_addr[0] &
249 temp->mask.ipv6.sin6_addr.s6_addr[0]) ==
250 (temp->address.ipv6.sin6_addr.s6_addr[0] &
251 temp->mask.ipv6.sin6_addr.s6_addr[0]) &&
252 (lis->address.ipv6.sin6_addr.s6_addr[1] &
253 temp->mask.ipv6.sin6_addr.s6_addr[1]) ==
254 (temp->address.ipv6.sin6_addr.s6_addr[1] &
255 temp->mask.ipv6.sin6_addr.s6_addr[1]) &&
256 (lis->address.ipv6.sin6_addr.s6_addr[2] &
257 temp->mask.ipv6.sin6_addr.s6_addr[2]) ==
258 (temp->address.ipv6.sin6_addr.s6_addr[2] &
259 temp->mask.ipv6.sin6_addr.s6_addr[2]) &&
260 (lis->address.ipv6.sin6_addr.s6_addr[3] &
261 temp->mask.ipv6.sin6_addr.s6_addr[3]) ==
262 (temp->address.ipv6.sin6_addr.s6_addr[3] &
263 temp->mask.ipv6.sin6_addr.s6_addr[3]))
264 match = 1;
265 #endif /* AF_INET6 */
266
267 if (match)
268 {
269 temp->port = httpAddrPort(&(lis->address));
270 break;
271 }
272 }
273
274 /*
275 * Add it to the array...
276 */
277
278 cupsArrayAdd(NetIFList, temp);
279
280 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d",
281 temp->name, temp->hostname, temp->port);
282 }
283
284 freeifaddrs(addrs);
285 }
286
287
288 /*
289 * 'compare_netif()' - Compare two network interfaces.
290 */
291
292 static int /* O - Result of comparison */
293 compare_netif(cupsd_netif_t *a, /* I - First network interface */
294 cupsd_netif_t *b) /* I - Second network interface */
295 {
296 return (strcmp(a->name, b->name));
297 }
298
299
300 /*
301 * End of "$Id$".
302 */