UNIX Network Programming Reading Notes: interface address Structure

Source: Internet
Author: User

Preface

Most function sets require a pointer to the address structure of the set interface as a parameter. Each protocol family defines its own interface address structure. The names of these structures start with sockaddr _ and end with a unique suffix for each protocol family.

IPv4 address Structure

The IPv4 address structure is also known as the Internet address structure. It is named after sockaddr_in and is defined in the header file <netinet/in. h>. Its POSIX is defined as follows:

                                          in_addr   sin_addr;                                                             sin_zero[];    
Note:

(1) Unless the routeentry interface is involvedSin_len, we do not need to set or check.

(2)The IPv4 address and TCP or UDP port number are always stored in the network byte order in the address structure of the set interface.We must keep this in mind when using these members.

(3)There are two different methods to access a 32-bit IPv4 address.. For example, if serv is defined as an Internet interface address structure, then serv. the 32-bit IPv4 address provided by sin_addr is an in_addr structure, while the serv. the 32-bit IPv4 address provided by sin_addr.s_addr is an in_addr_t (usually an unsigned 32-bit integer ).

(4)Sin_zero is not used for the moment, but it is always set to 0. For convenience, we usually set the entire structure to 0 when initializing the structure, not just set the sin_zero member to 0. although the use of most structures does not require this member to be 0, this member must be 0 when a non-Wildcard IPv4 address is bound.

(5)The Set interface address structure is only used on a specified host.: Although some members (such as IP addresses and port numbers) in the structure are used in communication between different hosts, the structure itself is not involved in communication.

General interface address Structure

When passed as a parameter to any set of interface functions, the set interface address structure is always passed through pointers. However, A set of interface functions that use pointers to obtain this parameter must process the set interface address structure from any supported protocol family.

One problem is how to declare the Data Type of the passed pointer. Ansi c has a simple solution: it has a common pointer type void *. However, the set of interface functions are defined before ansi c. This method was adopted in 1982: DefineGeneral interface address Structure, As follows:

               sa_data[];      

As a result, the set of interface functions are defined as pointers pointing to the general set of interface address structures. This requires that any call to these functions must convert the pointer type pointing to the Protocol-specific socket interface address structure to a pointer pointing to the universal socket interface address structure.

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.