NetconEthernetTap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifdef ZT_ENABLE_NETCON
  28. #include <algorithm>
  29. #include <utility>
  30. #include <dlfcn.h>
  31. #include "NetconEthernetTap.hpp"
  32. #include "../node/Utils.hpp"
  33. #include "../osdep/OSUtils.hpp"
  34. #include "../osdep/Phy.hpp"
  35. #include "lwip/tcp_impl.h"
  36. #include "netif/etharp.h"
  37. #include "lwip/ip.h"
  38. #include "lwip/ip_addr.h"
  39. #include "lwip/ip_frag.h"
  40. #include "LWIPStack.hpp"
  41. #include "NetconService.hpp"
  42. #include "Intercept.h"
  43. #include "NetconUtilities.hpp"
  44. #define APPLICATION_POLL_FREQ 1
  45. namespace ZeroTier {
  46. NetconEthernetTap::NetconEthernetTap(
  47. const char *homePath,
  48. const MAC &mac,
  49. unsigned int mtu,
  50. unsigned int metric,
  51. uint64_t nwid,
  52. const char *friendlyName,
  53. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  54. void *arg) :
  55. _phy(this,false,true),
  56. _unixListenSocket((PhySocket *)0),
  57. _handler(handler),
  58. _arg(arg),
  59. _nwid(nwid),
  60. _homePath(homePath),
  61. _mtu(mtu),
  62. _enabled(true),
  63. _run(true)
  64. {
  65. char sockPath[4096];
  66. Utils::snprintf(sockPath,sizeof(sockPath),"/tmp/.ztnc_%.16llx",(unsigned long long)nwid);
  67. _dev = sockPath;
  68. lwipstack = new LWIPStack("/root/dev/netcon/liblwip.so");
  69. if(!lwipstack) // TODO double check this check
  70. throw std::runtime_error("unable to load lwip lib.");
  71. lwipstack->lwip_init();
  72. _unixListenSocket = _phy.unixListen(sockPath,(void *)this);
  73. if (!_unixListenSocket)
  74. throw std::runtime_error(std::string("unable to bind to ")+sockPath);
  75. _thread = Thread::start(this);
  76. }
  77. NetconEthernetTap::~NetconEthernetTap()
  78. {
  79. _run = false;
  80. _phy.whack();
  81. _phy.whack();
  82. Thread::join(_thread);
  83. _phy.close(_unixListenSocket,false);
  84. }
  85. void NetconEthernetTap::setEnabled(bool en)
  86. {
  87. _enabled = en;
  88. }
  89. bool NetconEthernetTap::enabled() const
  90. {
  91. return _enabled;
  92. }
  93. bool NetconEthernetTap::addIp(const InetAddress &ip)
  94. {
  95. Mutex::Lock _l(_ips_m);
  96. if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
  97. _ips.push_back(ip);
  98. std::sort(_ips.begin(),_ips.end());
  99. // TODO: alloc IP in LWIP
  100. //netif_set_addr(netif, ipaddr, netmask, gw);
  101. }
  102. return true; // TODO: what is exapected?
  103. }
  104. bool NetconEthernetTap::removeIp(const InetAddress &ip)
  105. {
  106. Mutex::Lock _l(_ips_m);
  107. std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
  108. if (i == _ips.end())
  109. return false;
  110. _ips.erase(i);
  111. // TODO: dealloc IP from LWIP
  112. return true;
  113. }
  114. std::vector<InetAddress> NetconEthernetTap::ips() const
  115. {
  116. Mutex::Lock _l(_ips_m);
  117. return _ips;
  118. }
  119. void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  120. {
  121. if (!_enabled)
  122. return;
  123. }
  124. std::string NetconEthernetTap::deviceName() const
  125. {
  126. return _dev;
  127. }
  128. void NetconEthernetTap::setFriendlyName(const char *friendlyName)
  129. {
  130. }
  131. void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  132. {
  133. // TODO: get multicast subscriptions from LWIP
  134. }
  135. NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
  136. {
  137. NetconConnection *c;
  138. for(size_t i=0; i<clients.size(); i++) {
  139. c = clients[i]->containsPCB(pcb);
  140. if(c) {
  141. return c;
  142. }
  143. }
  144. return NULL;
  145. }
  146. NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
  147. {
  148. for(size_t i=0; i<clients.size(); i++) {
  149. for(size_t j=0; j<clients[i]->connections.size(); j++) {
  150. if(_phy.getDescriptor(clients[i]->connections[j]->sock) == fd) {
  151. return clients[i]->connections[j];
  152. }
  153. }
  154. }
  155. return NULL;
  156. }
  157. NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
  158. {
  159. for(size_t i=0; i<clients.size(); i++) {
  160. if(clients[i]->containsPCB(pcb)) {
  161. return clients[i];
  162. }
  163. }
  164. return NULL;
  165. }
  166. void NetconEthernetTap::closeClient(NetconClient *client)
  167. {
  168. // erase from clients vector
  169. client->closeClient();
  170. }
  171. void NetconEthernetTap::closeConnection(NetconConnection *conn)
  172. {
  173. NetconClient *client = conn->owner;
  174. _phy.close(conn->sock);
  175. lwipstack->tcp_close(conn->pcb);
  176. client->removeConnection(conn->sock);
  177. }
  178. void NetconEthernetTap::threadMain()
  179. throw()
  180. {
  181. static ip_addr_t ipaddr, netmask, gw;
  182. char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
  183. IP4_ADDR(&gw, 192,168,0,1);
  184. IP4_ADDR(&netmask, 255,255,255,0);
  185. IP4_ADDR(&ipaddr, 192,168,0,2);
  186. strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
  187. strncpy(nm_str, lwipstack->ipaddr_ntoa(&netmask), sizeof(nm_str));
  188. strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
  189. unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
  190. unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
  191. unsigned long prev_tcp_time = 0;
  192. unsigned long prev_etharp_time = 0;
  193. unsigned long curr_time;
  194. unsigned long since_tcp;
  195. unsigned long since_etharp;
  196. struct timeval tv;
  197. while (_run) {
  198. gettimeofday(&tv, NULL);
  199. curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
  200. since_tcp = curr_time - prev_tcp_time;
  201. since_etharp = curr_time - prev_etharp_time;
  202. int min_time = min(since_tcp, since_etharp) * 1000; // usec
  203. if(since_tcp > tcp_time)
  204. {
  205. prev_tcp_time = curr_time+1;
  206. lwipstack->tcp_tmr();
  207. }
  208. if(since_etharp > etharp_time)
  209. {
  210. prev_etharp_time = curr_time;
  211. lwipstack->etharp_tmr();
  212. }
  213. _phy.poll(min_time / 1000); // conversion from usec to millisec, TODO: double check
  214. }
  215. // TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
  216. }
  217. void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
  218. {
  219. NetconClient *client = (NetconClient*)*uptr;
  220. closeConnection(client->getConnection(sock));
  221. }
  222. void NetconEthernetTap::phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n)
  223. {
  224. int r;
  225. NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
  226. if(c) {
  227. if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
  228. if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
  229. c->idx += r;
  230. handle_write(c);
  231. }
  232. }
  233. }
  234. }
  235. void NetconEthernetTap::phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr)
  236. {
  237. //_phy.setNotifyWritable(sock, false);
  238. }
  239. // Unused -- no UDP or TCP from this thread/Phy<>
  240. void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
  241. void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
  242. void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  243. void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
  244. void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  245. void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
  246. void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
  247. {
  248. NetconClient *newClient = new NetconClient();
  249. newClient->addConnection(RPC, *uptrN);
  250. }
  251. void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
  252. {
  253. ((NetconClient*)*uptr)->closeClient();
  254. }
  255. void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  256. {
  257. unsigned char *buf = (unsigned char*)data;
  258. NetconClient *client = (NetconClient*)*uptr;
  259. switch(buf[0])
  260. {
  261. case RPC_SOCKET:
  262. struct socket_st socket_rpc;
  263. memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st));
  264. client->tid = socket_rpc.__tid;
  265. handle_socket(client, &socket_rpc);
  266. break;
  267. case RPC_LISTEN:
  268. struct listen_st listen_rpc;
  269. memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st));
  270. client->tid = listen_rpc.__tid;
  271. handle_listen(client, &listen_rpc);
  272. break;
  273. case RPC_BIND:
  274. struct bind_st bind_rpc;
  275. memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st));
  276. client->tid = bind_rpc.__tid;
  277. handle_bind(client, &bind_rpc);
  278. break;
  279. case RPC_KILL_INTERCEPT:
  280. client->closeClient();
  281. break;
  282. case RPC_CONNECT:
  283. struct connect_st connect_rpc;
  284. memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st));
  285. client->tid = connect_rpc.__tid;
  286. handle_connect(client, &connect_rpc);
  287. break;
  288. case RPC_FD_MAP_COMPLETION:
  289. handle_retval(client, buf);
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
  296. {
  297. }
  298. int NetconEthernetTap::send_return_value(NetconClient *client, int retval)
  299. {
  300. if(!client->waiting_for_retval){
  301. // intercept isn't waiting for return value. Why are we here?
  302. return 0;
  303. }
  304. char retmsg[4];
  305. memset(&retmsg, '\0', sizeof(retmsg));
  306. retmsg[0]=RPC_RETVAL;
  307. memcpy(&retmsg[1], &retval, sizeof(retval));
  308. int n = write(_phy.getDescriptor(client->rpc->sock), &retmsg, sizeof(retmsg));
  309. if(n > 0) {
  310. // signal that we've satisfied this requirement
  311. client->waiting_for_retval = false;
  312. }
  313. else {
  314. // unable to send return value to the intercept
  315. closeClient(client);
  316. }
  317. return n;
  318. }
  319. /*------------------------------------------------------------------------------
  320. --------------------------------- LWIP callbacks -------------------------------
  321. ------------------------------------------------------------------------------*/
  322. err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
  323. {
  324. Larg *l = (Larg*)arg;
  325. NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
  326. NetconEthernetTap *tap = l->tap;
  327. if(c)
  328. tap->handle_write(c);
  329. return ERR_OK;
  330. }
  331. err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
  332. {
  333. return ERR_OK;
  334. }
  335. err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  336. {
  337. Larg *l = (Larg*)arg;
  338. NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
  339. NetconEthernetTap *tap = l->tap;
  340. int n;
  341. struct pbuf* q = p;
  342. int our_fd = tap->_phy.getDescriptor(c->sock);
  343. if(!c) {
  344. return ERR_OK; // ?
  345. }
  346. if(p == NULL) {
  347. if(c) {
  348. nc_close(tpcb);
  349. close(our_fd); // TODO: Check logic
  350. tap->closeConnection(c);
  351. }
  352. else {
  353. // can't locate connection via (arg)
  354. }
  355. return err;
  356. }
  357. q = p;
  358. while(p != NULL) { // Cycle through pbufs and write them to the socket
  359. if(p->len <= 0)
  360. break; // ?
  361. if((n = write(our_fd, p->payload, p->len)) > 0) {
  362. if(n < p->len) {
  363. // ERROR: unable to write entire pbuf to buffer
  364. //tap->_phy.setNotifyWritable(l->sock, true);
  365. }
  366. tap->lwipstack->tcp_recved(tpcb, n);
  367. }
  368. else {
  369. // Error: No data written to intercept buffer
  370. }
  371. p = p->next;
  372. }
  373. tap->lwipstack->pbuf_free(q); // free pbufs
  374. return ERR_OK;
  375. }
  376. void NetconEthernetTap::nc_err(void *arg, err_t err)
  377. {
  378. Larg *l = (Larg*)arg;
  379. NetconEthernetTap *tap = l->tap;
  380. NetconConnection *c = tap->getConnectionByThisFD(tap->_phy.getDescriptor(l->sock));
  381. if(c) {
  382. l->tap->closeConnection(c);
  383. //tcp_close(c->pcb);
  384. }
  385. else {
  386. // can't locate connection object for PCB
  387. }
  388. }
  389. void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb)
  390. {
  391. /*
  392. lwipstack->tcp_arg(tpcb, NULL);
  393. lwipstack->tcp_sent(tpcb, NULL);
  394. lwipstack->tcp_recv(tpcb, NULL);
  395. lwipstack->tcp_err(tpcb, NULL);
  396. lwipstack->tcp_poll(tpcb, NULL, 0);
  397. lwipstack->tcp_close(tpcb);
  398. */
  399. }
  400. err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
  401. {
  402. return ERR_OK;
  403. }
  404. err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
  405. {
  406. return len;
  407. }
  408. err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  409. {
  410. Larg *l = (Larg*)arg;
  411. NetconEthernetTap *tap = l->tap;
  412. for(size_t i=0; i<tap->clients.size(); i++) {
  413. if(tap->clients[i]->containsPCB(tpcb)) {
  414. tap->send_return_value(tap->clients[i],err);
  415. }
  416. }
  417. return err;
  418. }
  419. /*------------------------------------------------------------------------------
  420. ----------------------------- RPC Handler functions ----------------------------
  421. ------------------------------------------------------------------------------*/
  422. void NetconEthernetTap::handle_bind(NetconClient *client, struct bind_st *bind_rpc)
  423. {
  424. // FIXME: Is this hack still needed?
  425. struct sockaddr_in *connaddr;
  426. connaddr = (struct sockaddr_in *) &bind_rpc->addr;
  427. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  428. ip_addr_t conn_addr;
  429. IP4_ADDR(&conn_addr, 192,168,0,2);
  430. /*
  431. int ip = connaddr->sin_addr.s_addr;
  432. unsigned char bytes[4];
  433. bytes[0] = ip & 0xFF;
  434. bytes[1] = (ip >> 8) & 0xFF;
  435. bytes[2] = (ip >> 16) & 0xFF;
  436. bytes[3] = (ip >> 24) & 0xFF;
  437. "binding to: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]
  438. */
  439. NetconConnection *c = client->getConnectionByTheirFD(bind_rpc->sockfd);
  440. if(c) {
  441. if(c->pcb->state == CLOSED){
  442. int err = lwipstack->tcp_bind(c->pcb, &conn_addr, conn_port);
  443. if(err != ERR_OK) {
  444. // error while binding to addr/port
  445. }
  446. else {
  447. // bind successful
  448. }
  449. }
  450. else {
  451. // PCB not in CLOSED state. Ignoring BIND request.
  452. }
  453. }
  454. else {
  455. // can't locate connection for PCB
  456. }
  457. }
  458. void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *listen_rpc)
  459. {
  460. NetconConnection *c = client->getConnectionByTheirFD(listen_rpc->sockfd);
  461. if(c) {
  462. if(c->pcb->state == LISTEN) {
  463. // PCB is already in listening state.
  464. return;
  465. }
  466. struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(c->pcb);
  467. if(listening_pcb != NULL) {
  468. c->pcb = listening_pcb;
  469. lwipstack->tcp_accept(listening_pcb, nc_accept);
  470. lwipstack->tcp_arg(listening_pcb, new Larg(this, c->sock));
  471. client->waiting_for_retval=true;
  472. }
  473. else {
  474. // unable to allocate memory for new listening PCB
  475. }
  476. }
  477. else {
  478. // can't locate connection for PCB
  479. }
  480. }
  481. void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
  482. {
  483. if(client->unmapped_conn != NULL) {
  484. memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
  485. client->unmapped_conn = NULL;
  486. }
  487. }
  488. void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* socket_rpc)
  489. {
  490. struct tcp_pcb *pcb = lwipstack->tcp_new();
  491. if(pcb != NULL) {
  492. int *their_fd = NULL;
  493. NetconConnection *new_conn = client->addConnection(BUFFER, _phy.createSocketPair(*their_fd, client));
  494. new_conn->their_fd = *their_fd;
  495. new_conn->pcb = pcb;
  496. sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
  497. client->unmapped_conn = new_conn;
  498. }
  499. else {
  500. // Memory not available for new PCB
  501. }
  502. }
  503. void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st* connect_rpc)
  504. {
  505. // FIXME: Parse out address information -- Probably a more elegant way to do this
  506. struct sockaddr_in *connaddr;
  507. connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
  508. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  509. ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
  510. NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
  511. if(c!= NULL) {
  512. lwipstack->tcp_sent(c->pcb, NetconEthernetTap::nc_sent); // FIXME: Move?
  513. lwipstack->tcp_recv(c->pcb, nc_recved);
  514. lwipstack->tcp_err(c->pcb, nc_err);
  515. lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ);
  516. lwipstack->tcp_arg(c->pcb, new Larg(this, c->sock));
  517. int err = 0;
  518. if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0)
  519. {
  520. // dwr(h->tid, "tcp_connect() = %s\n", lwiperror(err));
  521. // We should only return a value if failure happens immediately
  522. // Otherwise, we still need to wait for a callback from lwIP.
  523. // - This is because an ERR_OK from tcp_connect() only verifies
  524. // that the SYN packet was enqueued onto the stack properly,
  525. // that's it!
  526. // - Most instances of a retval for a connect() should happen
  527. // in the nc_connect() and nc_err() callbacks!
  528. send_return_value(client, err);
  529. }
  530. // Everything seems to be ok, but we don't have enough info to retval
  531. client->waiting_for_retval=true;
  532. }
  533. else {
  534. // could not locate PCB based on their fd
  535. }
  536. }
  537. void NetconEthernetTap::handle_write(NetconConnection *c)
  538. {
  539. if(c) {
  540. int sndbuf = c->pcb->snd_buf;
  541. float avail = (float)sndbuf;
  542. float max = (float)TCP_SND_BUF;
  543. float load = 1.0 - (avail / max);
  544. if(load >= 0.9) {
  545. return;
  546. }
  547. int write_allowance = sndbuf < c->idx ? sndbuf : c->idx;
  548. int sz;
  549. if(write_allowance > 0) {
  550. int err = lwipstack->tcp_write(c->pcb, &c->buf, write_allowance, TCP_WRITE_FLAG_COPY);
  551. if(err != ERR_OK) {
  552. // error while writing to PCB
  553. return;
  554. }
  555. else {
  556. sz = (c->idx)-write_allowance;
  557. if(sz) {
  558. memmove(&c->buf, (c->buf+write_allowance), sz);
  559. }
  560. c->idx -= write_allowance;
  561. //c->data_sent += write_allowance;
  562. return;
  563. }
  564. }
  565. else {
  566. // lwIP stack full
  567. return;
  568. }
  569. }
  570. else {
  571. // could not locate connection for this fd
  572. }
  573. }
  574. } // namespace ZeroTier
  575. #endif // ZT_ENABLE_NETCON