-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.cpp
More file actions
179 lines (145 loc) · 4.25 KB
/
packet.cpp
File metadata and controls
179 lines (145 loc) · 4.25 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "packet.h"
#include <cassert>
#include <endian.h>
packet::packet(const u_char * p, struct timeval ts, unsigned int cap_len) {
using namespace std;
struct ip *mip;
struct tcphdr *mtcp;
unsigned int eth_header_length = sizeof(struct ether_header);
unsigned int ip_header_length;
unsigned int tcp_header_length;
this->p_string = ustring(p);
this->time_stamp_sec = (time_t)ts.tv_sec;
this->time_stamp_milli = ts.tv_usec;
this->capture_length = cap_len;
if (too_short(eth_header_length)) {
cerr << "Captured packet too small: ethhdr." << endl;
} else {
// jump over ethernet header
p += eth_header_length;
cap_len -= eth_header_length;
mip = (struct ip*)p;
// rip addresses out of ip header
this->saddr = string(inet_ntoa(mip->ip_src));
this->daddr = string(inet_ntoa(mip->ip_dst));
assert(this->saddr != this->daddr);
ip_header_length = mip->ip_hl * 4;
assert(ip_header_length >= 20 && ip_header_length <= 60);
this->ip_hdr_len = ip_header_length;
if (too_short(ip_header_length)) {
cerr << "Captured packet too small: iphdr." << endl;
} else {
// jump over ip header to the tcp header
p += ip_header_length;
cap_len -= ip_header_length;
mtcp = (struct tcphdr*)p;
tcp_header_length = mtcp->th_off * 4;
this->tcp_hdr_len = tcp_header_length;
if (too_short(tcp_header_length)) {
cerr << "Captured packet too small: tcphdr." << endl;
} else {
// rip data out of the tcp header
// TODO: this is converting big endian to little endian.
this->sport = short_swap(mtcp->th_sport);
this->dport = short_swap(mtcp->th_dport);
this->ack_num = be32toh(mtcp->th_ack);
this->seq_num = be32toh(mtcp->th_seq);
this->win = short_swap(mtcp->th_win);
this->flags = std::bitset<8>((int)mtcp->th_flags);
// jump over the tcp header
p += tcp_header_length;
cap_len -= tcp_header_length;
this->d_size = cap_len;
this->data = ustring((u_char*)p);
//assert(d_size == this->data.length());
//cout << this->data.c_str() << endl;
this->completed = true;
}
}
}
}
ustring packet::get_data() const {
if (!completed) throw bad_packet_error("get_data");
return data;
}
bool packet::too_short(unsigned int length) {
if (capture_length < length) return true;
return false;
}
std::string packet::src_addr() const {
if (!completed) throw bad_packet_error("src_addr");
return saddr;
}
std::string packet::dst_addr() const {
if (!completed) throw bad_packet_error("dst_addr");
return daddr;
}
u_short packet::dst_port() const {
if (!completed) throw bad_packet_error("dst_port");
return dport;
}
u_short packet::src_port() const {
if (!completed) throw bad_packet_error("src_port");
return sport;
}
bool packet::fin() const {
return (bool)flags[0];
}
bool packet::syn() const {
return (bool)flags[1];
}
bool packet::rst() const {
return (bool)flags[2];
}
bool packet::ack() const {
return (bool)flags[4];
}
tcp_seq packet::ack_number() const {
return ack_num;
}
tcp_seq packet::seq_number() const {
return seq_num;
}
time_t packet::ts_sec() const {
return time_stamp_sec;
}
suseconds_t packet::ts_milli() const {
return time_stamp_milli;
}
suseconds_t packet::ts() const {
return this->ts_milli() + this->ts_sec()*1000000;
}
unsigned int packet::data_size() const {
return this->d_size;
}
u_short packet::window_size() {
return this->win;
}
std::ostream& operator<<(std::ostream& os, const packet& p) {
os << " src_addr: " << p.src_addr()
<< " dst_addr: " << p.dst_addr()
//<< " src_port: " << p.src_port()
//<< " dst_port: " << p.dst_port()
//<< " ack_num: " << (p.ack_num)
//<< " seq_num: " << (p.seq_num)
//<< " ack: " << p.ack()
//<< " syn: " << p.syn()
<< " win size: " << p.win
<< " has data: " << p.has_data;
if (p.has_data) {
os << " data size: " << p.data_size();
}
return os;
}
u_short short_swap( u_short s ) {
unsigned char b1, b2;
b1 = s & 255;
b2 = (s >> 8) & 255;
return (b1 << 8) + b2;
}
void ConvertToBinary(int n) {
if (n / 2 != 0) {
ConvertToBinary(n / 2);
}
printf("%d", n % 2);
}