1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00
icecast-common/include/igloo/socket.h

321 lines
10 KiB
C
Raw Normal View History

2019-09-18 05:20:38 -04:00
/* Copyright (C) 2019 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _LIBIGLOO__SOCKET_H_
#define _LIBIGLOO__SOCKET_H_
/**
* @file
* Put a good description of this file here
*/
#ifdef __cplusplus
extern "C" {
#endif
/* Put stuff here */
2019-09-18 14:40:59 -04:00
#include <igloo/config.h>
#ifdef IGLOO_CTC_HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef IGLOO_CTC_HAVE_POLL
#include <poll.h>
#endif
2019-09-18 14:40:59 -04:00
#include "types.h"
2019-09-18 19:16:44 -04:00
#include "socketaddr.h"
2019-09-18 05:20:38 -04:00
igloo_RO_FORWARD_TYPE(igloo_socket_t);
2019-10-03 19:07:00 -04:00
/* Type used for socket shutdown. */
2019-09-18 14:40:59 -04:00
typedef enum {
2019-10-03 19:07:00 -04:00
/* Do not shut down the socket. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_SHUTDOWN_NONE = 0x0,
2019-10-03 19:07:00 -04:00
/* Shut the receiving end down. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_SHUTDOWN_RECEIVE = 0x1,
2019-10-03 19:07:00 -04:00
/* Shut the sending end down. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_SHUTDOWN_SEND = 0x2,
2019-10-03 19:07:00 -04:00
/* Shut down both ends. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_SHUTDOWN_RECEIVESEND = igloo_SOCKET_SHUTDOWN_RECEIVE|igloo_SOCKET_SHUTDOWN_SEND
} igloo_socket_shutdown_t;
/* Advanced control functions.
*/
typedef enum {
2019-10-03 19:10:13 -04:00
/* No operation.
* Parameters: None.
*/
2019-09-18 14:40:59 -04:00
igloo_SOCKET_CONTROL_NONE = 0,
2019-10-03 19:10:13 -04:00
/* Set no delay options.
* Parameters: None.
*/
2019-09-18 14:40:59 -04:00
igloo_SOCKET_CONTROL_SET_NODELAY,
2019-10-03 19:10:13 -04:00
/* Set send and receive buffers
* Parameters: (size_t)buffersize.
*/
2019-09-18 14:40:59 -04:00
igloo_SOCKET_CONTROL_SET_SEND_BUFFER,
igloo_SOCKET_CONTROL_SET_RECEIVE_BUFFER
} igloo_socket_control_t;
2019-10-03 19:07:00 -04:00
/* Address operations. */
2019-09-18 14:40:59 -04:00
typedef enum {
2019-10-03 19:07:00 -04:00
/* Clear the list of addresses for the given endpoint. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ADDRESSOP_CLEAR,
2019-10-03 19:07:00 -04:00
/* Add an address for the given endpoint. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ADDRESSOP_ADD,
2019-10-03 19:07:00 -04:00
/* Remove an address from the given endpoint. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ADDRESSOP_REMOVE,
2019-10-03 19:07:00 -04:00
/* Replace all addresses on an endpoint with the new address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ADDRESSOP_REPLACE
} igloo_socket_addressop_t;
2019-10-03 19:07:00 -04:00
/* Endpoints of the socket.
*
* Physical addresses are as seen by the process.
* Logical addresses are addresses as seen by the peer.
*
* E.g. when there is a port forwarding from A:a -> B:b
* the local address is A:a and the phyical address is B:b.
*/
2019-09-18 14:40:59 -04:00
typedef enum {
2019-10-03 19:07:00 -04:00
/* Local address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_LOCAL,
2019-10-03 19:07:00 -04:00
/* Local physical address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_LOCAL_PHYSICAL,
2019-10-03 19:07:00 -04:00
/* Local logical address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_LOCAL_LOGICAL,
2019-10-03 19:07:00 -04:00
/* Peer address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_PEER,
2019-10-03 19:07:00 -04:00
/* Peer's physical address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_PEER_PHYSICAL,
2019-10-03 19:07:00 -04:00
/* Peer's logical address. */
2019-09-18 14:40:59 -04:00
igloo_SOCKET_ENDPOINT_PEER_LOGICAL
} igloo_socket_endpoint_t;
2019-10-03 19:07:00 -04:00
/* Actions sockets can do. */
typedef enum {
2019-10-03 19:07:00 -04:00
/* No action. */
igloo_SOCKET_ACTION_NONE = 0,
2019-10-03 19:07:00 -04:00
/* Connecting to the peer. */
igloo_SOCKET_ACTION_CONNECT,
2019-10-03 19:07:00 -04:00
/* Waiting and accepting new client connections. */
igloo_SOCKET_ACTION_ACCEPT
} igloo_socket_action_t;
2019-10-03 19:07:00 -04:00
/* Create a new socket.
*
* Parameters:
* domain, type, protocol
* Domain, Type, and Protocol for the new socket.
* name, associated, instance
* See igloo_ro_new().
* error
* The error value if the socket can not be created.
*/
igloo_socket_t * igloo_socket_new(igloo_socketaddr_domain_t domain, igloo_socketaddr_type_t type, igloo_socketaddr_protocol_t protocol, const char *name, igloo_ro_t associated, igloo_ro_t instance, igloo_error_t *error);
2019-10-03 19:07:00 -04:00
/* Create a new socket based on an address.
*
* Parameters:
* endpoint
* The endpoint to set the address for.
* This is normally igloo_SOCKET_ENDPOINT_LOCAL for listen sockets and
* igloo_SOCKET_ENDPOINT_PEER for connectig sockets.
* addr
* The address to be set for the endpoint.
* error
* The error value if the socket can not be created.
*/
igloo_socket_t * igloo_socket_new_simple(igloo_socket_endpoint_t endpoint, igloo_socketaddr_t *addr, igloo_error_t *error);
2019-10-03 19:07:00 -04:00
/* Alter addresses for the socket.
*
* This can be used to set local and peer addresses for the socket.
* What addresses are supported depends on the domain of the socket.
*
* Parameters:
* sock
* The socket to operate on.
* op
* The operation to perform. See igloo_socket_addressop_t.
* endpoint
* The endpoint to operate on. See igloo_socket_endpoint_t.
* addr
* The address to use for the operation or NULL.
*/
2019-09-18 14:40:59 -04:00
igloo_error_t igloo_socket_alter_address(igloo_socket_t *sock, igloo_socket_addressop_t op, igloo_socket_endpoint_t endpoint, igloo_socketaddr_t *addr);
2019-10-03 19:07:00 -04:00
/* Get the main address for an endpoint.
*
* This returns the main address for an endpoint.
* It depends on the socket Domain, Type, and Protocol to define what the main address is.
*
* Parameters:
* sock
* The socket to operate on.
* endpoint
* The endpoint to operate on. See igloo_socket_endpoint_t.
* error
* The error in case the address can not be returned.
*/
2019-09-18 14:40:59 -04:00
igloo_socketaddr_t * igloo_socket_get_main_address(igloo_socket_t *sock, igloo_socket_endpoint_t endpoint, igloo_error_t *error);
2019-10-03 19:07:00 -04:00
/* Get the list of addresses for an endpoint.
*
* This returns the list of all known addresses for the given endpoint.
*
* Parameters:
* sock
* The socket to operate on.
* endpoint
* The endpoint to operate on. See igloo_socket_endpoint_t.
* error
* The error in case the address can not be returned.
*/
2019-09-18 14:40:59 -04:00
igloo_list_t * igloo_socket_get_address(igloo_socket_t *sock, igloo_socket_endpoint_t endpoint, igloo_error_t *error);
2019-10-03 19:07:00 -04:00
/* Connect the socket.
*
* This lets the socket connect to the peer. The operation is blocking.
* If you want to connect non-blocking use igloo_socket_nonblocking() with igloo_SOCKET_ACTION_CONNECT.
*
* Parameters:
* sock
* The socket to operate on.
*/
2019-09-18 14:40:59 -04:00
igloo_error_t igloo_socket_connect(igloo_socket_t *sock);
2019-10-03 19:07:00 -04:00
/* This sets the socket into the listening state.
*
* Parameters:
* sock
* The socket to operate on.
* backlog
* The backlog for the listen queue or -1 for default.
*/
2019-09-18 14:40:59 -04:00
igloo_error_t igloo_socket_listen(igloo_socket_t *sock, ssize_t backlog);
2019-10-03 19:07:00 -04:00
/* Shuts the socket down.
*
* This allows shuting a socket down. This can be useful e.g. to signal EOF
* to the peer once all writing has been done.
*
* Parameters:
* sock
* The socket to operate on.
* how
* How the socket is shut down. See igloo_socket_shutdown_t.
*/
2019-09-18 14:40:59 -04:00
igloo_error_t igloo_socket_shutdown(igloo_socket_t *sock, igloo_socket_shutdown_t how);
2019-10-03 19:07:00 -04:00
/* Accept a client on a listening socket.
*
* This accepts a client on a listening connection.
* This can be called on blocking sockets or after using igloo_socket_nonblocking() with
* igloo_SOCKET_ACTION_ACCEPT to fetch a client from the queue.
*
* Parameters:
* sock
* The socket to operate on.
* name, associated
* See igloo_ro_new().
* error
* The error if no client can be accepted.
*/
igloo_socket_t * igloo_socket_accept(igloo_socket_t *sock, const char *name, igloo_ro_t associated, igloo_error_t *error);
2019-10-03 19:07:00 -04:00
/* Advanced socket control.
*
* Parameters:
* sock
* The socket to operate on.
* control
* The control to use. See igloo_socket_control_t.
* ...
* The parameters for the specific control if any.
*/
2019-09-18 14:40:59 -04:00
igloo_error_t igloo_socket_control(igloo_socket_t *sock, igloo_socket_control_t control, ...);
2019-10-03 19:07:00 -04:00
/* Request an socket action to be done in non-blocking mode.
*
* Parameters:
* sock
* The socket to operate on.
* action
* The action to be done.
* igloo_SOCKET_ACTION_NONE leaves the non-blocking mode.
* igloo_SOCKET_ACTION_CONNECT requests connect.
* igloo_socket_connect() must not be called. After this action the socket is set back into blocking mode.
* igloo_SOCKET_ACTION_ACCEPT requests accepts.
* igloo_socket_accept() must be called to fetch each client connection.
*/
igloo_error_t igloo_socket_nonblocking(igloo_socket_t *sock, igloo_socket_action_t action);
#ifdef IGLOO_CTC_HAVE_SYS_SELECT_H
2019-10-03 19:07:00 -04:00
/* Uses select() for non-blocking operations.
*
* This set of functions is used to fill sets for select().
*
* The basic idea is:
* if ((error = igloo_socket_nonblocking_select_set(sock, &readfds, &writefds, &exceptfds, &maxfd)) != igloo_ERROR_NONE)
* return error;
* select(maxfd + 1, &readfds, &writefds, &exceptfds, &timeout);
* error = igloo_socket_nonblocking_select_result(sock, &readfds, &writefds, &exceptfds);
* if (error == igloo_ERROR_NONE) {
* success...;
* } else if (error == igloo_ERROR_AGAIN) {
* timeout...;
* } else {
* return error;
* }
*
* igloo_socket_nonblocking_select_clear() can be used to clear the socket from the sets in case the sets will be reused without the socket.
*/
igloo_error_t igloo_socket_nonblocking_select_set(igloo_socket_t *sock, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, int *maxfd);
igloo_error_t igloo_socket_nonblocking_select_clear(igloo_socket_t *sock, fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
igloo_error_t igloo_socket_nonblocking_select_result(igloo_socket_t *sock, fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
#endif
#ifdef IGLOO_CTC_HAVE_POLL
2019-10-03 19:07:00 -04:00
/* Uses poll() for non-blocking operations.
*
* This set is used to fill the structure for poll().
*
* The basic idea is:
* struct pollfd fd[n];
* if ((error = igloo_socket_nonblocking_poll_fill(sock, &(fd[m]))) != igloo_ERROR_NONE)
* return error;
* poll(fd, n, timeout);
* error = igloo_socket_nonblocking_poll_result(sock, &(fd[m]));
* if (error == igloo_ERROR_NONE) {
* success...;
* } else if (error == igloo_ERROR_AGAIN) {
* timeout...;
* } else {
* return error;
* }
*/
igloo_error_t igloo_socket_nonblocking_poll_fill(igloo_socket_t *sock, struct pollfd *fd);
igloo_error_t igloo_socket_nonblocking_poll_result(igloo_socket_t *sock, struct pollfd *fd);
#endif
2019-09-18 14:40:59 -04:00
2019-09-18 05:20:38 -04:00
#ifdef __cplusplus
}
#endif
2019-09-18 07:42:40 -04:00
#endif /* ! _LIBIGLOO__SOCKET_H_ */