Apnic 什么玩意? look here
http://baike.baidu.com/view/96315.html?fromTaglist
可以通过连接
http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest获取到亚洲所有国家的ip地址信息(包括Ipv4和Ipv6,这里只讨论ipv4。类似Apnic这样的机构全球有5个,其他4个可以Google)。
以下是ip地址信息的一个片段
apnic|CN|ipv4|222.249.160.0|4096|20040721|allocated
apnic|CN|ipv4|222.249.176.0|4096|20050511|allocated
apnic|CN|ipv4|222.249.192.0|16384|20050511|allocated
apnic|TW|ipv4|222.250.0.0|65536|20040525|allocated
apnic|TW|ipv4|222.251.0.0|32768|20040525|allocated
apnic|KR|ipv4|222.251.128.0|32768|20051215|allocated
apnic|VN|ipv4|222.252.0.0|262144|20040518|allocated
我们只关心每行信息中第2、4、5列的信息。
例如:第一行的,CN,222.249.160.0,4096三列的值,分别表示国家,开始ip,ip个数。这一列可以变成acl的格式 CN 222.249.160.0/20,这样就方便我们把这样包含了4096个ip地址的记录存放到二叉树中进行一些,查询,更新的操作(我怀疑qq纯真库就是用这种方法进行查询的)。
以下是一段实现这个算法的一段代码,包括ip地址acl的insert和merge操作(包含了查询)
import java.util.ArrayList;
import java.util.List;
public class AclTree {
private static final Integer MASK = 0x00000001;
private Node root;
private List<String> codelist;
private List<String> results;
public AclTree(){
root = null;
codelist = new ArrayList<String>();
results = new ArrayList<String>();
}
public String getStrIpBy32Int(Long k){
String ip="";
Long p1 = ( (k&0xff000000)>>24 );
Long p2 =( (k&0x00ff0000)>>16 );
Long p3 = (k&0x0000ff00)>>8;
Long p4 = ( k&0x000000ff );
ip=p1.toString() +"."+ p2.toString() +"."+p3.toString()+"."+p4.toString();
return ip;
}
public Long get32IntIpByIp(String ip){
Long ipInt = new Long(0);
String[] ps = ip.split("\\.");
if(4 != ps.length){
return ipInt;
}
Long p1 = new Long( ps[0] );
Long p2 = new Long( ps[1] );
Long p3 = new Long( ps[2] );
Long p4 = new Long( ps[3] );
ipInt = p1*256*256*256 + p2*256*256 + p3*256 + p4;
return ipInt;
}
private int getIndex(String obj){
int index = this.codelist.indexOf(obj);
if(0 > index){
index = this.codelist.size();
this.codelist.add(obj);
}
if("HuaShu"==obj){
System.out.println(index);
}
return index;
}
private Node insertNewNode(byte k, short v, Node n){
if(0 == k){//left child
if(null == n.left){
n.left = new Node();
}
if(null != n.codeIndex){ //split father to two leaf nodes
n.right = new Node();
n.right.codeIndex = n.codeIndex;
n.left.codeIndex = n.codeIndex;
n.codeIndex = null;
}
return n.left;
}else if( 1== k){//right child
if(null == n.right){
n.right = new Node();
}
if( null != n.codeIndex ){//split father to two leaf nodes
n.left = new Node();
n.left.codeIndex = n.codeIndex;
n.right.codeIndex = n.codeIndex;
n.codeIndex = null;
}
return n.right;
}
return null;
}
private void merge(Node node){
if(null == node){
return;
}
if( null != node.codeIndex ){ //leaf node
// if(246 == node.codeIndex){
// System.out.println("");
// }
return;
}
if( null != node.left ){
merge( node.left );
}
if( null != node.right){
merge( node.right );
}
if( (null != node.left)&&(null != node.right) ){
if ( (null != node.left.codeIndex)&&(null != node.right.codeIndex) ){
int l = node.left.codeIndex.intValue();
int r = node.right.codeIndex.intValue();
if(l == r){
if( null != node.left.codeIndex){//merge two children
node.codeIndex = node.left.codeIndex;
node.left = null;
node.right = null;
}
}
}
}
}
private void collectLeaves(Node node, long route, int level){
if(null == node){
return;
}
if(null != node.codeIndex){
long key = route<<(32-level);
String rs = this.codelist.get(node.codeIndex)+" "+this.getStrIpBy32Int(key)+" "+level;
this.results.add(rs);
// System.out.println(rs);
return;
}
long tmpRoute = route;
if(this.root != node){
tmpRoute = route<<1;
}
if(null != node.left){
int x = level+1;
this.collectLeaves( node.left, tmpRoute, x );
}
if(null != node.right){
int x = level+1;
this.collectLeaves( node.right, tmpRoute+1, x );
}
}
public void insert(String ipInt, String areaCode, Integer mask){
if(null == this.root){
this.root = new Node();
}
Node node = this.root;
short index = (short)this.getIndex(areaCode);
long x = new Long(ipInt);
x = x>>(32-mask);
for(int i = mask-1; i >= 0; i--){
byte k = (byte)( (x>>i)&MASK );
if(null != node.codeIndex){// repeat the same index
if(index == node.codeIndex){
break;
}
}
node = insertNewNode(k, index, node);
}
node.codeIndex = index;
}
public List< String > getAclData(){
this.merge(this.root);
this.collectLeaves(this.root, 0, 0);
return this.results;
}
public static void main(String args[]){
AclTree tmp = new AclTree();
Long x = tmp.get32IntIpByIp("110.152.0.0");
Long y = tmp.get32IntIpByIp("110.152.1.0");
tmp.insert(x.toString(), "xx", 23);
tmp.insert(y.toString(), "aa", 25);
System.out.println( tmp.getAclData() );
}
}
class Node{
Short codeIndex = null;
Node right = null;
Node left = null;
}
分享到:
相关推荐
Whois apnic 查询联通 网通、电信、铁通IP地址段方法
校园网同时接入电信网和教育网,访问教育网时数度较慢,本文分析了电信网和教育网的IP地址分配情况和路由器的路由配置,提出了解决方案,设计了一种IP地址聚合的算法,并用C++实现了该算法,对电信网、教育网所分配...
中国路由表整理工具源代码,能从APNIC上获取ip注册信息,以分离出中国电信、中国联通等运营商ip地址段
包含了中国所有的IP段, 包括各地网通, 电信, 移动, 铁通, 教育网, 以及广电运营商, 公司自建网络, 由Apnic官方提供, 童叟无欺. 使用方法很简单, Apache环境下利用.htaccess的命令deny, 你可以使用整理好的.htaccess...
在Linux下获得一些电信运营商的IP地址分配情况: shell> wget http://ftp.apnic.net/apnic/dbase/tools/ripe-dbase-client-v3.tar.gz shell> tar xzvf ripe-dbase-client-v3.tar.gz shell> cd whois-3.1 ...
主要介绍了从apnic提取ip信息的脚本,需要的朋友可以参考下
| 0.0.0.0 | 0.255.255.255 | IANA | 保留地址 | | 1.0.0.0 | 1.0.0.0 | 美国 | 亚太互联网络信息中心(CloudFlare节点) | | 1.0.0.1 | 1.0.0.1 | 美国 | APNIC&CloudFlare;公共DNS服务器 | | 1.0.0.2 | 1.0.0.255 | ...
ip 地址反向查询对应的whois信息,欢迎免费下载。
APNIC与LACNIC合作发起征求建议书 关注互联网成功的技术因素.pdf
APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的! 下面就让我们看看如何在Linux下获得一些电信运营商的IP地址分配情况: shell> wget ...
这是Apnic的官方IPV6课件,在杭州参加IPV6获得的课件。 这是Apnic的官方IPV6课件,在杭州参加IPV6获得的课件。 这是Apnic的官方IPV6课件,在杭州参加IPV6获得的课件。
:anchor: 无需离开终端即可解析IP地址或CDN的CNAME信息 安装 yarn global add nali-cli # npm install nali-cli -g 预构建的二进制文件也可以在GitHub Repo的目录下找到。 安装后qqwry.dat执行nali下载qqwry.dat ...
同时由Inter NIC、APNIC、RIPE三大网络信息中心具体负责美国及其它地区的IP地址分配。 固定IP:固定IP地址是长期固定分配给一台计算机使用的IP地址,一般是特殊的服务器 才拥有固定IP地址。 动态IP:因为IP地址资源...
该Java库主要提供一种检查IP地址或ASN (自治系统编号)是否已在CC (国家/地区代码)下正式注册的方法。 例如,这意味着您可以检查IP地址8.8.8.8是否已正式注册到国家代码“ US”(美国)。 除其他原因外,这...
大陆范围内的教育网、电信、长城宽带、移动、网通、铁通、普天、金桥信息网的ip段信息,只是以运营商分类,如果想要筛选省市,请到各分类下根据字母代码自行筛选,换行符是 \n 见谅 样例: inetnum: 106.32.0.0 - ...
生成 Mikrotik RouterOS 脚本以添加国家 IP 块地址列表 主要脚本 $ gen_mk_country_acl.rb 这是从源 (NIC) 下载 ipv4 列表的 ruby 脚本。 对于每个源,都会创建一个 Mikrotik .rsc 文件。 在 Mikrotik 防火墙中...
network magment
受信任的数据由APNIC( 。 此提供的数据旨在填补分解后的,并将每小时更新一次。 如何使用chnroute_auto数据 您可以在任何需要的地方简单地下载或包含数据: 或者,您可以克隆此存储库并自己同步: $ make sync #...
该信息来自apnic.net 该项目的灵感来自表现出色的 ,但我没有敲定他们的API,而是决定构建自己的服务,以便可以随意滥用它。 部署应用 npm run deploy 测试 npm run test 声纳扫描 安装声纳扫描 npm install -g ...