The purpose of this task is to demonstrate parsing of text-format IP addresses, using IPv4 and IPv6.



Taking the following as inputs:



127.0.0.1

The "localhost" IPv4 address


127.0.0.1:80

The "localhost" IPv4 address, with a specified port (80)


::1

The "localhost" IPv6 address


[::1]:80

The "localhost" IPv6 address, with a specified port (80)


2605:2700:0:3::4713:93e3

Rosetta Code's primary server's public IPv6 address


[2605:2700:0:3::4713:93e3]:80

Rosetta Code's primary server's public IPv6 address, with a specified port (80)



Task

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.



Example

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.




/*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