-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPHeader.cpp
More file actions
142 lines (116 loc) · 2.67 KB
/
IPHeader.cpp
File metadata and controls
142 lines (116 loc) · 2.67 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
#include "stdafx.h"
#include "IPHeader.h"
Yellow::IPHeader::IPHeader(void * begin)
: buff(static_cast<unsigned char *>(begin)), ip(static_cast<struct iphdr *>(begin))
{
memset(&sourceaddr, 0, sizeof(sourceaddr));
sourceaddr.sin_addr.s_addr = ip->saddr;
source = inet_ntoa(sourceaddr.sin_addr);
memset(&destaddr, 0, sizeof(destaddr));
destaddr.sin_addr.s_addr = ip->daddr;
dest = inet_ntoa(destaddr.sin_addr);
}
Yellow::IPHeader::IPHeader(const IPHeader &i)
{
buff = i.buff;
ip = i.ip;
}
Yellow::IPHeader & Yellow::IPHeader::operator=(const IPHeader &i)
{
buff = i.buff;
ip = i.ip;
return *this;
}
Yellow::IPHeader::~IPHeader()
{
}
void Yellow::IPHeader::setVersion(unsigned int ver)
{
ip->version = ver;
}
const unsigned int Yellow::IPHeader::getVersion() const
{
return ip->version;
}
void Yellow::IPHeader::setHeaderLenght(unsigned int nbWord)
{
ip->ihl = nbWord / 4;
}
const unsigned int Yellow::IPHeader::getHeaderLenght() const
{
return (ip->ihl * 4);
}
void Yellow::IPHeader::setTypeOfService(unsigned int tos)
{
ip->tos = tos;
}
const unsigned int Yellow::IPHeader::getTypeOfService() const
{
return (ip->tos);
}
void Yellow::IPHeader::setTotalLenght(unsigned short int tot)
{
ip->tot_len = htons(tot);
}
const unsigned short int Yellow::IPHeader::getTotalLenght() const
{
return ntohs(ip->tot_len);
}
void Yellow::IPHeader::setIdentification(unsigned short int id)
{
ip->id = htons(id);
}
const unsigned short Yellow::IPHeader::getIndentification() const
{
return ntohs(ip->id);
}
void Yellow::IPHeader::setTTL(unsigned int ttl)
{
ip->ttl = ttl;
}
const unsigned int Yellow::IPHeader::getTTL() const
{
return ip->ttl;
}
void Yellow::IPHeader::setProtocol(unsigned int proto)
{
ip->protocol = proto;
}
const unsigned int Yellow::IPHeader::getProtocol() const
{
return ip->protocol;
}
void Yellow::IPHeader::setChecksum(unsigned short int check)
{
ip->check = htons(check);
}
const unsigned short int Yellow::IPHeader::getChecksum() const
{
return ntohs(ip->check);
}
void Yellow::IPHeader::setSourceIP(const std::string &addr)
{
inet_aton(addr.c_str(), &sourceaddr.sin_addr);
ip->saddr = sourceaddr.sin_addr.s_addr;
source = addr;
}
const std::string &Yellow::IPHeader::getSourceIP() const
{
return source;
}
void Yellow::IPHeader::setDestIP(const std::string &addr)
{
inet_aton(addr.c_str(), &destaddr.sin_addr);
ip->daddr = destaddr.sin_addr.s_addr;
dest = addr;
}
const std::string &Yellow::IPHeader::getDestinationIP() const
{
return dest;
}
const unsigned char * Yellow::IPHeader::operator[](int idx) const
{
if (idx >= getHeaderLenght())
return NULL;
return &static_cast<const unsigned char *>(buff)[idx];
}