forked from PurpleI2P/i2pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TunnelConfig.h
207 lines (179 loc) · 4.43 KB
/
TunnelConfig.h
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
#ifndef TUNNEL_CONFIG_H__
#define TUNNEL_CONFIG_H__
#include <inttypes.h>
#include <sstream>
#include "RouterInfo.h"
#include "RouterContext.h"
namespace i2p
{
namespace tunnel
{
struct TunnelHopConfig
{
const i2p::data::RouterInfo * router, * nextRouter;
uint32_t tunnelID, nextTunnelID;
uint8_t layerKey[32];
uint8_t ivKey[32];
uint8_t replyKey[32];
uint8_t replyIV[16];
bool isGateway, isEndpoint;
TunnelHopConfig * next, * prev;
TunnelHopConfig (const i2p::data::RouterInfo * r)
{
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (layerKey, 32);
rnd.GenerateBlock (ivKey, 32);
rnd.GenerateBlock (replyIV, 16);
tunnelID = rnd.GenerateWord32 ();
isGateway = true;
isEndpoint = true;
router = r;
nextRouter = 0;
nextTunnelID = 0;
next = 0;
prev = 0;
}
void SetNextRouter (const i2p::data::RouterInfo * r)
{
nextRouter = r;
isEndpoint = false;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
nextTunnelID = rnd.GenerateWord32 ();
}
void SetReplyHop (TunnelHopConfig * replyFirstHop)
{
nextRouter = replyFirstHop->router;
nextTunnelID = replyFirstHop->tunnelID;
isEndpoint = true;
}
void SetNext (TunnelHopConfig * n)
{
next = n;
if (next)
{
next->prev = this;
next->isGateway = false;
isEndpoint = false;
nextRouter = next->router;
nextTunnelID = next->tunnelID;
}
}
void SetPrev (TunnelHopConfig * p)
{
prev = p;
if (prev)
{
prev->next = this;
prev->isEndpoint = false;
isGateway = false;
}
}
};
class TunnelConfig
{
public:
TunnelConfig (const i2p::data::RouterInfo * peer, TunnelConfig * replyTunnelConfig = 0) // one hop
{
m_FirstHop = new TunnelHopConfig (peer);
m_LastHop = m_FirstHop;
if (replyTunnelConfig) // outbound
{
m_FirstHop->isGateway = false;
m_LastHop->SetReplyHop (replyTunnelConfig->GetFirstHop ());
}
else
m_FirstHop->SetNextRouter (&i2p::context.GetRouterInfo ());
}
TunnelConfig (const i2p::data::RouterInfo * peer1, const i2p::data::RouterInfo * peer2, TunnelConfig * replyTunnelConfig = 0) // two hops
{
m_FirstHop = new TunnelHopConfig (peer1);
m_LastHop = new TunnelHopConfig (peer2);
m_FirstHop->SetNext (m_LastHop);
if (replyTunnelConfig)
{
m_FirstHop->isGateway = false;
m_LastHop->SetReplyHop (replyTunnelConfig->GetFirstHop ());
}
else
m_LastHop->SetNextRouter (&i2p::context.GetRouterInfo ());
}
~TunnelConfig ()
{
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
delete hop;
hop = hop->next;
}
}
TunnelHopConfig * GetFirstHop () const
{
return m_FirstHop;
}
TunnelHopConfig * GetLastHop () const
{
return m_LastHop;
}
size_t GetNumHops () const
{
size_t num = 0;
TunnelHopConfig * hop = m_FirstHop;
while (hop)
{
num++;
hop = hop->next;
}
return num;
}
void Print (std::stringstream& s) const
{
TunnelHopConfig * hop = m_FirstHop;
if (!m_FirstHop->isGateway)
s << "me";
s << "-->" << m_FirstHop->tunnelID;
while (hop)
{
s << ":" << hop->router->GetIdentHashAbbreviation () << "-->";
if (!hop->isEndpoint)
s << hop->nextTunnelID;
else
return;
hop = hop->next;
}
// we didn't reach enpoint that mean we are last hop
s << ":me";
}
TunnelConfig * Invert () const
{
TunnelConfig * newConfig = new TunnelConfig ();
TunnelHopConfig * hop = m_FirstHop, * nextNewHop = nullptr;
while (hop)
{
TunnelHopConfig * newHop = new TunnelHopConfig (hop->router);
if (nextNewHop)
newHop->SetNext (nextNewHop);
nextNewHop = newHop;
newHop->isEndpoint = hop->isGateway;
newHop->isGateway = hop->isEndpoint;
if (!hop->prev) // first hop
{
newConfig->m_LastHop = newHop;
if (hop->isGateway) // inbound tunnel
newHop->SetReplyHop (m_FirstHop); // use it as reply tunnel
}
if (!hop->next) newConfig->m_FirstHop = newHop; // last hop
hop = hop->next;
}
return newConfig;
}
private:
// this constructor can't be called from outside
TunnelConfig (): m_FirstHop (nullptr), m_LastHop (nullptr)
{
}
private:
TunnelHopConfig * m_FirstHop, * m_LastHop;
};
}
}
#endif