NetconService.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "Intercept.h"
  31. #include "LWIPStack.hpp"
  32. #ifndef _NETCON_SERVICE_H_
  33. #define _NETCON_SERVICE_H_
  34. using namespace std;
  35. namespace ZeroTier {
  36. enum NetconConnectionType { RPC, BUFFER };
  37. class NetconClient;
  38. class NetconConnection;
  39. //
  40. class NetconConnection
  41. {
  42. int their_fd;
  43. unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
  44. int idx;
  45. NetconConnectionType type;
  46. struct tcp_pcb *pcb;
  47. PhySocket *sock;
  48. NetconClient *owner;
  49. NetconConnection(NetconConnectionType type, PhySocket *sock) : type(type), sock(sock) {}
  50. };
  51. //
  52. class NetconClient
  53. {
  54. private:
  55. vector<NetconConnection*> connections;
  56. public:
  57. int tid;
  58. NetconConnection *rpc;
  59. void addConnection(NetconConnectionType type, PhySocket *sock)
  60. {
  61. NetconConnection new_conn = new NetconConnection(type, sock);
  62. new_conn->owner = this;
  63. }
  64. // Check data and RPC connections
  65. NetconConnection *getConnection(PhySocket *sock)
  66. {
  67. if(sock && sock == rpc->sock) {
  68. return rpc;
  69. }
  70. for(int i=0; i<connections.size(); i++) {
  71. if(sock == connections[i]->sock) { return connections[i];}
  72. }
  73. return NULL;
  74. }
  75. //
  76. NetconConnection *getConnectionByTheirFD(int fd)
  77. {
  78. for(int i=0; i<connections.size(); i++) {
  79. if(connections[i]->their_fd == fd) { return connections[i]; }
  80. }
  81. }
  82. //
  83. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
  84. {
  85. for(int i=0; i<connections.size(); i++) {
  86. if(connections[i]->pcb = pcb) { return connections[i]; }
  87. }
  88. }
  89. void close()
  90. {
  91. // close all connections
  92. // -- pcb
  93. // -- PhySocket
  94. }
  95. };
  96. } // namespace ZeroTier
  97. #endif