blob: 9e7261398b950156d9daf2cec5bf47f7720eabda [file] [log] [blame]
Jari Aaltocce855b1998-04-17 19:52:44 +00001#
Chet Rameyac50fba2014-02-26 09:36:43 -05002# Chet Ramey <chet.ramey@case.edu>
3#
4# Copyright 2002 Chester Ramey
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2, or (at your option)
9# any later version.
10#
11# TThis program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software Foundation,
18# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20#
Jari Aaltocce855b1998-04-17 19:52:44 +000021# inet2hex - Internet address conversion, dotted-decimal to hex
22#
23inet2hex ()
24{
25 local IFS
26
27 IFS=.
28 set -- $1
29
30 if (( $# != 4 )); then
31 echo "inet2hex: incorrect input format: $1" >&2
32 echo "inet2hex: usage: inet2hex XX.XX.XX.XX" >&2
33 return 2
34 fi
35
36 printf "0x%02x%02x%02x%02x\n" $1 $2 $3 $4
37}
38
39#
40# hex2inet - Internet address conversion, hex to dotted-decimal
41#
42hex2inet ()
43{
44 local x1 x2 x3 x4
Jari Aaltob72432f1999-02-19 17:11:39 +000045 local rev
46
47 OPTIND=1
48 while getopts "r" o
49 do
50 case "$o" in
51 r) rev=true;;
Jari Aalto7117c2d2002-07-17 14:10:11 +000052 *) echo "hex2inet: usage: hex2inet [-r] [0x]XXXXXXXX" >&2 ; exit 2;;
Jari Aaltob72432f1999-02-19 17:11:39 +000053 esac
54 done
55 shift $(( $OPTIND - 1 ))
Jari Aaltocce855b1998-04-17 19:52:44 +000056
57 case "$1" in
58 0x*) h=${1#??} ;;
59 *) h=$1 ;;
60 esac
61
62 if (( ${#h} != 8 )); then
63 echo "hex2inet: $h not in inet format" >&2
64 echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2
65 return 2
66 fi
67
68 x1=$(( 0x${h:0:2} ))
69 x2=$(( 0x${h:2:2} ))
70 x3=$(( 0x${h:4:2} ))
71 x4=$(( 0x${h:6:2} ))
72
Jari Aaltob72432f1999-02-19 17:11:39 +000073 if [ -z "$rev" ] ; then
74 printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4
75 else
76 printf "%d.%d.%d.%d\n" $x4 $x3 $x2 $x1
77 fi
78 return 0
Jari Aaltocce855b1998-04-17 19:52:44 +000079}