Skip to content

Instantly share code, notes, and snippets.

@atr000
Created January 6, 2010 03:38
Show Gist options
  • Select an option

  • Save atr000/269986 to your computer and use it in GitHub Desktop.

Select an option

Save atr000/269986 to your computer and use it in GitHub Desktop.
// works on osx!
// from this nice developer: http://blog.loudhush.ro/2009/03/network-statistics-on-os-x-using-sysctl.html
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socketvar.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcp_var.h>
int main()
{
void * oldp = malloc(1024);
size_t oldlen = sizeof(struct ipstat), newlen;
void * newp = NULL;
int retval = sysctlbyname("net.inet.ip.stats", oldp, &oldlen, newp, newlen);
struct ipstat * g = (struct ipstat *) oldp;
printf("IP packets received: %d\n", g->ips_total);
printf("IP packets generated here: %d\n", g->ips_localout);
struct tcpstat * t = (struct tcpstat *) oldp;
oldlen = sizeof(struct tcpstat);
retval = sysctlbyname("net.inet.tcp.stats", oldp, &oldlen, newp, newlen);
printf("TCP connection attempts: %d\n", t->tcps_connattempt);
printf("TCP total packets sent: %d\n", t->tcps_sndtotal);
printf("TCP total packets received: %d\n", t->tcps_rcvtotal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment