libgslutil
1.2.3
|
Macros | |
#define | GSL_IPV6ONLY (0x00000001) |
Force IPv6 sockets to bind to only IPv6 addresses.This flag causes IPv6 server sockets to bind only to IPv6 addresses, not to IPv4-mapped addresses. Useful when you are binding to multiple wildcard addresses. More... | |
Enumerations | |
enum | ip_subproto_t { ip_subproto_any, ip_subproto_tcp, ip_subproto_udp } |
IP-oriented connections: sub-protocol. More... | |
Functions | |
int | ip_connect (int flags, const struct addrinfo *addr)) |
Connect to a remote IP server. More... | |
int | ip_server (int flags, const struct addrinfo *addr)) |
Create an IP server. More... | |
int | ip_accept (int flags, int fd, struct addrinfo *addr)) |
Accept a client from a connection-oriented IP server. More... | |
int * | ip_servers2 (int flags, const struct addrinfo *addrs) |
Create a set of IP servers (advanced interface). More... | |
int * | ip_servers (int flags, enum ip_subproto_t subproto, const char *addrs) |
Create a set of IP servers (basic interface). More... | |
ip_connect() instantiates and connects a client socket. ip_server() instantiates and binds a server socket. ip_accept() accepts incoming connections on a server socket.
#define GSL_IPV6ONLY (0x00000001) |
Force IPv6 sockets to bind to only IPv6 addresses.This flag causes IPv6 server sockets to bind only to IPv6 addresses, not to IPv4-mapped addresses. Useful when you are binding to multiple wildcard addresses.
enum ip_subproto_t |
int ip_connect | ( | int | flags, |
const struct addrinfo * | addr | ||
) |
Connect to a remote IP server.
flags | Flags for connection. |
addr | Address of remote host (perhaps from ip_resolve()). |
-1 | on error (and see errno). |
This function attempts to create an IP client and connect it to a remote server. It will return -1 on error (errno will be set according to socket() or connect()).
flags may contain GSL_NONBLOCK to specify non-blocking mode.
If addr contains more than one entry, each entry will be tried in turn to connect to the host until one succeeds. In this case, -1 will be returned if none of the addresses succeed.
int ip_server | ( | int | flags, |
const struct addrinfo * | addr | ||
) |
Create an IP server.
flags | Flags for server socket. |
addr | Address to bind to. |
-1 | on error (and see errno). |
This function attempts to create an IP server and bind it to the specified local address. It will return -1 on error (errno will be set according to socket(), bind() or (if relevant) listen()). The address will typically come from ip_resolve(), which can be called with a host argument of 0 to return an address that allows the server to listen on all network interfaces.
flags may contain GSL_NONBLOCK to specify non-blocking mode (this will only affect the server; newly-accepted clients may be either blocking or non-blocking). It may also contain GSL_IPV6ONLY to ensure a socket is not bound to an IPv4-in-IPv6 mapped address (ignored unless addr is an IPv6 address).
int ip_accept | ( | int | flags, |
int | fd, | ||
struct addrinfo * | addr | ||
) |
Accept a client from a connection-oriented IP server.
flags | Flags for client socket. | |
fd | Server's file descriptor. | |
[out] | addr | The client's remote address. May be 0. |
-1 | on error (and see errno). |
This function attempts to accept() a connection from an IP server. It will retry on any error except EAGAIN, which indicates that there are actually no more clients to accept. It should be called in a loop until it does this. If a persistent error occurs, -1 will be returned and the last error recorded in errno.
The client's remote address may be retrieved by passing a pointer to a struct addrinfo
in addr (see sockaddr_to_addrinfo()).
flags may contain GSL_NONBLOCK to specify that a newly-accepted client will be in non-blocking mode.
int* ip_servers2 | ( | int | flags, |
const struct addrinfo * | addrs | ||
) |
Create a set of IP servers (advanced interface).
flags | Flags for server socket. |
addrs | Address(es) to bind to, in linked list form as per ipaddr_resolve or getaddrinfo(3) . |
0 | on error (and see errno). |
This function creates a set of IP server sockets based on all the addresses given in the list addrs.
Any IPv6 sockets created are bound with IPV6_V6ONLY
(see ipv6(7)
) in order to avoid problems with "Address already in use" when binding to e.g. the wildcard address.
If resolving or binding of any of the addresses fails, then any opened sockets are closed and 0 is returned. errno will be set appropriately.
The returned array should be freed with free(3)
. The array has a terminating value of -1, the invalid file descriptor.
int* ip_servers | ( | int | flags, |
enum ip_subproto_t | subproto, | ||
const char * | addrs | ||
) |
Create a set of IP servers (basic interface).
flags | Flags for server socket. |
subproto | IP subprotocol (TCP, UDP, any). |
addrs | Address(es) to bind to. |
0 | on error (and see errno). |
This function creates a set of IP server sockets based on all the addresses given in the string addrs.
If subproto is specified as TCP or UDP, then only TCP or UDP sockets (respectively) will be created. However, if subproto is specified as ip_subproto_any, then both TCP and UDP sockets may be created, depending on how the service name is specified. A service name specified as a port number resolves successfully as both TCP and UDP. String names depend upon /etc/services to determine whether TCP, UDP or both are valid for each service.
The string addrs is a set of one or more whitespace-separated addresses. Each address may consist of either service
or bind,service
where bind
is taken to be "any address" if it is omitted as in the first form. Each address listed might return multiple socket addresses (for instance, specifying just "domain" is likely to return four sockets, TCP/IPv4, UDP/IPv4, TCP/IPv6 and UDP/IPv6).
If the string in addrs cannot be interpreted (usually because one of the component names is too long), the function will fail (returning 0) and errno will be set to EDOM
. If resolving fails, errno will be set to EADDRNOTAVAIL
, and the error may be further found from ipaddr_resolve_errstr.