File: network.c

package info (click to toggle)
cups 1.4.4-7+squeeze10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 25,548 kB
  • ctags: 9,583
  • sloc: ansic: 138,214; sh: 37,926; cpp: 25,469; makefile: 3,285; perl: 190; python: 127; php: 28
file content (309 lines) | stat: -rw-r--r-- 7,692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * "$Id: network.c 9098 2010-04-09 22:42:09Z mike $"
 *
 *   Network interface functions for the CUPS scheduler.
 *
 *   Copyright 2007-2010 by Apple Inc.
 *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   "LICENSE" which should have been included with this file.  If this
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 * Contents:
 *
 *   cupsdNetIFFind()   - Find a network interface.
 *   cupsdNetIFFree()   - Free the current network interface list.
 *   cupsdNetIFUpdate() - Update the network interface list as needed...
 *   compare_netif()    - Compare two network interfaces.
 */

/*
 * Include necessary headers.
 */

#include <cups/http-private.h>
#include "cupsd.h"


/*
 * Local functions...
 */

static void	cupsdNetIFFree(void);
static int	compare_netif(cupsd_netif_t *a, cupsd_netif_t *b);


/*
 * 'cupsdNetIFFind()' - Find a network interface.
 */

cupsd_netif_t *				/* O - Network interface data */
cupsdNetIFFind(const char *name)	/* I - Name of interface */
{
  cupsd_netif_t	key;			/* Search key */


 /*
  * Update the interface list as needed...
  */

  if (NetIFUpdate)
    cupsdNetIFUpdate();

 /*
  * Search for the named interface...
  */

  strlcpy(key.name, name, sizeof(key.name));

  return ((cupsd_netif_t *)cupsArrayFind(NetIFList, &key));
}


/*
 * 'cupsdNetIFFree()' - Free the current network interface list.
 */

static void
cupsdNetIFFree(void)
{
  cupsd_netif_t	*current;		/* Current interface in array */


 /*
  * Loop through the interface list and free all the records...
  */

  for (current = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
       current;
       current = (cupsd_netif_t *)cupsArrayNext(NetIFList))
  {
    cupsArrayRemove(NetIFList, current);
    free(current);
  }
}


/*
 * 'cupsdNetIFUpdate()' - Update the network interface list as needed...
 */

void
cupsdNetIFUpdate(void)
{
  int			match;		/* Matching address? */
  cupsd_listener_t	*lis;		/* Listen address */
  cupsd_netif_t		*temp;		/* New interface */
  struct ifaddrs	*addrs,		/* Interface address list */
			*addr;		/* Current interface address */
  char			hostname[1024];	/* Hostname for address */
  size_t		hostlen;	/* Length of hostname */


 /*
  * Only update the list if we need to...
  */

  if (!NetIFUpdate)
    return;

  NetIFUpdate = 0;

 /*
  * Free the old interfaces...
  */

  cupsdNetIFFree();

 /*
  * Make sure we have an array...
  */

  if (!NetIFList)
    NetIFList = cupsArrayNew((cups_array_func_t)compare_netif, NULL);

  if (!NetIFList)
    return;

 /*
  * Grab a new list of interfaces...
  */

  if (getifaddrs(&addrs) < 0)
    return;

  for (addr = addrs; addr != NULL; addr = addr->ifa_next)
  {
   /*
    * See if this interface address is IPv4 or IPv6...
    */

    if (addr->ifa_addr == NULL ||
        (addr->ifa_addr->sa_family != AF_INET
#ifdef AF_INET6
	 && addr->ifa_addr->sa_family != AF_INET6
#endif
	) ||
        addr->ifa_netmask == NULL || addr->ifa_name == NULL)
      continue;

   /*
    * Try looking up the hostname for the address as needed...
    */

#ifdef __APPLE__
    if (HostNameLookups)
#else
    if (HostNameLookups || RemotePort)
#endif /* __APPLE__ */
      httpAddrLookup((http_addr_t *)(addr->ifa_addr), hostname,
                     sizeof(hostname));
    else
    {
     /*
      * Map the default server address and localhost to the server name
      * and localhost, respectively; for all other addresses, use the
      * dotted notation...
      */

      if (httpAddrLocalhost((http_addr_t *)(addr->ifa_addr)))
        strcpy(hostname, "localhost");
      else
	httpAddrString((http_addr_t *)(addr->ifa_addr), hostname,
		       sizeof(hostname));
    }

   /*
    * Create a new address element...
    */

    hostlen = strlen(hostname);
    if ((temp = calloc(1, sizeof(cupsd_netif_t) + hostlen)) == NULL)
      break;

   /*
    * Copy all of the information...
    */

    strlcpy(temp->name, addr->ifa_name, sizeof(temp->name));
    temp->hostlen = hostlen;
    strcpy(temp->hostname, hostname);	/* Safe because hostname is allocated */

    if (addr->ifa_addr->sa_family == AF_INET)
    {
     /*
      * Copy IPv4 addresses...
      */

      memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in));
      memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in));

      if (addr->ifa_dstaddr)
	memcpy(&(temp->broadcast), addr->ifa_dstaddr,
	       sizeof(struct sockaddr_in));
    }
