• Unix Network Programming Episode 79


    ‘gai_strerror’ Function

    The nonzero error return values from getaddrinfo have the names and meanings shown in Figure 11.7. The function gai_strerror takes one of these values as an argument and returns a pointer to the corresponding error string.

    #include 
    const char *gai_strerror (int error);
    
    • 1
    • 2
    ‘freeaddrinfo’ Function

    All the storage returned by getaddrinfo, the addrinfo structures, the ai_addr structures, and the ai_canonname string are obtained dynamically (e.g., from malloc). This storage is returned by calling freeaddrinfo.

    #include 
    void freeaddrinfo (struct addrinfo *ai);
    
    • 1
    • 2

    ai should point to the first addrinfo structure returned by getaddrinfo. All the structures in the linked list are freed, along with any dynamic storage pointed to by those structures (e.g., socket address structures and canonical hostnames).

    ‘getaddrinfo’ Function: IPv6

    The POSIX specification defines the getaddrinfo function and the information it returns for both IPv4 and IPv6.

    • getaddrinfo is dealing with two different inputs: the type of socket address structure the caller wants back and the type of records that should be searched for in the DNS or other database.
    • The address family in the hints structure provided by the caller specifies the type of socket address structure that the caller expects to be returned. If the caller specifies AF_INET, the function must not return any sockaddr_in6 structures; if the caller specifies AF_INET6, the function must not return any sockaddr_in structures.
    • POSIX says that specifying AF_UNSPEC will return addresses that can be used with any protocol family that can be used with the hostname and service name. This implies that if a host has both AAAA records and A records, the AAAA records are returned as sockaddr_in6 structures and the A records are returned as sockaddr_in structures. It makes no sense to also return the A records as IPv4-mapped IPv6 addresses in sockaddr_in6 structures because no additional information is being returned: These addresses are already being returned in sockaddr_in structures.
    • This statement in the POSIX specification also implies that if the AI_PASSIVE flag is specified without a hostname, then the IPv6 wildcard address (IN6ADDR_ANY_INIT or 0::0) should be returned as a sockaddr_in6 structure, along with the IPv4 wildcard address (INADDR_ANY or 0.0.0.0), which is returned as a sockaddr_in structure. It also makes sense to return the IPv6 wildcard address first because we will see in Section 12.2(See 9.1.2) that an IPv6 server socket can handle both IPv6 and IPv4 clients on a dual-stack host.
    • The address family specified in the hint structure’s ai_family member, along with the flags such as AI_V4MAPPED and AI_ALL specified in the ai_flags member, dictate the type of records that are searched for in the DNS (A and/or AAAA) and what type of addresses are returned (IPv4, IPv6, and/or IPv4-mapped IPv6). We summarize this in Figure 11.8.
    • The hostname can also be either an IPv6 hex string or an IPv4 dotted-decimal string. The validity of this string depends on the address family specified by the caller. An IPv6 hex string is not acceptable if AF_INET is specified, and an IPv4 dotted-decimal string is not acceptable if AF_INET6 is specified. But, if AF_UNSPEC is specified, either is acceptable and the appropriate type of socket address structure is returned.
    ‘getaddrinfo’ Function: Examples

    We will now show some examples of getaddrinfo using a test program that lets us enter all the parameters: the hostname, service name, address family, socket type, and AI_CANONNAME and AI_PASSIVE flags. (We do not show this test program, as it is about 350 lines of uninteresting code. It is provided with the source code for the book, as described in the Preface.) The test program outputs information on the variable number of addrinfo structures that are returned, showing the arguments for a call to socket and the address in each socket address structure.

    ‘host_serv’ Function

    Our first interface to getaddrinfo does not require the caller to allocate a hints structure and fill it in. Instead, the two fields of interest, the address family and the socket type, are arguments to our host_serv function.

    #include "unp.h"
    struct addrinfo *host_serv (const char *hostname, const char *service, int family, ints socktype);
    
    • 1
    • 2
    #include "unp.h"
    
    struct addrinfo* host_serv(const char *host, const char *serv, int family, int socktype)
    {
        int n;
        struct addrinfo hints, *res;
        bzero(&hints, sizeof(struct addrinfo));
        hints.ai_flags=AI_CANONNAME;
        hints.ai_family=family;
        hints.ai_socktype=socktype;
    
        if((n=getaddrinfo(host, serv, &hints, &res))!=0)
        {
            return NULL;
        }
        return res;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    host_serv function

  • 相关阅读:
    Java - Object#finalize在JDK9中被标记废弃了!
    2023年全球市场单晶金刚石(SCD)晶片总体规模、主要生产商、主要地区、产品和应用细分研究报告
    RabbitMQ(七)【SpringBoot案例】
    一道晦涩难懂的Pandas基础题
    实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
    深入解析 const 关键字:指针、参数、返回值和类成员函数
    Chronos——中
    CSS 模糊效果 CSS 黑白效果 CSS调整亮度 对比度 饱和度 模糊效果 黑白效果反转颜色
    springcloud新闻发布系统源码
    【小程序】微信小程序自定义导航栏及其封装
  • 原文地址:https://blog.csdn.net/myfather103/article/details/82690702