NetconEthernetTap.cpp 18 KB

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