美文网首页
域名解析

域名解析

作者: 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;
    }
    

    相关文章

      网友评论

          本文标题:域名解析

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