Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
39
Task/Parse-an-IP-Address/Tcl/parse-an-ip-address-1.tcl
Normal file
39
Task/Parse-an-IP-Address/Tcl/parse-an-ip-address-1.tcl
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package require Tcl 8.5
|
||||
package require ip
|
||||
|
||||
proc parseIP {address} {
|
||||
set result {}
|
||||
set family [ip::version $address]
|
||||
set port -1
|
||||
if {$family == -1} {
|
||||
if {[regexp {^\[(.*)\]:(\d+)$} $address -> address port]} {
|
||||
dict set result port $port
|
||||
set family [ip::version $address]
|
||||
if {$family != 6} {
|
||||
return -code error "bad address"
|
||||
}
|
||||
} elseif {[regexp {^(.*):(\d+)$} $address -> address port]} {
|
||||
dict set result port $port
|
||||
set family [ip::version $address]
|
||||
if {$family != 4} {
|
||||
return -code error "bad address"
|
||||
}
|
||||
} else {
|
||||
return -code error "bad address"
|
||||
}
|
||||
}
|
||||
# Only possible error in ports is to be too large an integer
|
||||
if {$port > 65535} {
|
||||
return -code error "bad port"
|
||||
}
|
||||
dict set result family $family
|
||||
if {$family == 4} {
|
||||
# IPv4 normalized form is dotted quad, but toInteger helps
|
||||
dict set result addr [format %x [ip::toInteger $address]]
|
||||
} else {
|
||||
# IPv6 normalized form is colin-separated hex
|
||||
dict set result addr [string map {: ""} [ip::normalize $address]]
|
||||
}
|
||||
# Return the descriptor dictionary
|
||||
return $result
|
||||
}
|
||||
27
Task/Parse-an-IP-Address/Tcl/parse-an-ip-address-2.tcl
Normal file
27
Task/Parse-an-IP-Address/Tcl/parse-an-ip-address-2.tcl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
foreach address {
|
||||
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
|
||||
::ffff:192.168.0.1
|
||||
[::ffff:192.168.0.1]:22
|
||||
::ffff:127.0.0.0.1
|
||||
a::b::1
|
||||
127.0.0.1:100000
|
||||
} {
|
||||
if {[catch {
|
||||
set parsed [parseIP $address]
|
||||
} msg]} {
|
||||
puts "error ${msg}: \"$address\""
|
||||
continue
|
||||
}
|
||||
dict with parsed {
|
||||
puts -nonewline "family: IPv$family addr: $addr"
|
||||
if {[dict exists $parsed port]} {
|
||||
puts -nonewline " port: $port"
|
||||
}
|
||||
puts ""
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue