1 // $Id: ICMP_Socket.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/ICMP_Socket.h"
5 #if defined (ACE_HAS_ICMP_SUPPORT) && (ACE_HAS_ICMP_SUPPORT == 1)
8 #include "ace/Log_Msg.h"
9 #include "ace/OS_NS_netdb.h"
10 #include "ace/OS_NS_sys_socket.h"
15 "$Id: ICMP_Socket.cpp 80826 2008-03-04 14:51:23Z wotte $")
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 ACE_ALLOC_HOOK_DEFINE (ACE_ICMP_Socket
)
24 ACE_ICMP_Socket::dump (void) const
26 ACE_TRACE ("ACE_ICMP_Socket::dump");
29 ACE_ICMP_Socket::ACE_ICMP_Socket (void)
31 ACE_TRACE ("ACE_ICMP_Socket::ACE_ICMP_Socket");
35 ACE_ICMP_Socket::send (void const * buf
,
37 ACE_Addr
const & addr
,
40 ACE_TRACE ("ACE_ICMP_Socket::send");
42 return ACE_OS::sendto (this->get_handle (),
46 (sockaddr
const *) addr
.get_addr (),
51 ACE_ICMP_Socket::recv (void * buf
,
56 ACE_TRACE ("ACE_ICMP_Socket::recv");
58 int addr_len
= addr
.get_size ();
59 ssize_t status
= ACE_OS::recvfrom (this->get_handle (),
63 (sockaddr
*) addr
.get_addr (),
65 addr
.set_size (addr_len
);
71 ACE_ICMP_Socket::recv (void * buf
,
74 ACE_Time_Value
const * timeout
) const
76 ACE_TRACE ("ACE_ICMP_Socket::recv");
78 return ACE::recv (this->get_handle (),
86 ACE_ICMP_Socket::open (ACE_Addr
const & local
,
90 ACE_TRACE ("ACE_ICMP_Socket::open");
92 // Check if icmp protocol is supported on this host
93 int proto_number
= -1;
96 if (! (proto
= getprotobyname ("icmp")))
100 ACE_TEXT ("(%P|%t) ACE_ICMP_Socket::open: %p; %s\n"),
101 ACE_TEXT ("getprotobyname"),
102 ACE_TEXT ("ICMP protocol is not properly configured ")
103 ACE_TEXT ("or not supported.")),
106 proto_number
= proto
->p_proto
;
108 if (proto_number
!= IPPROTO_ICMP
|| proto_number
!= protocol
)
110 ACE_ERROR_RETURN ((LM_ERROR
,
111 ACE_TEXT ("(%P|%t) ACE::ICMP_Socket::open - ")
112 ACE_TEXT ("only IPPROTO_ICMP protocol is ")
113 ACE_TEXT ("currently supported.\n")),
117 if (ACE_SOCK::open (SOCK_RAW
,
125 return this->shared_open (local
);
129 ACE_ICMP_Socket::shared_open (ACE_Addr
const & local
)
131 ACE_TRACE ("ACE_ICMP_Socket::shared_open");
134 if (local
== ACE_Addr::sap_any
)
136 if (ACE::bind_port (this->get_handle ()) == -1)
141 else if (ACE_OS::bind (this->get_handle (),
142 reinterpret_cast<sockaddr
*> (local
.get_addr ()),
143 local
.get_size ()) == -1)
153 return error
? -1 : 0;
157 ACE_ICMP_Socket::calculate_checksum (unsigned short * paddress
,
162 unsigned short * w
= paddress
;
163 unsigned short answer
= 0;
172 *((unsigned char *) &answer
) = *((unsigned char *) w
);
176 // add back carry outs from top 16 bits to low 16 bits
177 sum
= (sum
>> 16) + (sum
& 0xffff); // add hi 16 to low 16
178 sum
+= (sum
>> 16); // add carry
179 answer
= ~sum
; // truncate to 16 bits
184 ACE_END_VERSIONED_NAMESPACE_DECL
186 #endif /* ACE_HAS_ICMP_SUPPORT == 1 */