summaryrefslogtreecommitdiff
path: root/other
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-02-01 18:45:37 -0500
committerirungentoo <irungentoo@gmail.com>2014-02-01 18:45:37 -0500
commitad5d58b4a2908946458c34dbd0034a8a60ff5f7e (patch)
tree1b08d130b093db0f9efd55b6d3de334efdb0dc55 /other
parentd5c005f02462b996c067d7ed4f1ef71bbd4e5dd2 (diff)
Added DHT bootstrap server info packets.
define DHT_SERVER_EXTRA_PACKETS to enable.
Diffstat (limited to 'other')
-rw-r--r--other/DHT_bootstrap.c12
-rw-r--r--other/bootstrap_server_packets.c65
2 files changed, 76 insertions, 1 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c
index ebb0ab78..307f80f6 100644
--- a/other/DHT_bootstrap.c
+++ b/other/DHT_bootstrap.c
@@ -33,6 +33,13 @@
33 33
34#include "../testing/misc_tools.c" 34#include "../testing/misc_tools.c"
35 35
36#ifdef DHT_SERVER_EXTRA_PACKETS
37#include "./bootstrap_server_packets.c"
38
39#define DHT_VERSION_NUMBER 1
40#define DHT_MOTD "This is a test motd"
41#endif
42
36/* Sleep function (x = milliseconds) */ 43/* Sleep function (x = milliseconds) */
37#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 44#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
38#define c_sleep(x) Sleep(1*x) 45#define c_sleep(x) Sleep(1*x)
@@ -45,7 +52,6 @@
45#define PORT 33445 52#define PORT 33445
46 53
47 54
48
49void manage_keys(DHT *dht) 55void manage_keys(DHT *dht)
50{ 56{
51 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 57 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
@@ -105,6 +111,10 @@ int main(int argc, char *argv[])
105 Onion *onion = new_onion(dht); 111 Onion *onion = new_onion(dht);
106 Onion_Announce *onion_a = new_onion_announce(dht); 112 Onion_Announce *onion_a = new_onion_announce(dht);
107 113
114#ifdef DHT_SERVER_EXTRA_PACKETS
115 bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
116#endif
117
108 if (!(onion && onion_a)) { 118 if (!(onion && onion_a)) {
109 printf("Something failed to initialize.\n"); 119 printf("Something failed to initialize.\n");
110 exit(1); 120 exit(1);
diff --git a/other/bootstrap_server_packets.c b/other/bootstrap_server_packets.c
new file mode 100644
index 00000000..c9297535
--- /dev/null
+++ b/other/bootstrap_server_packets.c
@@ -0,0 +1,65 @@
1/* bootstrap_server_packets.c
2 *
3 * Special bootstrap server only packets.
4 *
5 * Include it in your bootstrap server and use: bootstrap_set_callbacks() to enable.
6 *
7 * Copyright (C) 2013 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#define MAX_MOTD_LENGTH 256 /* I recommend you use a maximum of 96 bytes. The hard maximum is this though. */
27
28#define INFO_REQUEST_PACKET_LENGTH 78
29
30static uint32_t bootstrap_version;
31static uint8_t bootstrap_motd[MAX_MOTD_LENGTH];
32static uint16_t bootstrap_motd_length;
33
34/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH
35 * with the first byte being BOOTSTRAP_INFO_PACKET_ID
36 */
37static int handle_info_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
38{
39 if (length != INFO_REQUEST_PACKET_LENGTH)
40 return 1;
41
42 uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
43 data[0] = BOOTSTRAP_INFO_PACKET_ID;
44 memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
45 uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
46 memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length);
47
48 if (sendpacket(object, source, data, len) == len)
49 return 0;
50
51 return 1;
52}
53
54int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *motd, uint16_t motd_length)
55{
56 if (motd_length > MAX_MOTD_LENGTH)
57 return -1;
58
59 bootstrap_version = htonl(version);
60 memcpy(bootstrap_motd, motd, motd_length);
61 bootstrap_motd_length = motd_length;
62
63 networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net);
64 return 0;
65}