-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp.bk
128 lines (103 loc) · 3.21 KB
/
main.cpp.bk
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <string.h>
#include <string>
#include <iostream>
#include "PracticalSocket.h" // For UDPSocket and SocketException
using namespace std;
typedef struct PacketCnt_t{
struct pcap_pkthdr pcap_hdr;
u_char pcap_pkt[];
}PacketCnt;
/*
* workhorse function, we will be modifying this function
*/
void pcap_callback(u_char *userdata, const struct pcap_pkthdr *h, const u_char *p) {
unsigned char* packet; // ponter to packet binary
unsigned char* l3_header;
struct ip* ip_header;
struct in_addr src_ip;
struct in_addr dst_ip;
struct timeval timestamp;
PacketCnt *pcnt;
pcnt = (PacketCnt *)malloc(sizeof(PacketCnt) + h->caplen);
memcpy(&(pcnt->pcap_hdr), h, sizeof(struct pcap_pkthdr));
memcpy(pcnt->pcap_pkt, p, h->caplen);
// timestamp = packet_cnt->pcap_hdr.ts;
packet = (unsigned char *)malloc(h->caplen);
memcpy(packet, pcnt->pcap_pkt, h->caplen);
l3_header = packet + sizeof(struct ether_header); //IP header
ip_header = (struct ip *)l3_header;
src_ip = ip_header->ip_src;
dst_ip = ip_header->ip_dst;
cout << "src_ip: " << inet_ntoa(src_ip) << endl;
cout << "content: "<< pcnt->pcap_pkt << endl;
}
int main(int argc,char **argv)
{
char *dev;
//char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
struct bpf_program fp; /* hold compiled program */
bpf_u_int32 maskp; /* subnet mask */
bpf_u_int32 netp; /* ip */
u_char* args = NULL;
static const int iterate = -1;
char ebuf[PCAP_ERRBUF_SIZE];
pcap_t *pd = NULL;
cout << "goehogeh" << endl;
cout << argv[1] << endl;
pd = pcap_open_offline(argv[1], ebuf);
if (pcap_loop(pd, iterate, pcap_callback, NULL) < 0) {
(void)fprintf(stderr, "pcap_loop: error occurred\n");
exit(1);
}
pcap_close(pd);
//
// /* Options must be passed in as a string because I am lazy */
// if(argc < 2){
// fprintf(stdout,"Usage: %s numpackets \"options\"\n",argv[0]);
// return 0;
// }
//
// /* grab a device to peak into... */
// dev = pcap_lookupdev(errbuf);
// if(dev == NULL)
// { printf("%s\n",errbuf); exit(1); }
//
// /* ask pcap for the network address and mask of the device */
// pcap_lookupnet(dev,&netp,&maskp,errbuf);
//
// /* open device for reading. NOTE: defaulting to
// * promiscuous mode*/
// descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
// if(descr == NULL)
// { printf("pcap_open_live(): %s\n",errbuf); exit(1); }
//
//
// if(argc > 2)
// {
// /* Lets try and compile the program.. non-optimized */
// if(pcap_compile(descr,&fp,argv[2],0,netp) == -1)
// { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }
//
// /* set the compiled program as the filter */
// if(pcap_setfilter(descr,&fp) == -1)
// { fprintf(stderr,"Error setting filter\n"); exit(1); }
// }
//
// /* ... and loop */
// pcap_loop(descr,atoi(argv[1]),my_callback,args);
//
// fprintf(stdout,"\nfinished\n");
// return 0;
}