summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--netcon/NetconEthernetTap.cpp826
-rw-r--r--netcon/NetconEthernetTap.hpp322
-rw-r--r--netcon/RPC.c18
-rw-r--r--netcon/RPC.h65
-rw-r--r--netcon/common.inc.c10
5 files changed, 602 insertions, 639 deletions
diff --git a/netcon/NetconEthernetTap.cpp b/netcon/NetconEthernetTap.cpp
index 65fc10b1..03d83091 100644
--- a/netcon/NetconEthernetTap.cpp
+++ b/netcon/NetconEthernetTap.cpp
@@ -54,10 +54,10 @@
#include "common.inc.c"
#include "RPC.h"
-#define APPLICATION_POLL_FREQ 2
+#define APPLICATION_POLL_FREQ 50
#define ZT_LWIP_TCP_TIMER_INTERVAL 5
-#define STATUS_TMR_INTERVAL 500 // How often we check connection statuses (in ms)
-#define DEFAULT_BUFFER_SIZE 1024 * 256
+#define STATUS_TMR_INTERVAL 1000 // How often we check connection statuses (in ms)
+#define DEFAULT_BUF_SZ 1024 * 1024 * 2
namespace ZeroTier {
@@ -70,26 +70,23 @@ static err_t tapif_init(struct netif *netif)
return ERR_OK;
}
+/*
+ * Outputs data from the pbuf queue to the interface
+ */
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
struct pbuf *q;
char buf[ZT_MAX_MTU+32];
char *bufptr;
- int tot_len = 0;
+ int totalLength = 0;
ZeroTier::NetconEthernetTap *tap = (ZeroTier::NetconEthernetTap*)netif->state;
-
- /* initiate transfer(); */
bufptr = buf;
-
+ // Copy data from each pbuf, one at a time
for(q = p; q != NULL; q = q->next) {
- /* Send the data from the pbuf to the interface, one pbuf at a
- time. The size of the data in each pbuf is kept in the ->len
- variable. */
- /* send data from(q->payload, q->len); */
memcpy(bufptr, q->payload, q->len);
bufptr += q->len;
- tot_len += q->len;
+ totalLength += q->len;
}
// [Send packet to network]
@@ -99,12 +96,11 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
ZeroTier::MAC src_mac;
ZeroTier::MAC dest_mac;
-
src_mac.setTo(ethhdr->src.addr, 6);
dest_mac.setTo(ethhdr->dest.addr, 6);
tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
- Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),tot_len - sizeof(struct eth_hdr));
+ Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),totalLength - sizeof(struct eth_hdr));
return ERR_OK;
}
@@ -114,15 +110,13 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
class TcpConnection
{
public:
-
- bool pending, listening;
+ bool listening;
int pid, txsz, rxsz;
- PhySocket *rpcsock;
- PhySocket *sock;
+ PhySocket *rpcSock, *sock;
struct tcp_pcb *pcb;
struct sockaddr_storage *addr;
- unsigned char txbuf[DEFAULT_BUFFER_SIZE];
- unsigned char rxbuf[DEFAULT_BUFFER_SIZE];
+ unsigned char txbuf[DEFAULT_BUF_SZ];
+ unsigned char rxbuf[DEFAULT_BUF_SZ];
};
/*
@@ -159,7 +153,7 @@ NetconEthernetTap::NetconEthernetTap(
_run(true)
{
char sockPath[4096],lwipPath[4096];
- rpc_counter = -1;
+ rpcCounter = -1;
Utils::snprintf(sockPath,sizeof(sockPath),"%s%snc_%.16llx",homePath,ZT_PATH_SEPARATOR_S,_nwid,ZT_PATH_SEPARATOR_S,(unsigned long long)nwid);
_dev = sockPath; // in netcon mode, set device to be just the network ID
@@ -180,7 +174,7 @@ NetconEthernetTap::~NetconEthernetTap()
{
_run = false;
_phy.whack();
- _phy.whack();
+ _phy.whack(); // TODO: Rationale?
Thread::join(_thread);
_phy.close(_unixListenSocket,false);
delete lwipstack;
@@ -328,9 +322,7 @@ void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,s
void NetconEthernetTap::threadMain()
throw()
{
- uint64_t prev_tcp_time = 0;
- uint64_t prev_status_time = 0;
- uint64_t prev_etharp_time = 0;
+ uint64_t prev_tcp_time = 0, prev_status_time = 0, prev_etharp_time = 0;
// Main timer loop
while (_run) {
@@ -344,48 +336,44 @@ void NetconEthernetTap::threadMain()
// Connection prunning
if (since_status >= STATUS_TMR_INTERVAL) {
-
prev_status_time = now;
status_remaining = STATUS_TMR_INTERVAL - since_status;
- dwr(MSG_DEBUG," tap_thread(): tcp\\jobs = {%d, %d}\n", tcp_connections.size(), jobmap.size());
- for(size_t i=0; i<tcp_connections.size(); i++) {
- // No TCP connections are associated, this is a candidate for removal
- if(!tcp_connections[i]->sock)
+
+ for(size_t i=0;i<_TcpConnections.size();++i) {
+ if(!_TcpConnections[i]->sock)
continue; // Skip, this is a pending connection
- int fd = _phy.getDescriptor(tcp_connections[i]->sock);
+ int fd = _phy.getDescriptor(_TcpConnections[i]->sock);
+ dwr(MSG_DEBUG," tap_thread(): tcp\\jobs = {%d, %d}\n", _TcpConnections.size(), jobmap.size());
fcntl(fd, F_SETFL, O_NONBLOCK);
unsigned char tmpbuf[BUF_SZ];
+
int n = read(fd,&tmpbuf,BUF_SZ);
- if(tcp_connections[i]->pcb->state == SYN_SENT) {
- dwr(MSG_DEBUG_EXTRA," tap_thread(): <%x> state = SYN_SENT, candidate for removal\n", tcp_connections[i]->sock);
+ if(_TcpConnections[i]->pcb->state == SYN_SENT) {
+ dwr(MSG_DEBUG_EXTRA," tap_thread(): <%x> state = SYN_SENT, should finish or be removed soon\n", _TcpConnections[i]->sock);
}
if((n < 0 && errno != EAGAIN) || (n == 0 && errno == EAGAIN)) {
- dwr(MSG_DEBUG," tap_thread(): closing sock (%x)\n", tcp_connections[i]->sock);
- closeConnection(tcp_connections[i]->sock);
- }
- else if (n > 0) {
+ dwr(MSG_DEBUG," tap_thread(): closing sock (%x)\n", _TcpConnections[i]->sock);
+ closeConnection(_TcpConnections[i]->sock);
+ } else if (n > 0) {
dwr(MSG_DEBUG," tap_thread(): data read during connection check (%d bytes)\n", n);
- phyOnUnixData(tcp_connections[i]->sock,_phy.getuptr(tcp_connections[i]->sock),&tmpbuf,BUF_SZ);
+ phyOnUnixData(_TcpConnections[i]->sock,_phy.getuptr(_TcpConnections[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; i<tcp_connections.size(); i++) {
- if(tcp_connections[i]->txsz > 0){
+ for(size_t i=0;i<_TcpConnections.size();++i) {
+ if(_TcpConnections[i]->txsz > 0){
lwipstack->_lock.lock();
- handle_write(tcp_connections[i]);
+ handleWrite(_TcpConnections[i]);
lwipstack->_lock.unlock();
}
}
-
} else {
tcp_remaining = ZT_LWIP_TCP_TIMER_INTERVAL - since_tcp;
}
@@ -408,36 +396,35 @@ 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::addConnection(TcpConnection *conn)
+
+TcpConnection *NetconEthernetTap::getConnection(PhySocket *sock)
{
Mutex::Lock _l(_tcpconns_m);
- tcp_connections.push_back(conn);
+ for(size_t i=0;i<_TcpConnections.size();++i) {
+ if(_TcpConnections[i]->sock == sock)
+ return _TcpConnections[i];
+ }
+ return NULL;
}
-void NetconEthernetTap::removeConnection(TcpConnection *conn)
+TcpConnection *NetconEthernetTap::addConnection(TcpConnection *conn)
{
Mutex::Lock _l(_tcpconns_m);
- for(size_t i=0; i<tcp_connections.size(); i++) {
- if(tcp_connections[i] == conn){
- tcp_connections.erase(tcp_connections.begin() + i);
- return;
- }
- }
+ _TcpConnections.push_back(conn);
+ return conn;
}
-TcpConnection *NetconEthernetTap::getConnection(PhySocket *sock)
+void NetconEthernetTap::removeConnection(TcpConnection *conn)
{
Mutex::Lock _l(_tcpconns_m);
- for(size_t i=0; i<tcp_connections.size(); i++) {
- if(tcp_connections[i]->sock == sock)
- return tcp_connections[i];
+ for(size_t i=0;i<_TcpConnections.size();++i) {
+ if(_TcpConnections[i] == conn){
+ _TcpConnections.erase(_TcpConnections.begin() + i);
+ return;
+ }
}
- return NULL;
}
-/*
- * Closes a TcpConnection and associated LWIP PCB strcuture.
- */
void NetconEthernetTap::closeConnection(PhySocket *sock)
{
dwr(MSG_DEBUG,"closeConnection(%x)\n",sock);
@@ -462,232 +449,189 @@ void NetconEthernetTap::closeConnection(PhySocket *sock)
}
if(!sock)
return;
- close(_phy.getDescriptor(sock)); // close underlying fd
- _phy.close(sock, false); // close PhySocket
+ close(_phy.getDescriptor(sock));
+ _phy.close(sock, false);
}
-/*
- * Signals us to close the TcpConnection associated with this PhySocket
- */
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) {
closeConnection(sock);
}
-/*
- * Handles data on a client's data buffer. Data is sent to LWIP to be enqueued.
- */
void NetconEthernetTap::phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {
- dwr(MSG_DEBUG,"\nphyOnFileDescriptorActivity(): new connection = %x\n", sock);
+ // Currently unused since phyOnUnixData() handles everything now
}
-/*
- * Add a new PhySocket for the client connections
- */
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {
dwr(MSG_DEBUG,"\nphyOnUnixAccept(): new connection = %x\n", sockN);
}
-/*
- * 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 (CANARY[sizeof(uint64_t)]), 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));
- memcpy(CANARY, &buf[IDX_PAYLOAD+1], CANARY_SIZE);
-}
-
-/*
- * Notifies us that we can write to the application's socket
- */
void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
{
- Mutex::Lock _l(_rx_buf_m);
TcpConnection *conn = getConnection(sock);
- float max = (float)DEFAULT_BUFFER_SIZE;
- fprintf(stderr, " ---------------- { TX: %.3f | RX: %.3f } \n", (float)conn->txsz / max, (float)conn->rxsz / max);
-
+ Mutex::Lock _l(_rx_buf_m);
int len = conn->rxsz;
int n = _phy.streamSend(conn->sock, conn->rxbuf, len);
if(n > 0) {
if(n < len) {
dwr(MSG_ERROR,"\n phyOnUnixWritable(): unable to write entire \"block\" to stream\n");
}
- memcpy(conn->rxbuf, conn->rxbuf+n, conn->rxsz-n);
+ if(len-n)
+ memcpy(conn->rxbuf, conn->rxbuf+n, len-n);
conn->rxsz -= n;
- //fprintf(stderr,"RX <--- %d bytes (sz = %d)\n", n, conn->rxsz);
+ float max = (float)DEFAULT_BUF_SZ;
+ dwr(MSG_TRANSFER," <--- RX :: { TX: %.3f%% | RX: %.3f%% } :: %d bytes\n",
+ (float)conn->txsz / max, (float)conn->rxsz / max, n);
lwipstack->_tcp_recved(conn->pcb, n);
if(conn->rxsz == 0){
_phy.setNotifyWritable(conn->sock, false); // Nothing more to be notified about
}
- dwr(MSG_ERROR," phyOnUnixWritable(): wrote { %d / %d } bytes from RX buffer to <%x> (sz = %d)\n", n, len, conn->sock, conn->rxsz);
- }
- else {
+ } else {
perror("\n");
- fprintf(stderr, "errno = %d\n", errno);
+ dwr(MSG_ERROR, "errno = %d\n", errno);
dwr(MSG_ERROR," phyOnUnixWritable(): No data written to stream <%x>\n", conn->sock);
}
}
-/*
- * Processes incoming data on a client-specific RPC connection
- */
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
{
uint64_t CANARY_num;
pid_t pid, tid;
- int rpc_count;
- char cmd, timestamp[20], CANARY[CANARY_SIZE];
+ int rpcCount, wlen = len;
+ char cmd, timestamp[20], CANARY[CANARY_SZ], padding[] = {PADDING};
void *payload;
unsigned char *buf = (unsigned char*)data;
std::pair<PhySocket*, void*> sockdata;
- PhySocket *rpcsock;
- bool found_job = false, detected_rpc = false;
+ PhySocket *rpcSock;
+ bool foundJob = false, detected_rpc = false;
TcpConnection *conn;
- int wlen = len;
// RPC
- char phrase[RPC_PHRASE_SIZE];
- memset(phrase, 0, RPC_PHRASE_SIZE);
+ char phrase[RPC_PHRASE_SZ];
+ memset(phrase, 0, RPC_PHRASE_SZ);
if(len == BUF_SZ) {
- memcpy(phrase, buf, RPC_PHRASE_SIZE);
+ memcpy(phrase, buf, RPC_PHRASE_SZ);
if(strcmp(phrase, RPC_PHRASE) == 0)
detected_rpc = true;
}
if(detected_rpc) {
- unload_rpc(data, pid, tid, rpc_count, timestamp, CANARY, cmd, payload);
- memcpy(&CANARY_num, CANARY, CANARY_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);
+ unloadRPC(data, pid, tid, rpcCount, timestamp, CANARY, cmd, payload);
+ memcpy(&CANARY_num, CANARY, CANARY_SZ);
+ dwr(MSG_DEBUG," <%x> RPC: (pid=%d, tid=%d, rpcCount=%d, timestamp=%s, cmd=%d)\n",
+ sock, pid, tid, rpcCount, timestamp, cmd);
+
if(cmd == RPC_SOCKET) {
dwr(MSG_DEBUG," <%x> RPC_SOCKET\n", sock);
// Create new lwip socket and associate it with this sock
struct socket_st socket_rpc;
memcpy(&socket_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct socket_st));
TcpConnection * new_conn;
- if((new_conn = handle_socket(sock, uptr, &socket_rpc))) {
- pidmap[sock] = pid;
- new_conn->pid = pid;
+ if((new_conn = handleSocket(sock, uptr, &socket_rpc))) {
+ new_conn->pid = pid; // Merely kept to look up application path/names later, not strictly necessary
}
- }
- else { // All RPCs other than RPC_SOCKET
+ } else {
jobmap[CANARY_num] = std::make_pair<PhySocket*, void*>(sock, data);
}
- write(_phy.getDescriptor(sock), "z", 1); // RPC ACK byte to maintain RPC->Stream order
+ write(_phy.getDescriptor(sock), "z", 1); // RPC ACK byte to maintain order
}
-
// STREAM
else {
- int data_start = -1, data_end = -1, token_pos = -1, padding_pos = -1;
- char padding[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
- dwr(MSG_DEBUG," <%x> stream data, len = %d\n", sock, len);
+ int data_start = -1, data_end = -1, canary_pos = -1, padding_pos = -1;
// Look for padding
- std::string padding_pattern(padding, padding+CANARY_PADDING_SIZE);
+ std::string padding_pattern(padding, padding+PADDING_SZ);
std::string buffer(buf, buf + len);
padding_pos = buffer.find(padding_pattern);
- token_pos = padding_pos-CANARY_SIZE;
- dwr(MSG_DEBUG, " <%x> padding_pos = %d\n", sock, padding_pos);
+ canary_pos = padding_pos-CANARY_SZ;
// Grab token, next we'll use it to look up an RPC job
- if(token_pos > -1) {
- memcpy(&CANARY_num, buf+token_pos, CANARY_SIZE);
+ if(canary_pos > -1) {
+ memcpy(&CANARY_num, buf+canary_pos, CANARY_SZ);
if(CANARY_num != 0) { // TODO: Added to address CANARY_num==0 bug, last seeen 20160108
// Find job
sockdata = jobmap[CANARY_num];
if(!sockdata.first) { // Stream before RPC
dwr(MSG_DEBUG," <%x> unable to locate job entry for %llu\n", sock, CANARY_num);
return;
- }
- else
- found_job = true;
+ } else
+ foundJob = true;
}
}
conn = getConnection(sock);
-
- float max = (float)DEFAULT_BUFFER_SIZE;
- fprintf(stderr, " ---------------- { TX: %.3f | RX: %.3f } \n", (float)conn->txsz / max, (float)conn->rxsz / max);
-
if(!conn)
return;
if(padding_pos == -1) { // [DATA]
memcpy(&conn->txbuf[conn->txsz], buf, wlen);
- }
- else { // Padding found, implies a token is present
- // [TOKEN]
- if(len == TOKEN_SIZE && token_pos == 0) {
+ } else { // Padding found, implies a canary is present
+ // [CANARY]
+ if(len == CANARY_SZ+PADDING_SZ && canary_pos == 0) {
wlen = 0; // Nothing to write
- }
- else {
- // [TOKEN] + [DATA]
- if(len > TOKEN_SIZE && token_pos == 0) {
- wlen = len - TOKEN_SIZE;
- data_start = padding_pos+CANARY_PADDING_SIZE;
+ } else {
+ // [CANARY] + [DATA]
+ if(len > CANARY_SZ+PADDING_SZ && canary_pos == 0) {
+ wlen = len - CANARY_SZ+PADDING_SZ;
+ data_start = padding_pos+PADDING_SZ;
memcpy((&conn->txbuf)+conn->txsz, buf+data_start, wlen);
}
- // [DATA] + [TOKEN]
- if(len > TOKEN_SIZE && token_pos > 0 && token_pos == len - TOKEN_SIZE) {
- wlen = len - TOKEN_SIZE;
+ // [CANARY] + [TOKEN]
+ if(len > CANARY_SZ+PADDING_SZ && canary_pos > 0 && canary_pos == len - CANARY_SZ+PADDING_SZ) {
+ wlen = len - CANARY_SZ+PADDING_SZ;
data_start = 0;
memcpy((&conn->txbuf)+conn->txsz, buf+data_start, wlen);
}
- // [DATA] + [TOKEN] + [DATA]
- if(len > TOKEN_SIZE && token_pos > 0 && len > (token_pos + TOKEN_SIZE)) {
- wlen = len - TOKEN_SIZE;
+ // [CANARY] + [TOKEN] + [DATA]
+ if(len > CANARY_SZ+PADDING_SZ && canary_pos > 0 && len > (canary_pos + CANARY_SZ+PADDING_SZ)) {
+ wlen = len - CANARY_SZ+PADDING_SZ;
data_start = 0;
- data_end = padding_pos-CANARY_SIZE;
+ data_end = padding_pos-CANARY_SZ;
memcpy((&conn->txbuf)+conn->txsz, buf+data_start, (data_end-data_start)+1);
- memcpy((&conn->txbuf)+conn->txsz, buf+(padding_pos+CANARY_PADDING_SIZE), len-(token_pos+TOKEN_SIZE));
+ memcpy((&conn->txbuf)+conn->txsz, buf+(padding_pos+PADDING_SZ), len-(canary_pos+CANARY_SZ+PADDING_SZ));
}
}
}
// Write data from stream
- if(conn->txsz > (DEFAULT_BUFFER_SIZE / 2)) {
+ if(conn->txsz > (DEFAULT_BUF_SZ / 2)) {
_phy.setNotifyReadable(sock, false);
}
lwipstack->_lock.lock();
conn->txsz += wlen;
- handle_write(conn);
+ handleWrite(conn);
lwipstack->_lock.unlock();
}
-
- if(found_job) {
- rpcsock = sockdata.first;
+ if(foundJob) {
+ rpcSock = sockdata.first;
buf = (unsigned char*)sockdata.second;
}
// Process RPC if we have a corresponding jobmap entry
- if(found_job) {
- unload_rpc(buf, pid, tid, rpc_count, timestamp, CANARY, cmd, payload);
- dwr(MSG_DEBUG," <%x> RPC: (pid=%d, tid=%d, rpc_count=%d, timestamp=%s, cmd=%d)\n", sock, pid, tid, rpc_count, timestamp, cmd);
+ if(foundJob) {
+ unloadRPC(buf, pid, tid, rpcCount, timestamp, CANARY, cmd, payload);
+ dwr(MSG_DEBUG," <%x> RPC: (pid=%d, tid=%d, rpcCount=%d, timestamp=%s, cmd=%d)\n",
+ sock, pid, tid, rpcCount, timestamp, cmd);
+
switch(cmd) {
case RPC_BIND:
dwr(MSG_DEBUG," <%x> RPC_BIND\n", sock);
struct bind_st bind_rpc;
memcpy(&bind_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct bind_st));
- handle_bind(sock, rpcsock, uptr, &bind_rpc);
+ handleBind(sock, rpcSock, uptr, &bind_rpc);
break;
case RPC_LISTEN:
dwr(MSG_DEBUG," <%x> RPC_LISTEN\n", sock);
struct listen_st listen_rpc;
memcpy(&listen_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct listen_st));
- handle_listen(sock, rpcsock, uptr, &listen_rpc);
+ handleListen(sock, rpcSock, uptr, &listen_rpc);
break;
case RPC_GETSOCKNAME:
dwr(MSG_DEBUG," <%x> RPC_GETSOCKNAME\n", sock);
struct getsockname_st getsockname_rpc;
memcpy(&getsockname_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct getsockname_st));
- handle_getsockname(sock, rpcsock, uptr, &getsockname_rpc);
+ handleGetsockname(sock, rpcSock, uptr, &getsockname_rpc);
break;
case RPC_CONNECT:
dwr(MSG_DEBUG," <%x> RPC_CONNECT\n", sock);
struct connect_st connect_rpc;
memcpy(&connect_rpc, &buf[IDX_PAYLOAD+STRUCT_IDX], sizeof(struct connect_st));
- handle_connect(sock, rpcsock, conn, &connect_rpc);
+ handleConnect(sock, rpcSock, conn, &connect_rpc);
jobmap.erase(CANARY_num);
return; // Keep open RPC, we'll use it once in nc_connected to send retval
default:
@@ -699,18 +643,12 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
}
}
-/*
- * Sends a return value to the intercepted application
- */
-int NetconEthernetTap::send_return_value(PhySocket *sock, int retval, int _errno = 0){
- return send_return_value(_phy.getDescriptor(sock), retval, _errno);
+int NetconEthernetTap::sendReturnValue(PhySocket *sock, int retval, int _errno = 0){
+ return sendReturnValue(_phy.getDescriptor(sock), retval, _errno);
}
-/*
- * Sends a return value to the intercepted application
- */
-int NetconEthernetTap::send_return_value(int fd, int retval, int _errno = 0)
+int NetconEthernetTap::sendReturnValue(int fd, int retval, int _errno = 0)
{
- dwr(MSG_DEBUG," send_return_value(): fd = %d, retval = %d, errno = %d\n", fd, retval, _errno);
+ dwr(MSG_DEBUG," sendReturnValue(): 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));
@@ -720,117 +658,70 @@ int NetconEthernetTap::send_return_value(int fd, int retval, int _errno = 0)
return write(fd, &retmsg, sz);
}
+void NetconEthernetTap::unloadRPC(void *data, pid_t &pid, pid_t &tid,
+ int &rpcCount, char (timestamp[RPC_TIMESTAMP_SZ]), char (CANARY[sizeof(uint64_t)]), 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(&rpcCount, &buf[IDX_COUNT], sizeof(int));
+ memcpy(timestamp, &buf[IDX_TIME], RPC_TIMESTAMP_SZ);
+ memcpy(&cmd, &buf[IDX_PAYLOAD], sizeof(char));
+ memcpy(CANARY, &buf[IDX_PAYLOAD+1], CANARY_SZ);
+}
+
/*------------------------------------------------------------------------------
--------------------------------- LWIP callbacks -------------------------------
------------------------------------------------------------------------------*/
-// NOTE: these are called from within LWIP, meaning that lwipstack->_lock is ALREADY
-// locked in this case!
-
-/*
- * Callback from LWIP for when a connection has been accepted and the PCB has been
- * put into an ACCEPT state.
- *
- * A socketpair is created, one end is kept and wrapped into a PhySocket object
- * for use in the main ZT I/O loop, and one end is sent to the client. The client
- * is then required to tell the service what new file descriptor it has allocated
- * for this connection. After the mapping is complete, the accepted socket can be
- * used.
- *
- * @param associated service state object
- * @param newly allocated PCB
- * @param error code
- * @return ERR_OK if everything is ok, -1 otherwise
-
- i := should be implemented in intercept lib
- I := is implemented in intercept lib
- X := is implemented in service
- ? := required treatment Unknown
- - := Not needed
-
- [ ] EAGAIN or EWOULDBLOCK - The socket is marked nonblocking and no connections are present
- to be accepted. POSIX.1-2001 allows either error to be returned for
- this case, and does not require these constants to have the same value,
- so a portable application should check for both possibilities.
- [I] EBADF - The descriptor is invalid.
- [I] ECONNABORTED - A connection has been aborted.
- [i] EFAULT - The addr argument is not in a writable part of the user address space.
- [-] EINTR - The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
- [I] EINVAL - Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
- [I] EINVAL - (accept4()) invalid value in flags.
- [I] EMFILE - The per-process limit of open file descriptors has been reached.
- [ ] ENFILE - The system limit on the total number of open files has been reached.
- [ ] ENOBUFS, ENOMEM - Not enough free memory. This often means that the memory allocation is
- limited by the socket buffer limits, not by the system memory.
- [I] ENOTSOCK - The descriptor references a file, not a socket.
- [I] EOPNOTSUPP - The referenced socket is not of type SOCK_STREAM.
- [ ] EPROTO - Protocol error.
- *
- */
-err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
+err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err)
{
- dwr(MSG_DEBUG," nc_accept()\n");
Larg *l = (Larg*)arg;
TcpConnection *conn = l->conn;
NetconEthernetTap *tap = l->tap;
if(!conn->sock)
return -1;
- int listening_fd = tap->_phy.getDescriptor(conn->sock);
+ int fd = tap->_phy.getDescriptor(conn->sock);
if(conn) {
// create new socketpair
ZT_PHY_SOCKFD_TYPE fds[2];
if(socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) {
if(errno < 0) {
- l->tap->send_return_value(conn, -1, errno);
+ l->tap->sendReturnValue(conn, -1, errno);
dwr(MSG_ERROR," nc_accept(): unable to create socketpair\n");
return ERR_MEM;
}
}
// create and populate new TcpConnection
- TcpConnection *new_tcp_conn = new TcpConnection();
- tap->addConnection(new_tcp_conn);
- new_tcp_conn->pcb = newpcb;
- new_tcp_conn->sock = tap->_phy.wrapSocket(fds[0], new_tcp_conn);
+ TcpConnection *newTcpConn = new TcpConnection();
+ tap->addConnection(newTcpConn);
+ newTcpConn->pcb = newPCB;
+ newTcpConn->sock = tap->_phy.wrapSocket(fds[0], newTcpConn);
- if(sock_fd_write(listening_fd, fds[1]) < 0)
+ if(sock_fd_write(fd, fds[1]) < 0)
return -1;
else {
//close(fds[1]); // close other end of socketpair
- new_tcp_conn->pending = true;
}
- tap->lwipstack->_tcp_arg(newpcb, new Larg(tap, new_tcp_conn));
- tap->lwipstack->_tcp_recv(newpcb, nc_recved);
- tap->lwipstack->_tcp_err(newpcb, nc_err);
- tap->lwipstack->_tcp_sent(newpcb, nc_sent);
- tap->lwipstack->_tcp_poll(newpcb, nc_poll, 1);
+ tap->lwipstack->_tcp_arg(newPCB, new Larg(tap, newTcpConn));
+ tap->lwipstack->_tcp_recv(newPCB, nc_recved);
+ tap->lwipstack->_tcp_err(newPCB, nc_err);
+ tap->lwipstack->_tcp_sent(newPCB, nc_sent);
+ tap->lwipstack->_tcp_poll(newPCB, nc_poll, 1);
if(conn->pcb->state == LISTEN) {
- dwr(MSG_DEBUG," nc_accept(): Can't call tcp_accept() on LISTEN socket (pcb = %x)\n", conn->pcb);
+ dwr(MSG_DEBUG," nc_accept(): can't call tcp_accept() on LISTEN socket (pcb = %x)\n", conn->pcb);
return ERR_OK; // TODO: Verify this is correct
}
tcp_accepted(conn->pcb); // Let lwIP know that it can queue additional incoming connections
return ERR_OK;
- }
- else
- dwr(MSG_ERROR," nc_accept(%d): can't locate Connection object for PCB.\n", listening_fd);
+ } else
+ dwr(MSG_ERROR," nc_accept(%d): can't locate Connection object for PCB.\n", fd);
return -1;
}
-/*
- * Callback from LWIP for when data is available to be read from the network.
- *
- * Data is in the form of a linked list of struct pbufs, it is then recombined and
- * send to the client over the associated unix socket.
- *
- * @param associated service state object
- * @param allocated PCB
- * @param chain of pbufs
- * @param error code
- * @return ERR_OK if everything is ok, -1 otherwise
- *
- */
-err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
+err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err)
{
Larg *l = (Larg*)arg;
int tot = 0;
@@ -844,7 +735,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
if(l->conn && !l->conn->listening) {
dwr(MSG_INFO," nc_recved(): closing connection\n");
if(l->tap->lwipstack->_tcp_close(l->conn->pcb) != ERR_OK) {
- dwr(MSG_ERROR," nc_recved(): Error while calling tcp_close()\n");
+ dwr(MSG_ERROR," nc_recved(): error while calling tcp_close()\n");
}
l->tap->closeConnection(l->conn->sock);
return ERR_ABRT;
@@ -857,97 +748,88 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
while(p != NULL) {
if(p->len <= 0)
break;
- int avail = DEFAULT_BUFFER_SIZE - l->conn->rxsz;
+ int avail = DEFAULT_BUF_SZ - l->conn->rxsz;
int len = p->len;
if(avail < len)
- dwr(MSG_ERROR," nc_recv(): not enough room (%d bytes) on RX buffer\n", avail);
+ dwr(MSG_ERROR," nc_recved(): not enough room (%d bytes) on RX buffer\n", avail);
memcpy(l->conn->rxbuf + (l->conn->rxsz), p->payload, len);
l->conn->rxsz += len;
- l->tap->_phy.setNotifyWritable(l->conn->sock, true); // Signal that we're interested in knowing when we can write
p = p->next;
tot += len;
}
- dwr(MSG_ERROR," nc_recv(): wrote %d bytes to RX buffer for <%x> (sz = %d)\n", tot, l->conn->sock, l->conn->rxsz);
+ if(tot)
+ l->tap->_phy.setNotifyWritable(l->conn->sock, true); // Signal that we're interested in knowing when we can write
l->tap->lwipstack->_pbuf_free(q);
return ERR_OK;
}
-/*
- * Callback from LWIP when an internal error is associtated with the given (arg)
- *
- * Since the PCB related to this error might no longer exist, only its perviously
- * associated (arg) is provided to us.
- *
- * @param associated service state object
- * @param error code
- *
- */
void NetconEthernetTap::nc_err(void *arg, err_t err)
{
dwr(MSG_DEBUG,"nc_err() = %d\n", err);
Larg *l = (Larg*)arg;
if(!l->conn)
- dwr(MSG_ERROR,"nc_err(): Connection is NULL!\n");
+ dwr(MSG_ERROR,"nc_err(): connection is NULL!\n");
+ int fd = l->tap->_phy.getDescriptor(l->conn->sock);
switch(err)
{
case ERR_MEM:
dwr(MSG_ERROR,"nc_err(): ERR_MEM->ENOMEM\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, ENOMEM);
+ l->tap->sendReturnValue(fd, -1, ENOMEM);
break;
case ERR_BUF:
dwr(MSG_ERROR,"nc_err(): ERR_BUF->ENOBUFS\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, ENOBUFS);
+ l->tap->sendReturnValue(fd, -1, ENOBUFS);
break;
case ERR_TIMEOUT:
dwr(MSG_ERROR,"nc_err(): ERR_TIMEOUT->ETIMEDOUT\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, ETIMEDOUT);
+ l->tap->sendReturnValue(fd, -1, ETIMEDOUT);
break;
case ERR_RTE:
dwr(MSG_ERROR,"nc_err(): ERR_RTE->ENETUNREACH\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, ENETUNREACH);
+ l->tap->sendReturnValue(fd, -1, ENETUNREACH);
break;
case ERR_INPROGRESS:
dwr(MSG_ERROR,"nc_err(): ERR_INPROGRESS->EINPROGRESS\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, EINPROGRESS);
+ l->tap->sendReturnValue(fd, -1, EINPROGRESS);
break;
case ERR_VAL:
dwr(MSG_ERROR,"nc_err(): ERR_VAL->EINVAL\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, EINVAL);
+ l->tap->sendReturnValue(fd, -1, EINVAL);
break;
case ERR_WOULDBLOCK:
dwr(MSG_ERROR,"nc_err(): ERR_WOULDBLOCK->EWOULDBLOCK\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, EWOULDBLOCK);
+ l->tap->sendReturnValue(fd, -1, EWOULDBLOCK);
break;
case ERR_USE:
dwr(MSG_ERROR,"nc_err(): ERR_USE->EADDRINUSE\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, EADDRINUSE);
+ l->tap->sendReturnValue(fd, -1, EADDRINUSE);
break;
case ERR_ISCONN:
dwr(MSG_ERROR,"nc_err(): ERR_ISCONN->EISCONN\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, EISCONN);
+ l->tap->sendReturnValue(fd, -1, EISCONN);
break;
case ERR_ABRT:
dwr(MSG_ERROR,"nc_err(): ERR_ABRT->ECONNREFUSED\n");
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, ECONNREFUSED);
+ l->tap->sendReturnValue(fd, -1, ECONNREFUSED);
break;
// FIXME: Below are errors which don't have a standard errno correlate
case ERR_RST:
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, -1);
+ l->tap->sendReturnValue(fd, -1, -1);
break;
case ERR_CLSD:
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, -1);
+ l->tap->sendReturnValue(fd, -1, -1);
break;
case ERR_CONN:
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, -1);
+ l->tap->sendReturnValue(fd, -1, -1);
break;
case ERR_ARG:
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, -1);
+ l->tap->sendReturnValue(fd, -1, -1);
break;
case ERR_IF:
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->sock), -1, -1);
+ l->tap->sendReturnValue(fd, -1, -1);
break;
default:
break;
@@ -956,37 +838,16 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
l->tap->closeConnection(l->conn);
}
-/*
- * Callback from LWIP to do whatever work we might need to do.
- *
- * @param associated service state object
- * @param PCB we're polling on
- * @return ERR_OK if everything is ok, -1 otherwise
- *
- */
-err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
+err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *PCB)
{
return ERR_OK;
}
-/*
- * Callback from LWIP to signal that 'len' bytes have successfully been sent.
- * As a result, we should put our socket back into a notify-on-readability state
- * since there is now room on the PCB buffer to write to.
- *
- * NOTE: This could be used to track the amount of data sent by a connection.
- *
- * @param associated service state object
- * @param relevant PCB
- * @param length of data sent
- * @return ERR_OK if everything is ok, -1 otherwise
- *
- */
-err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
+err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *PCB, u16_t len)
{
Larg *l = (Larg*)arg;
if(len) {
- float max = (float)DEFAULT_BUFFER_SIZE;
+ float max = (float)DEFAULT_BUF_SZ;
if(l->conn->txsz < max / 2) {
l->tap->_phy.setNotifyReadable(l->conn->sock, true);
l->tap->_phy.whack();
@@ -995,21 +856,10 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
return ERR_OK;
}
-/*
- * Callback from LWIP which sends a return value to the client to signal that
- * a connection was established for this PCB
- *
- * @param associated service state object
- * @param relevant PCB
- * @param error code
- * @return ERR_OK if everything is ok, -1 otherwise
- *
- */
-err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
+err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *PCB, err_t err)
{
- dwr(MSG_DEBUG," nc_connected()\n");
Larg *l = (Larg*)arg;
- l->tap->send_return_value(l->tap->_phy.getDescriptor(l->conn->rpcsock), ERR_OK);
+ l->tap->sendReturnValue(l->tap->_phy.getDescriptor(l->conn->rpcSock), ERR_OK);
return ERR_OK;
}
@@ -1017,241 +867,112 @@ err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err
----------------------------- RPC Handler functions ----------------------------
------------------------------------------------------------------------------*/
-/* Return the address that the socket is bound to */
-void NetconEthernetTap::handle_getsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc)
+void NetconEthernetTap::handleGetsockname(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct getsockname_st *getsockname_rpc)
{
TcpConnection *conn = getConnection(sock);
- // Assemble address "command" to send to intercept
char retmsg[sizeof(struct sockaddr_storage)];
memset(&retmsg, 0, sizeof(retmsg));
if ((conn)&&(conn->addr))
memcpy(&retmsg, conn->addr, sizeof(struct sockaddr_storage));
- write(_phy.getDescriptor(rpcsock), &retmsg, sizeof(struct sockaddr_storage));
+ write(_phy.getDescriptor(rpcSock), &retmsg, sizeof(struct sockaddr_storage));
}
-/*
- * Handles an RPC to bind an LWIP PCB to a given address and port
- *
- * @param PhySocket associated with this RPC connection
- * @param structure containing the data and parameters for this client's RPC
- *
-
- i := should be implemented in intercept lib
- I := is implemented in intercept lib
- X := is implemented in service
- ? := required treatment Unknown
- - := Not needed
-
- [ ] EACCES - The address is protected, and the user is not the superuser.
- [X] EADDRINUSE - The given address is already in use.
- [I] EBADF - sockfd is not a valid descriptor.
- [X] EINVAL - The socket is already bound to an address.
- [I] ENOTSOCK - sockfd is a descriptor for a file, not a socket.
-
- [X] ENOMEM - Insufficient kernel memory was available.
-
- - The following errors are specific to UNIX domain (AF_UNIX) sockets:
-
- [-] EACCES - Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
- [-] EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local.
- [-] EFAULT - addr points outside the user's accessible address space.
- [-] EINVAL - The addrlen is wrong, or the socket was not in the AF_UNIX family.
- [-] ELOOP - Too many symbolic links were encountered in resolving addr.
- [-] ENAMETOOLONG - s addr is too long.
- [-] ENOENT - The file does not exist.
- [-] ENOTDIR - A component of the path prefix is not a directory.
- [-] EROFS - The socket inode would reside on a read-only file system.
-
- */
-void NetconEthernetTap::handle_bind(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct bind_st *bind_rpc)
+void NetconEthernetTap::handleBind(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct bind_st *bind_rpc)
{
- struct sockaddr_in *connaddr;
- connaddr = (struct sockaddr_in *) &bind_rpc->addr;
- int conn_port = lwipstack->ntohs(connaddr->sin_port);
- ip_addr_t conn_addr;
- conn_addr.addr = *((u32_t *)_ips[0].rawIpData());
+ struct sockaddr_in *rawAddr = (struct sockaddr_in *) &bind_rpc->addr;
+ int port = lwipstack->ntohs(rawAddr->sin_port);
+ ip_addr_t connAddr;
+ connAddr.addr = *((u32_t *)_ips[0].rawIpData());
TcpConnection *conn = getConnection(sock);
- dwr(MSG_DEBUG," handle_bind(%d)\n", bind_rpc->sockfd);
-
+ dwr(MSG_DEBUG," handleBind(%d)\n", bind_rpc->sockfd);
if(conn) {
if(conn->pcb->state == CLOSED){
- int err = lwipstack->tcp_bind(conn->pcb, &conn_addr, conn_port);
- int ip = connaddr->sin_addr.s_addr;
+ int err = lwipstack->tcp_bind(conn->pcb, &connAddr, port);
+ int ip = rawAddr->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;
- dwr(MSG_DEBUG," handle_bind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], conn_port);
+ dwr(MSG_DEBUG," handleBind(): %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
if(err != ERR_OK) {
- dwr(MSG_ERROR," handle_bind(): err = %d\n", err);
+ dwr(MSG_ERROR," handleBind(): err = %d\n", err);
if(err == ERR_USE)
- send_return_value(rpcsock, -1, EADDRINUSE);
+ sendReturnValue(rpcSock, -1, EADDRINUSE);
if(err == ERR_MEM)
- send_return_value(rpcsock, -1, ENOMEM);
+ sendReturnValue(rpcSock, -1, ENOMEM);
if(err == ERR_BUF)
- send_return_value(rpcsock, -1, ENOMEM);
- }
- else {
+ sendReturnValue(rpcSock, -1, ENOMEM);
+ } else {
conn->addr = (struct sockaddr_storage *) &bind_rpc->addr;
- send_return_value(rpcsock, ERR_OK, ERR_OK); // Success
+ sendReturnValue(rpcSock, ERR_OK, ERR_OK); // Success
}
+ } else {
+ dwr(MSG_ERROR," handleBind(): PCB (%x) not in CLOSED state. Ignoring BIND request.\n", conn->pcb);
+ sendReturnValue(rpcSock, -1, EINVAL);
}
- else {
- dwr(MSG_ERROR," handle_bind(): PCB (%x) not in CLOSED state. Ignoring BIND request.\n", conn->pcb);
- send_return_value(rpcsock, -1, EINVAL);
- }
- }
- else {
- dwr(MSG_ERROR," handle_bind(): can't locate connection for PCB\n");
- send_return_value(rpcsock, -1, EBADF);
+ } else {
+ dwr(MSG_ERROR," handleBind(): can't locate connection for PCB\n");
+ sendReturnValue(rpcSock, -1, EBADF);
}
}
-/*
- * Handles an RPC to put an LWIP PCB into LISTEN mode
- *
- * @param PhySocket associated with this RPC connection
- * @param structure containing the data and parameters for this client's RPC
- *
-
- i := should be implemented in intercept lib
- I := is implemented in intercept lib
- X := is implemented in service
- ? := required treatment Unknown
- - := Not needed
-
-[?] EADDRINUSE - Another socket is already listening on the same port.
-[IX] EBADF - The argument sockfd is not a valid descriptor.
-[I] ENOTSOCK - The argument sockfd is not a socket.
-[I] EOPNOTSUPP - The socket is not of a type that supports the listen() operation.
-
- */
-void NetconEthernetTap::handle_listen(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct listen_st *listen_rpc)
+void NetconEthernetTap::handleListen(PhySocket *sock, PhySocket *rpcSock, void **uptr, struct listen_st *listen_rpc)
{
- dwr(MSG_DEBUG," handle_listen(their=%d):\n", listen_rpc->sockfd);
TcpConnection *conn = getConnection(sock);
if(!conn){
- dwr(MSG_ERROR," handle_listen(): unable to locate connection object\n");
- send_return_value(rpcsock, -1, EBADF);
+ dwr(MSG_ERROR," handleListen(): unable to locate connection object\n");
+ sendReturnValue(rpcSock, -1, EBADF);
return;
}
if(conn->pcb->state == LISTEN) {
- dwr(MSG_ERROR," handle_listen(): PCB is already in listening state.\n");
- send_return_value(rpcsock, ERR_OK, ERR_OK);
+ dwr(MSG_ERROR," handleListen(): PCB is already in listening state.\n");
+ sendReturnValue(rpcSock, ERR_OK, ERR_OK);
return;
}
- struct tcp_pcb* listening_pcb;
+ struct tcp_pcb* listeningPCB;
#ifdef TCP_LISTEN_BACKLOG
- listening_pcb = lwipstack->tcp_listen_with_backlog(conn->pcb, listen_rpc->backlog);
+ listeningPCB = lwipstack->tcp_listen_with_backlog(conn->pcb, listen_rpc->backlog);
#else
- listening_pcb = lwipstack->tcp_listen(conn->pcb);
+ listeningPCB = lwipstack->tcp_listen(conn->pcb);
#endif
- if(listening_pcb != NULL) {
- conn->pcb = listening_pcb;
- lwipstack->tcp_accept(listening_pcb, nc_accept);
- lwipstack->tcp_arg(listening_pcb, new Larg(this, conn));
+ if(listeningPCB != NULL) {
+ conn->pcb = listeningPCB;
+ lwipstack->tcp_accept(listeningPCB, nc_accept);
+ lwipstack->tcp_arg(listeningPCB, new Larg(this, conn));
/* we need to wait for the client to send us the fd allocated on their end
for this listening socket */
fcntl(_phy.getDescriptor(conn->sock), F_SETFL, O_NONBLOCK);
conn->listening = true;
- conn->pending = true;
- send_return_value(rpcsock, ERR_OK, ERR_OK);
+ sendReturnValue(rpcSock, ERR_OK, ERR_OK);
return;
}
- send_return_value(rpcsock, -1, -1);
+ sendReturnValue(rpcSock, -1, -1);
}
-/*
- * Handles an RPC to create a socket (LWIP PCB and associated socketpair)
- *
- * A socketpair is created, one end is kept and wrapped into a PhySocket object
- * for use in the main ZT I/O loop, and one end is sent to the client. The client
- * is then required to tell the service what new file descriptor it has allocated
- * for this connection. After the mapping is complete, the socket can be used.
- *
- * @param PhySocket associated with this RPC connection
- * @param structure containing the data and parameters for this client's RPC
- *
-
- i := should be implemented in intercept lib
- I := is implemented in intercept lib
- X := is implemented in service
- ? := required treatment Unknown
- - := Not needed
-
- [-] EACCES - Permission to create a socket of the specified type and/or protocol is denied.
- [I] EAFNOSUPPORT - The implementation does not support the specified address family.
- [I] EINVAL - Unknown protocol, or protocol family not available.
- [I] EINVAL - Invalid flags in type.
- [I] EMFILE - Process file table overflow.
- [?] ENFILE - The system limit on the total number of open files has been reached.
- [X] ENOBUFS or ENOMEM - Insufficient memory is available. The socket cannot be created until sufficient resources are freed.
- [?] EPROTONOSUPPORT - The protocol type or the specified protocol is not supported within this domain.
-
- */
-TcpConnection * NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
+TcpConnection * NetconEthernetTap::handleSocket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
{
- struct tcp_pcb *newpcb = lwipstack->tcp_new();
- dwr(MSG_DEBUG," handle_socket(): pcb=%x\n", newpcb);
- if(newpcb != NULL) {
- TcpConnection *new_conn = new TcpConnection();
- *uptr = new_conn;
- new_conn->sock = sock;
- new_conn->pcb = newpcb;
- addConnection(new_conn);
- new_conn->pending = true;
- return new_conn;
+ struct tcp_pcb *newPCB = lwipstack->tcp_new();
+ if(newPCB != NULL) {
+ TcpConnection *newConn = new TcpConnection();
+ *uptr = newConn;
+ newConn->sock = sock;
+ newConn->pcb = newPCB;
+ return addConnection(newConn);;
}
- dwr(MSG_ERROR," handle_socket(): Memory not available for new PCB\n");
- send_return_value(_phy.getDescriptor(sock), -1, ENOMEM);
+ dwr(MSG_ERROR," handleSocket(): Memory not available for new PCB\n");
+ sendReturnValue(_phy.getDescriptor(sock), -1, ENOMEM);
return NULL;
}
-/*
- * Handles an RPC to connect to a given address and port
- *
- * @param PhySocket associated with this RPC connection
- * @param structure containing the data and parameters for this client's RPC
-
- --- Error handling in this method will only catch problems which are immedately
- apprent. Some errors will need to be caught in the nc_connected(0 callback
-
- i := should be implemented in intercept lib
- I := is implemented in intercept lib
- X := is implemented in service
- ? := required treatment Unknown
- - := Not needed
-
- [-] EACCES - For UNIX domain sockets, which are identified by pathname: Write permission is denied ...
- [?] EACCES, EPERM - The user tried to connect to a broadcast address without having the socket broadcast flag enabled ...
- [X] EADDRINUSE - Local address is already in use.
- [I] EAFNOSUPPORT - The passed address didn't have the correct address family in its sa_family field.
- [X] EAGAIN - No more free local ports or insufficient entries in the routing cache.
- [ ] EALREADY - The socket is nonblocking and a previous connection attempt has not yet been completed.
- [IX] EBADF - The file descriptor is not a valid index in the descriptor table.
- [ ] ECONNREFUSED - No-one listening on the remote address.
- [i] EFAULT - The socket structure address is outside the user's address space.
- [ ] EINPROGRESS - The socket is nonblocking and the connection cannot be completed immediately.
- [-] EINTR - The system call was interrupted by a signal that was caught.
- [X] EISCONN - The socket is already connected.
- [X] ENETUNREACH - Network is unreachable.
- [I] ENOTSOCK - The file descriptor is not associated with a socket.
- [X] ETIMEDOUT - Timeout while attempting connection.
-
- [X] EINVAL - Invalid argument, SVr4, generally makes sense to set this
- *
- */
-void NetconEthernetTap::handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpConnection *conn, struct connect_st* connect_rpc)
+void NetconEthernetTap::handleConnect(PhySocket *sock, PhySocket *rpcSock, TcpConnection *conn, struct connect_st* connect_rpc)
{
- dwr(MSG_DEBUG," handle_connect()\n");
- struct sockaddr_in *connaddr;
- connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
- int conn_port = lwipstack->ntohs(connaddr->sin_port);
- ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
+ struct sockaddr_in *rawAddr = (struct sockaddr_in *) &connect_rpc->__addr;
+ int port = lwipstack->ntohs(rawAddr->sin_port);
+ ip_addr_t connAddr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
if(conn != NULL) {
lwipstack->tcp_sent(conn->pcb, nc_sent);
@@ -1260,44 +981,35 @@ void NetconEthernetTap::handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpC
lwipstack->tcp_poll(conn->pcb, nc_poll, APPLICATION_POLL_FREQ);
lwipstack->tcp_arg(conn->pcb, new Larg(this, conn));
- 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;
- dwr(MSG_DEBUG,"handle_write(): %d.%d.%d.%d:\n", d[0],d[1],d[2],d[3]);
-
- dwr(MSG_DEBUG,"handle_connect(): conn_port = %d\n", conn_port);
- int err = 0;
-
- dwr(MSG_DEBUG,"handle_connect(): pcb->state = %x\n", conn->pcb->state);
+ int err = 0, ip = rawAddr->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;
+ dwr(MSG_DEBUG," handleConnect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], port);
+ dwr(MSG_DEBUG," handleConnect(): pcb->state = %x\n", conn->pcb->state);
if(conn->pcb->state != CLOSED) {
- dwr(MSG_DEBUG,"handle_connect(): PCB != CLOSED, cannot connect using this PCB\n");
- send_return_value(rpcsock, -1, EAGAIN);
+ dwr(MSG_DEBUG," handleConnect(): PCB != CLOSED, cannot connect using this PCB\n");
+ sendReturnValue(rpcSock, -1, EAGAIN);
return;
}
-
- if((err = lwipstack->tcp_connect(conn->pcb,&conn_addr,conn_port, nc_connected)) < 0)
+ if((err = lwipstack->tcp_connect(conn->pcb,&connAddr,port,nc_connected)) < 0)
{
if(err == ERR_ISCONN) {
- send_return_value(rpcsock, -1, EISCONN); // Already in connected state
+ sendReturnValue(rpcSock, -1, EISCONN); // Already in connected state
return;
- }
- if(err == ERR_USE) {
- send_return_value(rpcsock, -1, EADDRINUSE); // Already in use
+ } if(err == ERR_USE) {
+ sendReturnValue(rpcSock, -1, EADDRINUSE); // Already in use
return;
- }
- if(err == ERR_VAL) {
- send_return_value(rpcsock, -1, EINVAL); // Invalid ipaddress parameter
+ } if(err == ERR_VAL) {
+ sendReturnValue(rpcSock, -1, EINVAL); // Invalid ipaddress parameter
return;
- }
- if(err == ERR_RTE) {
- send_return_value(rpcsock, -1, ENETUNREACH); // No route to host
+ } if(err == ERR_RTE) {
+ sendReturnValue(rpcSock, -1, ENETUNREACH); // No route to host
return;
- }
- if(err == ERR_BUF) {
- send_return_value(rpcsock, -1, EAGAIN); // No more ports available
+ } if(err == ERR_BUF) {
+ sendReturnValue(rpcSock, -1, EAGAIN); // No more ports available
return;
}
if(err == ERR_MEM) {
@@ -1312,7 +1024,7 @@ void NetconEthernetTap::handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpC
3) Cannot allocate new TCP segment
*/
- send_return_value(rpcsock, -1, EAGAIN); // FIXME: Doesn't describe the problem well, but closest match
+ sendReturnValue(rpcSock, -1, EAGAIN); // FIXME: Doesn't describe the problem well, but closest match
return;
}
@@ -1323,45 +1035,36 @@ void NetconEthernetTap::handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpC
// that's it!
// - Most instances of a retval for a connect() should happen
// in the nc_connect() and nc_err() callbacks!
- dwr(MSG_ERROR," handle_connect(): unable to connect\n");
- send_return_value(rpcsock, -1, EAGAIN);
+ dwr(MSG_ERROR," handleConnect(): unable to connect\n");
+ sendReturnValue(rpcSock, -1, EAGAIN);
}
// Everything seems to be ok, but we don't have enough info to retval
- conn->pending=true;
conn->listening=true;
- conn->rpcsock=rpcsock; // used for return value from lwip CB
- }
- else {
- dwr(MSG_ERROR," handle_connect(): could not locate PCB based on their fd\n");
- send_return_value(rpcsock, -1, EBADF);
+ conn->rpcSock=rpcSock; // used for return value from lwip CB
+ } else {
+ dwr(MSG_ERROR," handleConnect(): could not locate PCB based on their fd\n");
+ sendReturnValue(rpcSock, -1, EBADF);
}
}
-/*
- * Writes data from the application's socket to the LWIP connection
- */
-void NetconEthernetTap::handle_write(TcpConnection *conn)
+void NetconEthernetTap::handleWrite(TcpConnection *conn)
{
- dwr(MSG_DEBUG_EXTRA,"handle_write(): conn->txsz = %d, conn->sock = %x\n", conn->txsz, conn->sock);
- if(!conn) {
- dwr(MSG_ERROR," handle_write(): invalid connection\n");
- return;
- }
- if(!conn->pcb) {
- dwr(MSG_ERROR," handle_write(): conn->pcb == NULL. Failed to write.\n");
+ if(!conn || !conn->pcb) {
+ dwr(MSG_ERROR," handleWrite(): invalid connection/PCB\n");
return;
}
- int err, sz, r, sndbuf = conn->pcb->snd_buf; // How much we are currently allowed to write to the connection
+ // How much we are currently allowed to write to the connection
+ int err, sz, r, sndbuf = conn->pcb->snd_buf;
if(sndbuf == 0) {
/* PCB send buffer is full,turn off readability notifications for the
corresponding PhySocket until nc_sent() is called and confirms that there is
now space on the buffer */
- dwr(MSG_DEBUG," handle_write(): sndbuf == 0, LWIP stack is full\n");
+ dwr(MSG_DEBUG," handleWrite(): sndbuf == 0, LWIP stack is full\n");
_phy.setNotifyReadable(conn->sock, false);
return;
}
if(conn->txsz <= 0) {
- dwr(MSG_DEBUG,"handle_write(): conn->txsz <= 0, nothing in buffer to write\n");
+ dwr(MSG_DEBUG,"handleWrite(): conn->txsz <= 0, nothing in buffer to write\n");
return;
}
if(!conn->listening)
@@ -1369,32 +1072,29 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
if(conn->sock) {
r = conn->txsz < sndbuf ? conn->txsz : sndbuf;
- dwr(MSG_DEBUG,"handle_write(): r = %d, sndbuf = %d\n", r, sndbuf);
/* Writes data pulled from the client's socket buffer to LWIP. This merely sends the
* data to LWIP to be enqueued and eventually sent to the network. */
if(r > 0) {
- // NOTE: this assumes that lwipstack->_lock is locked, either
- // because we are in a callback or have locked it manually.
err = lwipstack->_tcp_write(conn->pcb, &conn->txbuf, r, TCP_WRITE_FLAG_COPY);
lwipstack->_tcp_output(conn->pcb);
if(err != ERR_OK) {
- dwr(MSG_ERROR," handle_write(): error while writing to PCB, (err = %d)\n", err);
+ dwr(MSG_ERROR," handleWrite(): error while writing to PCB, (err = %d)\n", err);
if(err == -1)
- dwr(MSG_DEBUG," handle_write(): possibly out of memory\n");
+ dwr(MSG_DEBUG," handleWrite(): possibly out of memory\n");
return;
- }
- else {
+ } else {
sz = (conn->txsz)-r;
if(sz)
memmove(&conn->txbuf, (conn->txbuf+r), sz);
conn->txsz -= r;
- //fprintf(stderr," TX ---> %d bytes (sz = %d)\n", r, conn->txsz);
+
+ float max = (float)DEFAULT_BUF_SZ;
+ dwr(MSG_TRANSFER," TX ---> :: { TX: %.3f%% | RX: %.3f%% } :: %d bytes\n",
+ (float)conn->txsz / max, (float)conn->rxsz / max, r);
return;
}
}
}
}
-
-
} // namespace ZeroTier
diff --git a/netcon/NetconEthernetTap.hpp b/netcon/NetconEthernetTap.hpp
index 5de458c2..47f50e4c 100644
--- a/netcon/NetconEthernetTap.hpp
+++ b/netcon/NetconEthernetTap.hpp
@@ -46,6 +46,8 @@
#include "netif/etharp.h"
+#include "RPC.h"
+
struct tcp_pcb;
struct socket_st;
struct listen_st;
@@ -102,44 +104,312 @@ public:
private:
// LWIP callbacks
- static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
- static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
- static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
+ // NOTE: these are called from within LWIP, meaning that lwipstack->_lock is ALREADY
+ // locked in this case!
+
+ /*
+ * Callback from LWIP for when a connection has been accepted and the PCB has been
+ * put into an ACCEPT state.
+ *
+ * A socketpair is created, one end is kept and wrapped into a PhySocket object
+ * for use in the main ZT I/O loop, and one end is sent to the client. The client
+ * is then required to tell the service what new file descriptor it has allocated
+ * for this connection. After the mapping is complete, the accepted socket can be
+ * used.
+ *
+ * @param associated service state object
+ * @param newly allocated PCB
+ * @param error code
+ * @return ERR_OK if everything is ok, -1 otherwise
+ *
+ * i := should be implemented in intercept lib
+ * I := is implemented in intercept lib
+ * X := is implemented in service
+ * ? := required treatment Unknown
+ * - := Not needed
+ *
+ * [ ] EAGAIN or EWOULDBLOCK - The socket is marked nonblocking and no connections are present
+ * to be accepted. POSIX.1-2001 allows either error to be returned for
+ * this case, and does not require these constants to have the same value,
+ * so a portable application should check for both possibilities.
+ * [I] EBADF - The descriptor is invalid.
+ * [I] ECONNABORTED - A connection has been aborted.
+ * [i] EFAULT - The addr argument is not in a writable part of the user address space.
+ * [-] EINTR - The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
+ * [I] EINVAL - Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
+ * [I] EINVAL - (accept4()) invalid value in flags.
+ * [I] EMFILE - The per-process limit of open file descriptors has been reached.
+ * [ ] ENFILE - The system limit on the total number of open files has been reached.
+ * [ ] ENOBUFS, ENOMEM - Not enough free memory. This often means that the memory allocation is
+ * limited by the socket buffer limits, not by the system memory.
+ * [I] ENOTSOCK - The descriptor references a file, not a socket.
+ * [I] EOPNOTSUPP - The referenced socket is not of type SOCK_STREAM.
+ * [ ] EPROTO - Protocol error.
+ *
+ */
+ static err_t nc_accept(void *arg, struct tcp_pcb *newPCB, err_t err);
+
+ /*
+ * Callback from LWIP for when data is available to be read from the network.
+ *
+ * Data is in the form of a linked list of struct pbufs, it is then recombined and
+ * send to the client over the associated unix socket.
+ *
+ * @param associated service state object
+ * @param allocated PCB
+ * @param chain of pbufs
+ * @param error code
+ * @return ERR_OK if everything is ok, -1 otherwise
+ *
+ */
+ static err_t nc_recved(void *arg, struct tcp_pcb *PCB, struct pbuf *p, err_t err);
+
+ /*
+ * Callback from LWIP when an internal error is associtated with the given (arg)
+ *
+ * Since the PCB related to this error might no longer exist, only its perviously
+ * associated (arg) is provided to us.
+ *
+ * @param associated service state object
+ * @param error code
+ *
+ */
static void nc_err(void *arg, err_t err);
- static void nc_close(struct tcp_pcb *tpcb);
- static err_t nc_send(struct tcp_pcb *tpcb);
- static err_t nc_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
- 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 (magic[sizeof(uint64_t)]), char &cmd, void* &payload);
-
- void handle_getsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
- void handle_bind(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct bind_st *bind_rpc);
- void handle_listen(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct listen_st *listen_rpc);
- TcpConnection * handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc);
- void handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpConnection *conn, struct connect_st* connect_rpc);
- void handle_write(TcpConnection *conn);
-
- int send_return_value(PhySocket *sock, int retval, int _errno);
- int send_return_value(int fd, int retval, int _errno);
+ /*
+ * Callback from LWIP to do whatever work we might need to do.
+ *
+ * @param associated service state object
+ * @param PCB we're polling on
+ * @return ERR_OK if everything is ok, -1 otherwise
+ *
+ */
+ static err_t nc_poll(void* arg, struct tcp_pcb *PCB);
+
+ /*
+ * Callback from LWIP to signal that 'len' bytes have successfully been sent.
+ * As a result, we should put our socket back into a notify-on-readability state
+ * since there is now room on the PCB buffer to write to.
+ *
+ * NOTE: This could be used to track the amount of data sent by a connection.
+ *
+ * @param associated service state object
+ * @param relevant PCB
+ * @param length of data sent
+ * @return ERR_OK if everything is ok, -1 otherwise
+ *
+ */
+ static err_t nc_sent(void *arg, struct tcp_pcb *PCB, u16_t len);
+
+ /*
+ * Callback from LWIP which sends a return value to the client to signal that
+ * a connection was established for this PCB
+ *
+ * @param associated service state object
+ * @param relevant PCB
+ * @param error code
+ * @return ERR_OK if everything is ok, -1 otherwise
+ *
+ */
+ static err_t nc_connected(void *arg, struct tcp_pcb *PCB, err_t err);
+
+ //static void nc_close(struct tcp_pcb *PCB);
+ //static err_t nc_send(struct tcp_pcb *PCB);
+
+ /*
+ * Handles an RPC to bind an LWIP PCB to a given address and port
+ *
+ * @param PhySocket associated with this RPC connection
+ * @param structure containing the data and parameters for this client's RPC
+ *
+
+ i := should be implemented in intercept lib
+ I := is implemented in intercept lib
+ X := is implemented in service
+ ? := required treatment Unknown
+ - := Not needed
+
+ [ ] EACCES - The address is protected, and the user is not the superuser.
+ [X] EADDRINUSE - The given address is already in use.
+ [I] EBADF - sockfd is not a valid descriptor.
+ [X] EINVAL - The socket is already bound to an address.
+ [I] ENOTSOCK - sockfd is a descriptor for a file, not a socket.
+
+ [X] ENOMEM - Insufficient kernel memory was available.
+
+ - The following errors are specific to UNIX domain (AF_UNIX) sockets:
+
+ [-] EACCES - Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
+ [-] EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local.
+ [-] EFAULT - addr points outside the user's accessible address space.
+ [-] EINVAL - The addrlen is wrong, or the socket was not in the AF_UNIX family.
+ [-] ELOOP - Too many symbolic links were encountered in resolving addr.
+ [-] ENAMETOOLONG - s addr is too long.
+ [-] ENOENT - The file does not exist.
+ [-] ENOTDIR - A component of the path prefix is not a directory.
+ [-] EROFS - The socket inode would reside on a read-only file system.
+ */
+ void handleBind(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct bind_st *bind_rpc);
+
+ /*
+ * Handles an RPC to put an LWIP PCB into LISTEN mode
+ *
+ * @param PhySocket associated with this RPC connection
+ * @param structure containing the data and parameters for this client's RPC
+ *
+
+ i := should be implemented in intercept lib
+ I := is implemented in intercept lib
+ X := is implemented in service
+ ? := required treatment Unknown
+ - := Not needed
+
+ [?] EADDRINUSE - Another socket is already listening on the same port.
+ [IX] EBADF - The argument sockfd is not a valid descriptor.
+ [I] ENOTSOCK - The argument sockfd is not a socket.
+ [I] EOPNOTSUPP - The socket is not of a type that supports the listen() operation.
+ */
+ void handleListen(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct listen_st *listen_rpc);
+
+ /*
+ * Handles an RPC to create a socket (LWIP PCB and associated socketpair)
+ *
+ * A socketpair is created, one end is kept and wrapped into a PhySocket object
+ * for use in the main ZT I/O loop, and one end is sent to the client. The client
+ * is then required to tell the service what new file descriptor it has allocated
+ * for this connection. After the mapping is complete, the socket can be used.
+ *
+ * @param PhySocket associated with this RPC connection
+ * @param structure containing the data and parameters for this client's RPC
+ *
+
+ i := should be implemented in intercept lib
+ I := is implemented in intercept lib
+ X := is implemented in service
+ ? := required treatment Unknown
+ - := Not needed
+
+ [-] EACCES - Permission to create a socket of the specified type and/or protocol is denied.
+ [I] EAFNOSUPPORT - The implementation does not support the specified address family.
+ [I] EINVAL - Unknown protocol, or protocol family not available.
+ [I] EINVAL - Invalid flags in type.
+ [I] EMFILE - Process file table overflow.
+ [?] ENFILE - The system limit on the total number of open files has been reached.
+ [X] ENOBUFS or ENOMEM - Insufficient memory is available. The socket cannot be created until sufficient resources are freed.
+ [?] EPROTONOSUPPORT - The protocol type or the specified protocol is not supported within this domain.
+ */
+ TcpConnection * handleSocket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc);
+
+ /*
+ * Handles an RPC to connect to a given address and port
+ *
+ * @param PhySocket associated with this RPC connection
+ * @param structure containing the data and parameters for this client's RPC
+
+ --- Error handling in this method will only catch problems which are immedately
+ apprent. Some errors will need to be caught in the nc_connected(0 callback
+
+ i := should be implemented in intercept lib
+ I := is implemented in intercept lib
+ X := is implemented in service
+ ? := required treatment Unknown
+ - := Not needed
+
+ [-] EACCES - For UNIX domain sockets, which are identified by pathname: Write permission is denied ...
+ [?] EACCES, EPERM - The user tried to connect to a broadcast address without having the socket broadcast flag enabled ...
+ [X] EADDRINUSE - Local address is already in use.
+ [I] EAFNOSUPPORT - The passed address didn't have the correct address family in its sa_family field.
+ [X] EAGAIN - No more free local ports or insufficient entries in the routing cache.
+ [ ] EALREADY - The socket is nonblocking and a previous connection attempt has not yet been completed.
+ [IX] EBADF - The file descriptor is not a valid index in the descriptor table.
+ [ ] ECONNREFUSED - No-one listening on the remote address.
+ [i] EFAULT - The socket structure address is outside the user's address space.
+ [ ] EINPROGRESS - The socket is nonblocking and the connection cannot be completed immediately.
+ [-] EINTR - The system call was interrupted by a signal that was caught.
+ [X] EISCONN - The socket is already connected.
+ [X] ENETUNREACH - Network is unreachable.
+ [I] ENOTSOCK - The file descriptor is not associated with a socket.
+ [X] ETIMEDOUT - Timeout while attempting connection.
+
+ [X] EINVAL - Invalid argument, SVr4, generally makes sense to set this
+ */
+ void handleConnect(PhySocket *sock, PhySocket *rpcsock, TcpConnection *conn, struct connect_st* connect_rpc);
+
+ /*
+ * Return the address that the socket is bound to
+ */
+ void handleGetsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
+
+ /*
+ * Writes data from the application's socket to the LWIP connection
+ */
+ void handleWrite(TcpConnection *conn);
+
+ /*
+ * Sends a return value to the intercepted application
+ */
+ int sendReturnValue(PhySocket *sock, int retval, int _errno);
+ int sendReturnValue(int fd, int retval, int _errno);
+
+ /*
+ * Unpacks the buffer from an RPC command
+ */
+ void unloadRPC(void *data, pid_t &pid, pid_t &tid,
+ int &rpc_count, char (timestamp[RPC_TIMESTAMP_SZ]), char (magic[sizeof(uint64_t)]), char &cmd, void* &payload);
+
+ // Unused -- no UDP or TCP from this thread/Phy<>
void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
void phyOnTcpClose(PhySocket *sock,void **uptr);
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
void phyOnTcpWritable(PhySocket *sock,void **uptr);
+
+ /*
+ * Add a new PhySocket for the client connections
+ */
void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
+
+ /*
+ * Signals us to close the TcpConnection associated with this PhySocket
+ */
void phyOnUnixClose(PhySocket *sock,void **uptr);
+
+ /*
+ * Notifies us that there is data to be read from an application's socket
+ */
void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
+
+ /*
+ * Notifies us that we can write to an application's socket
+ */
void phyOnUnixWritable(PhySocket *sock,void **uptr);
+
+ /*
+ * Handles data on a application's data buffer. Data is sent to LWIP to be enqueued.
+ * TODO: This is a candidate for removal now that phyOnUnixData() is used for everything
+ */
void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable);
+ /*
+ * Returns a pointer to a TcpConnection associated with a given PhySocket
+ */
TcpConnection *getConnection(PhySocket *sock);
- void addConnection(TcpConnection *conn);
+
+ /*
+ * Safely adds a new TcpConnection to _TcpConnections
+ */
+ TcpConnection *addConnection(TcpConnection *conn);
+
+ /*
+ * Safely removes a TcpConnection from _TcpConnections
+ */
void removeConnection(TcpConnection *conn);
+
+ /*
+ * Closes a TcpConnection, associated LWIP PCB strcuture,
+ * PhySocket, and underlying file descriptor
+ */
void closeConnection(PhySocket *sock);
ip_addr_t convert_ip(struct sockaddr_in * addr)
@@ -157,12 +427,10 @@ private:
Phy<NetconEthernetTap *> _phy;
PhySocket *_unixListenSocket;
- std::vector<TcpConnection*> tcp_connections;
- std::map<PhySocket*, pid_t> pidmap;
-
+ std::vector<TcpConnection*> _TcpConnections;
std::map<uint64_t, std::pair<PhySocket*, void*> > jobmap;
- pid_t rpc_counter;
+ pid_t rpcCounter;
netif interface;
MAC _mac;
@@ -174,7 +442,7 @@ private:
Mutex _multicastGroups_m;
std::vector<InetAddress> _ips;
- Mutex _ips_m, _tcpconns_m, _rx_buf_m, _tx_buf_m;
+ Mutex _ips_m, _tcpconns_m, _rx_buf_m;
unsigned int _mtu;
volatile bool _enabled;
diff --git a/netcon/RPC.c b/netcon/RPC.c
index 17aa650c..a6965892 100644
--- a/netcon/RPC.c
+++ b/netcon/RPC.c
@@ -113,22 +113,22 @@ int rpc_join(const char * sockname)
int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
{
pthread_mutex_lock(&lock);
- char c, padding[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
- char cmdbuf[BUF_SZ], CANARY[TOKEN_SIZE], metabuf[BUF_SZ];
- memcpy(CANARY+CANARY_SIZE, padding, sizeof(padding));
+ char c, padding[] = {PADDING};
+ char cmdbuf[BUF_SZ], CANARY[CANARY_SZ+PADDING_SZ], metabuf[BUF_SZ];
+ memcpy(CANARY+CANARY_SZ, padding, sizeof(padding));
uint64_t canary_num;
// ephemeral RPC socket used only for this command
int rpc_sock = rpc_join(path);
// Generate token
int fdrand = open("/dev/urandom", O_RDONLY);
- if(read(fdrand, &CANARY, CANARY_SIZE) < 0) {
+ if(read(fdrand, &CANARY, CANARY_SZ) < 0) {
fprintf(stderr,"unable to read from /dev/urandom for RPC canary data\n");
return -1;
}
- memcpy(&canary_num, CANARY, CANARY_SIZE);
+ memcpy(&canary_num, CANARY, CANARY_SZ);
cmdbuf[CMD_ID_IDX] = cmd;
- memcpy(&cmdbuf[CANARY_IDX], &canary_num, CANARY_SIZE);
+ memcpy(&cmdbuf[CANARY_IDX], &canary_num, CANARY_SZ);
memcpy(&cmdbuf[STRUCT_IDX], data, len);
#ifdef VERBOSE
@@ -140,7 +140,7 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
time_t timestamp;
timestamp = time(NULL);
strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
- memcpy(metabuf, RPC_PHRASE, RPC_PHRASE_SIZE); // Write signal phrase
+ memcpy(metabuf, RPC_PHRASE, RPC_PHRASE_SZ); // Write signal phrase
memcpy(&metabuf[IDX_PID], &pid, sizeof(pid_t) ); /* pid */
memcpy(&metabuf[IDX_TID], &tid, sizeof(pid_t) ); /* tid */
@@ -148,7 +148,7 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
memcpy(&metabuf[IDX_TIME], &timestring, 20 ); /* timestamp */
#endif
/* Combine command flag+payload with RPC metadata */
- memcpy(&metabuf[IDX_PAYLOAD], cmdbuf, len + 1 + CANARY_SIZE);
+ memcpy(&metabuf[IDX_PAYLOAD], cmdbuf, len + 1 + CANARY_SZ);
// Write RPC
int n_write = write(rpc_sock, &metabuf, BUF_SZ);
@@ -162,7 +162,7 @@ int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
return -1;
}
if(c == 'z' && n_write > 0 && forfd > -1){
- if(send(forfd, &CANARY, TOKEN_SIZE, 0) < 0) {
+ if(send(forfd, &CANARY, CANARY_SZ+PADDING_SZ, 0) < 0) {
fprintf(stderr,"unable to write canary to stream\n");
return -1;
}
diff --git a/netcon/RPC.h b/netcon/RPC.h
index c4284159..ce09ef90 100644
--- a/netcon/RPC.h
+++ b/netcon/RPC.h
@@ -3,48 +3,43 @@
#include <stdint.h>
-#define CANARY_SIZE sizeof(uint64_t)
-#define CANARY_PADDING_SIZE 12
-#define TOKEN_SIZE CANARY_SIZE+CANARY_PADDING_SIZE
+#define CANARY_SZ sizeof(uint64_t)
+#define PADDING_SZ 12
+#define PADDING 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
-#define RPC_PHRASE "zerotier\0"
-#define RPC_PHRASE_SIZE 9
+#define RPC_PHRASE "zerotier\0"
+#define RPC_PHRASE_SZ 9
+#define RPC_TIMESTAMP_SZ 20
// 1st RPC section (metdata)
-#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 */
+#define IDX_SIGNAL_PHRASE 0
+#define IDX_PID IDX_SIGNAL_PHRASE + RPC_PHRASE_SZ
+#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 + RPC_TIMESTAMP_SZ
// 2nd RPC section (payload and canary)
-#define CMD_ID_IDX 0
-#define CANARY_IDX 1
-#define STRUCT_IDX CANARY_IDX+CANARY_SIZE
+#define CMD_ID_IDX 0
+#define CANARY_IDX 1
+#define STRUCT_IDX CANARY_IDX+CANARY_SZ
-#define BUF_SZ 512
+#define BUF_SZ 512
-#define ERR_OK 0
+#define ERR_OK 0
/* RPC codes */
-#define RPC_UNDEFINED 0
-#define RPC_CONNECT 1
-#define RPC_CONNECT_SOCKARG 2
-#define RPC_CLOSE 3
-#define RPC_READ 4
-#define RPC_WRITE 5
-#define RPC_BIND 6
-#define RPC_ACCEPT 7
-#define RPC_LISTEN 8
-#define RPC_SOCKET 9
-#define RPC_SHUTDOWN 10
-#define RPC_GETSOCKNAME 11
-
-/* Administration RPC codes */
-#define RPC_MAP 20 /* Give the service the value we "see" for the new buffer fd */
-#define RPC_MAP_REQ 21 /* A call to determine whether an fd is mapped to the service */
-#define RPC_RETVAL 22 /* not RPC per se, but something we should codify */
-#define RPC_KILL_INTERCEPT 23 /* Tells the service we need to shut down all connections */
-
+#define RPC_UNDEFINED 0
+#define RPC_CONNECT 1
+#define RPC_CONNECT_SOCKARG 2
+#define RPC_CLOSE 3
+#define RPC_READ 4
+#define RPC_WRITE 5
+#define RPC_BIND 6
+#define RPC_ACCEPT 7
+#define RPC_LISTEN 8
+#define RPC_SOCKET 9
+#define RPC_SHUTDOWN 10
+#define RPC_GETSOCKNAME 11
+#define RPC_RETVAL 22
#ifdef __cplusplus
extern "C" {
diff --git a/netcon/common.inc.c b/netcon/common.inc.c
index 858a2195..6468dac2 100644
--- a/netcon/common.inc.c
+++ b/netcon/common.inc.c
@@ -44,11 +44,11 @@
#define DEBUG_LEVEL 0
-#define MSG_WARNING 4
-#define MSG_ERROR 1 // Errors
-#define MSG_INFO 2 // Information which is generally useful to any user
-#define MSG_DEBUG 3 // Information which is only useful to someone debugging
-#define MSG_DEBUG_EXTRA 4 // If nothing in your world makes sense
+#define MSG_TRANSFER 1 // RX/TX specific statements
+#define MSG_ERROR 2 // Errors
+#define MSG_INFO 3 // Information which is generally useful to any user
+#define MSG_DEBUG 4 // Information which is only useful to someone debugging
+#define MSG_DEBUG_EXTRA 5 // If nothing in your world makes sense
#ifdef NETCON_INTERCEPT