Static Hostname definitions

This enables functionality like /etc/hosts for dns_query() More...

Detailed Description

This enables functionality like /etc/hosts for dns_query()

This module allows to make static hostname - IP address mappings that will be taken into consideration when using dns_query() or a higher level API that makes use of dns_query().

To enable this, add the hosts module to your Makefile:

USEMODULE += hosts

Usage

To add a static hostname mapping to your code, just declare it with HOST_IPV4() or HOST_IPV6() anywhere in a .c file

#define IPV4_ADDR_EXAMPLE_COM IPV4_ADDR_INIT(192,168,42,23)
#define IPV6_ADDR_EXAMPLE_COM {{ 0xfd, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x42, 0x23 }}
HOST_IPV4(example.com, IPV4_ADDR_EXAMPLE_COM);
HOST_IPV6(example.com, IPV6_ADDR_EXAMPLE_COM);
#define HOST_IPV4(host, ip)
Define IPv4 host.
Definition: hosts.h:105
#define HOST_IPV6(host, ip)
Define IPv6 host.
Definition: hosts.h:129

The hostname should then be resolved by any tool or function that makes use of dns_query():

> ping example.com
12 bytes from fd::42:23: icmp_seq=0 ttl=64 time=1.202 ms
12 bytes from fd::42:23: icmp_seq=1 ttl=64 time=1.202 ms
12 bytes from fd::42:23: icmp_seq=2 ttl=64 time=1.202 ms
--- example.com PING statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 1.202/1.202/1.202 ms

Files

file  hosts.h
 Static mappings from hostnames to IP addresses.
 

Data Structures

struct  hostnames_v4_xfa_t
 A single IPv4 host in the list of hostnames. More...
 
struct  hostnames_v6_xfa_t
 A single IPv6 host in the list of hostnames. More...
 

Macros

#define HOST_IPV4(host, ip)
 Define IPv4 host. More...
 
#define HOST_IPV6(host, ip)
 Define IPv6 host. More...
 

Functions

int hosts_query (const char *host_name, void *addr_out, int family)
 Get IP address for a host name. More...
 

Macro Definition Documentation

◆ HOST_IPV4

#define HOST_IPV4 (   host,
  ip 
)
Value:
XFA_USE_CONST(hostnames_v4_xfa_t, hostnames_v4_xfa); \
static FLASH_ATTR const char _xfa_ ## host ## _hostname[] = #host; \
XFA_CONST(hostnames_v4_xfa_t, hostnames_v4_xfa, 0) _xfa_ ## host ## _host = { \
.name = _xfa_ ## host ## _hostname, \
.addr = ip, \
};
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:67
A single IPv4 host in the list of hostnames.
Definition: hosts.h:74
#define XFA_USE_CONST(type, name)
Declare an external read-only cross-file array.
Definition: xfa.h:113

Define IPv4 host.

Note
This is not available from C++, but a trivial C++ file can easily hook up a extern "C" function implemented in C.

This macro is a helper for defining a hostname -> address mapping and adding it to the host names XFA (cross file array).

Example:

#include "hosts.h"
#define IPV4_ADDR_LOOPBACK
Static initializer for the loopback IPv4 address (127.0.0.1)
Definition: addr.h:53
Static mappings from hostnames to IP addresses.

Definition at line 105 of file hosts.h.

◆ HOST_IPV6

#define HOST_IPV6 (   host,
  ip 
)
Value:
XFA_USE_CONST(hostnames_v6_xfa_t, hostnames_v6_xfa); \
static FLASH_ATTR const char _xfa_ ## host ## _hostname[] = #host; \
XFA_CONST(hostnames_v6_xfa_t, hostnames_v6_xfa, 0) _xfa_ ## host ## _host = { \
.name = _xfa_ ## host ## _hostname, \
.addr = ip, \
};
A single IPv6 host in the list of hostnames.
Definition: hosts.h:84

Define IPv6 host.

Note
This is not available from C++, but a trivial C++ file can easily hook up a extern "C" function implemented in C.

This macro is a helper for defining a hostname -> address mapping and adding it to the host names XFA (cross file array).

Example:

#include "hosts.h"
#define IPV6_ADDR_LOOPBACK
Static initializer for the loopback IPv6 address (::1)
Definition: addr.h:89

Definition at line 129 of file hosts.h.

Function Documentation

◆ hosts_query()

int hosts_query ( const char *  host_name,
void *  addr_out,
int  family 
)

Get IP address for a host name.

Host Name <-> IP address mappings can be statically defined with the HOST_IPV6 macro.

Parameters
[in]host_nameHost name to resolve into address
[out]addr_outBuffer to write result into, but be able to accommodate an address of the selected family
[in]familyEither AF_INET (IPv4), AF_INET6 (IPv6) or AF_UNSPEC (either IPv4 or IPv6)
Returns
the size of the resolved address on success or an error code
Return values
-ENOENTif no matching entry could be found