C Language for IP address legality detection and subnet matching

Source: Internet
Author: User
Tags htons

Tag:io   os    use    ar    data    sp    on   c   amp   

#include <stdio.h> #include <stdlib.h> #ifdef win32#include <Winsock2.h> #else # <fcntl.h > #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/tcp.h > #include <arpa/inet.h> #include <sys/select.h> #include <sys/ioctl.h> #include <unistd.h> #include <netdb.h> #include <errno.h> #include <sys/un.h> #include <linux/types.h> #include <linux/netlink.h> #include <linux/if.h> #include <linux/sockios.h> #include <linux/ethtool.h > #ifdef win32#pragma Comment (lib, "Wsock32.lib") #endif/* parameter UINT32 are network byte order.    *//*ip address is valid, return ture, fail return false*/int netipisvalid (_uint32 IP) {int i;    struct IN_ADDR addr;        ADDR.S_ADDR = IP;    i = inet_addr (Inet_ntoa (addr)); if ((i = = 0) | |        (i = = 0xFFFFFFFF))    return FALSE;   else return TRUE;}    /*mask Subnet mask is legal, return ture, fail return false*/int netmaskisvalid (_uint32 mask) {int i;    unsigned long II; i = NetIPisvalid (mask);        if (i==true) {II = Ntohl (mask);        if ((ii|ii-1) ==0xffffffff) {return TRUE; }} return FALSE;}    /*mask Subnet mask is legal, return ture, fail return false*/int netmaskandipisvalid (_uint32 IP, _uint32 mask) {int i;    int A, b=0, C;    i = Netipisvalid (IP);    if (i!=true) return FALSE;    i = netmaskisvalid (mask);    if (i!=true) return FALSE;    A = ip&0x000000ff;    b = Ntohl (mask);        /* First compare with default subnet mask */if (a>0&&a<127) {if (MASK&LT;0X000000FF) return FALSE;    if (MASK&GT;0X000000FF) b-=0xff000000;        } if (a>=128&&a<=191) {if (MASK&LT;0X0000FFFF) return FALSE;    if (MASK&GT;0X0000FFFF) b-=0xffff0000;        } if (a>=192&&a<=223) {if (MASK&LT;0X00FFFFFF) return FALSE;    if (MASK&GT;0X00FFFFFF) b-=0xffffff00; }/* The first of each subnet segment is the network address, which is used to flag the network, and the last is the broadcast address, which is used to represent all hosts on the network. These two IP addresses are TCP/IP reserved, not allocated to host use. */c = ~ntohl (mask) &ntohl (IP); if (c==0| |    C==~ntohl (mask)) return FALSE;        When subnetting is specified in/*RFC 1009, the subnet number cannot be all 0 or 1, resulting in an IP address of ambiguity */if (b>0) {c = b& (Ntohl (IP)); if (c==0| |    C==B) return FALSE; } return TRUE;    /* Test whether the master and subnet matches, or whether the two host IP is within the same network segment */int Netipandsubnetvalid (_uint32 IP, _uint32 Subip, _uint32 mask) {int i;    int ADDR1, ADDR2;    i = Netmaskandipisvalid (IP, mask);    if (i!=true) return FALSE;    i = Netmaskandipisvalid (Subip, mask);    if (i!=true) return FALSE;    ADDR1 = ip&mask;    ADDR2 = subip&mask;    if (ADDR1!=ADDR2) return FALSE; return TRUE;}


Technical realization and function:
1. Implement in C language
2. Determine if the IP address is legitimate
3. Determine if mask is legal
4. Determine if mask and IP address combination is legal

Test:

