mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 03:53:44 +02:00
refactor in progress
This commit is contained in:
parent
e391bc004b
commit
b11d429bc0
3 changed files with 33 additions and 65 deletions
|
@ -159,7 +159,7 @@ void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,s
|
||||||
NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
|
NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
|
||||||
{
|
{
|
||||||
NetconConnection *c;
|
NetconConnection *c;
|
||||||
for(int i=0; i<clients.size(); i++) {
|
for(size_t i=0; i<clients.size(); i++) {
|
||||||
c = clients[i]->containsPCB(pcb);
|
c = clients[i]->containsPCB(pcb);
|
||||||
if(c) {
|
if(c) {
|
||||||
return c;
|
return c;
|
||||||
|
@ -170,12 +170,11 @@ NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
|
||||||
|
|
||||||
NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
|
NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
|
||||||
{
|
{
|
||||||
NetconConnection *c;
|
for(size_t i=0; i<clients.size(); i++) {
|
||||||
for(int i=0; i<clients.size(); i++) {
|
for(size_t j=0; j<clients[i]->connections.size(); j++) {
|
||||||
for(int j=0; j<clients[i]->connections[j].size(); j++) {
|
if(_phy.getDescriptor(clients[i]->connections[j]->sock) == fd) {
|
||||||
if(_phy.getDescriptor(clients[i]->connection[j]->sock) == fd) {
|
return clients[i]->connections[j];
|
||||||
return clients[i]->connections[j];
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -183,8 +182,8 @@ NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
|
||||||
|
|
||||||
NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
|
NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
|
||||||
{
|
{
|
||||||
for(int i=0; i<clients.size(); i++) {
|
for(size_t i=0; i<clients.size(); i++) {
|
||||||
if(clients[i].containsPCB(pcb)) {
|
if(clients[i]->containsPCB(pcb)) {
|
||||||
return clients[i];
|
return clients[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,7 +211,6 @@ void NetconEthernetTap::threadMain()
|
||||||
strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
|
strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
|
||||||
|
|
||||||
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
|
unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
|
||||||
unsigned long ipreass_time = TCP_TMR_INTERVAL / 1000;
|
|
||||||
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
|
unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
|
||||||
unsigned long prev_tcp_time = 0;
|
unsigned long prev_tcp_time = 0;
|
||||||
unsigned long prev_etharp_time = 0;
|
unsigned long prev_etharp_time = 0;
|
||||||
|
@ -243,8 +241,8 @@ void NetconEthernetTap::threadMain()
|
||||||
lwipstack->etharp_tmr();
|
lwipstack->etharp_tmr();
|
||||||
}
|
}
|
||||||
// should be set every time since tv_sel is modified after each select() call
|
// should be set every time since tv_sel is modified after each select() call
|
||||||
tv_sel.tv_sec = 0;
|
//tv_sel.tv_sec = 0;
|
||||||
tv_sel.tv_usec = min_time;
|
//tv_sel.tv_usec = min_time;
|
||||||
|
|
||||||
_phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check
|
_phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check
|
||||||
}
|
}
|
||||||
|
@ -264,22 +262,24 @@ void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
|
||||||
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
|
void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
|
||||||
{
|
{
|
||||||
NetconClient *newClient = new NetconClient();
|
NetconClient *newClient = new NetconClient();
|
||||||
newClient->addConnection(NetconConnectionType.RPC, *uptrN)
|
newClient->addConnection(RPC, *uptrN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
|
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
|
||||||
{
|
{
|
||||||
*uptr->close();
|
((NetconClient*)*uptr)->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
|
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
|
||||||
{
|
{
|
||||||
NetconConnection *c = *uptr->getConnection(sock);
|
unsigned char *buf = (unsigned char*)data;
|
||||||
|
|
||||||
|
NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
|
||||||
int r;
|
int r;
|
||||||
if(c->type == NetconConnectionType.BUFFER) {
|
if(c->type == BUFFER) {
|
||||||
if(c) {
|
if(c) {
|
||||||
if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
|
if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
|
||||||
if((r = read(sws->sock, (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
|
if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
|
||||||
c->idx += r;
|
c->idx += r;
|
||||||
handle_write(c);
|
handle_write(c);
|
||||||
}
|
}
|
||||||
|
@ -289,26 +289,26 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
|
||||||
// can't find connection for this fd
|
// can't find connection for this fd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(c->type == NetconConnectionType.RPC)
|
if(c->type == RPC)
|
||||||
{
|
{
|
||||||
NetconClient *client = (NetconClient*)*uptr;
|
NetconClient *client = (NetconClient*)*uptr;
|
||||||
switch((unsigned char*)data[0])
|
switch(buf[0])
|
||||||
{
|
{
|
||||||
case RPC_SOCKET:
|
case RPC_SOCKET:
|
||||||
struct socket_st socket_rpc;
|
struct socket_st socket_rpc;
|
||||||
memcpy(&socket_rpc, &data[1], sizeof(struct socket_st));
|
memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st));
|
||||||
client->tid = socket_rpc.__tid;
|
client->tid = socket_rpc.__tid;
|
||||||
handle_socket(client, &socket_rpc);
|
handle_socket(client, &socket_rpc);
|
||||||
break;
|
break;
|
||||||
case RPC_LISTEN:
|
case RPC_LISTEN:
|
||||||
struct listen_st listen_rpc;
|
struct listen_st listen_rpc;
|
||||||
memcpy(&listen_rpc, &data[1], sizeof(struct listen_st));
|
memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st));
|
||||||
client->tid = listen_rpc.__tid;
|
client->tid = listen_rpc.__tid;
|
||||||
handle_listen(client, &listen_rpc);
|
handle_listen(client, &listen_rpc);
|
||||||
break;
|
break;
|
||||||
case RPC_BIND:
|
case RPC_BIND:
|
||||||
struct bind_st bind_rpc;
|
struct bind_st bind_rpc;
|
||||||
memcpy(&bind_rpc, &data[1], sizeof(struct bind_st));
|
memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st));
|
||||||
client->tid = bind_rpc.__tid;
|
client->tid = bind_rpc.__tid;
|
||||||
handle_bind(client, &bind_rpc);
|
handle_bind(client, &bind_rpc);
|
||||||
break;
|
break;
|
||||||
|
@ -317,12 +317,12 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
|
||||||
break;
|
break;
|
||||||
case RPC_CONNECT:
|
case RPC_CONNECT:
|
||||||
struct connect_st connect_rpc;
|
struct connect_st connect_rpc;
|
||||||
memcpy(&connect_rpc, &data[1], sizeof(struct connect_st));
|
memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st));
|
||||||
client->tid = connect_rpc.__tid;
|
client->tid = connect_rpc.__tid;
|
||||||
handle_connect(client, &connect_rpc);
|
handle_connect(client, &connect_rpc);
|
||||||
break;
|
break;
|
||||||
case RPC_FD_MAP_COMPLETION:
|
case RPC_FD_MAP_COMPLETION:
|
||||||
handle_retval(client, data);
|
handle_retval(client, buf);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -369,39 +369,7 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
err_t NetconEthernetTap::nc_accept(void* arg, struct tcp_pcb *newpcb, err_t err)
|
|
||||||
{
|
|
||||||
NetconClient *client = getConnectionByThisFD((intptr_t)arg);
|
|
||||||
|
|
||||||
if(client)
|
|
||||||
{
|
|
||||||
int *their_fd;
|
|
||||||
int our_fd;
|
|
||||||
PhySocket *new_sock = _phy.createSocketPair(*their_fd, client)
|
|
||||||
NetconConnection c = addConnection(NetconConnectionType.BUFFER, new_sock);
|
|
||||||
our_fd = _phy.getDescriptor(c->sock);
|
|
||||||
if(c == NULL) return -1;
|
|
||||||
|
|
||||||
c->owner->unmapped_conn = c;
|
|
||||||
// write byte to let accept call know we have a new connection
|
|
||||||
int n = write(our_fd, "z", 1);
|
|
||||||
if(n > 0) {
|
|
||||||
sock_fd_write(c->owner->rpc, *their_fd);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// unknown error writing signal byte to listening socket
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
lwipstack->tcp_arg(newpcb, (void*)(intptr_t)(our_fd));
|
|
||||||
lwipstack->tcp_recv(newpcb, nc_recved);
|
|
||||||
lwipstack->tcp_err(newpcb, nc_err);
|
|
||||||
lwipstack->tcp_sent(newpcb, nc_sent);
|
|
||||||
lwipstack->tcp_poll(newpcb, nc_poll, APPLICATION_POLL_FREQ);
|
|
||||||
tcp_accepted(c->pcb);
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 *tpcb, struct pbuf *p, err_t err)
|
||||||
{
|
{
|
||||||
|
@ -578,7 +546,7 @@ void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* so
|
||||||
struct tcp_pcb *pcb = lwipstack->tcp_new();
|
struct tcp_pcb *pcb = lwipstack->tcp_new();
|
||||||
if(pcb != NULL) {
|
if(pcb != NULL) {
|
||||||
int *their_fd;
|
int *their_fd;
|
||||||
NetconConnection *new_conn = client->addConnection(NetconConnectionType.BUFFER, _phy.createSocketPair(*their_fd, client));
|
NetconConnection *new_conn = client->addConnection(BUFFER, _phy.createSocketPair(*their_fd, client));
|
||||||
new_conn->their_fd = *their_fd;
|
new_conn->their_fd = *their_fd;
|
||||||
new_conn->pcb = pcb;
|
new_conn->pcb = pcb;
|
||||||
sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
|
sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
|
||||||
|
|
|
@ -63,19 +63,19 @@ namespace ZeroTier {
|
||||||
//
|
//
|
||||||
class NetconClient
|
class NetconClient
|
||||||
{
|
{
|
||||||
private:
|
public:
|
||||||
vector<NetconConnection*> connections;
|
vector<NetconConnection*> connections;
|
||||||
|
|
||||||
public:
|
|
||||||
int tid;
|
int tid;
|
||||||
bool waiting_for_retval;
|
bool waiting_for_retval;
|
||||||
NetconConnection *rpc;
|
NetconConnection *rpc;
|
||||||
NetconConnection* unmapped_conn;
|
NetconConnection* unmapped_conn;
|
||||||
|
|
||||||
void addConnection(NetconConnectionType type, PhySocket *sock)
|
NetconConnection *addConnection(NetconConnectionType type, PhySocket *sock)
|
||||||
{
|
{
|
||||||
NetconConnection *new_conn = new NetconConnection(type, sock);
|
NetconConnection *new_conn = new NetconConnection(type, sock);
|
||||||
new_conn->owner = this;
|
new_conn->owner = this;
|
||||||
|
return new_conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check data and RPC connections
|
// Check data and RPC connections
|
||||||
|
@ -84,7 +84,7 @@ namespace ZeroTier {
|
||||||
if(sock && sock == rpc->sock) {
|
if(sock && sock == rpc->sock) {
|
||||||
return rpc;
|
return rpc;
|
||||||
}
|
}
|
||||||
for(int i=0; i<connections.size(); i++) {
|
for(size_t i=0; i<connections.size(); i++) {
|
||||||
if(sock == connections[i]->sock) { return connections[i];}
|
if(sock == connections[i]->sock) { return connections[i];}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -93,7 +93,7 @@ namespace ZeroTier {
|
||||||
//
|
//
|
||||||
NetconConnection *getConnectionByTheirFD(int fd)
|
NetconConnection *getConnectionByTheirFD(int fd)
|
||||||
{
|
{
|
||||||
for(int i=0; i<connections.size(); i++) {
|
for(size_t i=0; i<connections.size(); i++) {
|
||||||
if(connections[i]->their_fd == fd) { return connections[i]; }
|
if(connections[i]->their_fd == fd) { return connections[i]; }
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -102,7 +102,7 @@ namespace ZeroTier {
|
||||||
//
|
//
|
||||||
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
|
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
|
||||||
{
|
{
|
||||||
for(int i=0; i<connections.size(); i++) {
|
for(size_t i=0; i<connections.size(); i++) {
|
||||||
if(connections[i]->pcb = pcb) { return connections[i]; }
|
if(connections[i]->pcb = pcb) { return connections[i]; }
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -110,7 +110,7 @@ namespace ZeroTier {
|
||||||
|
|
||||||
NetconConnection *containsPCB(struct tcp_pcb *pcb)
|
NetconConnection *containsPCB(struct tcp_pcb *pcb)
|
||||||
{
|
{
|
||||||
for(int i=0; i<connections.size(); i++) {
|
for(size_t i=0; i<connections.size(); i++) {
|
||||||
if(connections[i]->pcb = pcb) { return connections[i]; }
|
if(connections[i]->pcb = pcb) { return connections[i]; }
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -289,7 +289,7 @@ public:
|
||||||
* @param uptr Pointer to associate with local side of socket pair
|
* @param uptr Pointer to associate with local side of socket pair
|
||||||
* @return PhySocket for local side of socket pair
|
* @return PhySocket for local side of socket pair
|
||||||
*/
|
*/
|
||||||
inline PhySocket *createSocketPair(ZT_PHY_SOCKFD_TYPE &remoteSocketDescriptor,void *uptr = (void *)0)
|
static inline PhySocket *createSocketPair(ZT_PHY_SOCKFD_TYPE &remoteSocketDescriptor,void *uptr = (void *)0)
|
||||||
{
|
{
|
||||||
if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
|
if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
|
||||||
return (PhySocket *)0;
|
return (PhySocket *)0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue