How to resolve the algorithm Parse an IP Address step by step in the REXX programming language
How to resolve the algorithm Parse an IP Address step by step in the REXX programming language
Table of Contents
Problem Statement
The purpose of this task is to demonstrate parsing of text-format IP addresses, using IPv4 and IPv6.
Taking the following as inputs:
Emit each described IP address as a hexadecimal integer representing the address, the address space, and the port number specified, if any.
In languages where variant result types are clumsy, the result should be ipv4 or ipv6 address number, something which says which address space was represented, port number and something that says if the port was specified.
127.0.0.1 has the address number 7F000001 (2130706433 decimal)
in the ipv4 address space.
::ffff:127.0.0.1 represents the same address in the ipv6 address space where it has the
address number FFFF7F000001 (281472812449793 decimal).
::1 has address number 1 and serves the same purpose in the ipv6 address
space that 127.0.0.1 serves in the ipv4 address space.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Parse an IP Address step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program parses an IP address into ──► IPv4 or IPv6 format, optional pport.*/
_= "_"; say center('input IP address' , 30),
center('hex IP address' , 32),
center('decimal IP address' , 39) "space port"
say copies(_, 30) copies(_, 32) copies(_, 39) copies(_, 5) copies(_, 5)
call IP_parse 127.0.0.1 /*this simple IP doesn't need quotes.*/
call IP_parse '127.0.0.1:80'
call IP_parse '::1'
call IP_parse '[::1]:80'
call IP_parse '2605:2700:0:3::4713:93e3'
call IP_parse '[2605:2700:0:3::4713:93e3]:80'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
IP_parse: procedure; parse arg a .; hx=; @.=; numeric digits 50
dot= pos(., a)\==0 /*see if there is a dot present in IP. */
if dot then do; parse var a @.1 '.' @.2 "." @.3 '.' @.4 ":" port
do j=1 for 4; hx= hx || d2x(@.j, 2)
end /*j*/
end
else do; parse var a pureA ']:' port
_= space( translate( pureA, , '[]'), 0) /*remove brackets.*/
parse var _ x '::' y
do L=1 until x=='' /*get left side. */
parse var x @.L ':' x
end /*L*/
y= reverse(y)
do r=8 by -1 /*get right side. */
parse var y z ':' y; if z=='' then leave
@.r= reverse(z)
end /*r*/
do k=1 for 8; hx=hx || right( word(@.k 0, 1), 4, 0)
end /*k*/
end
say left(a,30) right(hx,32) right(x2d(hx),39) ' IPv' || (6-2*dot) right(port,5)
return
/* REXX ***************************************************************
* 27.05.2013 Walter Pachl
**********************************************************************/
Numeric Digits 100
say center('input IP address' ,30),
center('hex IP address' ,32),
center('decimal IP address' ,39) 'space port'
say copies(_,30) copies(_,32) copies(_,39) copies(_,5) copies(_,5) /*hdr*/
call expand '127.0.0.1'
call expand '127.0.0.1:80'
call expand '::1'
call expand '[::1]:80'
call expand '2605:2700:0:3::4713:93e3'
call expand '[2605:2700:0:3::4713:93e3]:80'
Say ' '
Say 'The following 2 are the same'
Call expand '2001:0db8:0:0:0:0:1428:57ab'
Call expand '2001:db8::1428:57ab'
Say ' '
Say 'The following 3 are all the same'
Call expand '2001:0db8:0:0:8d3:0:0:0'
Call expand '2001:db8:0:0:8d3::'
Call expand '2001:db8::8d3:0:0:0'
Exit
expand: Procedure
Parse Arg s
If pos('.',s)>0 Then
Parse Value expand_ip4(s) With exp space port
Else
Parse Value expand_ip6(s) With exp space port
Say left(s,30) right(exp,32) right(x2d(exp),39) right(space,5) right(port,5)
Return
expand_ip4: Procedure
Parse Arg s
If pos(':',s)>0 Then
Parse Var s s ':' port
Else
port=''
Do i=1 To 4
Parse Var s a.i '.' s
End
res=''
Do i=1 To 4
res=res||d2x(a.i,2)
End
Return res 'IPv4' port
expand_ip6: Procedure
/**********************************************************************
* Note: Doublecolon ('::') requires the inclusion of as many 0000
* tokens as necessary to result in 8 tokens
**********************************************************************/
Parse Arg s
If pos(']:',s)>0 Then
Parse Var s '[' s ']:' port
Else
port=''
sc=s
ii=0
Do i=1 To 8 While s<>''
Parse Var s x.i ':' s
If left(s,1)=':' Then Do
ii=i
s=substr(s,2)
End
End
n=i-1
ol=''
o2=''
Do i=1 To n
ol=ol||right(x.i,4,'0')
o2=o2||right(x.i,4,'0')
If i=ii Then Do
ol=ol||'----'
Do j=1 To 8-n
o2=o2||'0000'
End
End
End
Return o2 'IPv6' port
You may also check:How to resolve the algorithm Search a list of records step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the Rust programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Batch File programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the Elixir programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Fortran programming language