Summarize the usage of several sockaddr Data Structures

Source: Internet
Author: User

Summarize the usage of several sockaddr Data Structures


 
 
  1. ************************ *********/

  2. Struct sockaddr_in {
  3. _ Kernel_sa_family_t sin_family;/* AF_INET */
  4. _ Be16 sin_port;/* Port number */
  5. Struct in_addr sin_addr;/* Internet address */

  6. /* Pad to size of 'struct sockadd '.*/
  7. Unsigned char _ pad [_ SOCK_SIZE _-sizeof (short int )-
  8. Sizeof (unsigned short int)-sizeof (struct in_addr)];
  9. };


 
 
  1. ***********************/

  2. Struct sockaddr_in6 {
  3. Unsigned short int sin6_family;/* AF_INET6 */
  4. _ Be16 sin6_port;/* Transport layer port #*/
  5. _ Be32 sin6_flowinfo;/* IPv6 flow information */
  6. Struct in6_addr sin6_addr;/* IPv6 address */
  7. _ U32 sin6_scope_id;/* scope id (new in RFC2553 )*/
  8. };

 
 
  1. ************************* */
  2. Struct sockaddr_nl {
  3. _ Kernel_sa_family_t nl_family;/* AF_NETLINK */
  4. Unsigned short nl_pad;/* zero */
  5. _ U32 nl_pid;/* port ID */
  6. _ U32 nl_groups;/* multicast groups mask */
  7. };

 
 
  1. /* The device-independent physical layer address structure. You can use setsockopt to set the multicast or hybrid mode of the NIC */

  2. Struct sockaddr_ll {
  3. Unsigned short sll_family;/* AF_PACKET */
  4. _ Be16 sll_protocol;
  5. Int sll_ifindex;
  6. Unsigned short sll_hatype;
  7. Unsigned char sll_pkttype;
  8. Unsigned char sll_halen;
  9. Unsigned char sll_addr [8];
  10. };
  11. In versions earlier than linux 2.0, structsockaddr_pkt is used, and the second-layer socket is obtained through socket (AF_INET, SOCK_PACKET, protocol). There is no AF_PACKET domain, and only SOCK_PACKET socket_type


 
 
  1. **************************/

  2. Struct sockaddr {
  3. Sa_family_t sa_family;/* address family, AF_xxx */
  4. Char sa_data [14];/* 14 bytes of protocol address */
  5. };


  6. # Define _ K_SS_MAXSIZE128/* Implementation specific max size */
  7. # Define _ K_SS_ALIGNSIZE (_ alignof _ (struct sockaddr *))
  8. /* Implementation specific desired alignment */

  9. Typedef unsigned short _ kernel_sa_family_t;

  10. # Define sockaddr_storage _ kernel_sockaddr_storage
  11. Struct _ kernel_sockaddr_storage {
  12. _ Kernel_sa_family_tss_family;/* address family */
  13. /* Following field (s) are implementation specific */
  14. Char _ data [_ K_SS_MAXSIZE-sizeof (unsigned short)];
  15. /* Space to achieve desired size ,*/
  16. /* _ SS_MAXSIZE value minus size of ss_family */
  17. } _ Attribute _ (aligned (_ K_SS_ALIGNSIZE);/* force desired alignment */

  18. Both struct sockaddr and struct sockaddr_storage are common sockets


To unify various protocols, the socket interface defines two general structures: sockaddr (16 bytes) and sockaddr_storage (128 bytes). sockaddr_storage is used to adapt sockaddr_in6 (28 bytes) the protocol with a relatively large length is defined later. If a general socket such as sockaddr_storage needs to be used, it is strongly converted to sockaddr and the length is sizeof (struct sockaddr_storage)

Several Interfaces in network programming will use the above data structure

Application Layer-> kernel, which is sent to the application layer, data is sent to the protocol stack, and addrlen is the input parameter.

Int bind (int sockfd, struct sockaddr * my_addr, socklen_t addrlen );

Int connect (int sockfd, const structsockaddr * serv_addr, socklen_t addrlen );

Int sendto (int s, const void * msg, size_tlen, int flags, const struct sockaddr * to, socklen_t tolen );

Kernel> application layer, which is received by the application layer. data comes from the protocol stack, and addrlen is the value-result parameter.

Intaccept (int s, struct sockaddr * addr, socklen_t * addrlen );

Int recvfrom (int s, void * buf, size_t len, intflags, struct sockaddr * from, socklen_t * fromlen );

Intgetpeername (int s, struct sockaddr * name, socklen_t * namelen );

Int getsockname (int s, struct sockaddr * name, socklen_t * namelen );

Getsockname: return local Protocol address: getpeername: Return remote Protocol address

Applicable scenarios:

1. if you do not need to use bind () or call bind () without specifying the local Protocol address, you can call getsockname () to return the local IP address and port number allocated to the connection by the kernel, you can also obtain the protocol family of a set of interfaces.

2. When a new connection is established, the server can also call getsockname () to obtain the local IP address allocated to the connection.

3. When a server sub-process calls the exec function to start execution, it can only call the getpeername () function to obtain the customer's IP address and port number.

4. If bind () is not called, connect () is called. Only the getsockname () call can obtain the local address in the system. When returned, the namelen parameter contains the actual number of bytes of the name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.