3 //=============================================================================
5 * @file Enum_Interfaces_Test.cpp
7 * This is a simple test of <ACE::get_ip_interfaces>. This call
8 * retrieves the IP addresses assigned to the host by
9 * interrogating the kernel. Network applications typically
10 * assume gethostbyname(uname()) will work, but this is just a
11 * convention. It is also problematic if the resolver code
12 * (DNS/NIS+...) is misconfigured. This happens more than
13 * programmers realize. It is better to find out by asking the
14 * kernel for local address assignments. This API is similar to
15 * what netstat -ni or ifconfig -a produces on UNIX or ipconfig on
16 * Windows NT. In fact, it was by reverse engineering these tools
17 * that this api was created.
19 * @author Michael R. MacFaden <mrm@cisco.com>
21 //=============================================================================
23 #include "test_config.h"
24 #include "ace/OS_NS_sys_utsname.h"
25 #include "ace/INET_Addr.h"
28 run_main (int, ACE_TCHAR
*[])
30 ACE_START_TEST (ACE_TEXT ("Enum_Interfaces_Test"));
33 ACE_OS::uname (&uname
);
34 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Machine: %C running on %C\n"),
35 uname
.nodename
, uname
.machine
));
36 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Platform: %C, %C, %C\n"),
37 uname
.sysname
, uname
.release
, uname
.version
));
39 ACE_INET_Addr
*the_addr_array
= 0;
42 int rc
= ACE::get_ip_interfaces (how_many
, the_addr_array
);
47 ACE_TEXT ("ACE::get_ip_interfaces failed")));
48 else if (how_many
== 0)
50 ACE_TEXT ("No interfaces presently configured in the kernel\n")));
53 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT (" there are %d interfaces\n"), how_many
));
56 for (size_t i
= 0; i
< how_many
; i
++)
58 if (the_addr_array
[i
].get_type() == AF_INET
)
60 #if defined (ACE_HAS_IPV6)
61 else if (the_addr_array
[i
].get_type() == AF_INET6
)
63 #endif /* ACE_HAS_IPV6 */
64 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\t%C\n"),
65 the_addr_array
[i
].get_host_addr ()));
68 delete [] the_addr_array
;
71 ACE_TEXT (" there are %d IPv4 interfaces, ")
72 ACE_TEXT ("and %d IPv6 interfaces\n"),
74 #if defined (ACE_HAS_IPV6)
79 ACE_TEXT ("Since ACE_HAS_IPV6 is set, at least 1 ")
80 ACE_TEXT ("ipv6 interface was expected\n")));
82 #endif /* ACE_HAS_IPV6 */
86 return rc
!= 0; // return 1 if get_ip_interfaces() failed