1#!/usr/bin/awk -f
2
3# This file is part of the aMule project.
4#
5# Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6# Copyright (c) 2004-2011 xmb ( http://xmb.ath.cx )
7# Copyright (c) 2004-2011 Jacobo Vilella (Jacobo221)
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.
13# 
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.
18# 
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
22#
23
24##########################################
25#                                        #
26#   Convert eD2k HighID numbers to IPs   #
27#                                        #
28##########################################
29#                                        #
30#          Original code: xmb            #
31#  Further code improvements: Jacobo221  #
32#                                        #
33#  Contact: IRC @ irc.freenode.net/#awk  #
34#           jacobo221 at @amule dot org  #
35#                                        #
36##########################################
37#                                        #
38#  This code is distributed under terms  #
39#          of the GPL license            #
40#  http://www.gnu.org/copyleft/gpl.html  #
41#                                        #
42##########################################
43#                                        #
44#       Usage: id2ip.awk ID <...>        #
45#                                        #
46##########################################
47
48BEGIN {
49
50	if (ARGC == 1) {
51		printf "Usage: id2ip.awk ID <...>\n"
52		exit
53	}
54
55	while (num = ARGV[++i]) {
56
57		if (ARGV[i] < 16777216) {
58			printf "%s -> LowID\n",ARGV[i]
59		} else if (ARGV[i] > 256*256*256*256) {
60			printf "%s -> Invalid IP\n",ARGV[i]
61		} else {
62
63			m = 256 * 256 * 256
64
65			for (c = 0; m > 0; c++ ) {
66				IP[c] = int(num / m)
67				num -= IP[c] * m
68				m /= 256
69			}
70
71			printf "%s -> %d.%d.%d.%d\n", ARGV[i], IP[3], IP[2], IP[1], IP[0]
72		}
73	}
74
75}
76