美文网首页
域名解析

域名解析

作者: arkliu | 来源:发表于2022-12-06 09:24 被阅读0次

域名解析相关api

#include <netdb.h>
extern int h_errno;

struct hostent *gethostent(void);
struct hostent *gethostbyname(const char *name);
void sethostent(int stayopen);
void endhostent(void);

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 */
           }


域名解析使用gethostbyname

/etc/hosts 文件

#include<netdb.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<memory.h>
#include<arpa/inet.h>

void out_addr(struct hostent *h) {
    printf("hostname:%s\n", h->h_name);
    printf("addrtype:%s\n", h->h_addrtype ==AF_INET ? "ipv4":"ipv6");
    char ip[16];
    memset(ip, 0, sizeof(ip));
    inet_ntop(h->h_addrtype,
                h->h_addr_list[0], ip, sizeof(ip));
    printf("ip address:%s\n", ip);

    int i = 0;
    while(h->h_aliases[i] != NULL) {
        printf("aliase:%s\n", h->h_aliases[i]);
        i++;
    }
}

// /etc/hosts 文件
int main(int argc, char* argv[]) {
    if (argc < 2){
        printf("usage:%s host\n",argv[0]);
        exit(1);
    }
    struct hostent *h;
    h = gethostbyname(argv[1]);
    if(h != NULL) {
        out_addr(h);
    } else {
        printf("no %s exist\n",argv[1]);
    }
    return 0;
}
image.png

gethostbyname在多线程中存在问题,另外gethostbyname只支持ipv4

使用gethostent做域名解析

#include<netdb.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<memory.h>
#include<arpa/inet.h>

void out_addr(struct hostent *h) {
    printf("hostname:%s\n", h->h_name);
    printf("addrtype:%s\n", h->h_addrtype ==AF_INET ? "ipv4":"ipv6");
    char ip[16];
    memset(ip, 0, sizeof(ip));
    inet_ntop(h->h_addrtype,
                h->h_addr_list[0], ip, sizeof(ip));
    printf("ip address:%s\n", ip);

    int i = 0;
    while(h->h_aliases[i] != NULL) {
        printf("aliase:%s\n", h->h_aliases[i]);
        i++;
    }
}

// /etc/hosts 文件
int main(int argc, char* argv[]) {
    if (argc < 2){
        printf("usage:%s host\n",argv[0]);
        exit(1);
    }
    struct hostent *h;
    while((h = gethostent()) != NULL) {
        if(!strcmp(argv[1], h->h_name)) {
            out_addr(h);
            exit(0);
        } else {
            int i = 0;
            while(h->h_aliases[i] != NULL) {
                if(!strcmp(argv[1],h->h_aliases[i])) {
                    out_addr(h);
                    exit(0);
                }
                i++;
            }
        }
    }
    endhostent();
    printf("no %s exist \n",argv[1]);
    return 0;
}

相关文章

  • 1.2-从域名到url

    1.常用的2种域名解析 2.域名解析的使用场景及url 3.基于网易蜂巢的域名解析实战

  • DNS域名解析

    本文解决如果几个问题:1、什么是DNS域名解析2、域名解析过程3、如何搭建本地DNS服务器 什么是DNS域名解析 ...

  • 网络基本服务

    网络基本服务 域名解析 域名系统是因特网使用的命名系统,完成域名解析,将域名解析到特定的IP地址。 DNS采用客户...

  • 页面资源加载

    先来个题外话,地址栏输入后,到页面加载完成,发生了什么? 1.进行域名解析 DNS域名解析系统 -》把域名解析成...

  • DNS

    .域名解析服务

  • 从URL输入到页面展现发生了什么?

    1. 域名解析(DNS):将域名解析成 IP 地址 DNS 是一个网络服务器,我们的域名解析简单来说就是在 DNS...

  • DNS域名服务器——原理

    1 DNS是什么? DNS (Domain Name System) 中文叫做域名解析系统,所谓域名解析就是把域名...

  • 二. 网络应用-域名系统(DNS)

    域名系统(DomainNaming System, DNS) 域名解析:将域名映射为IP地址 域名解析的原理:为了...

  • 如何实现动态域名解析(附源码)

    阿里云动态域名解析工具ddn ddn是基于阿里云开发接口实现的一个动态域名解析工具,如果你在阿里云的域名解析记录中...

  • 计算机网络:DNS

    定义 DNS即域名解析,将域名解析为IP地址,如将www.baidu.com解析为202.108.22.5。 名称...

网友评论

      本文标题:域名解析

      本文链接:https://www.haomeiwen.com/subject/zzksfdtx.html