Internetprotocol-Address

  • Resolve DNS names into IP adresses
Summary
Internetprotocol-Address
CopyrightThis program is free software.
Files
C-kern/api/io/ip/ipaddr.hHeader file of Internetprotocol-Address.
C-kern/platform/Linux/io/ipaddr.cLinux specific implementation Internetprotocol-Address Linux.
Types
struct ipport_tExport ipport_t into global namespace.
struct ipaddr_tExport ipaddr_t.
struct ipaddr_list_tExport ipaddr_list_t.
struct ipaddr_storage_tExport ipaddr_storage_t to allow static allocation of an ipaddr_t.
Enumerations
ipversion_eSelects the version of the ip address.
ipprotocol_eCurrently supported internet protocols.
Functions
test
unittest_io_ipaddrUnittest for resolving dns names into ip addresses.
ipport_tThe UDP or TCP port number.
lifetime
ipport_ANYSystem chooses free port number for you.
initnamed_ipportReturns the tcp and udp port numbers of a named IP service.
ipaddr_tStores description of an internet protocol address.
protocolIndicates a specific internet protocol.
addrlenLength of internal address representation addr.
addrOpaque representation of internet address (IPv4 or IPv6).
lifetime
new_ipaddrCreate a new internet address and init it with the given values.
newdnsquery_ipaddrResolves a hostname into its first queried ip address.
newaddr_ipaddrCreate a new internet address and init it a system specific socket address value.
newany_ipaddrCreate a new internet address suitable to listen on any network interface.
newloopback_ipaddrReturns an ip address suitable for host only inter process communications.
newcopy_ipaddrCopy an internet address to store it for later usage.
delete_ipaddrDeletes a created or copied address.
query
compare_ipaddrReturns result of comparison (0 == equal).
isvalid_ipaddrChecks that internal fields are ok.
port_ipaddrReturns the port number of the address.
protocol_ipaddrReturns the specified protocol for this ip address.
version_ipaddrReturns the supported version of the address.
dnsname_ipaddrTries a reverse mapping from a binary ip address into its dns name representation.
dnsnameace_ipaddrTries a reverse mapping from a binary ip address into its dns name representation.
numericname_ipaddrReturns the numeric ascii representation of the ip address.
log
logurl_ipaddrWrites “varname: [tcp|udp]://[ip]:[port]” to logchannel.
change
copy_ipaddrCopies the ip number, protocol and port from source to dest.
setprotocol_ipaddrChanges ip address’ protocol value.
setport_ipaddrChanges ip address’ port value.
setaddr_ipaddrSets the ip number, protocol and port.
ipaddr_storage_tHolds enough memory for every possible version of ipaddr_t.
protocolSame as ipaddr_t.protocol.
addrlenSame as ipaddr_t.addrlen.
addrSame as ipaddr_t.addr.
storageAdditional space used by socket addr version with maximum size.
lifetime
initany_ipaddrstorageInits ipaddr_storage_t as an ip addr of type ipaddr_t with given ipversion_e.
ipaddr_list_tStores list of ipaddr_t.
lifetime
newdnsquery_ipaddrlistResolves a host name into a list of internet addresses.
delete_ipaddrlistFrees memory of internal list of ipaddr_list_t.
read
gofirst_ipaddrlistResets iterator to first ipaddr_t of internal list of addresses.
next_ipaddrlistReturns next ipaddr_t from of internal list.
inline implementation

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/io/ip/ipaddr.h

Header file of Internetprotocol-Address.

C-kern/platform/Linux/io/ipaddr.c

Linux specific implementation Internetprotocol-Address Linux.

Types

struct ipport_t

Export ipport_t into global namespace.

struct ipaddr_t

typedef struct ipaddr_t ipaddr_t

Export ipaddr_t.

struct ipaddr_list_t

typedef struct ipaddr_list_t ipaddr_list_t

Export ipaddr_list_t.

struct ipaddr_storage_t

typedef struct ipaddr_storage_t ipaddr_storage_t

Export ipaddr_storage_t to allow static allocation of an ipaddr_t.

Enumerations

ipversion_e

Selects the version of the ip address.

ipversion_ANYOnly useful as a filter argument to <newdnsquery_ipaddrlist>.  It means to return appropriate addresses for IPv4 and IPv6.
ipversion_4Selects IP version 4 address types of the form “A.B.C.D”, e.g.  “192.168.2.1”.  They are stored as a 32 bit value.  This value is assigned the system specific value AF_INET.
ipversion_6Selects IP version 6 address types of the form “AABB:CCDD::XXYY” . They are stored as a 128 bit value.  This value is assigned the system specific value AF_INET6.

