Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

host_addr.h File Reference


Detailed Description

Host address functions.

Author:
Christian Biere
Date:
2005

#include "common.h"
#include "lib/endian.h"
#include "lib/misc.h"

Go to the source code of this file.

Data Structures

struct  host_addr
struct  packed_host_addr
struct  packed_host
union  socket_addr

Typedefs

typedef host_addr host_addr_t
typedef socket_addr socket_addr_t

Enumerations

enum  net_type { NET_TYPE_NONE = 0, NET_TYPE_LOCAL = 1, NET_TYPE_IPV4 = 4, NET_TYPE_IPV6 = 6 }

Functions

gint net_type_to_pf (enum net_type net)
gint net_type_to_af (enum net_type net)
gboolean host_addr_convert (const host_addr_t from, host_addr_t *to, enum net_type to_net)
 Tries to convert the host address "from" to the network type "to_net" and stores the converted address in "*to".
gboolean host_addr_can_convert (const host_addr_t from, enum net_type to_net)
gboolean host_addr_6to4_to_ipv4 (const host_addr_t from, host_addr_t *to)
gboolean host_addr_initialized (const host_addr_t ha)
const gchar * net_type_to_string (enum net_type net)
enum net_type host_addr_net (const host_addr_t ha)
guint32 host_addr_ipv4 (const host_addr_t ha)
const guint8 * host_addr_ipv6 (const host_addr_t *ha)
host_addr_t host_addr_get_ipv4 (guint32 ip)
host_addr_t host_addr_peek_ipv4 (const void *ipv4)
host_addr_t host_addr_peek_ipv6 (const guint8 *ipv6)
gboolean host_addr_equal (const host_addr_t a, const host_addr_t b)
gint host_addr_cmp (host_addr_t a, host_addr_t b)
gboolean host_addr_matches (const host_addr_t a, const host_addr_t b, guint8 bits)
gboolean is_host_addr (const host_addr_t ha)
guint32 host_addr_hash (host_addr_t ha)
host_addr_t socket_addr_get_addr (const socket_addr_t *addr)
 Retrieves the address from a socket_addr_t.
guint16 socket_addr_get_port (const socket_addr_t *addr)
 Retrieves the port number from a socket_addr_t.
socklen_t socket_addr_get_len (const socket_addr_t *addr)
const struct sockaddr * socket_addr_get_const_sockaddr (const socket_addr_t *addr)
sockaddr * socket_addr_get_sockaddr (socket_addr_t *addr)
gint socket_addr_get_family (const socket_addr_t *addr)
socklen_t socket_addr_set (socket_addr_t *sa_ptr, const host_addr_t addr, guint16 port)
 Initializes sa_ptr from a host address and a port number.
socklen_t socket_addr_init (socket_addr_t *sa_ptr, enum net_type net)
guint host_addr_hash_func (gconstpointer key)
gboolean host_addr_eq_func (gconstpointer p, gconstpointer q)
void wfree_host_addr (gpointer key, gpointer unused_data)
int host_addr_family (const host_addr_t ha)
gboolean is_private_addr (const host_addr_t addr)
 Checks for RFC1918 private addresses but also IPv6 link-local and site-local addresses.
gboolean host_addr_is_routable (const host_addr_t addr)
 Check whether host can be reached from the Internet.
gboolean host_addr_is_loopback (const host_addr_t addr)
 Checks whether the given address is 127.0.0.1 or ::1.
gboolean host_addr_is_ipv4_mapped (const host_addr_t addr)
const gchar * host_addr_to_string (const host_addr_t addr)
 Prints the host address ``ha'' to a static buffer.
size_t host_addr_to_string_buf (const host_addr_t addr, gchar *, size_t)
 Prints the host address ``ha'' to ``dst''.
gboolean string_to_host_addr (const gchar *s, const gchar **endptr, host_addr_t *addr_ptr)
const gchar * host_addr_port_to_string (const host_addr_t addr, guint16 port)
 Prints the host address ``ha'' followed by ``port'' to a static buffer.
const gchar * host_addr_port_to_string2 (const host_addr_t addr, guint16 port)
size_t host_addr_port_to_string_buf (const host_addr_t addr, guint16 port, gchar *, size_t)
 Prints the host address ``ha'' followed by ``port'' to ``dst''.
gboolean string_to_host_addr_port (const gchar *str, const gchar **endptr, host_addr_t *addr_ptr, guint16 *port_ptr)
const gchar * host_port_to_string (const gchar *hostname, host_addr_t addr, guint16 port)
GSList * name_to_host_addr (const gchar *host, enum net_type net)
 Resolves a hostname to IP addresses per DNS.
