3 #include <sys/socket.h>
9 #include <linux/sockios.h>
13 #if defined(__FreeBSD_kernel__)
17 #include <netinet/in.h>
58 unsigned long long in_packets, in_bytes, in_errors, in_drops;
59 unsigned long long in_fifo, in_frame, in_compress, in_multicast;
60 unsigned long long out_bytes, out_packets, out_errors, out_drops;
61 unsigned long long out_fifo, out_colls, out_carrier, out_multicast;
65 void print_quad_ipv4(in_addr_t i) {
68 (i & 0xff000000) >> 24,
69 (i & 0x00ff0000) >> 16,
70 (i & 0x0000ff00) >> 8,
74 void print_quad_ipv6(uint16_t *a) {
75 printf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
76 a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
79 void print_quad(struct sockaddr *adr) {
80 switch (adr->sa_family) {
82 print_quad_ipv4(((struct sockaddr_in*)adr)->sin_addr.s_addr);
85 print_quad_ipv6(((struct sockaddr_in6*)adr)->sin6_addr.s6_addr16);
93 enum print_error_enum {
102 static int do_socket_ioctl(const char *ifname, const unsigned long int request,
103 struct ifreq *req, int *ioctl_errno,
104 const enum print_error_enum print_error) {
107 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1)
109 strncpy(req->ifr_name, ifname, IFNAMSIZ);
110 req->ifr_name[IFNAMSIZ - 1] = 0;
112 if ((res = ioctl(sock, request, req)) == -1) {
114 *ioctl_errno = errno;
115 if (print_error == PRINT_ERROR)
116 fprintf(stderr, "ioctl on %s: %s\n", ifname, strerror(errno));
126 int if_exists(const char *iface) {
128 return !do_socket_ioctl(iface, SIOCGIFFLAGS, &r, NULL, PRINT_NO_ERROR);
131 #if defined(__linux__)
133 void if_flags(const char *iface) {
141 { IFF_BROADCAST, "Broadcast" },
142 { IFF_DEBUG, "Debugging" },
143 { IFF_LOOPBACK, "Loopback" },
144 { IFF_POINTOPOINT, "Ppp" },
145 { IFF_NOTRAILERS, "No-trailers" },
146 { IFF_RUNNING, "Running" },
147 { IFF_NOARP, "No-arp" },
148 { IFF_PROMISC, "Promiscuous" },
149 { IFF_ALLMULTI, "All-multicast" },
150 { IFF_MASTER, "Load-master" },
151 { IFF_SLAVE, "Load-slave" },
152 { IFF_MULTICAST, "Multicast" },
153 { IFF_PORTSEL, "Port-select" },
154 { IFF_AUTOMEDIA, "Auto-detect" },
155 { IFF_DYNAMIC, "Dynaddr" },
156 { 0xffff0000, "Unknown-flags" },
159 if (do_socket_ioctl(iface, SIOCGIFFLAGS, &r, NULL, PRINT_ERROR))
162 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++)
163 printf("%s%s%s", (r.ifr_flags & flags[i].flag) ? "On " : "Off ",
165 sizeof(flags) / sizeof(flags[0]) - 1 == i ? "" : "\n");
168 void if_hwaddr(const char *iface) {
170 unsigned char *hwaddr;
172 if (do_socket_ioctl(iface, SIOCGIFHWADDR, &r, NULL, PRINT_ERROR))
175 hwaddr = (unsigned char *)r.ifr_hwaddr.sa_data;
176 printf("%02X:%02X:%02X:%02X:%02X:%02X",
177 hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
182 static struct sockaddr *if_addr_value(const char *iface, struct ifreq *r,
183 unsigned long int request) {
186 if (do_socket_ioctl(iface, request, r, &e, PRINT_NO_ERROR)) {
187 if (e == EADDRNOTAVAIL)
194 struct sockaddr *if_addr(const char *iface, struct ifreq *r) {
195 return if_addr_value(iface, r, SIOCGIFADDR);
198 struct sockaddr *if_mask(const char *iface, struct ifreq *r) {
199 return if_addr_value(iface, r, SIOCGIFNETMASK);
202 struct sockaddr *if_bcast(const char *iface, struct ifreq *r) {
203 return if_addr_value(iface, r, SIOCGIFBRDADDR);
206 struct sockaddr *if_network(const char *iface) {
207 struct sockaddr *saddr;
208 static struct ifreq req;
211 if (!(saddr = if_mask(iface, &req)))
214 mask = ((struct sockaddr_in*)saddr)->sin_addr.s_addr;
216 if (!(saddr = if_addr(iface, &req)))
219 ((struct sockaddr_in*)saddr)->sin_addr.s_addr &= mask;
223 int if_mtu(const char *iface) {
224 static struct ifreq req;
226 if (do_socket_ioctl(iface, SIOCGIFMTU, &req, NULL, PRINT_ERROR))
232 #if defined(__linux__)
234 static void skipline(FILE *fd) {
238 } while (ch != '\n' && ch != EOF);
241 struct if_stat *get_stats(const char *iface) {
243 struct if_stat *ifstat;
246 if (!(ifstat = malloc(sizeof(struct if_stat)))) {
251 if ((fd = fopen("/proc/net/dev", "r")) == NULL) {
252 perror("fopen(\"/proc/net/dev\")");
262 int items = fscanf(fd,
263 " %20[^:]:%llu %llu %llu %llu %llu %llu %llu %llu "
264 "%llu %llu %llu %llu %llu %llu %llu %llu",
266 &ifstat->in_bytes, &ifstat->in_packets,
267 &ifstat->in_errors, &ifstat->in_drops,
268 &ifstat->in_fifo, &ifstat->in_frame,
269 &ifstat->in_compress, &ifstat->in_multicast,
270 &ifstat->out_bytes, &ifstat->out_packets,
271 &ifstat->out_errors, &ifstat->out_drops,
272 &ifstat->out_fifo, &ifstat->out_colls,
273 &ifstat->out_carrier, &ifstat->out_carrier
279 fprintf(stderr, "Invalid data read, check!\n");
283 if (!strncmp(name, iface, sizeof(name))) {
299 unsigned int is_stat;
302 { "-e", DO_EXISTS, 0, "Reports interface existence via return code" },
303 { "-p", DO_PALL, 0, "Print out the whole config of iface" },
304 { "-pe", DO_PEXISTS, 0, "Print out yes or no according to existence" },
305 { "-pa", DO_PADDRESS, 0, "Print out the address" },
306 { "-pn", DO_PMASK, 0, "Print netmask" },
307 { "-pN", DO_PNETWORK, 0, "Print network address" },
308 { "-pb", DO_PCAST, 0, "Print broadcast" },
309 { "-pm", DO_PMTU, 0, "Print mtu" },
310 #if defined(__linux__)
311 { "-ph", DO_PHWADDRESS, 0, "Print out the hardware address" },
312 { "-pf", DO_PFLAGS, 0, "Print flags" },
313 { "-si", DO_SINALL, 1, "Print all statistics on input" },
314 { "-sip", DO_SINPACKETS, 1, "Print # of in packets" },
315 { "-sib", DO_SINBYTES, 1, "Print # of in bytes" },
316 { "-sie", DO_SINERRORS, 1, "Print # of in errors" },
317 { "-sid", DO_SINDROPS, 1, "Print # of in drops" },
318 { "-sif", DO_SINFIFO, 1, "Print # of in fifo overruns" },
319 { "-sic", DO_SINCOMPRESSES, 1, "Print # of in compress" },
320 { "-sim", DO_SINMULTICAST, 1, "Print # of in multicast" },
321 { "-so", DO_SOUTALL, 1, "Print all statistics on output" },
322 { "-sop", DO_SOUTPACKETS, 1, "Print # of out packets" },
323 { "-sob", DO_SOUTBYTES, 1, "Print # of out bytes" },
324 { "-soe", DO_SOUTERRORS, 1, "Print # of out errors" },
325 { "-sod", DO_SOUTDROPS, 1, "Print # of out drops" },
326 { "-sof", DO_SOUTFIFO, 1, "Print # of out fifo overruns" },
327 { "-sox", DO_SOUTCOLLS, 1, "Print # of out collisions" },
328 { "-soc", DO_SOUTCARRIER, 1, "Print # of out carrier loss" },
329 { "-som", DO_SOUTMULTICAST, 1, "Print # of out multicast" },
330 { "-bips",DO_BIPS, 1, "Print # of incoming bytes per second" },
331 { "-bops",DO_BOPS, 1, "Print # of outgoing bytes per second" },
335 void usage(const char *name) {
338 fprintf(stderr, "Usage: %s [options] iface\n", name);
339 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) {
340 fprintf(stderr, " %5s %s\n",
341 options[i].option, options[i].description);
345 void add_do(int *ndo, int **todo, int act) {
346 *todo = realloc(*todo, (*ndo+1) * sizeof(int));
351 static void print_addr(struct sockaddr *sadr) {
353 fprintf(stderr, "Error\n");
359 struct if_stat *ifstats, *ifstats2 = NULL;
361 void please_do(int ndo, int *todo, const char *ifname) {
363 static struct ifreq req;
365 // printf("I have %d items in my queue.\n",ndo);
366 for (i=0; i<ndo; i++) {
369 exit(!if_exists(ifname));
371 printf("%s", if_exists(ifname) ? "yes" : "no");
374 print_addr(if_addr(ifname, &req));
376 #if defined(__linux__)
385 print_addr(if_mask(ifname, &req));
388 print_addr(if_bcast(ifname, &req));
391 printf("%d", if_mtu(ifname));
394 print_addr(if_network(ifname));
397 print_addr(if_addr(ifname, &req));
399 print_addr(if_mask(ifname, &req));
401 print_addr(if_bcast(ifname, &req));
403 printf("%d", if_mtu(ifname));
405 #if defined(__linux__)
407 printf("%llu",ifstats->in_packets);
410 printf("%llu",ifstats->in_bytes);
413 printf("%llu",ifstats->in_errors);
416 printf("%llu",ifstats->in_drops);
419 printf("%llu",ifstats->in_fifo);
422 printf("%llu",ifstats->in_frame);
424 case DO_SINCOMPRESSES:
425 printf("%llu",ifstats->in_compress);
427 case DO_SINMULTICAST:
428 printf("%llu",ifstats->in_multicast);
431 printf("%llu %llu %llu %llu %llu %llu %llu %llu",
432 ifstats->in_bytes, ifstats->in_packets,
433 ifstats->in_errors, ifstats->in_drops,
434 ifstats->in_fifo, ifstats->in_frame,
435 ifstats->in_compress, ifstats->in_multicast);
438 printf("%llu",ifstats->out_bytes);
441 printf("%llu",ifstats->out_packets);
444 printf("%llu",ifstats->out_errors);
447 printf("%llu",ifstats->out_drops);
450 printf("%llu",ifstats->out_fifo);
453 printf("%llu",ifstats->out_colls);
456 printf("%llu",ifstats->out_carrier);
458 case DO_SOUTMULTICAST:
459 printf("%llu",ifstats->out_multicast);
462 if (ifstats2 == NULL) {
464 ifstats2 = get_stats(ifname);
466 printf("%llu", ifstats2->in_bytes-ifstats->in_bytes);
469 if (ifstats2 == NULL) {
471 ifstats2 = get_stats(ifname);
473 printf("%llu", ifstats2->out_bytes-ifstats->out_bytes);
476 printf("%llu %llu %llu %llu %llu %llu %llu %llu",
477 ifstats->out_bytes, ifstats->out_packets,
478 ifstats->out_errors, ifstats->out_drops,
479 ifstats->out_fifo, ifstats->out_colls,
480 ifstats->out_carrier, ifstats->out_multicast);
484 printf("Unknown command: %d", todo[i]);
491 int main(int argc, char *argv[]) {
497 unsigned int i, found;
504 while (narg < argc - 1) {
509 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) {
510 if (!strcmp(argv[narg], options[i].option)) {
511 add_do(&ndo, &todo, options[i].flag);
512 do_stats |= options[i].is_stat;
521 if (argv[narg][0] == '-') {
531 if (narg + 1 < argc || !ifname) {
536 #if defined(__linux__)
537 if (do_stats && (ifstats = get_stats(ifname)) == NULL) {
538 fprintf(stderr, "Error getting statistics for %s\n", ifname);
543 please_do(ndo, todo, ifname);