]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/ifinfo/ifinfo.c
This commit was generated by cvs2svn to compensate for changes in r147013,
[FreeBSD/FreeBSD.git] / tools / tools / ifinfo / ifinfo.c
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/types.h>
32 #include <sys/socket.h>         /* for PF_LINK */
33 #include <sys/sysctl.h>
34 #include <sys/time.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43
44 #include <net/if.h>
45 #include <net/if_types.h>
46 #include <net/if_mib.h>
47
48 #include "ifinfo.h"
49
50 static void printit(const struct ifmibdata *);
51 static const char *iftype(int);
52 static const char *ifphys(int, int);
53 static int isit(int, char **, const char *);
54 static printfcn findlink(int);
55
56 static void
57 usage(const char *argv0)
58 {
59         fprintf(stderr, "%s: usage:\n\t%s [-l]\n", argv0, argv0);
60         exit(EX_USAGE);
61 }
62
63 int
64 main(int argc, char **argv)
65 {
66         int i, maxifno, retval;
67         struct ifmibdata ifmd;
68         int name[6];
69         size_t len;
70         int c;
71         int dolink = 0;
72         void *linkmib;
73         size_t linkmiblen;
74         printfcn pf;
75
76         while ((c = getopt(argc, argv, "l")) != -1) {
77                 switch(c) {
78                 case 'l':
79                         dolink = 1;
80                         break;
81                 default:
82                         usage(argv[0]);
83                 }
84         }
85         
86         retval = 1;
87
88         name[0] = CTL_NET;
89         name[1] = PF_LINK;
90         name[2] = NETLINK_GENERIC;
91         name[3] = IFMIB_SYSTEM;
92         name[4] = IFMIB_IFCOUNT;
93
94         len = sizeof maxifno;
95         if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
96                 err(EX_OSERR, "sysctl(net.link.generic.system.ifcount)");
97
98         for (i = 1; i <= maxifno; i++) {
99                 len = sizeof ifmd;
100                 name[3] = IFMIB_IFDATA;
101                 name[4] = i;
102                 name[5] = IFDATA_GENERAL;
103                 if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
104                         if (errno == ENOENT)
105                                 continue;
106
107                         err(EX_OSERR, "sysctl(net.link.ifdata.%d.general)",
108                             i);
109                 }
110
111                 if (!isit(argc - optind, argv + optind, ifmd.ifmd_name))
112                         continue;
113                 printit(&ifmd);
114                 if (dolink && (pf = findlink(ifmd.ifmd_data.ifi_type))) {
115                         name[5] = IFDATA_LINKSPECIFIC;
116                         if (sysctl(name, 6, 0, &linkmiblen, 0, 0) < 0)
117                                 err(EX_OSERR, 
118                                     "sysctl(net.link.ifdata.%d.linkspec) size",
119                                     i);
120                         linkmib = malloc(linkmiblen);
121                         if (!linkmib)
122                                 err(EX_OSERR, "malloc(%lu)", 
123                                     (u_long)linkmiblen);
124                         if (sysctl(name, 6, linkmib, &linkmiblen, 0, 0) < 0)
125                                 err(EX_OSERR, 
126                                     "sysctl(net.link.ifdata.%d.linkspec)",
127                                     i);
128                         pf(linkmib, linkmiblen);
129                         free(linkmib);
130                 }
131                 retval = 0;
132         }
133
134         return retval;
135 }
136
137 static void
138 printit(const struct ifmibdata *ifmd)
139 {
140         printf("Interface %.*s:\n", IFNAMSIZ, ifmd->ifmd_name);
141         printf("\tflags: %x\n", ifmd->ifmd_flags);
142         printf("\tpromiscuous listeners: %d\n", ifmd->ifmd_pcount);
143         printf("\tsend queue length: %d\n", ifmd->ifmd_snd_len);
144         printf("\tsend queue max length: %d\n", ifmd->ifmd_snd_maxlen);
145         printf("\tsend queue drops: %d\n", ifmd->ifmd_snd_drops);
146         printf("\ttype: %s\n", iftype(ifmd->ifmd_data.ifi_type));
147         printf("\tphysical: %s\n", ifphys(ifmd->ifmd_data.ifi_type,
148                                           ifmd->ifmd_data.ifi_physical));
149         printf("\taddress length: %d\n", ifmd->ifmd_data.ifi_addrlen);
150         printf("\theader length: %d\n", ifmd->ifmd_data.ifi_hdrlen);
151         printf("\treceive quota: %d\n", ifmd->ifmd_data.ifi_recvquota);
152         printf("\ttransmit quota: %d\n", ifmd->ifmd_data.ifi_xmitquota);
153         printf("\tmtu: %lu\n", ifmd->ifmd_data.ifi_mtu);
154         printf("\tmetric: %lu\n", ifmd->ifmd_data.ifi_metric);
155         printf("\tline rate: %lu bit/s\n", ifmd->ifmd_data.ifi_baudrate);
156         printf("\tpackets received: %lu\n", ifmd->ifmd_data.ifi_ipackets);
157         printf("\tinput errors: %lu\n", ifmd->ifmd_data.ifi_ierrors);
158         printf("\tpackets transmitted: %lu\n", ifmd->ifmd_data.ifi_opackets);
159         printf("\toutput errors: %lu\n", ifmd->ifmd_data.ifi_oerrors);
160         printf("\tcollisions: %lu\n", ifmd->ifmd_data.ifi_collisions);
161         printf("\tbytes received: %lu\n", ifmd->ifmd_data.ifi_ibytes);
162         printf("\tbytes transmitted: %lu\n", ifmd->ifmd_data.ifi_obytes);
163         printf("\tmulticasts received: %lu\n", ifmd->ifmd_data.ifi_imcasts);
164         printf("\tmulticasts transmitted: %lu\n", ifmd->ifmd_data.ifi_omcasts);
165         printf("\tinput queue drops: %lu\n", ifmd->ifmd_data.ifi_iqdrops);
166         printf("\tpackets for unknown protocol: %lu\n", 
167                ifmd->ifmd_data.ifi_noproto);
168 #ifdef notdef
169         printf("\treceive timing: %lu usec\n", ifmd->ifmd_data.ifi_recvtiming);
170         printf("\ttransmit timing: %lu usec\n", 
171                ifmd->ifmd_data.ifi_xmittiming);
172 #endif
173 }
174
175 static const char *const if_types[] = {
176         "reserved",
177         "other",
178         "BBN 1822",
179         "HDH 1822",
180         "X.25 DDN",
181         "X.25",
182         "Ethernet",
183         "ISO 8802-3 CSMA/CD",
184         "ISO 8802-4 Token Bus",
185         "ISO 8802-5 Token Ring",
186         "ISO 8802-6 DQDB MAN",
187         "StarLAN",
188         "Proteon proNET-10",
189         "Proteon proNET-80",
190         "HyperChannel",
191         "FDDI",
192         "LAP-B",
193         "SDLC",
194         "T-1",
195         "CEPT",
196         "Basic rate ISDN",
197         "Primary rate ISDN",
198         "Proprietary P2P",
199         "PPP",
200         "Loopback",
201         "ISO CLNP over IP",
202         "Experimental Ethernet",
203         "XNS over IP",
204         "SLIP",
205         "Ultra Technologies",
206         "DS-3",
207         "SMDS",
208         "Frame Relay",
209         "RS-232 serial",
210         "Parallel printer port",
211         "ARCNET",
212         "ARCNET+",
213         "ATM",
214         "MIOX25",
215         "SONET/SDH",
216         "X25PLE",
217         "ISO 8802-2 LLC",
218         "LocalTalk",
219         "SMDSDXI",
220         "Frame Relay DCE",
221         "V.35",
222         "HSSI",
223         "HIPPI",
224         "Generic Modem",
225         "ATM AAL5",
226         "SONETPATH",
227         "SONETVT",
228         "SMDS InterCarrier Interface",
229         "Proprietary virtual interface",
230         "Proprietary multiplexing",
231         "Generic tunnel interface",
232         "IPv6-to-IPv4 TCP relay capturing interface",
233         "6to4 tunnel interface"
234 };
235 #define NIFTYPES ((sizeof if_types)/(sizeof if_types[0]))
236
237 static const char *
238 iftype(int type)
239 {
240         static char buf[256];
241
242         if (type <= 0 || type >= NIFTYPES) {
243                 sprintf(buf, "unknown type %d", type);
244                 return buf;
245         }
246
247         return if_types[type];
248 }
249
250 static const char *
251 ifphys(int type, int phys)
252 {
253         static char buf[256];
254
255         sprintf(buf, "unknown physical %d", phys);
256         return buf;
257 }
258
259 static int
260 isit(int argc, char **argv, const char *name)
261 {
262         if (argc == 0)
263                 return 1;
264         for (argc = 0; argv[argc]; argc++) {
265                 if (strncmp(argv[argc], name, IFNAMSIZ) == 0)
266                         return 1;
267         }
268         return 0;
269 }
270
271 static printfcn
272 findlink(int type)
273 {
274         switch(type) {
275         case IFT_ETHER:
276         case IFT_ISO88023:
277         case IFT_STARLAN:
278                 return print_1650;
279         }
280
281         return 0;
282 }