NetconService.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. enum NetconConnectionType { RPC, BUFFER };
  38. class NetconEthernetTap;
  39. /*
  40. * A helper class for passing a reference to _phy to LWIP callbacks as a "state"
  41. */
  42. class Larg
  43. {
  44. public:
  45. NetconEthernetTap *tap;
  46. PhySocket *sock;
  47. Larg(NetconEthernetTap *_tap, PhySocket *_sock) : tap(_tap), sock(_sock) {}
  48. };
  49. // prototypes
  50. class NetconClient;
  51. class NetconConnection;
  52. /*
  53. * A data connection, any number of these may be associated with a NetconClient
  54. */
  55. class NetconConnection
  56. {
  57. public:
  58. int their_fd;
  59. unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
  60. int idx;
  61. NetconConnectionType type;
  62. struct tcp_pcb *pcb;
  63. PhySocket *sock;
  64. NetconClient *owner;
  65. NetconConnection(NetconConnectionType type, PhySocket *sock) : type(type), sock(sock) {}
  66. };
  67. /*
  68. * A "harnessed" client with associated rpc and data connections.
  69. */
  70. class NetconClient
  71. {
  72. public:
  73. vector<NetconConnection*> connections;
  74. int tid;
  75. bool waiting_for_retval;
  76. NetconConnection *rpc;
  77. NetconConnection* unmapped_conn;
  78. NetconConnection *addConnection(NetconConnectionType type, PhySocket *sock)
  79. {
  80. NetconConnection *new_conn = new NetconConnection(type, sock);
  81. new_conn->owner = this;
  82. return new_conn;
  83. }
  84. // Check data and RPC connections
  85. NetconConnection *getConnection(PhySocket *sock)
  86. {
  87. if(sock && sock == rpc->sock) {
  88. return rpc;
  89. }
  90. for(size_t i=0; i<connections.size(); i++) {
  91. if(sock == connections[i]->sock) return connections[i];
  92. }
  93. return NULL;
  94. }
  95. //
  96. NetconConnection *getConnectionByTheirFD(int fd)
  97. {
  98. for(size_t i=0; i<connections.size(); i++) {
  99. if(connections[i]->their_fd == fd) return connections[i];
  100. }
  101. return NULL;
  102. }
  103. //
  104. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
  105. {
  106. for(size_t i=0; i<connections.size(); i++) {
  107. if(connections[i]->pcb == pcb) return connections[i];
  108. }
  109. return NULL;
  110. }
  111. NetconConnection *containsPCB(struct tcp_pcb *pcb)
  112. {
  113. for(size_t i=0; i<connections.size(); i++) {
  114. if(connections[i]->pcb == pcb) return connections[i];
  115. }
  116. return NULL;
  117. }
  118. void removeConnection(PhySocket *sock)
  119. {
  120. for(size_t i=0; i<connections.size(); i++) {
  121. if(connections[i]->sock == sock)
  122. connections.erase(connections.begin() + i);
  123. }
  124. }
  125. };
  126. } // namespace ZeroTier
  127. #endif