void host_addr_free_list (GSList **sl_ptr)
 Frees a singly-linked list of host_addr_t elements.
host_addr_t name_to_single_host_addr (const gchar *host, enum net_type net)
 Resolves a hostname to an IP address per DNS.
const gchar * host_addr_to_name (const host_addr_t addr)
 Resolves an IP address to a hostname per DNS.
gboolean string_to_host_or_addr (const char *s, const gchar **endptr, host_addr_t *ha)
 Parses the NUL-terminated string ``s'' for a host address or a hostname.
GSList * host_addr_get_interface_addrs (enum net_type net)
void host_addr_free_interface_addrs (GSList **sl_ptr)
 Frees a list along with its item returned by host_addr_get_interface_addrs() and nullifies the given pointer.
guint packed_host_addr_size (const struct packed_host_addr paddr)
packed_host_addr host_addr_pack (const host_addr_t addr)
host_addr_t packed_host_addr_unpack (const struct packed_host_addr paddr)
guint packed_host_size (const struct packed_host paddr)
 Significant size of a packed host (serialization size).
packed_host host_pack (const host_addr_t addr, guint16 port)
gboolean packed_host_unpack (const struct packed_host phost, host_addr_t *addr_ptr, guint16 *port_ptr)
guint packed_host_hash_func (gconstpointer key)
 Hash a packed host buffer (variable-sized).
gboolean packed_host_eq_func (gconstpointer p, gconstpointer q)
 Compare two packed host buffers (variable-sized).
gpointer walloc_packed_host (const host_addr_t addr, guint16 port)
 Allocate a packed_host key.
void wfree_packed_host (gpointer key, gpointer unused_data)
 Release packed_host key.

Variables

const host_addr_t ipv4_unspecified
const host_addr_t ipv4_loopback
const host_addr_t ipv6_unspecified
const host_addr_t ipv6_loopback
const host_addr_t ipv6_ipv4_mapped
const host_addr_t ipv6_multicast
const host_addr_t ipv6_link_local
const host_addr_t ipv6_site_local
const host_addr_t ipv6_6to4
const host_addr_t local_host_addr
const host_addr_t zero_host_addr


Typedef Documentation

typedef struct host_addr host_addr_t
 

typedef union socket_addr socket_addr_t
 


Enumeration Type Documentation

enum net_type
 

Note:
AF_UNIX/AF_LOCAL (unix domain) sockets are not fully supported. These are only used for "anonymous" incoming connections, not for outgoing connections and a potential pathname is ignored.
Enumeration values:
NET_TYPE_NONE 
NET_TYPE_LOCAL 
NET_TYPE_IPV4 
NET_TYPE_IPV6 


Function Documentation

gboolean host_addr_6to4_to_ipv4 const host_addr_t  from,
host_addr_t to
 

gboolean host_addr_can_convert const host_addr_t  from,
enum net_type  to_net
 

gint host_addr_cmp host_addr_t  a,
host_addr_t  b
[inline, static]
 

gboolean host_addr_convert const host_addr_t  from,
host_addr_t to,
enum net_type  to_net
 

Tries to convert the host address "from" to the network type "to_net" and stores the converted address in "*to".

If conversion is not possible, FALSE is returned and "*to" is set to zero_host_addr.

Parameters:
from The address to convert.
to Will hold the converted address.
to_net The network type to convert the address to.
Returns:
TRUE if the address could be converted, FALSE otherwise.

gboolean host_addr_eq_func gconstpointer  p,
gconstpointer  q
 

gboolean host_addr_equal const host_addr_t  a,
const host_addr_t  b
[inline, static]
 

int host_addr_family const host_addr_t  ha  ) 
 

Parameters:
ha An initialized host address.
Returns:
The proper AF_* value or -1 if not available.

void host_addr_free_interface_addrs GSList **  sl_ptr  ) 
 

Frees a list along with its item returned by host_addr_get_interface_addrs() and nullifies the given pointer.

void host_addr_free_list GSList **  sl_ptr  ) 
 

Frees a singly-linked list of host_addr_t elements.

GSList* host_addr_get_interface_addrs const enum net_type  net  ) 
 

Returns:
A list of all IPv4 and IPv6 addresses assigned to interfaces of the machine.

host_addr_t host_addr_get_ipv4 guint32  ip  )  [inline, static]
 

guint32 host_addr_hash host_addr_t  ha  )  [inline, static]
 

guint host_addr_hash_func gconstpointer  key  ) 
 

