Streamlined debug output code

This commit is contained in:
Joseph Henry 2015-11-12 14:32:27 -08:00
parent e5fad005a9
commit d1a22a0878
10 changed files with 211 additions and 288 deletions

View File

@ -29,7 +29,8 @@ endif
UNAME_M=$(shell uname -m)
INCLUDES=-Iext/lwip/src/include -Iext/lwip/src/include/ipv4 -Iext/lwip/src/include/ipv6
DEFS=-DZT_ENABLE_NETCON
DEFS=-DZT_ENABLE_NETCON #-DVERBOSE
CXXFLAGS+=-Wc++11-compat-deprecated-writable-strings
LDLIBS?=
include objects.mk

View File

@ -37,14 +37,15 @@
#include <netinet/in.h>
#include <pthread.h>
#define DEBUG_LEVEL 5
#define MSG_ERROR 0 // Errors
#define MSG_INFO 1 // Information which is generally useful to any user
#define MSG_DEBUG 2 // Information which is only useful to someone debugging
#include "Common.h"
void dwr(const char *fmt, ...);
/* defined in intercept and service */
//extern FILE* logfile;
//extern char* logfilename;
//extern flog = -1;
#ifdef NETCON_INTERCEPT
extern pthread_mutex_t loglock;
@ -70,83 +71,38 @@ void print_addr(struct sockaddr *addr)
fprintf(stderr, "IP address: %s\n", s);
free(s);
}
#endif
#ifdef NETCON_SERVICE
void dwr(int, const char *fmt, ...);
void dwr(const char *fmt, ...)
{
int saveerr;
va_list ap;
va_start(ap, fmt);
saveerr = errno;
dwr(-1, fmt, ap);
errno = saveerr;
va_end(ap);
}
void dwr(int pid, const char *fmt, ...)
namespace ZeroTier {
#endif
#ifdef NETCON_INTERCEPT
void dwr(const char *fmt, ...)
#endif
{
va_list ap;
int saveerr;
char timestring[20];
time_t timestamp;
void dwr(int level, char *fmt, ... )
{
#ifdef NETCON_SERVICE
if(level > DEBUG_LEVEL)
return;
#endif
int saveerr;
saveerr = errno;
va_list ap;
va_start(ap, fmt);
timestamp = time(NULL);
strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
char timestring[20];
time_t timestamp;
timestamp = time(NULL);
strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
//if(logfile)
// fprintf(logfile, "%s ", timestring);
fprintf(stderr, "%s ", timestring);
pid_t pid = getpid();
#ifdef VERBOSE
fprintf(stderr, "%s [pid=%7d] ", timestring, pid);
#endif
vfprintf(stderr, fmt, ap);
fflush(stderr);
#ifdef NETCON_SERVICE
if(ns != NULL)
{
size_t num_intercepts = ns->intercepts.size();
size_t num_connections = ns->connections.size();
//if(logfile)
// fprintf(logfile, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
fprintf(stderr, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
}
else {
//if(logfile)
// fprintf(logfile, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
fprintf(stderr, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
errno = saveerr;
va_end(ap);
}
#endif
#ifdef NETCON_INTERCEPT
//pthread_mutex_lock(&loglock);
int pid = getpid();
//if(logfile)
// fprintf(logfile, "[tid=%7d]", pid);
fprintf(stderr, "[tid=%7d]", pid);
//pthread_mutex_unlock(&loglock);
#endif
//if(logfile)
// fputs(" ", logfile);
fputs(" ", stderr);
/* logfile */
va_start(ap, fmt);
saveerr = errno;
//if(logfile){
// vfprintf(logfile, fmt, ap);
// fflush(logfile);
//}
va_end(ap);
/* console */
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
errno = saveerr;
va_end(ap);
#ifdef NETCON_SERVICE
}
#endif

View File

@ -21,14 +21,10 @@
#include <signal.h>
#include <sys/socket.h>
/*
void set_log_options(int, char *, int);
void show_msg(int level, char *, ...);
unsigned int resolve_ip(char *, int, int);
#define MSGERR 0
#define MSGDEBUG 1
*/
#endif

View File

@ -56,7 +56,7 @@ char *progname = "";
#include <arpa/inet.h>
#include "Intercept.h"
#include "Common.h"
#include "Common.c"
#ifdef CHECKS
#include <sys/resource.h>
@ -110,7 +110,7 @@ int dup3(DUP3_SIG);
int connect_to_service(void);
int init_service_connection();
void dwr(const char *fmt, ...);
//void dwr(const char *fmt, ...);
void load_symbols(void);
void set_up_intercept();
int checkpid();
@ -128,12 +128,12 @@ void handle_error(char *name, char *info, int err)
{
#ifdef ERRORS_ARE_FATAL
if(err < 0) {
dwr("handle_error(%s)=%d: FATAL: %s\n", name, err, info);
dwr(MSG_DEBUG,"handle_error(%s)=%d: FATAL: %s\n", name, err, info);
//exit(-1);
}
#endif
#ifdef VERBOSE
dwr("%s()=%d\n", name, err);
dwr(MSG_DEBUG,"%s()=%d\n", name, err);
#endif
}
@ -167,29 +167,20 @@ int checkpid() {
*/
int send_command(int rpc_fd, char *cmd)
{
#ifdef DEBUG
/*
dwr(" - IDX_PID = %d\n", IDX_PID);
dwr(" - IDX_TID = %d\n", IDX_TID);
dwr(" - IDX_COUNT = %d\n", IDX_COUNT);
dwr(" - IDX_TIME = %d\n", IDX_TIME);
dwr(" - IDX_PAYLOAD = %d\n", IDX_PAYLOAD);
*/
/*
#ifdef DEBUG_RPC
/*
#define IDX_PID 0
#define IDX_TID sizeof(pid_t)
#define IDX_COUNT IDX_TID + sizeof(pid_t)
#define IDX_TIME IDX_COUNT + sizeof(int)
#define IDX_CMD IDX_TIME + 20 // 20 being the length of the timestamp string
#define IDX_PAYLOAD IDX_TIME + sizeof(char)
*/
*/
// [pid_t] [pid_t] [rpc_count] [int] [...]
char metabuf[BUF_SZ]; // portion of buffer which contains RPC meta-data for debugging
memset(metabuf, '\0', BUF_SZ);
pid_t pid = syscall(SYS_getpid);
pid_t tid = syscall(SYS_gettid);
int payload_idx = sizeof(pid_t)*2 + sizeof(rpc_count);
rpc_count++;
char timestring[20];
time_t timestamp;
@ -202,12 +193,11 @@ int send_command(int rpc_fd, char *cmd)
memcpy(&metabuf[IDX_TIME], &timestring, 20 ); // timestamp
#endif
// copy payload into final command buffer
int copied = BUF_SZ-IDX_PAYLOAD;
memcpy(&metabuf[IDX_PAYLOAD], cmd, 200);
//dwr(" RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestring, cmd[0]);
memcpy(&metabuf[IDX_PAYLOAD], cmd, PAYLOAD_SZ);
//dwr(MSG_DEBUG," RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestring, cmd[0]);
int n_write = write(rpc_fd, &metabuf, BUF_SZ);
if(n_write < 0){
dwr("Error writing command to service (CMD = %d)\n", cmd[0]);
dwr(MSG_DEBUG,"Error writing command to service (CMD = %d)\n", cmd[0]);
errno = 0;
return -1;
}
@ -219,7 +209,7 @@ int send_command(int rpc_fd, char *cmd)
*/
int get_retval()
{
dwr("get_retval()\n");
dwr(MSG_DEBUG,"get_retval()\n");
if(fdret_sock >= 0) {
int retval;
int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
@ -232,7 +222,7 @@ int get_retval()
return retval;
}
}
dwr("unable to read return value\n");
dwr(MSG_DEBUG,"unable to read return value\n");
return -1;
}
@ -242,7 +232,7 @@ that the service uses. We don't want to keep state in the intercept, so
we simply ask the service via an RPC */
int is_mapped_to_service(int sockfd)
{
dwr("is_mapped_to_service()\n");
dwr(MSG_DEBUG,"is_mapped_to_service()\n");
char cmd[BUF_SZ];
memset(cmd, '\0', BUF_SZ);
cmd[0] = RPC_MAP_REQ;
@ -262,7 +252,7 @@ int is_mapped_to_service(int sockfd)
/* Sets up the connection pipes and sockets to the service */
int init_service_connection()
{
dwr("init_service_connection()\n");
dwr(MSG_DEBUG,"init_service_connection()\n");
if(!is_initialized) {
struct sockaddr_un addr;
int tfd = -1, attempts = 0, conn_err = -1;
@ -277,14 +267,14 @@ int init_service_connection()
while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS) {
conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
if(conn_err < 0) {
dwr("re-attempting connection in %ds\n", 1+attempts);
dwr(MSG_DEBUG,"re-attempting connection in %ds\n", 1+attempts);
sleep(1);
}
else {
dwr("AF_UNIX connection established: %d\n", tfd);
dwr(MSG_DEBUG,"AF_UNIX connection established: %d\n", tfd);
is_initialized = 1;
int newtfd = realdup2(tfd, 1023);
dwr("dup'd to rpc_fd = %d\n", newtfd);
dwr(MSG_DEBUG,"dup'd to rpc_fd = %d\n", newtfd);
close(tfd);
return newtfd;
}
@ -300,7 +290,7 @@ int init_service_connection()
void my_dest(void) __attribute__ ((destructor));
void my_dest(void) {
//dwr("closing connections to service...\n");
//dwr(MSG_DEBUG,"closing connections to service...\n");
close(fdret_sock);
pthread_mutex_destroy(&lock);
}
@ -312,11 +302,12 @@ void load_symbols(void)
#endif
/* possibly add check to beginning of each method to avoid needing to cll the constructor */
if(thispid == getpid()) {
dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
dwr(MSG_DEBUG,"detected duplicate call to global ctor (pid=%d).\n", thispid);
}
//dwr(" -- pid = %d\n", getpid());
//dwr(" -- uid = %d\n", getuid());
//dwr(MSG_DEBUG," -- pid = %d\n", getpid());
//dwr(MSG_DEBUG," -- uid = %d\n", getuid());
thispid = getpid();
//thistid = gettid();
#ifndef USE_OLD_DLSYM
realconnect = dlsym(RTLD_NEXT, "connect");
@ -391,7 +382,7 @@ void set_up_intercept()
/* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
int setsockopt(SETSOCKOPT_SIG)
{
dwr("\n\nsetsockopt(%d)\n", socket);
dwr(MSG_DEBUG,"\n\nsetsockopt(%d)\n", socket);
/*
if(is_mapped_to_service(socket) < 0) { // First, check if the service manages this
return realsetsockopt(socket, level, option_name, option_value, option_len);
@ -422,7 +413,7 @@ int setsockopt(SETSOCKOPT_SIG)
/* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
int getsockopt(GETSOCKOPT_SIG)
{
dwr("\n\ngetsockopt(%d)\n", sockfd);
dwr(MSG_DEBUG,"\n\ngetsockopt(%d)\n", sockfd);
/*
if(is_mapped_to_service(sockfd) < 0) { // First, check if the service manages this
return realgetsockopt(sockfd, level, optname, optval, optlen);
@ -452,7 +443,7 @@ int getsockopt(GETSOCKOPT_SIG)
socket() intercept function */
int socket(SOCKET_SIG)
{
dwr("\n\nsocket()*:\n");
dwr(MSG_DEBUG,"\n\nsocket():\n");
int err;
#ifdef CHECKS
/* Check that type makes sense */
@ -488,7 +479,7 @@ int socket(SOCKET_SIG)
char cmd[BUF_SZ];
fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
if(fdret_sock < 0) {
dwr("BAD service connection. exiting.\n");
dwr(MSG_DEBUG,"BAD service connection. exiting.\n");
handle_error("socket4", "", -1);
exit(-1);
}
@ -496,7 +487,7 @@ int socket(SOCKET_SIG)
|| socket_family == AF_NETLINK
|| socket_family == AF_UNIX) {
int err = realsocket(socket_family, socket_type, protocol);
dwr("realsocket, err = %d\n", err);
dwr(MSG_DEBUG,"realsocket, err = %d\n", err);
handle_error("socket5", "", err);
return err;
}
@ -508,7 +499,7 @@ int socket(SOCKET_SIG)
rpc_st.__tid = syscall(SYS_gettid);
memset(cmd, '\0', BUF_SZ);
cmd[0] = RPC_SOCKET;
dwr("pid = %d\n", thispid);
dwr(MSG_DEBUG,"pid = %d\n", thispid);
memcpy(&cmd[1]+sizeof(pid_t), &rpc_st, sizeof(struct socket_st));
pthread_mutex_lock(&lock);
send_command(fdret_sock, cmd);
@ -516,11 +507,10 @@ int socket(SOCKET_SIG)
/* get new fd */
char rbuf[16];
ssize_t sz = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &newfd);
int tmp = newfd;
dwr("read %d bytes (%s)\n", sz, &rbuf);
dwr(MSG_DEBUG,"read %d bytes (%s)\n", sz, &rbuf);
if(sz > 0)
{
dwr("sending fd = %d to Service over (%d)\n", newfd, fdret_sock);
dwr(MSG_DEBUG,"sending fd = %d to Service over (%d)\n", newfd, fdret_sock);
/* send our local-fd number back to service so
it can complete its mapping table entry */
memset(cmd, '\0', BUF_SZ);
@ -535,7 +525,7 @@ int socket(SOCKET_SIG)
return newfd;
}
else { // Try to read retval+errno since we RXed a bad fd
dwr("Error, service sent bad fd.\n");
dwr(MSG_DEBUG,"Error, service sent bad fd.\n");
err = get_retval();
pthread_mutex_unlock(&lock);
handle_error("socket7", "", -1);
@ -544,7 +534,7 @@ int socket(SOCKET_SIG)
}
else {
dwr("Error while receiving new FD.\n");
dwr(MSG_DEBUG,"Error while receiving new FD.\n");
err = get_retval();
pthread_mutex_unlock(&lock);
handle_error("socket8", "", -1);
@ -560,7 +550,7 @@ int socket(SOCKET_SIG)
connect() intercept function */
int connect(CONNECT_SIG)
{
dwr("\n\nconnect(%d):\n", __fd);
dwr(MSG_DEBUG,"\n\nconnect(%d):\n", __fd);
print_addr(__addr);
struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *) __addr;
@ -641,7 +631,7 @@ int connect(CONNECT_SIG)
fd_set *exceptfds, struct timeval *timeout */
int select(SELECT_SIG)
{
//dwr("select():\n");
//dwr(MSG_DEBUG,"select():\n");
return realselect(n, readfds, writefds, exceptfds, timeout);
}
@ -653,7 +643,7 @@ int select(SELECT_SIG)
bind() intercept function */
int bind(BIND_SIG)
{
dwr("\n\nbind(%d):\n", sockfd);
dwr(MSG_DEBUG,"\n\nbind(%d):\n", sockfd);
print_addr(addr);
#ifdef CHECKS
/* Check that this is a valid fd */
@ -685,7 +675,7 @@ int bind(BIND_SIG)
|| connaddr->sin_family == AF_NETLINK
|| connaddr->sin_family == AF_UNIX) {
int err = realbind(sockfd, addr, addrlen);
dwr("realbind, err = %d\n", err);
dwr(MSG_DEBUG,"realbind, err = %d\n", err);
return err;
}
@ -716,7 +706,7 @@ int bind(BIND_SIG)
/* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
int accept4(ACCEPT4_SIG)
{
dwr("\n\naccept4(%d):\n", sockfd);
dwr(MSG_DEBUG,"\n\naccept4(%d):\n", sockfd);
#ifdef CHECKS
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
errno = EINVAL;
@ -743,13 +733,13 @@ int accept4(ACCEPT4_SIG)
accept() intercept function */
int accept(ACCEPT_SIG)
{
dwr("\n\naccept(%d):\n", sockfd);
dwr(MSG_DEBUG,"\n\naccept(%d):\n", sockfd);
#ifdef CHECKS
/* Check that this is a valid fd */
if(fcntl(sockfd, F_GETFD) < 0) {
return -1;
errno = EBADF;
dwr("EBADF\n");
dwr(MSG_DEBUG,"EBADF\n");
handle_error("accept", "EBADF", -1);
return -1;
}
@ -758,14 +748,14 @@ int accept(ACCEPT_SIG)
socklen_t opt_len;
if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
errno = ENOTSOCK;
dwr("ENOTSOCK\n");
dwr(MSG_DEBUG,"ENOTSOCK\n");
handle_error("accept", "ENOTSOCK", -1);
return -1;
}
/* Check that this socket supports accept() */
if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
errno = EOPNOTSUPP;
dwr("EOPNOTSUPP\n");
dwr(MSG_DEBUG,"EOPNOTSUPP\n");
handle_error("accept", "EOPNOTSUPP", -1);
return -1;
}
@ -774,14 +764,14 @@ int accept(ACCEPT_SIG)
getrlimit(RLIMIT_NOFILE, &rl);
if(sockfd >= rl.rlim_cur){
errno = EMFILE;
dwr("EMFILE\n");
dwr(MSG_DEBUG,"EMFILE\n");
handle_error("accept", "EMFILE", -1);
return -1;
}
/* Check address length */
if(addrlen < 0) {
errno = EINVAL;
dwr("EINVAL\n");
dwr(MSG_DEBUG,"EINVAL\n");
handle_error("accept", "EINVAL", -1);
return -1;
}
@ -789,7 +779,7 @@ int accept(ACCEPT_SIG)
/* redirect calls for standard I/O descriptors to kernel */
if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
dwr("realaccept():\n");
dwr(MSG_DEBUG,"realaccept():\n");
return(realaccept(sockfd, addr, addrlen));
}
@ -826,7 +816,7 @@ int accept(ACCEPT_SIG)
}
pthread_mutex_unlock(&lock);
errno = ERR_OK;
dwr("*accept()=%d\n", new_conn_socket);
dwr(MSG_DEBUG,"*accept()=%d\n", new_conn_socket);
handle_error("accept", "", new_conn_socket);
return new_conn_socket; // OK
}
@ -850,7 +840,7 @@ int accept(ACCEPT_SIG)
listen() intercept function */
int listen(LISTEN_SIG)
{
dwr("\n\nlisten(%d):\n", sockfd);
dwr(MSG_DEBUG,"\n\nlisten(%d):\n", sockfd);
int sock_type;
socklen_t sock_type_len = sizeof(sock_type);
@ -882,7 +872,7 @@ int listen(LISTEN_SIG)
if(is_mapped_to_service(sockfd) < 0) {
// We now know this socket is not one of our socketpairs
int err = reallisten(sockfd, backlog);
dwr("reallisten()=%d\n", err);
dwr(MSG_DEBUG,"reallisten()=%d\n", err);
return err;
}
@ -897,7 +887,7 @@ int listen(LISTEN_SIG)
memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
pthread_mutex_lock(&lock);
send_command(fdret_sock, cmd);
int err = get_retval();
/*int err = */get_retval();
pthread_mutex_unlock(&lock);
handle_error("listen", "", ERR_OK);
return ERR_OK;
@ -911,7 +901,7 @@ int listen(LISTEN_SIG)
/*
int clone(CLONE_SIG)
{
dwr("clone()\n");
dwr(MSG_DEBUG,"clone()\n");
return realclone(fn, child_stack, flags, arg);
}
*/
@ -925,7 +915,7 @@ int clone(CLONE_SIG)
/*
int poll(POLL_SIG)
{
dwr("poll()\n");
dwr(MSG_DEBUG,"poll()\n");
return realpoll(fds, nfds, timeout);
//return ERESTART_RESTARTBLOCK;
}
@ -938,12 +928,12 @@ int poll(POLL_SIG)
// int fd
int close(CLOSE_SIG)
{
dwr("close(%d)\n", fd);
dwr(MSG_DEBUG,"close(%d)\n", fd);
if(fd == fdret_sock)
return 0; // FIXME: Ignore request to shut down our rpc fd, this is *almost always* safe
if(fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO){
if(fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
return realclose(fd);
}
return -1;
}
/*------------------------------------------------------------------------------
@ -953,15 +943,15 @@ int close(CLOSE_SIG)
// int oldfd, int newfd
int dup2(DUP2_SIG)
{
dwr("dup2(%d, %d)\n", oldfd, newfd);
dwr(MSG_DEBUG,"dup2(%d, %d)\n", oldfd, newfd);
if(oldfd == fdret_sock) {
dwr("client application attempted to dup2 RPC socket (%d). This is not allowed.\n", oldfd);
dwr(MSG_DEBUG,"client application attempted to dup2 RPC socket (%d). This is not allowed.\n", oldfd);
errno = EBADF;
return -1;
}
if(oldfd != STDIN_FILENO && oldfd != STDOUT_FILENO && oldfd != STDERR_FILENO) {
if(oldfd != STDIN_FILENO && oldfd != STDOUT_FILENO && oldfd != STDERR_FILENO)
return realdup2(oldfd, newfd);
}
return -1;
}
/*------------------------------------------------------------------------------
@ -971,7 +961,7 @@ int dup2(DUP2_SIG)
// int oldfd, int newfd, int flags
int dup3(DUP3_SIG)
{
dwr("dup3(%d, %d, %d)\n", oldfd, newfd, flags);
dwr(MSG_DEBUG,"dup3(%d, %d, %d)\n", oldfd, newfd, flags);
#ifdef DEBUG
// Only do this check if we want to debug the intercept, otherwise, dont mess with
// the client application's logging methods
@ -990,7 +980,7 @@ int dup3(DUP3_SIG)
long syscall(SYSCALL_SIG)
{
dwr("syscall(%u, ...):\n", number);
dwr(MSG_DEBUG,"syscall(%u, ...):\n", number);
va_list ap;
uintptr_t a,b,c,d,e,f;

View File

@ -37,7 +37,9 @@
#define IDX_TIME IDX_COUNT + sizeof(int)
#define IDX_PAYLOAD IDX_TIME + 20 // 20 being the length of the timestamp string
#define BUF_SZ 256
#define BUF_SZ 256
#define PAYLOAD_SZ 223 // BUF_SZ-IDX_PAYLOAD
#define ERR_OK 0
/* Userland RPC codes */

View File

@ -48,6 +48,7 @@
#include "NetconService.hpp"
#include "Intercept.h"
#include "NetconUtilities.hpp"
#include "Common.c"
#define APPLICATION_POLL_FREQ 20
#define ZT_LWIP_TCP_TIMER_INTERVAL 5
@ -86,7 +87,7 @@ NetconEthernetTap::NetconEthernetTap(
lwipstack->lwip_init();
_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
dwr(" NetconEthernetTap(): RPC listening on: %d\n", _phy.getDescriptor(_unixListenSocket));
dwr(1, " NetconEthernetTap(): RPC listening on: %d\n", _phy.getDescriptor(_unixListenSocket));
if (!_unixListenSocket)
throw std::runtime_error(std::string("unable to bind to ")+sockPath);
_thread = Thread::start(this);
@ -186,7 +187,7 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
// First pbuf gets ethernet header at start
q = p;
if (q->len < sizeof(ethhdr)) {
dwr("_put(): Dropped packet: first pbuf smaller than ethernet header\n");
dwr(1, "_put(): Dropped packet: first pbuf smaller than ethernet header\n");
return;
}
memcpy(q->payload,&ethhdr,sizeof(ethhdr));
@ -199,14 +200,14 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
dataptr += q->len;
}
} else {
dwr("put(): Dropped packet: no pbufs available\n");
dwr(1, "put(): Dropped packet: no pbufs available\n");
return;
}
{
Mutex::Lock _l2(lwipstack->_lock);
if(interface.input(p, &interface) != ERR_OK) {
dwr("put(): Error while RXing packet (netif->input)\n");
dwr(1, "put(): Error while RXing packet (netif->input)\n");
}
}
}
@ -353,7 +354,7 @@ void NetconEthernetTap::closeConnection(TcpConnection *conn)
{
if(!conn)
return;
dwr(3, " closeConnection(%x, %d)\n", conn->pcb, _phy.getDescriptor(conn->dataSock));
dwr(MSG_DEBUG, " closeConnection(%x, %d)\n", conn->pcb, _phy.getDescriptor(conn->dataSock));
//lwipstack->_tcp_sent(conn->pcb, NULL);
//lwipstack->_tcp_recv(conn->pcb, NULL);
//lwipstack->_tcp_err(conn->pcb, NULL);
@ -416,7 +417,7 @@ void NetconEthernetTap::threadMain()
uint64_t prev_status_time = 0;
uint64_t prev_etharp_time = 0;
/*
fprintf(stderr, "- MEM_SIZE = %dM\n", MEM_SIZE / (1024*1024));
fprintf(stderr, "- PBUF_POOL_SIZE = %d\n", PBUF_POOL_SIZE);
fprintf(stderr, "- PBUF_POOL_BUFSIZE = %d\n", PBUF_POOL_BUFSIZE);
@ -435,7 +436,7 @@ void NetconEthernetTap::threadMain()
fprintf(stderr, "- ARP_TMR_INTERVAL = %d\n", ARP_TMR_INTERVAL);
fprintf(stderr, "- TCP_TMR_INTERVAL = %d\n", TCP_TMR_INTERVAL);
fprintf(stderr, "- IP_TMR_INTERVAL = %d\n", IP_TMR_INTERVAL);
*/
// Main timer loop
@ -467,7 +468,7 @@ void NetconEthernetTap::threadMain()
}
else {
// Here we should handle the case there there is incoming data (?)
dwr(3, " tap_thread(): Listening socketpair closed. Removing RPC connection (%d)\n",
dwr(MSG_DEBUG, " tap_thread(): Listening socketpair closed. Removing RPC connection (%d)\n",
_phy.getDescriptor(tcp_connections[i]->dataSock));
closeConnection(tcp_connections[i]);
}
@ -487,7 +488,7 @@ void NetconEthernetTap::threadMain()
unsigned char tmpbuf[BUF_SZ];
int n;
if((n = read(fd,&tmpbuf,BUF_SZ)) < 0) {
dwr(3, " tap_thread(): closing RPC (%d)\n", _phy.getDescriptor(rpc_sockets[i]));
dwr(MSG_DEBUG, " tap_thread(): closing RPC (%d)\n", _phy.getDescriptor(rpc_sockets[i]));
closeClient(rpc_sockets[i]);
}
// < 0 is failure
@ -495,7 +496,7 @@ void NetconEthernetTap::threadMain()
// > 0 RPC data read, handle it
else if (n > 0) {
// Handle RPC call, this is rare
dwr(3, " tap_thread(): RPC read during connection check (%d bytes)\n", n);
dwr(MSG_DEBUG, " tap_thread(): RPC read during connection check (%d bytes)\n", n);
phyOnUnixData(rpc_sockets[i],_phy.getuptr(rpc_sockets[i]),&tmpbuf,BUF_SZ);
}
}
@ -528,9 +529,8 @@ void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
{
dwr(3, " phyOnUnixClose(sock=0x%x, uptr=0x%x): fd = %d\n", sock, uptr, _phy.getDescriptor(sock));
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) {
dwr(MSG_DEBUG, " phyOnUnixClose(sock=0x%x, uptr=0x%x): fd = %d\n", sock, uptr, _phy.getDescriptor(sock));
TcpConnection *conn = (TcpConnection*)*uptr;
closeConnection(conn);
}
@ -551,7 +551,7 @@ void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,
}
}
else {
dwr(5, "phyOnFileDescriptorActivity(): PhySocket not readable\n");
dwr(MSG_ERROR, "phyOnFileDescriptorActivity(): PhySocket not readable\n");
}
}
@ -560,15 +560,12 @@ void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,
*/
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {
if(find(rpc_sockets.begin(), rpc_sockets.end(), sockN) != rpc_sockets.end()){
dwr(3, " phyOnUnixAccept(): SockN (0x%x) already exists!\n", sockN);
dwr(MSG_ERROR, " phyOnUnixAccept(): SockN (0x%x) already exists!\n", sockN);
return;
}
dwr(3, " phyOnUnixAccept(): push_back(0x%x)\n", sockN);
rpc_sockets.push_back(sockN);
}
void unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload);
/*
* Processes incoming data on a client-specific RPC connection
*/
@ -576,64 +573,59 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
{
pid_t pid, tid;
int rpc_count;
char timestamp[20];
char cmd;
char cmd, timestamp[20];
void *payload;
unload_rpc(data, pid, tid, rpc_count, timestamp, cmd, payload);
dwr("\n\nRX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestamp, cmd);
dwr(MSG_DEBUG, "\n\nRPC: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestamp, cmd);
unsigned char *buf = (unsigned char*)data;
switch(cmd)
{
case RPC_SOCKET:
dwr(2, "RPC_SOCKET\n");
struct socket_st socket_rpc;
memcpy(&socket_rpc, &buf[IDX_PAYLOAD+1], sizeof(struct socket_st));
dwr(MSG_DEBUG, "RPC_SOCKET\n");
struct socket_st socket_rpc;
memcpy(&socket_rpc, &buf[IDX_PAYLOAD+1], sizeof(struct socket_st));
if(rpc_count==rpc_counter) {
dwr("Detected repeat RPC.\n");
//return;
}
else {
rpc_counter = rpc_count;
}
if(rpc_count==rpc_counter) {
dwr(MSG_ERROR, "Detected repeat RPC.\n");
//return;
}
else {
rpc_counter = rpc_count;
}
TcpConnection * new_conn;
if((new_conn = handle_socket(sock, uptr, &socket_rpc))) {
pidmap[sock] = pid;
new_conn->pid = pid;
}
TcpConnection * new_conn;
if((new_conn = handle_socket(sock, uptr, &socket_rpc))) {
pidmap[sock] = pid;
new_conn->pid = pid;
}
break;
case RPC_LISTEN:
dwr(2, "RPC_LISTEN\n");
dwr(MSG_DEBUG, "RPC_LISTEN\n");
struct listen_st listen_rpc;
memcpy(&listen_rpc, &buf[IDX_PAYLOAD+1], sizeof(struct listen_st));
handle_listen(sock, uptr, &listen_rpc);
break;
case RPC_BIND:
dwr(2, "RPC_BIND\n");
dwr(MSG_DEBUG, "RPC_BIND\n");
struct bind_st bind_rpc;
memcpy(&bind_rpc, &buf[IDX_PAYLOAD+1], sizeof(struct bind_st));
handle_bind(sock, uptr, &bind_rpc);
break;
case RPC_CONNECT:
dwr(2, "RPC_CONNECT\n");
dwr(MSG_DEBUG, "RPC_CONNECT\n");
struct connect_st connect_rpc;
memcpy(&connect_rpc, &buf[IDX_PAYLOAD+1], sizeof(struct connect_st));
handle_connect(sock, uptr, &connect_rpc);
break;
case RPC_MAP:
dwr(2, "RPC_MAP (len = %d)\n", len);
dwr(MSG_DEBUG, "RPC_MAP (len = %d)\n", len);
int newfd;
//memcpy(&pid, &buf[IDX_PAYLOAD+1], sizeof(pid_t)); // PID for client RPC tracking (only for debug)
memcpy(&newfd, &buf[IDX_PAYLOAD+1], sizeof(int));
//dwr("newfd = %d\n", newfd);
handle_retval(sock, uptr, rpc_count, newfd);
break;
case RPC_MAP_REQ:
dwr(2, "RPC_MAP_REQ\n");
dwr(MSG_DEBUG, "RPC_MAP_REQ\n");
handle_map_request(sock, uptr, buf);
break;
default:
@ -652,7 +644,7 @@ int NetconEthernetTap::send_return_value(TcpConnection *conn, int retval, int _e
if(n > 0)
conn->pending = false;
else {
dwr(" Unable to send return value to the intercept. Closing connection\n");
dwr(MSG_ERROR, " Unable to send return value to the intercept. Closing connection\n");
closeConnection(conn);
}
return n;
@ -662,7 +654,7 @@ int NetconEthernetTap::send_return_value(TcpConnection *conn, int retval, int _e
int NetconEthernetTap::send_return_value(int fd, int retval, int _errno = 0)
{
dwr(3, " send_return_value(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno);
dwr(MSG_DEBUG, " send_return_value(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno);
int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
char retmsg[sz];
memset(&retmsg, '\0', sizeof(retmsg));
@ -722,7 +714,7 @@ int NetconEthernetTap::send_return_value(int fd, int retval, int _errno = 0)
*/
err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
dwr(4, "nc_accept()\n");
dwr(MSG_DEBUG, "nc_accept()\n");
Larg *l = (Larg*)arg;
TcpConnection *conn = l->conn;
NetconEthernetTap *tap = l->tap;
@ -733,7 +725,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
if(errno < 0) {
l->tap->send_return_value(conn, -1, errno);
dwr("nc_accept(): unable to create socketpair\n");
dwr(MSG_ERROR, "nc_accept(): unable to create socketpair\n");
return ERR_MEM;
}
}
@ -743,12 +735,10 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
new_tcp_conn->pcb = newpcb;
new_tcp_conn->their_fd = fds[1];
tap->tcp_connections.push_back(new_tcp_conn);
dwr(4, "socketpair = {%d, %d}\n", fds[0], fds[1]);
dwr(MSG_DEBUG, "socketpair = {%d, %d}\n", fds[0], fds[1]);
int n, send_fd = tap->_phy.getDescriptor(conn->rpcSock);
dwr(4, "write(%d,...)\n", listening_fd);
//int n = write(listening_fd, "z", 1); // accept() in library waits for this byte
if((n = send(listening_fd, "z", 1, MSG_NOSIGNAL)) < 0) {
dwr(" nc_accept(): Error: [send(listening_fd,...) = MSG_NOSIGNAL].\n");
dwr(MSG_ERROR, " nc_accept(): Error: [send(listening_fd,...) = MSG_NOSIGNAL].\n");
return -1;
}
else if(n > 0) {
@ -757,11 +747,11 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
new_tcp_conn->pending = true;
}
else {
dwr("nc_accept(%d): unable to send fd to client\n", listening_fd);
dwr(MSG_ERROR, "nc_accept(%d): unable to send fd to client\n", listening_fd);
}
}
else {
dwr("nc_accept(%d): error writing signal byte (send_fd = %d, perceived_fd = %d)\n", listening_fd, send_fd, fds[1]);
dwr(MSG_ERROR, "nc_accept(%d): error writing signal byte (send_fd = %d, perceived_fd = %d)\n", listening_fd, send_fd, fds[1]);
return -1;
}
tap->lwipstack->_tcp_arg(newpcb, new Larg(tap, new_tcp_conn));
@ -773,7 +763,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
return ERR_OK;
}
else {
dwr("nc_accept(%d): can't locate Connection object for PCB.\n", listening_fd);
dwr(MSG_ERROR, "nc_accept(%d): can't locate Connection object for PCB.\n", listening_fd);
}
return -1;
}
@ -793,23 +783,23 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
*/
err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
dwr(5, " nc_recved()\n");
dwr(MSG_DEBUG, " nc_recved()\n");
Larg *l = (Larg*)arg;
int n;
struct pbuf* q = p;
if(!l->conn) {
dwr(" nc_recved(): no connection object\n");
dwr(MSG_ERROR, " nc_recved(): no connection object\n");
return ERR_OK; // ?
}
if(p == NULL) {
if(l->conn) {
dwr(" nc_recved(): closing connection\n");
dwr(MSG_INFO, " nc_recved(): closing connection\n");
l->tap->closeConnection(l->conn);
return ERR_ABRT;
}
else {
dwr(" nc_recved(): can't locate connection via (arg)\n");
dwr(MSG_ERROR, " nc_recved(): can't locate connection via (arg)\n");
}
return err;
}
@ -819,12 +809,12 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
break; // ?
if((n = l->tap->_phy.streamSend(l->conn->dataSock,p->payload, p->len)) > 0) {
if(n < p->len) {
dwr(" nc_recved(): unable to write entire pbuf to buffer\n");
dwr(MSG_INFO, " nc_recved(): unable to write entire pbuf to buffer\n");
}
l->tap->lwipstack->_tcp_recved(tpcb, n); // TODO: would it be more efficient to call this once at the end?
}
else {
dwr(" nc_recved(): No data written to intercept buffer\n");
dwr(MSG_INFO, " nc_recved(): No data written to intercept buffer\n");
}
p = p->next;
}
@ -844,51 +834,51 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
*/
void NetconEthernetTap::nc_err(void *arg, err_t err)
{
dwr(3, "nc_err()\n");
dwr(MSG_DEBUG, "nc_err()\n");
Larg *l = (Larg*)arg;
if(!l->conn)
dwr("nc_err(): Connection is NULL!\n");
dwr(MSG_ERROR, "nc_err(): Connection is NULL!\n");
switch(err)
{
case ERR_MEM:
dwr("nc_err(): ERR_MEM->ENOMEM\n");
dwr(MSG_ERROR, "nc_err(): ERR_MEM->ENOMEM\n");
l->tap->send_return_value(l->conn, -1, ENOMEM);
break;
case ERR_BUF:
dwr("nc_err(): ERR_BUF->ENOBUFS\n");
dwr(MSG_ERROR, "nc_err(): ERR_BUF->ENOBUFS\n");
l->tap->send_return_value(l->conn, -1, ENOBUFS);
break;
case ERR_TIMEOUT:
dwr("nc_err(): ERR_TIMEOUT->ETIMEDOUT\n");
dwr(MSG_ERROR, "nc_err(): ERR_TIMEOUT->ETIMEDOUT\n");
l->tap->send_return_value(l->conn, -1, ETIMEDOUT);
break;
case ERR_RTE:
dwr("nc_err(): ERR_RTE->ENETUNREACH\n");
dwr(MSG_ERROR, "nc_err(): ERR_RTE->ENETUNREACH\n");
l->tap->send_return_value(l->conn, -1, ENETUNREACH);
break;
case ERR_INPROGRESS:
dwr("nc_err(): ERR_INPROGRESS->EINPROGRESS\n");
dwr(MSG_ERROR, "nc_err(): ERR_INPROGRESS->EINPROGRESS\n");
l->tap->send_return_value(l->conn, -1, EINPROGRESS);
break;
case ERR_VAL:
dwr("nc_err(): ERR_VAL->EINVAL\n");
dwr(MSG_ERROR, "nc_err(): ERR_VAL->EINVAL\n");
l->tap->send_return_value(l->conn, -1, EINVAL);
break;
case ERR_WOULDBLOCK:
dwr("nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n");
dwr(MSG_ERROR, "nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n");
l->tap->send_return_value(l->conn, -1, EWOULDBLOCK);
break;
case ERR_USE:
dwr("nc_err(): ERR_USE->EADDRINUSE\n");
dwr(MSG_ERROR, "nc_err(): ERR_USE->EADDRINUSE\n");
l->tap->send_return_value(l->conn, -1, EADDRINUSE);
break;
case ERR_ISCONN:
dwr("nc_err(): ERR_ISCONN->EISCONN\n");
dwr(MSG_ERROR, "nc_err(): ERR_ISCONN->EISCONN\n");
l->tap->send_return_value(l->conn, -1, EISCONN);
break;
case ERR_ABRT:
dwr("nc_err(): ERR_ABRT->ECONNREFUSED\n");
dwr(MSG_ERROR, "nc_err(): ERR_ABRT->ECONNREFUSED\n");
l->tap->send_return_value(l->conn, -1, ECONNREFUSED);
break;
@ -912,7 +902,7 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
default:
break;
}
dwr("nc_err(): closing connection\n");
dwr(MSG_ERROR, "nc_err(): closing connection\n");
l->tap->closeConnection(l->conn);
}
@ -976,7 +966,7 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
*/
err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
dwr(3, " nc_connected()\n");
dwr(MSG_DEBUG, " nc_connected()\n");
Larg *l = (Larg*)arg;
l->tap->send_return_value(l->conn, ERR_OK);
return ERR_OK;
@ -986,6 +976,19 @@ err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err
----------------------------- RPC Handler functions ----------------------------
------------------------------------------------------------------------------*/
/* Unpacks the buffer from an RPC command */
void NetconEthernetTap::unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload)
{
unsigned char *buf = (unsigned char*)data;
memcpy(&pid, &buf[IDX_PID], sizeof(pid_t));
memcpy(&tid, &buf[IDX_TID], sizeof(pid_t));
memcpy(&rpc_count, &buf[IDX_COUNT], sizeof(int));
memcpy(timestamp, &buf[IDX_TIME], 20);
memcpy(&cmd, &buf[IDX_PAYLOAD], sizeof(char));
payload = buf+IDX_PAYLOAD+1;
//dwr("RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestamp, cmd);
}
/*
Responds to a request from the [intercept] to determine whether a local socket is
mapped to this service. In other words, how do the intercept's overridden calls
@ -1001,13 +1004,13 @@ void NetconEthernetTap::handle_map_request(PhySocket *sock, void **uptr, unsigne
for(size_t i=0; i<tcp_connections.size(); i++) {
if(tcp_connections[i]->rpcSock == conn->rpcSock && tcp_connections[i]->perceived_fd == req_fd){
send_return_value(conn, 1, ERR_OK); // True
dwr(" handle_map_request(their=%d): MAPPED (to %d)\n", req_fd,
dwr(MSG_DEBUG, " handle_map_request(their=%d): MAPPED (to %d)\n", req_fd,
_phy.getDescriptor(tcp_connections[i]->dataSock));
return;
}
}
send_return_value(conn, 0, ERR_OK); // False
dwr(" handle_map_request(their=%d): NOT MAPPED\n", req_fd);
dwr(MSG_DEBUG, " handle_map_request(their=%d): NOT MAPPED\n", req_fd);
}
/**
@ -1029,15 +1032,11 @@ void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, int rpc_coun
conn->perceived_fd = newfd;
if(rpc_count==rpc_counter) {
dwr("Detected repeat RPC.\n");
dwr(2, "Detected repeat RPC.\n");
//return;
}
else {
rpc_counter = rpc_count;
//dwr("pid = %d\n", rpc_count);
//dwr("rpc_counter = %d\n", rpc_counter);
}
else
rpc_counter = rpc_count;
dwr(4, " handle_retval(): CONN:%x - Mapping [our=%d -> their=%d]\n",conn,
_phy.getDescriptor(conn->dataSock), conn->perceived_fd);
@ -1056,11 +1055,11 @@ void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, int rpc_coun
if(tcp_connections[i]->perceived_fd == conn->perceived_fd) {
int n;
if((n = send(_phy.getDescriptor(tcp_connections[i]->dataSock), "z", 1, MSG_NOSIGNAL)) < 0) {
dwr(" handle_retval(): CONN:%x - Socket (%d) already mapped (originally CONN:%x)\n", conn, tcp_connections[i]->perceived_fd, tcp_connections[i]);
dwr(MSG_DEBUG, " handle_retval(): CONN:%x - Socket (%d) already mapped (originally CONN:%x)\n", conn, tcp_connections[i]->perceived_fd, tcp_connections[i]);
closeConnection(tcp_connections[i]);
}
else {
dwr(" handle_retval(): CONN:%x - This socket is mapped to two different pipes (?). Exiting.\n", conn);
dwr(MSG_ERROR, " handle_retval(): CONN:%x - This socket is mapped to two different pipes (?). Exiting.\n", conn);
//die(0); // FIXME: Print service mapping state and exit
}
}
@ -1124,8 +1123,8 @@ void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st
d[1] = (ip >> 8) & 0xFF;
d[2] = (ip >> 16) & 0xFF;
d[3] = (ip >> 24) & 0xFF;
dwr(" handle_bind(): error binding to %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], conn_port);
dwr(" handle_bind(): err = %d\n", err);
dwr(MSG_ERROR, " handle_bind(): error binding to %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], conn_port);
dwr(MSG_ERROR, " handle_bind(): err = %d\n", err);
if(err == ERR_USE)
send_return_value(conn, -1, EADDRINUSE);
@ -1138,12 +1137,12 @@ void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st
send_return_value(conn, ERR_OK, ERR_OK); // Success
}
else {
dwr(" handle_bind(): PCB (%x) not in CLOSED state. Ignoring BIND request.\n", conn->pcb);
dwr(MSG_ERROR, " handle_bind(): PCB (%x) not in CLOSED state. Ignoring BIND request.\n", conn->pcb);
send_return_value(conn, -1, EINVAL);
}
}
else {
dwr(" handle_bind(): can't locate connection for PCB\n");
dwr(MSG_ERROR, " handle_bind(): can't locate connection for PCB\n");
send_return_value(conn, -1, EBADF);
}
}
@ -1172,14 +1171,14 @@ void NetconEthernetTap::handle_listen(PhySocket *sock, void **uptr, struct liste
dwr(3, " handle_listen(their=%d):\n", listen_rpc->sockfd);
TcpConnection *conn = getConnectionByTheirFD(sock, listen_rpc->sockfd);
if(!conn){
dwr(" handle_listen(): unable to locate connection object\n");
dwr(MSG_ERROR, " handle_listen(): unable to locate connection object\n");
// ? send_return_value(conn, -1, EBADF);
return;
}
dwr(3, " handle_listen(our=%d -> their=%d)\n", _phy.getDescriptor(conn->dataSock), conn->perceived_fd);
if(conn->pcb->state == LISTEN) {
dwr(" handle_listen(): PCB is already in listening state.\n");
dwr(MSG_ERROR, " handle_listen(): PCB is already in listening state.\n");
return;
}
struct tcp_pcb* listening_pcb;
@ -1247,7 +1246,7 @@ TcpConnection * NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, s
{
int rpc_fd = _phy.getDescriptor(sock);
struct tcp_pcb *newpcb = lwipstack->tcp_new();
dwr(3, " handle_socket(): pcb=%x\n", newpcb);
dwr(MSG_DEBUG, " handle_socket(): pcb=%x\n", newpcb);
if(newpcb != NULL) {
ZT_PHY_SOCKFD_TYPE fds[2];
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
@ -1256,7 +1255,7 @@ TcpConnection * NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, s
return NULL;
}
}
dwr(" handle_socket(): socketpair = {%d, %d}\n", fds[0], fds[1]);
dwr(MSG_DEBUG, " handle_socket(): socketpair = {%d, %d}\n", fds[0], fds[1]);
TcpConnection *new_conn = new TcpConnection();
new_conn->dataSock = _phy.wrapSocket(fds[0], new_conn);
*uptr = new_conn;
@ -1272,7 +1271,7 @@ TcpConnection * NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, s
}
else {
sock_fd_write(rpc_fd, -1); // Send a bad fd, to signal error
dwr(" handle_socket(): Memory not available for new PCB\n");
dwr(MSG_ERROR, " handle_socket(): Memory not available for new PCB\n");
send_return_value(rpc_fd, -1, ENOMEM);
return NULL;
}
@ -1315,7 +1314,7 @@ TcpConnection * NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, s
*/
void NetconEthernetTap::handle_connect(PhySocket *sock, void **uptr, struct connect_st* connect_rpc)
{
dwr(3, " handle_connect()\n");
dwr(MSG_DEBUG, " handle_connect()\n");
TcpConnection *conn = (TcpConnection*)*uptr;
struct sockaddr_in *connaddr;
connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
@ -1376,14 +1375,14 @@ void NetconEthernetTap::handle_connect(PhySocket *sock, void **uptr, struct conn
// that's it!
// - Most instances of a retval for a connect() should happen
// in the nc_connect() and nc_err() callbacks!
dwr(" handle_connect(): unable to connect\n");
dwr(MSG_ERROR, " handle_connect(): unable to connect\n");
send_return_value(conn, -1, EAGAIN);
}
// Everything seems to be ok, but we don't have enough info to retval
conn->pending=true;
}
else {
dwr(" handle_connect(): could not locate PCB based on their fd\n");
dwr(MSG_ERROR, " handle_connect(): could not locate PCB based on their fd\n");
send_return_value(conn, -1, EBADF);
}
}
@ -1391,17 +1390,17 @@ void NetconEthernetTap::handle_connect(PhySocket *sock, void **uptr, struct conn
void NetconEthernetTap::handle_write(TcpConnection *conn)
{
//dwr(5, " handle_write()\n");
//dwr(MSG_DEBUG, " handle_write()\n");
float max = (float)TCP_SND_BUF;
int r;
if(!conn) {
dwr(" handle_write(): could not locate connection for this fd\n");
dwr(MSG_ERROR, " handle_write(): could not locate connection for this fd\n");
return;
}
if(conn->idx < max) {
if(!conn->pcb) {
dwr(" handle_write(): conn->pcb == NULL. Failed to write.\n");
dwr(MSG_ERROR, " handle_write(): conn->pcb == NULL. Failed to write.\n");
return;
}
int sndbuf = conn->pcb->snd_buf; // How much we are currently allowed to write to the connection
@ -1428,7 +1427,7 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
int err = lwipstack->_tcp_write(conn->pcb, &conn->buf, r, TCP_WRITE_FLAG_COPY);
lwipstack->_tcp_output(conn->pcb);
if(err != ERR_OK) {
dwr(" handle_write(): error while writing to PCB, (err = %d)\n", err);
dwr(MSG_ERROR, " handle_write(): error while writing to PCB, (err = %d)\n", err);
return;
}
else {
@ -1442,7 +1441,7 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
}
}
else {
dwr(" handle_write(): LWIP stack full\n");
dwr(MSG_INFO, " handle_write(): LWIP stack full\n");
return;
}
}

View File

@ -53,6 +53,7 @@ namespace ZeroTier {
class NetconEthernetTap;
/**
* Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
*/
@ -104,6 +105,8 @@ private:
static err_t nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
// RPC handlers (from NetconIntercept)
void unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload);
void handle_bind(PhySocket *sock, void **uptr, struct bind_st *bind_rpc);
void handle_listen(PhySocket *sock, void **uptr, struct listen_st *listen_rpc);
void handle_map_request(PhySocket *sock, void **uptr, unsigned char* buf);

View File

@ -37,15 +37,14 @@
#include "lwip/ip_addr.h"
#include "lwip/ip_frag.h"
#include "Intercept.h"
#ifndef _NETCON_UTILITIES_CPP
#define _NETCON_UTILITIES_CPP
#define DEBUG_LEVEL 5
#define DEBUG_LEVEL 4
namespace ZeroTier
{
/*
void dwr(int level, char *fmt, ... )
{
if(level > DEBUG_LEVEL)
@ -65,27 +64,7 @@ namespace ZeroTier
fflush(stderr);
va_end(ap);
}
void unload_rpc(void *data, pid_t &pid, pid_t &tid, int &rpc_count, char (timestamp[20]), char &cmd, void* &payload)
{
unsigned char *buf = (unsigned char*)data;
/*
dwr(" - IDX_PID = %d\n", IDX_PID);
dwr(" - IDX_TID = %d\n", IDX_TID);
dwr(" - IDX_COUNT = %d\n", IDX_COUNT);
dwr(" - IDX_TIME = %d\n", IDX_TIME);
dwr(" - IDX_PAYLOAD = %d\n", IDX_PAYLOAD);
*/
memcpy(&pid, &buf[IDX_PID], sizeof(pid_t));
memcpy(&tid, &buf[IDX_TID], sizeof(pid_t));
memcpy(&rpc_count, &buf[IDX_COUNT], sizeof(int));
memcpy(timestamp, &buf[IDX_TIME], 20);
memcpy(&cmd, &buf[IDX_PAYLOAD], sizeof(char));
payload = buf+IDX_PAYLOAD+1;
//dwr("RX: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d\n", pid, tid, rpc_count, timestamp, cmd);
}
void clearscreen(){
fprintf(stderr, "\033[2J");
}

View File

@ -31,8 +31,6 @@
namespace ZeroTier
{
void dwr(int level, char *fmt, ... );
void dwr(char *fmt, ... );
void clearscreen();
void gotoxy(int x,int y);
void get_path_from_pid(char* dest, int pid);

View File

@ -27,7 +27,7 @@
SHCC=gcc
intercept_CFLAGS = -c -fPIC -g -O2 -Wall -std=c99 -DERRORS_ARE_FATAL -DDEBUG -DVERBOSE -DCHECKS -D_GNU_SOURCE -DNETCON_INTERCEPT
intercept_CFLAGS = -c -fPIC -g -O2 -Wall -std=c99 -DERRORS_ARE_FATAL -DDEBUG_RPC -DVERBOSE -DCHECKS -D_GNU_SOURCE -DNETCON_INTERCEPT
LIB_NAME = intercept
SHLIB_EXT=dylib
SHLIB_MAJOR = 1
@ -40,9 +40,8 @@ LIBS = -ldl -lc -lrt -lpthread
lib:
${SHCC} $(intercept_CFLAGS) -I. Intercept.c -o Intercept.o
${SHCC} $(intercept_CFLAGS) -I. Common.c -o Common.o
${SHCC} $(intercept_CFLAGS) -I. Sendfd.c -o Sendfd.o
${SHCC} $(SHLDFLAGS) Intercept.o Common.o Sendfd.o -o libintercept.so.1.0 $(LIBS)
${SHCC} $(SHLDFLAGS) Intercept.o Sendfd.o -o libintercept.so.1.0 $(LIBS)
install:
cp libintercept.so.1.0 /lib/libintercept.so.1.0