1 #include "ace/ICMP_Socket.h"
3 #if defined (ACE_HAS_ICMP_SUPPORT) && (ACE_HAS_ICMP_SUPPORT == 1)
6 #include "ace/Sock_Connect.h"
7 #include "ace/Log_Category.h"
8 #include "ace/OS_NS_netdb.h"
9 #include "ace/OS_NS_sys_socket.h"
10 #if defined (ACE_HAS_ALLOC_HOOKS)
11 # include "ace/Malloc_Base.h"
12 #endif /* ACE_HAS_ALLOC_HOOKS */
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 ACE_ALLOC_HOOK_DEFINE (ACE_ICMP_Socket
)
20 ACE_ICMP_Socket::dump () const
22 ACE_TRACE ("ACE_ICMP_Socket::dump");
25 ACE_ICMP_Socket::ACE_ICMP_Socket ()
27 ACE_TRACE ("ACE_ICMP_Socket::ACE_ICMP_Socket");
31 ACE_ICMP_Socket::send (void const * buf
,
33 ACE_Addr
const & addr
,
36 ACE_TRACE ("ACE_ICMP_Socket::send");
38 return ACE_OS::sendto (this->get_handle (),
42 (sockaddr
const *) addr
.get_addr (),
47 ACE_ICMP_Socket::recv (void * buf
,
52 ACE_TRACE ("ACE_ICMP_Socket::recv");
54 int addr_len
= addr
.get_size ();
55 ssize_t status
= ACE_OS::recvfrom (this->get_handle (),
59 (sockaddr
*) addr
.get_addr (),
61 addr
.set_size (addr_len
);
67 ACE_ICMP_Socket::recv (void * buf
,
70 ACE_Time_Value
const * timeout
) const
72 ACE_TRACE ("ACE_ICMP_Socket::recv");
74 return ACE::recv (this->get_handle (),
82 ACE_ICMP_Socket::open (ACE_Addr
const & local
,
86 ACE_TRACE ("ACE_ICMP_Socket::open");
88 // Check if icmp protocol is supported on this host
89 int proto_number
= -1;
92 if (! (proto
= ACE_OS::getprotobyname ("icmp")))
96 ACE_TEXT ("(%P|%t) ACE_ICMP_Socket::open: %p; %s\n"),
97 ACE_TEXT ("getprotobyname"),
98 ACE_TEXT ("ICMP protocol is not properly configured ")
99 ACE_TEXT ("or not supported.")),
102 proto_number
= proto
->p_proto
;
104 if (proto_number
!= IPPROTO_ICMP
|| proto_number
!= protocol
)
106 ACELIB_ERROR_RETURN ((LM_ERROR
,
107 ACE_TEXT ("(%P|%t) ACE::ICMP_Socket::open - ")
108 ACE_TEXT ("only IPPROTO_ICMP protocol is ")
109 ACE_TEXT ("currently supported.\n")),
113 if (ACE_SOCK::open (SOCK_RAW
,
121 return this->shared_open (local
);
125 ACE_ICMP_Socket::shared_open (ACE_Addr
const & local
)
127 ACE_TRACE ("ACE_ICMP_Socket::shared_open");
130 if (local
== ACE_Addr::sap_any
)
132 if (ACE::bind_port (this->get_handle ()) == -1)
137 else if (ACE_OS::bind (this->get_handle (),
138 reinterpret_cast<sockaddr
*> (local
.get_addr ()),
139 local
.get_size ()) == -1)
149 return error
? -1 : 0;
153 ACE_ICMP_Socket::calculate_checksum (unsigned short * paddress
,
158 unsigned short * w
= paddress
;
159 unsigned short answer
= 0;
168 *((unsigned char *) &answer
) = *((unsigned char *) w
);
172 // add back carry outs from top 16 bits to low 16 bits
173 sum
= (sum
>> 16) + (sum
& 0xffff); // add hi 16 to low 16
174 sum
+= (sum
>> 16); // add carry
175 answer
= ~sum
; // truncate to 16 bits
180 ACE_END_VERSIONED_NAMESPACE_DECL
182 #endif /* ACE_HAS_ICMP_SUPPORT == 1 */