Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding struct sockaddr

Tags:

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?

like image 933
Aquarius_Girl Avatar asked Sep 14 '11 10:09

Aquarius_Girl


People also ask

What is a Sockaddr structure?

sockaddr. The first structure is sockaddr that holds the socket information − struct sockaddr { unsigned short sa_family; char sa_data[14]; }; This is a generic socket address structure, which will be passed in most of the socket function calls.

What does the struct In_addr structure represent?

The in_addr structure represents an IPv4 Internet address.

What is the structure of socket?

The socket data structure defines the socket. During a socket subroutine, the system dynamically creates the socket data structure. The socket address is specified by a data structure that is defined in a header file. See the sockaddr Structure figure (Figure 1) for an illustration of this data structure.

What is Sa_family?

sa_family. The address family for the transport address.


1 Answers

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
like image 146
Len Holgate Avatar answered Oct 01 '22 13:10

Len Holgate