WHOIS协议原理

Whois 协议原理及使用RFC812定义了一个非常简单的Internet 信息查询协议——WHOIS 协议。其基本内容是,先向服务器的TCP 端口43建立一个连接,发送查询关键字并加上回车换行,然后

Whois 协议原理及使用

RFC812定义了一个非常简单的Internet 信息查询协议——WHOIS 协议。其基本内容是,先向服务器的TCP 端口43建立一个连接,发送查询关键字并加上回车换行,然后接收服务器的查询结果。

世界上各级Internet 管理机构秉承公开、公正、共享的原则,设立了可以查知IP 地址和域名所有者登记资料的WHOIS 服务器,以便所有Internet 的使用者排除故障、打击网上非法活动。全世界国际区域性的IP 地址管理机构有四个:ARIN 、RIPE 、APNIC 、LACNIC ,他们负责的IP 地址的地理区域如下图所示。

四个国际区域性IP 地址管理机构所负责的区域

(此图摘自《RIPE 2002年度报告》)

重要的Internet 管理机构和常用的WHOIS 服务器

机构缩写 WHOIS 服务器地址 机构全名及地点 提供查询内容

CERNIC whois.edu.cn 中国教育与科研计算机网网络信息中心

(清华大学·中国北京) 中国教育网内的IP 地址和.edu.cn 域名信息

CNNIC whois.cnnic.net.cn 中国互联网络信息中心

(中国科学院计算机网络信息中心·中国北京) .cn 域名(除.edu.cn) 信息

INTERNIC whois.internic.net 互联网络信息中心

(美国洛杉矶市Marina del Rey镇) .com,.net,.org,.biz,.info,.name

域名的注册信息(只给出注册代理公司)

ARIN whois.arin.net 美国Internet 号码注册中心

(美国弗吉尼亚州Chantilly 市) 全世界早期网络及现在的美国、加拿大、撒哈拉沙漠以南非洲的IP 地址信息

APNIC whois.apnic.net 亚洲与太平洋地区网络信息中心

(澳大利亚昆士兰州密尔顿镇) 东亚(包括中国大陆和台湾) 、南亚、大洋洲IP 地址注信息 RIPE whois.ripe.net 欧州IP 地址注册中心(荷兰阿姆斯特丹) 欧洲、北非、西亚地区的IP 地址信息

TWNIC whois.twnic.net 台湾互联网络信息中心(中国台湾台北) .tw 域名和部分台湾岛内IP 地址信息

JPNIC whois.nic.ad.jp 日本互联网络信息中心(日本东京) .jp 域名和日本境内的IP 地址信息

www.jianfeidongli.com

,

KRNIC whois.krnic.net 韩国互联网络信息中心(韩国汉城) .kr 域名和韩国境内的IP 地址信息

LACNIC whois.lacnic.net 拉丁美洲及加勒比互联网络信息中心(巴西圣保罗) 拉丁美洲及加勒比海诸岛IP 地址信息

本机上的自动WHOIS 服务,是按照下图所示的流程,依次查询若干个WHOIS 服务器之后,得到某个IP 地址的WHOIS 信息。

当我们准备建立一个Web 站点,就必须向域名登记机构申请一个Internet 域名,因此,我们通常希望了解自己准备使用的域名是否已经被注册,这时,可以简单地访问InterNIC 站点http://www.internic.net/whois.html,在"Registry Whois" 输入框中输入需查询的域名,就可以得到我们需要的结果。本文介绍了如何使用Java 编程来实现这个过程。

一 原理

原理非常简单,域名的查询主要是基于RFC 954提供的WHOIS 协议。在上述过程中,我们实际上是访问了InterNIC 站点的WHOIS 服务器,该服务器从WHOIS 数据库中查询我们所需要的内容。

WHOIS 服务器是一个基于" 查询/响应" 的TCP 事务服务器,它运行在SRI-NIC 机器上(26.0.0.73或10.0.0.51) ,向用户提供internet 范围内的目录服务。本地主机上的用户程序可以通过Internet 访问该服务器,其过程主要有下面三步:

(1)在TCP 服务端口43(十进制) 连接SRI-NIC 服务主机;

(2)发送一个命令,以回车和换行()结尾;

(3)接受相应命令的返回信息,一旦输出结束,服务器将关闭连接。

命令的格式非常简单。可以直接输入域名,例如,可以使用"sohu.com" 查询" 搜狐" 网站的域名信息;也可以使用"help" 得到详细的帮助信息。

