Network byte order and address translation

Source: Internet
Author: User
Tags 04x printf htons

1: Size end byte order

Reference blog: size-end mode

2: Store byte order and transmit byte order

Storage byte order: a multi-byte variable is stored in memory, the small end of the variable is stored in the memory starting position is the small end byte order; The big-endian data of the variable is stored in the memory starting position is the endian byte;

Transmission byte Order: The protocol transmits the multi-byte variable transmission way, first transmits the big-endian byte the way to become the big endian byte order, transmits the small end byte the way to become the small end byte, the network protocol uses the big endian byte sequence transmission data, the USB protocol uses the small end byte sequence transmission protocol;

The relationship between the two: due to the existence of the system host size and end mode, and can not guarantee the network communication between the two hosts of the storage byte sequence consistent, so the network protocol in the transmission of data when the byte order is used in the endian byte sequence, so that the network communication between the host will be agreed, docking received data (big-endian mode) Processing is based on its own storage byte order, and the storage byte order and the transmission byte order are essentially the same concept;

3: Byte-order conversion function

#include <netinet/in.h>

uint16_t htons (uint16_t host16bitvalue)   //Returns the value of the network byte order
uint32_t htonl ( uint32_t host32bitvalue)   //Returns the value of the network byte order

uint16_t Ntohs (uint16_t net16bitvalue)    //Returns the value of the host byte order
uint32_t Ntohl (uint32_t net32bitvalue)    //Returns the value of the host byte order
In the above function, H represents host,n representing Network,s on behalf of SHORT,L Short;

When using these functions, we do not care about the host byte order and the real value of the network byte order, as long as the appropriate function is called to convert a given value between the host and the network byte order, because in those systems with the same network byte order (big endian), these functions are empty, the following code:

#ifdef __optimize__/
* We can OPTIMIZE calls to the conversion functions.  Either nothing have to is done
    or we is using directly the byte-swapping functions which
    often can be inlined.
  
   */
 # if __byte_order = = __big_endian/
 * The host byte order is the same as network BYTE order, so
    these funct Ions is all just identity.  * * *
 define NTOHL (x)   (x)
 # define NTOHS (x)   (x)
 # define HTONL (x)   (x)
 # define Htons ( x)   (x)
 # Else
 #  If __byte_order = = __little_endian
 #   define NTOHL (x) __bswap_32 (x)
 #   define NTOHS (x) __bswap_16 (x)
 #   define HTONL (x) __bswap_32 (x)
 #   define htons (x) __ Bswap_16 (x)
 #  endif
 # endif 
 #endif  
  

If the host system byte-order is the big-endian mode, these functions are defined as NULL, in the socket programming, in order to improve the portability of code, should always use these functions for host byte order and network byte order processing;

Examples of function use:

#include <stdio.h>
#include <netinet/in.h>

int main (int argc,char **argv)
{
	int value1 = 0x12345678;          Defines a 4-byte data short
	int value2 = 0x1234;        Define a 2-byte data
	printf ("htonl (0x%08x) = 0x%08x\r\n", value1,htonl (value1));
	printf ("htons (0x%04x) = 0x%04x\r\n", Value2,htons (value2));
}
The code executes as follows:


4: Address conversion function

Two address-handling functions that use protocol independence, the functions are as follows:

#include <arpa/inet.h>

//return: If the input is not a valid expression format return 0, Error returns-1
int Inet_pton (int family,const char *) If successful return 1 StrPtr, void *addrptr);

Returns: A pointer to the result if successful, or a null
const char *inet_ntop (int family,const void *addrptr,char *strptr,size_t len) if an error occurs;
Examples of function use:

#include <stdio.h>
#include <arpa/inet.h>

int main (int argc,char **argv)
{
	unsigned char ADDRESS[4];   Store IP address
	char Str[inet_addrstrlen] = {0};
	char *string = NULL;
	
	string = "192.168.1.1";
	Inet_pton (af_inet,string,address);
	printf ("%d%d%d\r\n", address[0],address[1],address[2],address[3]);

	Address[0] = 127;address[1] = 127;address[2] = 33;address[3] =;
	Inet_ntop (af_inet,address,str,16);
	printf ("%s\r\n", str);

	return 0;
}

The code executes as follows:

The code verifies that the byte order stored in the address array after the conversion of the Inet_pton function is already a network byte order (big-endian byte order);







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.