]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/getifaddrs.c
2 * "$Id: getifaddrs.c 6649 2007-07-11 21:46:42Z mike $"
4 * Network interface functions for the Common UNIX Printing System
7 * Copyright 2007 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * "LICENSE" which should have been included with this file. If this
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * _cups_getifaddrs() - Get a list of network interfaces on the system.
19 * _cups_freeifaddrs() - Free an interface list...
23 * Include necessary headers.
26 #include "http-private.h"
29 #ifndef HAVE_GETIFADDRS
31 * '_cups_getifaddrs()' - Get a list of network interfaces on the system.
34 int /* O - 0 on success, -1 on error */
35 _cups_getifaddrs(struct ifaddrs
**addrs
)/* O - List of interfaces */
37 int sock
; /* Socket */
38 char buffer
[65536], /* Buffer for address info */
39 *bufptr
, /* Pointer into buffer */
40 *bufend
; /* End of buffer */
41 struct ifconf conf
; /* Interface configurations */
42 struct sockaddr addr
; /* Address data */
43 struct ifreq
*ifp
; /* Interface data */
44 int ifpsize
; /* Size of interface data */
45 struct ifaddrs
*temp
; /* Pointer to current interface */
46 struct ifreq request
; /* Interface request */
50 * Start with an empty list...
59 * Create a UDP socket to get the interface data...
62 memset (&addr
, 0, sizeof(addr
));
63 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
67 * Try to get the list of interfaces...
70 conf
.ifc_len
= sizeof(buffer
);
71 conf
.ifc_buf
= buffer
;
73 if (ioctl(sock
, SIOCGIFCONF
, &conf
) < 0)
76 * Couldn't get the list of interfaces...
84 * OK, got the list of interfaces, now lets step through the
85 * buffer to pull them out...
88 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
89 # define sockaddr_len(a) ((a)->sa_len)
91 # define sockaddr_len(a) (sizeof(struct sockaddr))
92 # endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
94 for (bufptr
= buffer
, bufend
= buffer
+ conf
.ifc_len
;
99 * Get the current interface information...
102 ifp
= (struct ifreq
*)bufptr
;
103 ifpsize
= sizeof(ifp
->ifr_name
) + sockaddr_len(&(ifp
->ifr_addr
));
105 if (ifpsize
< sizeof(struct ifreq
))
106 ifpsize
= sizeof(struct ifreq
);
108 memset(&request
, 0, sizeof(request
));
109 memcpy(request
.ifr_name
, ifp
->ifr_name
, sizeof(ifp
->ifr_name
));
112 * Check the status of the interface...
115 if (ioctl(sock
, SIOCGIFFLAGS
, &request
) < 0)
119 * Allocate memory for a single interface record...
122 if ((temp
= calloc(1, sizeof(struct ifaddrs
))) == NULL
)
125 * Unable to allocate memory...
133 * Add this record to the front of the list and copy the name, flags,
134 * and network address...
137 temp
->ifa_next
= *addrs
;
139 temp
->ifa_name
= strdup(ifp
->ifr_name
);
140 temp
->ifa_flags
= request
.ifr_flags
;
141 if ((temp
->ifa_addr
= calloc(1, sockaddr_len(&(ifp
->ifr_addr
)))) != NULL
)
142 memcpy(temp
->ifa_addr
, &(ifp
->ifr_addr
), sockaddr_len(&(ifp
->ifr_addr
)));
145 * Try to get the netmask for the interface...
148 if (!ioctl(sock
, SIOCGIFNETMASK
, &request
))
151 * Got it, make a copy...
154 if ((temp
->ifa_netmask
= calloc(1, sizeof(request
.ifr_netmask
))) != NULL
)
155 memcpy(temp
->ifa_netmask
, &(request
.ifr_netmask
),
156 sizeof(request
.ifr_netmask
));
160 * Then get the broadcast or point-to-point (destination) address,
164 if (temp
->ifa_flags
& IFF_BROADCAST
)
167 * Have a broadcast address, so get it!
170 if (!ioctl(sock
, SIOCGIFBRDADDR
, &request
))
173 * Got it, make a copy...
176 if ((temp
->ifa_broadaddr
=
177 calloc(1, sizeof(request
.ifr_broadaddr
))) != NULL
)
178 memcpy(temp
->ifa_broadaddr
, &(request
.ifr_broadaddr
),
179 sizeof(request
.ifr_broadaddr
));
182 else if (temp
->ifa_flags
& IFF_POINTOPOINT
)
185 * Point-to-point interface; grab the remote address...
188 if (!ioctl(sock
, SIOCGIFDSTADDR
, &request
))
190 temp
->ifa_dstaddr
= malloc(sizeof(request
.ifr_dstaddr
));
191 memcpy(temp
->ifa_dstaddr
, &(request
.ifr_dstaddr
),
192 sizeof(request
.ifr_dstaddr
));
198 * OK, we're done with the socket, close it and return 0...
208 * '_cups_freeifaddrs()' - Free an interface list...
212 _cups_freeifaddrs(struct ifaddrs
*addrs
)/* I - Interface list to free */
214 struct ifaddrs
*next
; /* Next interface in list */
217 while (addrs
!= NULL
)
220 * Make a copy of the next interface pointer...
223 next
= addrs
->ifa_next
;
226 * Free data values as needed...
231 free(addrs
->ifa_name
);
232 addrs
->ifa_name
= NULL
;
237 free(addrs
->ifa_addr
);
238 addrs
->ifa_addr
= NULL
;
241 if (addrs
->ifa_netmask
)
243 free(addrs
->ifa_netmask
);
244 addrs
->ifa_netmask
= NULL
;
247 if (addrs
->ifa_dstaddr
)
249 free(addrs
->ifa_dstaddr
);
250 addrs
->ifa_dstaddr
= NULL
;
254 * Free this node and continue to the next...
262 #endif /* !HAVE_GETIFADDRS */
266 * End of "$Id: getifaddrs.c 6649 2007-07-11 21:46:42Z mike $".