2013-02-13 13:43:52 +00:00
|
|
|
#ifndef SOCKUTIL_H
|
|
|
|
|
|
|
|
#define SOCKUTIL_H
|
|
|
|
|
2013-02-14 16:24:10 +00:00
|
|
|
#include <sys/time.h>
|
2013-02-13 13:43:52 +00:00
|
|
|
#include <sys/socket.h>
|
2013-02-14 16:24:10 +00:00
|
|
|
#include <sys/select.h>
|
2013-02-13 13:43:52 +00:00
|
|
|
|
|
|
|
/* Returns the size of the sockaddr, or 0 on error */
|
2018-02-20 10:05:35 +00:00
|
|
|
size_t sockaddr_size(const struct sockaddr *sa);
|
2013-02-13 13:43:52 +00:00
|
|
|
|
|
|
|
/* Convert a sockaddr into an address. Like inet_ntop, it returns dest if
|
|
|
|
* successful, NULL otherwise. In the latter case, dest will contain "???"
|
|
|
|
*/
|
2018-02-20 10:05:35 +00:00
|
|
|
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
|
|
|
|
size_t len);
|
2013-02-13 13:43:52 +00:00
|
|
|
|
2018-01-10 13:49:22 +00:00
|
|
|
/* Configure TCP keepalive on a socket */
|
2018-02-20 10:05:35 +00:00
|
|
|
int sock_set_keepalive_params(int fd, int time, int intvl, int probes);
|
2018-01-10 13:49:22 +00:00
|
|
|
|
|
|
|
/* Set the SOL_KEEPALIVE otion */
|
|
|
|
int sock_set_keepalive(int fd, int optval);
|
|
|
|
|
2013-02-13 13:43:52 +00:00
|
|
|
/* Set the SOL_REUSEADDR otion */
|
|
|
|
int sock_set_reuseaddr(int fd, int optval);
|
|
|
|
|
2018-01-10 13:49:22 +00:00
|
|
|
/* Set the tcp_keepidle option */
|
|
|
|
int sock_set_tcp_keepidle(int fd, int optval);
|
|
|
|
|
|
|
|
/* Set the tcp_keepintvl option */
|
|
|
|
int sock_set_tcp_keepintvl(int fd, int optval);
|
|
|
|
|
|
|
|
/* Set the tcp_keepcnt option */
|
|
|
|
int sock_set_tcp_keepcnt(int fd, int optval);
|
|
|
|
|
2013-02-13 13:43:52 +00:00
|
|
|
/* Set the tcp_nodelay option */
|
|
|
|
int sock_set_tcp_nodelay(int fd, int optval);
|
|
|
|
|
2014-02-25 15:44:46 +00:00
|
|
|
/* Set the tcp_cork option */
|
|
|
|
int sock_set_tcp_cork(int fd, int optval);
|
2013-02-13 13:43:52 +00:00
|
|
|
|
2013-02-14 16:24:10 +00:00
|
|
|
int sock_set_nonblock(int fd, int optval);
|
|
|
|
|
2013-02-13 13:43:52 +00:00
|
|
|
/* Attempt to bind the fd to the sockaddr, retrying common transient failures */
|
2018-02-20 10:05:35 +00:00
|
|
|
int sock_try_bind(int fd, const struct sockaddr *sa);
|
2013-02-13 13:43:52 +00:00
|
|
|
|
2013-02-14 16:24:10 +00:00
|
|
|
/* Try to call select(), retrying EINTR */
|
2018-02-20 10:05:35 +00:00
|
|
|
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
|
|
|
|
fd_set * exceptfds, struct timeval *timeout);
|
2013-02-14 16:24:10 +00:00
|
|
|
|
|
|
|
/* Try to call connect(), timing out after wait seconds */
|
2018-02-20 10:05:35 +00:00
|
|
|
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
|
|
|
|
int wait);
|
2013-02-14 16:24:10 +00:00
|
|
|
|
2013-03-15 12:07:16 +00:00
|
|
|
/* Try to call close(), retrying EINTR */
|
2018-02-20 10:05:35 +00:00
|
|
|
int sock_try_close(int fd);
|
2013-03-15 12:07:16 +00:00
|
|
|
|
2013-02-13 13:43:52 +00:00
|
|
|
#endif
|