C实现DNS到IP地址的实现

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int dns2ip(const char *domain, char * ip, int len)
{
    char **pptr;  
    struct hostent *hptr;  
#if 0

/* Description of data base entry for a single host.  */
struct hostent
{
  char *h_name;                 /* Official name of host.  */
  char **h_aliases;             /* Alias list.  */
  int h_addrtype;               /* Host address type.  */
  int h_length;                 /* Length of address.  */
  char **h_addr_list;           /* List of addresses from name server.  */
#ifdef __USE_MISC
# define        h_addr  h_addr_list[0] /* Address, for backward compatibility.*/
#endif
};

#endif
    
    if((hptr = gethostbyname(domain)) == 0)                                                          
    {                                                                                                
        printf(" gethostbyname error for host:%s\n", domain);                                           
        return -1;
    }
    else
    {
//        printf("official hostname:%s\n", hptr->h_name);                                                   
        for(pptr = hptr->h_aliases; *pptr != 0; pptr++)                                               
        {
            printf(" alias:%s\n", *pptr);                                                                 
        }
        switch(hptr->h_addrtype)                                                                         
        {                                                                                                
            case AF_INET:                                                                                
            case AF_INET6:                                                                               
            pptr=hptr->h_addr_list;                                                                  
            for(; *pptr!=0; pptr++)
            {    
                /*  const char *inet_ntop(int af, const void *src,
                             char *dst, socklen_t size); */
                inet_ntop(hptr->h_addrtype, *pptr, ip, len);
            }               
            break; 
            
            default:                                                                                     
            printf("unknown address type\n");
            break;                                                                                       
        }       

    }
    
    return 0;
}

int main(int argc, const char *argv[])
{
    char ip[32] = { 0 };
    
	if(argc != 2)
    {
		fprintf(stderr, "Usage: %s <nds_name>\n", argv[0]);
		return -1;
	}

    memset(ip, 0, sizeof(ip));
	if(dns2ip(argv[1], ip, sizeof(ip)/sizeof(char)) < 0)
	{
        printf("dns2ip error!\n");
        return -2;
    }
    printf(" address:%s\n", ip);                        
    return 0;
}


测试结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值