debian: fix build-deps for focal
[amule.git] / src / utils / scripts / kadnodescreate.pl
blob0f54d41918259a0d1cd8f8d774709b774ecd0b96
1 #!/usr/bin/perl
4 ## This file is part of the aMule Project
5 ##
6 ## Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
7 ## Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
8 ##
9 ## This program is free software; you can redistribute it and/or
10 ## modify it under the terms of the GNU General Public License
11 ## as published by the Free Software Foundation; either
12 ## version 2 of the License, or (at your option) any later version.
14 ## This program is distributed in the hope that it will be useful,
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ## GNU General Public License for more details.
19 ## You should have received a copy of the GNU General Public License
20 ## along with this program; if not, write to the Free Software
21 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 use warnings;
24 use strict;
26 my $exit_with_help;
28 if (!($ARGV[0])) {
29 print "You must specify at least one ip.\n";
30 $exit_with_help = "true";
33 if ($exit_with_help) {
34 die "Usage: kadnodescreate.pl [hash:ip:TCPport:UDPport:type]+\n";
38 print "Creating nodes.dat...\n";
40 #Open the new file
41 open(MET," > nodes.dat");
42 binmode MET;
44 my $contactcount = $#ARGV + 1;
46 print "\tContacts: " . $contactcount . "\n";
48 print MET &int32_string($contactcount);
50 my $contact;
51 my $hash;
52 my $ip;
53 my $tcpport;
54 my $udpport;
55 my $type;
57 my $contactnumber = 0;
58 foreach $contact (@ARGV) {
59 $contactnumber++;
60 if ($contact =~ /^(.*):(.*):(.*):(.*):(.*)$/) {
62 $hash = &check_hash($1);
63 if ($hash == 0) {
64 die "Malformed hash, can't continue: " . $1 . "\n";
67 $ip = &check_ip($2);
68 if ($ip == 0) {
69 die "Malformed ip, can't continue: " . $2 . "\n";
72 my $tcpport = &check_port($3);
73 if ($tcpport == 0) {
74 die "Malformed tcp port, can't continue: " . $3 . "\n";
77 $udpport = &check_port($4);
78 if ($udpport == 0) {
79 die "Malformed udp port, can't continue: " . $4 . "\n";
82 $type = &check_type($5);
83 if ($type == 9) {
84 die "Malformed contact type, can't continue: " . $5 . "\n";
88 print "\t\tAdding Contact " . $contactnumber . ":\n";
89 print "\t\t\tHash : " . $1 . "\n";
90 print "\t\t\tIP : " . $ip . "\n";
91 print "\t\t\tTCPPort : " . $tcpport . "\n";
92 print "\t\t\tUDPPort : " . $udpport . "\n";
93 print "\t\t\tType : " . $type . "\n";
95 print MET &hash_string($1) .
96 &int32_string($ip) .
97 &int16_string($tcpport) .
98 &int16_string($udpport) .
99 &byte_string($type);
100 } else {
101 die "Malformed contact line, can't continue: " . $contact . "\n";
105 print "Closing nodes.dat\n\n";
106 close(MET);
109 # Functions
111 sub check_ip {
112 my $ipresult = 0;
113 if ($_[0] =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
114 $ipresult = ($1*16777216) + ($2*65536) + ($3*256) + $4;
116 $ipresult;
119 sub check_port {
120 my $portresult = 0;
121 if ($_[0] =~ /^([0-9]{1,5})$/) {
122 if ($1 < 65535) {
123 $portresult = $1;
126 $portresult;
129 sub check_type {
130 my $typeresult = -1;
131 if ($_[0] =~ /^([0-9])$/) {
132 $typeresult = $1;
134 $typeresult;
137 sub check_hash {
138 my $hashresult = 0;
139 if ($_[0] =~ /^([A-Z]|[0-9]|[a-z]){32}$/) {
140 $hashresult = 1;
142 $hashresult;
145 #Hex write functions
147 sub byte_string {
148 sprintf("%c",$_[0]);
151 sub int16_string {
152 &byte_string($_[0] % 256) . &byte_string($_[0] / 256);
155 sub int32_string {
156 &int16_string($_[0] % 65536) . &int16_string($_[0] / 65536);
159 sub int64_string {
160 &int32_string($_[0] % 4294967296) . &int32_string($_[0] / 4294967296);
163 sub hash_string {
164 my $i = 0;
165 my $final_string = "";
166 while ($i < 32) {
167 $final_string = $final_string . &byte_string(hex(substr($_[0],$i,2)));
168 $i += 2;
170 $final_string;