NetconEthernetTap.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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(int 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. NetconConnection *c;
  147. for(int i=0; i<clients.size(); i++) {
  148. for(int j=0; j<clients[i]->connections[j].size(); j++) {
  149. if(_phy.getDescriptor(clients[i]->connection[j]->sock) == fd) {
  150. return clients[i]->connections[j];
  151. }
  152. }
  153. }
  154. return NULL;
  155. }
  156. NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
  157. {
  158. for(int i=0; i<clients.size(); i++) {
  159. if(clients[i].containsPCB(pcb)) {
  160. return clients[i];
  161. }
  162. }
  163. return NULL;
  164. }
  165. void NetconEthernetTap::closeClient(NetconClient *client)
  166. {
  167. // erase from clients vector
  168. client->close();
  169. }
  170. void NetconEthernetTap::threadMain()
  171. throw()
  172. {
  173. static ip_addr_t ipaddr, netmask, gw;
  174. char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
  175. IP4_ADDR(&gw, 192,168,0,1);
  176. IP4_ADDR(&netmask, 255,255,255,0);
  177. IP4_ADDR(&ipaddr, 192,168,0,2);
  178. strncpy(ip_str, lwipstack->ipaddr_ntoa(&ipaddr), sizeof(ip_str));
  179. strncpy(nm_str, lwipstack->ipaddr_ntoa(&netmask), sizeof(nm_str));
  180. strncpy(gw_str, lwipstack->ipaddr_ntoa(&gw), sizeof(gw_str));
  181. unsigned long tcp_time = ARP_TMR_INTERVAL / 5000;
  182. unsigned long ipreass_time = TCP_TMR_INTERVAL / 1000;
  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(NetconConnectionType.RPC, *uptrN)
  225. }
  226. void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
  227. {
  228. *uptr->close();
  229. }
  230. void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  231. {
  232. NetconConnection *c = *uptr->getConnection(sock);
  233. int r;
  234. if(c->type == NetconConnectionType.BUFFER) {
  235. if(c) {
  236. if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
  237. if((r = read(sws->sock, (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
  238. c->idx += r;
  239. handle_write(c);
  240. }
  241. }
  242. }
  243. else {
  244. // can't find connection for this fd
  245. }
  246. }
  247. if(c->type == NetconConnectionType.RPC)
  248. {
  249. NetconClient *client = (NetconClient*)*uptr;
  250. switch((unsigned char*)data[0])
  251. {
  252. case RPC_SOCKET:
  253. struct socket_st socket_rpc;
  254. memcpy(&socket_rpc, &data[1], sizeof(struct socket_st));
  255. client->tid = socket_rpc.__tid;
  256. handle_socket(client, &socket_rpc);
  257. break;
  258. case RPC_LISTEN:
  259. struct listen_st listen_rpc;
  260. memcpy(&listen_rpc, &data[1], sizeof(struct listen_st));
  261. client->tid = listen_rpc.__tid;
  262. handle_listen(client, &listen_rpc);
  263. break;
  264. case RPC_BIND:
  265. struct bind_st bind_rpc;
  266. memcpy(&bind_rpc, &data[1], sizeof(struct bind_st));
  267. client->tid = bind_rpc.__tid;
  268. handle_bind(client, &bind_rpc);
  269. break;
  270. case RPC_KILL_INTERCEPT:
  271. client->close();
  272. break;
  273. case RPC_CONNECT:
  274. struct connect_st connect_rpc;
  275. memcpy(&connect_rpc, &data[1], sizeof(struct connect_st));
  276. client->tid = connect_rpc.__tid;
  277. handle_connect(client, &connect_rpc);
  278. break;
  279. case RPC_FD_MAP_COMPLETION:
  280. handle_retval(client, data);
  281. break;
  282. default:
  283. break;
  284. }
  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. NetconConnection *c = getConnectionByPCB(tpcb); // TODO: make sure this works, if not, use arg to look up the connection
  317. if(c)
  318. handle_write(c);
  319. return ERR_OK;
  320. }
  321. err_t NetconEthernetTap::nc_accept(void* arg, struct tcp_pcb *newpcb, err_t err)
  322. {
  323. NetconClient *client = getConnectionByThisFD((intptr_t)arg);
  324. if(client)
  325. {
  326. int *their_fd;
  327. int our_fd;
  328. PhySocket *new_sock = _phy.createSocketPair(*their_fd, client)
  329. NetconConnection c = addConnection(NetconConnectionType.BUFFER, new_sock);
  330. our_fd = _phy.getDescriptor(c->sock);
  331. if(c == NULL) return -1;
  332. c->owner->unmapped_conn = c;
  333. // write byte to let accept call know we have a new connection
  334. int n = write(our_fd, "z", 1);
  335. if(n > 0) {
  336. sock_fd_write(c->owner->rpc, *their_fd);
  337. }
  338. else {
  339. // unknown error writing signal byte to listening socket
  340. return -1;
  341. }
  342. lwipstack->tcp_arg(newpcb, (void*)(intptr_t)(our_fd));
  343. lwipstack->tcp_recv(newpcb, nc_recved);
  344. lwipstack->tcp_err(newpcb, nc_err);
  345. lwipstack->tcp_sent(newpcb, nc_sent);
  346. lwipstack->tcp_poll(newpcb, nc_poll, APPLICATION_POLL_FREQ);
  347. tcp_accepted(c->pcb);
  348. return ERR_OK;
  349. }
  350. return -1;
  351. }
  352. err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  353. {
  354. int n;
  355. struct pbuf* q = p;
  356. NetconConnection *c = getConnectionByPCB(tpcb); // TODO: make sure this works, if not, use arg as "buf sock"
  357. int our_fd = _phy.getDescriptor(c->sock);
  358. if(c) {
  359. //dwr(c->owner->tid, "nc_recved(%d)\n", (intptr_t)arg);
  360. }
  361. else {
  362. //dwr(-1, "nc_recved(%d): unable to locate connection\n", (intptr_t)arg);
  363. return ERR_OK; // ?
  364. }
  365. if(p == NULL) {
  366. //dwr(c->owner->tid, "nc_recved() = %s\n", lwiperror(err));
  367. if(c)
  368. //dwr(c->owner->tid, "nc_recved()\n");
  369. if(c) {
  370. //dwr(c->owner->tid, "closing connection\n");
  371. nc_close(tpcb);
  372. close(our_fd); /* TODO: Check logic */
  373. //nc_service->remove_connection(c);
  374. c->owner->close(c);
  375. }
  376. else {
  377. //dwr(-1, "can't locate connection via (arg)\n");
  378. }
  379. return err;
  380. }
  381. q = p;
  382. while(p != NULL) { // Cycle through pbufs and write them to the socket
  383. //dwr(c->owner->tid, "writing data to mapped sock (%d)\n", c->our_fd);
  384. if(p->len <= 0)
  385. break; // ?
  386. if((n = write(our_fd, p->payload, p->len)) > 0) {
  387. if(n < p->len) {
  388. //dwr(c->owner->tid, "ERROR: unable to write entire pbuf to buffer\n");
  389. }
  390. lwipstack->tcp_recved(tpcb, n);
  391. }
  392. else {
  393. //dwr(c->owner->tid, "ERROR: No data written to intercept buffer.\n");
  394. }
  395. p = p->next;
  396. }
  397. lwipstack->pbuf_free(q); /* free pbufs */
  398. return ERR_OK;
  399. }
  400. void NetconEthernetTap::nc_err(void *arg, err_t err)
  401. {
  402. NetconConnection *c = getConnectionByThisFD((intptr)arg);
  403. if(c) {
  404. //nc_service->remove_connection(c);
  405. c->owner->close(c);
  406. //tcp_close(c->pcb);
  407. }
  408. else {
  409. // can't locate connection object for PCB
  410. }
  411. }
  412. void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb)
  413. {
  414. //NetconConnection *c = getConnectionByPCB(tpcb);
  415. lwipstack->tcp_arg(tpcb, NULL);
  416. lwipstack->tcp_sent(tpcb, NULL);
  417. lwipstack->tcp_recv(tpcb, NULL);
  418. lwipstack->tcp_err(tpcb, NULL);
  419. lwipstack->tcp_poll(tpcb, NULL, 0);
  420. lwipstack->tcp_close(tpcb);
  421. }
  422. err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
  423. {
  424. return ERR_OK;
  425. }
  426. err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
  427. {
  428. //NetconConnection *c = _phy->getConnectionByPCB(tpcb);
  429. //if(c)
  430. //c->data_sent += len;
  431. return len;
  432. }
  433. err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  434. {
  435. for(int i=0; i<clients.size(); i++) {
  436. if(clients[i].containsPCB(tpcb)) {
  437. send_return_value(clients[i],err);
  438. }
  439. }
  440. return err;
  441. }
  442. /*------------------------------------------------------------------------------
  443. ----------------------------- RPC Handler functions ----------------------------
  444. ------------------------------------------------------------------------------*/
  445. void NetconEthernetTap::handle_bind(NetconClient *client, struct bind_st *bind_rpc)
  446. {
  447. // FIXME: Is this hack still needed?
  448. struct sockaddr_in *connaddr;
  449. connaddr = (struct sockaddr_in *) &bind_rpc->addr;
  450. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  451. ip_addr_t conn_addr;
  452. IP4_ADDR(&conn_addr, 192,168,0,2);
  453. int ip = connaddr->sin_addr.s_addr;
  454. unsigned char bytes[4];
  455. bytes[0] = ip & 0xFF;
  456. bytes[1] = (ip >> 8) & 0xFF;
  457. bytes[2] = (ip >> 16) & 0xFF;
  458. bytes[3] = (ip >> 24) & 0xFF;
  459. // "binding to: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]
  460. NetconConnection *c = client->getConnectionByTheirFD(bind_rpc->sockfd);
  461. if(c) {
  462. if(c->pcb->state == CLOSED){
  463. int err = lwipstack->tcp_bind(c->pcb, &conn_addr, conn_port);
  464. if(err != ERR_OK) {
  465. // error while binding to addr/port
  466. }
  467. else {
  468. // bind successful
  469. }
  470. }
  471. else {
  472. // PCB not in CLOSED state. Ignoring BIND request.
  473. }
  474. }
  475. else {
  476. // can't locate connection for PCB
  477. }
  478. }
  479. void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *listen_rpc)
  480. {
  481. NetconConnection *c = client->getConnectionByTheirFD(listen_rpc->sockfd);
  482. if(c) {
  483. if(c->pcb->state == LISTEN) {
  484. // PCB is already in listening state.
  485. return;
  486. }
  487. struct tcp_pcb* listening_pcb = lwipstack->tcp_listen(c->pcb);
  488. if(listening_pcb != NULL) {
  489. c->pcb = listening_pcb;
  490. lwipstack->tcp_accept(listening_pcb, nc_accept);
  491. int our_fd = _phy.getDescriptor(c->sock);
  492. lwipstack->tcp_arg(listening_pcb, (void*)(intptr_t)our_fd);
  493. client->waiting_for_retval=true;
  494. }
  495. else {
  496. // unable to allocate memory for new listening PCB
  497. }
  498. }
  499. else {
  500. // can't locate connection for PCB
  501. }
  502. }
  503. void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
  504. {
  505. if(client->unmapped_conn != NULL) {
  506. memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
  507. client->unmapped_conn = NULL;
  508. }
  509. }
  510. void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* socket_rpc)
  511. {
  512. struct tcp_pcb *pcb = lwipstack->tcp_new();
  513. if(pcb != NULL) {
  514. int *their_fd;
  515. NetconConnection *new_conn = client->addConnection(NetconConnectionType.BUFFER, _phy.createSocketPair(*their_fd, client));
  516. new_conn->their_fd = *their_fd;
  517. new_conn->pcb = pcb;
  518. sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
  519. client->unmapped_conn = new_conn;
  520. }
  521. else {
  522. // Memory not available for new PCB
  523. }
  524. }
  525. void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st* connect_rpc)
  526. {
  527. // FIXME: Parse out address information -- Probably a more elegant way to do this
  528. struct sockaddr_in *connaddr;
  529. connaddr = (struct sockaddr_in *) &connect_rpc->__addr;
  530. int conn_port = lwipstack->ntohs(connaddr->sin_port);
  531. ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
  532. NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
  533. int our_fd = _phy.getDescriptor(c->sock);
  534. if(c!= NULL) {
  535. lwipstack->tcp_sent(c->pcb, nc_sent); // FIXME: Move?
  536. lwipstack->tcp_recv(c->pcb, nc_recved);
  537. lwipstack->tcp_err(c->pcb, nc_err);
  538. lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ);
  539. lwipstack->tcp_arg(c->pcb,(void*)(intptr_t)our_fd);
  540. int err = 0;
  541. if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0)
  542. {
  543. // dwr(h->tid, "tcp_connect() = %s\n", lwiperror(err));
  544. // We should only return a value if failure happens immediately
  545. // Otherwise, we still need to wait for a callback from lwIP.
  546. // - This is because an ERR_OK from tcp_connect() only verifies
  547. // that the SYN packet was enqueued onto the stack properly,
  548. // that's it!
  549. // - Most instances of a retval for a connect() should happen
  550. // in the nc_connect() and nc_err() callbacks!
  551. send_return_value(client, err);
  552. }
  553. // Everything seems to be ok, but we don't have enough info to retval
  554. client->waiting_for_retval=true;
  555. }
  556. else {
  557. // could not locate PCB based on their fd
  558. }
  559. }
  560. void NetconEthernetTap::handle_write(NetconConnection *c)
  561. {
  562. if(c) {
  563. int sndbuf = c->pcb->snd_buf;
  564. float avail = (float)sndbuf;
  565. float max = (float)TCP_SND_BUF;
  566. float load = 1.0 - (avail / max);
  567. if(load >= 0.9) {
  568. return;
  569. }
  570. int write_allowance = sndbuf < c->idx ? sndbuf : c->idx;
  571. int sz;
  572. if(write_allowance > 0) {
  573. int err = lwipstack->tcp_write(c->pcb, &c->buf, write_allowance, TCP_WRITE_FLAG_COPY);
  574. if(err != ERR_OK) {
  575. // error while writing to PCB
  576. return;
  577. }
  578. else {
  579. sz = (c->idx)-write_allowance;
  580. if(sz) {
  581. memmove(&c->buf, (c->buf+write_allowance), sz);
  582. }
  583. c->idx -= write_allowance;
  584. //c->data_sent += write_allowance;
  585. return;
  586. }
  587. }
  588. else {
  589. // lwIP stack full
  590. return;
  591. }
  592. }
  593. else {
  594. // could not locate connection for this fd
  595. }
  596. }
  597. } // namespace ZeroTier
  598. #endif // ZT_ENABLE_NETCON