summaryrefslogtreecommitdiff
path: root/netcon/NetconService.h
blob: 08250fdb574a064535c9341e386cc1f3535b189d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2015  ZeroTier, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#include <sys/poll.h>
#include <string>
#include "Intercept.h"
#include "LWIPStack.hpp"

#ifndef _NETCON_SERVICE_H_
#define _NETCON_SERVICE_H_

using namespace std;

enum NetconSocketType { RPC, BUFFER };

class NetconIntercept;
class NetconConnection;
class NetconSocket;

class NetconSocket{
  int fd;
  NetconSocketType type;
};


class NetconConnection
{
public:

  int tid;
  int our_fd;
  int their_fd; /* what the user app sees -- and what they will send us */
  struct tcp_pcb *pcb; /* for handling lwIP calls/data */
  bool unacked;

  unsigned char *write_buf; /* we need this reference so we can grab data from the buffer during a lwip-poll callback */
  long unsigned int write_count;
  long unsigned int write_total;

  unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
  int idx;

  unsigned long data_acked;
  unsigned long data_sent;
  unsigned long data_read;
  unsigned long data_recvd;

  NetconIntercept* owner;

  NetconConnection(NetconIntercept* owner,
    int tid,
    int our_fd,
    int their_fd,
    struct tcp_pcb *pcb)
    :
      write_total(0),
      write_count(0),
      write_buf(NULL),
      owner(owner),
      tid(tid),
      our_fd(our_fd),
      their_fd(their_fd),
      pcb(pcb)
    {}
};

class NetconIntercept
{
public:

  // State (this needs to be evaluated)
  NetconConnection *unmapped_conn;
  bool waiting_for_retval;

  // Connections created for this intercept
  vector<NetconConnection*> owned_connections;

  int tid; /* just for uniqueness */
  int rpc;

  NetconIntercept(int tid, int rpc)
  :
    waiting_for_retval(false),
    unmapped_conn(NULL),
    tid(tid),
    rpc(rpc)
  {}
};


#define POLL_SZ ZT_PHY_MAX_SOCKETS+(ZT_PHY_MAX_INTERCEPTS*2)+2
class NetconService
{
public:

  LWIPStack *ls;

  // TODO: shall replace with map
  vector<NetconIntercept*> intercepts;
  vector<NetconConnection*> connections;

  /* fd_sets for main I/O polling */
  fd_set fdset;
  fd_set exfdset;
  int maxfd;
  size_t sz;
  int tapfd;

  // Sets of file descriptors we will poll() on
  int default_rpc_pipe;
  struct pollfd allfds[POLL_SZ];
  int alltypes[POLL_SZ];

  /* cached fd_sets */
  bool possible_state_change;
  fd_set cached_fdset;
  fd_set cached_exfdset;
  int cached_alltypes[POLL_SZ];
  int cached_maxfd;
  int cached_sz;

  NetconService(LWIPStack *ls, string _handle)
  :
    ls(ls),
    maxfd(0),
    sz(0),
    possible_state_change(true)
  {}

  /* Assemble single poll list */
  void assemble_fd_sets()
  {
    sz = 2 + connections.size() + intercepts.size();
    // initialize
    for(size_t i=0; i<sz; i++){
      allfds[i].fd = 0;
      allfds[i].events = 0;
    }
    int idx = 0;
    // default rpc fd
    allfds[0].fd = default_rpc_pipe;
    allfds[0].events = POLLIN;
    alltypes[0] = 1;
    // netif fd
    allfds[1].fd=tapfd;
    allfds[1].events = POLLIN;
    alltypes[1] = 2;
    // buffers
    for(size_t i=0; i<connections.size(); i++) {
      idx = i + 2;
      allfds[idx].fd = connections[i]->our_fd;
      allfds[idx].events = POLLIN;
      alltypes[idx] = 3;
    }
    // established connections
    for(size_t i=0; i<intercepts.size(); i++) {
      idx = i + connections.size() + 2;
      allfds[idx].fd = intercepts[i]->rpc;
      allfds[idx].events = POLLIN;
      alltypes[idx] = 4;
    }
    FD_ZERO(&fdset);
    FD_ZERO(&exfdset);
    // populate master fd_set
    for(size_t i=0; i<sz; i++) {
      FD_SET(allfds[i].fd, &fdset);
      FD_SET(allfds[i].fd, &exfdset);
    }
    // get maxfd
    for(size_t i=0; i<sz; i++)
    {
      if(allfds[i].fd > maxfd)
        maxfd = allfds[i].fd;
    }
    // cache copy of valid fd_set
    possible_state_change = false;
    memcpy(&cached_fdset, &fdset, sizeof(fdset));
    memcpy(&cached_exfdset, &exfdset, sizeof(exfdset));
    memcpy(&cached_alltypes, &alltypes, sizeof(alltypes));
    cached_maxfd = maxfd;
    cached_sz = sz;
  }


