Upstream tarball 20080630
[amule.git] / src / utils / scripts / kadnodescreate.pl
blob9a4ec55119e474f52e2caef5726a4e277724aec3
1 #!/usr/bin/perl
4 ## This file is part of the aMule Project
5 ##
6 ## Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
7 ## Copyright (c) 2006-2008 aMule Project ( http://www.amule-project.net )
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 File::Copy;
24 use warnings;
25 use strict;
27 my $exit_with_help;
29 if (!($ARGV[0])) {
30 print "You must specify at least one ip.\n";
31 $exit_with_help = "true";
34 if ($exit_with_help) {
35 die "Usage: kadnodescreate.pl [hash:ip:TCPport:UDPport:type]+\n";
39 print "Creating nodes.dat...\n";
41 #Open the new file
42 open(MET," > nodes.dat");
43 binmode MET;
45 my $contactcount = $#ARGV + 1;
47 print "\tContacts: " . $contactcount . "\n";
49 print MET &int32_string($contactcount);
51 my $contact;
52 my $hash;
53 my $ip;
54 my $tcpport;
55 my $udpport;
56 my $type;
58 my $contactnumber = 0;
59 foreach $contact (@ARGV) {
60 $contactnumber++;
61 if ($contact =~ /^(.*):(.*):(.*):(.*):(.*)$/) {
63 $hash = &check_hash($1);
64 if ($hash == 0) {
65 die "Malformed hash, can't continue: " . $1 . "\n";
68 $ip = &check_ip($2);
69 if ($ip == 0) {
70 die "Malformed ip, can't continue: " . $2 . "\n";
73 my $tcpport = &check_port($3);
74 if ($tcpport == 0) {
75 die "Malformed tcp port, can't continue: " . $3 . "\n";
78 $udpport = &check_port($4);
79 if ($udpport == 0) {
80 die "Malformed udp port, can't continue: " . $4 . "\n";
83 $type = &check_type($5);
84 if ($type == 9) {
85 die "Malformed contact type, can't continue: " . $5 . "\n";
89 print "\t\tAdding Contact " . $contactnumber . ":\n";
90 print "\t\t\tHash : " . $1 . "\n";
91 print "\t\t\tIP : " . $ip . "\n";
92 print "\t\t\tTCPPort : " . $tcpport . "\n";
93 print "\t\t\tUDPPort : " . $udpport . "\n";
94 print "\t\t\tType : " . $type . "\n";
96 print MET &hash_string($1) .
97 &int32_string($ip) .
98 &int16_string($tcpport) .
99 &int16_string($udpport) .
100 &byte_string($type);
101 } else {
102 die "Malformed contact line, can't continue: " . $contact . "\n";
106 print "Closing nodes.dat\n\n";
107 close(MET);
110 # Functions
112 sub check_ip {
113 my $ipresult = 0;
114 if ($_[0] =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
115 $ipresult = ($1*16777216) + ($2*65536) + ($3*256) + $4;
117 $ipresult;
120 sub check_port {
121 my $portresult = 0;
122 if ($_[0] =~ /^([0-9]{1,5})$/) {
123 if ($1 < 65535) {
124 $portresult = $1;
127 $portresult;
130 sub check_type {
131 my $typeresult = -1;
132 if ($_[0] =~ /^([0-9])$/) {
133 $typeresult = $1;
135 $typeresult;
138 sub check_hash {
139 my $hashresult = 0;
140 if ($_[0] =~ /^([A-Z]|[0-9]|[a-z]){32}$/) {
141 $hashresult = 1;
143 $hashresult;
146 #Hex write functions
148 sub byte_string {
149 sprintf("%c",$_[0]);
152 sub int16_string {
153 &byte_string($_[0] % 256) . &byte_string($_[0] / 256);
156 sub int32_string {
157 &int16_string($_[0] % 65536) . &int16_string($_[0] / 65536);
160 sub int64_string {
161 &int32_string($_[0] % 4294967296) . &int32_string($_[0] / 4294967296);
164 sub hash_string {
165 my $i = 0;
166 my $final_string = "";
167 while ($i < 32) {
168 $final_string = $final_string . &byte_string(hex(substr($_[0],$i,2)));
169 $i += 2;
171 $final_string;