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
|
#include "ace/ICMP_Socket.h"
#if defined (ACE_HAS_ICMP_SUPPORT) && (ACE_HAS_ICMP_SUPPORT == 1)
#include "ace/ACE.h"
#include "ace/Sock_Connect.h"
#include "ace/Log_Category.h"
#include "ace/OS_NS_netdb.h"
#include "ace/OS_NS_sys_socket.h"
#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
ACE_ALLOC_HOOK_DEFINE (ACE_ICMP_Socket)
void
ACE_ICMP_Socket::dump () const
{
ACE_TRACE ("ACE_ICMP_Socket::dump");
}
ACE_ICMP_Socket::ACE_ICMP_Socket ()
{
ACE_TRACE ("ACE_ICMP_Socket::ACE_ICMP_Socket");
}
ssize_t
ACE_ICMP_Socket::send (void const * buf,
size_t n,
ACE_Addr const & addr,
int flags) const
{
ACE_TRACE ("ACE_ICMP_Socket::send");
return ACE_OS::sendto (this->get_handle (),
(char const *) buf,
n,
flags,
(sockaddr const *) addr.get_addr (),
addr.get_size ());
}
ssize_t
ACE_ICMP_Socket::recv (void * buf,
size_t n,
ACE_Addr & addr,
int flags) const
{
ACE_TRACE ("ACE_ICMP_Socket::recv");
int addr_len = addr.get_size ();
ssize_t status = ACE_OS::recvfrom (this->get_handle (),
(char *) buf,
n,
flags,
(sockaddr *) addr.get_addr (),
(int*) &addr_len);
addr.set_size (addr_len);
return status;
}
ssize_t
ACE_ICMP_Socket::recv (void * buf,
size_t n,
int flags,
ACE_Time_Value const * timeout) const
{
ACE_TRACE ("ACE_ICMP_Socket::recv");
return ACE::recv (this->get_handle (),
buf,
n,
flags,
timeout);
}
int
ACE_ICMP_Socket::open (ACE_Addr const & local,
int protocol,
int reuse_addr)
{
ACE_TRACE ("ACE_ICMP_Socket::open");
// Check if icmp protocol is supported on this host
int proto_number = -1;
protoent *proto = 0;
if (! (proto = ACE_OS::getprotobyname ("icmp")))
{
ACELIB_ERROR_RETURN
((LM_ERROR,
ACE_TEXT ("(%P|%t) ACE_ICMP_Socket::open: %p; %s\n"),
ACE_TEXT ("getprotobyname"),
ACE_TEXT ("ICMP protocol is not properly configured ")
ACE_TEXT ("or not supported.")),
-1);
}
proto_number = proto->p_proto;
if (proto_number != IPPROTO_ICMP || proto_number != protocol)
{
ACELIB_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%P|%t) ACE::ICMP_Socket::open - ")
ACE_TEXT ("only IPPROTO_ICMP protocol is ")
ACE_TEXT ("currently supported.\n")),
-1);
}
if (ACE_SOCK::open (SOCK_RAW,
AF_INET,
protocol,
reuse_addr) == -1)
{
return -1;
}
return this->shared_open (local);
}
int
ACE_ICMP_Socket::shared_open (ACE_Addr const & local)
{
ACE_TRACE ("ACE_ICMP_Socket::shared_open");
bool error = false;
if (local == ACE_Addr::sap_any)
{
if (ACE::bind_port (this->get_handle ()) == -1)
{
error = true;
}
}
else if (ACE_OS::bind (this->get_handle (),
reinterpret_cast<sockaddr *> (local.get_addr ()),
local.get_size ()) == -1)
{
error = true;
}
if (error)
{
this->close ();
}
return error ? -1 : 0;
}
unsigned short
ACE_ICMP_Socket::calculate_checksum (unsigned short * paddress,
int len)
{
int nleft = len;
int sum = 0;
unsigned short * w = paddress;
unsigned short answer = 0;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*((unsigned char *) &answer) = *((unsigned char *) w);
sum += answer;
}
// add back carry outs from top 16 bits to low 16 bits
sum = (sum >> 16) + (sum & 0xffff); // add hi 16 to low 16
sum += (sum >> 16); // add carry
answer = ~sum; // truncate to 16 bits
return (answer);
}
ACE_END_VERSIONED_NAMESPACE_DECL
#endif /* ACE_HAS_ICMP_SUPPORT == 1 */
|