Compile fixes
[ACE_TAO.git] / ACE / tests / Enum_Interfaces_Test.cpp
blob62c6400c5b603c97e16a0fdd3028bb55a0ecc6ee
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
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"
27 int
28 run_main (int, ACE_TCHAR *[])
30 ACE_START_TEST (ACE_TEXT ("Enum_Interfaces_Test"));
32 ACE_utsname uname;
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;
40 size_t how_many = 0;
42 int rc = ACE::get_ip_interfaces (how_many, the_addr_array);
44 if (rc != 0)
45 ACE_ERROR ((LM_ERROR,
46 ACE_TEXT ("%p\n"),
47 ACE_TEXT ("ACE::get_ip_interfaces failed")));
48 else if (how_many == 0)
49 ACE_ERROR ((LM_ERROR,
50 ACE_TEXT ("No interfaces presently configured in the kernel\n")));
51 else
53 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" there are %d interfaces\n"), how_many));
54 int num_ipv4 = 0;
55 int num_ipv6 = 0;
56 for (size_t i = 0; i < how_many; i++)
58 if (the_addr_array[i].get_type() == AF_INET)
59 ++num_ipv4;
60 #if defined (ACE_HAS_IPV6)
61 else if (the_addr_array[i].get_type() == AF_INET6)
62 ++num_ipv6;
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;
70 ACE_DEBUG ((LM_DEBUG,
71 ACE_TEXT (" there are %d IPv4 interfaces, ")
72 ACE_TEXT ("and %d IPv6 interfaces\n"),
73 num_ipv4, num_ipv6));
74 #if defined (ACE_HAS_IPV6)
75 if (num_ipv6 == 0)
77 rc = 1;
78 ACE_DEBUG ((LM_ERROR,
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 */
85 ACE_END_TEST;
86 return rc != 0; // return 1 if get_ip_interfaces() failed