1*640235e2SEnji Cooper /* $NetBSD: ifconf.c,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $ */
2*640235e2SEnji Cooper /*
3*640235e2SEnji Cooper * Copyright (c) 2014 The NetBSD Foundation, Inc.
4*640235e2SEnji Cooper * All rights reserved.
5*640235e2SEnji Cooper *
6*640235e2SEnji Cooper * Redistribution and use in source and binary forms, with or without
7*640235e2SEnji Cooper * modification, are permitted provided that the following conditions
8*640235e2SEnji Cooper * are met:
9*640235e2SEnji Cooper * 1. Redistributions of source code must retain the above copyright
10*640235e2SEnji Cooper * notice, this list of conditions and the following disclaimer.
11*640235e2SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
12*640235e2SEnji Cooper * notice, this list of conditions and the following disclaimer in the
13*640235e2SEnji Cooper * documentation and/or other materials provided with the distribution.
14*640235e2SEnji Cooper *
15*640235e2SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*640235e2SEnji Cooper * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*640235e2SEnji Cooper * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*640235e2SEnji Cooper * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*640235e2SEnji Cooper * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*640235e2SEnji Cooper * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*640235e2SEnji Cooper * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*640235e2SEnji Cooper * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*640235e2SEnji Cooper * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*640235e2SEnji Cooper * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*640235e2SEnji Cooper */
26*640235e2SEnji Cooper
27*640235e2SEnji Cooper #include <sys/cdefs.h>
28*640235e2SEnji Cooper __RCSID("$NetBSD: ifconf.c,v 1.1 2014/12/08 04:23:03 ozaki-r Exp $");
29*640235e2SEnji Cooper #include <sys/types.h>
30*640235e2SEnji Cooper #include <sys/socket.h>
31*640235e2SEnji Cooper #include <sys/sockio.h>
32*640235e2SEnji Cooper #include <sys/ioctl.h>
33*640235e2SEnji Cooper
34*640235e2SEnji Cooper #include <net/if.h>
35*640235e2SEnji Cooper
36*640235e2SEnji Cooper #include <stdio.h>
37*640235e2SEnji Cooper #include <err.h>
38*640235e2SEnji Cooper #include <stdlib.h>
39*640235e2SEnji Cooper #include <unistd.h>
40*640235e2SEnji Cooper #include <string.h>
41*640235e2SEnji Cooper
42*640235e2SEnji Cooper static void
help(void)43*640235e2SEnji Cooper help(void)
44*640235e2SEnji Cooper {
45*640235e2SEnji Cooper fprintf(stderr, "usage:\n\t%s total\n\t%s list [<nifreqs>]\n",
46*640235e2SEnji Cooper getprogname(), getprogname());
47*640235e2SEnji Cooper exit(EXIT_FAILURE);
48*640235e2SEnji Cooper }
49*640235e2SEnji Cooper
50*640235e2SEnji Cooper static int
get_number_of_entries(void)51*640235e2SEnji Cooper get_number_of_entries(void)
52*640235e2SEnji Cooper {
53*640235e2SEnji Cooper int fd, r;
54*640235e2SEnji Cooper struct ifconf ifc;
55*640235e2SEnji Cooper
56*640235e2SEnji Cooper fd = socket(AF_INET, SOCK_DGRAM, 0);
57*640235e2SEnji Cooper if (fd == -1)
58*640235e2SEnji Cooper err(EXIT_FAILURE, "socket");
59*640235e2SEnji Cooper
60*640235e2SEnji Cooper ifc.ifc_len = 0;
61*640235e2SEnji Cooper ifc.ifc_buf = NULL;
62*640235e2SEnji Cooper
63*640235e2SEnji Cooper r = ioctl(fd, SIOCGIFCONF, &ifc);
64*640235e2SEnji Cooper if (r == -1)
65*640235e2SEnji Cooper err(EXIT_FAILURE, "ioctl");
66*640235e2SEnji Cooper
67*640235e2SEnji Cooper close(fd);
68*640235e2SEnji Cooper
69*640235e2SEnji Cooper return ifc.ifc_len / sizeof(struct ifreq);
70*640235e2SEnji Cooper }
71*640235e2SEnji Cooper
72*640235e2SEnji Cooper static void
show_number_of_entries(void)73*640235e2SEnji Cooper show_number_of_entries(void)
74*640235e2SEnji Cooper {
75*640235e2SEnji Cooper printf("%d\n", get_number_of_entries());
76*640235e2SEnji Cooper }
77*640235e2SEnji Cooper
78*640235e2SEnji Cooper static void
show_interfaces(int nifreqs)79*640235e2SEnji Cooper show_interfaces(int nifreqs)
80*640235e2SEnji Cooper {
81*640235e2SEnji Cooper int i, fd, r;
82*640235e2SEnji Cooper struct ifconf ifc;
83*640235e2SEnji Cooper struct ifreq *ifreqs;
84*640235e2SEnji Cooper
85*640235e2SEnji Cooper if (nifreqs == 0)
86*640235e2SEnji Cooper nifreqs = get_number_of_entries();
87*640235e2SEnji Cooper
88*640235e2SEnji Cooper if (nifreqs <= 0)
89*640235e2SEnji Cooper errx(EXIT_FAILURE, "nifreqs=%d", nifreqs);
90*640235e2SEnji Cooper
91*640235e2SEnji Cooper ifreqs = malloc(sizeof(struct ifreq) * nifreqs);
92*640235e2SEnji Cooper if (ifreqs == NULL)
93*640235e2SEnji Cooper err(EXIT_FAILURE, "malloc(sizeof(ifreq) * %d)", nifreqs);
94*640235e2SEnji Cooper
95*640235e2SEnji Cooper fd = socket(AF_INET, SOCK_DGRAM, 0);
96*640235e2SEnji Cooper if (fd == -1)
97*640235e2SEnji Cooper err(EXIT_FAILURE, "socket");
98*640235e2SEnji Cooper
99*640235e2SEnji Cooper ifc.ifc_len = sizeof(struct ifreq) * nifreqs;
100*640235e2SEnji Cooper ifc.ifc_req = ifreqs;
101*640235e2SEnji Cooper
102*640235e2SEnji Cooper r = ioctl(fd, SIOCGIFCONF, &ifc);
103*640235e2SEnji Cooper if (r == -1)
104*640235e2SEnji Cooper err(EXIT_FAILURE, "ioctl");
105*640235e2SEnji Cooper close(fd);
106*640235e2SEnji Cooper
107*640235e2SEnji Cooper for (i=0; i < (int)(ifc.ifc_len / sizeof(struct ifreq)); i++) {
108*640235e2SEnji Cooper printf("%s: af=%hhu socklen=%hhu\n", ifreqs[i].ifr_name,
109*640235e2SEnji Cooper ifreqs[i].ifr_addr.sa_family, ifreqs[i].ifr_addr.sa_len);
110*640235e2SEnji Cooper }
111*640235e2SEnji Cooper
112*640235e2SEnji Cooper free(ifreqs);
113*640235e2SEnji Cooper }
114*640235e2SEnji Cooper
115*640235e2SEnji Cooper int
main(int argc,char * argv[])116*640235e2SEnji Cooper main(int argc, char *argv[])
117*640235e2SEnji Cooper {
118*640235e2SEnji Cooper if (argc < 2)
119*640235e2SEnji Cooper help();
120*640235e2SEnji Cooper
121*640235e2SEnji Cooper if (strcmp(argv[1], "total") == 0) {
122*640235e2SEnji Cooper show_number_of_entries();
123*640235e2SEnji Cooper } else if (strcmp(argv[1], "list") == 0) {
124*640235e2SEnji Cooper if (argc == 2)
125*640235e2SEnji Cooper show_interfaces(0);
126*640235e2SEnji Cooper else if (argc == 3)
127*640235e2SEnji Cooper show_interfaces(atoi(argv[2]));
128*640235e2SEnji Cooper else
129*640235e2SEnji Cooper help();
130*640235e2SEnji Cooper } else
131*640235e2SEnji Cooper help();
132*640235e2SEnji Cooper
133*640235e2SEnji Cooper return EXIT_SUCCESS;
134*640235e2SEnji Cooper }
135