NetconEthernetTap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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::threadMain()
  172. throw()
  173. {
  174. static ip_addr_t ipaddr, netmask, gw;
  175. char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
  176. IP4_ADDR(&gw, 192,168,0,1);
  177. IP4_ADDR(&netmask, 255,255,255,0);
  178. IP4_ADDR(&ipaddr, 192,168,0,2);
  179. strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
  180. strncpy(nm_str, lwipstack->ipaddr_ntoa(&netmask), sizeof(nm_str));
  181. strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
  182. unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
  183. unsigned long etharp_time = IP_TMR_INTERVAL / 1000;
  184. unsigned long prev_tcp_time = 0;
  185. unsigned long prev_etharp_time = 0;
  186. unsigned long curr_time;
  187. unsigned long since_tcp;
  188. unsigned long since_etharp;
  189. struct timeval tv;
  190. while (_run) {
  191. gettimeofday(&tv, NULL);
  192. curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
  193. since_tcp = curr_time - prev_tcp_time;
  194. since_etharp = curr_time - prev_etharp_time;
  195. int min_time = min(since_tcp, since_etharp) * 1000; // usec
  196. if(since_tcp > tcp_time)
  197. {
  198. prev_tcp_time = curr_time+1;
  199. lwipstack->tcp_tmr();
  200. }
  201. if(since_etharp > etharp_time)
  202. {
  203. prev_etharp_time = curr_time;
  204. lwipstack->etharp_tmr();
  205. }
  206. _phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check
  207. }
  208. // TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
  209. }
  210. void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
  211. {
  212. NetconClient *client = (NetconClient*)*uptr;
  213. client->closeConnection(sock);
  214. }
  215. void NetconEthernetTap::phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n)
  216. {
  217. int r;
  218. NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
  219. if(c) {
  220. if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
  221. if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
  222. c->idx += r;
  223. handle_write(c);
  224. }
  225. }
  226. }
  227. }
  228. void NetconEthernetTap::phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr)
  229. {
  230. }
  231. // Unused -- no UDP or TCP from this thread/Phy<>
  232. void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
  233. void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
  234. void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  235. void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
  236. void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  237. void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
  238. void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
  239. {
  240. NetconClient *newClient = new NetconClient();
  241. newClient->addConnection(RPC, *uptrN);
  242. }
  243. void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
  244. {
  245. ((NetconClient*)*uptr)->closeClient();
  246. }
  247. void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  248. {
  249. unsigned char *buf = (unsigned char*)data;
  250. NetconClient *client = (NetconClient*)*uptr;
  251. switch(buf[0])
  252. {
  253. case RPC_SOCKET:
  254. struct socket_st socket_rpc;
  255. memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st));
  256. client->tid = socket_rpc.__tid;
  257. handle_socket(client, &socket_rpc);
  258. break;
  259. case RPC_LISTEN:
  260. struct listen_st listen_rpc;
  261. memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st));
  262. client->tid = listen_rpc.__tid;
  263. handle_listen(client, &listen_rpc);
  264. break;
  265. case RPC_BIND:
  266. struct bind_st bind_rpc;
  267. memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st));
  268. client->tid = bind_rpc.__tid;
  269. handle_bind(client, &bind_rpc);
  270. break;
  271. case RPC_KILL_INTERCEPT:
  272. client->closeClient();
  273. break;
  274. case RPC_CONNECT:
  275. struct connect_st connect_rpc;
  276. memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st));
  277. client->tid = connect_rpc.__tid;
  278. handle_connect(client, &connect_rpc);
  279. break;
  280. case RPC_FD_MAP_COMPLETION:
  281. handle_retval(client, buf);
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
  288. {
  289. }
  290. int NetconEthernetTap::send_return_value(NetconClient *client, int retval)
  291. {
  292. if(!client->waiting_for_retval){
  293. // intercept isn't waiting for return value. Why are we here?
  294. return 0;
  295. }
  296. char retmsg[4];
  297. memset(&retmsg, '\0', sizeof(retmsg));
  298. retmsg[0]=RPC_RETVAL;
  299. memcpy(&retmsg[1], &retval, sizeof(retval));
  300. int n = write(_phy.getDescriptor(client->rpc->sock), &retmsg, sizeof(retmsg));
  301. if(n > 0) {
  302. // signal that we've satisfied this requirement
  303. client->waiting_for_retval = false;
  304. }
  305. else {
  306. // unable to send return value to the intercept
  307. closeClient(client);
  308. }
  309. return n;
  310. }
  311. /*------------------------------------------------------------------------------
  312. --------------------------------- LWIP callbacks -------------------------------
  313. ------------------------------------------------------------------------------*/
  314. err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
  315. {
  316. Larg *l = (Larg*)arg;
  317. NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
  318. NetconEthernetTap *tap = l->tap;
  319. if(c)
  320. tap->handle_write(c);
  321. return ERR_OK;
  322. }
  323. err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
  324. {
  325. return ERR_OK;
  326. }
  327. err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  328. {
  329. Larg *l = (Larg*)arg;
  330. NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
  331. NetconEthernetTap *tap = l->tap;
  332. int n;
  333. struct pbuf* q = p;
  334. int our_fd = tap->_phy.getDescriptor(c->sock);
  335. if(!c) {
  336. return ERR_OK; // ?
  337. }
  338. if(p == NULL) {
  339. if(c) {
  340. nc_close(tpcb);
  341. close(our_fd); // TODO: Check logic
  342. c->owner->closeConnection(c);
  343. }
  344. else {
  345. // can't locate connection via (arg)
  346. }
  347. return err;
  348. }
  349. q = p;
  350. while(p != NULL) { // Cycle through pbufs and write them to the socket
  351. if(p->len <= 0)
  352. break; // ?
  353. if((n = write(our_fd, p->payload, p->len)) > 0) {
  354. if(n < p->len) {
  355. // ERROR: unable to write entire pbuf to buffer
  356. }
  357. tap->lwipstack->tcp_recved(tpcb, n);
  358. }
  359. else {
  360. // Error: No data written to intercept buffer
  361. }
  362. p = p->next;
  363. }
  364. tap->lwipstack->pbuf_free(q); // free pbufs
  365. return ERR_OK;
  366. }
  367. void NetconEthernetTap::nc_err(void *arg, err_t err)
  368. {
  369. Larg *l = (Larg*)arg;
  370. NetconEthernetTap *tap = l->tap;
  371. NetconConnection *c = tap->getConnectionByThisFD(tap->_phy.getDescriptor(l->sock));
  372. if(c) {
  373. c->owner->closeConnection(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. /*
  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. }
  391. err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
  392. {
  393. return ERR_OK;
  394. }
  395. err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
  396. {
  397. return len;
  398. }
  399. err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  400. {
  401. Larg *l = (Larg*)arg;
  402. NetconEthernetTap *tap = l->tap;
  403. for(size_t i=0; i<tap->clients.size(); i++) {
  404. if(tap->clients[i]->containsPCB(tpcb)) {
  405. tap->send_return_value(tap->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. /*
  422. int ip = connaddr->sin_addr.s_addr;
  423. unsigned char bytes[4];
  424. bytes[0] = ip & 0xFF;
  425. bytes[1] = (ip >> 8) & 0xFF;
  426. bytes[2] = (ip >> 16) & 0xFF;
  427. bytes[3] = (ip >> 24) & 0xFF;
  428. "binding to: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]
  429. */
  430. NetconConnection *c = client->getConnectionByTheirFD(bind_rpc->sockfd);
  431. if(c) {
  432. if(c->pcb->state == CLOSED){
  433. int err = lwipstack->tcp_bind(c->pcb, &conn_addr, conn_port);
  434. if(err != ERR_OK) {
  435. // error while binding to addr/port
  436. }
  437. else {
  438. // bind successful
  439. }
  440. }
  441. else {
  442. // PCB not in CLOSED state. Ignoring BIND request.
  443. }
  444. }
  445. else {
  446. // can't locate connection for PCB
  447. }
  448. }
  449. void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *listen_rpc)
  450. {
  451. NetconConnection *c = client->getConnectionByTheirFD(listen_rpc->sockfd);
  452. if(c) {
  453. if(c->pcb->state == LISTEN) {
  454. // PCB is already in listening state.
  455. return;
  456. }
  457. struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(c->pcb);
  458. if(listening_pcb != NULL) {
  459. c->pcb = listening_pcb;
  460. lwipstack->tcp_accept(listening_pcb, nc_accept);
  461. lwipstack->tcp_arg(listening_pcb, new Larg(this, c->sock));
  462. client->waiting_for_retval=true;
  463. }
  464. else {
  465. // unable to allocate memory for new listening PCB
  466. }
  467. }
  468. else {
  469. // can't locate connection for PCB
  470. }
  471. }
  472. void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
  473. {
  474. if(client->unmapped_conn != NULL) {
  475. memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
  476. client->unmapped_conn = NULL;
  477. }
  478. }
  479. void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* socket_rpc)
  480. {
  481. struct tcp_pcb *pcb = lwipstack->tcp_new();
  482. if(pcb != NULL) {
  483. int *their_fd = NULL;
  484. NetconConnection *new_conn = client->addConnection(BUFFER, _phy.createSocketPair(*their_fd, client));
  485. new_conn->their_fd = *their_fd;
  486. new_conn->pcb = pcb;
  487. sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
  488. client->unmapped_conn = new_conn;
  489. }
  490. else {
  491. // Memory not available for new PCB
  492. }
  493. }
  494. void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st* connect_rpc)
  495. {
  496. // FIXME: Parse out address information -- Probably a more elegant way to do this
  497. struct sockaddr_in *connaddr;
  498. connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
  499. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  500. ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
  501. NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
  502. if(c!= NULL) {
  503. lwipstack->tcp_sent(c->pcb, NetconEthernetTap::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, new Larg(this, c->sock));
  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