logical_network_interface.hpp
1 #pragma once
2 
3 #include "../global.hpp"
4 
5 #include "../network_connection.hpp"
6 #include "gevserver.hpp"
7 
8 #include "../_cexports/c_img.h"
9 
10 namespace Cvb
11 {
12 CVB_BEGIN_INLINE_NS
13 
14 template <>
15 inline HandleGuard<CExports::GEVIFACELIST>::HandleGuard(void *handle) noexcept
16  : HandleGuard<CExports::GEVIFACELIST>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
17 {
18 }
19 
20 namespace GevServer
21 {
27 {
31  typedef std::uint16_t PortType;
32 
39  {
40  HandleGuard<CExports::GEVIFACELIST> guard(CVB_CALL_CAPI(CreateInterfaceList()));
41  if (!guard.Handle())
42  throw std::runtime_error("Failed to create interface list");
43 
44  auto numInterfaces = CVB_CALL_CAPI(ILCount(guard.Handle()));
46 
47  for (std::size_t i = 0; i < numInterfaces; ++i)
48  {
49  CExports::gsipv4 ip;
50  auto result = CVB_CALL_CAPI(ILIPv4Address(guard.Handle(), i, ip));
51  if (result < 0)
52  throw std::runtime_error("Error retrieving IP");
53 
54  CExports::gsipv4 subnetMask;
55  result = CVB_CALL_CAPI(ILIPv4SubnetMask(guard.Handle(), i, subnetMask));
56 
59  }
60  return interfaces;
61  }
62 
63  LogicalNetworkInterface(const IPAddressType &ip, const IPv4MaskType &ipv4Mask) : ipAddress_{ip}, ipv4Mask_{ipv4Mask}
64  {
65  }
66 
67  LogicalNetworkInterface(const IPAddressTypeUInt &ipUInt, const IPv4MaskType &ipv4Mask)
68  : ipAddressUInt_{ipUInt}, ipv4Mask_{ipv4Mask}
69  {
70  }
71 
75  IPAddressType IPAddress() const { return ipAddress_; }
76 
80  void SetIPAddress(const IPAddressType &ipAddress)
81  {
82  ipAddress_ = ipAddress;
83  ipAddressUInt_ = Cvb::NetworkConnection::ToIPAddressUInt(ipAddress_);
84  }
85 
89  IPAddressTypeUInt IPAddressUInt() const { return ipAddressUInt_; }
90 
94  void SetIPAddressUInt(const IPAddressTypeUInt &ipAddressUInt)
95  {
96  ipAddressUInt_ = ipAddressUInt;
97  ipAddress_ = Cvb::NetworkConnection::ToIPAddress(ipAddressUInt_);
98  }
99 
103  IPAddressType IPv4Mask() const { return ipv4Mask_; }
104 
108  void IPv4Mask(const IPAddressType &ipv4Mask) { ipv4Mask_ = ipv4Mask; }
109 
110  IPAddressType ipAddress_;
111  IPAddressTypeUInt ipAddressUInt_;
112  IPv4MaskType ipv4Mask_;
113 };
114 }
115 CVB_END_INLINE_NS
116 }
An IP network interface the GevServer can be bound to.
Definition: logical_network_interface.hpp:26
static IPAddressUInt ToIPAddressUInt(const IPAddress &ip)
Creates an uint ip . from the given IPAddress
Definition: network_connection.hpp:64
void IPv4Mask(const IPAddressType &ipv4Mask)
Sets the subnet mask of the Address.
Definition: logical_network_interface.hpp:108
static IPAddress ToIPAddress(const IPAddressUInt &ipAddressInt)
Creates an IPAddress from the given uint ip .
Definition: network_connection.hpp:42
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
void SetIPAddress(const IPAddressType &ipAddress)
Sets the address of the interface as string and converted uint.
Definition: logical_network_interface.hpp:80
IPAddressTypeUInt IPAddressUInt() const
Gets the address of the interface as unsigned integer.
Definition: logical_network_interface.hpp:89
STL class.
IPAddressType IPv4Mask() const
Gets the subnet mask of the Address.
Definition: logical_network_interface.hpp:103
static std::vector< LogicalNetworkInterface > GetAllAvailable()
Gets all available network interfaces usable by the GevServer.
Definition: logical_network_interface.hpp:38
void SetIPAddressUInt(const IPAddressTypeUInt &ipAddressUInt)
Sets the address of the interface as unsigned integer and converted string ip address.
Definition: logical_network_interface.hpp:94
IPAddressType IPAddress() const
Gets the address of the interface.
Definition: logical_network_interface.hpp:75