NetconService.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <sys/poll.h>
  28. #include <string>
  29. #include "../osdep/Phy.hpp"
  30. #include "NetconEthernetTap.hpp"
  31. #include "Intercept.h"
  32. #include "LWIPStack.hpp"
  33. #ifndef _NETCON_SERVICE_HPP
  34. #define _NETCON_SERVICE_HPP
  35. using namespace std;
  36. namespace ZeroTier {
  37. class NetconEthernetTap;
  38. // Helper class for passing reference to Phy to LWIP callbacks
  39. class Larg
  40. {
  41. public:
  42. NetconEthernetTap *tap;
  43. PhySocket *sock;
  44. Larg(NetconEthernetTap *_tap, PhySocket *_sock) : tap(_tap), sock(_sock) {}
  45. };
  46. enum NetconConnectionType { RPC, BUFFER };
  47. // prototypes
  48. class NetconClient;
  49. class NetconConnection;
  50. //
  51. class NetconConnection
  52. {
  53. public:
  54. int their_fd;
  55. unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
  56. int idx;
  57. NetconConnectionType type;
  58. struct tcp_pcb *pcb;
  59. PhySocket *sock;
  60. NetconClient *owner;
  61. NetconConnection(NetconConnectionType type, PhySocket *sock) : type(type), sock(sock) {}
  62. };
  63. //
  64. class NetconClient
  65. {
  66. public:
  67. vector<NetconConnection*> connections;
  68. int tid;
  69. bool waiting_for_retval;
  70. NetconConnection *rpc;
  71. NetconConnection* unmapped_conn;
  72. NetconConnection *addConnection(NetconConnectionType type, PhySocket *sock)
  73. {
  74. NetconConnection *new_conn = new NetconConnection(type, sock);
  75. new_conn->owner = this;
  76. return new_conn;
  77. }
  78. // Check data and RPC connections
  79. NetconConnection *getConnection(PhySocket *sock)
  80. {
  81. if(sock && sock == rpc->sock) {
  82. return rpc;
  83. }
  84. for(size_t i=0; i<connections.size(); i++) {
  85. if(sock == connections[i]->sock) { return connections[i];}
  86. }
  87. return NULL;
  88. }
  89. //
  90. NetconConnection *getConnectionByTheirFD(int fd)
  91. {
  92. for(size_t i=0; i<connections.size(); i++) {
  93. if(connections[i]->their_fd == fd) { return connections[i]; }
  94. }
  95. return NULL;
  96. }
  97. //
  98. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
  99. {
  100. for(size_t i=0; i<connections.size(); i++) {
  101. if(connections[i]->pcb == pcb) { return connections[i]; }
  102. }
  103. return NULL;
  104. }
  105. NetconConnection *containsPCB(struct tcp_pcb *pcb)
  106. {
  107. for(size_t i=0; i<connections.size(); i++) {
  108. if(connections[i]->pcb == pcb) { return connections[i]; }
  109. }
  110. return NULL;
  111. }
  112. void closeConnection(NetconConnection *c)
  113. {
  114. // close all connections
  115. // -- pcb
  116. // -- PhySocket
  117. }
  118. void closeConnection(PhySocket *sock)
  119. {
  120. }
  121. void closeClient()
  122. {
  123. }
  124. };
  125. } // namespace ZeroTier
  126. #endif