IP conversion of Linux networks

Source: Internet
Author: User

An IP address is represented by a decimal number separated by a decimal point, which we call dotted decimal notation. Each of these decimal numbers represents a single byte of unsigned value (according to the network byte order) because each byte is an unsigned 8-bit value, which limits the range that each byte can represent is 0~255.

A special IP

    • Each byte is a 0 address ("0.0.0.0") corresponding to the current host;
    • Each byte in the IP address is a 1 IP address ("255.255.255.255") that is the broadcast address of the current subnet;
    • IP address in any of the "11110" beginning of the class E IP address are reserved for future and experimental use;
    • The IP address can not start with the decimal "127", the address of the number 127.0.0.1 to 127.255.255.255 for loop testing, such as 127.0.0.1 can represent the native IP address;
    • The first 8-bit group of the network ID also cannot have all "0", and the full "0" represents the local network.
related functions for manipulating IP addresses in_addr_t inet_addr (const char *string);
This function uses string as the input parameter and converts the dot-decimal IP address into an integer value based on the network byte order. The function returns the IP address of the binary network byte order (struct In_add), otherwise returns Inaddr_none (-1).
Example:
#include <arpa/inet.h> #include <stdio.h> #include <stdlib.h>int main (int argc, char *argv[]) {struct IN_ADDR addr;if (argc! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} if ((addr.s_addr = inet_addr (argv[1])) = =-1) {fprintf (stderr, "%s is Invalid address\n", argv[1]);} else{fprintf (stdout, "%u\n", addr.s_addr);} return 0;} Note: If the IP address is 255.255.255.255. Then 1 is returned after calling the Inet_addr () function (since the complement of-1 is 0xFFFFFFFF). It is therefore not recommended to use the INET_ADDR () function. Instead, use the Inet_aton () function.

int Inet_aton (const char *string, strcut in_addr *addr);
Converts the IP address of a dotted decimal string type stored in string to a binary IP address, and the converted value is stored in the struct in_addr that the pointer addr points to. The function execution successfully returns a value other than 0, and the failure returns 0.
Example:
#include <stdio.h> #include <arpa/inet.h> #include <stdlib.h>int main (int argc, char *argv[]) {if (argc ! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct IN_ADDR addr;if (Inet_aton (argv[1],&addr)! = 0) fprintf (stdout, "%u\n", addr.s_addr); elsefprintf (stdout, "%s Invalid address\n ", argv[1]); return 0;}

Char *inet_ntoa (strcut in_addr addr);
Converts a 32-bit binary IP address into a dotted-decimal string form. The function execution succeeds returns the string, and the failure returns NULL.
#include <stdio.h> #include <arpa/inet.h> #include <stdlib.h>int main (int argc, char *argv[]) {if (argc ! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct IN_ADDR addr;if (Inet_aton (argv[1],&addr) = = 0) {fprintf (stderr, "%s Invalid address\n", argv[1]); Exit (Exit_ FAILURE);} printf ("%s\n", Inet_ntoa (addr)); return 0;}

in_addr_t inet_network (const char *STR);
Converts the network address of the string form of the parameter str to the host byte-order binary IP address (ignoring the size side). After the successful return of the converted result, the failure returns-1. Example:
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h>int main ( int argc, char *argv[]) {if (argc! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct in_addr addr;if ((addr.s_addr = Inet_network (argv[1])) = =-1) {fprintf (stderr, "%s Invalid address\n", argv[1]); Exit (exit_failure);} else{fprintf (stdout, "%u \ n", addr.s_addr);} return 0;}


in_addr_t inet_lnaof (struct in_addr addr);
Extracts the host address from the parameter addr and executes a host address that successfully returns the host's byte-order form.
such as: 192.168.1.1 belongs to Class C address, the host address is 1.
Example:
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h>int main ( int argc, char *argv[]) {if (argc! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct in_addr addr;if ((addr.s_addr = Inet_network (argv[1])) = =-1) {fprintf (stderr, "%s Invalid address\n", argv[1]); Exit (exit_failure);} ADDR.S_ADDR = htonl (ADDR.S_ADDR); fprintf (stdout, "0x%x \ n", inet_lnaof (addr)); return 0;}


in_addr_t inet_netof (struct in_addr addr);
Extracts the network address from the parameter addr, performing a successful return of the network address in the host byte order form.
For example: 192.168.1.1 belongs to class C address, the network address is 0x c0a801.
Example:
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h>int main ( int argc, char *argv[]) {if (argc! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct in_addr addr;if ((addr.s_addr = Inet_network (argv[1])) = =-1) {fprintf (stderr, "%s Invalid address\n", argv[1]); Exit (exit_failure);} printf ("0x%x\n", addr.s_addr); addr.s_addr = htonl (ADDR.S_ADDR); fprintf (stdout, "0x%x \ n", Inet_netof (addr)); return 0;}


struct IN_ADDR inet_makeaddr (int net, int host);
Merges the host byte-order network address NET with the host address host into a network byte-order IP address.
Example:
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h>int main ( int argc, char *argv[]) {if (argc! = 2) {fprintf (stderr, "%s <dotted-address>\n", argv[0]); exit (exit_failure);} struct IN_ADDR addr, addr_1;if ((addr.s_addr = Inet_network (argv[1])) = =-1) {fprintf (stderr, "%s Invalid address\n", argv[ 1]); exit (exit_failure);} ADDR.S_ADDR = htonl (addr.s_addr); uint32_t net = inet_netof (addr); uint32_t host = inet_lnaof (addr); addr_1 = Inet_makeaddr (net,host);p rintf ("0x%x \ n", htonl (ADDR_1.S_ADDR)); return 0;}


IP conversion of Linux networks

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.