Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 604 lines (534 sloc) 23.105 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/AddressCalc.h"
15 #include "crypto/Crypto.h"
16 #include "crypto/CryptoAuth.h"
6ac4ca5 Got SwitchConnectorModule into a compilable state and removed some ol…
cjdelisle authored
17 #include "dht/ReplyModule.h"
0689af8 Halfway through debugging...
cjdelisle authored
18 #include "dht/SerializationModule.h"
ddc1976 Renamed SwitchConnectorModule.c Ducttape.c
cjdelisle authored
19 #include "dht/Ducttape.h"
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
20 #include "exception/ExceptionHandler.h"
21 #include "exception/AbortHandler.h"
22 #include "interface/Interface.h"
23 #include "interface/TUNInterface.h"
24 #include "interface/UDPInterface.h"
ea252d1 more stuff
cjdelisle authored
25 #include "io/Reader.h"
26 #include "io/FileReader.h"
63234a2 Added new logging infrastructure.
cjdelisle authored
27 #include "io/Writer.h"
28 #include "io/FileWriter.h"
ea252d1 more stuff
cjdelisle authored
29 #include "libbenc/serialization/BencSerializer.h"
30 #include "libbenc/serialization/json/JsonBencSerializer.h"
b7f23b1 Moved log to util
cjdelisle authored
31 #include "util/Log.h"
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
32 #include "memory/MallocAllocator.h"
ea252d1 more stuff
cjdelisle authored
33 #include "memory/BufferAllocator.h"
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
34 #include "memory/MemAllocator.h"
35 #include "switch/SwitchCore.h"
36 #include "util/Base32.h"
ea252d1 more stuff
cjdelisle authored
37 #include "util/Hex.h"
11af610 Added version information.
cjdelisle authored
38 #include "Version.h"
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
39
ea252d1 more stuff
cjdelisle authored
40 #include "crypto_scalarmult_curve25519.h"
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
41
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
42 #include <stdint.h>
43 #include <assert.h>
ea252d1 more stuff
cjdelisle authored
44 #include <unistd.h>
45 #include <stdio.h>
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
46
47 struct Context
48 {
49 struct event_base* base;
50
51 struct MemAllocator* allocator;
52
ea252d1 more stuff
cjdelisle authored
53 struct ExceptionHandler* eHandler;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
54
55 struct CryptoAuth* ca;
56
7cc5e02 More debugging
cjdelisle authored
57 struct Interface* routerIf;
6baa018 Changed RouterModule to handle new address types, added Address.h, be…
cjdelisle authored
58
59 struct SwitchCore* switchCore;
0689af8 Halfway through debugging...
cjdelisle authored
60
61 struct DHTModuleRegistry* registry;
62
63 struct RouterModule* routerModule;
63234a2 Added new logging infrastructure.
cjdelisle authored
64
65 struct Log* logger;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
66 };
67
ea252d1 more stuff
cjdelisle authored
68 struct UDPInterfaceContext
69 {
70 struct Context* context;
71 struct UDPInterface* udpContext;
72 };
73
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
74 struct User
75 {
76 uint64_t trust;
77 };
78
ea252d1 more stuff
cjdelisle authored
79 static int genAddress(uint8_t addressOut[40],
80 uint8_t privateKeyHexOut[65],
81 uint8_t publicKeyBase32Out[53])
82 {
83 struct Address address;
84 uint8_t privateKey[32];
85
86 for (;;) {
87 randombytes(privateKey, 32);
88 crypto_scalarmult_curve25519_base(address.key, privateKey);
89 AddressCalc_addressForPublicKey(address.ip6.bytes, address.key);
90 // Brute force for keys until one matches FC00:/8
91 if (address.ip6.bytes[0] == 0xFC) {
92 Hex_encode(privateKeyHexOut, 65, privateKey, 32);
93 Base32_encode(publicKeyBase32Out, 53, address.key, 32);
94 Address_printIp(addressOut, &address);
95 return 0;
96 }
97 }
98 }
fd06d8a Improved 'documentation'
cjdelisle authored
99
ea252d1 more stuff
cjdelisle authored
100 static int genconf()
101 {
102 uint8_t publicKeyBase32[53];
103 uint8_t address[40];
104 uint8_t privateKeyHex[65];
105 genAddress(address, privateKeyHex, publicKeyBase32);
106
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
107 printf("{\n"
108 " // Private key:\n"
109 " // This key corrisponds to the public key: %s.k\n", publicKeyBase32);
110 printf(" // And the ipv6 address: %s\n", address);
111 printf(" // Your confidentiality and data integrity depend on this key, keep it secret!\n"
112 " //\n"
113 " \"privateKey\": \"%s\",\n", privateKeyHex);
114 printf("\n"
115 " // Anyone connecting and offering these passwords on connection will be allowed.\n"
116 " //\n"
117 " // WARNING: Currently there is no key derivation done on the password field,\n"
118 " // DO NOT USE A PASSWORD HERE use something which is truely random and\n"
119 " // cannot be guessed.\n"
120 " // Including a username in the beginning of the password string is encouraged\n"
121 " // to aid in remembering which users are who.\n"
122 " //\n"
123 " \"authorizedPasswords\": [\n"
124 " {\n"
125 " // A unique string which is known to the client and server.\n"
126 " \"password\": \"Bob - 2Q4qAPGemxgrydSSetSmOWlE2YO8wYMSG2H1aBPolS3n\",\n"
127 "\n"
128 " // the authentication type, currently only 1 is supported.\n"
129 " \"authType\": 1,\n"
130 "\n"
131 " // How much anti-flood trust to give a client\n"
132 " // who connects with this password.\n"
133 " \"trust\": 5000\n"
134 " },\n"
135 "\n"
136 " /* You can add as many authorized passwords as you want.\n"
137 " {\n"
138 " \"password\": \"Alice - wTNeK7nlFRn1tRfgnOkWEATkd/RFlZOQVuOsUy8ATWjD\",\n"
139 " \"authType\": 1,\n"
140 " \"trust\": 2500\n"
141 " },*/\n"
142 " ],\n"
143 "\n"
144 " // Interfaces to connect to the switch core.\n"
145 " \"interfaces\":\n"
146 " {\n"
147 " // The interface which connects over UDP/IP based VPN tunnel.\n"
148 " \"UDPInterface\":\n"
149 " {\n"
150 " // Bind to this port.\n"
82b30da Removed an assertion which is occasionally wrong.
cjdelisle authored
151 " \"bind\": \"0.0.0.0:10000\",\n"
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
152 "\n"
153 " // Nodes to connect to.\n"
154 " \"connectTo\":\n"
155 " {\n"
156 " \"127.0.0.1:10000\":\n"
157 " {\n"
158 " // Password to present when connecting.\n"
159 " \"password\": \"secret\",\n"
160 "\n"
161 " // The method of authenticating, only 1 is supported for now.\n"
162 " \"authType\": 1,\n"
163 "\n"
164 " // The public key of the node to connect to.\n"
165 " \"publicKey\": "
166 "\"y39gwfy5259s8fj4khntfy95bx6wxu5lbm2m132yx0ucrk0ruyx0.k\",\n"
167 "\n"
168 " // Anti-flood trust level.\n"
169 " \"trust\": 9000\n"
170 " },\n"
171 " /* You may connect to as many other nodes as you want.\n"
172 " \"1.2.3.4:56789\": {\n"
173 " \"password\": \"secret2\",\n"
174 " \"authType\": 1,\n"
175 " \"publicKey\": "
176 "\"y39gwfy5259s8fj4khntfy95bx6wxu5lbm2m132yx0ucrk0ruyx0.k\",\n"
177 " \"trust\": 1234\n"
178 " }\n"
179 " */\n"
180 " }\n"
181 " }\n"
182 " },\n"
183 "\n"
184 " // Configuration for the router.\n"
185 " \"router\":\n"
186 " {\n"
187 " // The interface which is used for connecting to the cjdns network.\n"
188 " \"interface\":\n"
189 " {\n"
190 " // The type of interface (only TUNInterface is supported for now)\n"
191 " \"type\": \"TUNInterface\",\n"
192 "\n"
fd06d8a Improved 'documentation'
cjdelisle authored
193 " // The name of the TUN device to use.\n"
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
194 " // This allows you to create a persistent TUN device with the cjdns user\n"
195 " // authorized to use it so that cjdns does not need to run as root.\n"
fd06d8a Improved 'documentation'
cjdelisle authored
196 " // If this is commented out, cjdns will try to allocate a TUN on startup.\n"
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
197 " // If it can't do that (because it's not root?) then it will run as a\n"
198 " // pure router, unable to send or receive traffic.\n"
fd06d8a Improved 'documentation'
cjdelisle authored
199 " \"tunDevice\": \"tun0\"\n"
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
200 " }\n"
201 " }\n"
202 "\n"
793dabe added debugging to cryptoauth
cjdelisle authored
203 " \"resetAfterInactivitySeconds\": 20,\n"
204 "\n"
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
205 " // Version of the config file, used internally for migration.\n"
206 " \"version\": 0\n"
207 "}\n");
ea252d1 more stuff
cjdelisle authored
208
209 return 0;
210 }
211
212 #define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })
bd259a8 more debugging and got tap working
cjdelisle authored
213 static void parsePrivateKey(Dict* config, struct Address* addr, uint8_t privateKey[32])
ea252d1 more stuff
cjdelisle authored
214 {
215 String* privateKeyStr = benc_lookupString(config, BSTR("privateKey"));
216 if (privateKeyStr == NULL) {
217 fprintf(stderr, "Could not extract private key from configuration.\n");
218 } else if (privateKeyStr->len != 64) {
219 fprintf(stderr, "Private key is not 32 bytes long.\n");
220 } else if (Hex_decode(privateKey, 32, (uint8_t*)privateKeyStr->bytes, 64) != 32) {
221 fprintf(stderr, "Failed to parse private key.\n");
222 } else {
bd259a8 more debugging and got tap working
cjdelisle authored
223 crypto_scalarmult_curve25519_base(addr->key, privateKey);
224 AddressCalc_addressForPublicKey(addr->ip6.bytes, addr->key);
225 if (addr->ip6.bytes[0] != 0xFC) {
226 fprintf(stderr, "Ip address is outside of the FC00/8 range, "
227 "invalid private key.\n");
ea252d1 more stuff
cjdelisle authored
228 } else {
229 return;
230 }
231 }
232 exit(-1);
233 }
234
fd06d8a Improved 'documentation'
cjdelisle authored
235 static int usage(char* appName)
236 {
237 printf("Step 1:\n"
238 " Generate a new configuration file.\n"
239 " %s --genconf > cjdroute.conf\n"
240 "\n"
241 "Step 2:\n"
242 " From a root shell or using sudo, run use these commands:\n"
243 "\n"
244 " Create a cjdns user so it can run unprivileged.\n"
245 " useradd cjdns\n"
246 "\n"
247 " Create a new TUN device and give the cjdns user authority to access it:\n"
248 " /sbin/ip tuntap add mode tun user cjdns\n"
249 " /sbin/ip tuntap list | grep `id -u cjdns`\n"
250 " The output of the last command will tell you the name of the new device.\n"
251 " This is needed to edit the configuration file.\n"
252 "\n"
253 "Step 3:\n"
254 " Edit the configuration file, fill in the key from the node to connect to and your\n"
255 " password as well as the bind address to listen for UDP packets on and the\n"
256 " passwords of other nodes who are allowed to connect to this node.\n"
257 " Also replace \"tunDevice\": \"tun0\" with the name of the TUN device gotten\n"
258 " in step 2\n"
259 "\n"
260 "Step 4:\n"
261 " Get the commands to run in order to prepare your TUN device by running:\n"
262 " %s --getcmds < cjdroute.conf\n"
263 " These commands should be executed as root now every time the system restarts.\n"
264 "\n"
265 "Step 5:\n"
266 " Fire it up!\n"
431770d More documentation and more debugging code.
cjdelisle authored
267 " sudo -u cjdns %s < cjdroute.conf\n"
84f82a3 explained how to delete a tunnel
cjdelisle authored
268 "\n"
269 "Notes:\n"
270 " To delete a tunnel, use this command:\n"
271 " /sbin/ip tuntap del mode tun <name of tunnel>\n"
272 "\n",
fd06d8a Improved 'documentation'
cjdelisle authored
273 appName, appName, appName);
274
275 return 0;
276 }
277
bd259a8 more debugging and got tap working
cjdelisle authored
278 static int getcmds(Dict* config)
279 {
280 uint8_t privateKey[32];
281 struct Address addr;
282 parsePrivateKey(config, &addr, privateKey);
283
284 uint8_t myIp[40];
285 Address_printIp(myIp, &addr);
286
287 Dict* router = benc_lookupDictionary(config, BSTR("router"));
288 Dict* iface = benc_lookupDictionary(router, BSTR("interface"));
289 String* type = benc_lookupString(iface, BSTR("type"));
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
290 String* tunDevice = benc_lookupString(iface, BSTR("tunDevice"));
bd259a8 more debugging and got tap working
cjdelisle authored
291 if (!benc_stringEquals(type, BSTR("TUNInterface"))) {
292 fprintf(stderr, "router.interface.type is not recognized.\n");
293 return -1;
294 }
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
295 char* tunDev = tunDevice ? tunDevice->bytes : "tun0";
bd259a8 more debugging and got tap working
cjdelisle authored
296 if (strrchr(tunDev, '/') != NULL) {
297 tunDev = strrchr(tunDev, '/') + 1;
298 }
299
300 printf("#!/bin/bash\n"
301 "# Run these commands as root to get the interfaces setup properly.\n\n");
302 printf("/sbin/ip addr add %s dev %s\n", myIp, tunDev);
fd06d8a Improved 'documentation'
cjdelisle authored
303 printf("/sbin/ip -6 route add fc00::/8 dev %s\n", tunDev);
304
bd259a8 more debugging and got tap working
cjdelisle authored
305 return 0;
306 }
307
ea252d1 more stuff
cjdelisle authored
308 static void authorizedPassword(String* passwd,
309 Integer* authType,
310 Integer* trust,
311 uint32_t index,
312 struct Context* ctx)
313 {
314 if (!(passwd && authType && trust)) {
315 fprintf(stderr,
316 "authorizedPasswords[%u] is must specify authType, password, and trust.\n",
317 index);
318 exit(-1);
319 }
320 if (*authType < 1 || *authType > 255) {
321 fprintf(stderr,
322 "authorizedPasswords[%u] auth must be between 1 and 255 inclusive.\n",
323 index);
324 exit(-1);
325 }
326 if (*trust < 0) {
327 fprintf(stderr, "authorizedPasswords[%u] trust cannot be negative.\n", index);
328 exit(-1);
329 }
330 printf("adding authorized password.\n");
331 struct User* u = ctx->allocator->malloc(sizeof(struct User), ctx->allocator);
332 u->trust = (uint64_t) *trust;
333 CryptoAuth_addUser(passwd, *authType, u, ctx->ca);
334 }
335
336 static void authorizedPasswords(List* list, struct Context* ctx)
337 {
338 uint32_t count = benc_itemCount(list);
339 for (uint32_t i = 0; i < count; i++) {
340 Dict* d = benc_getDictionary(list, i);
341 if (!d) {
342 fprintf(stderr, "authorizedPasswords[%u] is not a dictionary type.\n", i);
343 exit(-1);
344 }
345 String* passwd = benc_lookupString(d, BSTR("password"));
346 Integer* authType = benc_lookupInteger(d, BSTR("authType"));
347 Integer* trust = benc_lookupInteger(d, BSTR("trust"));
348 authorizedPassword(passwd, authType, trust, i, ctx);
349 }
350 }
351
15a73ac Changed Interface#receiveMessage to return an error code.
cjdelisle authored
352 static uint8_t serverFirstIncoming(struct Message* msg, struct Interface* iface)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
353 {
ea252d1 more stuff
cjdelisle authored
354 struct UDPInterfaceContext* uictx = (struct UDPInterfaceContext*) iface->receiverContext;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
355
ea252d1 more stuff
cjdelisle authored
356 struct Interface* udpDefault = UDPInterface_getDefaultInterface(uictx->udpContext);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
357 assert(udpDefault);
358 UDPInterface_bindToCurrentEndpoint(udpDefault);
359
360 struct User* u = CryptoAuth_getUser(iface);
361 assert(u);
fbbff77 more debugging
cjdelisle authored
362 // Add it to the switch, this will change the receiveMessage for this interface.
ab7ee29 Improved logging, fixed the bug that caused garbage in the table, hel…
cjdelisle authored
363 struct Address addr;
364 memset(&addr, 0, sizeof(struct Address));
365 SwitchCore_addInterface(iface, u->trust, &addr.networkAddress_be, uictx->context->switchCore);
366
367 uint8_t* herKey = CryptoAuth_getHerPublicKey(iface);
368 memcpy(addr.key, herKey, 32);
369 uint8_t printedAddr[60];
370 Address_print(printedAddr, &addr);
371 Log_info1(uictx->context->logger,
372 "Node %s has connected to us.\n",
373 printedAddr);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
374
375 // Prepare for the next connection.
ea252d1 more stuff
cjdelisle authored
376 struct Interface* newUdpDefault = UDPInterface_getDefaultInterface(uictx->udpContext);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
377 struct Interface* newAuthedUdpDefault =
ea252d1 more stuff
cjdelisle authored
378 CryptoAuth_wrapInterface(newUdpDefault, NULL, true, true, uictx->context->ca);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
379 newAuthedUdpDefault->receiveMessage = serverFirstIncoming;
ea252d1 more stuff
cjdelisle authored
380 newAuthedUdpDefault->receiverContext = uictx;
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
381
382 // Send the message on to the switch so the first message isn't lost.
15a73ac Changed Interface#receiveMessage to return an error code.
cjdelisle authored
383 return iface->receiveMessage(msg, iface);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
384 }
385
ea252d1 more stuff
cjdelisle authored
386 static void udpConnectTo(String* connectToAddress,
387 Dict* config,
388 struct UDPInterface* udpContext,
389 struct Context* ctx)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
390 {
ea252d1 more stuff
cjdelisle authored
391 String* password = benc_lookupString(config, BSTR("password"));
392 Integer* authType = benc_lookupInteger(config, BSTR("authType"));
393 String* publicKey = benc_lookupString(config, BSTR("publicKey"));
394 Integer* trust = benc_lookupInteger(config, BSTR("trust"));
395
396 #define FAIL_IF_NULL(cannotBeNull, fieldName) \
397 if (!cannotBeNull) { \
398 fprintf(stderr, \
399 "interfaces.UDPInterface['%s']." fieldName " is not set, " \
400 "this field is mandatory.\n", \
401 connectToAddress->bytes); \
402 exit(-1); \
403 }
404
405 FAIL_IF_NULL(password, "password")
406 FAIL_IF_NULL(authType, "authType")
407 FAIL_IF_NULL(publicKey, "publicKey")
408 FAIL_IF_NULL(trust, "trust")
409
410 #undef FAIL_IF_NULL
411
412 #define CHECK_RANGE(number, min, max) \
413 if (number < min || number > max) { \
414 fprintf(stderr, \
415 "interfaces.UDPInterface['%s'].number must be between min and max\n", \
416 connectToAddress->bytes); \
417 exit(-1); \
418 }
419
420 CHECK_RANGE(*authType, 1, 255)
421 CHECK_RANGE(*trust, 0, INT64_MAX)
422
423 #undef CHECK_RANGE
424
425 uint8_t pkBytes[32];
426 if (publicKey->len < 52 || Base32_decode(pkBytes, 32, (uint8_t*)publicKey->bytes, 52) != 32) {
427 fprintf(stderr,
428 "interfaces.UDPInterface['%s'].publicKey could not be parsed.\n",
429 connectToAddress->bytes);
430 exit(-1);
431 }
432 uint8_t addressBytes[16];
433 AddressCalc_addressForPublicKey(addressBytes, pkBytes);
434 if (addressBytes[0] != 0xFC) {
435 fprintf(stderr,
436 "interfaces.UDPInterface['%s'].publicKey\n( %s )\nis not in FC00/8 range, "
437 "it was probably mistranscribed.\n",
438 connectToAddress->bytes,
439 publicKey->bytes);
440 exit(-1);
441 }
442
443 struct Interface* udp =
444 UDPInterface_addEndpoint(udpContext, connectToAddress->bytes, ctx->eHandler);
445 struct Interface* authedUdp = CryptoAuth_wrapInterface(udp, pkBytes, false, true, ctx->ca);
446 CryptoAuth_setAuth(password, *authType, authedUdp);
447
448 uint64_t switchAddr_be;
449 SwitchCore_addInterface(authedUdp, *trust, &switchAddr_be, ctx->switchCore);
0ae0048 improved key validation, made a distance based NodeCollector used for…
cjdelisle authored
450 struct Address addr;
451 memset(&addr, 0, sizeof(struct Address));
452 memcpy(addr.key, pkBytes, 32);
453 addr.networkAddress_be = switchAddr_be;
454 RouterModule_addNode(&addr, ctx->routerModule);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
455 }
456
ea252d1 more stuff
cjdelisle authored
457 static void configureUDP(Dict* config, struct Context* ctx)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
458 {
ea252d1 more stuff
cjdelisle authored
459 String* bindStr = benc_lookupString(config, BSTR("bind"));
460 char* bindAddress = bindStr ? bindStr->bytes : NULL;
461
462 struct UDPInterface* udpContext =
a2b3372 More logging improvements
cjdelisle authored
463 UDPInterface_new(ctx->base, bindAddress, ctx->allocator, ctx->eHandler, ctx->logger);
ea252d1 more stuff
cjdelisle authored
464
465 if (bindStr) {
466 struct Interface* udpDefault = UDPInterface_getDefaultInterface(udpContext);
467 struct Interface* authedDef =
468 CryptoAuth_wrapInterface(udpDefault, NULL, true, true, ctx->ca);
469
470 struct UDPInterfaceContext* uictx =
471 ctx->allocator->malloc(sizeof(struct UDPInterfaceContext), ctx->allocator);
472 uictx->context = ctx;
473 uictx->udpContext = udpContext;
474 authedDef->receiveMessage = serverFirstIncoming;
475 authedDef->receiverContext = uictx;
476 }
477
478 Dict* connectTo = benc_lookupDictionary(config, BSTR("connectTo"));
479 if (connectTo) {
480 benc_dict_entry_t* entry = *connectTo;
481 while (entry != NULL) {
482 String* key = (String*) entry->key;
483 if (entry->val->type != BENC_DICT) {
484 fprintf(stderr,
485 "interfaces.UDPInterface.connectTo: entry %s is not a dictionary type.\n",
486 key->bytes);
487 abort();
488 }
489 Dict* value = entry->val->as.dictionary;
490
491 udpConnectTo(key, value, udpContext, ctx);
492
493 entry = entry->next;
494 }
495 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
496 }
ea252d1 more stuff
cjdelisle authored
497
7cc5e02 More debugging
cjdelisle authored
498 static void registerRouter(Dict* config, uint8_t myPubKey[32], struct Context* context)
499 {
500 Dict* iface = benc_lookupDictionary(config, BSTR("interface"));
501 if (benc_stringEquals(benc_lookupString(iface, BSTR("type")), BSTR("TUNInterface"))) {
7d6f45f Changed the router config because you use the tun name not path.
cjdelisle authored
502 String* tunPath = benc_lookupString(iface, BSTR("tunDevice"));
7cc5e02 More debugging
cjdelisle authored
503 context->routerIf = TunInterface_new(tunPath, context->base, context->allocator);
504 }
a2b3372 More logging improvements
cjdelisle authored
505 context->routerModule = RouterModule_register(context->registry,
506 context->allocator,
507 myPubKey,
508 context->base,
509 context->logger);
7cc5e02 More debugging
cjdelisle authored
510 }
511
ea252d1 more stuff
cjdelisle authored
512 int main(int argc, char** argv)
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
513 {
55fb7a7 Added KEYS logging level and made logging level choosable at build time.
cjdelisle authored
514 #ifdef Log_KEYS
515 fprintf(stderr, "Log_LEVEL = KEYS, EXPECT TO SEE PRIVATE KEYS IN YOUR LOGS!\n");
516 #endif
ea252d1 more stuff
cjdelisle authored
517 Crypto_init();
518 assert(argc > 0);
519 if (isatty(STDIN_FILENO)) {
bd259a8 more debugging and got tap working
cjdelisle authored
520 if (argc < 2) {
521 return usage(argv[0]);
522 }
523 if (strcmp(argv[1], "--genconf") == 0) {
ea252d1 more stuff
cjdelisle authored
524 return genconf();
525 }
526 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
527
ea252d1 more stuff
cjdelisle authored
528 struct Context context;
529 memset(&context, 0, sizeof(struct Context));
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
530
0f3119b increased available space -- for testing, and increased space in TUN …
cjdelisle authored
531 // Allow it to allocate 4MB
532 context.allocator = MallocAllocator_new(1<<22);
ea252d1 more stuff
cjdelisle authored
533 struct Reader* reader = FileReader_new(stdin, context.allocator);
534 Dict config;
535 if (benc_getJsonBencSerializer()->parseDictionary(reader, context.allocator, &config)) {
536 fprintf(stderr, "Failed to parse configuration.\n");
537 return -1;
538 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
539
bd259a8 more debugging and got tap working
cjdelisle authored
540 if (argc == 2 && strcmp(argv[1], "--getcmds") == 0) {
541 return getcmds(&config);
542 }
543
11af610 Added version information.
cjdelisle authored
544 printf("Version: " Version_STRING "\n");
545
63234a2 Added new logging infrastructure.
cjdelisle authored
546 // Logging
547 struct Writer* logwriter = FileWriter_new(stdout, context.allocator);
548 struct Log logger = { .writer = logwriter };
549 context.logger = &logger;
550
bd259a8 more debugging and got tap working
cjdelisle authored
551 struct Address myAddr;
ea252d1 more stuff
cjdelisle authored
552 uint8_t privateKey[32];
bd259a8 more debugging and got tap working
cjdelisle authored
553 parsePrivateKey(&config, &myAddr, privateKey);
ea252d1 more stuff
cjdelisle authored
554
555 context.eHandler = AbortHandler_INSTANCE;
556 context.base = event_base_new();
63234a2 Added new logging infrastructure.
cjdelisle authored
557 context.switchCore = SwitchCore_new(context.logger, context.allocator);
ff3fee6 Some bug fixes and imrpoved logging.
cjdelisle authored
558 context.ca =
559 CryptoAuth_new(&config, context.allocator, privateKey, context.base, context.logger);
ea252d1 more stuff
cjdelisle authored
560 context.registry = DHTModules_new(context.allocator);
561 ReplyModule_register(context.registry, context.allocator);
562
7cc5e02 More debugging
cjdelisle authored
563 // Router
564 Dict* routerConf = benc_lookupDictionary(&config, BSTR("router"));
bd259a8 more debugging and got tap working
cjdelisle authored
565 registerRouter(routerConf, myAddr.key, &context);
7cc5e02 More debugging
cjdelisle authored
566
ea252d1 more stuff
cjdelisle authored
567 SerializationModule_register(context.registry, context.allocator);
568
569 // Authed passwords.
570 List* authedPasswords = benc_lookupList(&config, BSTR("authorizedPasswords"));
571 if (authedPasswords) {
572 authorizedPasswords(authedPasswords, &context);
573 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
574
ea252d1 more stuff
cjdelisle authored
575 // Interfaces.
576 Dict* interfaces = benc_lookupDictionary(&config, BSTR("interfaces"));
577 Dict* udpConf = benc_lookupDictionary(interfaces, BSTR("UDPInterface"));
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
578
ea252d1 more stuff
cjdelisle authored
579 if (udpConf) {
580 configureUDP(udpConf, &context);
581 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
582
ea252d1 more stuff
cjdelisle authored
583 if (udpConf == NULL) {
584 fprintf(stderr, "No interfaces configured to connect to.\n");
585 return -1;
586 }
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
587
ff3fee6 Some bug fixes and imrpoved logging.
cjdelisle authored
588 Ducttape_register(&config,
589 privateKey,
ddc1976 Renamed SwitchConnectorModule.c Ducttape.c
cjdelisle authored
590 context.registry,
591 context.routerModule,
592 context.routerIf,
593 context.switchCore,
594 context.base,
63234a2 Added new logging infrastructure.
cjdelisle authored
595 context.allocator,
596 context.logger);
0689af8 Halfway through debugging...
cjdelisle authored
597
598 uint8_t address[53];
bd259a8 more debugging and got tap working
cjdelisle authored
599 Base32_encode(address, 53, myAddr.key, 32);
0689af8 Halfway through debugging...
cjdelisle authored
600 printf("Your address is: %s.k\n", address);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
601
ea252d1 more stuff
cjdelisle authored
602 event_base_loop(context.base, 0);
1f05911 Whole bunch of unfinished work for gluing the switch to the router.
cjdelisle authored
603 }
Something went wrong with that request. Please try again.