OpenDDS  Snapshot(2023/04/28-20:55)
NetworkConfigModifier.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "DCPS/DdsDcps_pch.h" //Only the _pch include should start with DCPS/
9 
10 #include "NetworkConfigModifier.h"
11 
12 #ifdef OPENDDS_NETWORK_CONFIG_MODIFIER
13 
14 #include "debug.h"
15 
16 #include <ace/OS_NS_sys_socket.h>
17 
19 
20 #include <net/if.h>
21 
23 
24 namespace OpenDDS {
25 namespace DCPS {
26 
27 bool NetworkConfigModifier::open()
28 {
29  update_interfaces();
30  return true;
31 }
32 
33 bool NetworkConfigModifier::close()
34 {
36  return true;
37 }
38 
39 void NetworkConfigModifier::update_interfaces()
40 {
41  if (DCPS_debug_level >= 1) {
42  ACE_DEBUG((LM_DEBUG, "(%P|%t) NetworkConfigModifier::update_interfaces: enumerating interfaces.\n"));
43  }
44 
45  ifaddrs* p_ifa = 0;
46  ifaddrs* p_if = 0;
47 
48  if (::getifaddrs(&p_ifa) != 0) {
49  if (log_level >= LogLevel::Error) {
50  ACE_ERROR((LM_ERROR, "(%P|%t) ERROR: NetworkConfigModifier::update_interfaces: %p\n",
51  "getifaddrs error"));
52  }
53 
54  return;
55  }
56 
57  // Using logic from ACE::get_ip_interfaces_getifaddrs
58  // but need ifa_name which is not returned by it
59  List nia_list;
60 
61  // Pull the address out of each INET interface.
62  for (p_if = p_ifa; p_if != 0; p_if = p_if->ifa_next) {
63  if (p_if->ifa_addr == 0)
64  continue;
65 
66  // Check to see if it's up.
67  if ((p_if->ifa_flags & IFF_UP) != IFF_UP)
68  continue;
69 
70  if (p_if->ifa_addr->sa_family == AF_INET) {
71  struct sockaddr_in* addr = reinterpret_cast<sockaddr_in*> (p_if->ifa_addr);
72 
73  // Sometimes the kernel returns 0.0.0.0 as the interface
74  // address, skip those...
75  if (addr->sin_addr.s_addr != INADDR_ANY) {
76  ACE_INET_Addr address;
77  address.set((u_short) 0, addr->sin_addr.s_addr, 0);
78 
79  nia_list.push_back(NetworkInterfaceAddress(p_if->ifa_name, p_if->ifa_flags & (IFF_MULTICAST | IFF_LOOPBACK), NetworkAddress(address)));
80  }
81  }
82 # if defined (ACE_HAS_IPV6)
83  else if (p_if->ifa_addr->sa_family == AF_INET6) {
84  struct sockaddr_in6 *addr = reinterpret_cast<sockaddr_in6 *> (p_if->ifa_addr);
85 
86  // Skip the ANY address
87  if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr)) {
88  ACE_INET_Addr address;
89  address.set(reinterpret_cast<struct sockaddr_in *> (addr), sizeof(sockaddr_in6));
90 
91  nia_list.push_back(NetworkInterfaceAddress(p_if->ifa_name, p_if->ifa_flags & (IFF_MULTICAST | IFF_LOOPBACK), NetworkAddress(address)));
92  }
93  }
94 # endif /* ACE_HAS_IPV6 */
95  }
96 
97  ::freeifaddrs (p_ifa);
98 
99  NetworkConfigMonitor::set(nia_list);
100 }
101 
102 void NetworkConfigModifier::add_interface(const OPENDDS_STRING&)
103 {
104  // This is a no-op.
105 }
106 
107 void NetworkConfigModifier::remove_interface(const OPENDDS_STRING& name)
108 {
110 }
111 
112 void NetworkConfigModifier::add_address(const OPENDDS_STRING& name,
113  bool can_multicast,
114  const ACE_INET_Addr& addr)
115 {
116  NetworkConfigMonitor::set(NetworkInterfaceAddress(name, can_multicast, NetworkAddress(addr)));
117 }
118 
119 void NetworkConfigModifier::remove_address(const OPENDDS_STRING& name, const ACE_INET_Addr& addr)
120 {
121  NetworkConfigMonitor::remove_address(name, NetworkAddress(addr));
122 }
123 
124 } // DCPS
125 } // OpenDDS
126 
128 
129 #endif // OPENDDS_NETWORK_CONFIG_MONITOR
#define ACE_DEBUG(X)
#define ACE_ERROR(X)
#define OPENDDS_STRING
void remove_interface(const OPENDDS_STRING &name)
const char *const name
Definition: debug.cpp:60
int set(const ACE_INET_Addr &)
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30
OpenDDS_Dcps_Export LogLevel log_level
void remove_address(const OPENDDS_STRING &name, const NetworkAddress &address)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28