gboolean host_addr_initialized const host_addr_t  ha  )  [inline, static]
 

guint32 host_addr_ipv4 const host_addr_t  ha  )  [inline, static]
 

const guint8* host_addr_ipv6 const host_addr_t ha  )  [inline, static]
 

gboolean host_addr_is_ipv4_mapped const host_addr_t  addr  )  [inline, static]
 

gboolean host_addr_is_loopback const host_addr_t  addr  ) 
 

Checks whether the given address is 127.0.0.1 or ::1.

gboolean host_addr_is_routable const host_addr_t  addr  ) 
 

Check whether host can be reached from the Internet.

We rule out IPs of private networks, plus some other invalid combinations.

gboolean host_addr_matches const host_addr_t  a,
const host_addr_t  b,
guint8  bits
[inline, static]
 

enum net_type host_addr_net const host_addr_t  ha  )  [inline, static]
 

struct packed_host_addr host_addr_pack const host_addr_t  addr  ) 
 

host_addr_t host_addr_peek_ipv4 const void *  ipv4  )  [inline, static]
 

host_addr_t host_addr_peek_ipv6 const guint8 *  ipv6  )  [inline, static]
 

const gchar* host_addr_port_to_string const host_addr_t  ha,
guint16  port
 

Prints the host address ``ha'' followed by ``port'' to a static buffer.

Parameters:
ha the host address.
port the port number.
Returns:
a pointer to a static buffer holding a NUL-terminated string representing the given host address and port.

const gchar* host_addr_port_to_string2 const host_addr_t  addr,
guint16  port
 

size_t host_addr_port_to_string_buf const host_addr_t  ha,
guint16  port,
gchar *  dst,
size_t  size
 

Prints the host address ``ha'' followed by ``port'' to ``dst''.

The string written to ``dst'' is always NUL-terminated unless ``size'' is zero. If ``size'' is too small, the string will be truncated.

Parameters:
dst the destination buffer; may be NULL iff ``size'' is zero.
ha the host address.
port the port number.
size the size of ``dst'' in bytes.
Returns:
The length of the resulting string assuming ``size'' is sufficient.

const gchar* host_addr_to_name host_addr_t  addr  ) 
 

Resolves an IP address to a hostname per DNS.

Parameters:
ha The host address to resolve.
Returns:
On success, the hostname is returned. Otherwise, NULL is returned. The resulting string points to a static buffer.

const gchar* host_addr_to_string const host_addr_t  ha  ) 
 

Prints the host address ``ha'' to a static buffer.

Parameters:
ha the host address.
Returns:
a pointer to a static buffer holding a NUL-terminated string representing the given host address.

size_t host_addr_to_string_buf const host_addr_t  ha,
gchar *  dst,
size_t  size
 

Prints the host address ``ha'' to ``dst''.

The string written to ``dst'' is always NUL-terminated unless ``size'' is zero. If ``size'' is too small, the string will be truncated.

Parameters:
dst the destination buffer; may be NULL iff ``size'' is zero.
ha the host address.
size the size of ``dst'' in bytes.
Returns:
The length of the resulting string assuming ``size'' is sufficient.

struct packed_host host_pack const host_addr_t  addr,
guint16  port
 

const gchar* host_port_to_string const gchar *  hostname,
host_addr_t  addr,
guint16  port
 

gboolean is_host_addr const host_addr_t  ha  )  [inline, static]
 

gboolean is_private_addr const host_addr_t  addr  ) 
 

Checks for RFC1918 private addresses but also IPv6 link-local and site-local addresses.

Returns:
TRUE if is a private address.

GSList* name_to_host_addr const gchar *  host,
enum net_type  net
 

Resolves a hostname to IP addresses per DNS.

Todo:
TODO: This should return all resolved address not just the first and it should be possible to request only IPv4 or IPv6 addresses.
Parameters:
host A NUL-terminated string holding the hostname to resolve.
net Use NET_TYPE_IPV4 if you want only IPv4 addresses or like-wise NET_TYPE_IPV6. If you don't care, use NET_TYPE_NONE.
Returns:
On success, a single-linked list of walloc()ated host_addr_t items is returned. Use host_addr_free_list() for convience. On failure, NULL is returned.

host_addr_t name_to_single_host_addr const gchar *  host,
enum net_type  net
 

Resolves a hostname to an IP address per DNS.

This is the same as name_to_host_addr() but we pick a random item from the result list and return it.

gint net_type_to_af enum net_type  net  )  [inline, static]
 

gint net_type_to_pf enum net_type  net  )  [inline, static]
 

