From 78c0f0a443919d8f214486f188c6e83a8af15fef Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Mon, 11 Jan 2016 13:23:43 -0800 Subject: [PATCH] RPC detection fix --- netcon/NetconEthernetTap.cpp | 32 ++++++++++++-------------------- netcon/RPC.c | 3 +-- netcon/RPC.h | 14 ++++++++------ netcon/common.inc.c | 2 +- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/netcon/NetconEthernetTap.cpp b/netcon/NetconEthernetTap.cpp index 23f86d5e6..942da6cad 100644 --- a/netcon/NetconEthernetTap.cpp +++ b/netcon/NetconEthernetTap.cpp @@ -56,7 +56,7 @@ #define APPLICATION_POLL_FREQ 20 #define ZT_LWIP_TCP_TIMER_INTERVAL 5 -#define STATUS_TMR_INTERVAL 10000 // How often we check connection statuses (in ms) +#define STATUS_TMR_INTERVAL 60000 // How often we check connection statuses (in ms) #define DEFAULT_READ_BUFFER_SIZE 1024 * 1024 * 5 namespace ZeroTier { @@ -363,7 +363,6 @@ void NetconEthernetTap::threadMain() uint64_t status_remaining = STATUS_TMR_INTERVAL; // Connection prunning - /* if (since_status >= STATUS_TMR_INTERVAL) { prev_status_time = now; status_remaining = STATUS_TMR_INTERVAL - since_status; @@ -392,17 +391,15 @@ void NetconEthernetTap::threadMain() phyOnUnixData(tcp_connections[i]->sock,_phy.getuptr(tcp_connections[i]->sock),&tmpbuf,BUF_SZ); } } - }*/ + } // Main TCP/ETHARP timer section if (since_tcp >= ZT_LWIP_TCP_TIMER_INTERVAL) { prev_tcp_time = now; lwipstack->tcp_tmr(); // Makeshift poll - for(size_t i=0; iidx > 0){ - //dwr(MSG_DEBUG, "writing from poll\n"); lwipstack->_lock.lock(); handle_write(tcp_connections[i]); lwipstack->_lock.unlock(); @@ -497,7 +494,6 @@ void NetconEthernetTap::unload_rpc(void *data, pid_t &pid, pid_t &tid, */ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) { - //dwr(MSG_DEBUG,"\n\n\n<%x> phyOnUnixData(): len = %d\n", sock, len); uint64_t magic_num; pid_t pid, tid; int rpc_count; @@ -506,13 +502,19 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns unsigned char *buf = (unsigned char*)data; std::pair sockdata; PhySocket *streamsock, *rpcsock; - bool found_job = false; - + bool found_job = false, detected_rpc = false; TcpConnection *conn; int wlen = len; // RPC - if(buf[IDX_SIGNAL_BYTE] == 'R') { + char phrase[RPC_PHRASE_SIZE]; + memset(phrase, 0, RPC_PHRASE_SIZE); + if(len == BUF_SZ) { + memcpy(phrase, buf, RPC_PHRASE_SIZE); + if(strcmp(phrase, RPC_PHRASE) == 0) + detected_rpc = true; + } + if(detected_rpc) { unload_rpc(data, pid, tid, rpc_count, timestamp, magic, cmd, payload); memcpy(&magic_num, magic, MAGIC_SIZE); dwr(MSG_DEBUG," <%x> RPC: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d)\n", sock, pid, tid, rpc_count, timestamp, cmd); @@ -526,11 +528,8 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns pidmap[sock] = pid; new_conn->pid = pid; } - //return; // Don't close the socket, we'll use this later for data } else { // All RPCs other than RPC_SOCKET - // No matching stream has been encountered, create jobmap entry - dwr(MSG_DEBUG," <%x> creating jobmap (cmd=%d) entry for %llu\n", sock, cmd, magic_num); jobmap[magic_num] = std::make_pair(sock, data); } write(_phy.getDescriptor(sock), "z", 1); // RPC ACK byte to maintain RPC->Stream order @@ -597,10 +596,8 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns } } } - // Write data from stream if(conn->idx > (DEFAULT_READ_BUFFER_SIZE / 2)) { - dwr(MSG_DEBUG,"Buffer near full. Slowing\n"); _phy.setNotifyReadable(sock, false); } lwipstack->_lock.lock(); @@ -618,8 +615,6 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns if(found_job) { conn = getConnection(sock); unload_rpc(buf, pid, tid, rpc_count, timestamp, magic, cmd, payload); - //dwr(MSG_DEBUG," <%x> RPC: (pid=, tid=, rpc_count=, timestamp=, cmd=%d)\n", sock, /*pid, tid, rpc_count, timestamp, */cmd); - switch(cmd) { case RPC_BIND: dwr(MSG_DEBUG," <%x> RPC_BIND\n", sock); @@ -939,10 +934,7 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len) { Larg *l = (Larg*)arg; if(len) { - //dwr(MSG_DEBUG,"nc_sent(ACKED): len = %d\n",len); - //l->conn->acked+=len; - if(l->conn->idx < DEFAULT_READ_BUFFER_SIZE / 2) - { + if(l->conn->idx < DEFAULT_READ_BUFFER_SIZE / 2) { l->tap->_phy.setNotifyReadable(l->conn->sock, true); l->tap->_phy.whack(); } diff --git a/netcon/RPC.c b/netcon/RPC.c index cfcf9a2df..46f09401a 100644 --- a/netcon/RPC.c +++ b/netcon/RPC.c @@ -133,8 +133,7 @@ int rpc_send_command(int cmd, int forfd, void *data, int len) time_t timestamp; timestamp = time(NULL); strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(×tamp)); - - metabuf[IDX_SIGNAL_BYTE] = 'R'; + memcpy(metabuf, RPC_PHRASE, RPC_PHRASE_SIZE); // Write signal phrase memcpy(&metabuf[IDX_PID], &pid, sizeof(pid_t) ); /* pid */ memcpy(&metabuf[IDX_TID], &tid, sizeof(pid_t) ); /* tid */ diff --git a/netcon/RPC.h b/netcon/RPC.h index ee8182e87..5affc0d4a 100644 --- a/netcon/RPC.h +++ b/netcon/RPC.h @@ -7,13 +7,15 @@ #define MAGIC_PADDING_SIZE 12 #define TOKEN_SIZE MAGIC_SIZE+MAGIC_PADDING_SIZE +#define RPC_PHRASE "zerotier\0" +#define RPC_PHRASE_SIZE 9 // 1st section -#define IDX_SIGNAL_BYTE 0 -#define IDX_PID 1 -#define IDX_TID sizeof(pid_t) + 1 -#define IDX_COUNT IDX_TID + sizeof(pid_t) -#define IDX_TIME IDX_COUNT + sizeof(int) -#define IDX_PAYLOAD IDX_TIME + 20 /* 20 being the length of the timestamp string */ +#define IDX_SIGNAL_PHRASE 0 +#define IDX_PID IDX_SIGNAL_PHRASE + RPC_PHRASE_SIZE +#define IDX_TID sizeof(pid_t) + IDX_PID +#define IDX_COUNT IDX_TID + sizeof(pid_t) +#define IDX_TIME IDX_COUNT + sizeof(int) +#define IDX_PAYLOAD IDX_TIME + 20 /* 20 being the length of the timestamp string */ // 2nd section #define CMD_ID_IDX 0 diff --git a/netcon/common.inc.c b/netcon/common.inc.c index 669dff227..858a2195f 100644 --- a/netcon/common.inc.c +++ b/netcon/common.inc.c @@ -42,7 +42,7 @@ #ifndef _COMMON_H #define _COMMON_H 1 -#define DEBUG_LEVEL 1 +#define DEBUG_LEVEL 0 #define MSG_WARNING 4 #define MSG_ERROR 1 // Errors