#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
+#ifdef HAVE_NETINET_TCP_FSM_H
+#include <netinet/tcp_fsm.h>
+#endif
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdlib.h>
# endif
#endif
+#define socket_wrapper_init_mutex(m) \
+ _socket_wrapper_init_mutex(m, #m)
+
/* Add new global locks here please */
-# define SWRAP_LOCK_ALL \
- swrap_mutex_lock(&libc_symbol_binding_mutex); \
+# define SWRAP_REINIT_ALL do { \
+ size_t __i; \
+ int ret; \
+ ret = socket_wrapper_init_mutex(&sockets_mutex); \
+ if (ret != 0) exit(-1); \
+ ret = socket_wrapper_init_mutex(&socket_reset_mutex); \
+ if (ret != 0) exit(-1); \
+ ret = socket_wrapper_init_mutex(&first_free_mutex); \
+ if (ret != 0) exit(-1); \
+ for (__i = 0; (sockets != NULL) && __i < socket_info_max; __i++) { \
+ ret = socket_wrapper_init_mutex(&sockets[__i].meta.mutex); \
+ if (ret != 0) exit(-1); \
+ } \
+ ret = socket_wrapper_init_mutex(&autobind_start_mutex); \
+ if (ret != 0) exit(-1); \
+ ret = socket_wrapper_init_mutex(&pcap_dump_mutex); \
+ if (ret != 0) exit(-1); \
+ ret = socket_wrapper_init_mutex(&mtu_update_mutex); \
+ if (ret != 0) exit(-1); \
+} while(0)
-# define SWRAP_UNLOCK_ALL \
- swrap_mutex_unlock(&libc_symbol_binding_mutex); \
+# define SWRAP_LOCK_ALL do { \
+ size_t __i; \
+ swrap_mutex_lock(&sockets_mutex); \
+ swrap_mutex_lock(&socket_reset_mutex); \
+ swrap_mutex_lock(&first_free_mutex); \
+ for (__i = 0; (sockets != NULL) && __i < socket_info_max; __i++) { \
+ swrap_mutex_lock(&sockets[__i].meta.mutex); \
+ } \
+ swrap_mutex_lock(&autobind_start_mutex); \
+ swrap_mutex_lock(&pcap_dump_mutex); \
+ swrap_mutex_lock(&mtu_update_mutex); \
+} while(0)
+
+# define SWRAP_UNLOCK_ALL do { \
+ size_t __s; \
+ swrap_mutex_unlock(&mtu_update_mutex); \
+ swrap_mutex_unlock(&pcap_dump_mutex); \
+ swrap_mutex_unlock(&autobind_start_mutex); \
+ for (__s = 0; (sockets != NULL) && __s < socket_info_max; __s++) { \
+ size_t __i = (socket_info_max - 1) - __s; \
+ swrap_mutex_unlock(&sockets[__i].meta.mutex); \
+ } \
+ swrap_mutex_unlock(&first_free_mutex); \
+ swrap_mutex_unlock(&socket_reset_mutex); \
+ swrap_mutex_unlock(&sockets_mutex); \
+} while(0)
#define SOCKET_INFO_CONTAINER(si) \
(struct socket_info_container *)(si)
} sa;
};
-int first_free;
+static int first_free;
struct socket_info
{
int defer_connect;
int pktinfo;
int tcp_nodelay;
+ int listening;
/* The unix path so we can unlink it on close() */
struct sockaddr_un un_addr;
/* Hash table to map fds to corresponding socket_info index */
static int *socket_fds_idx;
-/* Mutex to synchronize access to global libc.symbols */
-static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
-
/* Mutex for syncronizing port selection during swrap_auto_bind() */
-static pthread_mutex_t autobind_start_mutex;
+static pthread_mutex_t autobind_start_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex to guard the initialization of array of socket_info structures */
-static pthread_mutex_t sockets_mutex;
+static pthread_mutex_t sockets_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex to guard the socket reset in swrap_close() and swrap_remove_stale() */
-static pthread_mutex_t socket_reset_mutex;
+static pthread_mutex_t socket_reset_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex to synchronize access to first free index in socket_info array */
-static pthread_mutex_t first_free_mutex;
+static pthread_mutex_t first_free_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex to synchronize access to packet capture dump file */
-static pthread_mutex_t pcap_dump_mutex;
+static pthread_mutex_t pcap_dump_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex for synchronizing mtu value fetch*/
-static pthread_mutex_t mtu_update_mutex;
+static pthread_mutex_t mtu_update_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Function prototypes */
bool socket_wrapper_enabled(void);
+#if ! defined(HAVE_CONSTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_INIT)
+/* xlC and other oldschool compilers support (only) this */
+#pragma init (swrap_constructor)
+#endif
void swrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
+#if ! defined(HAVE_DESTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_FINI)
+#pragma fini (swrap_destructor)
+#endif
void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
#ifndef HAVE_GETPROGNAME
enum swrap_lib {
SWRAP_LIBC,
- SWRAP_LIBNSL,
SWRAP_LIBSOCKET,
};
switch (lib) {
case SWRAP_LIBC:
return "libc";
- case SWRAP_LIBNSL:
- return "libnsl";
case SWRAP_LIBSOCKET:
return "libsocket";
}
#endif
switch (lib) {
- case SWRAP_LIBNSL:
- FALL_THROUGH;
case SWRAP_LIBSOCKET:
#ifdef HAVE_LIBSOCKET
handle = swrap.libc.socket_handle;
}
break;
#endif
- FALL_THROUGH;
case SWRAP_LIBC:
handle = swrap.libc.handle;
#ifdef LIBC_SO
handle = swrap.libc.handle = swrap.libc.socket_handle = RTLD_NEXT;
#else
SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to dlopen library: %s\n",
+ "Failed to dlopen library: %s",
dlerror());
exit(-1);
#endif
func = dlsym(handle, fn_name);
if (func == NULL) {
SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to find %s: %s\n",
+ "Failed to find %s: %s",
fn_name,
dlerror());
exit(-1);
return func;
}
-static void swrap_mutex_lock(pthread_mutex_t *mutex)
+#define swrap_mutex_lock(m) _swrap_mutex_lock(m, #m, __func__, __LINE__)
+static void _swrap_mutex_lock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
{
int ret;
ret = pthread_mutex_lock(mutex);
if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't lock pthread mutex - %s",
- strerror(ret));
+ SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't lock pthread mutex(%s) - %s",
+ getpid(), getppid(), caller, line, name, strerror(ret));
}
}
-static void swrap_mutex_unlock(pthread_mutex_t *mutex)
+#define swrap_mutex_unlock(m) _swrap_mutex_unlock(m, #m, __func__, __LINE__)
+static void _swrap_mutex_unlock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
{
int ret;
ret = pthread_mutex_unlock(mutex);
if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't unlock pthread mutex - %s",
- strerror(ret));
+ SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't unlock pthread mutex(%s) - %s",
+ getpid(), getppid(), caller, line, name, strerror(ret));
}
}
* This is an optimization to avoid locking each time we check if the symbol is
* bound.
*/
+#define _swrap_bind_symbol_generic(lib, sym_name) do { \
+ swrap.libc.symbols._libc_##sym_name.obj = \
+ _swrap_bind_symbol(lib, #sym_name); \
+} while(0);
+
#define swrap_bind_symbol_libc(sym_name) \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap_mutex_lock(&libc_symbol_binding_mutex); \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap.libc.symbols._libc_##sym_name.obj = \
- _swrap_bind_symbol(SWRAP_LIBC, #sym_name); \
- } \
- swrap_mutex_unlock(&libc_symbol_binding_mutex); \
- }
+ _swrap_bind_symbol_generic(SWRAP_LIBC, sym_name)
#define swrap_bind_symbol_libsocket(sym_name) \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap_mutex_lock(&libc_symbol_binding_mutex); \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap.libc.symbols._libc_##sym_name.obj = \
- _swrap_bind_symbol(SWRAP_LIBSOCKET, #sym_name); \
- } \
- swrap_mutex_unlock(&libc_symbol_binding_mutex); \
- }
+ _swrap_bind_symbol_generic(SWRAP_LIBSOCKET, sym_name)
-#define swrap_bind_symbol_libnsl(sym_name) \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap_mutex_lock(&libc_symbol_binding_mutex); \
- if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
- swrap.libc.symbols._libc_##sym_name.obj = \
- _swrap_bind_symbol(SWRAP_LIBNSL, #sym_name); \
- } \
- swrap_mutex_unlock(&libc_symbol_binding_mutex); \
- }
+static void swrap_bind_symbol_all(void);
/****************************************************************************
* IMPORTANT
socklen_t *addrlen,
int flags)
{
- swrap_bind_symbol_libsocket(accept4);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_accept4.f(sockfd, addr, addrlen, flags);
}
static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
- swrap_bind_symbol_libsocket(accept);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_accept.f(sockfd, addr, addrlen);
}
const struct sockaddr *addr,
socklen_t addrlen)
{
- swrap_bind_symbol_libsocket(bind);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_bind.f(sockfd, addr, addrlen);
}
static int libc_close(int fd)
{
- swrap_bind_symbol_libc(close);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_close.f(fd);
}
const struct sockaddr *addr,
socklen_t addrlen)
{
- swrap_bind_symbol_libsocket(connect);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_connect.f(sockfd, addr, addrlen);
}
static int libc_dup(int fd)
{
- swrap_bind_symbol_libc(dup);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_dup.f(fd);
}
static int libc_dup2(int oldfd, int newfd)
{
- swrap_bind_symbol_libc(dup2);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_dup2.f(oldfd, newfd);
}
#ifdef HAVE_EVENTFD
static int libc_eventfd(int count, int flags)
{
- swrap_bind_symbol_libc(eventfd);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_eventfd.f(count, flags);
}
void *arg;
int rc;
- swrap_bind_symbol_libc(fcntl);
+ swrap_bind_symbol_all();
arg = va_arg(ap, void *);
struct sockaddr *addr,
socklen_t *addrlen)
{
- swrap_bind_symbol_libsocket(getpeername);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_getpeername.f(sockfd, addr, addrlen);
}
struct sockaddr *addr,
socklen_t *addrlen)
{
- swrap_bind_symbol_libsocket(getsockname);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_getsockname.f(sockfd, addr, addrlen);
}
void *optval,
socklen_t *optlen)
{
- swrap_bind_symbol_libsocket(getsockopt);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_getsockopt.f(sockfd,
level,
void *arg;
int rc;
- swrap_bind_symbol_libc(ioctl);
+ swrap_bind_symbol_all();
arg = va_arg(ap, void *);
static int libc_listen(int sockfd, int backlog)
{
- swrap_bind_symbol_libsocket(listen);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_listen.f(sockfd, backlog);
}
static FILE *libc_fopen(const char *name, const char *mode)
{
- swrap_bind_symbol_libc(fopen);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_fopen.f(name, mode);
}
#ifdef HAVE_FOPEN64
static FILE *libc_fopen64(const char *name, const char *mode)
{
- swrap_bind_symbol_libc(fopen64);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_fopen64.f(name, mode);
}
int mode = 0;
int fd;
- swrap_bind_symbol_libc(open);
+ swrap_bind_symbol_all();
if (flags & O_CREAT) {
mode = va_arg(ap, int);
int mode = 0;
int fd;
- swrap_bind_symbol_libc(open64);
+ swrap_bind_symbol_all();
if (flags & O_CREAT) {
mode = va_arg(ap, int);
int mode = 0;
int fd;
- swrap_bind_symbol_libc(openat);
+ swrap_bind_symbol_all();
if (flags & O_CREAT) {
mode = va_arg(ap, int);
static int libc_pipe(int pipefd[2])
{
- swrap_bind_symbol_libsocket(pipe);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_pipe.f(pipefd);
}
static int libc_read(int fd, void *buf, size_t count)
{
- swrap_bind_symbol_libc(read);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_read.f(fd, buf, count);
}
static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
{
- swrap_bind_symbol_libsocket(readv);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_readv.f(fd, iov, iovcnt);
}
static int libc_recv(int sockfd, void *buf, size_t len, int flags)
{
- swrap_bind_symbol_libsocket(recv);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_recv.f(sockfd, buf, len, flags);
}
struct sockaddr *src_addr,
socklen_t *addrlen)
{
- swrap_bind_symbol_libsocket(recvfrom);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_recvfrom.f(sockfd,
buf,
static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
{
- swrap_bind_symbol_libsocket(recvmsg);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_recvmsg.f(sockfd, msg, flags);
}
static int libc_send(int sockfd, const void *buf, size_t len, int flags)
{
- swrap_bind_symbol_libsocket(send);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_send.f(sockfd, buf, len, flags);
}
static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
{
- swrap_bind_symbol_libsocket(sendmsg);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_sendmsg.f(sockfd, msg, flags);
}
const struct sockaddr *dst_addr,
socklen_t addrlen)
{
- swrap_bind_symbol_libsocket(sendto);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_sendto.f(sockfd,
buf,
const void *optval,
socklen_t optlen)
{
- swrap_bind_symbol_libsocket(setsockopt);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_setsockopt.f(sockfd,
level,
#ifdef HAVE_SIGNALFD
static int libc_signalfd(int fd, const sigset_t *mask, int flags)
{
- swrap_bind_symbol_libsocket(signalfd);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_signalfd.f(fd, mask, flags);
}
static int libc_socket(int domain, int type, int protocol)
{
- swrap_bind_symbol_libsocket(socket);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_socket.f(domain, type, protocol);
}
static int libc_socketpair(int domain, int type, int protocol, int sv[2])
{
- swrap_bind_symbol_libsocket(socketpair);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_socketpair.f(domain, type, protocol, sv);
}
#ifdef HAVE_TIMERFD_CREATE
static int libc_timerfd_create(int clockid, int flags)
{
- swrap_bind_symbol_libc(timerfd_create);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_timerfd_create.f(clockid, flags);
}
static ssize_t libc_write(int fd, const void *buf, size_t count)
{
- swrap_bind_symbol_libc(write);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_write.f(fd, buf, count);
}
static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
{
- swrap_bind_symbol_libsocket(writev);
+ swrap_bind_symbol_all();
return swrap.libc.symbols._libc_writev.f(fd, iov, iovcnt);
}
/* DO NOT call this function during library initialization! */
-static void swrap_bind_symbol_all(void)
+static void __swrap_bind_symbol_all_once(void)
{
#ifdef HAVE_ACCEPT4
swrap_bind_symbol_libsocket(accept4);
swrap_bind_symbol_libsocket(writev);
}
+static void swrap_bind_symbol_all(void)
+{
+ static pthread_once_t all_symbol_binding_once = PTHREAD_ONCE_INIT;
+
+ pthread_once(&all_symbol_binding_once, __swrap_bind_symbol_all_once);
+}
+
/*********************************************************
* SWRAP HELPER FUNCTIONS
*********************************************************/
+/*
+ * We return 127.0.0.0 (default) or 10.53.57.0.
+ *
+ * This can be controlled by:
+ * SOCKET_WRAPPER_IPV4_NETWORK=127.0.0.0 (default)
+ * or
+ * SOCKET_WRAPPER_IPV4_NETWORK=10.53.57.0
+ */
+static in_addr_t swrap_ipv4_net(void)
+{
+ static int initialized;
+ static in_addr_t hv;
+ const char *net_str = NULL;
+ struct in_addr nv;
+ int ret;
+
+ if (initialized) {
+ return hv;
+ }
+ initialized = 1;
+
+ net_str = getenv("SOCKET_WRAPPER_IPV4_NETWORK");
+ if (net_str == NULL) {
+ net_str = "127.0.0.0";
+ }
+
+ ret = inet_pton(AF_INET, net_str, &nv);
+ if (ret <= 0) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "INVALID IPv4 Network [%s]",
+ net_str);
+ abort();
+ }
+
+ hv = ntohl(nv.s_addr);
+
+ switch (hv) {
+ case 0x7f000000:
+ /* 127.0.0.0 */
+ break;
+ case 0x0a353900:
+ /* 10.53.57.0 */
+ break;
+ default:
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "INVALID IPv4 Network [%s][0x%x] should be "
+ "127.0.0.0 or 10.53.57.0",
+ net_str, (unsigned)hv);
+ abort();
+ }
+
+ return hv;
+}
+
+/*
+ * This returns 127.255.255.255 or 10.255.255.255
+ */
+static in_addr_t swrap_ipv4_bcast(void)
+{
+ in_addr_t hv;
+
+ hv = swrap_ipv4_net();
+ hv |= IN_CLASSA_HOST;
+
+ return hv;
+}
+
+/*
+ * This returns 127.0.0.${iface} or 10.53.57.${iface}
+ */
+static in_addr_t swrap_ipv4_iface(unsigned int iface)
+{
+ in_addr_t hv;
+
+ if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "swrap_ipv4_iface(%u) invalid!",
+ iface);
+ abort();
+ return -1;
+ }
+
+ hv = swrap_ipv4_net();
+ hv |= iface;
+
+ return hv;
+}
+
#ifdef HAVE_IPV6
/*
* FD00::5357:5FXX
sic->meta.next_free = next_free;
}
+static int swrap_un_path(struct sockaddr_un *un,
+ const char *swrap_dir,
+ char type,
+ unsigned int iface,
+ unsigned int prt)
+{
+ int ret;
+
+ ret = snprintf(un->sun_path,
+ sizeof(un->sun_path),
+ "%s/"SOCKET_FORMAT,
+ swrap_dir,
+ type,
+ iface,
+ prt);
+ if ((size_t)ret >= sizeof(un->sun_path)) {
+ return ENAMETOOLONG;
+ }
+
+ return 0;
+}
+
+static int swrap_un_path_EINVAL(struct sockaddr_un *un,
+ const char *swrap_dir)
+{
+ int ret;
+
+ ret = snprintf(un->sun_path,
+ sizeof(un->sun_path),
+ "%s/EINVAL",
+ swrap_dir);
+
+ if ((size_t)ret >= sizeof(un->sun_path)) {
+ return ENAMETOOLONG;
+ }
+
+ return 0;
+}
+
+static bool swrap_dir_usable(const char *swrap_dir)
+{
+ struct sockaddr_un un;
+ int ret;
+
+ ret = swrap_un_path(&un, swrap_dir, SOCKET_TYPE_CHAR_TCP, 0, 0);
+ if (ret == 0) {
+ return true;
+ }
+
+ ret = swrap_un_path_EINVAL(&un, swrap_dir);
+ if (ret == 0) {
+ return true;
+ }
+
+ return false;
+}
+
static char *socket_wrapper_dir(void)
{
char *swrap_dir = NULL;
char *s = getenv("SOCKET_WRAPPER_DIR");
+ char *t;
+ bool ok;
- if (s == NULL) {
+ if (s == NULL || s[0] == '\0') {
+ SWRAP_LOG(SWRAP_LOG_WARN, "SOCKET_WRAPPER_DIR not set");
return NULL;
}
swrap_dir = realpath(s, NULL);
if (swrap_dir == NULL) {
SWRAP_LOG(SWRAP_LOG_ERROR,
- "Unable to resolve socket_wrapper dir path: %s",
+ "Unable to resolve socket_wrapper dir path: %s - %s",
+ s,
strerror(errno));
- return NULL;
+ abort();
}
+ ok = swrap_dir_usable(swrap_dir);
+ if (ok) {
+ goto done;
+ }
+
+ free(swrap_dir);
+
+ ok = swrap_dir_usable(s);
+ if (!ok) {
+ SWRAP_LOG(SWRAP_LOG_ERROR, "SOCKET_WRAPPER_DIR is too long");
+ abort();
+ }
+
+ t = getenv("SOCKET_WRAPPER_DIR_ALLOW_ORIG");
+ if (t == NULL) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "realpath(SOCKET_WRAPPER_DIR) too long and "
+ "SOCKET_WRAPPER_DIR_ALLOW_ORIG not set");
+ abort();
+
+ }
+
+ swrap_dir = strdup(s);
+ if (swrap_dir == NULL) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "Unable to duplicate socket_wrapper dir path");
+ abort();
+ }
+
+ SWRAP_LOG(SWRAP_LOG_WARN,
+ "realpath(SOCKET_WRAPPER_DIR) too long, "
+ "using original SOCKET_WRAPPER_DIR\n");
+
+done:
SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", swrap_dir);
return swrap_dir;
}
return max_mtu;
}
-static int socket_wrapper_init_mutex(pthread_mutex_t *m)
+static int _socket_wrapper_init_mutex(pthread_mutex_t *m, const char *name)
{
pthread_mutexattr_t ma;
- int ret;
-
- ret = pthread_mutexattr_init(&ma);
- if (ret != 0) {
- return ret;
- }
-
- ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK);
- if (ret != 0) {
- goto done;
- }
-
- ret = pthread_mutex_init(m, &ma);
+ bool need_destroy = false;
+ int ret = 0;
+
+#define __CHECK(cmd) do { \
+ ret = cmd; \
+ if (ret != 0) { \
+ SWRAP_LOG(SWRAP_LOG_ERROR, \
+ "%s: %s - failed %d", \
+ name, #cmd, ret); \
+ goto done; \
+ } \
+} while(0)
+ *m = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+ __CHECK(pthread_mutexattr_init(&ma));
+ need_destroy = true;
+ __CHECK(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK));
+ __CHECK(pthread_mutex_init(m, &ma));
done:
- pthread_mutexattr_destroy(&ma);
-
+ if (need_destroy) {
+ pthread_mutexattr_destroy(&ma);
+ }
return ret;
}
{
size_t max_sockets;
size_t i;
- int ret;
+ int ret = 0;
+
+ swrap_bind_symbol_all();
swrap_mutex_lock(&sockets_mutex);
return;
}
+ /*
+ * Intialize the static cache early before
+ * any thread is able to start.
+ */
+ (void)swrap_ipv4_net();
+
socket_wrapper_init_fds_idx();
/* Needs to be called inside the sockets_mutex lock here. */
for (i = 0; i < max_sockets; i++) {
swrap_set_next_free(&sockets[i].info, i+1);
+ sockets[i].meta.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+ }
+
+ for (i = 0; i < max_sockets; i++) {
ret = socket_wrapper_init_mutex(&sockets[i].meta.mutex);
if (ret != 0) {
SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
+ "Failed to initialize pthread mutex i=%zu", i);
goto done;
}
}
/* mark the end of the free list */
swrap_set_next_free(&sockets[max_sockets-1].info, -1);
- ret = socket_wrapper_init_mutex(&autobind_start_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- goto done;
- }
-
- ret = socket_wrapper_init_mutex(&pcap_dump_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- goto done;
- }
-
- ret = socket_wrapper_init_mutex(&mtu_update_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- goto done;
- }
-
done:
swrap_mutex_unlock(&first_free_mutex);
swrap_mutex_unlock(&sockets_mutex);
static void set_socket_info_index(int fd, int idx)
{
+ SWRAP_LOG(SWRAP_LOG_TRACE,
+ "fd=%d idx=%d",
+ fd, idx);
socket_fds_idx[fd] = idx;
/* This builtin issues a full memory barrier. */
__sync_synchronize();
static void reset_socket_info_index(int fd)
{
+ SWRAP_LOG(SWRAP_LOG_TRACE,
+ "fd=%d idx=%d",
+ fd, -1);
set_socket_info_index(fd, -1);
}
memset(in2, 0, sizeof(*in2));
in2->sin_family = AF_INET;
- in2->sin_addr.s_addr = htonl((127<<24) | iface);
+ in2->sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
in2->sin_port = htons(prt);
*len = sizeof(*in2);
char u_type = '\0';
char b_type = '\0';
char a_type = '\0';
+ const unsigned int sw_net_addr = swrap_ipv4_net();
+ const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
switch (si->type) {
case SOCK_STREAM:
b_type = SOCKET_TYPE_CHAR_UDP;
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
is_bcast = 2;
type = a_type;
iface = socket_wrapper_default_iface();
- } else if (b_type && addr == 0x7FFFFFFF) {
- /* 127.255.255.255 only udp */
+ } else if (b_type && addr == sw_bcast_addr) {
+ /*
+ * 127.255.255.255
+ * or
+ * 10.255.255.255
+ * only udp
+ */
is_bcast = 1;
type = b_type;
iface = socket_wrapper_default_iface();
- } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
- /* 127.0.0.X */
+ } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
+ /* 127.0.0.X or 10.53.57.X */
is_bcast = 0;
type = u_type;
iface = (addr & 0x000000FF);
type = SOCKET_TYPE_CHAR_UDP_V6;
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
}
#endif
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!");
errno = ENETUNREACH;
return -1;
}
if (prt == 0) {
- SWRAP_LOG(SWRAP_LOG_WARN, "Port not set\n");
+ SWRAP_LOG(SWRAP_LOG_WARN, "Port not set");
errno = EINVAL;
return -1;
}
swrap_dir = socket_wrapper_dir();
+ if (swrap_dir == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
if (is_bcast) {
- snprintf(un->sun_path, sizeof(un->sun_path),
- "%s/EINVAL", swrap_dir);
+ swrap_un_path_EINVAL(un, swrap_dir);
SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
SAFE_FREE(swrap_dir);
/* the caller need to do more processing */
return 0;
}
- snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
- swrap_dir, type, iface, prt);
+ swrap_un_path(un, swrap_dir, type, iface, prt);
SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
SAFE_FREE(swrap_dir);
char d_type = '\0';
char b_type = '\0';
char a_type = '\0';
+ const unsigned int sw_net_addr = swrap_ipv4_net();
+ const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
prt = ntohs(in->sin_port);
b_type = SOCKET_TYPE_CHAR_UDP;
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
is_bcast = 2;
type = a_type;
iface = socket_wrapper_default_iface();
- } else if (b_type && addr == 0x7FFFFFFF) {
+ } else if (b_type && addr == sw_bcast_addr) {
/* 127.255.255.255 only udp */
is_bcast = 1;
type = b_type;
iface = socket_wrapper_default_iface();
- } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
+ } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
/* 127.0.0.X */
is_bcast = 0;
type = u_type;
ZERO_STRUCT(bind_in);
bind_in.sin_family = in->sin_family;
bind_in.sin_port = in->sin_port;
- bind_in.sin_addr.s_addr = htonl(0x7F000000 | iface);
-
+ bind_in.sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
si->bindname.sa_socklen = blen;
memcpy(&si->bindname.sa.in, &bind_in, blen);
}
type = SOCKET_TYPE_CHAR_UDP_V6;
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
}
#endif
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
errno = EADDRNOTAVAIL;
return -1;
}
}
swrap_dir = socket_wrapper_dir();
+ if (swrap_dir == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
if (prt == 0) {
/* handle auto-allocation of ephemeral ports */
for (prt = 5001; prt < 10000; prt++) {
- snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
- swrap_dir, type, iface, prt);
+ swrap_un_path(un, swrap_dir, type, iface, prt);
if (stat(un->sun_path, &st) == 0) continue;
set_port(si->family, prt, &si->myname);
}
}
- snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
- swrap_dir, type, iface, prt);
+ swrap_un_path(un, swrap_dir, type, iface, prt);
SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
SAFE_FREE(swrap_dir);
case SOCK_DGRAM:
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
}
errno = EAFNOSUPPORT;
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
return -1;
}
case SOCK_DGRAM:
break;
default:
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
errno = ESOCKTNOSUPPORT;
return -1;
}
break;
}
- SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+ SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
errno = EAFNOSUPPORT;
return -1;
}
if (strncmp(s, "./", 2) == 0) {
s += 2;
}
+ SWRAP_LOG(SWRAP_LOG_TRACE, "SOCKET_WRAPPER_PCAP_FILE: %s", s);
return s;
}
int unreachable,
size_t *_packet_len)
{
- uint8_t *base;
- uint8_t *buf;
- struct swrap_packet_frame *frame;
- union swrap_packet_ip *ip;
+ uint8_t *base = NULL;
+ uint8_t *buf = NULL;
+ union {
+ uint8_t *ptr;
+ struct swrap_packet_frame *frame;
+ } f;
+ union {
+ uint8_t *ptr;
+ union swrap_packet_ip *ip;
+ } i;
union swrap_packet_payload *pay;
size_t packet_len;
size_t alloc_len;
- size_t nonwire_len = sizeof(*frame);
+ size_t nonwire_len = sizeof(struct swrap_packet_frame);
size_t wire_hdr_len = 0;
size_t wire_len = 0;
size_t ip_hdr_len = 0;
dest_in = (const struct sockaddr_in *)(const void *)dest;
src_port = src_in->sin_port;
dest_port = dest_in->sin_port;
- ip_hdr_len = sizeof(ip->v4);
+ ip_hdr_len = sizeof(i.ip->v4);
break;
#ifdef HAVE_IPV6
case AF_INET6:
dest_in6 = (const struct sockaddr_in6 *)(const void *)dest;
src_port = src_in6->sin6_port;
dest_port = dest_in6->sin6_port;
- ip_hdr_len = sizeof(ip->v6);
+ ip_hdr_len = sizeof(i.ip->v6);
break;
#endif
default:
if (wire_len > 64 ) {
icmp_truncate_len = wire_len - 64;
}
- wire_hdr_len += icmp_hdr_len;
wire_len += icmp_hdr_len;
}
}
buf = base;
+ f.ptr = buf;
+
+ f.frame->seconds = tval->tv_sec;
+ f.frame->micro_seconds = tval->tv_usec;
+ f.frame->recorded_length = wire_len - icmp_truncate_len;
+ f.frame->full_length = wire_len - icmp_truncate_len;
- frame = (struct swrap_packet_frame *)(void *)buf;
- frame->seconds = tval->tv_sec;
- frame->micro_seconds = tval->tv_usec;
- frame->recorded_length = wire_len - icmp_truncate_len;
- frame->full_length = wire_len - icmp_truncate_len;
buf += SWRAP_PACKET_FRAME_SIZE;
- ip = (union swrap_packet_ip *)(void *)buf;
+ i.ptr = buf;
switch (src->sa_family) {
case AF_INET:
- ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
- ip->v4.tos = 0x00;
- ip->v4.packet_length = htons(wire_len - icmp_truncate_len);
- ip->v4.identification = htons(0xFFFF);
- ip->v4.flags = 0x40; /* BIT 1 set - means don't fragment */
- ip->v4.fragment = htons(0x0000);
- ip->v4.ttl = 0xFF;
- ip->v4.protocol = protocol;
- ip->v4.hdr_checksum = htons(0x0000);
- ip->v4.src_addr = src_in->sin_addr.s_addr;
- ip->v4.dest_addr = dest_in->sin_addr.s_addr;
+ if (src_in == NULL || dest_in == NULL) {
+ SAFE_FREE(base);
+ return NULL;
+ }
+
+ i.ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
+ i.ip->v4.tos = 0x00;
+ i.ip->v4.packet_length = htons(wire_len - icmp_truncate_len);
+ i.ip->v4.identification = htons(0xFFFF);
+ i.ip->v4.flags = 0x40; /* BIT 1 set - means don't fragment */
+ i.ip->v4.fragment = htons(0x0000);
+ i.ip->v4.ttl = 0xFF;
+ i.ip->v4.protocol = protocol;
+ i.ip->v4.hdr_checksum = htons(0x0000);
+ i.ip->v4.src_addr = src_in->sin_addr.s_addr;
+ i.ip->v4.dest_addr = dest_in->sin_addr.s_addr;
buf += SWRAP_PACKET_IP_V4_SIZE;
break;
#ifdef HAVE_IPV6
case AF_INET6:
- ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
- ip->v6.flow_label_high = 0x00;
- ip->v6.flow_label_low = 0x0000;
- ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
- ip->v6.next_header = protocol;
- memcpy(ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
- memcpy(ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
+ if (src_in6 == NULL || dest_in6 == NULL) {
+ SAFE_FREE(base);
+ return NULL;
+ }
+
+ i.ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
+ i.ip->v6.flow_label_high = 0x00;
+ i.ip->v6.flow_label_low = 0x0000;
+ i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
+ i.ip->v6.next_header = protocol;
+ memcpy(i.ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
+ memcpy(i.ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
buf += SWRAP_PACKET_IP_V6_SIZE;
break;
#endif
pay->icmp4.code = 0x01; /* host unreachable */
pay->icmp4.checksum = htons(0x0000);
pay->icmp4.unused = htonl(0x00000000);
+
buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
/* set the ip header in the ICMP payload */
- ip = (union swrap_packet_ip *)(void *)buf;
- ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
- ip->v4.tos = 0x00;
- ip->v4.packet_length = htons(wire_len - icmp_hdr_len);
- ip->v4.identification = htons(0xFFFF);
- ip->v4.flags = 0x40; /* BIT 1 set - means don't fragment */
- ip->v4.fragment = htons(0x0000);
- ip->v4.ttl = 0xFF;
- ip->v4.protocol = icmp_protocol;
- ip->v4.hdr_checksum = htons(0x0000);
- ip->v4.src_addr = dest_in->sin_addr.s_addr;
- ip->v4.dest_addr = src_in->sin_addr.s_addr;
+ i.ptr = buf;
+ i.ip->v4.ver_hdrlen = 0x45; /* version 4 and 5 * 32 bit words */
+ i.ip->v4.tos = 0x00;
+ i.ip->v4.packet_length = htons(wire_len - icmp_hdr_len);
+ i.ip->v4.identification = htons(0xFFFF);
+ i.ip->v4.flags = 0x40; /* BIT 1 set - means don't fragment */
+ i.ip->v4.fragment = htons(0x0000);
+ i.ip->v4.ttl = 0xFF;
+ i.ip->v4.protocol = icmp_protocol;
+ i.ip->v4.hdr_checksum = htons(0x0000);
+ i.ip->v4.src_addr = dest_in->sin_addr.s_addr;
+ i.ip->v4.dest_addr = src_in->sin_addr.s_addr;
+
buf += SWRAP_PACKET_IP_V4_SIZE;
src_port = dest_in->sin_port;
buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
/* set the ip header in the ICMP payload */
- ip = (union swrap_packet_ip *)(void *)buf;
- ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
- ip->v6.flow_label_high = 0x00;
- ip->v6.flow_label_low = 0x0000;
- ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
- ip->v6.next_header = protocol;
- memcpy(ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
- memcpy(ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
+ i.ptr = buf;
+ i.ip->v6.ver_prio = 0x60; /* version 4 and 5 * 32 bit words */
+ i.ip->v6.flow_label_high = 0x00;
+ i.ip->v6.flow_label_low = 0x0000;
+ i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
+ i.ip->v6.next_header = protocol;
+ memcpy(i.ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
+ memcpy(i.ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
+
buf += SWRAP_PACKET_IP_V6_SIZE;
src_port = dest_in6->sin6_port;
case AF_PACKET:
#endif /* AF_PACKET */
case AF_UNIX:
- return libc_socket(family, type, protocol);
+ fd = libc_socket(family, type, protocol);
+ if (fd != -1) {
+ /* Check if we have a stale fd and remove it */
+ swrap_remove_stale(fd);
+ SWRAP_LOG(SWRAP_LOG_TRACE,
+ "Unix socket fd=%d",
+ fd);
+ }
+ return fd;
default:
errno = EAFNOSUPPORT;
return -1;
fd = ret;
+ /* Check if we have a stale fd and remove it */
+ swrap_remove_stale(fd);
+
SWRAP_LOCK_SI(parent_si);
ret = sockaddr_convert_from_un(parent_si,
memset(&in, 0, sizeof(in));
in.sin_family = AF_INET;
- in.sin_addr.s_addr = htonl(127<<24 |
- socket_wrapper_default_iface());
+ in.sin_addr.s_addr = htonl(swrap_ipv4_iface(
+ socket_wrapper_default_iface()));
si->myname = (struct swrap_address) {
.sa_socklen = sizeof(in),
}
swrap_dir = socket_wrapper_dir();
+ if (swrap_dir == NULL) {
+ errno = EINVAL;
+ ret = -1;
+ goto done;
+ }
for (i = 0; i < SOCKET_MAX_SOCKETS; i++) {
port = autobind_start + i;
- snprintf(un_addr.sa.un.sun_path, sizeof(un_addr.sa.un.sun_path),
- "%s/"SOCKET_FORMAT, swrap_dir, type,
- socket_wrapper_default_iface(), port);
+ swrap_un_path(&un_addr.sa.un,
+ swrap_dir,
+ type,
+ socket_wrapper_default_iface(),
+ port);
if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
}
if (si->family != serv_addr->sa_family) {
+ SWRAP_LOG(SWRAP_LOG_ERROR,
+ "called for fd=%d (family=%d) called with invalid family=%d",
+ s, si->family, serv_addr->sa_family);
errno = EINVAL;
ret = -1;
goto done;
}
ret = libc_listen(s, backlog);
+ if (ret == 0) {
+ si->listening = 1;
+ }
out:
SWRAP_UNLOCK_SI(si);
ret = 0;
goto done;
#endif /* TCP_NODELAY */
+#ifdef TCP_INFO
+ case TCP_INFO: {
+ struct tcp_info info;
+ socklen_t ilen = sizeof(info);
+
+#ifdef HAVE_NETINET_TCP_FSM_H
+/* This is FreeBSD */
+# define __TCP_LISTEN TCPS_LISTEN
+# define __TCP_ESTABLISHED TCPS_ESTABLISHED
+# define __TCP_CLOSE TCPS_CLOSED
+#else
+/* This is Linux */
+# define __TCP_LISTEN TCP_LISTEN
+# define __TCP_ESTABLISHED TCP_ESTABLISHED
+# define __TCP_CLOSE TCP_CLOSE
+#endif
+
+ ZERO_STRUCT(info);
+ if (si->listening) {
+ info.tcpi_state = __TCP_LISTEN;
+ } else if (si->connected) {
+ /*
+ * For now we just fake a few values
+ * supported both by FreeBSD and Linux
+ */
+ info.tcpi_state = __TCP_ESTABLISHED;
+ info.tcpi_rto = 200000; /* 200 msec */
+ info.tcpi_rtt = 5000; /* 5 msec */
+ info.tcpi_rttvar = 5000; /* 5 msec */
+ } else {
+ info.tcpi_state = __TCP_CLOSE;
+ info.tcpi_rto = 1000000; /* 1 sec */
+ info.tcpi_rtt = 0;
+ info.tcpi_rttvar = 250000; /* 250 msec */
+ }
+
+ if (optval == NULL || optlen == NULL ||
+ *optlen < (socklen_t)ilen) {
+ errno = EINVAL;
+ ret = -1;
+ goto done;
+ }
+
+ *optlen = ilen;
+ memcpy(optval, &info, ilen);
+
+ ret = 0;
+ goto done;
+ }
+#endif /* TCP_INFO */
default:
break;
}
{
struct socket_info *si = find_socket_info(s);
va_list ap;
- int value;
+ int *value_ptr = NULL;
int rc;
if (!si) {
switch (r) {
case FIONREAD:
- value = *((int *)va_arg(ap, int *));
+ if (rc == 0) {
+ value_ptr = ((int *)va_arg(ap, int *));
+ }
if (rc == -1 && errno != EAGAIN && errno != ENOBUFS) {
swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
- } else if (value == 0) { /* END OF FILE */
+ } else if (value_ptr != NULL && *value_ptr == 0) { /* END OF FILE */
swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
}
break;
+#ifdef FIONWRITE
+ case FIONWRITE:
+ /* this is FreeBSD */
+ FALL_THROUGH; /* to TIOCOUTQ */
+#endif /* FIONWRITE */
+ case TIOCOUTQ: /* same as SIOCOUTQ on Linux */
+ /*
+ * This may return more bytes then the application
+ * sent into the socket, for tcp it should
+ * return the number of unacked bytes.
+ *
+ * On AF_UNIX, all bytes are immediately acked!
+ */
+ if (rc == 0) {
+ value_ptr = ((int *)va_arg(ap, int *));
+ *value_ptr = 0;
+ }
+ break;
}
va_end(ap);
type = SOCKET_TYPE_CHAR_UDP;
swrap_dir = socket_wrapper_dir();
+ if (swrap_dir == NULL) {
+ return -1;
+ }
for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
- snprintf(un_addr.sa.un.sun_path,
- sizeof(un_addr.sa.un.sun_path),
- "%s/"SOCKET_FORMAT, swrap_dir, type, iface, prt);
+ swrap_un_path(&un_addr.sa.un,
+ swrap_dir,
+ type,
+ iface,
+ prt);
if (stat(un_addr.sa.un.sun_path, &st) != 0) continue;
/* ignore the any errors in broadcast sends */
type = SOCKET_TYPE_CHAR_UDP;
swrap_dir = socket_wrapper_dir();
+ if (swrap_dir == NULL) {
+ free(buf);
+ return -1;
+ }
for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
- snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
- swrap_dir, type, iface, prt);
+ swrap_un_path(&un_addr, swrap_dir, type, iface, prt);
if (stat(un_addr.sun_path, &st) != 0) continue;
msg.msg_name = &un_addr; /* optional address */
return libc_close(fd);
}
+ SWRAP_LOG(SWRAP_LOG_TRACE, "Close wrapper for fd=%d", fd);
reset_socket_info_index(fd);
si = swrap_get_socket_info(si_index);
static void swrap_thread_child(void)
{
- SWRAP_UNLOCK_ALL;
+ SWRAP_REINIT_ALL;
}
/****************************
***************************/
void swrap_constructor(void)
{
- int ret;
+ SWRAP_REINIT_ALL;
/*
* If we hold a lock and the application forks, then the child
pthread_atfork(&swrap_thread_prepare,
&swrap_thread_parent,
&swrap_thread_child);
-
- ret = socket_wrapper_init_mutex(&sockets_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- exit(-1);
- }
-
- ret = socket_wrapper_init_mutex(&socket_reset_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- exit(-1);
- }
-
- ret = socket_wrapper_init_mutex(&first_free_mutex);
- if (ret != 0) {
- SWRAP_LOG(SWRAP_LOG_ERROR,
- "Failed to initialize pthread mutex");
- exit(-1);
- }
}
/****************************
dlclose(swrap.libc.socket_handle);
}
}
+
+#if defined(HAVE__SOCKET) && defined(HAVE__CLOSE)
+/*
+ * On FreeBSD 12 (and maybe other platforms)
+ * system libraries like libresolv prefix there
+ * syscalls with '_' in order to always use
+ * the symbols from libc.
+ *
+ * In the interaction with resolv_wrapper,
+ * we need to inject socket wrapper into libresolv,
+ * which means we need to private all socket
+ * related syscalls also with the '_' prefix.
+ *
+ * This is tested in Samba's 'make test',
+ * there we noticed that providing '_read'
+ * and '_open' would cause errors, which
+ * means we skip '_read', '_write' and
+ * all non socket related calls without
+ * further analyzing the problem.
+ */
+#define SWRAP_SYMBOL_ALIAS(__sym, __aliassym) \
+ extern typeof(__sym) __aliassym __attribute__ ((alias(#__sym)))
+
+#ifdef HAVE_ACCEPT4
+SWRAP_SYMBOL_ALIAS(accept4, _accept4);
+#endif
+SWRAP_SYMBOL_ALIAS(accept, _accept);
+SWRAP_SYMBOL_ALIAS(bind, _bind);
+SWRAP_SYMBOL_ALIAS(close, _close);
+SWRAP_SYMBOL_ALIAS(connect, _connect);
+SWRAP_SYMBOL_ALIAS(dup, _dup);
+SWRAP_SYMBOL_ALIAS(dup2, _dup2);
+SWRAP_SYMBOL_ALIAS(fcntl, _fcntl);
+SWRAP_SYMBOL_ALIAS(getpeername, _getpeername);
+SWRAP_SYMBOL_ALIAS(getsockname, _getsockname);
+SWRAP_SYMBOL_ALIAS(getsockopt, _getsockopt);
+SWRAP_SYMBOL_ALIAS(ioctl, _ioctl);
+SWRAP_SYMBOL_ALIAS(listen, _listen);
+SWRAP_SYMBOL_ALIAS(readv, _readv);
+SWRAP_SYMBOL_ALIAS(recv, _recv);
+SWRAP_SYMBOL_ALIAS(recvfrom, _recvfrom);
+SWRAP_SYMBOL_ALIAS(recvmsg, _recvmsg);
+SWRAP_SYMBOL_ALIAS(send, _send);
+SWRAP_SYMBOL_ALIAS(sendmsg, _sendmsg);
+SWRAP_SYMBOL_ALIAS(sendto, _sendto);
+SWRAP_SYMBOL_ALIAS(setsockopt, _setsockopt);
+SWRAP_SYMBOL_ALIAS(socket, _socket);
+SWRAP_SYMBOL_ALIAS(socketpair, _socketpair);
+SWRAP_SYMBOL_ALIAS(writev, _writev);
+
+#endif /* SOCKET_WRAPPER_EXPORT_UNDERSCORE_SYMBOLS */