二 Java socket编程简述

在Java 中,使用Socket 类可以实现客户端的sockets ,建立与服务器的网络连接。本文www.jianfeidongli.com

,

使用下面所示的Socket 类的构造器来创建一个流socket ,并连接到主机"whois.internic.net" 的端口43。

public Socket(String host, int port, boolean stream) throws IOException

其中,参数host 为远程主机的主机名,port 为远程主机的端口号,如果参数stream 为true ,则创建一个流socket ,否则创建一个数据报socket 。

如果创建socket 时发生I/O错误,将抛掷一个IOException 异常。

当创建了一个连接到远程主机的socket 对象后,我们可以使用getInputStream()和getOutputStream()方法分别得到该socket 对象的输入流和输出流,用于对该socket 进行数据读写,为了使应用程序设计简单,这些方法返回的流通常使用java.io 包中的实例对象来处理,例如:DataInputStream 和PrintWriter 。

从socket 读数据使用readline()方法,一次读取一行数据(字符串) :

public String readLine() throws IOException

向socket 写数据使用print()方法:

public void print(String s)

当完成socket 通讯后,应该首先关闭DataInputStream 和PrintWriter 对象,最后才关闭socket 对象。

三 源程序

import java.net.*;

import java.io.*;

public class whois

{

public final static int port = 43;

public final static String hostname = "whois.internic.net";

public static void main(String[] args)

{

Socket theSocket;

DataInputStream theWhoisStream;

PrintStream ps;

//检查命令行参数

if (args.length <1)

{

System.out.println("nUsage: java whois ");

System.out.println("Parameters:");

www.jianfeidongli.com

,

System.out.println(

"tcommand = one or more Domain name, or other command."); System.out.println("Example:");

System.out.println("tjava whois sohu.com");

System.out.println("tjava whois help");

System.exit(1); //退出

}

try {

//在TCP 服务端口43(十进制) 连接SRI-NIC 服务主机

theSocket = new Socket(hostname, port, true);

ps = new PrintStream(theSocket.getOutputStream());

//发送用户提供的一个或多个命令

for (int i = 0; i < args.length; i )

ps.print(args[i] " ");

//以回车和换行()结尾

ps.print("rn");

//接受相应命令的返回信息

theWhoisStream = new DataInputStream(theSocket.getInputStream()); String s;

while ((s = theWhoisStream.readLine()) != null) {

System.out.println(s);

}

//关闭DataInputStream 和PrintWriter

theWhoisStream.close();

ps.close();

//关闭socket

theSocket.close();

}

catch (IOException e) {

System.err.println(e);

}

}

}

运行:java whois sina.com

运行结果:

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered

with many different competing registrars. Go to http://www.internic.net for detailed information.

www.jianfeidongli.com

,

SINA.COM.CN

SINA.COM

To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record.

>>> Last update of whois database: Tue, 03 Mar 2009 03:37:55 UTC <<<

NOTICE: The expiration date displayed in this record is the date the registrar's sponsorship of the domain name registration in the registry is currently set to expire. This date does not necessarily reflect the expiration date of the domain name registrant's agreement with the sponsoring

registrar. Users may consult the sponsoring registrar's Whois database to view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois database through the use of electronic processes that are high-volume and automated except as reasonably necessary to register domain names or modify existing registrations; the Data in VeriSign Global Registry Services' ("VeriSign") Whois database is provided by VeriSign for

information purposes only, and to assist persons in obtaining information about or related to a domain name registration record. VeriSign does not guarantee its accuracy. By submitting a Whois query, you agree to abide by the following terms of use: You agree that you may use this Data only for lawful purposes and that under no circumstances will you use this Data to: (1) allow, enable, or otherwise support the transmission of mass

unsolicited, commercial advertising or solicitations via e-mail, telephone, or facsimile; or (2) enable high volume, automated, electronic processes that apply to VeriSign (or its computer systems). The compilation, repackaging, dissemination or other use of this Data is expressly

prohibited without the prior written consent of VeriSign. You agree not to use electronic processes that are automated and high-volume to access or query the Whois database except as reasonably necessary to register

domain names or modify existing registrations. VeriSign reserves the right to restrict your access to the Whois database in its sole discretion to ensure operational stability. VeriSign may restrict or terminate your access to the Whois database for failure to abide by these terms of use. VeriSign reserves the right to modify these terms at any time.

The Registry database contains ONLY .COM, .NET, .EDU domains and Registrars.

www.jianfeidongli.com

标签: