Apache source code Panoramic Analysis: network address Processing

Source: Internet
Author: User
Article Title: Apache source code Panoramic Analysis: network address processing. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Socket address

Before we understand the encapsulation of IP addresses in APR, we should first take a look at the general usage of IP addresses. The following code hides the simple server socket address initialization process:

Struct sockaddr_in server_addr;/* local address information */

Server_addr.sin_family = AF_INET;

Server_addr.sin_port = htons (SERVPORT );

Server_addr.sin_addr.s_addr = INADDR_ANY;

Bzero (& (server_addr.sin_zero), 8 );

......

Bind (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr ));

Accept (sockfd, (struct sockaddr *) & remote_addr, & sin_size );

The Socket API provides three types of addresses: sockaddr, sockaddr_in, and sockaddr_un. Sockaddr is a common Socket Structure. sockaddr_in is the address description structure of the Internet protocol family. sockaddr_un is the address description structure of the Unix protocol group. The sa_family in the sockaddr_in structure determines whether it is sockaddr_in or sockaddr_un.

If you directly use the address structure provided by the Socket API, there are at least the following problems:

1. For internet addresses in network applications, as shown in the code above, sockaddr_in is usually used to describe the internet addresses. In some Socket API functions, sockaddr is used as the Socket address, therefore, sockaddr_in must be forcibly converted to the sockaddr type during use, which is troublesome and error-prone.

2. sockaddr_in is not a particularly understandable data structure. Generally, sin_family and sin_port are relatively easy to remember, while the socket address sin_addr.s_addr is not necessarily. This structure of sockets is undoubtedly a nightmare for ordinary people.

3. Another problem is the Ipv6 address. Currently, Apache supports both Ipv4 and Ipv6 addresses. To support Ipv6, you must use the address data structure corresponding to Ipv6.

For a good class library, both Ipv4 and Ipv6 protocols must provide the same interface, which must be easy to understand and hide internal details as much as possible, for example, sin_addr.s_addr is only exposed to users.

Based on the above analysis, APR only uses a Data Structure apr_sockaddr_t to describe the IP address, which is defined in the file apr_network_io.h:

Struct apr_sockaddr_t {

Apr_pool_t * pool;

/* Part 1 */

Char * hostname;

Char * servname;

/* Part 2 */

Apr_port_t port;

Apr_int32_t family;

Union {

Struct sockaddr_in sin;

# If APR_HAVE_IPV6

Struct sockaddr_in6 sin6;

# Endif

# If APR_HAVE_SA_STORAGE

Struct sockaddr_storage sas;

# Endif

} Sa;

/* Part 3 */

Apr_socklen_t salen;

Int ipaddr_len;

Int addr_str_len;

Void * ipaddr_ptr;

Apr_sockaddr_t * next;

};

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] Next page

Related Article

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.