Skip to content

Commit 0ba838e

Browse files
committed
Rewrote ip2c lib
git-svn-id: https://narf.ofloo.net/svn/ip2c.tcl/trunk@4 a165b9e1-329b-db11-a830-0002a5ce716d
1 parent 452ea86 commit 0ba838e

File tree

1 file changed

+66
-54
lines changed

1 file changed

+66
-54
lines changed

ip2c.tcl

+66-54
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,105 @@
1-
#!/usr/local/bin/tclsh8.4
1+
#!/usr/local/bin/tclsh8.5
22
################################################################################
3-
#
3+
#
44
# TCL scripts by Ofloo all rights reserved.
5-
#
5+
#
66
# HomePage: http://ofloo.net/
77
# CVS: http://cvs.ofloo.net/
88
# Email: support[at]ofloo.net
9-
#
9+
#
1010
# This program is free software; you can redistribute it and/or
1111
# modify it under the terms of the GNU General Public License
1212
# as published by the Free Software Foundation; either version 2
1313
# of the License, or (at your option) any later version.
14-
#
14+
#
1515
# This program is distributed in the hope that it will be useful,
1616
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1717
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1818
# GNU General Public License for more details.
19-
#
19+
#
2020
# You should have received a copy of the GNU General Public License
2121
# along with this program; if not, write to the Free Software
2222
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23-
#
23+
#
2424
################################################################################
2525

2626
namespace eval ip2c {
27-
28-
variable version 1.0
29-
variable mirror "ip2c.ofloo.net"
3027

3128
package require http
29+
package require ip
3230

33-
proc lookup {longip {type {}}} {
34-
variable mirror
35-
if {[regexp {^[\d]{1,10}$} $longip] && (0 >= $longip <= 4294967295)} {
36-
if {[regexp -nocase {<resolve[\s]{0,100}c02="([a-z]{2})"[\s]{0,100}c03="([a-z]{3})"[\s]{0,100}full="(.*?)"[\s]{0,100}/>} [[namespace current]::getPage http://${mirror}/${longip}] -> tld short full]} {
37-
switch -- $type {
38-
"-tld" {
39-
return $tld
40-
}
41-
"-short" {
42-
return $short
43-
}
44-
"-full" {
45-
return $full
46-
}
47-
"" {
48-
return "$tld $short $full"
49-
}
31+
variable api "api.ip2c.info"
32+
variable version 1.1
33+
34+
variable registry
35+
variable assigned
36+
variable short
37+
variable long
38+
variable country
39+
40+
#
41+
# locate ?-ip <ip>? ?-server <server?
42+
# returns: -1 on invalid ip or number of results
43+
#
44+
45+
proc locate {{list_0 {}} {list_1 {}} {list_2 {}} {list_3 {}}} {
46+
variable registry; variable assigned; variable long; variable short; variable country; variable address; variable api
47+
foreach {key value} [info var list_?] {
48+
if {[set $key] == ""} {continue}
49+
switch -- [set ${key}] {
50+
"-server" {
51+
variable api [set $value]
52+
}
53+
"-ip" {
54+
set ip [set $value]
55+
}
56+
default {
57+
error "bad option \"[set $key]\": must be -ip or -server"
58+
}
59+
}
60+
}
61+
if {![info exists foreach {x} [array names short] {
62+
lappend out $short($x)
63+
lappend out $long($x)
5064
}
5165
}
5266
}
53-
error "bad option \"${type}\": must be -tld, -short, -full"
67+
return $out
5468
}
5569

56-
proc getPage {url} {
57-
set token [::http::geturl $url]
58-
set data [::http::data $token]
59-
::http::cleanup $token
60-
return $data
61-
}
70+
#
71+
# registry
72+
# returns: To which registry the IP is assigned
73+
#
6274

63-
# error -1
64-
proc ip2longip {ipaddr} {
65-
if {[[namespace current]::isip $ipaddr]} {
66-
foreach ipbyte [split $ipaddr \x2E] {
67-
append hexaddr [format {%02x} $ipbyte]
68-
}
69-
return [format {%u} "0x$hexaddr"]
75+
proc registry {} {
76+
variable registry
77+
set out {}
78+
foreach {x} [array names registry] {
79+
lappend out $registry($x)
7080
}
71-
return -1
81+
return $out
7282
}
7383

74-
#error -1
75-
proc longip2ip {longip} {
76-
if {$longip > 4294967295} {
77-
return -1
78-
}
79-
return [expr {$longip>>24&255}]\x2E[expr {$longip>>16&255}]\x2E[expr {$longip>>8&255}]\x2E[expr {$longip&255}]
80-
}
84+
#
85+
# cleanup removes cache
86+
# returns: null
87+
#
8188

82-
proc isip {ip} {
83-
if {[regexp {^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$} $ip -> a b c d]} {
84-
if {($a <= 255) && ($b <= 255) && ($c <= 255) && ($d <= 255)} {
85-
return 1
89+
proc cleanup {} {
90+
variable registry; variable assigned; variable long; variable short; variable country; variable address
91+
foreach {n} "registry assigned long short country address" {
92+
if {[array exists [set n]]} {
93+
foreach {x} [array names [set n]] {
94+
array unset [set n] $x
95+
}
8696
}
8797
}
88-
return 0
8998
}
9099

100+
cleanup
101+
91102
}
92103

93104
package provide ip2c $::ip2c::version
105+

0 commit comments

Comments
 (0)