diff options
Diffstat (limited to 'other/bootstrap_server_packets.c')
-rw-r--r-- | other/bootstrap_server_packets.c | 65 |
1 files changed, 65 insertions, 0 deletions
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 | |||
30 | static uint32_t bootstrap_version; | ||
31 | static uint8_t bootstrap_motd[MAX_MOTD_LENGTH]; | ||
32 | static 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 | */ | ||
37 | static 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 | |||
54 | int 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 | } | ||