-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (62 loc) · 2.06 KB
/
main.cpp
File metadata and controls
65 lines (62 loc) · 2.06 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
#include "ifscanner.h"
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
int out_func_tcp (Buffer *bufrec, Buffer *indata, Buffer *outdata)
{
char* buf = bufrec->buff;
struct in_addr saddr,daddr;
struct iphdr *iph = (struct iphdr *)buf;
struct tcphdr *tcph = (struct tcphdr *) (buf + sizeof(struct iphdr));
saddr.s_addr=iph->saddr;
daddr.s_addr=iph->daddr;
std::cout<<"TCP\nSize: "<<bufrec->buffsize<<std::endl;
std::cout<<"From: "<<inet_ntoa(saddr)<<"\tTO: "<<inet_ntoa(daddr)<<std::endl;
std::cout<<"ID: "<<iph->id<<"\tTTL: "<<(int)iph->ttl<<std::endl;
std::cout<<"Source Port: "<<ntohs(tcph->th_sport)<<"\tDest Port: "<<ntohs(tcph->th_dport)<<std::endl;
std::cout<<"Sequence: "<<tcph->th_seq<<"\tAck: "<<tcph->th_ack<<std::endl;
return 0;
}
int out_func_udp (Buffer *bufrec, Buffer *indata, Buffer *outdata)
{
char* buf = bufrec->buff;
struct in_addr saddr,daddr;
struct iphdr *iph = (struct iphdr *)buf;
struct udphdr *udph = (struct udphdr *) (buf + sizeof(struct iphdr));
saddr.s_addr=iph->saddr;
daddr.s_addr=iph->daddr;
std::cout<<"UDP\nSize: "<<bufrec->buffsize<<std::endl;
std::cout<<"From: "<<inet_ntoa(saddr)<<"\tTO: "<<inet_ntoa(daddr)<<std::endl;
std::cout<<"ID: "<<iph->id<<"\tTTL: "<<(int)iph->ttl<<std::endl;
std::cout<<"Source port: "<<ntohs(udph->uh_sport)<<"\tDest Port: "<<ntohs(udph->uh_dport)<<std::endl;
return 0;
}
int main(int argc,char **argv)
{
int prot1,prot2;
InterfaceScanner ifsc1("eth0",1);
prot1 = ifsc1.add_protocol(IPPROTO_TCP);
prot2 = ifsc1.add_protocol(IPPROTO_UDP);
ifsc1.set_callback(prot1,out_func_tcp);
ifsc1.set_callback(prot2,out_func_udp);
ifsc1.set_max_buf(prot1,10000);
ifsc1.set_max_buf(prot2,10000);
ifsc1.launch_all();
bool ext = 0;
std::vector<std::string> log;
log = ifsc1.get_error_log();
for (size_t i = 0; i < log.size(); i++)
std::cout<<log.at(i)<<std::endl;
log = ifsc1.get_log();
for (size_t i = 0; i < log.size(); i++)
std::cout<<log.at(i)<<std::endl;
while (!ext)
{
std::cin>>ext;
}
ifsc1.clean_all();
return 0;
}