Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2019 Michal Kalewski <mkalewski at cs.put.poznan.pl>
- *
- * Compilation: gcc -Wall ./pcapsniff.c -o ./pcapsniff -lpcap
- * Usage: ./pcapsniff INTERFACE
- * NOTE: This program requires root privileges.
- *
- * Bug reports: https://gitlab.cs.put.poznan.pl/mkalewski/ps-2019/issues
- *
- */
- #include <pcap.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <string.h>
- #include <linux/if_ether.h>
- #include <linux/ip.h>
- #include <linux/types.h>
- char* errbuf;
- pcap_t* handle;
- struct stats {
- long arp;
- long ip;
- long ip_udp;
- long ip_tcp;
- long inne;
- };
- struct stats staty;
- void resetStaty(){
- staty.arp = 0;
- staty.ip = 0;
- staty.ip_udp = 0;
- staty.ip_tcp = 0;
- staty.inne = 0;
- }
- void printStats() {
- printf("Statystyki: \n");
- printf("\tArp:\t%d\n", staty.arp);
- printf("\tIP:\t%d\n", staty.ip);
- printf("\t\tUDP:\t%d\n", staty.ip_udp);
- printf("\t\tTCP:\t%d\n", staty.ip_tcp);
- printf("\tInne:\t%d\n", staty.inne);
- }
- void cleanup() {
- pcap_close(handle);
- free(errbuf);
- }
- void stop(int signo) {
- // print stats
- exit(EXIT_SUCCESS);
- }
- void trap(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) {
- struct ethhdr *ramka = (struct ethhdr*) bytes;
- int etherType = ntohs(ramka-> h_proto);
- if(ETH_P_ARP == etherType) {
- staty.arp++;
- } else if (ETH_P_IP == etherType ){
- staty.ip++;
- struct iphdr *ipRamka = (struct iphdr*) (sizeof(struct ethhdr)+ ramka);
- if(17 == ipRamka->protocol){
- staty.ip_udp++;
- } else if(6 == ipRamka->protocol){
- staty.ip_tcp++;
- }
- free(ipRamka);
- } else {
- staty.inne++;
- }
- free(ramka);
- // struct ethhdr = ((struct ethhdr*) bytes);
- // ETH_P_ARP
- // ETH_P_IP
- printf("[%dB of %dB]\n", h->caplen, h->len);
- }
- int main(int argc, char** argv) {
- atexit(cleanup);
- signal(SIGINT, stop);
- errbuf = malloc(PCAP_ERRBUF_SIZE);
- handle = pcap_create(argv[1], errbuf);
- pcap_set_promisc(handle, 1);
- pcap_set_snaplen(handle, 65535);
- pcap_activate(handle);
- pcap_loop(handle, -1, trap, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement