summaryrefslogtreecommitdiff
path: root/other/bootstrap_node_packets.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2014-02-22 17:06:07 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2014-02-22 17:07:15 -0500
commit5a142bb697651693e4ce7654d2f74e688aab8894 (patch)
tree4268ae9377593d8632efbbe57bb4680744014f2a /other/bootstrap_node_packets.c
parent44c1dfc5fda6393f18ef7b4178c0c32f3d64f357 (diff)
Renamed dht server to dht node
Diffstat (limited to 'other/bootstrap_node_packets.c')
-rw-r--r--other/bootstrap_node_packets.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c
new file mode 100644
index 00000000..ea57b0d2
--- /dev/null
+++ b/other/bootstrap_node_packets.c
@@ -0,0 +1,65 @@
1/* bootstrap_node_packets.c
2 *
3 * Special bootstrap node only packets.
4 *
5 * Include it in your bootstrap node 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}