Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / ICMP_Socket.cpp
blob074c23bb2060460cb3d2b5d986edc1bf2cda091d
1 #include "ace/ICMP_Socket.h"
3 #if defined (ACE_HAS_ICMP_SUPPORT) && (ACE_HAS_ICMP_SUPPORT == 1)
5 #include "ace/ACE.h"
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)
19 void
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");
30 ssize_t
31 ACE_ICMP_Socket::send (void const * buf,
32 size_t n,
33 ACE_Addr const & addr,
34 int flags) const
36 ACE_TRACE ("ACE_ICMP_Socket::send");
38 return ACE_OS::sendto (this->get_handle (),
39 (char const *) buf,
41 flags,
42 (sockaddr const *) addr.get_addr (),
43 addr.get_size ());
46 ssize_t
47 ACE_ICMP_Socket::recv (void * buf,
48 size_t n,
49 ACE_Addr & addr,
50 int flags) const
52 ACE_TRACE ("ACE_ICMP_Socket::recv");
54 int addr_len = addr.get_size ();
55 ssize_t status = ACE_OS::recvfrom (this->get_handle (),
56 (char *) buf,
58 flags,
59 (sockaddr *) addr.get_addr (),
60 (int*) &addr_len);
61 addr.set_size (addr_len);
63 return status;
66 ssize_t
67 ACE_ICMP_Socket::recv (void * buf,
68 size_t n,
69 int flags,
70 ACE_Time_Value const * timeout) const
72 ACE_TRACE ("ACE_ICMP_Socket::recv");
74 return ACE::recv (this->get_handle (),
75 buf,
77 flags,
78 timeout);
81 int
82 ACE_ICMP_Socket::open (ACE_Addr const & local,
83 int protocol,
84 int reuse_addr)
86 ACE_TRACE ("ACE_ICMP_Socket::open");
88 // Check if icmp protocol is supported on this host
89 int proto_number = -1;
90 protoent *proto = 0;
92 if (! (proto = ACE_OS::getprotobyname ("icmp")))
94 ACELIB_ERROR_RETURN
95 ((LM_ERROR,
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.")),
100 -1);
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")),
110 -1);
113 if (ACE_SOCK::open (SOCK_RAW,
114 AF_INET,
115 protocol,
116 reuse_addr) == -1)
118 return -1;
121 return this->shared_open (local);
125 ACE_ICMP_Socket::shared_open (ACE_Addr const & local)
127 ACE_TRACE ("ACE_ICMP_Socket::shared_open");
129 bool error = false;
130 if (local == ACE_Addr::sap_any)
132 if (ACE::bind_port (this->get_handle ()) == -1)
134 error = true;
137 else if (ACE_OS::bind (this->get_handle (),
138 reinterpret_cast<sockaddr *> (local.get_addr ()),
139 local.get_size ()) == -1)
141 error = true;
144 if (error)
146 this->close ();
149 return error ? -1 : 0;
152 unsigned short
153 ACE_ICMP_Socket::calculate_checksum (unsigned short * paddress,
154 int len)
156 int nleft = len;
157 int sum = 0;
158 unsigned short * w = paddress;
159 unsigned short answer = 0;
160 while (nleft > 1)
162 sum += *w++;
163 nleft -= 2;
166 if (nleft == 1)
168 *((unsigned char *) &answer) = *((unsigned char *) w);
169 sum += answer;
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
177 return (answer);
180 ACE_END_VERSIONED_NAMESPACE_DECL
182 #endif /* ACE_HAS_ICMP_SUPPORT == 1 */