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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 | // Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <asiolink/io_address.h>
#include <dhcp/pkt4.h>
#include <dhcp/tests/pkt_filter_test_utils.h>
using namespace isc::asiolink;
using namespace boost::posix_time;
namespace isc {
namespace dhcp {
namespace test {
PktFilterTest::PktFilterTest(const uint16_t port)
: port_(port),
sock_info_(isc::asiolink::IOAddress("127.0.0.1"), port, -1, -1),
send_msg_sock_(-1),
start_time_(PktEvent::now()) {
// Initialize ifname_ and ifindex_.
loInit();
// Initialize test_message_.
initTestMessage();
}
PktFilterTest::~PktFilterTest() {
// Cleanup after each test. This guarantees
// that the sockets do not hang after a test.
if (sock_info_.sockfd_ >= 0) {
close(sock_info_.sockfd_);
}
if (sock_info_.fallbackfd_ >=0) {
close(sock_info_.fallbackfd_);
}
if (send_msg_sock_ >= 0) {
close(send_msg_sock_);
}
}
void
PktFilterTest::initTestMessage() {
// Let's create a DHCPv4 message instance.
test_message_.reset(new Pkt4(DHCPOFFER, 0));
// Set required fields.
test_message_->setLocalAddr(IOAddress("127.0.0.1"));
test_message_->setRemoteAddr(IOAddress("127.0.0.1"));
test_message_->setRemotePort(port_);
test_message_->setLocalPort(port_ + 1);
test_message_->setIndex(ifindex_);
test_message_->setIface(ifname_);
test_message_->setHops(6);
test_message_->setSecs(42);
test_message_->setCiaddr(IOAddress("192.0.2.1"));
test_message_->setSiaddr(IOAddress("192.0.2.2"));
test_message_->setYiaddr(IOAddress("192.0.2.3"));
test_message_->setGiaddr(IOAddress("192.0.2.4"));
try {
test_message_->pack();
} catch (const isc::Exception& ex) {
ADD_FAILURE() << "failed to create test message for PktFilterTest";
}
}
void
PktFilterTest::loInit() {
if (if_nametoindex("lo") > 0) {
ifname_ = "lo";
ifindex_ = if_nametoindex("lo");
} else if (if_nametoindex("lo0") > 0) {
ifname_ = "lo0";
ifindex_ = if_nametoindex("lo0");
} else {
std::cout << "Failed to detect loopback interface. Neither "
<< "lo nor lo0 worked. Giving up." << std::endl;
FAIL();
}
}
void
PktFilterTest::sendMessage(const IOAddress& dest) {
// Packet will be sent over loopback interface.
Iface iface(ifname_, ifindex_);
IOAddress addr("127.0.0.1");
struct sockaddr_in addr4;
memset(&addr4, 0, sizeof(sockaddr));
addr4.sin_family = AF_INET;
addr4.sin_port = htons(port_ + 1);
send_msg_sock_ = socket(AF_INET, SOCK_DGRAM, 0);
ASSERT_GE(send_msg_sock_, 0);
ASSERT_GE(bind(send_msg_sock_, (struct sockaddr *)&addr4,<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
sizeof(addr4)), 0);
struct sockaddr_in dest_addr4;
memset(&dest_addr4, 0, sizeof(sockaddr));
dest_addr4.sin_family = AF_INET;
dest_addr4.sin_port = htons(port_);
dest_addr4.sin_addr.s_addr = htonl(dest.toUint32());
ASSERT_EQ(sendto(send_msg_sock_, test_message_->getBuffer().getData(),
test_message_->getBuffer().getLength(), 0,
reinterpret_cast<struct sockaddr*>(&dest_addr4),
sizeof(sockaddr)), test_message_->getBuffer().getLength());
close(send_msg_sock_);
send_msg_sock_ = -1;
}
void
PktFilterTest::testDgramSocket(const int sock) const {
// Check that socket has been opened.
ASSERT_GE(sock, 0);
// Verify that the socket belongs to AF_INET family.
sockaddr_in sock_address;
socklen_t sock_address_len = sizeof(sock_address);
ASSERT_EQ(0, getsockname(sock,
reinterpret_cast<sockaddr*>(&sock_address),
&sock_address_len));
EXPECT_EQ(AF_INET, sock_address.sin_family);
// Verify that the socket is bound the appropriate address.
const std::string bind_addr(inet_ntoa(sock_address.sin_addr));
EXPECT_EQ("127.0.0.1", bind_addr);
// Verify that the socket is bound to appropriate port.
EXPECT_EQ(port_, ntohs(sock_address.sin_port));
// Verify that the socket has SOCK_DGRAM type.
int sock_type;
socklen_t sock_type_len = sizeof(sock_type);
ASSERT_EQ(0, getsockopt(sock, SOL_SOCKET, SO_TYPE,
&sock_type, &sock_type_len));
EXPECT_EQ(SOCK_DGRAM, sock_type);
}
void
PktFilterTest::testRcvdMessage(const Pkt4Ptr& rcvd_msg) const {
EXPECT_EQ(test_message_->getHops(), rcvd_msg->getHops());
EXPECT_EQ(test_message_->getOp(), rcvd_msg->getOp());
EXPECT_EQ(test_message_->getSecs(), rcvd_msg->getSecs());
EXPECT_EQ(test_message_->getFlags(), rcvd_msg->getFlags());
EXPECT_EQ(test_message_->getCiaddr(), rcvd_msg->getCiaddr());
EXPECT_EQ(test_message_->getSiaddr(), rcvd_msg->getSiaddr());
EXPECT_EQ(test_message_->getYiaddr(), rcvd_msg->getYiaddr());
EXPECT_EQ(test_message_->getGiaddr(), rcvd_msg->getGiaddr());
EXPECT_EQ(test_message_->getTransid(), rcvd_msg->getTransid());
EXPECT_TRUE(test_message_->getSname() == rcvd_msg->getSname());
EXPECT_TRUE(test_message_->getFile() == rcvd_msg->getFile());
EXPECT_EQ(test_message_->getHtype(), rcvd_msg->getHtype());
EXPECT_EQ(test_message_->getHlen(), rcvd_msg->getHlen());
}
void
PktFilterTest::testRcvdMessageAddressPort(const Pkt4Ptr& rcvd_msg) const {
EXPECT_EQ(test_message_->getRemoteAddr(), rcvd_msg->getLocalAddr());
EXPECT_EQ(test_message_->getLocalAddr(), rcvd_msg->getRemoteAddr());
EXPECT_EQ(test_message_->getRemotePort(), rcvd_msg->getLocalPort());
EXPECT_EQ(test_message_->getLocalPort(), rcvd_msg->getRemotePort());
}
void
PktFilterTest::testReceivedPktEvents(const PktPtr& msg,
bool so_time_supported) const {
std::list<std::string> expected_events;
if (so_time_supported) {
expected_events.push_back(PktEvent::SOCKET_RECEIVED);
}
expected_events.push_back(PktEvent::BUFFER_READ);
testPktEvents(msg, start_time_, expected_events);
}
void
PktFilterTest::testPktEvents(const PktPtr& msg, ptime start_time,
std::list<std::string> expected_events) const {
ASSERT_NE(start_time, PktEvent::EMPTY_TIME());
auto events = msg->getPktEvents();
ASSERT_EQ(events.size(), expected_events.size());
ptime prev_time = start_time;
auto expected_event = expected_events.begin();
for (auto const& event : events) {
ASSERT_EQ(event.label_, *expected_event);
EXPECT_GE(event.timestamp_, prev_time);
prev_time = event.timestamp_;
++expected_event;
}
}
bool
PktFilterStub::isDirectResponseSupported() const {
return (true);
}
bool
PktFilterStub::isSocketReceivedTimeSupported() const {
return (true);
}
SocketInfo
PktFilterStub::openSocket(Iface&,
const isc::asiolink::IOAddress& addr,
const uint16_t port, const bool, const bool) {
return (SocketInfo(addr, port, 0));
}
Pkt4Ptr
PktFilterStub::receive(Iface&, const SocketInfo&) {
return Pkt4Ptr();
}
int
PktFilterStub::send(const Iface&, uint16_t, const Pkt4Ptr&) {
return (0);
}
} // end of isc::dhcp::test namespace
} // end of isc::dhcp namespace
} // end of isc namespace
|