Efficient way to store IPv4/IPv6 addresses

前端 未结 3 1628
春和景丽
春和景丽 2021-01-12 06:27

I am working on a C/C++ networking project that it should be able to both use the IPv4 and IPv6 networking stacks. The project works only on Linux. So, I tried to find an e

3条回答
  •  抹茶落季
    2021-01-12 07:21

    This code is from nginx. It's the same as 1st answer. I don't think it's a good idea to use pointer sockaddr_in* .

    typedef union {
        struct sockaddr           sockaddr;
        struct sockaddr_in        sockaddr_in;
    #if (NGX_HAVE_INET6)
        struct sockaddr_in6       sockaddr_in6;
    #endif
    #if (NGX_HAVE_UNIX_DOMAIN)
        struct sockaddr_un        sockaddr_un;
    #endif
    } ngx_sockaddr_t;
    

    https://github.com/nginx/nginx/blob/912fb44e25c6ab2598e36b4544c709b871251b2e/src/core/ngx_inet.h#L44

提交回复
热议问题