The Nebula Device 3: Win32::Win32IpAddress Class Reference

The Nebula Device 3

Win32::Win32IpAddress Class Reference

#include <win32ipaddress.h>


Detailed Description

Represents an IP address, consisting of a IPv4 host address and a port number. Performs automatic name lookup on the host name. Can extract address information from an URI and automatically converts host names to addresses, and offers the special hostnames "localhost", "any", "broadcast", "self" and "inetself" where:

  • "localhost" will translate to 127.0.0.1
  • "any" will translate to INADDR_ANY, which is 0.0.0.0
  • "broadcast" will translate to INADDR_BROADCAST, which is 255.255.255.255
  • "self" will translate to the first valid tcp/ip address for this host (there may be more then one address bound to the host)
  • "inetself" will translate to the first host address which is not a LAN address (which is not a class A, B, or C network) if none such exists the address will fall back to "self"

(C) 2006 Radon Labs GmbH

Public Member Functions

 Win32IpAddress ()
 default constructor
 Win32IpAddress (const Win32IpAddress &rhs)
 copy constructor
 Win32IpAddress (const IO::URI &uri)
 construct from URI
 Win32IpAddress (const Util::String &hostName, ushort portNumber)
 construct from host name and port number
bool operator== (const Win32IpAddress &rhs) const
 equality operator
bool operator< (const Win32IpAddress &rhs) const
 less-then operator
bool operator> (const Win32IpAddress &rhs) const
 greater-then operator
void ExtractFromUri (const IO::URI &uri)
 extract host name and port number from URI
void SetHostName (const Util::String &hostName)
 set host name
const Util::StringGetHostName () const
 get host name
void SetPort (ushort port)
 set port number
ushort GetPort () const
 get port number
const Util::StringGetHostAddr () const
 get the ip address resulting from the host name as string

Protected Member Functions

void SetSockAddr (const sockaddr_in &addr)
 set sockaddr_in directly
const sockaddr_in & GetSockAddr () const
 get sockaddr_in field

Static Protected Member Functions

static bool GetHostByName (const Util::String &hostName, in_addr &outAddr)
 perform address resolution, understands special host names
static bool IsInetAddr (const in_addr *addr)
 return true if an address is an internet address (not class A,B,C)

Member Function Documentation

void Win32::Win32IpAddress::ExtractFromUri ( const IO::URI uri  ) 

extract host name and port number from URI

Extract the host name and optionally the port number from the provided URI. If no port number is set in the URI, the current port number will be left as is. If the host name is empty, it will be set to "localhost".

void Win32::Win32IpAddress::SetHostName ( const Util::String hostName  ) 

set host name

Set the host name, and immediately convert it to an ip address. This accepts the special hostnames "any", "broadcast", "localhost", "self" and "inetself". The result ip address can be returned in string form with the method GetAddrAsString().

const String & Win32::Win32IpAddress::GetHostName (  )  const

get host name

Get the host name.

void Win32::Win32IpAddress::SetPort ( ushort  port  ) 

set port number

Set the port number. Will be translated to network byte order internally.

ushort Win32::Win32IpAddress::GetPort (  )  const

get port number

Get the port number in host byte order.

const String & Win32::Win32IpAddress::GetHostAddr (  )  const

get the ip address resulting from the host name as string

Return the in address as string.

void Win32::Win32IpAddress::SetSockAddr ( const sockaddr_in &  sa  )  [protected]

set sockaddr_in directly

Set the address directly from a sockaddr_in struct. This will set the host name to the string representation of the host address.

const sockaddr_in & Win32::Win32IpAddress::GetSockAddr (  )  const [protected]

get sockaddr_in field

Get the sockaddr_in struct, which has either been set directly with SetSockAddr() or indirectly through host name, port number or from an URI.

bool Win32::Win32IpAddress::GetHostByName ( const Util::String hostName,
in_addr &  outAddr 
) [static, protected]

perform address resolution, understands special host names

This resolves a host name into a IPv4 ip address. The ip address is returned in network byte order in the hostAddress argument. The return value indicates whether the operation was successful. The following special hostnames can be defined:

  • "any" resolves to INADDR_ANY (0.0.0.0)
  • "broadcast" resolves to INADDR_BROADCAST (255.255.255.255)
  • "localhost" resolves to 127.0.0.1
  • "self" resolves to the first address of this host
  • "inetself" resolves to the first address which is not a LAN address

An empty host name is invalid. A hostname can also be an address string of the form xxx.yyy.zzz.www.

bool Win32::Win32IpAddress::IsInetAddr ( const in_addr *  addr  )  [static, protected]

return true if an address is an internet address (not class A,B,C)

This method checks if the provided address is an "internet" address, not a LAN address (not a class A, B or C network address).