WSF
WsfCommAddress.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2017 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef WSFCOMMADDRESS_HPP
13#define WSFCOMMADDRESS_HPP
14
15#include "wsf_export.h"
16
17#include <array>
18#include <string>
19#include <vector>
20
21#include "UtScriptAccessible.hpp"
22
23class UtInput;
24
25namespace wsf
26{
27namespace comm
28{
29
34struct IPv4
35{
36 static constexpr size_t cBIT_SIZE = 32;
37 static constexpr size_t cNUM_FIELDS = 4;
38 static constexpr const char* cDELIMITER = ".";
39 static constexpr std::uint32_t cMASK_MAX = 0xFFFFFFFF;
40 static constexpr std::uint32_t cMASK_FIELD = 0xFF;
41 static constexpr const char* cNULL_ADDRESS = "0.0.0.0"; // Uninitialized address indicator
42 static constexpr size_t cDEFAULT_CIDR = 24;
43 static constexpr const char* cCIDR_DELIMITER = "/";
44 using ByteArray = std::array<unsigned char, 4>;
45};
46
56class WSF_EXPORT Address : public UtScriptAccessible
57{
58public:
59 Address() = default;
60 Address(const std::string& aAddress, size_t aSubnetMaskBitLength = IPv4::cDEFAULT_CIDR);
61
66 explicit Address(size_t aSubnetMaskBitLength);
67
68 virtual ~Address() = default;
69
70 const char* GetScriptClassName() const override;
71
72 void SetAddress(const std::string& aAddress, size_t aSubnetBitLength = IPv4::cDEFAULT_CIDR);
73
80 static Address ProcessInput(UtInput& aInput);
81
84 static Address ProcessInput(std::string aInput);
85
88 static bool IsValidAddress(const std::string& aPossibleAddress);
89
96 static size_t GetCIDR_ValueFromString(std::string& aPossibleAddress);
97
104 void GenerateAddress(const std::string& aGenerationToken, size_t aSubnetBitLength = IPv4::cDEFAULT_CIDR);
105
106 const std::string& GetAddress() const { return mAddress; }
107 size_t GetSubnetMaskBitLength() const { return mSubnetMaskBitLength; }
108 const std::string& GetRoutingPrefix() const { return mRoutingPrefix; }
109 const std::string& GetSubnet() const { return mSubnet; }
110 size_t GetNumHostAddresses() const;
111 bool IsNull() const { return (mAddress == IPv4::cNULL_ADDRESS); }
112 size_t GetBinaryInteger() const { return mBinaryIntegerValue; }
113
114 bool operator==(const Address& aRhs) const;
115 bool operator!=(const Address& aRhs) const;
116 bool operator<(const Address& aRhs) const;
117
126 Address& operator++(); //++Address
127 Address operator++(int); // Address++
128
129 friend WSF_EXPORT std::ostream& operator<<(std::ostream& aOut, const Address& aAddress);
130
132 template<typename T>
133 void Serialize(T& aBuff)
134 {
135 aBuff& mAddress& mRoutingPrefix& mSubnet& mSubnetMaskBitLength;
136 }
137
141 static size_t GetStringValue(const std::string& aString);
142
147 static bool CheckInAddressRange(const Address& aNetworkAddress, const Address& aCheckAddress);
148
152 std::string GetGenerationToken() const { return mAddressToken; }
153
155 std::string GetBroadcastAddress() const;
156
157private:
160 std::vector<size_t> TranslateIntegerIP(size_t aIP_Value) const;
161
162 static bool IsNumber(const std::string& aString);
163
166 void PopulateAddressFromBytes();
167
168 std::string mAddress{IPv4::cNULL_ADDRESS};
169 std::string mRoutingPrefix{""};
170 std::string mSubnet{""};
171 uint32_t mSubnetIntegerValue{0};
172 size_t mSubnetMaskBitLength{IPv4::cDEFAULT_CIDR};
173 uint32_t mBinaryIntegerValue{0};
174 IPv4::ByteArray mBytes{{0, 0, 0, 0}};
175 std::string mAddressToken{""};
176};
177
178} // namespace comm
179} // namespace wsf
180
181UT_MAP_CLASS_TO_SCRIPT_NAME(wsf::comm::Address, "WsfAddress")
182
183
184namespace std
185{
186template<>
187struct hash<wsf::comm::Address>
188{
189 size_t operator()(const wsf::comm::Address& aRhs) const
190 {
191 return hash<size_t>()(wsf::comm::Address::GetStringValue(aRhs.GetAddress()));
192 }
193};
194
195} // namespace std
196
197#endif
wsf::comm::Address Address
Definition WsfScriptCommRouterClass.cpp:29
bool operator!=(int aId, const WsfStringInt &aRhs)
Not equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:75
bool operator==(int aId, const WsfStringInt &aRhs)
Equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:69
std::ostream & operator<<(std::ostream &aStream, const WsfTSPI &aTSPI)
Stream out operator (typically, to a file); units are converted on output.
Definition WsfTSPI.cpp:92
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
Definition WsfCommAddress.hpp:57
bool IsNull() const
Definition WsfCommAddress.hpp:111
const std::string & GetRoutingPrefix() const
Definition WsfCommAddress.hpp:108
static size_t GetCIDR_ValueFromString(std::string &aPossibleAddress)
Definition WsfCommAddress.cpp:383
virtual ~Address()=default
static Address ProcessInput(UtInput &aInput)
Definition WsfCommAddress.cpp:305
std::string GetGenerationToken() const
Definition WsfCommAddress.hpp:152
static bool IsValidAddress(const std::string &aPossibleAddress)
Definition WsfCommAddress.cpp:332
size_t GetSubnetMaskBitLength() const
Definition WsfCommAddress.hpp:107
void GenerateAddress(const std::string &aGenerationToken, size_t aSubnetBitLength=IPv4::cDEFAULT_CIDR)
Definition WsfCommAddress.cpp:255
void Serialize(T &aBuff)
For XIO usage.
Definition WsfCommAddress.hpp:133
size_t GetBinaryInteger() const
Definition WsfCommAddress.hpp:112
const std::string & GetSubnet() const
Definition WsfCommAddress.hpp:109
static size_t GetStringValue(const std::string &aString)
Definition WsfCommAddress.cpp:283
void SetAddress(const std::string &aAddress, size_t aSubnetBitLength=IPv4::cDEFAULT_CIDR)
Definition WsfCommAddress.cpp:44
const std::string & GetAddress() const
Definition WsfCommAddress.hpp:106
const char * GetScriptClassName() const override
Template specialization for wsf::comm::Address. Allows usage of Address in std::unordered_map/set.
Definition WsfCommAddress.hpp:185
Definition WsfComm.cpp:34
Function definitions for those who need run-time access to the version.
Definition WsfComm.cpp:32
size_t operator()(const wsf::comm::Address &aRhs) const
Definition WsfCommAddress.hpp:189
Definition WsfCommAddress.hpp:35
static constexpr const char * cCIDR_DELIMITER
Definition WsfCommAddress.hpp:43
static constexpr std::uint32_t cMASK_MAX
Definition WsfCommAddress.hpp:39
static constexpr std::uint32_t cMASK_FIELD
Definition WsfCommAddress.hpp:40
static constexpr const char * cDELIMITER
Definition WsfCommAddress.hpp:38
static constexpr size_t cNUM_FIELDS
Definition WsfCommAddress.hpp:37
static constexpr const char * cNULL_ADDRESS
Definition WsfCommAddress.hpp:41
static constexpr size_t cDEFAULT_CIDR
Definition WsfCommAddress.hpp:42
std::array< unsigned char, 4 > ByteArray
Definition WsfCommAddress.hpp:44
static constexpr size_t cBIT_SIZE
Definition WsfCommAddress.hpp:36
Copyrights Multiple, All Rights Reserved