NetconEthernetTap.cpp 17 KB

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