Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
56
Task/Parse-an-IP-Address/Crystal/parse-an-ip-address.cr
Normal file
56
Task/Parse-an-IP-Address/Crystal/parse-an-ip-address.cr
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
require "socket"
|
||||
|
||||
def parse_ip_address (s)
|
||||
if m = /^(?:\[(.+)\]|([^:]+))(?::(\d+))?$/.match(s)
|
||||
ip6, ip4, port = m[1]?, m[2]?, m[3]?
|
||||
else
|
||||
ip6 = s
|
||||
end
|
||||
port = port.to_i if port
|
||||
return {nil, nil} if port && !Socket::IPAddress.valid_port?(port)
|
||||
hex = if ip6
|
||||
Socket::IPAddress.parse_v6_fields?(ip6).try &.map {|i| "%04X" % i }.join
|
||||
elsif ip4
|
||||
Socket::IPAddress.parse_v4_fields?(ip4).try &.map {|i| "%02X" % i }.join
|
||||
end
|
||||
{ hex, (port if hex) }
|
||||
end
|
||||
|
||||
cases = %w(
|
||||
127.0.0.1
|
||||
127.0.0.1:80
|
||||
::1
|
||||
[::1]:80
|
||||
2605:2700:0:3::4713:93e3
|
||||
[2605:2700:0:3::4713:93e3]:80
|
||||
2001:db8:85a3:0:0:8a2e:370:7334
|
||||
2001:db8:85a3::8a2e:370:7334
|
||||
[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443
|
||||
192.168.0.1
|
||||
::ffff:192.168.0.1
|
||||
::ffff:71.19.147.227
|
||||
[::ffff:71.19.147.227]:80
|
||||
::
|
||||
256.0.0.0
|
||||
g::1
|
||||
0000
|
||||
0000:0000
|
||||
0000:0000:0000:0000:0000:0000:0000:0000
|
||||
0000:0000:0000::0000:0000
|
||||
0000::0000::0000:0000
|
||||
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
|
||||
ffff:ffff:ffff:fffg:ffff:ffff:ffff:ffff
|
||||
fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
|
||||
fff:ffff:0:ffff:ffff:ffff:ffff:ffff
|
||||
)
|
||||
|
||||
maxwd = cases.max_of &.size
|
||||
puts "%-*s family hex port" % {maxwd, "address"}
|
||||
puts "%s------------------------------------------------" % {"-"*maxwd}
|
||||
cases.each do |addr|
|
||||
hex, port = parse_ip_address addr
|
||||
puts "%-*s %s %32s %s" % { maxwd, addr,
|
||||
hex && (hex.size == 8 ? "IPv4" : "IPv6"),
|
||||
hex || "(bogus address)",
|
||||
port }
|
||||
end
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
function Get-IpAddress
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([PSCustomObject])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]
|
||||
$InputObject
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
function Get-Address ([string]$Address)
|
||||
{
|
||||
if ($Address.IndexOf(".") -ne -1)
|
||||
{
|
||||
$Address, $port = $Address.Split(":")
|
||||
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = $port
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($Address.IndexOf("[") -ne -1)
|
||||
{
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = ($Address.Split("]")[-1]).TrimStart(":")
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
$InputObject | ForEach-Object {
|
||||
$address = Get-Address $_
|
||||
$bytes = ([System.Net.IPAddress]$address.IPAddress).GetAddressBytes()
|
||||
[Array]::Reverse($bytes)
|
||||
$i = 0
|
||||
$bytes | ForEach-Object -Begin {[bigint]$decimalIP = 0} `
|
||||
-Process {$decimalIP += [bigint]$_ * [bigint]::Pow(256, $i); $i++} `
|
||||
-End {[PSCustomObject]@{
|
||||
Address = $address.IPAddress
|
||||
Port = $address.Port
|
||||
Hex = "0x$($decimalIP.ToString('x'))"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ipAddresses = "127.0.0.1","127.0.0.1:80","::1","[::1]:80","2605:2700:0:3::4713:93e3","[2605:2700:0:3::4713:93e3]:80" | Get-IpAddress
|
||||
$ipAddresses
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ipAddresses[5].Address
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ipAddresses | where {$_.Address.AddressFamily -eq "InterNetworkV6" -and $_.Port -ne $null}
|
||||
87
Task/Parse-an-IP-Address/VBScript/parse-an-ip-address.vbs
Normal file
87
Task/Parse-an-IP-Address/VBScript/parse-an-ip-address.vbs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
Function parse_ip(addr)
|
||||
'ipv4 pattern
|
||||
Set ipv4_pattern = New RegExp
|
||||
ipv4_pattern.Global = True
|
||||
ipv4_pattern.Pattern = "(\d{1,3}\.){3}\d{1,3}"
|
||||
'ipv6 pattern
|
||||
Set ipv6_pattern = New RegExp
|
||||
ipv6_pattern.Global = True
|
||||
ipv6_pattern.Pattern = "([0-9a-fA-F]{0,4}:){2}[0-9a-fA-F]{0,4}"
|
||||
'test if address is ipv4
|
||||
If ipv4_pattern.Test(addr) Then
|
||||
port = Split(addr,":")
|
||||
octet = Split(port(0),".")
|
||||
ipv4_hex = ""
|
||||
For i = 0 To UBound(octet)
|
||||
If octet(i) <= 255 And octet(i) >= 0 Then
|
||||
ipv4_hex = ipv4_hex & Right("0" & Hex(octet(i)),2)
|
||||
Else
|
||||
ipv4_hex = "Erroneous Address"
|
||||
Exit For
|
||||
End If
|
||||
Next
|
||||
parse_ip = "Test Case: " & addr & vbCrLf &_
|
||||
"Address: " & ipv4_hex & vbCrLf
|
||||
If UBound(port) = 1 Then
|
||||
If port(1) <= 65535 And port(1) >= 0 Then
|
||||
parse_ip = parse_ip & "Port: " & port(1) & vbCrLf
|
||||
Else
|
||||
parse_ip = parse_ip & "Port: Invalid" & vbCrLf
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
'test if address is ipv6
|
||||
If ipv6_pattern.Test(addr) Then
|
||||
parse_ip = "Test Case: " & addr & vbCrLf
|
||||
port_v6 = "Port: "
|
||||
ipv6_hex = ""
|
||||
'check and extract port information if any
|
||||
If InStr(1,addr,"[") Then
|
||||
'extract the port
|
||||
port_v6 = port_v6 & Mid(addr,InStrRev(addr,"]")+2,Len(addr)-Len(Mid(addr,1,InStrRev(addr,"]")+1)))
|
||||
'extract the address
|
||||
addr = Mid(addr,InStrRev(addr,"[")+1,InStrRev(addr,"]")-(InStrRev(addr,"[")+1))
|
||||
End If
|
||||
word = Split(addr,":")
|
||||
word_count = 0
|
||||
For i = 0 To UBound(word)
|
||||
If word(i) = "" Then
|
||||
If i < UBound(word) Then
|
||||
If Int((7-(i+1))/2) = 1 Then
|
||||
k = 1
|
||||
ElseIf UBound(word) < 6 Then
|
||||
k = Int((7-(i+1))/2)
|
||||
ElseIf UBound(word) >= 6 Then
|
||||
k = Int((7-(i+1))/2)-1
|
||||
End If
|
||||
For j = 0 To k
|
||||
ipv6_hex = ipv6_hex & "0000"
|
||||
word_count = word_count + 1
|
||||
Next
|
||||
Else
|
||||
For j = 0 To (7-word_count)
|
||||
ipv6_hex = ipv6_hex & "0000"
|
||||
Next
|
||||
End If
|
||||
Else
|
||||
ipv6_hex = ipv6_hex & Right("0000" & word(i),4)
|
||||
word_count = word_count + 1
|
||||
End If
|
||||
Next
|
||||
parse_ip = parse_ip & "Address: " & ipv6_hex &_
|
||||
vbCrLf & port_v6 & vbCrLf
|
||||
End If
|
||||
'test if the address in invalid
|
||||
If ipv4_pattern.Test(addr) = False And ipv6_pattern.Test(addr) = False Then
|
||||
parse_ip = "Test Case: " & addr & vbCrLf &_
|
||||
"Address: Invalid Address" & vbCrLf
|
||||
End If
|
||||
End Function
|
||||
|
||||
'Testing the function
|
||||
ip_arr = Array("127.0.0.1","127.0.0.1:80","::1",_
|
||||
"[::1]:80","2605:2700:0:3::4713:93e3","[2605:2700:0:3::4713:93e3]:80","RosettaCode")
|
||||
|
||||
For n = 0 To UBound(ip_arr)
|
||||
WScript.StdOut.Write parse_ip(ip_arr(n)) & vbCrLf
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue