Host byte order and network byte order

Source: Internet
Author: User
Tags htons

Different CPUs have different byte-order types, which are the order in which integers are stored in memory, called the host order.

Two of the most common:

1. Little endian: Storing low-order bytes at the start address

2. Big Endian: Stores high-order bytes at the start address

LE Little-endian (small end)
    • The byte sequence that best fits the human mind;
    • Low store value of address low;
    • High-level storage value of the address;
    • How to say is the most consistent with the human mind of the byte sequence, because from the first impression of people;
    • Low value, it should be placed in the memory address is small, that is, memory address low;
    • Conversely, the high value should be placed in the memory address large place, that is, memory address high;
Be Big-endian (big-endian)
    • The most intuitive byte-order;
    • Address low storage value of high;
    • Address high storage value of low;
    • Why speak intuitively, do not consider the correspondence relation;
    • Just write the memory address from left to right in the order from low to high;
    • Write the values in the usual high to low order;
    • In contrast, a byte-by-byte padding is entered;

Example: How to store a double-character 0x01020304 (DWORD) in memory

Memory address

4000 4001 4002 4003
LE 04 03 02 01
be 01 02 03 04

Example: If we write 0X1234ABCD to memory starting with 0x0000, the result is

Big-endian Little-endian
0x0000 0x12 0xCD
0x0001 0x23 0xAB
0x0002 0xAB 0x34
0x0003 0xCD 0x12

The x86 series CPUs are Little-endian byte-order.

Network byte order is a well-defined data representation format in TCP/IP, which is independent of the specific CPU type, operating system and so on, so that the data can be interpreted correctly when transferring between different hosts. The network byte order takes the big endian sort method.

In order to convert the BSD socket provides a conversion function with the following four

Htons converting the unsigned short type from the host sequence to the network sequence
Htonl converting unsigned long from host to network order
Ntohs converting unsigned short type from network order to host order
Ntohl converting unsigned long from network order to host sequence

In systems using little endian, these functions convert the byte sequence;

In systems that use the big endian type, these functions are defined as empty macros;

Similarly, in the development of network programs or cross-platform development, you should also pay attention to ensure that only one byte order, otherwise the two sides of the interpretation of the same will produce a bug.

Note:

1, network and host byte conversion function: Htons (), Ntohs (), htonl (), Ntohl () (Note: S is short L is a long H is host n is the network)

2, different CPUs running different operating systems, byte order is also different, see the table below.

Processor OS byte sequencing
Alpha all Little endian
Hp-pa NT Little Endian
Hp-pa UNIX Big Endian
Intelx86 all Little Endian <-----x86 system is a small-endian byte-order system
motorola680x () all Big endian
MIPS NT Little Endian
MIPS UNIX Big Endian
PowerPC NT Little Endian
PowerPC non-NT Big endian <-----PPC system is endian system
rs/6000 UNIX Big Endian
SPARC UNIX Big Endian
IXP1200 Arm Core All Little endian

Reference:

Http://baike.baidu.com/view/2194385.htm

Three representation formats of IP addresses and their application in development

Using TCP/IP protocol for Network application development The first thing to face is the processing of IP address information. There are three different representation formats for IP addresses. IP address is the basis of data transmission in an IP network, it identifies a connection in an IP network, a host can have multiple IP addresses, and the IP address in the IP packet will remain intact in the network transmission. The following is a detailed description of the IP address of the three different presentation formats.

One, point 10 binary representation format

This is our most common representation format, such as the IP address of a machine may be "202.101.105.66". In fact, for Ipv4 (IP version), the IP address is composed of a 32-bit binary number, but such a sequence of numbers is undoubtedly very lengthy and difficult to read and remember. In order to facilitate people's memory and use, the series of numbers are divided into 4 groups, each group of 8, and replaced by 10 binary number to represent, and finally separated by small origin, so it evolved into a "point 10 binary representation format."

Take a look at the specific conversion process for that IP address:

IP Address: 11001010011001010110100101000010

Divided into 4 groups: 11001010 01100101 01101001 01000010

Decimal notation: 202 101 105 66

Point of Indication: 202.101.105.66

Network byte sequence format (nbo,network byte order)

Let's talk about the network byte-order format, which, like the host byte-order format we'll cover later, will only be encountered in network development. Therefore, in the following introduction, I assume that the reader has a certain basis for socket programming knowledge.

In the network transmission, the TCP/IP protocol, when saving the IP address of this 32-bit binary number, the protocol specifies that the low storage address contains data in the order of high-order bytes, which is called network byte order format. In the actual network transmission, the data according to each 32-bit binary number for a set of transmission, due to the impact of the storage sequence, the actual byte transmission sequence is from high-order byte to low byte transmission sequence.

In order for both sides of the communication to understand binary information such as the source address, destination address, and the length of the packet that the data is being grouped in, the binary information must be converted to the TCP/IP standard network byte order format before each packet is sent, regardless of the host or router. The network byte order format address is not affected by the host, router type, and its representation is unique.

In the development of socket programming, the conversion between the dot-string and the network byte-order format IP address can be realized through the functions inet_addr and Inet_ntoa.

The INET_ADDR function is prototyped as follows:

unsigned long inet_addr (const char FAR * CP)

The parameter CP in the function points to a standard point-and-click Address string in the network, where each number separated by a dot cannot be greater than 255, and these numbers can be decimal, octal, hexadecimal, or mixed.

such as "10.23.2.3", "012.003.002.024", "0xa.0x3.0x14.0x2", "10.003.2.0x12".

Here is a small example of a function that can be used to test whether a port of a target host is open, which is the basis of port scanning technology.

BOOL Scanport (char * m_ip,u_short m_port) {struct sockaddr_in m_sqladdress;//server ' address. SOCKET M_socket;int Ret;memset ((char *) &m_sqladdress,0,sizeof (m_sqladdress)); m_sqladdress.sin_port = Htons (m_ Port); M_SQLADDRESS.SIN_ADDR.S_ADDR = inet_addr (M_IP); m_sqladdress.sin_family = Af_inet;m_socket = socket (PF_INET,SOCK_STREAM,IPPROTO_TCP); Create TCP Connect.if (M_socket < 0) {return FALSE;} ret = Connect (m_socket, (struct sockaddr *) &m_sqladdress,sizeof (m_sqladdress)); return (ret);}
Host byte sequence format (hbo,host byte order)

Host byte order format as the name implies, the format of its IP address is related to a specific host or router. For different hosts, there are different formats for IP address storage, for example, for the Motorola 68k series host, HBO is the same as Nbo. For the Intel x86 series, HBO and Nbo are just the opposite.

In socket programming, there are four functions to complete the conversion between host byte order format and network byte order format: Htonl, Htons, Ntohl, and Ntohs. Htons and Ntohs Complete the conversion of 16-bit unsigned numbers, htonl and Ntohl to complete the conversion of 32-bit unsigned numbers.

In practical applications we are familiar with examples of the conversion of port numbers (as in the above example). This is because if the user enters a number and will specify that the number be used as the port number, the application must convert it from host byte order to network byte order (using the htons () function) before using it to establish the address, in order to comply with the storage standards specified by the TCP/IP protocol. Accordingly, if the application wants to display the port number contained in an address (for example, returned from the Getpeername () function), the port number must be converted from the network order to the host order (using the Ntohs () function) before it is displayed.

So, for IP addresses, what are the applications for host byte-order format conversions?

Application One: What if you want to know how many host addresses are between the two IPs from 202.156.2.23 to 202.156.9.65? At this point, you can convert two IP addresses to the host byte order format and subtract from it, the implementation is as follows:

int Getipcount (char * Ip1,char * ip2) {long pp;; Long SS;; pp = Ntohl (inet_addr (IP1));; SS = Ntohl (inet_addr (IP2));; Return (SS-PP + 1);;}

Application two: If a network segment is scanned, for example, is currently scanning 202.156.23.255, how to let the program know the next should be swept IP is 202.156.24.0? At this point, the current IP can be converted to host byte order format and add 1, in the conversion back to the network format, the implementation of the following:

Char *getnextip (char *m_curip) {struct sockaddr_in in;; Long pp;; char *re;; pp = Ntohl (inet_addr (M_curip));; PP = pp + 1;; in.sin_addr.s_addr = htonl (PP);; Re = Inet_ntoa (in.sin_addr);; return (re);;}
Summarize

This article introduces three different representations of IP addresses, including the causes of various formats, their meanings, and some applications in the development of socket programming. In the practical application, it is necessary to follow the standard of the format used in the application, as well as the flexibility to use the conversion between the formats and calculation techniques. Through the reading of this article, hope can give readers in the future study and work development to bring inspiration.

Reference:

http://bbs.csdn.net/topics/60375114

Host byte order and network 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.