Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 141 lines (119 sloc) 5.181 kB
9aa22fe Offered permission to copy under gplv3 license.
cjdelisle authored
1 /*
2 * You may redistribute this program and/or modify it under the terms of
3 * the GNU General Public License as published by the Free Software Foundation,
4 * either version 3 of the License, or (at your option) any later version.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
14 #include "crypto/CryptoAuth.h"
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
15 #include "interface/Interface.h"
16 #include "interface/InterfaceMap.h"
cc3cded Rename MemAllocator Allocator
cjdelisle authored
17 #include "memory/Allocator.h"
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
18 #include "util/Time.h"
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
19 #include "util/Timeout.h"
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
20 #include "wire/Error.h"
21 #include "wire/Headers.h"
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
22 #include "wire/Message.h"
23
24 #include <stdint.h>
25 #include <event2/event.h>
26
27 /** The number of seconds of inactivity before a session should expire. */
0ec237d Major refactoring, committing to test.
cjdelisle authored
28 #define SESSION_TIMEOUT_SECONDS 600
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
29
30 /** The number of seconds between cleanup cycles. */
0ec237d Major refactoring, committing to test.
cjdelisle authored
31 #define CLEANUP_CYCLE_SECONDS 20
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
32
33 /**
0ec237d Major refactoring, committing to test.
cjdelisle authored
34 * A SessionManager is a mechanism for getting a crypto session based on a given key.
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
35 */
36 struct SessionManager
37 {
0ec237d Major refactoring, committing to test.
cjdelisle authored
38 Interface_CONST_CALLBACK(decryptedIncoming);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
39
0ec237d Major refactoring, committing to test.
cjdelisle authored
40 Interface_CONST_CALLBACK(encryptedOutgoing);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
41
0ec237d Major refactoring, committing to test.
cjdelisle authored
42 void* const interfaceContext;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
43
0ec237d Major refactoring, committing to test.
cjdelisle authored
44 uint16_t keySize;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
45
46 struct event_base* const eventBase;
47
48 struct InterfaceMap ifaceMap;
49
cc3cded Rename MemAllocator Allocator
cjdelisle authored
50 struct Allocator* const allocator;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
51
52 struct Timeout* cleanupInterval;
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
53
54 struct CryptoAuth* cryptoAuth;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
55 };
56
4ae68d4 make nodes able to handle traffic from peers not in their routing table
cjdelisle authored
57 static void cleanup(void* vsm)
58 {
59 struct SessionManager* sm = (struct SessionManager*) vsm;
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
60 uint64_t nowSecs = Time_currentTimeSeconds(sm->eventBase);
4ae68d4 make nodes able to handle traffic from peers not in their routing table
cjdelisle authored
61 for (uint32_t i = 0; i < sm->ifaceMap.count; i++) {
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
62 if (sm->ifaceMap.lastMessageTimes[i] < (nowSecs - SESSION_TIMEOUT_SECONDS)) {
cc3cded Rename MemAllocator Allocator
cjdelisle authored
63 struct Allocator* ifAllocator = sm->ifaceMap.interfaces[i]->allocator;
4ae68d4 make nodes able to handle traffic from peers not in their routing table
cjdelisle authored
64 ifAllocator->free(ifAllocator);
65 InterfaceMap_remove(i, &sm->ifaceMap);
66 i--;
67 }
68 }
69 }
70
0ec237d Major refactoring, committing to test.
cjdelisle authored
71 struct Interface* SessionManager_getSession(uint8_t* lookupKey,
72 uint8_t cryptoKey[32],
73 struct SessionManager* sm)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
74 {
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
75 uint64_t nowSecs = Time_currentTimeSeconds(sm->eventBase);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
76
0ec237d Major refactoring, committing to test.
cjdelisle authored
77 int ifaceIndex = InterfaceMap_indexOf(lookupKey, &sm->ifaceMap);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
78 if (ifaceIndex == -1) {
4ae68d4 make nodes able to handle traffic from peers not in their routing table
cjdelisle authored
79 // Make sure cleanup() doesn't get behind.
80 cleanup(sm);
81
cc3cded Rename MemAllocator Allocator
cjdelisle authored
82 struct Allocator* ifAllocator = sm->allocator->child(sm->allocator);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
83 struct Interface* outsideIf =
84 ifAllocator->clone(sizeof(struct Interface), ifAllocator, &(struct Interface) {
0ec237d Major refactoring, committing to test.
cjdelisle authored
85 .sendMessage = sm->encryptedOutgoing,
86 .senderContext = sm->interfaceContext,
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
87 .allocator = ifAllocator
88 });
89 struct Interface* insideIf =
0ec237d Major refactoring, committing to test.
cjdelisle authored
90 CryptoAuth_wrapInterface(outsideIf, cryptoKey, false, false, sm->cryptoAuth);
91 insideIf->receiveMessage = sm->decryptedIncoming;
92 insideIf->receiverContext = sm->interfaceContext;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
93
94 // Create a trick interface which pretends to be on both sides of the crypto.
95 struct Interface* combinedIf =
96 ifAllocator->clone(sizeof(struct Interface), ifAllocator, &(struct Interface) {
97 .sendMessage = insideIf->sendMessage,
98 .senderContext = insideIf->senderContext,
99 .receiveMessage = outsideIf->receiveMessage,
100 .receiverContext = outsideIf->receiverContext,
101 .allocator = ifAllocator
102 });
103
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
104 InterfaceMap_put(lookupKey, combinedIf, nowSecs, &sm->ifaceMap);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
105 return combinedIf;
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
106 } else {
107 // Interface already exists, set the time of last message to "now".
1e9a7cf Killed ping-from-the-past bug, also made all time calls use libevent …
cjdelisle authored
108 sm->ifaceMap.lastMessageTimes[ifaceIndex] = nowSecs;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
109 }
110
111 return sm->ifaceMap.interfaces[ifaceIndex];
112 }
113
0ec237d Major refactoring, committing to test.
cjdelisle authored
114 struct SessionManager* SessionManager_new(uint16_t keySize,
115 Interface_CALLBACK(decryptedIncoming),
116 Interface_CALLBACK(encryptedOutgoing),
117 void* interfaceContext,
118 struct event_base* eventBase,
119 struct CryptoAuth* cryptoAuth,
cc3cded Rename MemAllocator Allocator
cjdelisle authored
120 struct Allocator* allocator)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
121 {
122 struct SessionManager* sm = allocator->malloc(sizeof(struct SessionManager), allocator);
4be4cf7 @fintler Fixed compiler parsing issue.
fintler authored
123 memcpy(sm, (&(struct SessionManager) {
0ec237d Major refactoring, committing to test.
cjdelisle authored
124 .decryptedIncoming = decryptedIncoming,
125 .encryptedOutgoing = encryptedOutgoing,
126 .interfaceContext = interfaceContext,
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
127 .keySize = keySize,
128 .eventBase = eventBase,
129 .ifaceMap = {
0689af8 Halfway through debugging...
cjdelisle authored
130 .keySize = keySize,
131 .allocator = allocator,
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
132 },
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
133 .cryptoAuth = cryptoAuth,
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
134 .allocator = allocator,
135 .cleanupInterval =
136 Timeout_setInterval(cleanup, sm, 1000 * CLEANUP_CYCLE_SECONDS, eventBase, allocator)
4be4cf7 @fintler Fixed compiler parsing issue.
fintler authored
137 }), sizeof(struct SessionManager));
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
138
0ec237d Major refactoring, committing to test.
cjdelisle authored
139 return sm;
0689af8 Halfway through debugging...
cjdelisle authored
140 }
Something went wrong with that request. Please try again.