Base Framework
Public Types | Public Member Functions | Static Public Member Functions | List of all members
InetAddress Class Reference

Internet Protocol address. More...

#include <base/net/InetAddress.h>

Inherits Object.

Public Types

enum  Family { IP_VERSION_4, IP_VERSION_6 }
 
enum  AddressType {
  IPV4 = 1, IPV6 = 2, UNSPECIFIED = 4, LOOPBACK = 8,
  MULTICAST = 16, LINK_LOCAL = 32, SITE_LOCAL = 64, IPV4_MAPPED = 128,
  IPV4_COMPATIBLE = 256, CLASS_A = 512, CLASS_B = 1024, CLASS_C = 2048,
  CLASS_D = 4096, CLASS_E = 8192
}
 
enum  MulticastFlags { MULTICAST_TRANSIENT = 1 }
 
enum  MulticastScope {
  RESERVED_0, NODE_LOCAL_SCOPE = 1, LINK_LOCAL_SCOPE = 2, RESERVED_3,
  RESERVED_4, SITE_LOCAL_SCOPE = 5, RESERVED_6, RESERVED_7,
  ORGANIZATION_LOCAL_SCOPE = 8, RESERVED_9, RESERVED_10, RESERVED_11,
  RESERVED_12, RESERVED_13, GLOBAL_SCOPE, RESERVED_15
}
 

Public Member Functions

 InetAddress () noexcept
 
 InetAddress (const uint8 *address, Family family) noexcept
 
 InetAddress (const String &address)
 
 InetAddress (const String &address, Family family)
 
 InetAddress (const InetAddress &copy) noexcept
 
InetAddressoperator= (const InetAddress &assign) noexcept
 
Family getFamily () const noexcept
 
bool isIPv4 () const noexcept
 
bool isIPv6 () const noexcept
 
const uint32 * getWords () const noexcept
 
const uint8 * getAddress () const noexcept
 
const uint8 * getIPv4Address () const noexcept
 
String getHostName (bool fullyQualified=false) const
 
bool operator== (const InetAddress &compare) const noexcept
 
bool operator!= (const InetAddress &compare) const noexcept
 
bool isSynonymous (const InetAddress &eq) const noexcept
 
bool isUnspecified () const noexcept
 
bool isLoopback () const noexcept
 
bool isMulticast () const noexcept
 
unsigned int getMulticastFlags () const noexcept
 
unsigned int getMulticastScope () const noexcept
 
bool isLinkLocal () const noexcept
 
bool isSiteLocal () const noexcept
 
bool isIPv4Mapped () const noexcept
 
bool isIPv4Compatible () const noexcept
 
unsigned int getType () const noexcept
 
bool convertToIPv6 () noexcept
 
bool convertToIPv4 () noexcept
 
void setAddress (const uint8 *address, Family family) noexcept
 

Static Public Member Functions

static bool isIPv4 (const String::ReadIterator begin, const String::ReadIterator end) noexcept
 
static bool isIPv6 (const String::ReadIterator begin, const String::ReadIterator end) noexcept
 
static String getLocalHost ()
 
static List< InetAddressgetAddressesByName (const String &name)
 
static InetAddress getAddressByName (const String &name)
 

Detailed Description

Internet Protocol address.

This class represents an Internet Protocol (IP) address (supports both IPv4 and IPv6 addresses). IPv6 addresses can be represented on platforms without builtin IPv6 support. However, using an IPv6 address on platforms without direct support requires implicit conversion to an IPv4 address. If such a conversion isn't possible a NetworkException is raised. See RFC 2373.

Version
1.2
Examples
testsuite/client.cpp, testsuite/echod.cpp, testsuite/ftp.cpp, testsuite/http.cpp, testsuite/ping.cpp, and testsuite/server.cpp.

Member Enumeration Documentation

◆ AddressType

Type of address.

Enumerator
IPV4 

Address is an IPv4 address.

IPV6 

Address is an IPv6 address.

UNSPECIFIED 

Address is the unspecified addresss (i.e. ::0).

LOOPBACK 

Address is the loopback address.

