TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::NetAddress Class Reference

#include <NetAddress.h>

Public Types

enum  { DEFAULT_ADAPTER_HOST = 0 }
 

Public Member Functions

 NetAddress (uint32 host, uint16 port=0)
 
 NetAddress (const std::string &hostname, uint16 port)
 
 NetAddress (const std::string &hostnameAndPort)
 
 NetAddress ()
 
void serialize (class BinaryOutput &b) const
 
void deserialize (class BinaryInput &b)
 
bool ok () const
 Returns true if this is not an illegal address. More...
 
uint32 ip () const
 Returns a value in host format (i.e., don't worry about endian issues) More...
 
uint16 port () const
 
std::string ipString () const
 
std::string toString () const
 
std::string hostname () const
 

Static Public Member Functions

static NetAddress broadcastAddress (uint16 port)
 Creates a UDP broadcast address for use with a G3D::LightweightConduit. More...
 
static void localHostAddresses (Array< NetAddress > &array)
 
static std::string localHostname ()
 

Private Member Functions

void init (uint32 host, uint16 port)
 
void init (const std::string &hostname, uint16 port)
 
 NetAddress (const SOCKADDR_IN &a)
 
 NetAddress (const struct in_addr &addr, uint16 port=0)
 

Private Attributes

SOCKADDR_IN addr
 

Friends

class NetworkDevice
 
class LightweightConduit
 
class ReliableConduit
 

Member Enumeration Documentation

anonymous enum
Enumerator
DEFAULT_ADAPTER_HOST 

Use the host portion of the IP address of the default adapter on this machine.

34  {
38  // Must match ENET_HOST_ANY
40  };
Definition: NetAddress.h:39

Constructor & Destructor Documentation

G3D::NetAddress::NetAddress ( const SOCKADDR_IN a)
private
122  {
123  addr = a;
124 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
G3D::NetAddress::NetAddress ( const struct in_addr &  addr,
uint16  port = 0 
)
private
127  {
128  #ifdef G3D_WINDOWS
129  init(ntohl(addr.S_un.S_addr), port);
130  #else
131  init(htonl(addr.s_addr), port);
132  #endif
133 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
uint16 port() const
Definition: NetAddress.h:91

+ Here is the call graph for this function:

G3D::NetAddress::NetAddress ( uint32  host,
uint16  port = 0 
)
explicit

In host byte order.

See also
DEFAULT_ADAPTER_HOST
103  {
104  init(hostip, port);
105 }
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
uint16 port() const
Definition: NetAddress.h:91

+ Here is the call graph for this function:

G3D::NetAddress::NetAddress ( const std::string &  hostname,
uint16  port 
)
Parameters
portSpecified in host byte order (i.e., don't worry about endian issues)
66  {
67  init(hostname, port);
68 }
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
uint16 port() const
Definition: NetAddress.h:91
std::string hostname() const
G3D::NetAddress::NetAddress ( const std::string &  hostnameAndPort)
explicit
Parameters
hostnameAndPortin the form "hostname:port" or "ip:port"
113  {
114 
115  Array<std::string> part = stringSplit(hostnameAndPort, ':');
116 
117  debugAssert(part.length() == 2);
118  init(part[0], atoi(part[1].c_str()));
119 }
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
#define debugAssert(exp)
Definition: debugAssert.h:160
G3D::Array< std::string > stringSplit(const std::string &x, char splitChar)
Definition: stringutils.cpp:230

+ Here is the call graph for this function:

G3D::NetAddress::NetAddress ( )
46  {
47  System::memset(&addr, 0, sizeof(addr));
48 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Function Documentation

NetAddress G3D::NetAddress::broadcastAddress ( uint16  port)
static

Creates a UDP broadcast address for use with a G3D::LightweightConduit.

Deprecated:
Use G3D::NetworkDevice::broadcastAddressArray()

UDP broadcast allows one machine to send a packet to all machines on the same local network. The IP portion of the address is 0xFFFFFFFF, which indicates "broadcast" to the underlying socket API. This feature is not available with the connection-based TCP protocol abstracted by G3D::ReliableConduit; use multisend instead.

108  {
109  return NetAddress(NetworkDevice::instance()->broadcastAddressArray()[0], port);
110 }
NetAddress()
Definition: NetAddress.cpp:46
uint16 port() const
Definition: NetAddress.h:91
static NetworkDevice * instance()
Definition: NetworkDevice.cpp:109

+ Here is the call graph for this function:

void G3D::NetAddress::deserialize ( class BinaryInput b)
164  {
165  uint32 i;
166  uint16 p;
167 
168  i = b.readUInt32();
169  p = b.readUInt16();
170 
171  init(i, p);
172 }
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
uint32_t uint32
Definition: Define.h:150
uint16_t uint16
Definition: Define.h:151

+ Here is the call graph for this function:

std::string G3D::NetAddress::hostname ( ) const

Name of this address, without the domain. Performs reverse DNS lookup on this address. This may make a network connection to a DNS server and block until that communication completes if the address is one that has not been recently checked.

void G3D::NetAddress::init ( uint32  host,
uint16  port 
)
private

Host byte order

50  {
51  if ((host != 0) || (port != 0)) {
52  addr.sin_family = AF_INET;
53  addr.sin_port = htons(port);
54  if (host == 0) {
55  host = INADDR_ANY;
56  }
57  addr.sin_addr.s_addr = htonl(host);
58  } else {
59  System::memset(&addr, 0, sizeof(addr));
60  }
61 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695
uint16 port() const
Definition: NetAddress.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::NetAddress::init ( const std::string &  hostname,
uint16  port 
)
private
73  {
74 
75  uint32 addr;
76 
77  if (hostname == "") {
78  addr = INADDR_NONE;
79  } else {
80  addr = inet_addr(hostname.c_str());
81  }
82 
83  // The address wasn't in numeric form, resolve it
84  if (addr == INADDR_NONE) {
85  // Get the IP address of the server and store it in host
86  struct hostent* host = gethostbyname(hostname.c_str());
87 
88  if (host == NULL) {
89  init(0, 0);
90  return;
91  }
92 
93  System::memcpy(&addr, host->h_addr_list[0], host->h_length);
94  }
95 
96  if (addr != INADDR_NONE) {
97  addr = ntohl(addr);
98  }
99  init(addr, port);
100 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
static void memcpy(void *dst, const void *src, size_t numBytes)
Definition: System.cpp:643
arena_t NULL
Definition: jemalloc_internal.h:624
void init(uint32 host, uint16 port)
Definition: NetAddress.cpp:50
uint32_t uint32
Definition: Define.h:150
uint16 port() const
Definition: NetAddress.h:91
std::string hostname() const

+ Here is the call graph for this function:

uint32 G3D::NetAddress::ip ( ) const
inline

Returns a value in host format (i.e., don't worry about endian issues)

86  {
87  return ntohl(addr.sin_addr.s_addr);
88  //return ntohl(addr.sin_addr.S_un.S_addr);
89  }
SOCKADDR_IN addr
Definition: NetAddress.h:30

+ Here is the caller graph for this function:

std::string G3D::NetAddress::ipString ( ) const
180  {
181  return format("%s", inet_ntoa(*(in_addr*)&(addr.sin_addr)));
182 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::NetAddress::localHostAddresses ( Array< NetAddress > &  array)
static
135  {
136  array.resize(0);
137 
138  char ac[256];
139 
140  if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
141  Log::common()->printf("Error while getting local host name\n");
142  return;
143  }
144 
145  struct hostent* phe = gethostbyname(ac);
146  if (phe == 0) {
147  Log::common()->printf("Error while getting local host address\n");
148  return;
149  }
150 
151  for (int i = 0; (phe->h_addr_list[i] != 0); ++i) {
152  struct in_addr addr;
153  memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
154  array.append(NetAddress(addr));
155  }
156 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
NetAddress()
Definition: NetAddress.cpp:46
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: Log.cpp:119
static Log * common()
Definition: Log.cpp:100

+ Here is the call graph for this function:

static std::string G3D::NetAddress::localHostname ( )
static

Name of the local machine machine, without the domain. The value is cached after the first call.

bool G3D::NetAddress::ok ( ) const

Returns true if this is not an illegal address.

175  {
176  return addr.sin_family != 0;
177 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
uint16 G3D::NetAddress::port ( ) const
inline
91  {
92  return ntohs(addr.sin_port);
93  }
SOCKADDR_IN addr
Definition: NetAddress.h:30

+ Here is the caller graph for this function:

void G3D::NetAddress::serialize ( class BinaryOutput b) const
158  {
159  b.writeUInt32(ip());
160  b.writeUInt16(port());
161 }
uint32 ip() const
Returns a value in host format (i.e., don't worry about endian issues)
Definition: NetAddress.h:86
uint16 port() const
Definition: NetAddress.h:91

+ Here is the call graph for this function:

std::string G3D::NetAddress::toString ( ) const
185  {
186  return ipString() + format(":%d", ntohs(addr.sin_port));
187 }
SOCKADDR_IN addr
Definition: NetAddress.h:30
std::string ipString() const
Definition: NetAddress.cpp:180
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class LightweightConduit
friend
friend class NetworkDevice
friend
friend class ReliableConduit
friend

Member Data Documentation

SOCKADDR_IN G3D::NetAddress::addr
private

The documentation for this class was generated from the following files: