]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/getifaddrs.c
4 * Network interface functions for CUPS.
6 * Copyright 2007-2010 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 * _cups_getifaddrs() - Get a list of network interfaces on the system.
18 * _cups_freeifaddrs() - Free an interface list...
22 * Include necessary headers.
25 #include "http-private.h"
28 #ifndef HAVE_GETIFADDRS
30 * '_cups_getifaddrs()' - Get a list of network interfaces on the system.
33 int /* O - 0 on success, -1 on error */
34 _cups_getifaddrs(struct ifaddrs
**addrs
)/* O - List of interfaces */
36 int sock
; /* Socket */
37 char buffer
[65536], /* Buffer for address info */
38 *bufptr
, /* Pointer into buffer */
39 *bufend
; /* End of buffer */
40 struct ifconf conf
; /* Interface configurations */
41 struct sockaddr addr
; /* Address data */
42 struct ifreq
*ifp
; /* Interface data */
43 int ifpsize
; /* Size of interface data */
44 struct ifaddrs
*temp
; /* Pointer to current interface */
45 struct ifreq request
; /* Interface request */
49 * Start with an empty list...
58 * Create a UDP socket to get the interface data...
61 memset (&addr
, 0, sizeof(addr
));
62 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
66 * Try to get the list of interfaces...
69 conf
.ifc_len
= sizeof(buffer
);
70 conf
.ifc_buf
= buffer
;
72 if (ioctl(sock
, SIOCGIFCONF
, &conf
) < 0)
75 * Couldn't get the list of interfaces...
83 * OK, got the list of interfaces, now lets step through the
84 * buffer to pull them out...
87 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
88 # define sockaddr_len(a) ((a)->sa_len)
90 # define sockaddr_len(a) (sizeof(struct sockaddr))
91 # endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
93 for (bufptr
= buffer
, bufend
= buffer
+ conf
.ifc_len
;
98 * Get the current interface information...
101 ifp
= (struct ifreq
*)bufptr
;
102 ifpsize
= sizeof(ifp
->ifr_name
) + sockaddr_len(&(ifp
->ifr_addr
));
104 if (ifpsize
< sizeof(struct ifreq
))
105 ifpsize
= sizeof(struct ifreq
);
107 memset(&request
, 0, sizeof(request
));
108 memcpy(request
.ifr_name
, ifp
->ifr_name
, sizeof(ifp
->ifr_name
));
111 * Check the status of the interface...
114 if (ioctl(sock
, SIOCGIFFLAGS
, &request
) < 0)
118 * Allocate memory for a single interface record...
121 if ((temp
= calloc(1, sizeof(struct ifaddrs
))) == NULL
)
124 * Unable to allocate memory...
132 * Add this record to the front of the list and copy the name, flags,
133 * and network address...
136 temp
->ifa_next
= *addrs
;
138 temp
->ifa_name
= strdup(ifp
->ifr_name
);
139 temp
->ifa_flags
= request
.ifr_flags
;
140 if ((temp
->ifa_addr
= calloc(1, sockaddr_len(&(ifp
->ifr_addr
)))) != NULL
)
141 memcpy(temp
->ifa_addr
, &(ifp
->ifr_addr
), sockaddr_len(&(ifp
->ifr_addr
)));
144 * Try to get the netmask for the interface...
147 if (!ioctl(sock
, SIOCGIFNETMASK
, &request
))
150 * Got it, make a copy...
153 if ((temp
->ifa_netmask
= calloc(1, sizeof(request
.ifr_netmask
))) != NULL
)
154 memcpy(temp
->ifa_netmask
, &(request
.ifr_netmask
),
155 sizeof(request
.ifr_netmask
));
159 * Then get the broadcast or point-to-point (destination) address,
163 if (temp
->ifa_flags
& IFF_BROADCAST
)
166 * Have a broadcast address, so get it!
169 if (!ioctl(sock
, SIOCGIFBRDADDR
, &request
))
172 * Got it, make a copy...
175 if ((temp
->ifa_broadaddr
=
176 calloc(1, sizeof(request
.ifr_broadaddr
))) != NULL
)
177 memcpy(temp
->ifa_broadaddr
, &(request
.ifr_broadaddr
),
178 sizeof(request
.ifr_broadaddr
));
181 else if (temp
->ifa_flags
& IFF_POINTOPOINT
)
184 * Point-to-point interface; grab the remote address...
187 if (!ioctl(sock
, SIOCGIFDSTADDR
, &request
))
189 temp
->ifa_dstaddr
= malloc(sizeof(request
.ifr_dstaddr
));
190 memcpy(temp
->ifa_dstaddr
, &(request
.ifr_dstaddr
),
191 sizeof(request
.ifr_dstaddr
));
197 * OK, we're done with the socket, close it and return 0...
207 * '_cups_freeifaddrs()' - Free an interface list...
211 _cups_freeifaddrs(struct ifaddrs
*addrs
)/* I - Interface list to free */
213 struct ifaddrs
*next
; /* Next interface in list */
216 while (addrs
!= NULL
)
219 * Make a copy of the next interface pointer...
222 next
= addrs
->ifa_next
;
225 * Free data values as needed...
230 free(addrs
->ifa_name
);
231 addrs
->ifa_name
= NULL
;
236 free(addrs
->ifa_addr
);
237 addrs
->ifa_addr
= NULL
;
240 if (addrs
->ifa_netmask
)
242 free(addrs
->ifa_netmask
);
243 addrs
->ifa_netmask
= NULL
;
246 if (addrs
->ifa_dstaddr
)
248 free(addrs
->ifa_dstaddr
);
249 addrs
->ifa_dstaddr
= NULL
;
253 * Free this node and continue to the next...
261 #endif /* !HAVE_GETIFADDRS */