What's the difference between sockaddr, sockaddr_in, and sockaddr_in6?

前端 未结 2 1762
-上瘾入骨i
-上瘾入骨i 2020-12-04 10:21

I know that sockaddr_in is for IPv4, and sockaddr_in6 for IPv6. The confusion to me is the difference between sockaddr and sockaddr_in[6].

Some functions accept

2条回答
  •  天涯浪人
    2020-12-04 10:53

    I don't want to answer my question. But to give more information here which might be useful to other people, I decide to answer my question.

    After dig into the source code of linux. Following is my finding, there are possible multiple protocol which all implement the getsockname. And each have themself underling address data structure, for example, for IPv4 it is sockaddr_in, and IPV6 sockaddr_in6, and sockaddr_un for AF_UNIX socket. sockaddr are used as the common data strut in the signature of those APIs.

    Those API will copy the socketaddr_in or sockaddr_in6 or sockaddr_un to sockaddr base on another parameter length by memcpy.

    And all of the data structure begin with same type field sa_family.

    Base on those reason, the code snippet is valid, because both sockaddr_in and sockaddr_in6 have sa_family and then we can cast it to the correct data structure for usage after check sa_family.

    BTY, I'm not sure why the sizeof(sockaddr_in6) > sizeof(sockaddr), which cause allocate memory base on size of sockaddr is not enough for ipv6( that is error-prone), but I guess it is because of history reason.

提交回复
热议问题