mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-02-23 02:32:37 +00:00
Get rid of constructor/destructor in libzerotierintercept, and simplify a few things.
This commit is contained in:
parent
ba9fcb31d0
commit
c6571073fe
@ -58,15 +58,11 @@
|
|||||||
#include "RPC.h"
|
#include "RPC.h"
|
||||||
#include "common.inc.c"
|
#include "common.inc.c"
|
||||||
|
|
||||||
static void load_symbols(void);
|
|
||||||
static void set_up_intercept();
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
/*------------------------------------------------------------------------------
|
||||||
------------------- Intercept<--->Service Comm mechanisms ----------------------
|
------------------- Intercept<--->Service Comm mechanisms ----------------------
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
static int thispid = -1;
|
static char *network_pathname = (char *)0;
|
||||||
char *network_pathname;
|
|
||||||
|
|
||||||
/* Check whether the socket is mapped to the service or not. We
|
/* 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
|
need to know if this is a regular AF_LOCAL socket or an end of a socketpair
|
||||||
@ -87,59 +83,48 @@ static int connected_to_service(int sockfd)
|
|||||||
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Yes, %s\n", addr_un->sun_path);
|
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Yes, %s\n", addr_un->sun_path);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Not connected to service\n");
|
dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Not connected to service\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*static void my_dest(void) __attribute__ ((destructor));
|
||||||
/*------------------------------------------------------------------------------
|
|
||||||
------------------------ ctors and dtors (and friends) ------------------------
|
|
||||||
------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
static void my_dest(void) __attribute__ ((destructor));
|
|
||||||
static void my_dest(void) {
|
static void my_dest(void) {
|
||||||
dwr(MSG_DEBUG,"closing connections to service...\n");
|
dwr(MSG_DEBUG,"closing connections to service...\n");
|
||||||
rpc_mutex_destroy();
|
rpc_mutex_destroy();
|
||||||
}
|
}*/
|
||||||
|
|
||||||
static void load_symbols(void)
|
|
||||||
{
|
|
||||||
if(thispid == getpid()) {
|
|
||||||
dwr(MSG_DEBUG,"detected duplicate call to global constructor (pid=%d).\n", thispid);
|
|
||||||
}
|
|
||||||
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");
|
|
||||||
realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
|
|
||||||
realaccept4 = dlsym(RTLD_NEXT, "accept4");
|
|
||||||
realclone = dlsym(RTLD_NEXT, "clone");
|
|
||||||
realclose = dlsym(RTLD_NEXT, "close");
|
|
||||||
realsyscall = dlsym(RTLD_NEXT, "syscall");
|
|
||||||
realdup2 = dlsym(RTLD_NEXT, "dup2");
|
|
||||||
realdup3 = dlsym(RTLD_NEXT, "dup3");
|
|
||||||
realgetsockname = dlsym(RTLD_NEXT, "getsockname");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Private Function Prototypes */
|
/* Private Function Prototypes */
|
||||||
static void _init(void) __attribute__ ((constructor));
|
/*static void _init(void) __attribute__ ((constructor));
|
||||||
static void _init(void) { set_up_intercept(); }
|
static void _init(void) { set_up_intercept(); } */
|
||||||
|
|
||||||
/* get symbols and initialize mutexes */
|
/* get symbols and initialize mutexes */
|
||||||
static void set_up_intercept()
|
static int set_up_intercept()
|
||||||
{
|
{
|
||||||
network_pathname = getenv("ZT_NC_NETWORK");
|
if (!realconnect) {
|
||||||
dwr(MSG_DEBUG,"Connecting to service at: %s\n", network_pathname);
|
realconnect = dlsym(RTLD_NEXT, "connect");
|
||||||
if (!getenv("ZT_NC_NETWORK"))
|
realbind = dlsym(RTLD_NEXT, "bind");
|
||||||
return;
|
realaccept = dlsym(RTLD_NEXT, "accept");
|
||||||
/* Hook/intercept Posix net API symbols */
|
reallisten = dlsym(RTLD_NEXT, "listen");
|
||||||
rpc_mutex_init();
|
realsocket = dlsym(RTLD_NEXT, "socket");
|
||||||
load_symbols();
|
realbind = dlsym(RTLD_NEXT, "bind");
|
||||||
|
realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
|
||||||
|
realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
|
||||||
|
realaccept4 = dlsym(RTLD_NEXT, "accept4");
|
||||||
|
realclose = dlsym(RTLD_NEXT, "close");
|
||||||
|
realsyscall = dlsym(RTLD_NEXT, "syscall");
|
||||||
|
realgetsockname = dlsym(RTLD_NEXT, "getsockname");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!network_pathname) {
|
||||||
|
network_pathname = getenv("ZT_NC_NETWORK");
|
||||||
|
if (!network_pathname)
|
||||||
|
return 0;
|
||||||
|
dwr(MSG_DEBUG,"Connecting to service at: %s\n", network_pathname);
|
||||||
|
/* Hook/intercept Posix net API symbols */
|
||||||
|
rpc_mutex_init();
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
/*------------------------------------------------------------------------------
|
||||||
@ -149,10 +134,9 @@ static void set_up_intercept()
|
|||||||
/* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
|
/* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
|
||||||
int setsockopt(SETSOCKOPT_SIG)
|
int setsockopt(SETSOCKOPT_SIG)
|
||||||
{
|
{
|
||||||
if(realsetsockopt == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"setsockopt(): SYMBOL NOT FOUND.\n");
|
return realsetsockopt(socket, level, option_name, option_value, option_len);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
|
dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
|
||||||
/* return(realsetsockopt(socket, level, option_name, option_value, option_len)); */
|
/* return(realsetsockopt(socket, level, option_name, option_value, option_len)); */
|
||||||
if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
|
if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
|
||||||
@ -177,10 +161,9 @@ int setsockopt(SETSOCKOPT_SIG)
|
|||||||
/* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
|
/* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
|
||||||
int getsockopt(GETSOCKOPT_SIG)
|
int getsockopt(GETSOCKOPT_SIG)
|
||||||
{
|
{
|
||||||
if(realgetsockopt == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"getsockopt(): SYMBOL NOT FOUND.\n");
|
return realgetsockopt(sockfd, level, optname, optval, optlen);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
|
dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
|
||||||
if(!connected_to_service(sockfd)) {
|
if(!connected_to_service(sockfd)) {
|
||||||
return realgetsockopt(sockfd, level, optname, optval, optlen);
|
return realgetsockopt(sockfd, level, optname, optval, optlen);
|
||||||
@ -200,9 +183,10 @@ int getsockopt(GETSOCKOPT_SIG)
|
|||||||
/* int socket_family, int socket_type, int protocol
|
/* int socket_family, int socket_type, int protocol
|
||||||
socket() intercept function */
|
socket() intercept function */
|
||||||
int socket(SOCKET_SIG)
|
int socket(SOCKET_SIG)
|
||||||
{
|
{
|
||||||
if(realsocket == NULL)
|
if (!set_up_intercept())
|
||||||
set_up_intercept();
|
return realsocket(socket_family, socket_type, protocol);
|
||||||
|
|
||||||
dwr(MSG_DEBUG,"socket():\n");
|
dwr(MSG_DEBUG,"socket():\n");
|
||||||
/* Check that type makes sense */
|
/* Check that type makes sense */
|
||||||
int flags = socket_type & ~SOCK_TYPE_MASK;
|
int flags = socket_type & ~SOCK_TYPE_MASK;
|
||||||
@ -246,10 +230,9 @@ int socket(SOCKET_SIG)
|
|||||||
connect() intercept function */
|
connect() intercept function */
|
||||||
int connect(CONNECT_SIG)
|
int connect(CONNECT_SIG)
|
||||||
{
|
{
|
||||||
if(realconnect == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"connect(): SYMBOL NOT FOUND.\n");
|
return realconnect(__fd, __addr, __len);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"connect(%d):\n", __fd);
|
dwr(MSG_DEBUG,"connect(%d):\n", __fd);
|
||||||
struct sockaddr_in *connaddr;
|
struct sockaddr_in *connaddr;
|
||||||
connaddr = (struct sockaddr_in *) __addr;
|
connaddr = (struct sockaddr_in *) __addr;
|
||||||
@ -298,10 +281,9 @@ int connect(CONNECT_SIG)
|
|||||||
bind() intercept function */
|
bind() intercept function */
|
||||||
int bind(BIND_SIG)
|
int bind(BIND_SIG)
|
||||||
{
|
{
|
||||||
if(realbind == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"bind(): SYMBOL NOT FOUND.\n");
|
return realbind(sockfd, addr, addrlen);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
|
dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
|
||||||
/* Check that this is a valid fd */
|
/* Check that this is a valid fd */
|
||||||
if(fcntl(sockfd, F_GETFD) < 0) {
|
if(fcntl(sockfd, F_GETFD) < 0) {
|
||||||
@ -353,10 +335,6 @@ int bind(BIND_SIG)
|
|||||||
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
|
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
|
||||||
int accept4(ACCEPT4_SIG)
|
int accept4(ACCEPT4_SIG)
|
||||||
{
|
{
|
||||||
if(realaccept4 == NULL){
|
|
||||||
dwr(MSG_ERROR,"accept4(): SYMBOL NOT FOUND.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
|
dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
|
||||||
if ((flags & SOCK_CLOEXEC))
|
if ((flags & SOCK_CLOEXEC))
|
||||||
fcntl(sockfd, F_SETFL, FD_CLOEXEC);
|
fcntl(sockfd, F_SETFL, FD_CLOEXEC);
|
||||||
@ -373,10 +351,9 @@ int accept4(ACCEPT4_SIG)
|
|||||||
accept() intercept function */
|
accept() intercept function */
|
||||||
int accept(ACCEPT_SIG)
|
int accept(ACCEPT_SIG)
|
||||||
{
|
{
|
||||||
if(realaccept == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"accept(): SYMBOL NOT FOUND.\n");
|
return realaccept(sockfd, addr, addrlen);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
|
dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
|
||||||
/* Check that this is a valid fd */
|
/* Check that this is a valid fd */
|
||||||
if(fcntl(sockfd, F_GETFD) < 0) {
|
if(fcntl(sockfd, F_GETFD) < 0) {
|
||||||
@ -441,10 +418,9 @@ int accept(ACCEPT_SIG)
|
|||||||
/* int sockfd, int backlog */
|
/* int sockfd, int backlog */
|
||||||
int listen(LISTEN_SIG)
|
int listen(LISTEN_SIG)
|
||||||
{
|
{
|
||||||
if(reallisten == NULL){
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"listen(): SYMBOL NOT FOUND.\n");
|
return(reallisten(sockfd, backlog));
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
|
dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
|
||||||
int sock_type;
|
int sock_type;
|
||||||
socklen_t sock_type_len = sizeof(sock_type);
|
socklen_t sock_type_len = sizeof(sock_type);
|
||||||
@ -479,23 +455,6 @@ int listen(LISTEN_SIG)
|
|||||||
return rpc_send_command(RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st));
|
return rpc_send_command(RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
|
||||||
-------------------------------------- clone() ---------------------------------
|
|
||||||
------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
/* int (*fn)(void *), void *child_stack, int flags, void *arg, ... */
|
|
||||||
int clone(CLONE_SIG)
|
|
||||||
{
|
|
||||||
if(realclone == NULL){
|
|
||||||
dwr(MSG_ERROR,"clone(): SYMBOL NOT FOUND.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"clone()\n");
|
|
||||||
int err = realclone(fn, child_stack, flags, arg);
|
|
||||||
set_up_intercept();
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------------------
|
/*------------------------------------------------------------------------------
|
||||||
------------------------------------- close() ----------------------------------
|
------------------------------------- close() ----------------------------------
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
@ -504,8 +463,7 @@ int clone(CLONE_SIG)
|
|||||||
int close(CLOSE_SIG)
|
int close(CLOSE_SIG)
|
||||||
{
|
{
|
||||||
dwr(MSG_DEBUG, "close(%d)\n", fd);
|
dwr(MSG_DEBUG, "close(%d)\n", fd);
|
||||||
if(realclose == NULL)
|
set_up_intercept();
|
||||||
set_up_intercept();
|
|
||||||
return realclose(fd);
|
return realclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -516,10 +474,9 @@ int close(CLOSE_SIG)
|
|||||||
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen */
|
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen */
|
||||||
int getsockname(GETSOCKNAME_SIG)
|
int getsockname(GETSOCKNAME_SIG)
|
||||||
{
|
{
|
||||||
if (realgetsockname == NULL) {
|
if (!set_up_intercept())
|
||||||
dwr(MSG_ERROR,"getsockname(): SYMBOL NOT FOUND. \n");
|
return realgetsockname(sockfd, addr, addrlen);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd);
|
dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd);
|
||||||
if(connected_to_service(sockfd) == 0) {
|
if(connected_to_service(sockfd) == 0) {
|
||||||
dwr(MSG_DEBUG,"getsockname(): not used by service\n");
|
dwr(MSG_DEBUG,"getsockname(): not used by service\n");
|
||||||
@ -537,11 +494,11 @@ int getsockname(GETSOCKNAME_SIG)
|
|||||||
/* read address info from service */
|
/* read address info from service */
|
||||||
char addrbuf[sizeof(struct sockaddr_storage)];
|
char addrbuf[sizeof(struct sockaddr_storage)];
|
||||||
memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
|
memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
|
||||||
|
|
||||||
if(rpcfd > -1)
|
if(rpcfd > -1)
|
||||||
if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
|
if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
|
||||||
close(rpcfd);
|
close(rpcfd);
|
||||||
|
|
||||||
struct sockaddr_storage sock_storage;
|
struct sockaddr_storage sock_storage;
|
||||||
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
|
memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
|
||||||
*addrlen = sizeof(struct sockaddr_in);
|
*addrlen = sizeof(struct sockaddr_in);
|
||||||
@ -554,8 +511,8 @@ int getsockname(GETSOCKNAME_SIG)
|
|||||||
------------------------------------ syscall() ---------------------------------
|
------------------------------------ syscall() ---------------------------------
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
long syscall(SYSCALL_SIG){
|
long syscall(SYSCALL_SIG)
|
||||||
dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
uintptr_t a,b,c,d,e,f;
|
uintptr_t a,b,c,d,e,f;
|
||||||
va_start(ap, number);
|
va_start(ap, number);
|
||||||
@ -567,8 +524,10 @@ long syscall(SYSCALL_SIG){
|
|||||||
f=va_arg(ap, uintptr_t);
|
f=va_arg(ap, uintptr_t);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
if(realsyscall == NULL)
|
if (!set_up_intercept())
|
||||||
return -1;
|
return realsyscall(number,a,b,c,d,e,f);
|
||||||
|
|
||||||
|
dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
|
||||||
|
|
||||||
#if defined(__i386__)
|
#if defined(__i386__)
|
||||||
/* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
|
/* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
|
||||||
|
@ -69,19 +69,16 @@ int dup2(DUP2_SIG);
|
|||||||
int dup3(DUP3_SIG);
|
int dup3(DUP3_SIG);
|
||||||
int getsockname(GETSOCKNAME_SIG);
|
int getsockname(GETSOCKNAME_SIG);
|
||||||
|
|
||||||
static int (*realconnect)(CONNECT_SIG);
|
static int (*realconnect)(CONNECT_SIG) = 0;
|
||||||
static int (*realbind)(BIND_SIG);
|
static int (*realbind)(BIND_SIG) = 0;
|
||||||
static int (*realaccept)(ACCEPT_SIG);
|
static int (*realaccept)(ACCEPT_SIG) = 0;
|
||||||
static int (*reallisten)(LISTEN_SIG);
|
static int (*reallisten)(LISTEN_SIG) = 0;
|
||||||
static int (*realsocket)(SOCKET_SIG);
|
static int (*realsocket)(SOCKET_SIG) = 0;
|
||||||
static int (*realsetsockopt)(SETSOCKOPT_SIG);
|
static int (*realsetsockopt)(SETSOCKOPT_SIG) = 0;
|
||||||
static int (*realgetsockopt)(GETSOCKOPT_SIG);
|
static int (*realgetsockopt)(GETSOCKOPT_SIG) = 0;
|
||||||
static int (*realaccept4)(ACCEPT4_SIG);
|
static int (*realaccept4)(ACCEPT4_SIG) = 0;
|
||||||
static long (*realsyscall)(SYSCALL_SIG);
|
static long (*realsyscall)(SYSCALL_SIG) = 0;
|
||||||
static int (*realclose)(CLOSE_SIG);
|
static int (*realclose)(CLOSE_SIG) = 0;
|
||||||
static int (*realclone)(CLONE_SIG);
|
static int (*realgetsockname)(GETSOCKNAME_SIG) = 0;
|
||||||
static int (*realdup2)(DUP2_SIG);
|
|
||||||
static int (*realdup3)(DUP3_SIG);
|
|
||||||
static int (*realgetsockname)(GETSOCKNAME_SIG);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user