Canonicalize CIDR: Difference between revisions
Content added Content deleted
(→OCaml: add) |
(Added uBasic/4tH version) |
||
Line 1,804: | Line 1,804: | ||
(str-inaddr-net @1.addr @1.prefix)))</syntaxhighlight> |
(str-inaddr-net @1.addr @1.prefix)))</syntaxhighlight> |
||
=={{header|uBasic/4tH}}== |
|||
<syntaxhighlight lang="uBasic/4tH">If Try (_CanonicalizeCIDR (Dup("36.18.154.103/12"))) Then Print "Illegal IP" |
|||
If Try (_CanonicalizeCIDR (Dup("62.62.197.11/29"))) Then Print "Illegal IP" |
|||
If Try (_CanonicalizeCIDR (Dup("67.137.119.181/4"))) Then Print "Illegal IP" |
|||
If Try (_CanonicalizeCIDR (Dup("161.214.74.0/24"))) Then Print "Illegal IP" |
|||
If Try (_CanonicalizeCIDR (Dup("184.232.176.184/18"))) Then Print "Illegal IP" |
|||
End |
|||
_CanonicalizeCIDR ' do the whole shebang |
|||
Param (1) ' IP string |
|||
Local (3) |
|||
b@ = FUNC(_GetIP (a@)) ' get IP address |
|||
c@ = FUNC(_GetMask(a@)) ' get the mask |
|||
d@ = FUNC(_Canonicalize (b@, c@)) ' canonicalize IP address |
|||
' now print the report |
|||
Print Show(a@); Tab(20); " -> "; FUNC(_IP(d@, 24)); "."; |
|||
Print FUNC(_IP(d@, 16)); "."; FUNC(_IP(d@, 8)); "."; AND(d@, 255); "/"; c@ |
|||
Return |
|||
_IP Param (2) : Return (AND(255, SHL(a@, -b@))) |
|||
_Canonicalize Param (2) : Return (AND(a@, FUNC(_MakeMask (b@)))) |
|||
_GetMask Param (1) : Return (Val(Chop(a@, o))) |
|||
_MakeMask Param (1) : Return (NOT(SHL(1, 32 - a@) - 1)) |
|||
_GetIP ' get the IP address |
|||
Param (1) ' IP string |
|||
Local (1) ' IP number |
|||
o = 0 |
|||
b@ = SHL(FUNC(_Parse (a@, Ord("."))), 24) |
|||
b@ = OR(b@, SHL(FUNC(_Parse (a@, Ord("."))), 16)) |
|||
b@ = OR(b@, SHL(FUNC(_Parse (a@, Ord("."))), 8)) |
|||
b@ = OR(b@, FUNC(_Parse (a@, Ord("/")))) |
|||
Return (b@) |
|||
_Parse ' parse a token |
|||
Param (2) ' string, delimiter |
|||
Local (1) ' token |
|||
c@ = Dup ("") ' start with an empty token |
|||
For o = o to Len (a@) - 1 Until Peek (a@, o) = b@ |
|||
c@ = Join (c@, Char (Peek (a@, o))) |
|||
Next |
|||
o = o + 1 |
|||
Return (Val(c@))</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>36.18.154.103/12 -> 36.16.0.0/12 |
|||
62.62.197.11/29 -> 62.62.197.8/29 |
|||
67.137.119.181/4 -> 64.0.0.0/4 |
|||
161.214.74.0/24 -> 161.214.74.0/24 |
|||
184.232.176.184/18 -> 184.232.128.0/18 |
|||
0 OK, 0:388</pre> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Again SHell}} |
{{works with|Bourne Again SHell}} |