Aquarius_Girl
Aquarius_Girl

Reputation: 22906

Understanding struct sockaddr

struct sockaddr {
    unsigned short sa_family;   // address family, AF_xxx
    char           sa_data[14]; // 14 bytes of protocol address
};

In this structure what exactly is the meaning address family depicted by sa_family?

Does it mean that protocols like TCP/UDP have "addresses"? Well, the protocols can be identification numbers not addresses, I think.

Anyway, if yes, then on what basis have their families been divided?

Upvotes: 34

Views: 123376

Answers (2)

Len Holgate
Len Holgate

Reputation: 21616

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:

Upvotes: 34

Jerry Miller
Jerry Miller

Reputation: 981

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.

Upvotes: -1

Related Questions