Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Enum_Interfaces_Test.cpp
blob77a3727f52c02f62c575ac9746bb20468fc685fe
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 //=============================================================================
24 #include "test_config.h"
25 #include "ace/OS_NS_sys_utsname.h"
26 #include "ace/INET_Addr.h"
30 int
31 run_main (int, ACE_TCHAR *[])
33 ACE_START_TEST (ACE_TEXT ("Enum_Interfaces_Test"));
35 ACE_utsname uname;
36 ACE_OS::uname (&uname);
37 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Machine: %C running on %C\n"),
38 uname.nodename, uname.machine ));
39 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Platform: %C, %C, %C\n"),
40 uname.sysname, uname.release, uname.version ));
42 ACE_INET_Addr *the_addr_array = 0;
43 size_t how_many = 0;
45 int rc = ACE::get_ip_interfaces (how_many, the_addr_array);
47 if (rc != 0)
48 ACE_ERROR ((LM_ERROR,
49 ACE_TEXT ("%p\n"),
50 ACE_TEXT ("ACE::get_ip_interfaces failed")));
51 else if (how_many == 0)
52 ACE_ERROR ((LM_ERROR,
53 ACE_TEXT ("No interfaces presently configured in the kernel\n")));
54 else
56 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" there are %d interfaces\n"), how_many));
57 int num_ipv4 = 0;
58 int num_ipv6 = 0;
59 for (size_t i = 0; i < how_many; i++)
61 if (the_addr_array[i].get_type() == AF_INET)
62 ++num_ipv4;
63 #if defined (ACE_HAS_IPV6)
64 else if (the_addr_array[i].get_type() == AF_INET6)
65 ++num_ipv6;
66 #endif /* ACE_HAS_IPV6 */
67 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\t%C\n"),
68 the_addr_array[i].get_host_addr ()));
71 delete [] the_addr_array;
73 ACE_DEBUG ((LM_DEBUG,
74 ACE_TEXT (" there are %d IPv4 interfaces, ")
75 ACE_TEXT ("and %d IPv6 interfaces\n"),
76 num_ipv4, num_ipv6));
77 #if defined (ACE_HAS_IPV6)
78 if (num_ipv6 == 0)
80 rc = 1;
81 ACE_DEBUG ((LM_ERROR,
82 ACE_TEXT ("Since ACE_HAS_IPV6 is set, at least 1 ")
83 ACE_TEXT ("ipv6 interface was expected\n")));
85 #endif /* ACE_HAS_IPV6 */
88 ACE_END_TEST;
89 return rc != 0; // return 1 if get_ip_interfaces() failed