3 #include <sys/socket.h>
6 #include <netinet/in.h>
18 /* XXX _SIZEOF_ADDR_IFREQ should accept ptr */
19 #define _SIZEOF_ADDR_IFREQ1(ifr) \
20 ((ifr)->ifr_addr.sa_len > sizeof(struct sockaddr) ? \
21 (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
22 (ifr)->ifr_addr.sa_len) : sizeof(struct ifreq))
25 usage(const char *cmd
)
27 fprintf(stderr
, "%s [-n naddrs]\n", cmd
);
32 main(int argc
, char *argv
[])
41 while ((opt
= getopt(argc
, argv
, "n:")) != -1) {
44 naddrs
= strtol(optarg
, NULL
, 10);
53 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
55 err(2, "socket failed");
57 memset(&ifc
, 0, sizeof(ifc
));
58 ifc
.ifc_len
= sizeof(struct sockaddr_storage
) * naddrs
;
59 ifc
.ifc_buf
= malloc(ifc
.ifc_len
);
60 if (ifc
.ifc_buf
== NULL
)
61 err(2, "malloc failed");
63 if (ioctl(s
, SIOCGIFCONF
, &ifc
, sizeof(ifc
)) < 0)
64 err(2, "ioctl failed");
67 while (ifc
.ifc_len
>= (int)sizeof(*ifr
)) {
70 ifr
= (struct ifreq
*)pos
;
71 len
= _SIZEOF_ADDR_IFREQ1(ifr
);
72 if (ifc
.ifc_len
< len
)
73 err(2, "invalid ifc.ifc_len");
75 if (ifr
->ifr_addr
.sa_family
== AF_UNSPEC
) {
76 printf("%s: no address\n", ifr
->ifr_name
);
77 } else if (ifr
->ifr_addr
.sa_family
== AF_INET
||
78 ifr
->ifr_addr
.sa_family
== AF_INET6
) {
79 char addr_str
[INET6_ADDRSTRLEN
];
83 if (ifr
->ifr_addr
.sa_family
== AF_INET
) {
84 const struct sockaddr_in
*in
=
85 (const struct sockaddr_in
*)&ifr
->ifr_addr
;
88 const struct sockaddr_in6
*in6
=
89 (const struct sockaddr_in6
*)&ifr
->ifr_addr
;
90 src
= &in6
->sin6_addr
;
93 ret
= inet_ntop(ifr
->ifr_addr
.sa_family
, src
,
94 addr_str
, sizeof(addr_str
));
96 err(2, "inet_ntop failed");
97 printf("%s: inet%c %s\n", ifr
->ifr_name
,
98 ifr
->ifr_addr
.sa_family
== AF_INET
? '4' : '6',
100 } else if (ifr
->ifr_addr
.sa_family
== AF_LINK
) {
101 const struct sockaddr_dl
*dl
=
102 (const struct sockaddr_dl
*)&ifr
->ifr_addr
;
104 printf("%s: link%d\n", ifr
->ifr_name
, dl
->sdl_index
);
106 printf("%s: unknown family %d\n", ifr
->ifr_name
,
107 ifr
->ifr_addr
.sa_family
);
113 if (ifc
.ifc_len
!= 0)
114 printf("ifc_len left %d\n", ifc
.ifc_len
);