]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/getifaddrs.c
2 * Network interface functions for CUPS.
4 * Copyright 2007-2010 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers.
18 #include "http-private.h"
21 #ifndef HAVE_GETIFADDRS
23 * '_cups_getifaddrs()' - Get a list of network interfaces on the system.
26 int /* O - 0 on success, -1 on error */
27 _cups_getifaddrs(struct ifaddrs
**addrs
)/* O - List of interfaces */
29 int sock
; /* Socket */
30 char buffer
[65536], /* Buffer for address info */
31 *bufptr
, /* Pointer into buffer */
32 *bufend
; /* End of buffer */
33 struct ifconf conf
; /* Interface configurations */
34 struct sockaddr addr
; /* Address data */
35 struct ifreq
*ifp
; /* Interface data */
36 int ifpsize
; /* Size of interface data */
37 struct ifaddrs
*temp
; /* Pointer to current interface */
38 struct ifreq request
; /* Interface request */
42 * Start with an empty list...
51 * Create a UDP socket to get the interface data...
54 memset (&addr
, 0, sizeof(addr
));
55 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
59 * Try to get the list of interfaces...
62 conf
.ifc_len
= sizeof(buffer
);
63 conf
.ifc_buf
= buffer
;
65 if (ioctl(sock
, SIOCGIFCONF
, &conf
) < 0)
68 * Couldn't get the list of interfaces...
76 * OK, got the list of interfaces, now lets step through the
77 * buffer to pull them out...
80 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
81 # define sockaddr_len(a) ((a)->sa_len)
83 # define sockaddr_len(a) (sizeof(struct sockaddr))
84 # endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
86 for (bufptr
= buffer
, bufend
= buffer
+ conf
.ifc_len
;
91 * Get the current interface information...
94 ifp
= (struct ifreq
*)bufptr
;
95 ifpsize
= sizeof(ifp
->ifr_name
) + sockaddr_len(&(ifp
->ifr_addr
));
97 if (ifpsize
< sizeof(struct ifreq
))
98 ifpsize
= sizeof(struct ifreq
);
100 memset(&request
, 0, sizeof(request
));
101 memcpy(request
.ifr_name
, ifp
->ifr_name
, sizeof(ifp
->ifr_name
));
104 * Check the status of the interface...
107 if (ioctl(sock
, SIOCGIFFLAGS
, &request
) < 0)
111 * Allocate memory for a single interface record...
114 if ((temp
= calloc(1, sizeof(struct ifaddrs
))) == NULL
)
117 * Unable to allocate memory...
125 * Add this record to the front of the list and copy the name, flags,
126 * and network address...
129 temp
->ifa_next
= *addrs
;
131 temp
->ifa_name
= strdup(ifp
->ifr_name
);
132 temp
->ifa_flags
= request
.ifr_flags
;
133 if ((temp
->ifa_addr
= calloc(1, sockaddr_len(&(ifp
->ifr_addr
)))) != NULL
)
134 memcpy(temp
->ifa_addr
, &(ifp
->ifr_addr
), sockaddr_len(&(ifp
->ifr_addr
)));
137 * Try to get the netmask for the interface...
140 if (!ioctl(sock
, SIOCGIFNETMASK
, &request
))
143 * Got it, make a copy...
146 if ((temp
->ifa_netmask
= calloc(1, sizeof(request
.ifr_netmask
))) != NULL
)
147 memcpy(temp
->ifa_netmask
, &(request
.ifr_netmask
),
148 sizeof(request
.ifr_netmask
));
152 * Then get the broadcast or point-to-point (destination) address,
156 if (temp
->ifa_flags
& IFF_BROADCAST
)
159 * Have a broadcast address, so get it!
162 if (!ioctl(sock
, SIOCGIFBRDADDR
, &request
))
165 * Got it, make a copy...
168 if ((temp
->ifa_broadaddr
=
169 calloc(1, sizeof(request
.ifr_broadaddr
))) != NULL
)
170 memcpy(temp
->ifa_broadaddr
, &(request
.ifr_broadaddr
),
171 sizeof(request
.ifr_broadaddr
));
174 else if (temp
->ifa_flags
& IFF_POINTOPOINT
)
177 * Point-to-point interface; grab the remote address...
180 if (!ioctl(sock
, SIOCGIFDSTADDR
, &request
))
182 temp
->ifa_dstaddr
= malloc(sizeof(request
.ifr_dstaddr
));
183 memcpy(temp
->ifa_dstaddr
, &(request
.ifr_dstaddr
),
184 sizeof(request
.ifr_dstaddr
));
190 * OK, we're done with the socket, close it and return 0...
200 * '_cups_freeifaddrs()' - Free an interface list...
204 _cups_freeifaddrs(struct ifaddrs
*addrs
)/* I - Interface list to free */
206 struct ifaddrs
*next
; /* Next interface in list */
209 while (addrs
!= NULL
)
212 * Make a copy of the next interface pointer...
215 next
= addrs
->ifa_next
;
218 * Free data values as needed...
223 free(addrs
->ifa_name
);
224 addrs
->ifa_name
= NULL
;
229 free(addrs
->ifa_addr
);
230 addrs
->ifa_addr
= NULL
;
233 if (addrs
->ifa_netmask
)
235 free(addrs
->ifa_netmask
);
236 addrs
->ifa_netmask
= NULL
;
239 if (addrs
->ifa_dstaddr
)
241 free(addrs
->ifa_dstaddr
);
242 addrs
->ifa_dstaddr
= NULL
;
246 * Free this node and continue to the next...
254 #endif /* !HAVE_GETIFADDRS */