diff options
author | notsecure <notsecure@marek.ca> | 2014-07-10 10:34:48 -0400 |
---|---|---|
committer | notsecure <notsecure@marek.ca> | 2014-07-10 10:34:48 -0400 |
commit | a754cb543733f3dddf2d7ff305fd9b1868e4b733 (patch) | |
tree | d4d8c6c38f5a3de576939ffbe33b8657f82f626d /other | |
parent | e2d388b13799ddbe5ba2a81b442d6481cec5b611 (diff) | |
parent | e334188353a9fad7d6d9f925b33a1684cdb4d958 (diff) |
Merge branch 'master' of https://github.com/irungentoo/toxcore into split-video
Diffstat (limited to 'other')
-rw-r--r-- | other/fun/bootstrap_node_info.py | 98 |
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 | """ | ||
3 | Copyright (c) 2014 by nurupo <nurupo.contributions@gmail.com> | ||
4 | |||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | of this software and associated documentation files (the "Software"), to deal | ||
7 | in the Software without restriction, including without limitation the rights | ||
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | copies of the Software, and to permit persons to whom the Software is | ||
10 | furnished to do so, subject to the following conditions: | ||
11 | |||
12 | The above copyright notice and this permission notice shall be included in | ||
13 | all copies or substantial portions of the Software. | ||
14 | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
21 | THE SOFTWARE. | ||
22 | """ | ||
23 | |||
24 | from socket import * | ||
25 | import sys | ||
26 | |||
27 | if sys.version_info[0] == 2: | ||
28 | print("This script requires Python 3+ in order to run.") | ||
29 | sys.exit(1) | ||
30 | |||
31 | def 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 | |||
45 | if len(sys.argv) != 4: | ||
46 | printHelp() | ||
47 | sys.exit(1) | ||
48 | |||
49 | protocol = sys.argv[1] | ||
50 | ip = sys.argv[2] | ||
51 | port = int(sys.argv[3]) | ||
52 | |||
53 | INFO_PACKET_ID = b"\xF0" # https://github.com/irungentoo/toxcore/blob/4940c4c62b6014d1f0586aa6aca7bf6e4ecfcf29/toxcore/network.h#L128 | ||
54 | INFO_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 | ||
56 | INFO_REQUEST_PACKET = INFO_PACKET_ID + ( b"0" * (INFO_REQUEST_PACKET_LENGTH - len(INFO_PACKET_ID)) ) | ||
57 | |||
58 | PACKET_ID_LENGTH = len(INFO_PACKET_ID) | ||
59 | VERSION_LENGTH = 4 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L44 | ||
60 | MAX_MOTD_LENGTH = 256 # https://github.com/irungentoo/toxcore/blob/881b2d900d1998981fb6b9938ec66012d049635f/other/bootstrap_node_packets.c#L26 | ||
61 | |||
62 | MAX_INFO_RESPONSE_PACKET_LENGTH = PACKET_ID_LENGTH + VERSION_LENGTH + MAX_MOTD_LENGTH | ||
63 | |||
64 | SOCK_TIMEOUT_SECONDS = 1.0 | ||
65 | |||
66 | sock = None | ||
67 | |||
68 | if protocol == "ipv4": | ||
69 | sock = socket(AF_INET, SOCK_DGRAM) | ||
70 | elif protocol == "ipv6": | ||
71 | sock = socket(AF_INET6, SOCK_DGRAM) | ||
72 | else: | ||
73 | print("Invalid first argument") | ||
74 | printHelp() | ||
75 | sys.exit(1) | ||
76 | |||
77 | sock.sendto(INFO_REQUEST_PACKET, (ip, port)) | ||
78 | |||
79 | sock.settimeout(SOCK_TIMEOUT_SECONDS) | ||
80 | |||
81 | try: | ||
82 | data, addr = sock.recvfrom(MAX_INFO_RESPONSE_PACKET_LENGTH) | ||
83 | except 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 | |||
88 | packetId = data[:PACKET_ID_LENGTH] | ||
89 | if 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 | |||
94 | version = int.from_bytes(data[PACKET_ID_LENGTH:PACKET_ID_LENGTH + VERSION_LENGTH], byteorder='big') | ||
95 | motd = data[PACKET_ID_LENGTH + VERSION_LENGTH:].decode("utf-8") | ||
96 | print("Version: " + str(version)) | ||
97 | print("MOTD: " + motd) | ||
98 | sys.exit(0) \ No newline at end of file | ||