ipprotocol_e

Currently supported internet protocols.

ipprotocol_ANYOnly useful as a filter argument to <newdnsquery_ipaddrlist>.  It means to return appropriate addresses for UDP and/or TCP protocols.  This value is assigned the system specific value IPPROTO_IP.
ipprotocol_TCPUse reliable transmission control protocol to send/receive messages.  This value is assigned the system specific value IPPROTO_TCP.
ipprotocol_UDPUse unreliable datagram protocol to send/receive messages.  This value is assigned the system specific value IPPROTO_UDP.

Functions

Summary
test
unittest_io_ipaddrUnittest for resolving dns names into ip addresses.

test

unittest_io_ipaddr

int unittest_io_ipaddr(void)

Unittest for resolving dns names into ip addresses.

ipport_t

The UDP or TCP port number.  Declares ipport_t as unsigned 16 bit.  The port is an id which identifies a running process on a host.  To communicate to a process on a remote system you need the IP address of that system, its port and the protocol (UDP or TCP).

Every application on a host must have a unique port number in the protocol domain to be unambiguously identifiable by the system network layer.  If you do not know the correct port number but the service name use initnamed_ipport first to determine it.

Summary
lifetime
ipport_ANYSystem chooses free port number for you.
initnamed_ipportReturns the tcp and udp port numbers of a named IP service.

lifetime

ipport_ANY

#define ipport_ANY 0

System chooses free port number for you.  If you assign ipport_ANY to your own port number the system chooses the next free port number > 0 for you if you create an ipsocket_t.

initnamed_ipport

int initnamed_ipport(/*out*/ipport_t *port,
const char *servicename,
ipprotocol_e protocol)

Returns the tcp and udp port numbers of a named IP service.  A returned value of 0 in tcp_port or udp_port means this kind of protocol is not supported by the service.  You can set tcp_port or udp_port to NULL if you want to query only for one protocol.

Returned error codes

ENOENTService name does not exist.
EPROTONOSUPPORTUDP or TCP protocol is not supported by service.

ipaddr_t

struct ipaddr_t

Stores description of an internet protocol address.  This object supports handling of IPv4 and IPv6 addresses.

The structure of an IP address

versionStores the protocol version; see ipversion_e.
protocolStores the protocol; see ipprotocol_e.
portStores the 16 bit port number.
internet addr.IPv4 (32 bit) or IPv6 (128 bit) number
Summary
protocolIndicates a specific internet protocol.
addrlenLength of internal address representation addr.
addrOpaque representation of internet address (IPv4 or IPv6).
lifetime
new_ipaddrCreate a new internet address and init it with the given values.
newdnsquery_ipaddrResolves a hostname into its first queried ip address.
newaddr_ipaddrCreate a new internet address and init it a system specific socket address value.
newany_ipaddrCreate a new internet address suitable to listen on any network interface.
newloopback_ipaddrReturns an ip address suitable for host only inter process communications.
newcopy_ipaddrCopy an internet address to store it for later usage.
delete_ipaddrDeletes a created or copied address.
query
compare_ipaddrReturns result of comparison (0 == equal).
isvalid_ipaddrChecks that internal fields are ok.
port_ipaddrReturns the port number of the address.
protocol_ipaddrReturns the specified protocol for this ip address.
version_ipaddrReturns the supported version of the address.
dnsname_ipaddrTries a reverse mapping from a binary ip address into its dns name representation.
dnsnameace_ipaddrTries a reverse mapping from a binary ip address into its dns name representation.
numericname_ipaddrReturns the numeric ascii representation of the ip address.
log
logurl_ipaddrWrites “varname: [tcp|udp]://[ip]:[port]” to logchannel.
change
copy_ipaddrCopies the ip number, protocol and port from source to dest.
setprotocol_ipaddrChanges ip address’ protocol value.
setport_ipaddrChanges ip address’ port value.
setaddr_ipaddrSets the ip number, protocol and port.

protocol

uint16_t protocol

Indicates a specific internet protocol.  See ipprotocol_e for a list of possible values.

addrlen

uint16_t addrlen

Length of internal address representation addr.

addr

sys_socketaddr_t addr[]

Opaque representation of internet address (IPv4 or IPv6).  The size in bytes is stored in addrlen.

lifetime

new_ipaddr

int new_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
const char *numeric_addr,
ipport_t port,
ipversion_e version)

Create a new internet address and init it with the given values.  If you do not longer need it do not forget to free it with delete_ipaddr.

Parameter

