hostport -- support of host/port tuple specifications according to RFC 2396
#include <afblib/hostport.h>
typedef struct hostport {
// parameters for socket()
int domain;
int protocol;
// parameters for bind() or connect()
struct sockaddr_storage addr;
socklen_t namelen;
} hostport;
bool parse_hostport(const char* input, hostport* hp, in_port_t defaultport);
bool get_hostport_of_peer(int socket, hostport* hp);
bool print_sockaddr(outbuf* out, struct sockaddr* addr, socklen_t namelen);
bool print_hostport(outbuf* out, hostport* hp);
parse_hostport supports the textual specification of hosts, either in domain style or as numerical IP address, and an optional port number. The term hostport was coined within RFC 2396 which provides the generic syntax for Uniform Resource Identifiers (URI). This syntax was extended by RFC 2732 to support IPv6 addresses which were already specified in RFC 2373.
Following syntax is supported:
hostport = host [ ":" port ]
host = hostname | IPv4address | IPv6reference
hostname = { domainlabel "." } toplabel [ "." ]
domainlabel = alphanum | alphanum { alphanum | "-" }
alphanum
toplabel = alpha | alpha { alphanum | "-" } alphanum
IPv4address = { digit } "." { digit } "." { digit } "." { digit }
IPv6reference = '[' IPv6address ']'
IPv6address = hexpart [ ":" IPv4address ]
hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
hexseq = { hexdigit } | hexseq ":" { hexdigit }
parse_hostport expects in input a string that conforms to the given syntax and returns, in case of success, a hostport structure that can be used for subsequent calls of socket and bind or connect. A default port can be specified using defaultport. This port is taken if no port is specified within input.
In addition, a hostport specification is permitted that begins with '/' or '.'. This is then considered to be the path of a UNIX domain socket.
get_hostport_of_peer may be called for connected sockets and returns, if successful, the peer's address of fd in *hp. print_sockaddr allows a socket address to be printed (numerically) to out. print_hostport calls print_sockaddr with the socket address stored in hp.
All functions returns true in case of success, and false otherwise.
Andreas F. Borchert