forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICMPSocket.cpp
More file actions
147 lines (112 loc) · 2.81 KB
/
ICMPSocket.cpp
File metadata and controls
147 lines (112 loc) · 2.81 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
//
// ICMPSocket.cpp
//
// Library: Net
// Package: ICMP
// Module: ICMPSocket
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/ICMPSocket.h"
#include "Poco/Net/ICMPSocketImpl.h"
#include "Poco/Exception.h"
#include "Poco/Net/NetException.h"
using Poco::InvalidArgumentException;
namespace Poco {
namespace Net {
ICMPSocket::ICMPSocket(IPAddress::Family family, int dataSize, int ttl, int timeout):
Socket(new ICMPSocketImpl(family, dataSize, ttl, timeout))
{
}
ICMPSocket::ICMPSocket(const Socket& socket):
Socket(socket)
{
if (!dynamic_cast<ICMPSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
ICMPSocket::ICMPSocket(SocketImpl* pImpl):
Socket(pImpl)
{
if (!dynamic_cast<ICMPSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
ICMPSocket::~ICMPSocket()
{
}
ICMPSocket& ICMPSocket::operator = (const Socket& socket)
{
if (dynamic_cast<ICMPSocketImpl*>(socket.impl()))
Socket::operator = (socket);
else
throw InvalidArgumentException("Cannot assign incompatible socket");
return *this;
}
int ICMPSocket::sendTo(const SocketAddress& address, int flags)
{
return impl()->sendTo(0, 0, address, flags);
}
int ICMPSocket::receiveFrom(SocketAddress& address, int flags)
{
return impl()->receiveFrom(0, 0, address, flags);
}
int ICMPSocket::dataSize() const
{
return static_cast<ICMPSocketImpl*>(impl())->dataSize();
}
int ICMPSocket::packetSize() const
{
return static_cast<ICMPSocketImpl*>(impl())->packetSize();
}
int ICMPSocket::ttl() const
{
return static_cast<ICMPSocketImpl*>(impl())->ttl();
}
int ICMPSocket::timeout() const
{
return static_cast<ICMPSocketImpl*>(impl())->timeout();
}
Poco::UInt16 ICMPSocket::mtu(const SocketAddress& address, Poco::UInt16 sz)
{
if (address.family() != IPAddress::IPv4) return 0;
SocketAddress returnAddress(address);
for (; sz >= 68 /*RFC791*/; --sz)
{
ICMPSocket icmpSocket(address.family(), sz);
#ifdef IP_DONTFRAGMENT
icmpSocket.setOption(IPPROTO_IP, IP_DONTFRAGMENT, 1);
#elif defined(IP_MTU_DISCOVER)
icmpSocket.setOption(IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DO);
#elif defined(IP_DONTFRAG)
icmpSocket.setOption(IPPROTO_IP, IP_DONTFRAG, 1);
#else
throw NotImplementedException("ICMPSocket::mtu()");
#endif
try
{
icmpSocket.sendTo(address);
icmpSocket.receiveFrom(returnAddress);
poco_assert_dbg (returnAddress == address);
return sz;
}
catch (ICMPFragmentationException&)
{
// PMTU fragmentation, continue discovery
continue;
}
catch (NetException& ex)
{
// local MTU limit, continue discovery
if (ex.code() == POCO_EMSGSIZE)
continue;
}
catch (Exception&)
{
return 0;
}
}
return 0;
}
} } // namespace Poco::Net