Understanding struct sockaddr

后端 未结 2 1703
清酒与你
清酒与你 2020-12-14 02:11
struct sockaddr {
    unsigned short sa_family;   // address family, AF_xxx
    char           sa_data[14]; // 14 bytes of protocol address
};

In t

相关标签:
2条回答
  • 2020-12-14 02:39

    I found this out when trying to duplicate the jnetpcap getHardwareAddress() method in C/C++. The MAC address is contained, when sa_family is 17 (0x11, AF_PACKET), in bytes 10-15.

    0 讨论(0)
  • 2020-12-14 02:47

    The format and size of the address is usually protocol specific.

    sockaddr is used as the base of a set of address structures that act like a discriminated union, see the Beej guide to networking. You generally look at the sa_family and then cast to the appropriate address family's specific address structure.

    TCP and UDP do not have addresses specific to them as such, rather the IP level has different sizes of address for IPv4 and IPv6.

    See also:

    • http://en.wikipedia.org/wiki/AF_INET
    • http://msdn.microsoft.com/en-us/library/ms740496(v=vs.85).aspx
    • http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml
    0 讨论(0)
提交回复
热议问题