Lely core libraries  2.2.5
addr.c File Reference
#include "io.h"
#include <lely/io/addr.h>
#include <lely/io/sock.h>
#include <lely/util/cmp.h>
#include <lely/util/errnum.h>
#include <assert.h>
#include <string.h>
#include <netdb.h>
Include dependency graph for addr.c:

Go to the source code of this file.

Functions

int io_addr_cmp (const void *p1, const void *p2)
 Compares two network addresses. More...
 
int io_addr_get_rfcomm_a (const io_addr_t *addr, char *ba, int *port)
 Obtains an RFCOMM Bluetooth device address and port number from a network address. More...
 
int io_addr_set_rfcomm_a (io_addr_t *addr, const char *ba, int port)
 Initializes a network address from an RFCOMM Bluetooth device address and port number. More...
 
int io_addr_get_rfcomm_n (const io_addr_t *addr, uint8_t ba[6], int *port)
 Obtains an RFCOMM Bluetooth device address and port number from a network address. More...
 
void io_addr_set_rfcomm_n (io_addr_t *addr, const uint8_t ba[6], int port)
 Initializes a network address from an RFCOMM Bluetooth device address and port number. More...
 
void io_addr_set_rfcomm_local (io_addr_t *addr, int port)
 Initializes a network address with the local Bluetooth (RFCOMM) device address (FF:FF:FF:00:00:00) and a port number. More...
 
int io_addr_get_ipv4_a (const io_addr_t *addr, char *ip, int *port)
 Obtains an IPv4 address and port number from a network address. More...
 
int io_addr_set_ipv4_a (io_addr_t *addr, const char *ip, int port)
 Initializes a network address from an IPv4 address and port number. More...
 
int io_addr_get_ipv4_n (const io_addr_t *addr, uint8_t ip[4], int *port)
 Obtains an IPv4 address and port number from a network address. More...
 
void io_addr_set_ipv4_n (io_addr_t *addr, const uint8_t ip[4], int port)
 Initializes a network address from an IPv4 address and port number. More...
 
void io_addr_set_ipv4_loopback (io_addr_t *addr, int port)
 Initializes a network address with the IPv4 loopback address and a port number. More...
 
void io_addr_set_ipv4_broadcast (io_addr_t *addr, int port)
 Initializes a network address with the IPv4 broadcast address and a port number. More...
 
int io_addr_get_ipv6_a (const io_addr_t *addr, char *ip, int *port)
 Obtains an IPv6 address and port number from a network address. More...
 
int io_addr_set_ipv6_a (io_addr_t *addr, const char *ip, int port)
 Initializes a network address from an IPv6 address and port number. More...
 
int io_addr_get_ipv6_n (const io_addr_t *addr, uint8_t ip[16], int *port)
 Obtains an IPv6 address and port number from a network address. More...
 
void io_addr_set_ipv6_n (io_addr_t *addr, const uint8_t ip[16], int port)
 Initializes a network address from an IPv6 address and port number. More...
 
void io_addr_set_ipv6_loopback (io_addr_t *addr, int port)
 Initializes a network address with the IPv6 loopback address and a port number. More...
 
int io_addr_get_unix (const io_addr_t *addr, char *path)
 Obtains a UNIX domain socket path name from a network address. More...
 
void io_addr_set_unix (io_addr_t *addr, const char *path)
 Initializes a network address from a UNIX domain socket path name. More...
 
int io_addr_get_domain (const io_addr_t *addr)
 Obtains the domain of a network address. More...
 
int io_addr_get_port (const io_addr_t *addr, int *port)
 Obtains the port number of an IPv4 or IPv6 network address. More...
 
int io_addr_set_port (io_addr_t *addr, int port)
 Initializes the port number of an IPv4 or IPv6 network address. More...
 
int io_addr_is_loopback (const io_addr_t *addr)
 Returns 1 if the network address is a loopback address, and 0 if not.
 
int io_addr_is_broadcast (const io_addr_t *addr)
 Returns 1 if the network address is a broadcast address, and 0 if not.
 
int io_addr_is_multicast (const io_addr_t *addr)
 Returns 1 if the network address is a multicast address, and 0 if not.
 
int io_get_addrinfo (int maxinfo, struct io_addrinfo *info, const char *nodename, const char *servname, const struct io_addrinfo *hints)
 Obtains a list of network addresses corresponding to a host and/or service name. More...
 

Detailed Description

This file is part of the I/O library; it contains the implementation of the network address functions.

See also
lely/io/addr.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file addr.c.

Function Documentation

◆ io_addr_cmp()

int io_addr_cmp ( const void *  p1,
const void *  p2 
)

Compares two network addresses.

Returns
an integer greater than, equal to, or less than 0 if the address at p1 is greater than, equal to, or less than the address at p2.

Definition at line 48 of file addr.c.

◆ io_addr_get_rfcomm_a()

int io_addr_get_rfcomm_a ( const io_addr_t addr,
char *  ba,
int *  port 
)

Obtains an RFCOMM Bluetooth device address and port number from a network address.

Parameters
addra pointer to a network address.
bathe address of a string containing at least IO_ADDR_BTH_STRLEN characters (can be NULL). On success, if ba is not NULL, *ba contains the text representation of the Bluetooth address.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number or channel.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_rfcomm_a()

Definition at line 74 of file addr.c.

◆ io_addr_set_rfcomm_a()

int io_addr_set_rfcomm_a ( io_addr_t addr,
const char *  ba,
int  port 
)

Initializes a network address from an RFCOMM Bluetooth device address and port number.

Parameters
addra pointer to the network address to be initialized.
bathe text representation of a Bluetooth address. If ba is NULL or an empty string, the wildcard address (00:00:00:00:00:00) is used.
portthe port number or channel. If port is 0, io_socket_bind() will dynamically assign a port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_get_rfcomm_a()

Definition at line 117 of file addr.c.

◆ io_addr_get_rfcomm_n()

int io_addr_get_rfcomm_n ( const io_addr_t addr,
uint8_t  ba[6],
int *  port 
)

Obtains an RFCOMM Bluetooth device address and port number from a network address.

Parameters
addra pointer to a network address.
bathe address of an array containing at least 6 four bytes (can be NULL). On success, if ba is not NULL, *ba contains the Bluetooth address in network byte order.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number or channel.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_rfcomm_n()

Definition at line 151 of file addr.c.

◆ io_addr_set_rfcomm_n()

void io_addr_set_rfcomm_n ( io_addr_t addr,
const uint8_t  ba[6],
int  port 
)

Initializes a network address from an RFCOMM Bluetooth device address and port number.

Parameters
addra pointer to the network address to be initialized.
baa pointer to a Bluetooth address in network byte order. If ba is, the wildcard address (00:00:00:00:00:00) is used.
portthe port number or channel. If port is 0, io_socket_bind() will dynamically assign a port number.
See also
io_addr_get_rfcomm_n()

Definition at line 196 of file addr.c.

◆ io_addr_set_rfcomm_local()

void io_addr_set_rfcomm_local ( io_addr_t addr,
int  port 
)

Initializes a network address with the local Bluetooth (RFCOMM) device address (FF:FF:FF:00:00:00) and a port number.

Parameters
addra pointer to the network address to be initialized.
portthe port number or PSM (Protocol Service Multiplexer). If port is 0, io_socket_bind() will dynamically assign a port number.

Definition at line 226 of file addr.c.

◆ io_addr_get_ipv4_a()

int io_addr_get_ipv4_a ( const io_addr_t addr,
char *  ip,
int *  port 
)

Obtains an IPv4 address and port number from a network address.

Parameters
addra pointer to a network address.
ipthe address of a string containing at least IO_ADDR_IPV4_STRLEN characters (can be NULL). On success, if ip is not NULL, *ip contains the text representation of the IPv4 address.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_ipv4_a()

Definition at line 255 of file addr.c.

◆ io_addr_set_ipv4_a()

int io_addr_set_ipv4_a ( io_addr_t addr,
const char *  ip,
int  port 
)

Initializes a network address from an IPv4 address and port number.

Parameters
addra pointer to the network address to be initialized.
ipthe text representation of an IPv4 address. If ip is NULL or an empty string, the wildcard address (INADDR_ANY) is used.
portthe port number.
See also
io_addr_get_ipv4_a()

Definition at line 283 of file addr.c.

◆ io_addr_get_ipv4_n()

int io_addr_get_ipv4_n ( const io_addr_t addr,
uint8_t  ip[4],
int *  port 
)

Obtains an IPv4 address and port number from a network address.

Parameters
addra pointer to a network address.
ipthe address of an array containing at least 4 four bytes (can be NULL). On success, if ip is not NULL, *ip contains the IPv4 address in network byte order.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_ipv4_n()

Definition at line 304 of file addr.c.

◆ io_addr_set_ipv4_n()

void io_addr_set_ipv4_n ( io_addr_t addr,
const uint8_t  ip[4],
int  port 
)

Initializes a network address from an IPv4 address and port number.

Parameters
addra pointer to the network address to be initialized.
ipa pointer to an IPv4 address in network byte order. If ip is NULL, the wildcard address (INADDR_ANY) is used.
portthe port number.
See also
io_addr_get_ipv4_n()

Definition at line 329 of file addr.c.

◆ io_addr_set_ipv4_loopback()

void io_addr_set_ipv4_loopback ( io_addr_t addr,
int  port 
)

Initializes a network address with the IPv4 loopback address and a port number.

Parameters
addra pointer to the network address to be initialized.
portthe port number.

Definition at line 346 of file addr.c.

◆ io_addr_set_ipv4_broadcast()

void io_addr_set_ipv4_broadcast ( io_addr_t addr,
int  port 
)

Initializes a network address with the IPv4 broadcast address and a port number.

Parameters
addra pointer to the network address to be initialized.
portthe port number.

Definition at line 360 of file addr.c.

◆ io_addr_get_ipv6_a()

int io_addr_get_ipv6_a ( const io_addr_t addr,
char *  ip,
int *  port 
)

Obtains an IPv6 address and port number from a network address.

Parameters
addra pointer to a network address.
ipthe address of a string containing at least IO_ADDR_IPV6_STRLEN characters (can be NULL). On success, if ip is not NULL, *ip contains the text representation of the IPv6 address.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_ipv6_a()

Definition at line 374 of file addr.c.

◆ io_addr_set_ipv6_a()

int io_addr_set_ipv6_a ( io_addr_t addr,
const char *  ip,
int  port 
)

Initializes a network address from an IPv6 address and port number.

Parameters
addra pointer to the network address to be initialized.
ipthe text representation of an IPv6 address. If ip is NULL or an empty string, the wildcard address (in6addr_any) is used.
portthe port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_get_ipv6_a()

Definition at line 402 of file addr.c.

◆ io_addr_get_ipv6_n()

int io_addr_get_ipv6_n ( const io_addr_t addr,
uint8_t  ip[16],
int *  port 
)

Obtains an IPv6 address and port number from a network address.

Parameters
addra pointer to a network address.
ipthe address of an array containing at least 16 four bytes (can be NULL). On success, if ip is not NULL, *ip contains the IPv6 address in network byte order.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_ipv6_a()

Definition at line 423 of file addr.c.

◆ io_addr_set_ipv6_n()

void io_addr_set_ipv6_n ( io_addr_t addr,
const uint8_t  ip[16],
int  port 
)

Initializes a network address from an IPv6 address and port number.

Parameters
addra pointer to the network address to be initialized.
ipa pointer to an IPv6 address in network byte order. If ip is NULL, the wildcard address (in6addr_any) is used.
portthe port number.
See also
io_addr_get_ipv6_a()

Definition at line 448 of file addr.c.

◆ io_addr_set_ipv6_loopback()

void io_addr_set_ipv6_loopback ( io_addr_t addr,
int  port 
)

Initializes a network address with the IPv6 loopback address and a port number.

Parameters
addra pointer to the network address to be initialized.
portthe port number.

Definition at line 465 of file addr.c.

◆ io_addr_get_unix()

int io_addr_get_unix ( const io_addr_t addr,
char *  path 
)

Obtains a UNIX domain socket path name from a network address.

Parameters
addra pointer to a network address.
paththe address of a string containing at least IO_ADDR_UNIX_STRLEN characters (can be NULL). On success, if path is not NULL, *path contains the path name.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_unix()

Definition at line 483 of file addr.c.

◆ io_addr_set_unix()

void io_addr_set_unix ( io_addr_t addr,
const char *  path 
)

Initializes a network address from a UNIX domain socket path name.

Parameters
addra pointer to the network address to be initialized.
patha pointer to a path name.
See also
io_addr_get_unix()

Definition at line 506 of file addr.c.

◆ io_addr_get_domain()

int io_addr_get_domain ( const io_addr_t addr)

Obtains the domain of a network address.

Returns
IO_SOCK_BTH, IO_SOCK_IPV4, IO_SOCK_IPV6 or IO_SOCK_UNIX, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_sock_get_domain()

Definition at line 527 of file addr.c.

◆ io_addr_get_port()

int io_addr_get_port ( const io_addr_t addr,
int *  port 
)

Obtains the port number of an IPv4 or IPv6 network address.

Parameters
addra pointer to a network address.
portthe address of a port number (can be NULL). On success, if port is not NULL, *port contains the port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_port()

Definition at line 548 of file addr.c.

◆ io_addr_set_port()

int io_addr_set_port ( io_addr_t addr,
int  port 
)

Initializes the port number of an IPv4 or IPv6 network address.

Parameters
addra pointer to the network address to be initialized.
portthe port number.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_addr_set_port()

Definition at line 614 of file addr.c.

◆ io_get_addrinfo()

int io_get_addrinfo ( int  maxinfo,
struct io_addrinfo info,
const char *  nodename,
const char *  servname,
const struct io_addrinfo hints 
)

Obtains a list of network addresses corresponding to a host and/or service name.

Parameters
maxinfothe maximum number of io_addrinfo structs to return.
infoan array of at least maxinfo io_addrinfo structs (can be NULL). On success, *info contains at most maxinfo structures describing the network addresses.
nodenamea pointer to a string containing a host (node) name or numeric address (can be NULL).
servnamea pointer to a string containing a service name or port number (can be NULL).
hintsa pointer to a network address structure containing hints about the domain and type of sockets the caller supports. If not NULL, only the domain and type fields of *hints are taken into account, if they are non-zero.
Returns
the total number of addresses (which may be different from maxinfo), or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 738 of file addr.c.