ZeroTierOne/netcon/Intercept.c

595 lines
19 KiB
C
Raw Normal View History

2015-10-14 17:47:35 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#ifdef USE_GNU_SOURCE
#define _GNU_SOURCE
#endif
2015-10-21 23:27:17 +00:00
#include <unistd.h>
#include <stdint.h>
2015-10-14 17:47:35 +00:00
#include <stdio.h>
#include <dlfcn.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <pwd.h>
#include <errno.h>
2015-10-28 00:12:32 +00:00
#include <linux/errno.h>
2015-10-14 17:47:35 +00:00
#include <stdarg.h>
#include <netdb.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/socket.h>
2015-10-28 00:12:32 +00:00
#include <sys/poll.h>
2015-10-14 17:47:35 +00:00
#include <sys/un.h>
#include <arpa/inet.h>
2015-12-21 13:03:26 +00:00
#include <sys/resource.h>
#include <linux/net.h> /* for NPROTO */
2015-10-14 17:47:35 +00:00
2015-12-21 13:03:26 +00:00
#define SOCK_MAX (SOCK_PACKET + 1)
#define SOCK_TYPE_MASK 0xf
2015-12-21 13:03:26 +00:00
#include "Intercept.h"
2015-12-21 13:36:27 +00:00
#include "RPC.h"
#include "common.inc.c"
2015-10-14 17:47:35 +00:00
static void load_symbols(void);
static void set_up_intercept();
2015-10-14 17:47:35 +00:00
/*------------------------------------------------------------------------------
2015-12-21 13:03:26 +00:00
------------------- Intercept<--->Service Comm mechanisms ----------------------
2015-10-14 17:47:35 +00:00
------------------------------------------------------------------------------*/
static int thispid = -1;
2016-01-11 18:12:59 +00:00
char *network_pathname;
2015-10-14 17:47:35 +00:00
/* Check whether the socket is mapped to the service or not. We
need to know if this is a regular AF_LOCAL socket or an end of a socketpair
that the service uses. We don't want to keep state in the intercept, so
we simply ask the service via an RPC */
2016-01-11 18:12:59 +00:00
static int connected_to_service(int sockfd)
2015-10-14 17:47:35 +00:00
{
2016-01-11 18:12:59 +00:00
dwr(MSG_DEBUG_EXTRA,"connected_to_service():\n");
socklen_t len;
struct sockaddr_storage addr;
len = sizeof addr;
struct sockaddr_un * addr_un;
getpeername(sockfd, (struct sockaddr*)&addr, &len);
if (addr.ss_family == AF_LOCAL || addr.ss_family == AF_LOCAL) {
addr_un = (struct sockaddr_un*)&addr;
if(strcmp(addr_un->sun_path, network_pathname) == 0) {
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Yes, %s\n", addr_un->sun_path);
return 1;
}
}
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Not connected to service\n");
return 0;
2015-10-14 17:47:35 +00:00
}
2016-01-11 18:12:59 +00:00
2015-10-14 17:47:35 +00:00
/*------------------------------------------------------------------------------
2015-12-21 13:03:26 +00:00
------------------------ ctors and dtors (and friends) ------------------------
2015-10-14 17:47:35 +00:00
------------------------------------------------------------------------------*/
static void my_dest(void) __attribute__ ((destructor));
static void my_dest(void) {
dwr(MSG_DEBUG,"closing connections to service...\n");
2015-12-21 13:03:26 +00:00
rpc_mutex_destroy();
2015-10-14 17:47:35 +00:00
}
static void load_symbols(void)
2015-10-14 17:47:35 +00:00
{
2015-12-21 15:17:37 +00:00
if(thispid == getpid()) {
2015-12-09 02:07:46 +00:00
dwr(MSG_DEBUG,"detected duplicate call to global constructor (pid=%d).\n", thispid);
2015-10-14 17:47:35 +00:00
}
thispid = getpid();
realconnect = dlsym(RTLD_NEXT, "connect");
realbind = dlsym(RTLD_NEXT, "bind");
realaccept = dlsym(RTLD_NEXT, "accept");
reallisten = dlsym(RTLD_NEXT, "listen");
realsocket = dlsym(RTLD_NEXT, "socket");
realbind = dlsym(RTLD_NEXT, "bind");
realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
2015-10-14 17:47:35 +00:00
realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
realaccept4 = dlsym(RTLD_NEXT, "accept4");
realclone = dlsym(RTLD_NEXT, "clone");
2015-11-12 16:21:05 +00:00
realclose = dlsym(RTLD_NEXT, "close");
2015-10-21 23:27:17 +00:00
realsyscall = dlsym(RTLD_NEXT, "syscall");
2015-11-12 16:21:05 +00:00
realdup2 = dlsym(RTLD_NEXT, "dup2");
realdup3 = dlsym(RTLD_NEXT, "dup3");
2015-12-09 02:07:46 +00:00
realgetsockname = dlsym(RTLD_NEXT, "getsockname");
2015-10-14 17:47:35 +00:00
}
/* Private Function Prototypes */
static void _init(void) __attribute__ ((constructor));
static void _init(void) { set_up_intercept(); }
2015-10-14 17:47:35 +00:00
/* get symbols and initialize mutexes */
static void set_up_intercept()
2015-10-14 17:47:35 +00:00
{
2016-01-11 18:12:59 +00:00
network_pathname = getenv("ZT_NC_NETWORK");
dwr(MSG_DEBUG,"Connecting to service at: %s\n", network_pathname);
if (!getenv("ZT_NC_NETWORK"))
return;
/* Hook/intercept Posix net API symbols */
2016-01-11 18:12:59 +00:00
rpc_mutex_init();
2015-10-14 17:47:35 +00:00
load_symbols();
}
/*------------------------------------------------------------------------------
--------------------------------- setsockopt() ---------------------------------
------------------------------------------------------------------------------*/
2015-10-14 17:47:35 +00:00
/* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
int setsockopt(SETSOCKOPT_SIG)
{
if(realsetsockopt == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"setsockopt(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
/* return(realsetsockopt(socket, level, option_name, option_value, option_len)); */
if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
return 0;
if(level == SOL_IP && option_name == IP_TTL)
return 0;
if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
2015-10-21 23:27:17 +00:00
return 0;
2015-10-14 17:47:35 +00:00
/* make sure we don't touch any standard outputs */
if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
return(realsetsockopt(socket, level, option_name, option_value, option_len));
int err = realsetsockopt(socket, level, option_name, option_value, option_len);
2016-01-11 18:12:59 +00:00
if(err < 0)
perror("setsockopt():\n");
2015-10-14 17:47:35 +00:00
return 0;
}
/*------------------------------------------------------------------------------
--------------------------------- getsockopt() ---------------------------------
------------------------------------------------------------------------------*/
/* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
2015-10-14 17:47:35 +00:00
int getsockopt(GETSOCKOPT_SIG)
{
if(realgetsockopt == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"getsockopt(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
2016-01-11 18:12:59 +00:00
if(!connected_to_service(sockfd)) {
return realgetsockopt(sockfd, level, optname, optval, optlen);
}
2015-12-21 13:03:26 +00:00
if(optname == SO_TYPE) {
2015-10-14 17:47:35 +00:00
int* val = (int*)optval;
*val = 2;
optval = (void*)val;
}
return 0;
}
/*------------------------------------------------------------------------------
----------------------------------- socket() -----------------------------------
------------------------------------------------------------------------------*/
/* int socket_family, int socket_type, int protocol
socket() intercept function */
int socket(SOCKET_SIG)
2015-12-21 13:03:26 +00:00
{
if(realsocket == NULL)
set_up_intercept();
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"socket():\n");
2015-10-14 17:47:35 +00:00
/* Check that type makes sense */
int flags = socket_type & ~SOCK_TYPE_MASK;
2015-10-14 19:46:22 +00:00
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
errno = EINVAL;
return -1;
}
2015-10-14 17:47:35 +00:00
socket_type &= SOCK_TYPE_MASK;
/* Check protocol is in range */
2015-10-14 19:46:22 +00:00
if (socket_family < 0 || socket_family >= NPROTO){
errno = EAFNOSUPPORT;
return -1;
}
if (socket_type < 0 || socket_type >= SOCK_MAX) {
errno = EINVAL;
return -1;
}
/* TODO: detect ENFILE condition */
2015-10-14 17:47:35 +00:00
if(socket_family == AF_LOCAL
|| socket_family == AF_NETLINK
|| socket_family == AF_UNIX) {
2015-10-28 00:12:32 +00:00
int err = realsocket(socket_family, socket_type, protocol);
2015-12-21 13:03:26 +00:00
dwr(MSG_DEBUG,"realsocket() = %d\n", err);
2015-10-28 00:12:32 +00:00
return err;
2015-10-14 17:47:35 +00:00
}
2015-10-21 23:27:17 +00:00
/* Assemble and send RPC */
2015-10-14 17:47:35 +00:00
struct socket_st rpc_st;
rpc_st.socket_family = socket_family;
rpc_st.socket_type = socket_type;
rpc_st.protocol = protocol;
rpc_st.__tid = syscall(SYS_gettid);
2016-01-11 18:12:59 +00:00
/* -1 is passed since we we're generating the new socket in this call */
return rpc_send_command(RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
2015-10-14 17:47:35 +00:00
}
/*------------------------------------------------------------------------------
---------------------------------- connect() -----------------------------------
------------------------------------------------------------------------------*/
/* int __fd, const struct sockaddr * __addr, socklen_t __len
connect() intercept function */
int connect(CONNECT_SIG)
{
if(realconnect == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"connect(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"connect(%d):\n", __fd);
2015-10-14 17:47:35 +00:00
struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *) __addr;
/* Check that this is a valid fd */
if(fcntl(__fd, F_GETFD) < 0) {
errno = EBADF;
2015-10-28 00:12:32 +00:00
return -1;
2015-10-14 17:47:35 +00:00
}
/* Check that it is a socket */
int sock_type;
socklen_t sock_type_len = sizeof(sock_type);
if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
errno = ENOTSOCK;
return -1;
}
/* Check family */
if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
errno = EAFNOSUPPORT;
return -1;
}
/* make sure we don't touch any standard outputs */
2015-12-21 13:03:26 +00:00
if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO)
2015-10-14 17:47:35 +00:00
return(realconnect(__fd, __addr, __len));
if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
|| connaddr->sin_family == PF_NETLINK
|| connaddr->sin_family == AF_NETLINK
|| connaddr->sin_family == AF_UNIX)) {
int err = realconnect(__fd, __addr, __len);
return err;
}
2015-10-21 23:27:17 +00:00
/* Assemble and send RPC */
2015-10-14 17:47:35 +00:00
struct connect_st rpc_st;
rpc_st.__tid = syscall(SYS_gettid);
rpc_st.__fd = __fd;
memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr_storage));
2015-10-14 17:47:35 +00:00
memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
2016-01-11 18:12:59 +00:00
return rpc_send_command(RPC_CONNECT, __fd, &rpc_st, sizeof(struct connect_st));
2015-10-14 17:47:35 +00:00
}
/*------------------------------------------------------------------------------
------------------------------------ bind() ------------------------------------
------------------------------------------------------------------------------*/
/* int sockfd, const struct sockaddr *addr, socklen_t addrlen
bind() intercept function */
int bind(BIND_SIG)
{
if(realbind == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"bind(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
2015-10-14 17:47:35 +00:00
/* Check that this is a valid fd */
if(fcntl(sockfd, F_GETFD) < 0) {
errno = EBADF;
2015-10-28 00:12:32 +00:00
return -1;
2015-10-14 17:47:35 +00:00
}
/* Check that it is a socket */
2015-10-21 23:27:17 +00:00
int opt = -1;
socklen_t opt_len;
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
2015-10-14 17:47:35 +00:00
errno = ENOTSOCK;
return -1;
}
/* make sure we don't touch any standard outputs */
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
return(realbind(sockfd, addr, addrlen));
/* If local, just use normal syscall */
struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *)addr;
2015-10-14 17:47:35 +00:00
if(connaddr->sin_family == AF_LOCAL
2015-10-14 17:47:35 +00:00
|| connaddr->sin_family == AF_NETLINK
|| connaddr->sin_family == AF_UNIX) {
int err = realbind(sockfd, addr, addrlen);
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"realbind, err = %d\n", err);
return err;
2015-10-14 17:47:35 +00:00
}
int port = connaddr->sin_port;
int ip = connaddr->sin_addr.s_addr;
unsigned char d[4];
d[0] = ip & 0xFF;
d[1] = (ip >> 8) & 0xFF;
d[2] = (ip >> 16) & 0xFF;
d[3] = (ip >> 24) & 0xFF;
2016-01-11 18:12:59 +00:00
dwr(MSG_DEBUG,"bind(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
2015-10-21 23:27:17 +00:00
/* Assemble and send RPC */
2015-10-14 17:47:35 +00:00
struct bind_st rpc_st;
rpc_st.sockfd = sockfd;
rpc_st.__tid = syscall(SYS_gettid);
memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
2015-10-14 17:47:35 +00:00
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
2016-01-11 18:12:59 +00:00
return rpc_send_command(RPC_BIND, sockfd, &rpc_st, sizeof(struct bind_st));
2015-10-14 17:47:35 +00:00
}
/*------------------------------------------------------------------------------
----------------------------------- accept4() ----------------------------------
------------------------------------------------------------------------------*/
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
int accept4(ACCEPT4_SIG)
{
if(realaccept4 == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"accept4(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
2015-12-18 22:26:31 +00:00
if ((flags & SOCK_CLOEXEC))
fcntl(sockfd, F_SETFL, FD_CLOEXEC);
if ((flags & SOCK_NONBLOCK))
fcntl(sockfd, F_SETFL, O_NONBLOCK);
2016-01-11 18:12:59 +00:00
return accept(sockfd, addr, addrlen);
2015-10-14 17:47:35 +00:00
}
/*------------------------------------------------------------------------------
----------------------------------- accept() -----------------------------------
------------------------------------------------------------------------------*/
/* int sockfd struct sockaddr *addr, socklen_t *addrlen
accept() intercept function */
int accept(ACCEPT_SIG)
{
if(realaccept == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"accept(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
2015-10-14 17:47:35 +00:00
/* Check that this is a valid fd */
if(fcntl(sockfd, F_GETFD) < 0) {
return -1;
errno = EBADF;
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"EBADF\n");
2015-10-28 00:12:32 +00:00
return -1;
2015-10-14 17:47:35 +00:00
}
/* Check that it is a socket */
2015-10-21 23:27:17 +00:00
int opt;
socklen_t opt_len;
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
2015-10-14 17:47:35 +00:00
errno = ENOTSOCK;
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"ENOTSOCK\n");
2015-10-14 17:47:35 +00:00
return -1;
}
/* Check that this socket supports accept() */
2015-10-21 23:27:17 +00:00
if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
2015-10-14 17:47:35 +00:00
errno = EOPNOTSUPP;
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"EOPNOTSUPP\n");
2015-10-14 17:47:35 +00:00
return -1;
}
/* Check that we haven't hit the soft-limit file descriptors allowed */
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
if(sockfd >= rl.rlim_cur){
errno = EMFILE;
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"EMFILE\n");
2015-10-14 17:47:35 +00:00
return -1;
}
2015-10-16 23:30:01 +00:00
/* Check address length */
if(addrlen < 0) {
errno = EINVAL;
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"EINVAL\n");
2015-10-16 23:30:01 +00:00
return -1;
}
2015-10-21 23:27:17 +00:00
/* redirect calls for standard I/O descriptors to kernel */
2015-10-28 00:12:32 +00:00
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"realaccept():\n");
2015-10-14 17:47:35 +00:00
return(realaccept(sockfd, addr, addrlen));
2015-10-28 00:12:32 +00:00
}
2015-10-21 23:27:17 +00:00
if(addr)
addr->sa_family = AF_INET;
2015-10-14 17:47:35 +00:00
2015-12-08 15:47:20 +00:00
/* The following line is required for libuv/nodejs to accept connections properly,
2015-12-09 02:07:46 +00:00
however, this has the side effect of causing certain webservers to max out the CPU
2015-12-08 15:47:20 +00:00
in an accept loop */
2015-12-18 22:26:31 +00:00
//fcntl(sockfd, F_SETFL, SOCK_NONBLOCK);
2016-01-11 18:12:59 +00:00
int new_fd = get_new_fd(sockfd);
if(new_fd > 0) {
2015-12-21 13:03:26 +00:00
errno = ERR_OK;
2016-01-11 18:12:59 +00:00
return new_fd;
2015-10-14 17:47:35 +00:00
}
2015-12-21 13:03:26 +00:00
errno = EAGAIN;
2015-11-23 09:14:17 +00:00
return -EAGAIN;
2015-10-14 17:47:35 +00:00
}
/*------------------------------------------------------------------------------
------------------------------------- listen()----------------------------------
------------------------------------------------------------------------------*/
/* int sockfd, int backlog */
2015-10-14 17:47:35 +00:00
int listen(LISTEN_SIG)
{
if(reallisten == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"listen(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-23 13:59:05 +00:00
dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
int sock_type;
socklen_t sock_type_len = sizeof(sock_type);
2015-10-14 17:47:35 +00:00
/* Check that this is a valid fd */
if(fcntl(sockfd, F_GETFD) < 0) {
errno = EBADF;
2015-10-28 00:12:32 +00:00
return -1;
2015-10-14 17:47:35 +00:00
}
/* Check that it is a socket */
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
errno = ENOTSOCK;
return -1;
}
/* Check that this socket supports accept() */
if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
errno = EOPNOTSUPP;
return -1;
}
/* make sure we don't touch any standard outputs */
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
return(reallisten(sockfd, backlog));
2016-01-11 18:12:59 +00:00
if(!connected_to_service(sockfd)) {
reallisten(sockfd, backlog);
}
2015-10-21 23:27:17 +00:00
/* Assemble and send RPC */
2015-10-14 17:47:35 +00:00
struct listen_st rpc_st;
rpc_st.sockfd = sockfd;
rpc_st.backlog = backlog;
rpc_st.__tid = syscall(SYS_gettid);
2016-01-11 18:12:59 +00:00
return rpc_send_command(RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st));
2015-10-14 17:47:35 +00:00
}
2015-10-21 23:27:17 +00:00
2015-10-28 00:12:32 +00:00
/*------------------------------------------------------------------------------
-------------------------------------- clone() ---------------------------------
2015-10-28 00:12:32 +00:00
------------------------------------------------------------------------------*/
/* int (*fn)(void *), void *child_stack, int flags, void *arg, ... */
2015-10-28 00:12:32 +00:00
int clone(CLONE_SIG)
{
if(realclone == NULL){
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"clone(): SYMBOL NOT FOUND.\n");
return -1;
}
2015-11-12 22:32:27 +00:00
dwr(MSG_DEBUG,"clone()\n");
int err = realclone(fn, child_stack, flags, arg);
2016-01-11 18:12:59 +00:00
set_up_intercept();
return err;
2015-10-28 00:12:32 +00:00
}
2015-11-12 16:21:05 +00:00
/*------------------------------------------------------------------------------
------------------------------------- close() ----------------------------------
2015-11-12 16:21:05 +00:00
------------------------------------------------------------------------------*/
/* int fd */
2015-11-12 16:21:05 +00:00
int close(CLOSE_SIG)
{
2015-12-10 17:24:56 +00:00
dwr(MSG_DEBUG, "close(%d)\n", fd);
2015-12-21 13:03:26 +00:00
if(realclose == NULL)
2016-01-11 18:12:59 +00:00
set_up_intercept();
return realclose(fd);
2015-11-12 16:21:05 +00:00
}
2015-12-09 02:07:46 +00:00
/*------------------------------------------------------------------------------
-------------------------------- getsockname() ---------------------------------
2015-12-09 02:07:46 +00:00
------------------------------------------------------------------------------*/
2016-01-11 18:12:59 +00:00
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen */
2015-12-09 02:07:46 +00:00
int getsockname(GETSOCKNAME_SIG)
{
if (realgetsockname == NULL) {
2016-01-11 18:12:59 +00:00
dwr(MSG_ERROR,"getsockname(): SYMBOL NOT FOUND. \n");
2015-12-09 02:07:46 +00:00
return -1;
}
2016-01-11 18:12:59 +00:00
dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd);
if(connected_to_service(sockfd) == 0) {
dwr(MSG_DEBUG,"getsockname(): not used by service\n");
2015-12-18 22:26:31 +00:00
return realgetsockname(sockfd, addr, addrlen);
2016-01-11 18:12:59 +00:00
}
/* This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in
* and is an IPv4 address. */
/* assemble and send command */
2015-12-09 20:20:38 +00:00
struct getsockname_st rpc_st;
rpc_st.sockfd = sockfd;
memcpy(&rpc_st.addr, addr, *addrlen);
2015-12-09 20:20:38 +00:00
memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
2016-01-11 18:12:59 +00:00
int rpcfd = rpc_send_command(RPC_GETSOCKNAME, sockfd, &rpc_st, sizeof(struct getsockname_st));
/* read address info from service */
char addrbuf[sizeof(struct sockaddr_storage)];
2015-12-18 18:31:11 +00:00
memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
2016-01-11 18:12:59 +00:00
if(rpcfd > -1)
if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
close(rpcfd);
struct sockaddr_storage sock_storage;
2015-12-18 18:31:11 +00:00
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
*addrlen = sizeof(struct sockaddr_in);
2015-12-18 18:31:11 +00:00
memcpy(addr, &sock_storage, (*addrlen > sizeof(sock_storage)) ? sizeof(sock_storage) : *addrlen);
addr->sa_family = AF_INET;
2015-12-09 20:20:38 +00:00
return 0;
2015-12-09 02:07:46 +00:00
}
2015-11-12 16:21:05 +00:00
2015-10-21 23:27:17 +00:00
/*------------------------------------------------------------------------------
------------------------------------ syscall() ---------------------------------
2015-10-21 23:27:17 +00:00
------------------------------------------------------------------------------*/
long syscall(SYSCALL_SIG){
2016-01-11 18:12:59 +00:00
dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
2015-10-21 23:27:17 +00:00
va_list ap;
uintptr_t a,b,c,d,e,f;
va_start(ap, number);
a=va_arg(ap, uintptr_t);
b=va_arg(ap, uintptr_t);
c=va_arg(ap, uintptr_t);
d=va_arg(ap, uintptr_t);
e=va_arg(ap, uintptr_t);
f=va_arg(ap, uintptr_t);
va_end(ap);
2015-12-21 13:03:26 +00:00
if(realsyscall == NULL)
return -1;
2015-10-21 23:27:17 +00:00
#if defined(__i386__)
/* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
args[0] = (unsigned long) fd;
args[1] = (unsigned long) addr;
args[2] = (unsigned long) addrlen;
args[3] = (unsigned long) flags;
*/
#else
if(number == __NR_accept4) {
int sockfd = a;
struct sockaddr * addr = (struct sockaddr*)b;
socklen_t * addrlen = (socklen_t*)c;
int flags = d;
int old_errno = errno;
int err = accept4(sockfd, addr, addrlen, flags);
errno = old_errno;
2015-12-21 13:03:26 +00:00
err = err == -EBADF ? -EAGAIN : err;
return err;
2015-10-21 23:27:17 +00:00
}
#endif
return realsyscall(number,a,b,c,d,e,f);
}