1 /* Determine protocol families for which interfaces exist. Linux version.
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/socket.h>
28 #include <asm/types.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
32 #include <kernel-features.h>
36 make_request (int fd
, pid_t pid
, bool *seen_ipv4
, bool *seen_ipv6
)
43 struct sockaddr_nl nladdr
;
45 req
.nlh
.nlmsg_len
= sizeof (req
);
46 req
.nlh
.nlmsg_type
= RTM_GETADDR
;
47 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
48 req
.nlh
.nlmsg_pid
= 0;
49 req
.nlh
.nlmsg_seq
= time (NULL
);
50 req
.g
.rtgen_family
= AF_UNSPEC
;
52 memset (&nladdr
, '\0', sizeof (nladdr
));
53 nladdr
.nl_family
= AF_NETLINK
;
55 if (TEMP_FAILURE_RETRY (__sendto (fd
, (void *) &req
, sizeof (req
), 0,
56 (struct sockaddr
*) &nladdr
,
57 sizeof (nladdr
))) < 0)
65 struct iovec iov
= { buf
, sizeof (buf
) };
71 (void *) &nladdr
, sizeof (nladdr
),
77 ssize_t read_len
= TEMP_FAILURE_RETRY (__recvmsg (fd
, &msg
, 0));
81 if (msg
.msg_flags
& MSG_TRUNC
)
84 struct nlmsghdr
*nlmh
;
85 for (nlmh
= (struct nlmsghdr
*) buf
;
86 NLMSG_OK (nlmh
, (size_t) read_len
);
87 nlmh
= (struct nlmsghdr
*) NLMSG_NEXT (nlmh
, read_len
))
89 if (nladdr
.nl_pid
!= 0 || (pid_t
) nlmh
->nlmsg_pid
!= pid
90 || nlmh
->nlmsg_seq
!= req
.nlh
.nlmsg_seq
)
93 if (nlmh
->nlmsg_type
== RTM_NEWADDR
)
95 struct ifaddrmsg
*ifam
= (struct ifaddrmsg
*) NLMSG_DATA (nlmh
);
97 switch (ifam
->ifa_family
)
110 else if (nlmh
->nlmsg_type
== NLMSG_DONE
)
111 /* We found the end, leave the loop. */
124 /* We don't know if we have NETLINK support compiled in in our
126 #if __ASSUME_NETLINK_SUPPORT == 0
127 /* Define in ifaddrs.h. */
128 extern int __no_netlink_support attribute_hidden
;
130 # define __no_netlink_support 0
136 __check_pf (bool *seen_ipv4
, bool *seen_ipv6
)
138 if (! __no_netlink_support
)
140 int fd
= __socket (PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
142 struct sockaddr_nl nladdr
;
143 memset (&nladdr
, '\0', sizeof (nladdr
));
144 nladdr
.nl_family
= AF_NETLINK
;
146 socklen_t addr_len
= sizeof (nladdr
);
149 && __bind (fd
, (struct sockaddr
*) &nladdr
, sizeof (nladdr
)) == 0
150 && __getsockname (fd
, (struct sockaddr
*) &nladdr
, &addr_len
) == 0
151 && make_request (fd
, nladdr
.nl_pid
, seen_ipv4
, seen_ipv6
) == 0)
158 #if __ASSUME_NETLINK_SUPPORT == 0
159 /* Remember that there is no netlink support. */
160 __no_netlink_support
= 1;
162 /* We cannot determine what interfaces are available. Be
169 #if __ASSUME_NETLINK_SUPPORT == 0
170 /* No netlink. Get the interface list via getifaddrs. */
171 struct ifaddrs
*ifa
= NULL
;
172 if (getifaddrs (&ifa
) != 0)
174 /* We cannot determine what interfaces are available. Be
184 struct ifaddrs
*runp
;
185 for (runp
= ifa
; runp
!= NULL
; runp
= runp
->ifa_next
)
186 if (runp
->ifa_addr
->sa_family
== PF_INET
)
188 else if (runp
->ifa_addr
->sa_family
== PF_INET6
)
191 (void) freeifaddrs (ifa
);