-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanalysis.cpp
More file actions
81 lines (68 loc) · 1.6 KB
/
analysis.cpp
File metadata and controls
81 lines (68 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//============================================================================
// Name : analysis.cpp
// Author : OM
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
<<<<<<< Updated upstream
//analyzing the numbe rof packets transfered
=======
//Analyse the packets transfered omkar
>>>>>>> Stashed changes
#include <bits/stdc++.h>
#include<pcap.h>
#include <arpa/inet.h>
#include<net/ethernet.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
using namespace std;
int ethernet=0,udp=0,tcp=0,ip=0;
void packetHandler(u_char* userdata,const struct pcap_pkthdr* pkthdr,const u_char* packet)
{
const struct ether_header *etherheader;
const struct ip* ipHeader;
etherheader=(struct ether_header*)packet;
ethernet++;
if(ntohs(etherheader->ether_type)==ETHERTYPE_IP)
{
ip++;
ipHeader=(struct ip*)(packet + sizeof(struct ether_header));
if(ipHeader->ip_p==IPPROTO_TCP)
{
tcp++;
}
if(ipHeader->ip_p==IPPROTO_UDP)
{
udp++;
}
}
}
int main(int argc,char* argv[]) {
pcap_t *desc;
char errbuff[PCAP_ERRBUF_SIZE];
if(argc<2)
{
cout<<"Enter filename";
exit(1);
}
desc=pcap_open_offline(argv[1],errbuff);
if(desc==NULL)
{
cout<<"Error in opening file\n";
cout<<errbuff<<endl;
exit(1);
}
//start loop
if(pcap_loop()desc,0,packetHandler,NULL)<0)
{
cout<<"Loop failed";
exit(1);
}
cout<<"Ethernet "<< ethernet<<endl;
cout<<"TCP "<< tcp<<endl;
cout<<"UDP "<< udp<<endl;
cout<<"IP "<< ip<<endl;
return 0;
}