Test Environment: Win7 VC + +;
Test Result:
 1, test IP is legal:
   input IP 0xFFFFFFFF is 255.255.255.255, the result is illegal;
   input IP is 0x00000000 that is 0.0.0.0, the result is illegal;
   The input IP is 0x00000000--0xffffffff between 0.0.0.0--255.255.255.255 (all 0 and all 1 except) and the result is legal.
 2, test subnet mask is legitimate:
   input 0x00c0ffff (1111 1111.1111 1111.1100 0000), the result is legal;
   Enter an unsigned int number, the binary form is full 1 on the left for all 0, the result is legal, the remaining forms are illegal.
 3, test subnet mask, and IP match:
   The input IP is 0x410aa8c0-192.168.10.65 (Class C IP), the input subnet mask is 0xc0ffffff, the result matches, and the input is less than the 0x00ffffff subnet mask;
   The input IP is 0x0141a885 or 133.168.65.1 (Class B IP), the input subnet mask is 0x00c0ffff, the result matches, and the input is less than 0x0000ffff subnet mask;
   The input IP is 0x0100413f-63.65.0.1 (Class A IP), the input subnet mask is 0x0000c0ff, the result matches, and the input is less than the 0x000000ff subnet mask. The first and last IPs in the
   subnet segment are not assignable, subnet numbers are all 0 or all 1 subnets are not available, and the results are mismatched.
 4, test whether two IPs are in the same subnet segment:
   enter two IPs for 0x0c81c480 0x118fc480, enter a subnet mask of 0x00c0ffff, the result is two IPs in the same network segment;
   input Two IP is 0x0c81c480 0x117fc480, input subnet mask is 0X00C0FFFF, result two IP is not the same network segment.

Note: The test data is of the unsigned int type, network byte order.

Additional knowledge:

In general, the 32-bit IP address is divided into two parts, namely the network number and the host number, which we call the IP address of the "Network Network part" and "local part" respectively. The subnet addressing technology further divides the local part into the physical network section and the hosts section, where the physical network is used to identify the different physical networks under the same IP network address, which is the subnet.

Class A IP segment 0.0.0.0 to 127.255.255.255

Class B IP segment 128.0.0.0 to 191.255.255.255

Class C IP segment 192.0.0.0 to 223.255.255.255

The default subnet mask assigned by XP is only 255 or 0 per segment

Class A default subnet mask 255.0.0.0 a subnet can hold up to more than 16.77 million computers

Default subnet mask for Class B 255.255.0.0 a subnet can hold up to 60,000 computers

Default subnet mask for Class C 255.255.255.0 a subnet can hold up to 254 computers
Take the Class C address as an example. The first 3 bytes in the IP address represent the network number, and the next byte indicates both the subnet number and the host number, as well as whether the two IP addresses belong to a network segment. If they belong to the same network interval, the exchange of information between the two addresses does not pass through the router. If it is not the same network interval, that is, the subnet number is different, the information exchange of two addresses will be through the router.

See an example:
IP address of a: 11000000,10101000,00000000,00000101
Subnet Mask: 11111111,11111111,11111111,00000000
IP address of B: 11000000,10101000,00000000,00010110

Look at the top of the content, the subnet mask on the left a total of 24 bits of 1, that means if the two IP address of the first 24 bits are the same, then the two IP address is in the same network segment, see my red labeled A and B are the same address, which means a and B in the same network segment.

One more example, if the data of the a address is sent to the C address, the IP address of C is 192.168.56.21
IP address of a: 11000000,10101000,00000000,00000101
Subnet Mask: 11111111,11111111,11111111,00000000
IP address of C: 11000000,10101000,00111000,00010101

Look at the top of a and C, according to the requirements of the subnet mask, if the first 24 bits of C and a the first 24 are the same, then A and C is the same network segment, see the address of the top C, I use the blue to mark the different digits, so that a and C is not in the same network segment, The router can not directly send a to C data directly through a router sent past, so the router will first forward the data of a to another router (one does not continue to send down), and then sent to C.

BYTE-Order conversion functions
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
These functions are very well remembered, for example Htons Hton represents host to network, s represents unsigned short
Char FAR * INET_NTOA (struct in_addr in);
Converts an IP to an Internet standard point-in-a-format string.
in_addr_t inet_addr (const char *CP);
Converts a dotted-decimal IP to a long integer (u_long type). The return value is already a network byte order and can be used directly as an Internet address

A function returns a value of TRUE or FALSE only if the return value is judged without the if (i==true) or if (I==false), and with the IF (i) if (!i) programming specification.

The string returned by Inet_ntoa () is temporarily mounted in a statically allocated buffer, and the next time the function is called, the buffer is rewritten

C Language for IP address legality detection and subnet matching

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.