  NetconConnection *get_connection_by_pcb(struct tcp_pcb *pcb) {
    for(size_t i=0; i<connections.size(); i++) {
      if(connections[i]->pcb == pcb) {
        return connections[i];
      }
    }
    return NULL;
  }

  NetconConnection *get_connection_by_buf_sock(int fd) {
    for(size_t i=0; i<connections.size(); i++) {
      if(connections[i]->our_fd==fd) {
        return connections[i];
      }
    }
    return NULL;
  }

  // FIXME: This will be a common operation and thus should be done via
  // some sort of hashing, not iterative lookup.
  NetconIntercept *get_intercept_by_pcb(struct tcp_pcb* pcb) {
    for(size_t i=0; i<connections.size(); i++) {
      if(connections[i]->pcb==pcb) {
        return connections[i]->owner;
      }
    }
    return NULL;
  }


  NetconIntercept *get_intercept_by_rpc(int af) {
    for(size_t i=0; i<intercepts.size(); i++) {
      if(intercepts[i]->rpc==af) {
        return intercepts[i];
      }
    }
    return NULL;
  }

  NetconConnection *get_connection_by_that_fd(NetconIntercept* h, int fd)
  {
    for(size_t i=0; i<h->owned_connections.size(); i++) {
      if(h->owned_connections[i]->their_fd==fd) {
        return h->owned_connections[i];
      }
    }
    return NULL;
  }

  NetconConnection *get_connection_by_this_fd(int fd)
  {
    for(size_t i=0; i<connections.size(); i++) {
      if(connections[i]->our_fd==fd) {
        return connections[i];
      }
    }
    return NULL;
  }

  NetconConnection *get_connection_by_that_fd(int fd)
  {
    for(size_t i=0; i<connections.size(); i++) {
      if(connections[i]->their_fd==fd) {
        return connections[i];
      }
    }
    return NULL;
  }

  NetconConnection *add_connection(NetconIntercept* owner,
      int tid,
      int our_fd,
      int their_fd,
      struct tcp_pcb* pcb)
  {
    possible_state_change = true;
    if(connections.size() >= ZT_PHY_MAX_SOCKETS) {
      return NULL;
    }
    NetconConnection *new_conn = new NetconConnection(owner, tid, our_fd, their_fd, pcb);
    connections.push_back(new_conn);

    NetconIntercept *h;
    for(size_t i=0; i<intercepts.size(); i++) {
      if(intercepts[i]->tid == tid) {
        h = intercepts[i];
      }
    }
    if(h)
      h->owned_connections.push_back(new_conn);
    return new_conn;
  }

  // Removes a Connection from the Service and updates poll lists
  void remove_connection(NetconConnection *c)
  {
    possible_state_change = true;
    for(size_t i=0; i<connections.size(); i++) {
      if(c == connections[i]) {
        close(connections[i]->our_fd);
        ls->tcp_close(c->pcb);
        delete c;
        connections.erase(connections.begin() + i);
      }
    }
  }

  int add_intercept(int listen_sock) {
    possible_state_change = true;
    int accept_socket = accept(listen_sock, NULL, NULL);
    intercepts.push_back(new NetconIntercept(999, accept_socket));
    return accept_socket;
  }


  // Removes an Intercept from the Service
  bool remove_intercept(NetconIntercept *h)
  {
    possible_state_change = true;
    // remove all connections owned by this Intercept
    for(size_t i=0; i<h->owned_connections.size(); i++) {
      remove_connection(h->owned_connections[i]);
    }
    // find and remove Intercept
    for(size_t i=0; i<intercepts.size(); i++)
    {
      if(h == intercepts[i]) {
        delete intercepts[i];
        intercepts.erase(intercepts.begin() + i);
        return true;
      }
    }
    return false;
  }
};

#endif