const gchar* net_type_to_string enum net_type  net  )  [inline, static]
 

guint packed_host_addr_size const struct packed_host_addr  paddr  ) 
 

host_addr_t packed_host_addr_unpack const struct packed_host_addr  paddr  ) 
 

gboolean packed_host_eq_func gconstpointer  p,
gconstpointer  q
 

Compare two packed host buffers (variable-sized).

guint packed_host_hash_func gconstpointer  key  ) 
 

Hash a packed host buffer (variable-sized).

guint packed_host_size const struct packed_host  paddr  ) 
 

Significant size of a packed host (serialization size).

gboolean packed_host_unpack const struct packed_host  phost,
host_addr_t addr_ptr,
guint16 *  port_ptr
 

host_addr_t socket_addr_get_addr const socket_addr_t addr  )  [inline, static]
 

Retrieves the address from a socket_addr_t.

Parameters:
addr a pointer to an initialized socket_addr_t
Returns:
the address.

const struct sockaddr* socket_addr_get_const_sockaddr const socket_addr_t addr  )  [inline, static]
 

gint socket_addr_get_family const socket_addr_t addr  )  [inline, static]
 

socklen_t socket_addr_get_len const socket_addr_t addr  )  [inline, static]
 

guint16 socket_addr_get_port const socket_addr_t addr  )  [inline, static]
 

Retrieves the port number from a socket_addr_t.

Parameters:
addr a pointer to an initialized socket_addr_t
Returns:
the port number in host byte order

struct sockaddr* socket_addr_get_sockaddr socket_addr_t addr  )  [inline, static]
 

socklen_t socket_addr_init socket_addr_t sa_ptr,
enum net_type  net
 

socklen_t socket_addr_set socket_addr_t sa_ptr,
const host_addr_t  addr,
guint16  port
 

Initializes sa_ptr from a host address and a port number.

Parameters:
addr The host address.
port The port number.
sa_ptr a pointer to a socket_addr_t
Returns:
The length of the initialized structure.

gboolean string_to_host_addr const gchar *  s,
const gchar **  endptr,
host_addr_t addr_ptr
 

gboolean string_to_host_addr_port const gchar *  str,
const gchar **  endptr,
host_addr_t addr_ptr,
guint16 *  port_ptr
 

gboolean string_to_host_or_addr const char *  s,
const gchar **  endptr,
host_addr_t ha
 

Parses the NUL-terminated string ``s'' for a host address or a hostname.

If ``s'' points to a parsable address, ``*ha'' will be set to it. Otherwise, ``*ha'' is set to ``zero_host_addr''. If the string is a possible hostname the function returns TRUE nonetheless and ``*endptr'' will point to the first character after hostname. If IPv6 support is disabled, "[::]" will be considered a hostname rather than a host address.

Parameters:
s the string to parse.
endptr if not NULL, it will point the first character after the parsed host address or hostname.
ha if not NULL, it is set to the parsed host address or ``zero_host_addr'' on failure.
Returns:
TRUE if the string points to host address or is a possible hostname.

gpointer walloc_packed_host const host_addr_t  addr,
guint16  port
 

Allocate a packed_host key.

void wfree_host_addr gpointer  key,
gpointer  unused_data
 

void wfree_packed_host gpointer  key,
gpointer  unused_data
 

Release packed_host key.


Variable Documentation

const host_addr_t ipv4_loopback [static]
 

Initial value:

 {  
    NET_TYPE_IPV4,
    { { 0x7f, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv4_unspecified [static]
 

Initial value:

 {  
    NET_TYPE_IPV4,
    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_6to4 [static]
 

Initial value:

 {          
    NET_TYPE_IPV6,
    { { 0x20, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_ipv4_mapped [static]
 

Initial value:

 {  
    NET_TYPE_IPV6,
    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_link_local [static]
 

Initial value:

 {  
    NET_TYPE_IPV6,
    { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_loopback [static]
 

Initial value:

 {  
    NET_TYPE_IPV6,
    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } },
}

const host_addr_t ipv6_multicast [static]
 

Initial value:

 {      
    NET_TYPE_IPV6,
    { { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_site_local [static]
 

Initial value:

 {  
    NET_TYPE_IPV6,
    { { 0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t ipv6_unspecified [static]
 

Initial value:

 {  
    NET_TYPE_IPV6,
    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t local_host_addr [static]
 

Initial value:

 {
    NET_TYPE_LOCAL,
    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
}

const host_addr_t zero_host_addr [static]
 


Generated on Sat Jun 30 17:53:27 2007 for gtk-gnutella by  doxygen 1.3.9.1