#ifdef AF_INET6
    else
    {
     /*
      * Copy IPv6 addresses...
      */

      memcpy(&(temp->address), addr->ifa_addr, sizeof(struct sockaddr_in6));
      memcpy(&(temp->mask), addr->ifa_netmask, sizeof(struct sockaddr_in6));

      if (addr->ifa_dstaddr)
	memcpy(&(temp->broadcast), addr->ifa_dstaddr,
	       sizeof(struct sockaddr_in6));
    }
#endif /* AF_INET6 */

    if (!(addr->ifa_flags & IFF_POINTOPOINT) &&
        !httpAddrLocalhost(&(temp->address)))
      temp->is_local = 1;

   /*
    * Determine which port to use when advertising printers...
    */

    for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
         lis;
	 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
    {
      match = 0;

      if (httpAddrAny(&(lis->address)))
        match = 1;
      else if (addr->ifa_addr->sa_family == AF_INET &&
               lis->address.addr.sa_family == AF_INET &&
               (lis->address.ipv4.sin_addr.s_addr &
	        temp->mask.ipv4.sin_addr.s_addr) ==
	           (temp->address.ipv4.sin_addr.s_addr &
		    temp->mask.ipv4.sin_addr.s_addr))
        match = 1;
#ifdef AF_INET6
      else if (addr->ifa_addr->sa_family == AF_INET6 &&
               lis->address.addr.sa_family == AF_INET6 &&
               (lis->address.ipv6.sin6_addr.s6_addr[0] &
	        temp->mask.ipv6.sin6_addr.s6_addr[0]) ==
		   (temp->address.ipv6.sin6_addr.s6_addr[0] &
		    temp->mask.ipv6.sin6_addr.s6_addr[0]) &&
               (lis->address.ipv6.sin6_addr.s6_addr[1] &
	        temp->mask.ipv6.sin6_addr.s6_addr[1]) ==
		   (temp->address.ipv6.sin6_addr.s6_addr[1] &
		    temp->mask.ipv6.sin6_addr.s6_addr[1]) &&
               (lis->address.ipv6.sin6_addr.s6_addr[2] &
	        temp->mask.ipv6.sin6_addr.s6_addr[2]) ==
		   (temp->address.ipv6.sin6_addr.s6_addr[2] &
		    temp->mask.ipv6.sin6_addr.s6_addr[2]) &&
               (lis->address.ipv6.sin6_addr.s6_addr[3] &
	        temp->mask.ipv6.sin6_addr.s6_addr[3]) ==
		   (temp->address.ipv6.sin6_addr.s6_addr[3] &
		    temp->mask.ipv6.sin6_addr.s6_addr[3]))
        match = 1;
#endif /* AF_INET6 */

      if (match)
      {
        if (lis->address.addr.sa_family == AF_INET)
          temp->port = ntohs(lis->address.ipv4.sin_port);
#ifdef AF_INET6
        else if (lis->address.addr.sa_family == AF_INET6)
          temp->port = ntohs(lis->address.ipv6.sin6_port);
#endif /* AF_INET6 */
	break;
      }
    }

   /*
    * Add it to the array...
    */

    cupsArrayAdd(NetIFList, temp);

    cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdNetIFUpdate: \"%s\" = %s:%d",
                    temp->name, temp->hostname, temp->port);
  }

  freeifaddrs(addrs);
}


/*
 * 'compare_netif()' - Compare two network interfaces.
 */

static int				/* O - Result of comparison */
compare_netif(cupsd_netif_t *a,		/* I - First network interface */
              cupsd_netif_t *b)		/* I - Second network interface */
{
  return (strcmp(a->name, b->name));
}


/*
 * End of "$Id: network.c 9098 2010-04-09 22:42:09Z mike $".
 */