3 #include <sys/socket.h>
7 #include <linux/sockios.h>
9 #include <netinet/in.h>
50 unsigned long long in_packets
, in_bytes
, in_errors
, in_drops
;
51 unsigned long long in_fifo
, in_frame
, in_compress
, in_multicast
;
52 unsigned long long out_bytes
, out_packets
, out_errors
, out_drops
;
53 unsigned long long out_fifo
, out_colls
, out_carrier
, out_multicast
;
57 void print_quad_ipv4(in_addr_t i
) {
60 (i
& 0xff000000) >> 24,
61 (i
& 0x00ff0000) >> 16,
62 (i
& 0x0000ff00) >> 8,
66 void print_quad_ipv6(uint16_t *a
) {
67 printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
68 a
[0], a
[1], a
[2], a
[3], a
[4], a
[5], a
[6], a
[7]);
71 void print_quad(struct sockaddr
*adr
) {
72 switch (adr
->sa_family
) {
74 print_quad_ipv4(((struct sockaddr_in
*)adr
)->sin_addr
.s_addr
);
77 print_quad_ipv6(((struct sockaddr_in6
*)adr
)->sin6_addr
.s6_addr16
);
85 enum print_error_enum
{
94 static int do_socket_ioctl(const char *ifname
, const int request
,
95 struct ifreq
*req
, int *ioctl_errno
,
96 const enum print_error_enum print_error
) {
99 if ((sock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
)) == -1)
101 strncpy(req
->ifr_name
, ifname
, IFNAMSIZ
);
102 req
->ifr_name
[IFNAMSIZ
- 1] = 0;
104 if ((res
= ioctl(sock
, request
, req
)) == -1) {
106 *ioctl_errno
= errno
;
107 if (print_error
== PRINT_ERROR
)
108 fprintf(stderr
, "ioctl on %s: %s\n", ifname
, strerror(errno
));
118 int if_exists(const char *iface
) {
120 return !do_socket_ioctl(iface
, SIOCGIFFLAGS
, &r
, NULL
, PRINT_NO_ERROR
);
123 void if_flags(const char *iface
) {
131 { IFF_BROADCAST
, "Broadcast" },
132 { IFF_DEBUG
, "Debugging" },
133 { IFF_LOOPBACK
, "Loopback" },
134 { IFF_POINTOPOINT
, "Ppp" },
135 { IFF_NOTRAILERS
, "No-trailers" },
136 { IFF_RUNNING
, "Running" },
137 { IFF_NOARP
, "No-arp" },
138 { IFF_PROMISC
, "Promiscuous" },
139 { IFF_ALLMULTI
, "All-multicast" },
140 { IFF_MASTER
, "Load-master" },
141 { IFF_SLAVE
, "Load-slave" },
142 { IFF_MULTICAST
, "Multicast" },
143 { IFF_PORTSEL
, "Port-select" },
144 { IFF_AUTOMEDIA
, "Auto-detect" },
145 { IFF_DYNAMIC
, "Dynaddr" },
146 { 0xffff0000, "Unknown-flags" },
149 if (do_socket_ioctl(iface
, SIOCGIFFLAGS
, &r
, NULL
, PRINT_ERROR
))
152 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++)
153 printf("%s%s%s", (r
.ifr_flags
& flags
[i
].flag
) ? "On " : "Off ",
155 sizeof(flags
) / sizeof(flags
[0]) - 1 == i
? "" : "\n");
158 void if_hwaddr(const char *iface
) {
160 unsigned char *hwaddr
;
162 if (do_socket_ioctl(iface
, SIOCGIFHWADDR
, &r
, NULL
, PRINT_ERROR
))
165 hwaddr
= (unsigned char *)r
.ifr_hwaddr
.sa_data
;
166 printf("%02X:%02X:%02X:%02X:%02X:%02X",
167 hwaddr
[0], hwaddr
[1], hwaddr
[2], hwaddr
[3], hwaddr
[4], hwaddr
[5]);
170 static struct sockaddr
*if_addr_value(const char *iface
, struct ifreq
*r
,
174 if (do_socket_ioctl(iface
, request
, r
, &e
, PRINT_NO_ERROR
)) {
175 if (e
== EADDRNOTAVAIL
)
182 struct sockaddr
*if_addr(const char *iface
, struct ifreq
*r
) {
183 return if_addr_value(iface
, r
, SIOCGIFADDR
);
186 struct sockaddr
*if_mask(const char *iface
, struct ifreq
*r
) {
187 return if_addr_value(iface
, r
, SIOCGIFNETMASK
);
190 struct sockaddr
*if_bcast(const char *iface
, struct ifreq
*r
) {
191 return if_addr_value(iface
, r
, SIOCGIFBRDADDR
);
194 struct sockaddr
*if_network(const char *iface
) {
195 struct sockaddr
*saddr
;
196 static struct ifreq req
;
199 if (!(saddr
= if_mask(iface
, &req
)))
202 mask
= ((struct sockaddr_in
*)saddr
)->sin_addr
.s_addr
;
204 if (!(saddr
= if_addr(iface
, &req
)))
207 ((struct sockaddr_in
*)saddr
)->sin_addr
.s_addr
&= mask
;
211 int if_mtu(const char *iface
) {
212 static struct ifreq req
;
214 if (do_socket_ioctl(iface
, SIOCGIFMTU
, &req
, NULL
, PRINT_ERROR
))
220 static void skipline(FILE *fd
) {
224 } while (ch
!= '\n' && ch
!= EOF
);
227 struct if_stat
*get_stats(const char *iface
) {
229 struct if_stat
*ifstat
;
232 if (!(ifstat
= malloc(sizeof(struct if_stat
)))) {
237 if ((fd
= fopen("/proc/net/dev", "r")) == NULL
) {
238 perror("fopen(\"/proc/net/dev\")");
248 int items
= fscanf(fd
,
249 " %20[^:]:%llu %llu %llu %llu %llu %llu %llu %llu "
250 "%llu %llu %llu %llu %llu %llu %llu %llu",
252 &ifstat
->in_bytes
, &ifstat
->in_packets
,
253 &ifstat
->in_errors
, &ifstat
->in_drops
,
254 &ifstat
->in_fifo
, &ifstat
->in_frame
,
255 &ifstat
->in_compress
, &ifstat
->in_multicast
,
256 &ifstat
->out_bytes
, &ifstat
->out_packets
,
257 &ifstat
->out_errors
, &ifstat
->out_drops
,
258 &ifstat
->out_fifo
, &ifstat
->out_colls
,
259 &ifstat
->out_carrier
, &ifstat
->out_carrier
265 fprintf(stderr
, "Invalid data read, check!\n");
269 if (!strncmp(name
, iface
, sizeof(name
))) {
283 unsigned int is_stat
;
286 { "-e", DO_EXISTS
, 0, "Reports interface existence via return code" },
287 { "-p", DO_PALL
, 0, "Print out the whole config of iface" },
288 { "-pe", DO_PEXISTS
, 0, "Print out yes or no according to existence" },
289 { "-ph", DO_PHWADDRESS
, 0, "Print out the hardware address" },
290 { "-pa", DO_PADDRESS
, 0, "Print out the address" },
291 { "-pn", DO_PMASK
, 0, "Print netmask" },
292 { "-pN", DO_PNETWORK
, 0, "Print network address" },
293 { "-pb", DO_PCAST
, 0, "Print broadcast" },
294 { "-pm", DO_PMTU
, 0, "Print mtu" },
295 { "-pf", DO_PFLAGS
, 0, "Print flags" },
297 { "-si", DO_SINALL
, 1, "Print all statistics on input" },
298 { "-sip", DO_SINPACKETS
, 1, "Print # of in packets" },
299 { "-sib", DO_SINBYTES
, 1, "Print # of in bytes" },
300 { "-sie", DO_SINERRORS
, 1, "Print # of in errors" },
301 { "-sid", DO_SINDROPS
, 1, "Print # of in drops" },
302 { "-sif", DO_SINFIFO
, 1, "Print # of in fifo overruns" },
303 { "-sic", DO_SINCOMPRESSES
, 1, "Print # of in compress" },
304 { "-sim", DO_SINMULTICAST
, 1, "Print # of in multicast" },
305 { "-so", DO_SOUTALL
, 1, "Print all statistics on output" },
306 { "-sop", DO_SOUTPACKETS
, 1, "Print # of out packets" },
307 { "-sob", DO_SOUTBYTES
, 1, "Print # of out bytes" },
308 { "-soe", DO_SOUTERRORS
, 1, "Print # of out errors" },
309 { "-sod", DO_SOUTDROPS
, 1, "Print # of out drops" },
310 { "-sof", DO_SOUTFIFO
, 1, "Print # of out fifo overruns" },
311 { "-sox", DO_SOUTCOLLS
, 1, "Print # of out collisions" },
312 { "-soc", DO_SOUTCARRIER
, 1, "Print # of out carrier loss" },
313 { "-som", DO_SOUTMULTICAST
, 1, "Print # of out multicast" },
314 { "-bips",DO_BIPS
, 1, "Print # of incoming bytes per second" },
315 { "-bops",DO_BOPS
, 1, "Print # of outgoing bytes per second" },
318 void usage(const char *name
) {
321 fprintf(stderr
, "Usage: %s [options] iface\n", name
);
322 for (i
= 0; i
< sizeof(options
) / sizeof(options
[0]); i
++) {
323 fprintf(stderr
, " %5s %s\n",
324 options
[i
].option
, options
[i
].description
);
328 void add_do(int *ndo
, int **todo
, int act
) {
329 *todo
= realloc(*todo
, (*ndo
+1) * sizeof(int));
334 static void print_addr(struct sockaddr
*sadr
) {
336 fprintf(stderr
, "Error\n");
342 struct if_stat
*ifstats
, *ifstats2
= NULL
;
344 void please_do(int ndo
, int *todo
, const char *ifname
) {
346 static struct ifreq req
;
348 // printf("I have %d items in my queue.\n",ndo);
349 for (i
=0; i
<ndo
; i
++) {
352 exit(!if_exists(ifname
));
354 printf("%s", if_exists(ifname
) ? "yes" : "no");
360 print_addr(if_addr(ifname
, &req
));
366 print_addr(if_mask(ifname
, &req
));
369 print_addr(if_bcast(ifname
, &req
));
372 printf("%d", if_mtu(ifname
));
375 print_addr(if_network(ifname
));
378 print_addr(if_addr(ifname
, &req
));
380 print_addr(if_mask(ifname
, &req
));
382 print_addr(if_bcast(ifname
, &req
));
384 printf("%d", if_mtu(ifname
));
388 printf("%llu",ifstats
->in_packets
);
391 printf("%llu",ifstats
->in_bytes
);
394 printf("%llu",ifstats
->in_errors
);
397 printf("%llu",ifstats
->in_drops
);
400 printf("%llu",ifstats
->in_fifo
);
403 printf("%llu",ifstats
->in_frame
);
405 case DO_SINCOMPRESSES
:
406 printf("%llu",ifstats
->in_compress
);
408 case DO_SINMULTICAST
:
409 printf("%llu",ifstats
->in_multicast
);
412 printf("%llu %llu %llu %llu %llu %llu %llu %llu",
413 ifstats
->in_bytes
, ifstats
->in_packets
,
414 ifstats
->in_errors
, ifstats
->in_drops
,
415 ifstats
->in_fifo
, ifstats
->in_frame
,
416 ifstats
->in_compress
, ifstats
->in_multicast
);
419 printf("%llu",ifstats
->out_bytes
);
422 printf("%llu",ifstats
->out_packets
);
425 printf("%llu",ifstats
->out_errors
);
428 printf("%llu",ifstats
->out_drops
);
431 printf("%llu",ifstats
->out_fifo
);
434 printf("%llu",ifstats
->out_colls
);
437 printf("%llu",ifstats
->out_carrier
);
439 case DO_SOUTMULTICAST
:
440 printf("%llu",ifstats
->out_multicast
);
443 if (ifstats2
== NULL
) {
445 ifstats2
= get_stats(ifname
);
447 printf("%llu", ifstats2
->in_bytes
-ifstats
->in_bytes
);
450 if (ifstats2
== NULL
) {
452 ifstats2
= get_stats(ifname
);
454 printf("%llu", ifstats2
->out_bytes
-ifstats
->out_bytes
);
457 printf("%llu %llu %llu %llu %llu %llu %llu %llu",
458 ifstats
->out_bytes
, ifstats
->out_packets
,
459 ifstats
->out_errors
, ifstats
->out_drops
,
460 ifstats
->out_fifo
, ifstats
->out_colls
,
461 ifstats
->out_carrier
, ifstats
->out_multicast
);
464 printf("Unknown command: %d", todo
[i
]);
471 int main(int argc
, char *argv
[]) {
477 unsigned int i
, found
;
484 while (narg
< argc
- 1) {
489 for (i
= 0; i
< sizeof(options
) / sizeof(options
[0]); i
++) {
490 if (!strcmp(argv
[narg
], options
[i
].option
)) {
491 add_do(&ndo
, &todo
, options
[i
].flag
);
492 do_stats
|= options
[i
].is_stat
;
501 if (argv
[narg
][0] == '-') {
511 if (narg
+ 1 < argc
|| !ifname
) {
516 if (do_stats
&& (ifstats
= get_stats(ifname
)) == NULL
) {
517 fprintf(stderr
, "Error getting statistics for %s\n", ifname
);
521 please_do(ndo
, todo
, ifname
);