EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
netint.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2024 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_NETINT_H_
24 #define ETCPAL_CPP_NETINT_H_
25 
26 #include <algorithm>
27 #include <vector>
28 #include "etcpal/netint.h"
29 #include "etcpal/cpp/common.h"
30 #include "etcpal/cpp/error.h"
31 #include "etcpal/cpp/inet.h"
32 
33 namespace etcpal
34 {
80 
83 
84 namespace netint
85 {
87 
88 namespace detail
89 {
91 {
92  static constexpr size_t kInitialEstimatedCount = 4u;
93 
94  int resize_count = 0;
95 
96  size_t num_netints = kInitialEstimatedCount; // Start with estimate
97  std::vector<EtcPalNetintInfo> c_netints(num_netints);
98  auto err = kEtcPalErrOk;
99  while ((err = (index.IsValid() ? etcpal_netint_get_interfaces_for_index(index.value(), c_netints.data(), &num_netints)
100  : etcpal_netint_get_interfaces(c_netints.data(), &num_netints))) == kEtcPalErrBufSize)
101  {
102  if (resize_count > 10)
103  return kEtcPalErrBufSize; // Something screwy is going on, so avoid a potentially infinite loop
104 
105  c_netints.resize(num_netints);
106  ++resize_count;
107  }
108 
109  if (err == kEtcPalErrOk)
110  {
111  std::vector<etcpal::NetintInfo> netints(num_netints);
112  for (size_t i = 0u; i < num_netints; ++i)
113  netints[i] = etcpal::NetintInfo(c_netints[i]);
114 
115  return netints;
116  }
117 
118  return err;
119 }
120 } // namespace detail
121 
123 
126 
138 {
139  return detail::GetInterfaces();
140 }
141 
152 {
153  if (!index)
154  return kEtcPalErrInvalid;
155 
156  return detail::GetInterfaces(index);
157 }
158 
169 {
170  if (!ip.IsValid())
171  return kEtcPalErrInvalid;
172 
173  EtcPalNetintInfo c_netint{};
174  auto err = etcpal_netint_get_interface_with_ip(&ip.get(), &c_netint);
175 
176  if (err == kEtcPalErrOk)
177  return etcpal::NetintInfo(c_netint);
178 
179  return err;
180 }
181 
198 {
199  unsigned int index = 0u;
200  auto err = etcpal_netint_get_default_interface(static_cast<etcpal_iptype_t>(type), &index);
201 
202  if (err == kEtcPalErrOk)
203  return NetintIndex(index);
204 
205  return err;
206 }
207 
219 {
220  unsigned int index = 0u;
221  auto err = etcpal_netint_get_interface_for_dest(&dest.get(), &index);
222 
223  if (err == kEtcPalErrOk)
224  return NetintIndex(index);
225 
226  return err;
227 }
228 
237 {
239 }
240 
249 inline bool IsUp(NetintIndex index) noexcept
250 {
251  return etcpal_netint_is_up(index.value());
252 }
253 
254 } // namespace netint
255 
257 
258 } // namespace etcpal
259 
260 #endif // ETCPAL_CPP_NETINT_H_
A wrapper class for the EtcPal error type.
Definition: error.h:94
A type representing either a value or an etcpal_error_t code.
Definition: error.h:477
A wrapper class for the EtcPal IP address type.
Definition: inet.h:59
A wrapper class for the EtcPal netint info type.
Definition: inet.h:810
A strongly-typed ID with arbitrary internal representation.
Definition: opaque_id.h:118
Common definitions used by EtcPal C++ wrappers.
C++ wrapper and utilities for etcpal/error.h.
C++ wrapper and utilities for etcpal/inet.h.
etcpal::OpaqueId< detail::NetintIndexType, unsigned int, 0 > NetintIndex
A handle that represents a network interface index.
Definition: inet.h:805
IpAddrType
Indicates an IP address family, or an invalid IP address.
Definition: inet.h:48
etcpal::Expected< etcpal::NetintInfo > GetInterfaceWithIp(const IpAddr &ip) noexcept
Get the network interface that has the specified IP address.
Definition: netint.h:168
etcpal::Expected< NetintIndex > GetInterfaceForDest(const etcpal::IpAddr &dest) noexcept
Get the network interface that the system will choose when routing an IP packet to the specified dest...
Definition: netint.h:218
bool IsUp(NetintIndex index) noexcept
Determine whether a network interface is currently up and running.
Definition: netint.h:249
etcpal::Expected< std::vector< etcpal::NetintInfo > > GetInterfacesForIndex(NetintIndex index) noexcept
Get a list of network interfaces that have the index specified.
Definition: netint.h:151
etcpal::Error RefreshInterfaces() noexcept
Refresh the list of network interfaces.
Definition: netint.h:236
etcpal::Expected< std::vector< etcpal::NetintInfo > > GetInterfaces() noexcept
Get a list of network interfaces on the system.
Definition: netint.h:137
etcpal::Expected< NetintIndex > GetDefaultInterface(etcpal::IpAddrType type) noexcept
Get information about the default network interface.
Definition: netint.h:197
@ kEtcPalErrInvalid
An invalid argument was provided to an API function.
Definition: error.h:62
@ kEtcPalErrOk
The call was successful, no error occurred.
Definition: error.h:51
@ kEtcPalErrBufSize
A buffer provided to a function was not big enough to hold the data that needed to be packed into it.
Definition: error.h:103
etcpal_iptype_t
Used to identify the type of IP address contained in a EtcPalIpAddr.
Definition: inet.h:53
bool etcpal_netint_is_up(unsigned int netint_index)
Determine whether a network interface is currently up and running.
Definition: netint.c:352
etcpal_error_t etcpal_netint_refresh_interfaces()
Refresh the list of network interfaces.
Definition: netint.c:324
etcpal_error_t etcpal_netint_get_interface_with_ip(const EtcPalIpAddr *ip, EtcPalNetintInfo *netint)
Get the network interface that has the specified IP address.
Definition: netint.c:146
etcpal_error_t etcpal_netint_get_interface_for_dest(const EtcPalIpAddr *dest, unsigned int *netint_index)
Get the network interface that the system will choose when routing an IP packet to the specified dest...
Definition: netint.c:225
etcpal_error_t etcpal_netint_get_default_interface(etcpal_iptype_t type, unsigned int *netint_index)
Get information about the default network interface.
Definition: netint.c:179
etcpal_error_t etcpal_netint_get_interfaces(EtcPalNetintInfo *netints, size_t *num_netints)
Get a list of network interfaces on the system (or just the number of interfaces).
Definition: netint.c:90
etcpal_error_t etcpal_netint_get_interfaces_for_index(unsigned int netint_index, EtcPalNetintInfo *netints, size_t *num_netints)
Get a list of network interfaces (or just the number of interfaces) that have the index specified.
Definition: netint.c:120
A description of a single address assigned to a network interface.
Definition: inet.h:355