Skip to content

Instantly share code, notes, and snippets.

@neeraj9
Created March 29, 2018 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neeraj9/00d69229253b1a08114179e345c45e1e to your computer and use it in GitHub Desktop.
Save neeraj9/00d69229253b1a08114179e345c45e1e to your computer and use it in GitHub Desktop.
Simple Bandwidth monitoring in Gnu Linux
/*
* Copyright (c) 2015 Neeraj Sharma. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h> /* strncpy() */
#include <stropts.h> /* ioctl() */
#include <sys/socket.h> /* socket() and friends */
#include <net/if.h> /* struct ifreq */
#include <sys/ioctl.h> /* SIOCGIFFLAGS and friends */
#include <linux/if_ether.h> /* ETH_P_ALL */
#define MAX_PACKET_CAPTURE 1000
#define MAX_BUFFER_SIZE 2048 /* lets not read jumbo frames */
int set_in_promiscuous_mode(const char *ifname, int sockfd)
{
struct ifreq ifrq = {0};
int ret = 0;
strncpy(ifrq.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sockfd, SIOCGIFFLAGS, &ifrq);
ifrq.ifr_flags |= IFF_PROMISC;
ret = ioctl(sockfd, SIOCSIFFLAGS, &ifrq);
return ret;
}
int bind_sock_to_dev(const char *ifname, int sockfd)
{
struct ifreq ifrq = {0};
void *opaque = &ifrq;
int ret = 0;
strncpy(ifrq.ifr_name, ifname, IFNAMSIZ);
ret = setsockopt(
sockfd, SOL_SOCKET, SO_BINDTODEVICE, opaque, sizeof(ifrq));
return ret;
}
int main(int argc, char *argv[])
{
int sockfd = -1;
char *ifname = NULL;
int ret = 0;
ssize_t octets;
int packet_count = 0;
const int buffer_len = MAX_BUFFER_SIZE;
char buffer[buffer_len];
if (argc != 2) {
fprintf(stderr, "need the interface name as an argument\n");
return 1;
}
/* lets open a raw socket to read even the ethernet headers */
sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
ret = set_in_promiscuous_mode(argv[1], sockfd);
if (ret < 0) {
fprintf(stderr, "cannot set interface %s in promiscuous mode\n",
argv[1]);
return 2;
}
/* Note that the loop can be run in while(1) for
* infinite monitoring to as deemed useful.
*/
while (packet_count < MAX_PACKET_CAPTURE) {
octets = recv(sockfd, buffer, buffer_len, 0);
if (octets < 0) {
break;
}
/* process the packet and derive bandwidth information along with
* traffic flow or any other complex statistics based on deep
* packet inspection.
*/
++packet_count;
}
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment