Spinning Topp Logo BlackTopp Studios
inc
networkutilities.cpp
1 // © Copyright 2010 - 2014 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 _networkutilities_cpp
42 #define _networkutilities_cpp
43 
44 #include "Network/platformsocket.h.cpp"
45 
46 #include "Network/networkutilities.h"
47 
48 #include "stringtool.h"
49 
50 namespace Mezzanine
51 {
52  namespace Network
53  {
55  { return htons(ToConvert); }
56 
58  { return htonl(ToConvert); }
59 
61  { return ntohs(ToConvert); }
62 
64  { return ntohl(ToConvert); }
65 
66  ///////////////////////////////////////////////////////////////////////////////
67  // DNS Lookup
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // SocketResultContainer Methods
71 
73  RefCount(new Integer(1)),
74  Addresses(Results),
75  CurrAddress(Results)
76  { }
77 
79  { this->Aquire(Other); }
80 
82  { this->Release(); }
83 
85  {
86  this->RefCount = Other.RefCount;
87  this->Addresses = Other.Addresses;
88  this->CurrAddress = Other.Addresses;
89  ++(*RefCount);
90  }
91 
93  {
94  if( --(*RefCount) <= 0 ) {
95  delete this->RefCount;
96  freeaddrinfo(this->Addresses);
97  }
98  }
99 
100  ///////////////////////////////////////////////////////////////////////////////
101  // Element Access
102 
104  {
105  if( this->CurrAddress != NULL ) {
106  this->CurrDescription = ConvertToSocketDescription( (sockaddr_storage&)(*this->CurrAddress->ai_addr) );
108 
109  this->CurrAddress = this->CurrAddress->ai_next;
110  return &(this->CurrDescription);
111  }
112  return NULL;
113  }
114 
116  { this->CurrAddress = this->Addresses; }
117 
118  ///////////////////////////////////////////////////////////////////////////////
119  // Resolve Methods
120 
122  {
123  addrinfo Hints;
124  addrinfo* Results;
125 
126  memset(&Hints,0,sizeof(Hints));
127  Hints.ai_flags = AI_NUMERICHOST;
128  Hints.ai_family = ConvertLayer3ProtocolType(Address.GetProtocol());
129  Hints.ai_socktype = ConvertToSocketType(TLP);
130  Hints.ai_protocol = ( TLP == 0 ? 0 : ConvertLayer4ProtocolType(TLP) );
131 
132  int Result = getaddrinfo(Address.GetAsString().c_str(),( Port != 0 ? StringTools::ConvertToString(Port).c_str() : NULL ),&Hints,&Results);
133  if( Result != 0 ) {
135  }
136 
137  return SocketResultContainer(Results);
138  }
139 
141  {
142  addrinfo Hints;
143  addrinfo* Results;
144 
145  memset(&Hints,0,sizeof(Hints));
146  Hints.ai_family = ConvertLayer3ProtocolType(NLP);
147  Hints.ai_socktype = ConvertToSocketType(TLP);
148  Hints.ai_protocol = ( TLP == 0 ? 0 : ConvertLayer4ProtocolType(TLP) );
149 
150  int Result = getaddrinfo(DomainName.c_str(),( Port != 0 ? StringTools::ConvertToString(Port).c_str() : NULL ),&Hints,&Results);
151  if( Result != 0 ) {
153  }
154 
155  return SocketResultContainer(Results);
156  }
157  }//Network
158 }//Mezzanine
159 
160 #endif
int ConvertLayer4ProtocolType(const TransportLayerProtocol Protocol)
Converts from a Mezzanine transport protocol type to it's internal counterpart.
Thrown when an unknown internal error occurred.
Definition: exception.h:116
short int ConvertLayer3ProtocolType(const NetworkLayerProtocol Protocol)
Converts from a Mezzanine address protocol type to it's internal counterpart.
String ConvertToString(const Vector2 &ToConvert)
Converts a Vector2 into a string.
Definition: stringtool.cpp:291
NetworkLayerProtocol
This is an enum listing for recognized protocols on Layer 3 of the OSI model.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
int ConvertToSocketType(const TransportLayerProtocol Protocol)
Converts from a Mezzanine transport protocol type to it's internal counterpart.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
SocketDescription ConvertToSocketDescription(const sockaddr_storage &BerkAddr)
Converts a Berkeley sockets address to a Mezzanine socket descriptor.
SocketResultContainer(addrinfo *Results)
Internal constructor.
A special container class used to store the results from a call to "ResolveDomainName".
UInt16 ConvertNetworkToHostByteOrder(const UInt16 ToConvert)
Converts a UInt16 from the network byte order to host byte order.
TransportLayerProtocol SocketProtocol
The protocol to used by a given socket.
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
TransportLayerProtocol
This is an enum listing for recognized protocols on Layer 4 of the OSI model.
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
void Aquire(const SocketResultContainer &Other)
Duplicates the necessary data when creating a copy instance of a set of socket results.
This is a simple class for representing IP addresses used throughout the Network subsystem.
Definition: ipaddress.h:57
const SocketDescription * GetNextResult()
Gets/converts the current result and increments the cursor to the next result if able.
addrinfo * Addresses
A pointer to the internal structure holding the resulting addresses.
SocketResultContainer ResolveNumericHost(const IPAddress &Address, const UInt16 Port, const TransportLayerProtocol TLP)
Performs a DNS lookup with the provided domain name and port.
String GetAsString() const
Gets this IPAddress in a human readable format.
Definition: ipaddress.cpp:173
SocketDescription CurrDescription
The converted values for the current address being presented.
Network::NetworkLayerProtocol GetProtocol() const
Gets the type of IP address this is.
Definition: ipaddress.cpp:162
void Release()
Decrements the ref count and destroys all internal data if necessary.
A convenience class storing socket data that can be returned from utility methods and used to create ...
UInt16 ConvertHostToNetworkByteOrder(const UInt16 ToConvert)
Converts a UInt16 from the host byte order to network byte order.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Integer * RefCount
The count of instances that exist for these results.
void Reset()
Resets the current elements pointed to back to the beginning.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
SocketResultContainer ResolveDomainName(const String &DomainName, const UInt16 Port, const NetworkLayerProtocol NLP, const TransportLayerProtocol TLP)
Performs a DNS lookup with the provided domain name and service.
addrinfo * CurrAddress
A pointer to the current address being presented.