Spinning Topp Logo BlackTopp Studios
inc
ipaddress.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _networkipaddress_h
42 #define _networkipaddress_h
43 
44 #include "datatypes.h"
45 
46 #include "Network/networkenumerations.h"
47 
48 #include "XML/xml.h"
49 
50 namespace Mezzanine
51 {
52  namespace Network
53  {
54  ///////////////////////////////////////////////////////////////////////////////
55  /// @brief This is a simple class for representing IP addresses used throughout the Network subsystem.
56  ///////////////////////////////////////
58  {
59  public:
60  /// @brief Convenience type to store the binary representation of IP addresses.
61  typedef std::vector<Int8> AddressContainer;
62 
63  /// @brief The amount of space needed in a buffer to store the binary representation of an IPv4 address.
64  static const UInt16 IPv4BinaryLength;
65  /// @brief The amount of space needed in a buffer to store the binary representation of an IPv6 address.
66  static const UInt16 IPv6BinaryLength;
67  /// @brief The amount of space needed in a string to store the character representation of an IPv4 address.
68  static const UInt16 IPv4StringLength;
69  /// @brief The amount of space needed in a string to store the character representation of an IPv6 address.
70  static const UInt16 IPv6StringLength;
71 
72  /// @brief The loopback address for IPv4.
74  /// @brief The broadcast address for IPv4.
76  /// @brief The loopback address for IPv6.
78  protected:
79  /// @internal
80  /// @brief A resizable vector to store the binary IP address regardless of version.
81  AddressContainer InternalAddress;
82  public:
83  /// @brief Class Constructor.
84  IPAddress();
85  /// @brief IPv4 Integer Constructor.
86  /// @param Address The integer address to be set.
87  /// @param NBO Whether or not the passed in v4 address is already in Network-Byte-Order.
88  IPAddress(const UInt32 Address, const Boole NBO = false);
89  /// @brief IP Agnostic String Constructor.
90  /// @param Address The string storing the address to use.
91  IPAddress(const String& Address);
92  /// @brief IP Agnostic Binary Constructor.
93  /// @param Address The binary address to use.
94  IPAddress(const AddressContainer& Address);
95  /// @brief Copy Constructor.
96  /// @param Other The other IPAddress to be copied.
97  IPAddress(const IPAddress& Other);
98  /// @brief Class Destructor.
99  ~IPAddress();
100 
101  ///////////////////////////////////////////////////////////////////////////////
102  // Static Helpers
103 
104  /// @brief Resolves an IPv4 or IPv6 address as a String to it's binary representation.
105  /// @remarks There are some sane limitations in how this function can be used/interpreted. The format for IP addresses that
106  /// are acceptable is "XXX.XXX.XXX.XXX" for IPv4, or "XXXX:XXXX:XXXX:XXXX::XXXX" (as an example) for IPv6. Other formats or
107  /// including a port number will cause this to fail. The returned container should always be of size 0, 4, or 16. A size of
108  /// zero indicates a failure, whereas a size of 4 is an IPv4 address, and a size of 16 is an IPv6 address. @n @n
109  /// This method will NOT perform DNS resolutions. Only numeric representations.
110  /// @param Address The address to be resolved to binary.
111  /// @return Returns a container storing the binary representation of the address provided.
112  static AddressContainer Resolve(const String& Address);
113  /// @brief Resolves an IPv4 or IPv6 address in it's binary representation to a human readable String.
114  /// @remarks The returned string is straightforward for IPv6, in typical "XXX.XXX.XXX.XXX" notation. For IPv6 addresses this
115  /// function will use the shortest notation that can be expressed with the address, and all letters will be in lower case.
116  /// @param Address The address to be resolved to a String.
117  /// @return Returns a String containing a human readable IP address, or an empty String if there was a failure.
118  static String Resolve(const AddressContainer& Address);
119 
120  ///////////////////////////////////////////////////////////////////////////////
121  // Utility
122 
123  /// @brief Gets the type of IP address this is.
124  /// @return Returns a NetworkLayerProtocol enum value indicating if this IP address is v4, v6 or invalid.
125  Network::NetworkLayerProtocol GetProtocol() const;
126  /// @brief Gets this IPAddress in a human readable format.
127  /// @return Returns a String containing a human friendly form of this IPAddress.
128  String GetAsString() const;
129 
130  /// @brief Sets the binary form of the address being stored.
131  /// @remarks Only use this if you KNOW your formatting is correct or are returning a container generated by IPAddress::Resolve.
132  /// @param Address The binary address to be set.
133  void SetBinaryAddress(const AddressContainer& Address);
134  /// @brief Gets the stored binary address of this IPAddress.
135  /// @return Returns a const AddressContainer reference that is storing the binary address.
136  const AddressContainer& GetBinaryAddress() const;
137 
138  /// @brief Sets a version 4 IP address value.
139  /// @warning This method will allocate for an IPv4 address regardless of it's current contents and assign it's value.
140  /// @param Address The v4 IP Address to set.
141  /// @param NBO Whether or not the passed in v4 address is already in Network-Byte-Order.
142  void SetV4Address(const UInt32 Address, const Boole NBO = false);
143  /// @brief Gets a version 4 IP address value.
144  /// @param NBO Whether or not the returned v4 address should be returned in Network-Byte-Order.
145  /// @return Returns a UInt32 containing the full IPv4 address currently set, or 0 if no IPAddress is allocated or if allocated for an IPv6 address.
146  UInt32 GetV4Address(const Boole NBO = false) const;
147 
148  ///////////////////////////////////////////////////////////////////////////////
149  // Operators
150 
151  /// @brief Assignment operator.
152  /// @param Other The other IPAddress to be copied from.
153  void operator=(const IPAddress& Other);
154 
155  /// @brief Equality operator.
156  /// @param Other The other IPAddress to compare to.
157  /// @return Returns true if the two IPAddresses are identical, false otherwise.
158  Boole operator==(const IPAddress& Other) const;
159  /// @brief Inequality operator.
160  /// @param Other The other IPAddress to compare to.
161  /// @return Returns false if the two IPAddresses are identical, true otherwise.
162  Boole operator!=(const IPAddress& Other) const;
163 
164  ///////////////////////////////////////////////////////////////////////////////
165  // Serialization
166 
167  /// @brief Convert this class to an XML::Node ready for serialization.
168  /// @param ParentNode The point in the XML hierarchy that this IPAddress should be appended to.
169  void ProtoSerialize(XML::Node& ParentNode) const;
170  /// @brief Take the data stored in an XML Node and overwrite this object with it.
171  /// @param SelfRoot An XML::Node containing the data to populate this class with.
172  void ProtoDeSerialize(const XML::Node& SelfRoot);
173 
174  /// @brief Get the name of the the XML tag the IPAddress class will leave behind as its instances are serialized.
175  /// @return A string containing the name of this class.
176  static String GetSerializableName();
177  };//IPAddress
178  }//Network
179 }//Mezzanine
180 
181 #endif
static const UInt16 IPv6BinaryLength
The amount of space needed in a buffer to store the binary representation of an IPv6 address...
Definition: ipaddress.h:66
static const UInt16 IPv6StringLength
The amount of space needed in a string to store the character representation of an IPv6 address...
Definition: ipaddress.h:70
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
static const String IPv6LoopbackAddress
The loopback address for IPv6.
Definition: ipaddress.h:77
NetworkLayerProtocol
This is an enum listing for recognized protocols on Layer 3 of the OSI model.
All the definitions for datatypes as well as some basic conversion functions are defined here...
static const String IPv4BroadcastAddress
The broadcast address for IPv4.
Definition: ipaddress.h:75
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
AddressContainer InternalAddress
A resizable vector to store the binary IP address regardless of version.
Definition: ipaddress.h:81
This is a simple class for representing IP addresses used throughout the Network subsystem.
Definition: ipaddress.h:57
static const String IPv4LoopbackAddress
The loopback address for IPv4.
Definition: ipaddress.h:73
static const UInt16 IPv4BinaryLength
The amount of space needed in a buffer to store the binary representation of an IPv4 address...
Definition: ipaddress.h:64
static const UInt16 IPv4StringLength
The amount of space needed in a string to store the character representation of an IPv4 address...
Definition: ipaddress.h:68
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::vector< Int8 > AddressContainer
Convenience type to store the binary representation of IP addresses.
Definition: ipaddress.h:61
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159