forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICMPSocketImpl.cpp
More file actions
131 lines (108 loc) · 3.09 KB
/
ICMPSocketImpl.cpp
File metadata and controls
131 lines (108 loc) · 3.09 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
//
// ICMPSocketImpl.cpp
//
// Library: Net
// Package: ICMP
// Module: ICMPSocketImpl
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/ICMPSocketImpl.h"
#include "Poco/Net/ICMPv4PacketImpl.h"
#include "Poco/Net/NetException.h"
#include "Poco/Format.h"
#include "Poco/Buffer.h"
using Poco::TimeoutException;
using Poco::Timespan;
using Poco::Exception;
namespace Poco {
namespace Net {
ICMPSocketImpl::ICMPSocketImpl(IPAddress::Family family, int dataSize, int ttl, int timeout):
RawSocketImpl(family, IPPROTO_ICMP),
_icmpPacket(family, dataSize),
_ttl(ttl),
_timeout(timeout)
{
setOption(IPPROTO_IP, IP_TTL, ttl);
setBlocking(true);
setReceiveTimeout(Timespan(timeout));
}
ICMPSocketImpl::~ICMPSocketImpl()
{
}
int ICMPSocketImpl::sendTo(const void*, int, const SocketAddress& address, int flags)
{
int n = SocketImpl::sendTo(_icmpPacket.packet(), _icmpPacket.packetSize(), address, flags);
return n;
}
void ICMPSocketImpl::checkFragmentation(const std::string& err, int type, int code)
{
if (type == ICMPv4PacketImpl::DESTINATION_UNREACHABLE &&
code == ICMPv4PacketImpl::FRAGMENTATION_NEEDED_AND_DF_SET)
{
throw ICMPFragmentationException(err);
}
}
int ICMPSocketImpl::receiveFrom(void*, int, SocketAddress& address, int flags)
{
int maxPacketSize = _icmpPacket.maxPacketSize();
Poco::Buffer<unsigned char> buffer(maxPacketSize);
int expected = _icmpPacket.packetSize();
int type = 0, code = 0;
try
{
Poco::Timestamp ts;
int rc;
do
{
// guard against a DoS attack
if (ts.isElapsed(_timeout)) throw TimeoutException();
buffer.clear();
SocketAddress respAddr;
rc = SocketImpl::receiveFrom(buffer.begin(), maxPacketSize, respAddr, flags);
if (rc == 0) break;
if (respAddr == address)
{
expected -= rc;
if (expected <= 0)
{
if (_icmpPacket.validReplyID(buffer.begin(), maxPacketSize)) break;
std::string err = _icmpPacket.errorDescription(buffer.begin(), maxPacketSize, type, code);
if (address.family() == IPAddress::IPv4) checkFragmentation(err, type, code);
if (!err.empty()) throw ICMPException(err);
throw ICMPException("Invalid ICMP reply");
}
}
else continue;
}
while (expected > 0 && !_icmpPacket.validReplyID(buffer.begin(), maxPacketSize));
}
catch (ICMPException&)
{
throw;
}
catch (TimeoutException&)
{
throw;
}
catch (Exception&)
{
std::string err = _icmpPacket.errorDescription(buffer.begin(), maxPacketSize, type, code);
if (address.family() == IPAddress::IPv4) checkFragmentation(err, type, code);
if (!err.empty()) throw ICMPException(err);
else throw;
}
if (expected > 0)
{
throw ICMPException(Poco::format("No response: expected %d, received: %d", _icmpPacket.packetSize(),
_icmpPacket.packetSize() - expected));
}
struct timeval then = _icmpPacket.time(buffer.begin(), maxPacketSize);
struct timeval now = _icmpPacket.time();
int elapsed = (((now.tv_sec * 1000000) + now.tv_usec) - ((then.tv_sec * 1000000) + then.tv_usec))/1000;
return elapsed;
}
} } // namespace Poco::Net