Restore build on FreeBSD.
[getmangos.git] / dep / ACE_wrappers / ace / ICMP_Socket.cpp
blob6178858f989e7acca79995a408f65857ba83477e
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)
7 #include "ace/ACE.h"
8 #include "ace/Log_Msg.h"
9 #include "ace/OS_NS_netdb.h"
10 #include "ace/OS_NS_sys_socket.h"
13 ACE_RCSID (ace,
14 ICMP_Socket,
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)
23 void
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");
34 ssize_t
35 ACE_ICMP_Socket::send (void const * buf,
36 size_t n,
37 ACE_Addr const & addr,
38 int flags) const
40 ACE_TRACE ("ACE_ICMP_Socket::send");
42 return ACE_OS::sendto (this->get_handle (),
43 (char const *) buf,
45 flags,
46 (sockaddr const *) addr.get_addr (),
47 addr.get_size ());
50 ssize_t
51 ACE_ICMP_Socket::recv (void * buf,
52 size_t n,
53 ACE_Addr & addr,
54 int flags) const
56 ACE_TRACE ("ACE_ICMP_Socket::recv");
58 int addr_len = addr.get_size ();
59 ssize_t status = ACE_OS::recvfrom (this->get_handle (),
60 (char *) buf,
62 flags,
63 (sockaddr *) addr.get_addr (),
64 (int*) &addr_len);
65 addr.set_size (addr_len);
67 return status;
70 ssize_t
71 ACE_ICMP_Socket::recv (void * buf,
72 size_t n,
73 int flags,
74 ACE_Time_Value const * timeout) const
76 ACE_TRACE ("ACE_ICMP_Socket::recv");
78 return ACE::recv (this->get_handle (),
79 buf,
81 flags,
82 timeout);
85 int
86 ACE_ICMP_Socket::open (ACE_Addr const & local,
87 int protocol,
88 int reuse_addr)
90 ACE_TRACE ("ACE_ICMP_Socket::open");
92 // Check if icmp protocol is supported on this host
93 int proto_number = -1;
94 protoent *proto;
96 if (! (proto = getprotobyname ("icmp")))
98 ACE_ERROR_RETURN
99 ((LM_ERROR,
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.")),
104 -1);
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")),
114 -1);
117 if (ACE_SOCK::open (SOCK_RAW,
118 AF_INET,
119 protocol,
120 reuse_addr) == -1)
122 return -1;
125 return this->shared_open (local);
129 ACE_ICMP_Socket::shared_open (ACE_Addr const & local)
131 ACE_TRACE ("ACE_ICMP_Socket::shared_open");
133 int error = 0;
134 if (local == ACE_Addr::sap_any)
136 if (ACE::bind_port (this->get_handle ()) == -1)
138 error = 1;
141 else if (ACE_OS::bind (this->get_handle (),
142 reinterpret_cast<sockaddr *> (local.get_addr ()),
143 local.get_size ()) == -1)
145 error = 1;
148 if (error != 0)
150 this->close ();
153 return error ? -1 : 0;
156 unsigned short
157 ACE_ICMP_Socket::calculate_checksum (unsigned short * paddress,
158 int len)
160 int nleft = len;
161 int sum = 0;
162 unsigned short * w = paddress;
163 unsigned short answer = 0;
164 while (nleft > 1)
166 sum += *w++;
167 nleft -= 2;
170 if (nleft == 1)
172 *((unsigned char *) &answer) = *((unsigned char *) w);
173 sum += answer;
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
181 return (answer);
184 ACE_END_VERSIONED_NAMESPACE_DECL
186 #endif /* ACE_HAS_ICMP_SUPPORT == 1 */