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 ()
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
),
28 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number
,
29 const char host_name
[],
32 const char *(secondary_host_names
[]),
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
],
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);
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
[],
67 const wchar_t *(secondary_host_names
[]),
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
],
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);
96 #endif /* ACE_HAS_WCHAR */
98 ACE_Multihomed_INET_Addr::ACE_Multihomed_INET_Addr(u_short port_number
,
99 ACE_UINT32 primary_ip_addr
,
101 const ACE_UINT32
*secondary_ip_addrs
,
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
],
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);
131 // Set implementations (NEED BETTER COMMENT HERE)
133 ACE_Multihomed_INET_Addr::set (u_short port_number
,
134 const char host_name
[],
137 const char *(secondary_host_names
[]),
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
],
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
[],
164 const wchar_t *(secondary_host_names
[]),
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
],
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
,
187 const ACE_UINT32
*secondary_ip_addrs
,
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
],
202 return ACE_INET_Addr::set(port_number
, primary_ip_addr
, encode
);
206 ACE_Multihomed_INET_Addr::set_port_number (u_short port_number
, int encode
)
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
,
220 size
< this->secondaries_
.size() ?
221 size
: this->secondaries_
.size();
223 for (size_t i
= 0; i
< top
; ++i
)
226 secondary_addrs
[i
].set (this->secondaries_
[i
]);
236 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in
*addrs
,
241 // Copy primary address(es) to the first slot(s) of the user-supplied array
242 ACE_INET_Addr
me (*this);
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
)
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
)
275 #if defined (ACE_HAS_IPV6)
277 ACE_Multihomed_INET_Addr::get_addresses(sockaddr_in6
*addrs
,
282 // Copy primary address(es) to the first slot(s) of the user-supplied array
283 ACE_INET_Addr
me (*this);
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
)
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
)
305 reinterpret_cast<sockaddr_in6
*> (copy
.get_addr ());
306 if (in6
->sin6_family
== AF_INET6
)
316 #endif /* ACE_HAS_IPV6 */
319 ACE_Multihomed_INET_Addr::~ACE_Multihomed_INET_Addr ()
323 ACE_END_VERSIONED_NAMESPACE_DECL