diff options
author | sr | 2013-01-15 17:57:27 +0100 |
---|---|---|
committer | sr | 2013-01-15 17:57:27 +0100 |
commit | 2d9e630a430e079ec30f674de1abbd7f4a186657 (patch) | |
tree | efb3f67a04a7dcc1f6889398a78059e31227c50b /src/server/sockhelper.h | |
parent | [SERVER] Add socket helper module to simplify connection setup (diff) | |
download | dnbd3-2d9e630a430e079ec30f674de1abbd7f4a186657.tar.gz dnbd3-2d9e630a430e079ec30f674de1abbd7f4a186657.tar.xz dnbd3-2d9e630a430e079ec30f674de1abbd7f4a186657.zip |
[SERVER] Add IPv6 support (clients and RPC connections)
Diffstat (limited to 'src/server/sockhelper.h')
-rw-r--r-- | src/server/sockhelper.h | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/server/sockhelper.h b/src/server/sockhelper.h index 28525c4..2cbcda1 100644 --- a/src/server/sockhelper.h +++ b/src/server/sockhelper.h @@ -4,8 +4,8 @@ #include <stdint.h> #include "../types.h" #include <sys/socket.h> -#include <netinet/in.h> #include <arpa/inet.h> +#include <netinet/in.h> int sock_connect4(struct sockaddr_in *addr, const int connect_ms, const int rw_ms); @@ -20,4 +20,52 @@ int sock_connect6(struct sockaddr_in6 *addr, const int connect_ms, const int rw_ */ int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const int rw_ms); +void sock_set_timeout(const int sockfd, const int milliseconds); + +/** + * Listen on all interfaces/available IP addresses, using the given protocol. + * IPv4 and IPv6 are supported. + * @param protocol_family PF_INET or PF_INET6 + * @param port port to listen on + * @return the socket descriptor if successful, -1 otherwise. + */ +int sock_listen_any(int protocol_family, uint16_t port); + +/** + * Listen on a specific address and port. + * @param addr pointer to a properly filled sockaddr_in or sockaddr_in6 + * @param addrlen length of the passed struct + */ +int sock_listen(struct sockaddr_storage *addr, int addrlen); + +/** + * This is a multi-socket version of accept. Pass in an array of listening sockets. + * If any of the sockets has an incoming connection, accept it and return the new socket's fd. + * On error, return -1, just like accept(). + * @param sockets array of listening socket fds + * @param socket_count number of sockets in that array + * @return fd of new client socket, -1 on error + */ +int accept_any(const int * const sockets, const int socket_count, struct sockaddr_storage *addr, socklen_t *length_ptr); + +void sock_set_nonblock(int sock); + +void sock_set_block(int sock); + +/** + * Add given socket to array. Take an existing empty slot ( == -1) if available, + * append to end otherwise. Updates socket count variable passed by reference. + * The passed socket fd is only added if it is != -1 for convenience, so you can + * directly pass the return value of sock_listen or sock_create, without checking the + * return value first. + * @param sock socket fd to add + * @param array the array of socket fds to add the socket to + * @param array_fill pointer to int telling how many sockets there are in the array. Empty slots + * in between are counted too. In other words: represents the index of the last valid socket fd in the + * array plus one, or 0 if there are none. + * @param array_length the capacity of the array + * @return TRUE on success or if the passed fd was -1, FALSE iff the array is already full + */ +int sock_add_array(const int sock, int *array, int *array_fill, const int array_length); + #endif /* SOCKHELPER_H_ */ |