1 /*
2  *  Hans - IP over ICMP
3  *  Copyright (C) 2009 Friedrich Schöller <hans@schoeller.se>
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 #ifndef SERVER_H
21 #define SERVER_H
22 
23 #include "worker.h"
24 #include "auth.h"
25 
26 #include <map>
27 #include <queue>
28 #include <vector>
29 #include <set>
30 
31 class Server : public Worker
32 {
33 public:
34     Server(int tunnelMtu, const char *deviceName, const char *passphrase, uint32_t network, bool answerEcho, uid_t uid, gid_t gid, int pollTimeout);
35     virtual ~Server();
36 
37     // change some time:
38     // struct __attribute__ ((__packed__)) ClientConnectData
39     struct ClientConnectData
40     {
41         uint8_t maxPolls;
42         uint32_t desiredIp;
43     };
44 
45     static const Worker::TunnelHeader::Magic magic;
46 
47 protected:
48     struct Packet
49     {
50         int type;
51         std::vector<char> data;
52     };
53 
54     struct ClientData
55     {
56         enum State
57         {
58             STATE_NEW,
59             STATE_CHALLENGE_SENT,
60             STATE_ESTABLISHED
61         };
62 
63         struct EchoId
64         {
EchoIdClientData::EchoId65             EchoId(uint16_t id, uint16_t seq) { this->id = id; this->seq = seq; }
66 
67             uint16_t id;
68             uint16_t seq;
69         };
70 
71         uint32_t realIp;
72         uint32_t tunnelIp;
73 
74         std::queue<Packet> pendingPackets;
75 
76         int maxPolls;
77         std::queue<EchoId> pollIds;
78         Time lastActivity;
79 
80         State state;
81 
82         Auth::Challenge challenge;
83     };
84 
85     typedef std::vector<ClientData> ClientList;
86     typedef std::map<uint32_t, int> ClientIpMap;
87 
88     virtual bool handleEchoData(const TunnelHeader &header, int dataLength, uint32_t realIp, bool reply, uint16_t id, uint16_t seq);
89     virtual void handleTunData(int dataLength, uint32_t sourceIp, uint32_t destIp);
90     virtual void handleTimeout();
91 
92     virtual void run();
93 
94     void serveTun(ClientData *client);
95 
96     void handleUnknownClient(const TunnelHeader &header, int dataLength, uint32_t realIp, uint16_t echoId, uint16_t echoSeq);
97     void removeClient(ClientData *client);
98 
99     void sendChallenge(ClientData *client);
100     void checkChallenge(ClientData *client, int dataLength);
101     void sendReset(ClientData *client);
102 
103     void sendEchoToClient(ClientData *client, int type, int dataLength);
104 
105     void pollReceived(ClientData *client, uint16_t echoId, uint16_t echoSeq);
106 
107     uint32_t reserveTunnelIp(uint32_t desiredIp);
108     void releaseTunnelIp(uint32_t tunnelIp);
109 
110     ClientData *getClientByTunnelIp(uint32_t ip);
111     ClientData *getClientByRealIp(uint32_t ip);
112 
113     Auth auth;
114 
115     uint32_t network;
116     std::set<uint32_t> usedIps;
117     uint32_t latestAssignedIpOffset;
118 
119     Time pollTimeout;
120 
121     ClientList clientList;
122     ClientIpMap clientRealIpMap;
123     ClientIpMap clientTunnelIpMap;
124 };
125 
126 #endif
127