Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Functions that expect a pointer to <code>struct sockaddr</code> probably typecast the pointer you send them to <code>sockaddr</code> when you send them a pointer to <code>struct sockaddr_storage</code>. In that way, they access it as if it was a <code>struct sockaddr</code>. </p> <p><code>struct sockaddr_storage</code> is designed to fit in both a <code>struct sockaddr_in</code> and <code>struct sockaddr_in6</code> </p> <p>You don't create your own <code>struct sockaddr</code>, you usually create a <code>struct sockaddr_in</code> or a <code>struct sockaddr_in6</code> depending on what IP version you're using. In order to avoid trying to know what IP version you will be using, you can use a <code>struct sockaddr_storage</code> which can hold either. This will in turn be typecasted to <code>struct sockaddr</code> by the connect(), bind(), etc functions and accessed that way.</p> <p>You can see all of these structs below (the padding is implementation specific, for alignment purposes):</p> <pre><code>struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct sockaddr_in6 { u_int16_t sin6_family; // address family, AF_INET6 u_int16_t sin6_port; // port number, Network Byte Order u_int32_t sin6_flowinfo; // IPv6 flow information struct in6_addr sin6_addr; // IPv6 address u_int32_t sin6_scope_id; // Scope ID }; struct sockaddr_storage { sa_family_t ss_family; // address family // all this is padding, implementation specific, ignore it: char __ss_pad1[_SS_PAD1SIZE]; int64_t __ss_align; char __ss_pad2[_SS_PAD2SIZE]; }; </code></pre> <p>So as you can see, if the function expects an IPv4 address, it will just read the first 4 bytes (because it assumes the struct is of type <code>struct sockaddr</code>. Otherwise it will read the full 16 bytes for IPv6).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload