Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Multihomed_INET_Addr.cpp
blob4589fbf22fed293a6e4374d04a8c822de74340a2
1 // Extends ACE_INET_Addr with support for multi-homed addresses.
3 #include "ace/Multihomed_INET_Addr.h"
4 #include "ace/Log_Category.h"
6 #if !defined (__ACE_INLINE__)
7 # include "ace/Multihomed_INET_Addr.inl"
8 #endif /* __ACE_INLINE__ */
10 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
12 ACE_ALLOC_HOOK_DEFINE(ACE_Multihomed_INET_Addr)
14 // Default constructor
16 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr ()
17 : secondaries_ (0)
19 ACE_TRACE ("ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr");
22 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr (const char address[])
23 : ACE_INET_Addr (address),
24 secondaries_ (0)
28 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
29 const char host_name[],
30 int encode,
31 int address_family,
32 const char *(secondary_host_names[]),
33 size_t size){
34 // Initialize the primary INET addr
35 ACE_INET_Addr::set(port_number, host_name, encode, address_family);
37 // check for secondary INET addrs
38 if (secondary_host_names && size){
39 // we have a non-zero pointer and size
40 this->secondaries_.size(size); // size the array
42 size_t next_empty_slot = 0;
43 for (size_t i = 0; i < size; ++i) {
44 int ret = this->secondaries_[next_empty_slot].set(port_number,
45 secondary_host_names[i],
46 encode,
47 address_family);
48 if (ret) {
49 ACELIB_DEBUG ((LM_DEBUG,
50 ACE_TEXT ("Invalid INET addr (%C:%u) will be ignored\n"),
51 secondary_host_names[i], port_number));
52 this->secondaries_.size(this->secondaries_.size() - 1);
54 else
55 ++next_empty_slot;
59 return;
62 #if defined (ACE_HAS_WCHAR)
63 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
64 const wchar_t host_name[],
65 int encode,
66 int address_family,
67 const wchar_t *(secondary_host_names[]),
68 size_t size){
69 // Initialize the primary INET addr
70 ACE_INET_Addr::set(port_number, host_name, encode, address_family);
72 // check for secondary INET addrs
73 if (secondary_host_names && size){
74 // we have a non-zero pointer and size
75 this->secondaries_.size(size); // size the array
77 size_t next_empty_slot = 0;
78 for (size_t i = 0; i < size; ++i) {
79 int ret = this->secondaries_[next_empty_slot].set(port_number,
80 secondary_host_names[i],
81 encode,
82 address_family);
83 if (ret) {
84 ACELIB_DEBUG ((LM_DEBUG,
85 ACE_TEXT ("Invalid INET addr (%s:%u) will be ignored\n"),
86 ACE_TEXT_WCHAR_TO_TCHAR (secondary_host_names[i]), port_number));
87 this->secondaries_.size(this->secondaries_.size() - 1);
89 else
90 ++next_empty_slot;
94 return;
96 #endif /* ACE_HAS_WCHAR */
98 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number,
99 ACE_UINT32 primary_ip_addr,
100 int encode,
101 const ACE_UINT32 *secondary_ip_addrs,
102 size_t size){
103 // Initialize the primary INET addr
104 ACE_INET_Addr::set(port_number, primary_ip_addr, encode);
106 // check for secondary INET addrs
107 if (secondary_ip_addrs && size){
108 // we have a non-zero pointer and size
109 this->secondaries_.size(size); // size the array
111 size_t next_empty_slot = 0;
112 for (size_t i = 0; i < size; ++i) {
113 int const ret = this->secondaries_[next_empty_slot].set(port_number,
114 secondary_ip_addrs[i],
115 encode);
117 if (ret) {
118 ACELIB_DEBUG ((LM_DEBUG,
119 "Invalid INET addr (%u:%u) will be ignored\n",
120 secondary_ip_addrs[i], port_number));
121 this->secondaries_.size(this->secondaries_.size() - 1);
123 else
124 ++next_empty_slot;
128 return;
131 // Set implementations (NEED BETTER COMMENT HERE)
133 ACE_Multihomed_INET_Addr::set (u_short port_number,
134 const char host_name[],
135 int encode,
136 int address_family,
137 const char *(secondary_host_names[]),
138 size_t size)
140 this->secondaries_.size(size);
142 for (size_t i = 0; i < size; ++i) {
143 int const ret = this->secondaries_[i].set(port_number,
144 secondary_host_names[i],
145 encode,
146 address_family);
147 if (ret) {
148 return ret;
152 return ACE_INET_Addr::set(port_number, host_name, encode, address_family);
155 #if defined (ACE_HAS_WCHAR)
157 // WCHAR version of ::set
160 ACE_Multihomed_INET_Addr::set (u_short port_number,
161 const wchar_t host_name[],
162 int encode,
163 int address_family,
164 const wchar_t *(secondary_host_names[]),
165 size_t size)
167 this->secondaries_.size(size);
169 for (size_t i = 0; i < size; ++i) {
170 int ret = this->secondaries_[i].set(port_number,
171 secondary_host_names[i],
172 encode,
173 address_family);
174 if (ret) {
175 return ret;
179 return ACE_INET_Addr::set(port_number, host_name, encode, address_family);
181 #endif /* ACE_HAS_WCHAR */
184 ACE_Multihomed_INET_Addr::set (u_short port_number,
185 ACE_UINT32 primary_ip_addr,
186 int encode,
187 const ACE_UINT32 *secondary_ip_addrs,
188 size_t size)
190 this->secondaries_.size(size);
192 for (size_t i = 0; i < size; ++i) {
193 int ret = this->secondaries_[i].set(port_number,
194 secondary_ip_addrs[i],
195 encode);
197 if (ret) {
198 return ret;
202 return ACE_INET_Addr::set(port_number, primary_ip_addr, encode);
205 void
206 ACE_Multihomed_INET_Addr::set_port_number (u_short port_number, int encode)
208 size_t i = 0;
209 while (i < secondaries_.size())
210 secondaries_[i++].set_port_number(port_number, encode);
212 this->ACE_INET_Addr::set_port_number(port_number, encode);
216 ACE_Multihomed_INET_Addr::get_secondary_addresses(ACE_INET_Addr *secondary_addrs,
217 size_t size) const
219 size_t top =
220 size < this->secondaries_.size() ?
221 size : this->secondaries_.size();
223 for (size_t i = 0; i < top; ++i)
225 int ret =
226 secondary_addrs[i].set (this->secondaries_[i]);
228 if (ret)
229 return ret;
232 return 0;
235 void
236 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in *addrs,
237 size_t size) const
239 if (size == 0)
240 return;
241 // Copy primary address(es) to the first slot(s) of the user-supplied array
242 ACE_INET_Addr me (*this);
243 size_t i = 0;
244 for (i = 0; i < size; ++i)
246 sockaddr_in *in4 = reinterpret_cast<sockaddr_in*> (me.get_addr ());
247 if (in4->sin_family == AF_INET)
249 addrs[i] = *in4;
250 ++i;
252 if (!me.next ())
253 break;
256 // Copy secondary addresses to remaining slots of the user-supplied
257 // array. Secondary address [i] is copied to slot [i+1]
258 for (size_t j = 0; j < this->secondaries_.size (); ++j)
260 ACE_INET_Addr copy (this->secondaries_[j]);
261 for (; i < size; ++i)
263 sockaddr_in *in4 = reinterpret_cast<sockaddr_in*> (copy.get_addr ());
264 if (in4->sin_family == AF_INET)
266 addrs[i] = *in4;
267 ++i;
269 if (!copy.next ())
270 break;
275 #if defined (ACE_HAS_IPV6)
276 void
277 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in6 *addrs,
278 size_t size) const
280 if (size == 0)
281 return;
282 // Copy primary address(es) to the first slot(s) of the user-supplied array
283 ACE_INET_Addr me (*this);
284 size_t i = 0;
285 for (i = 0; i < size; ++i)
287 sockaddr_in6 *in6 = reinterpret_cast<sockaddr_in6*> (me.get_addr ());
288 if (in6->sin6_family == AF_INET6)
290 addrs[i] = *in6;
291 ++i;
293 if (!me.next ())
294 break;
297 // Copy secondary addresses to remaining slots of the user-supplied
298 // array. Secondary address [i] is copied to slot [i+1]
299 for (size_t j = 0; j < this->secondaries_.size (); ++j)
301 ACE_INET_Addr copy (this->secondaries_[j]);
302 for (; i < size; ++i)
304 sockaddr_in6 *in6 =
305 reinterpret_cast<sockaddr_in6*> (copy.get_addr ());
306 if (in6->sin6_family == AF_INET6)
308 addrs[i] = *in6;
309 ++i;
311 if (!copy.next ())
312 break;
316 #endif /* ACE_HAS_IPV6 */
319 ACE_Multihomed_INET_Addr::~ACE_Multihomed_INET_Addr ()
323 ACE_END_VERSIONED_NAMESPACE_DECL