MULTICAST 

Address is a multicast address.

LINK_LOCAL 

Address is a link local address.

SITE_LOCAL 

Address is a site local address.

IPV4_MAPPED 

Address is an IPv4 mapped address.

IPV4_COMPATIBLE 

Address is compatible with IPv4.

CLASS_A 

IPv4 class A.

CLASS_B 

IPv4 class B.

CLASS_C 

IPv4 class C.

CLASS_D 

IPv4 class D.

CLASS_E 

IPv4 class E (experimantal).

◆ Family

Family of internet addresses.

Enumerator
IP_VERSION_4 

Specifies Internet Protocol (IP) version 4.

IP_VERSION_6 

Specifies Internet Protocol (IP) version 6.

◆ MulticastFlags

Multicast flags.

Enumerator
MULTICAST_TRANSIENT 

The address is multicast transient.

◆ MulticastScope

Multicast scope.

Enumerator
NODE_LOCAL_SCOPE 

Node-local scope.

LINK_LOCAL_SCOPE 

Link-local scope.

SITE_LOCAL_SCOPE 

Site-local scope.

ORGANIZATION_LOCAL_SCOPE 

Organization-local scope.

GLOBAL_SCOPE 

Global scope.

Constructor & Destructor Documentation

◆ InetAddress() [1/5]

InetAddress::InetAddress ( )
noexcept

Initializes the address as unspecified IPv4 address (matches any address).

◆ InetAddress() [2/5]

InetAddress::InetAddress ( const uint8 *  address,
Family  family 
)
noexcept

Initializes the address as from the specified binary address.

Parameters
addressThe Internet address in network byte order.
familySpecifies the family of the binary address (IPv4 or IPv6).

◆ InetAddress() [3/5]

InetAddress::InetAddress ( const String address)

Initializes the address from the specified string. Implicit initialization is allowed.

Parameters
addressThe internet address (e.g. '172.30.33.14' or '::ffff:172.30.33.14').

◆ InetAddress() [4/5]

InetAddress::InetAddress ( const String address,
Family  family 
)

Initializes the address from the specified string.

Parameters
addressThe internet address (e.g. '172.30.33.14' or '::ffff:172.30.33.14').
familySpecifies the family to accept.

◆ InetAddress() [5/5]

InetAddress::InetAddress ( const InetAddress copy)
noexcept

Copy constructor.

Member Function Documentation

◆ convertToIPv4()

bool InetAddress::convertToIPv4 ( )
noexcept

Converts an IPv4-mapped IPv6 address to an IPv4 address (IPv4 addresses are not modified).

Returns
Returns true on success (fails if address isn't an IPv4-mapped IPv6 address or an IPv4 address).

◆ convertToIPv6()

bool InetAddress::convertToIPv6 ( )
noexcept

Converts an IPv4 address to an IPv4-mapped IPv6 address (IPv6 addresses are not modified).

Returns
Always returns true.

◆ getAddress()

const uint8* InetAddress::getAddress ( ) const
inlinenoexcept

Returns the IP address in binary format in network byte order.

Examples
testsuite/ftp.cpp.

◆ getAddressByName()

static InetAddress InetAddress::getAddressByName ( const String name)
static

Returns the first IP address found for the specified host name.

Parameters
nameThe name of the host (e.g. 'www.google.com').
Examples
testsuite/ping.cpp.

◆ getAddressesByName()

static List<InetAddress> InetAddress::getAddressesByName ( const String name)
static

Returns the IP addresses associated with the specified host name.

Parameters
nameThe name of the host (e.g. 'www.google.com').
Examples
testsuite/client.cpp, testsuite/ftp.cpp, testsuite/http.cpp, and testsuite/server.cpp.

◆ getFamily()

Family InetAddress::getFamily ( ) const
inlinenoexcept

Returns the family of the address.

◆ getHostName()

String InetAddress::getHostName ( bool  fullyQualified = false) const

Returns the domain/host name associated with this IP address. Raises HostNotFound if the host cannot be resolved.

Parameters
fullyQualifiedSpecifies that the fully-qualified domain name should be returned for local hosts. Default is false.
Examples
testsuite/ping.cpp.

◆ getIPv4Address()

const uint8* InetAddress::getIPv4Address ( ) const
inlinenoexcept

Returns the IP address in binary format in network byte order (this is only valid if either isIPv4Mapped() or isIPv4Compatible() returns true).

◆ getLocalHost()

static String InetAddress::getLocalHost ( )
static

Returns the name of the local host.

Examples
testsuite/client.cpp, and testsuite/server.cpp.

◆ getMulticastFlags()

unsigned int InetAddress::getMulticastFlags ( ) const
inlinenoexcept

Returns the flags for a multicast address.

◆ getMulticastScope()

unsigned int InetAddress::getMulticastScope ( ) const
inlinenoexcept

Returns the scope of a multicast address. This is only valid if the address is a multicast address.

◆ getType()

unsigned int InetAddress::getType ( ) const
noexcept

Returns the type of the address.

◆ getWords()

const uint32* InetAddress::getWords ( ) const
inlinenoexcept

Returns the IP address in binary format in network byte order.

◆ isIPv4() [1/2]

bool InetAddress::isIPv4 ( ) const
inlinenoexcept

Returns true if the address family is IPv4.

◆ isIPv4() [2/2]

static bool InetAddress::isIPv4 ( const String::ReadIterator  begin,
const String::ReadIterator  end 
)
staticnoexcept

Returns true if the specified string is an IPv4 address.

◆ isIPv4Compatible()

bool InetAddress::isIPv4Compatible ( ) const
noexcept

Returns true if this address is an IPv4 compatible IPv6 address (e.g. '::127.0.0.1') or if it is an IPv4 address. Returns false if the address is the unspecified address (i.e. ::).

◆ isIPv4Mapped()

bool InetAddress::isIPv4Mapped ( ) const
noexcept

Returns true if the address is an IPv4-mapped IPv6 address (e.g. '::ffff:127.0.0.1'). Returns false if address is an IPv4 addresses or the address is the unspecified address (i.e. ::).

◆ isIPv6() [1/2]

bool InetAddress::isIPv6 ( ) const
inlinenoexcept

Returns true if the address family is IPv6.

◆ isIPv6() [2/2]

static bool InetAddress::isIPv6 ( const String::ReadIterator  begin,
const String::ReadIterator  end 
)
staticnoexcept

Returns true if the specified string is an IPv6 address.

◆ isLinkLocal()

bool InetAddress::isLinkLocal ( ) const
noexcept

Returns true if this address is link local.

◆ isLoopback()

bool InetAddress::isLoopback ( ) const
noexcept

Returns true if this address is the loopback address (i.e. '::1' and '127.0.0.1' in the case of IPv6 and IPv4, respectively). The loopback address is a unicast address used to send packets to the local host.

◆ isMulticast()

bool InetAddress::isMulticast ( ) const
noexcept

Returns true if this address is a multicast address.

◆ isSiteLocal()

bool InetAddress::isSiteLocal ( ) const
noexcept

Returns true if this address is site local.

◆ isSynonymous()

bool InetAddress::isSynonymous ( const InetAddress eq) const
noexcept

Returns true if the addresses are synonymous.

◆ isUnspecified()

bool InetAddress::isUnspecified ( ) const
noexcept

Returns true if this address is the unspecified address (i.e. '::' and '0.0.0.0' in the case of IPv6 and IPv4, respectively).

◆ operator!=()

bool InetAddress::operator!= ( const InetAddress compare) const
inlinenoexcept

Returns true if the adresses are non-equal.

◆ operator=()

InetAddress& InetAddress::operator= ( const InetAddress assign)
noexcept

Assignment operator.

◆ operator==()

bool InetAddress::operator== ( const InetAddress compare) const
noexcept

Returns true if the adresses are exactly equal.

◆ setAddress()

void InetAddress::setAddress ( const uint8 *  address,
Family  family 
)
noexcept

Sets the address.

Parameters
addressThe Internet address in network byte order.
familySpecifies the family of the binary address (IPv4 or IPv6).