summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2014-07-09 14:00:16 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2014-07-09 14:00:16 -0400
commit5e1ae35034330f4627ef8e83f0884914a8f880d2 (patch)
treed7064cd21d3a4cdee9613d8f62246af778ee4edb
parent881ccced56f486b41c3ad0ac0b8312b7ab43bc6b (diff)
Added a script to query DHT bootstrap node info
-rw-r--r--other/fun/bootstrap_node_info.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/other/fun/bootstrap_node_info.py b/other/fun/bootstrap_node_info.py
new file mode 100644
index 00000000..17349334
--- /dev/null
+++ b/other/fun/bootstrap_node_info.py
@@ -0,0 +1,98 @@
1#!/usr/bin/env python
2"""
3Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com>
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
22"""
23
24from socket import *
25import sys
26
27if sys.version_info[0] == 2:
28 print("This script requires Python 3+ in order to run.")
29 sys.exit(1)
30
31def printHelp():
32 print("Usage: " + sys.argv[0] + " <ipv4|ipv6> <ip/hostname> <port>")
33 print(" Example: " + sys.argv[0] + " ipv4 192.210.149.121 33445")
34 print(" Example: " + sys.argv[0] + " ipv4 23.226.230.47 33445")
35 print(" Example: " + sys.argv[0] + " ipv4 biribiri.org 33445")
36 print(" Example: " + sys.argv[0] + " ipv4 cerberus.zodiaclabs.org 33445")
37 print(" Example: " + sys.argv[0] + " ipv6 2604:180:1::3ded:b280 33445")
38 print("")
39 print("Return values:")
40 print(" 0 - received info reply from a node")
41 print(" 1 - incorrect command line arguments")
42 print(" 2 - didn't receive any reply from a node")
43 print(" 3 - received a malformed/unexpected reply")
44
45if len(sys.argv) != 4:
46 printHelp()
47 sys.exit(1)
48
49protocol = sys.argv[1]
50ip = sys.argv[2]
51port = int(sys.argv[3])
52
53INFO_PACKET_ID = b"\xF0" # https://github.com/irungentoo/toxcore/blob/4940c4c62b6014d1f0586aa6aca7bf6e4ecfcf29/toxcore/network.h#L128
54INFO_REQUEST_PACKET_LENGTH = 78 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L28
55# first byte is INFO_REQUEST_ID, other bytes don't matter as long as reqest's length matches INFO_REQUEST_LENGTH
56INFO_REQUEST_PACKET = INFO_PACKET_ID + ( b"0" * (INFO_REQUEST_PACKET_LENGTH - len(INFO_PACKET_ID)) )
57
58PACKET_ID_LENGTH = len(INFO_PACKET_ID)
59VERSION_LENGTH = 4 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L44
60MAX_MOTD_LENGTH = 256 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L26
61
62MAX_INFO_RESPONSE_PACKET_LENGTH = PACKET_ID_LENGTH + VERSION_LENGTH + MAX_MOTD_LENGTH
63
64SOCK_TIMEOUT_SECONDS = 1.0
65
66sock = None
67
68if protocol == "ipv4":
69 sock = socket(AF_INET, SOCK_DGRAM)
70elif protocol == "ipv6":
71 sock = socket(AF_INET6, SOCK_DGRAM)
72else:
73 print("Invalid first argument")
74 printHelp()
75 sys.exit(1)
76
77sock.sendto(INFO_REQUEST_PACKET, (ip, port))
78
79sock.settimeout(SOCK_TIMEOUT_SECONDS)
80
81try:
82 data, addr = sock.recvfrom(MAX_INFO_RESPONSE_PACKET_LENGTH)
83except timeout:
84 print("The DHT bootstrap node didn't reply in " + str(SOCK_TIMEOUT_SECONDS) + " sec.")
85 print("The likely reason for that is that the DHT bootstrap node is either offline or has no info set.")
86 sys.exit(2)
87
88packetId = data[:PACKET_ID_LENGTH]
89if packetId != INFO_PACKET_ID:
90 print("Bad response, first byte should be", INFO_PACKET_ID, "but got", packetId, "(", data, ")")
91 print("Are you sure that you are pointing the script at a Tox DHT bootstrap node and that the script is up to date?")
92 sys.exit(3)
93
94version = int.from_bytes(data[PACKET_ID_LENGTH:PACKET_ID_LENGTH + VERSION_LENGTH], byteorder='big')
95motd = data[PACKET_ID_LENGTH + VERSION_LENGTH:].decode("utf-8")
96print("Version: " + str(version))
97print("MOTD: " + motd)
98sys.exit(0) \ No newline at end of file