LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_netaddress.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_NETADDRESS_H
26 #define NRG_NETADDRESS_H
27 #include "nrg_core.h"
28 
29 namespace nrg {
30 
32 class NetAddress {
33 public:
35  NetAddress();
36 
38  NetAddress(const char* name, const char* port);
39 
41  NetAddress(const struct sockaddr_in& in);
42 
44  NetAddress(const struct sockaddr_in6& in6);
45 
47  NetAddress(const struct sockaddr_storage& s);
48 
50  NetAddress(const struct sockaddr& s);
51 
53  bool resolve(const char* name, const char* port);
54 
56  bool isValid() const;
57 
59  const char* getIP() const;
60 
62  int getFamily() const;
63 
65  uint16_t getPort() const;
66 
68  const struct sockaddr* toSockAddr(socklen_t& out_size) const;
69 
71  friend bool operator==(const NetAddress& a, const NetAddress& b);
73  friend bool operator!=(const NetAddress& a, const NetAddress& b);
75  friend bool operator<(const NetAddress& a, const NetAddress& b);
76 private:
77  mutable char text[INET6_ADDRSTRLEN];
78  struct sockaddr_storage addr;
79  socklen_t addr_len;
80 };
81 
82 }
83 
84 #endif
bool isValid() const
Queries whether or not the contained address is valid.
const char * getIP() const
Returns the contained IP address as a string, if no address is contained it returns "" - do not free...
NetAddress()
Default constructor.
friend bool operator<(const NetAddress &a, const NetAddress &b)
Less-than operator for total-ordered containers like std::map.
friend bool operator==(const NetAddress &a, const NetAddress &b)
Equality operator.
int getFamily() const
Returns the family of the contained address, (AF_INET, AF_INET6 or AF_UNSPEC)
const struct sockaddr * toSockAddr(socklen_t &out_size) const
Returns a sockaddr* representation of the NetAddress to be used with POSIX socket functions - do not ...
bool resolve(const char *name, const char *port)
Resolve the given IP/Hostname and Port/Service into an address, returning true on success...
Common defines and includes used by all the other nrg header files.
friend bool operator!=(const NetAddress &a, const NetAddress &b)
Unequality operator.
Class to wrap the various POSIX sockaddrs and resolve hostnames.
Definition: nrg_netaddress.h:32
uint16_t getPort() const
Returns the contained address' port as a short in host byte order.