NetconService.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // prototypes
  38. class NetconClient;
  39. class NetconConnection;
  40. //
  41. class NetconConnection
  42. {
  43. public:
  44. int their_fd;
  45. unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
  46. int idx;
  47. NetconConnectionType type;
  48. struct tcp_pcb *pcb;
  49. PhySocket *sock;
  50. NetconClient *owner;
  51. NetconConnection(NetconConnectionType type, PhySocket *sock) : type(type), sock(sock) {}
  52. };
  53. //
  54. class NetconClient
  55. {
  56. public:
  57. vector<NetconConnection*> connections;
  58. int tid;
  59. bool waiting_for_retval;
  60. NetconConnection *rpc;
  61. NetconConnection* unmapped_conn;
  62. NetconConnection *addConnection(NetconConnectionType type, PhySocket *sock)
  63. {
  64. NetconConnection *new_conn = new NetconConnection(type, sock);
  65. new_conn->owner = this;
  66. return new_conn;
  67. }
  68. // Check data and RPC connections
  69. NetconConnection *getConnection(PhySocket *sock)
  70. {
  71. if(sock && sock == rpc->sock) {
  72. return rpc;
  73. }
  74. for(size_t i=0; i<connections.size(); i++) {
  75. if(sock == connections[i]->sock) { return connections[i];}
  76. }
  77. return NULL;
  78. }
  79. //
  80. NetconConnection *getConnectionByTheirFD(int fd)
  81. {
  82. for(size_t i=0; i<connections.size(); i++) {
  83. if(connections[i]->their_fd == fd) { return connections[i]; }
  84. }
  85. return NULL;
  86. }
  87. //
  88. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb)
  89. {
  90. for(size_t i=0; i<connections.size(); i++) {
  91. if(connections[i]->pcb = pcb) { return connections[i]; }
  92. }
  93. return NULL;
  94. }
  95. NetconConnection *containsPCB(struct tcp_pcb *pcb)
  96. {
  97. for(size_t i=0; i<connections.size(); i++) {
  98. if(connections[i]->pcb = pcb) { return connections[i]; }
  99. }
  100. return NULL;
  101. }
  102. void close()
  103. {
  104. // close all connections
  105. // -- pcb
  106. // -- PhySocket
  107. }
  108. };
  109. } // namespace ZeroTier
  110. #endif