diff options
| author | Simon Rettberg | 2017-11-06 23:31:11 +0100 |
|---|---|---|
| committer | Simon Rettberg | 2017-11-06 23:31:11 +0100 |
| commit | 7f28e91b261d4f8cc8afda546a4a04867158b92f (patch) | |
| tree | 2d575a257f64d19312a8af056c2edf1b61c8c861 /src/shared/sockhelper.h | |
| parent | [FUSE] Remember up to 16 alt servers, but work only with 5 (diff) | |
| download | dnbd3-7f28e91b261d4f8cc8afda546a4a04867158b92f.tar.gz dnbd3-7f28e91b261d4f8cc8afda546a4a04867158b92f.tar.xz dnbd3-7f28e91b261d4f8cc8afda546a4a04867158b92f.zip | |
[SHARED] Add sockaddr2dnbd3 func, add multiConnect func, EINTR handling
EINTR was apparently not handled properly on non-linux for the
connect() syscall.
sockaddr2dnbd3 is what resolveToDnbd3Host already did internally,
now it's its own function.
sock_multiConnect() is a wrapper around connect() and poll,
making it easy to connect to multiple hosts in a cascaded manner,
with a slight delay between connect calls.
Diffstat (limited to 'src/shared/sockhelper.h')
| -rw-r--r-- | src/shared/sockhelper.h | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/shared/sockhelper.h b/src/shared/sockhelper.h index abfcb9c..8d70789 100644 --- a/src/shared/sockhelper.h +++ b/src/shared/sockhelper.h @@ -29,6 +29,8 @@ int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const in */ int sock_resolveToDnbd3Host(const char * const address, dnbd3_host_t * const dest, const int count); +bool sock_sockaddrToDnbd3(struct sockaddr* sa, dnbd3_host_t *host); + void sock_setTimeout(const int sockfd, const int milliseconds); size_t sock_printHost(const dnbd3_host_t * const host, char *output, const size_t len); @@ -50,18 +52,34 @@ void sock_destroyPollList(poll_list_t *list); * 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. + * @return true if any listen call was successful */ -int sock_listenAny(poll_list_t* list, uint16_t port); +bool sock_listenAny(poll_list_t* list, 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 + * @param bind_addr human readable address to bind to for listening + * @param port to listen on */ bool sock_listen(poll_list_t* list, char* bind_addr, uint16_t port); /** + * Asynchroneously connect to multiple hosts. + * This can be called multiple times with varying timeouts. Calling it + * the first time on an empty list is identical to sock_connect(). On + * consecutive calls, more nonblocking sockets in connecting state will + * be added to the list, and on each of these calls, all the pending + * sockets will be checked for successful connection (or error), respecting + * the passed timeout. + * host can be NULL to just wait on the sockets already in the list. + * If at least one socket completed the connection + * within the given timeout, it will be removed from the list and + * returned. On error or timeout, -1 is returned. If there are no more sockets + * in the list, -2 is returned. + */ +int sock_multiConnect(poll_list_t* list, const dnbd3_host_t* host, int connect_ms, int rw_ms); + +/** * 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(). |
