coordinator's planet

叶子 是不会飞翔的翅膀

翅膀 是落在天上的叶子

posts - 89,comments - 251,trackbacks - 5

题目:DNS Client
要求:案例模拟一个DNS客户端程序,根据指定的DNS服务器,对域名实施正向、逆向解析。
问题:目前只能用系统默认DNS服务器,无法逆向解析外网IP。
使用方法:
编译:$gcc -o dns dns.c
运行:$./dns www.whu.edu.cn
$./dns 192.168.0.51(我的内网IP)

程序:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main(int argc ,char *argv[])
{
        struct sockaddr_in addr;
        struct hostent *host;
        char **alias;
        
        if(argc!=2)
        {
         fprintf(stderr,"Usage:%s hostname|ip..\n\a",argv[0]);
         exit(1);
        }
        
        /* 这里我们假设是IP,通过IP获得主机信息*/   
        if(inet_aton(argv[1],&(addr.sin_addr))!=0)
        {
           host=gethostbyaddr((char *)&(addr.sin_addr),4,AF_INET); 
           printf("Address information of Ip %s\n",argv[1]); 
        } 
        else 
        {
        /* 否则用户应该输入的是域名,通过域名找主机信息*/
           host=gethostbyname(argv[1]);
           printf("Address information of host %s\n",argv[1]); 
        }
        if(host==NULL)
        {
        /* 都不是,算了不找了*/
           printf("No Information found");
           exit(1);
        }
        /*打印主机正式名称*/
        printf("Official host name:\n%s\n\n",host->h_name);
        /*打印主机其他名称*/
        printf("Name aliases:\n");
        for(alias=host->h_aliases;*alias!=NULL;alias++)
        printf("%s\n",*alias);
        /*打印主机系列IP*/
        printf("\nIp address:\n");
        for(alias=host->h_addr_list;*alias!=NULL;alias++)
        printf("%s\n",inet_ntoa(*(struct in_addr *)(*alias)));
}

打包下载:
http://files.cnblogs.com/lzcarl/dns.rar

posted on 2005-12-02 23:37 coordinator 阅读(1447) 评论(3) 编辑 收藏