protocolEither ipprotocol_UDP or ipprotocol_TCP.
numeric_addrThe numeric representation of the internet address ( “200.123.56.78” or “::1” ...)
portA port which identifies this process between all processes on this network node.  If set to <ipport_ANY> (== 0) a free port is randomly chosen by the system during socket creation.
versionIP version: Either ipversion_4 or ipversion_6.

newdnsquery_ipaddr

int newdnsquery_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
const char *hostname,
ipport_t port,
ipversion_e version)

Resolves a hostname into its first queried ip address.

newaddr_ipaddr

int newaddr_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
uint16_t sock_addr_len,
const sys_socketaddr_t *sock_addr)

Create a new internet address and init it a system specific socket address value.  Same as new_ipaddr except that port, numeric_address and version are represented by system specific type sys_socketaddr_t.  If you do not longer need it do not forget to free it with delete_ipaddr.

newany_ipaddr

int newany_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)

Create a new internet address suitable to listen on any network interface.  Same as new_ipaddr except that numeric_addr is set to “0.0.0.0” or “::” depending on the protocol version.  If you do not longer need it do not forget to free it with delete_ipaddr.

newloopback_ipaddr

int newloopback_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)

Returns an ip address suitable for host only inter process communications.  The loopback address allows to send messages between processes on the same local system (host).  Connection to remote network nodes are not supported.  Same as new_ipaddr except that numeric_addr is set to the loopback address “127.0.0.1” for IPv4 or “::1” for IPv6.

newcopy_ipaddr

int newcopy_ipaddr(/*out*/ipaddr_t **dest,
const ipaddr_t *source)

Copy an internet address to store it for later usage.  If you do no longer need it do not forget to free it with delete_ipaddr.

delete_ipaddr

int delete_ipaddr(ipaddr_t **addr)

Deletes a created or copied address.  Never free an address returned by ipaddr_list_t.

query

compare_ipaddr

int compare_ipaddr(const ipaddr_t *left,
const ipaddr_t *right)

Returns result of comparison (0 == equal).

Returns

< 0left < right
== 0left == right
0  - left  > right

isvalid_ipaddr

bool isvalid_ipaddr(const ipaddr_t *addr)

Checks that internal fields are ok.

port_ipaddr

ipport_t port_ipaddr(const ipaddr_t *addr)

Returns the port number of the address.

protocol_ipaddr

ipprotocol_e protocol_ipaddr(const ipaddr_t *addr)

Returns the specified protocol for this ip address.  See also ipprotocol_e.

Return Values

ipprotocol_TCPConnection oriented protocol
ipprotocol_UDPConnectionless protocol

version_ipaddr

ipversion_e version_ipaddr(const ipaddr_t *addr)

Returns the supported version of the address.  This value can not be changed after creation.

Returned Values

ipversion_4Internet Protocol version 4.
ipversion_6Internet Protocol version 6.

dnsname_ipaddr

int dnsname_ipaddr(const ipaddr_t *addr,
cstring_t *dns_name)

Tries a reverse mapping from a binary ip address into its dns name representation.  The returned name is converted into the current character encoding in case the returned name is an ACE encoded IDN (internat. domain name).  ACE stands for ASCII Compatible Encoding and is used to represent an IDN which contains unicode characters.

The name is returned in dns_name.  This string must in a initialized state.  Previous content is overwritten.

See

RFC: Internationalizing Domain Names in Applications

dnsnameace_ipaddr

int dnsnameace_ipaddr(const ipaddr_t *addr,
cstring_t *dns_name)

Tries a reverse mapping from a binary ip address into its dns name representation.  Same as dnsname_ipaddr except that IDN represented in ACE are left untouched (not converted into unicode).  The name is returned in dns_name.  This string must in a initialized state.  Previous content is overwritten.

numericname_ipaddr

int numericname_ipaddr(const ipaddr_t *addr,
cstring_t *numeric_name)

Returns the numeric ascii representation of the ip address.  The returned names are composed of four decimal numbers for IPv4, i.e.  ‘192.168.20.10’, or 8 16bit hexadecimal numbers separated by ‘:’ for IPv6, i.e.  ‘2010:0dcc:3543:0000:0000:4e9f:0370:2668’.  The name is returned in numeric_name.  This string must in a initialized state.  Previous content is overwritten.

log

logurl_ipaddr

void logurl_ipaddr(const ipaddr_t *addr,
const char *varname,
uint8_t logchannel)

Writes “varname: [tcp|udp]://[ip]:[port]” to logchannel.  Parameter logchannel is of type log_channel_e.

change

copy_ipaddr

int copy_ipaddr(ipaddr_t *dest,
const ipaddr_t *source)

Copies the ip number, protocol and port from source to dest.  If version of source is not the same as of dest EAFNOSUPPORT is returned.

setprotocol_ipaddr

int setprotocol_ipaddr(ipaddr_t *addr,
ipprotocol_e protocol)

Changes ip address’ protocol value.  Returns 0 in case of success else EINVAL if protocol is not a value from ipprotocol_e.

setport_ipaddr

int setport_ipaddr(ipaddr_t *addr,
ipport_t port)

Changes ip address’ port value.

setaddr_ipaddr

int setaddr_ipaddr(ipaddr_t *addr,
ipprotocol_e protocol,
uint16_t sock_addr_len,
const sys_socketaddr_t *sock_addr)

Sets the ip number, protocol and port.  If version of sock_addr is not the same as of addr EAFNOSUPPORT is returned.

ipaddr_storage_t

struct ipaddr_storage_t

Holds enough memory for every possible version of ipaddr_t.  Use function initany_ipaddrstorage to cast this object into an ipaddr_t with the correct ipversion_e.

Summary
protocolSame as ipaddr_t.protocol.
addrlenSame as ipaddr_t.addrlen.
addrSame as ipaddr_t.addr.
storageAdditional space used by socket addr version with maximum size.
lifetime
initany_ipaddrstorageInits ipaddr_storage_t as an ip addr of type ipaddr_t with given ipversion_e.

protocol

uint16_t protocol

Same as ipaddr_t.protocol.

addrlen

uint16_t addrlen

Same as ipaddr_t.addrlen.

addr

sys_socketaddr_t addr[1]

Same as ipaddr_t.addr.

storage

uint8_t storage[sys_socketaddr_MAXSIZE - sizeof(sys_socketaddr_t)]

Additional space used by socket addr version with maximum size.

lifetime

initany_ipaddrstorage

ipaddr_t * initany_ipaddrstorage(ipaddr_storage_t *addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)

Inits ipaddr_storage_t as an ip addr of type ipaddr_t with given ipversion_e.  In case any parameter is invalid the value NULL is returned.  The returned address is the same as a call to newany_ipaddr.

Never delete the returned ipaddr_t pointer.  It points to the given addr (ipaddr_storage_t) and is only valid as long as addr is not changed or deleted.

ipaddr_list_t

struct ipaddr_list_t

Stores list of ipaddr_t.  Allows to iterate over every entry.

Summary
lifetime
newdnsquery_ipaddrlistResolves a host name into a list of internet addresses.
delete_ipaddrlistFrees memory of internal list of ipaddr_list_t.
read
gofirst_ipaddrlistResets iterator to first ipaddr_t of internal list of addresses.
next_ipaddrlistReturns next ipaddr_t from of internal list.

lifetime

newdnsquery_ipaddrlist

int newdnsquery_ipaddrlist(/*out*/ipaddr_list_t **addrlist,
const char *hostname_or_numeric,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)

Resolves a host name into a list of internet addresses.

Returned error codes

EADDRNOTAVAILIP version for numeric name not supported (name == “127.0.0.1” && version == ipversion_6)
EAFNOSUPPORTValue in version is not supported.
EPROTONOSUPPORTValue in protocol is not supported.
ENOENTThe dns name is not known.
ENODATANo data received (timeout) or the specified network host exists, but does not have any network addresses defined.

delete_ipaddrlist

int delete_ipaddrlist(ipaddr_list_t **addrlist)

Frees memory of internal list of ipaddr_list_t.  Calling it more than once is safe.

read

gofirst_ipaddrlist

void gofirst_ipaddrlist(ipaddr_list_t *addrlist)

Resets iterator to first ipaddr_t of internal list of addresses.  After calling this function the next call to next_ipaddrlist returns the first address in the buffer.

next_ipaddrlist

const ipaddr_t * next_ipaddrlist(ipaddr_list_t *addrlist)

Returns next ipaddr_t from of internal list.  Only valid until next call to next_ipaddrlist or delete_ipaddrlist.

inline implementation

Implements Internetprotocol-Address on Linux.
The UDP or TCP port number.
typedef struct ipaddr_t ipaddr_t
Export ipaddr_t.
struct ipaddr_t
Stores description of an internet protocol address.
typedef struct ipaddr_list_t ipaddr_list_t
Export ipaddr_list_t.
struct ipaddr_list_t
Stores list of ipaddr_t.
typedef struct ipaddr_storage_t ipaddr_storage_t
Export ipaddr_storage_t to allow static allocation of an ipaddr_t.
struct ipaddr_storage_t
Holds enough memory for every possible version of ipaddr_t.
int unittest_io_ipaddr(void)
Unittest for resolving dns names into ip addresses.
#define ipport_ANY 0
System chooses free port number for you.
int initnamed_ipport(/*out*/ipport_t *port,
const char *servicename,
ipprotocol_e protocol)
Returns the tcp and udp port numbers of a named IP service.
uint16_t protocol
Indicates a specific internet protocol.
uint16_t addrlen
Length of internal address representation addr.
sys_socketaddr_t addr[]
Opaque representation of internet address (IPv4 or IPv6).
int new_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
const char *numeric_addr,
ipport_t port,
ipversion_e version)
Create a new internet address and init it with the given values.
int newdnsquery_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
const char *hostname,
ipport_t port,
ipversion_e version)
Resolves a hostname into its first queried ip address.
int newaddr_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
uint16_t sock_addr_len,
const sys_socketaddr_t *sock_addr)
Create a new internet address and init it a system specific socket address value.
int newany_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)
Create a new internet address suitable to listen on any network interface.
int newloopback_ipaddr(/*out*/ipaddr_t **addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)
Returns an ip address suitable for host only inter process communications.
int newcopy_ipaddr(/*out*/ipaddr_t **dest,
const ipaddr_t *source)
Copy an internet address to store it for later usage.
int delete_ipaddr(ipaddr_t **addr)
Deletes a created or copied address.
int compare_ipaddr(const ipaddr_t *left,
const ipaddr_t *right)
Returns result of comparison (0 == equal).
bool isvalid_ipaddr(const ipaddr_t *addr)
Checks that internal fields are ok.
ipport_t port_ipaddr(const ipaddr_t *addr)
Returns the port number of the address.
ipprotocol_e protocol_ipaddr(const ipaddr_t *addr)
Returns the specified protocol for this ip address.
ipversion_e version_ipaddr(const ipaddr_t *addr)
Returns the supported version of the address.
int dnsname_ipaddr(const ipaddr_t *addr,
cstring_t *dns_name)
Tries a reverse mapping from a binary ip address into its dns name representation.
int dnsnameace_ipaddr(const ipaddr_t *addr,
cstring_t *dns_name)
Tries a reverse mapping from a binary ip address into its dns name representation.
int numericname_ipaddr(const ipaddr_t *addr,
cstring_t *numeric_name)
Returns the numeric ascii representation of the ip address.
void logurl_ipaddr(const ipaddr_t *addr,
const char *varname,
uint8_t logchannel)
Writes “varname: [tcp|udp]://[ip]:[port]” to logchannel.
int copy_ipaddr(ipaddr_t *dest,
const ipaddr_t *source)
Copies the ip number, protocol and port from source to dest.
int setprotocol_ipaddr(ipaddr_t *addr,
ipprotocol_e protocol)
Changes ip address’ protocol value.
int setport_ipaddr(ipaddr_t *addr,
ipport_t port)
Changes ip address’ port value.
int setaddr_ipaddr(ipaddr_t *addr,
ipprotocol_e protocol,
uint16_t sock_addr_len,
const sys_socketaddr_t *sock_addr)
Sets the ip number, protocol and port.
uint16_t protocol
Same as ipaddr_t.protocol.
uint16_t addrlen
Same as ipaddr_t.addrlen.
sys_socketaddr_t addr[1]
Same as ipaddr_t.addr.
uint8_t storage[sys_socketaddr_MAXSIZE - sizeof(sys_socketaddr_t)]
Additional space used by socket addr version with maximum size.
ipaddr_t * initany_ipaddrstorage(ipaddr_storage_t *addr,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)
Inits ipaddr_storage_t as an ip addr of type ipaddr_t with given ipversion_e.
Selects the version of the ip address.
int newdnsquery_ipaddrlist(/*out*/ipaddr_list_t **addrlist,
const char *hostname_or_numeric,
ipprotocol_e protocol,
ipport_t port,
ipversion_e version)
Resolves a host name into a list of internet addresses.
int delete_ipaddrlist(ipaddr_list_t **addrlist)
Frees memory of internal list of ipaddr_list_t.
void gofirst_ipaddrlist(ipaddr_list_t *addrlist)
Resets iterator to first ipaddr_t of internal list of addresses.
const ipaddr_t * next_ipaddrlist(ipaddr_list_t *addrlist)
Returns next ipaddr_t from of internal list.
typedef sys_iochannel_t ipsocket_t
Defines ipsocket_t as alias for sys_iochannel_t.
Currently supported internet protocols.
Use unreliable datagram protocol to send/receive messages.
Use reliable transmission control protocol to send/receive messages.
Selects IP version 4 address types of the form “A.B.C.D”, e.g.
Selects IP version 6 address types of the form “AABB:CCDD::XXYY” .
Used to switch between log channels.
Close