serve: Refactor some socket utility code into its own module.

We'll be using this in proxy mode later
This commit is contained in:
nick
2013-02-13 13:43:52 +00:00
parent 0fcbe04f80
commit ac560bd907
5 changed files with 158 additions and 101 deletions

32
src/sockutil.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef SOCKUTIL_H
#define SOCKUTIL_H
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
/* Returns the size of the sockaddr, or 0 on error */
size_t sockaddr_size(const struct sockaddr* sa);
/* Convert a sockaddr into an address. Like inet_ntop, it returns dest if
* successful, NULL otherwise. In the latter case, dest will contain "???"
*/
const char* sockaddr_address_string(const struct sockaddr* sa, char* dest, size_t len);
/* Set the SOL_REUSEADDR otion */
int sock_set_reuseaddr(int fd, int optval);
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay(int fd, int optval);
/* TODO: Set the tcp_cork option */
// int sock_set_cork(int fd, int optval);
/* Attempt to bind the fd to the sockaddr, retrying common transient failures */
int sock_try_bind(int fd, const struct sockaddr* sa);
#endif