NetconEthernetTap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. //struct timeval tv_sel;
  191. while (_run) {
  192. gettimeofday(&tv, NULL);
  193. curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
  194. since_tcp = curr_time - prev_tcp_time;
  195. since_etharp = curr_time - prev_etharp_time;
  196. int min_time = min(since_tcp, since_etharp) * 1000; // usec
  197. if(since_tcp > tcp_time)
  198. {
  199. prev_tcp_time = curr_time+1;
  200. lwipstack->tcp_tmr();
  201. }
  202. if(since_etharp > etharp_time)
  203. {
  204. prev_etharp_time = curr_time;
  205. lwipstack->etharp_tmr();
  206. }
  207. // should be set every time since tv_sel is modified after each select() call
  208. //tv_sel.tv_sec = 0;
  209. //tv_sel.tv_usec = min_time;
  210. _phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check
  211. }
  212. // TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
  213. }
  214. // Unused -- no UDP or TCP from this thread/Phy<>
  215. void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
  216. void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
  217. void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  218. void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
  219. void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  220. void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
  221. void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
  222. {
  223. NetconClient *newClient = new NetconClient();
  224. newClient->addConnection(RPC, *uptrN);
  225. }
  226. void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
  227. {
  228. ((NetconClient*)*uptr)->closeClient();
  229. }
  230. void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  231. {
  232. unsigned char *buf = (unsigned char*)data;
  233. NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
  234. int r;
  235. if(c->type == BUFFER) {
  236. if(c) {
  237. if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
  238. if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
  239. c->idx += r;
  240. handle_write(c);
  241. }
  242. }
  243. }
  244. else {
  245. // can't find connection for this fd
  246. }
  247. }
  248. if(c->type == RPC)
  249. {
  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. }
  288. void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
  289. {
  290. }
  291. int NetconEthernetTap::send_return_value(NetconClient *client, int retval)
  292. {
  293. if(!client->waiting_for_retval){
  294. // intercept isn't waiting for return value. Why are we here?
  295. return 0;
  296. }
  297. char retmsg[4];
  298. memset(&retmsg, '\0', sizeof(retmsg));
  299. retmsg[0]=RPC_RETVAL;
  300. memcpy(&retmsg[1], &retval, sizeof(retval));
  301. int n = write(_phy.getDescriptor(client->rpc->sock), &retmsg, sizeof(retmsg));
  302. if(n > 0) {
  303. // signal that we've satisfied this requirement
  304. client->waiting_for_retval = false;
  305. }
  306. else {
  307. // unable to send return value to the intercept
  308. closeClient(client);
  309. }
  310. return n;
  311. }
  312. /*------------------------------------------------------------------------------
  313. --------------------------------- LWIP callbacks -------------------------------
  314. ------------------------------------------------------------------------------*/
  315. err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  316. {
  317. Larg *l = (Larg*)arg;
  318. NetconConnection *c = l->tap->getConnectionByPCB(tpcb);
  319. NetconEthernetTap *tap = l->tap;
  320. int n;
  321. struct pbuf* q = p;
  322. int our_fd = tap->_phy.getDescriptor(c->sock);
  323. if(c) {
  324. //dwr(c->owner->tid, "nc_recved(%d)\n", (intptr_t)arg);
  325. }
  326. else {
  327. //dwr(-1, "nc_recved(%d): unable to locate connection\n", (intptr_t)arg);
  328. return ERR_OK; // ?
  329. }
  330. if(p == NULL) {
  331. //dwr(c->owner->tid, "nc_recved()\n");
  332. if(c) {
  333. //dwr(c->owner->tid, "closing connection\n");
  334. nc_close(tpcb);
  335. close(our_fd); /* TODO: Check logic */
  336. //nc_service->remove_connection(c);
  337. c->owner->closeConnection(c);
  338. }
  339. else {
  340. // can't locate connection via (arg)
  341. }
  342. return err;
  343. }
  344. q = p;
  345. while(p != NULL) { // Cycle through pbufs and write them to the socket
  346. if(p->len <= 0)
  347. break; // ?
  348. if((n = write(our_fd, p->payload, p->len)) > 0) {
  349. if(n < p->len) {
  350. // ERROR: unable to write entire pbuf to buffer
  351. }
  352. tap->lwipstack->tcp_recved(tpcb, n);
  353. }
  354. else {
  355. // Error: No data written to intercept buffer
  356. }
  357. p = p->next;
  358. }
  359. tap->lwipstack->pbuf_free(q); // free pbufs
  360. return ERR_OK;
  361. }
  362. void NetconEthernetTap::nc_err(void *arg, err_t err)
  363. {
  364. Larg *l = (Larg*)arg;
  365. NetconEthernetTap *tap = l->tap;
  366. NetconConnection *c = tap->getConnectionByThisFD(tap->_phy.getDescriptor(l->sock));
  367. if(c) {
  368. c->owner->closeConnection(c);
  369. //tcp_close(c->pcb);
  370. }
  371. else {
  372. // can't locate connection object for PCB
  373. }
  374. }
  375. void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb)
  376. {
  377. /*
  378. lwipstack->tcp_arg(tpcb, NULL);
  379. lwipstack->tcp_sent(tpcb, NULL);
  380. lwipstack->tcp_recv(tpcb, NULL);
  381. lwipstack->tcp_err(tpcb, NULL);
  382. lwipstack->tcp_poll(tpcb, NULL, 0);
  383. lwipstack->tcp_close(tpcb);
  384. */
  385. }
  386. err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
  387. {
  388. return ERR_OK;
  389. }
  390. err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
  391. {
  392. return len;
  393. }
  394. err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  395. {
  396. Larg *l = (Larg*)arg;
  397. NetconEthernetTap *tap = l->tap;
  398. for(size_t i=0; i<tap->clients.size(); i++) {
  399. if(tap->clients[i]->containsPCB(tpcb)) {
  400. tap->send_return_value(tap->clients[i],err);
  401. }
  402. }
  403. return err;
  404. }
  405. /*------------------------------------------------------------------------------
  406. ----------------------------- RPC Handler functions ----------------------------
  407. ------------------------------------------------------------------------------*/
  408. void NetconEthernetTap::handle_bind(NetconClient *client, struct bind_st *bind_rpc)
  409. {
  410. // FIXME: Is this hack still needed?
  411. struct sockaddr_in *connaddr;
  412. connaddr = (struct sockaddr_in *) &bind_rpc->addr;
  413. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  414. ip_addr_t conn_addr;
  415. IP4_ADDR(&conn_addr, 192,168,0,2);
  416. /*
  417. int ip = connaddr->sin_addr.s_addr;
  418. unsigned char bytes[4];
  419. bytes[0] = ip & 0xFF;
  420. bytes[1] = (ip >> 8) & 0xFF;
  421. bytes[2] = (ip >> 16) & 0xFF;
  422. bytes[3] = (ip >> 24) & 0xFF;
  423. "binding to: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]
  424. */
  425. NetconConnection *c = client->getConnectionByTheirFD(bind_rpc->sockfd);
  426. if(c) {
  427. if(c->pcb->state == CLOSED){
  428. int err = lwipstack->tcp_bind(c->pcb, &conn_addr, conn_port);
  429. if(err != ERR_OK) {
  430. // error while binding to addr/port
  431. }
  432. else {
  433. // bind successful
  434. }
  435. }
  436. else {
  437. // PCB not in CLOSED state. Ignoring BIND request.
  438. }
  439. }
  440. else {
  441. // can't locate connection for PCB
  442. }
  443. }
  444. void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *listen_rpc)
  445. {
  446. NetconConnection *c = client->getConnectionByTheirFD(listen_rpc->sockfd);
  447. if(c) {
  448. if(c->pcb->state == LISTEN) {
  449. // PCB is already in listening state.
  450. return;
  451. }
  452. struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(c->pcb);
  453. if(listening_pcb != NULL) {
  454. c->pcb = listening_pcb;
  455. lwipstack->tcp_accept(listening_pcb, nc_accept);
  456. lwipstack->tcp_arg(listening_pcb, new Larg(this, c->sock));
  457. client->waiting_for_retval=true;
  458. }
  459. else {
  460. // unable to allocate memory for new listening PCB
  461. }
  462. }
  463. else {
  464. // can't locate connection for PCB
  465. }
  466. }
  467. void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
  468. {
  469. if(client->unmapped_conn != NULL) {
  470. memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
  471. client->unmapped_conn = NULL;
  472. }
  473. }
  474. void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* socket_rpc)
  475. {
  476. struct tcp_pcb *pcb = lwipstack->tcp_new();
  477. if(pcb != NULL) {
  478. int *their_fd;
  479. NetconConnection *new_conn = client->addConnection(BUFFER, _phy.createSocketPair(*their_fd, client));
  480. new_conn->their_fd = *their_fd;
  481. new_conn->pcb = pcb;
  482. sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
  483. client->unmapped_conn = new_conn;
  484. }
  485. else {
  486. // Memory not available for new PCB
  487. }
  488. }
  489. void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st* connect_rpc)
  490. {
  491. // FIXME: Parse out address information -- Probably a more elegant way to do this
  492. struct sockaddr_in *connaddr;
  493. connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
  494. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  495. ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
  496. NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
  497. if(c!= NULL) {
  498. lwipstack->tcp_sent(c->pcb, nc_sent); // FIXME: Move?
  499. lwipstack->tcp_recv(c->pcb, nc_recved);
  500. lwipstack->tcp_err(c->pcb, nc_err);
  501. lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ);
  502. lwipstack->tcp_arg(c->pcb, new Larg(this, c->sock));
  503. int err = 0;
  504. if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0)
  505. {
  506. // dwr(h->tid, "tcp_connect() = %s\n", lwiperror(err));
  507. // We should only return a value if failure happens immediately
  508. // Otherwise, we still need to wait for a callback from lwIP.
  509. // - This is because an ERR_OK from tcp_connect() only verifies
  510. // that the SYN packet was enqueued onto the stack properly,
  511. // that's it!
  512. // - Most instances of a retval for a connect() should happen
  513. // in the nc_connect() and nc_err() callbacks!
  514. send_return_value(client, err);
  515. }
  516. // Everything seems to be ok, but we don't have enough info to retval
  517. client->waiting_for_retval=true;
  518. }
  519. else {
  520. // could not locate PCB based on their fd
  521. }
  522. }
  523. void NetconEthernetTap::handle_write(NetconConnection *c)
  524. {
  525. if(c) {
  526. int sndbuf = c->pcb->snd_buf;
  527. float avail = (float)sndbuf;
  528. float max = (float)TCP_SND_BUF;
  529. float load = 1.0 - (avail / max);
  530. if(load >= 0.9) {
  531. return;
  532. }
  533. int write_allowance = sndbuf < c->idx ? sndbuf : c->idx;
  534. int sz;
  535. if(write_allowance > 0) {
  536. int err = lwipstack->tcp_write(c->pcb, &c->buf, write_allowance, TCP_WRITE_FLAG_COPY);
  537. if(err != ERR_OK) {
  538. // error while writing to PCB
  539. return;
  540. }
  541. else {
  542. sz = (c->idx)-write_allowance;
  543. if(sz) {
  544. memmove(&c->buf, (c->buf+write_allowance), sz);
  545. }
  546. c->idx -= write_allowance;
  547. //c->data_sent += write_allowance;
  548. return;
  549. }
  550. }
  551. else {
  552. // lwIP stack full
  553. return;
  554. }
  555. }
  556. else {
  557. // could not locate connection for this fd
  558. }
  559. }
  560. } // namespace ZeroTier
  561. #endif // ZT_ENABLE_NETCON