Reference: https://zhidao.baidu.com/question/191740827.html
Private IP address range:
a:10.0.0.010.255.255.255 10.0.0.0/8
b:172.16.0.0172.31.255.255 172.16.0.0/12
c:192.168.0.0192.168.255.255 192.168.0.0/16
The socket structure in the C language is as follows
struct sockaddr_in { short sin_family; USHORT Sin_port; IN_ADDR sin_addr; CHAR sin_zero[8];}
Where the IP address field is SIN_ADDR, when you select an IP address of type IPV4, Sin_addr is a 4-byte number for a network byte order.
Sample
192.168.207.1
C0 A8 CF 01
Filter:
A:startwith 10.x.x.x
B:startwith 172. (16~31). x.x
C:startwith 192.168.x.x
Order of Judgment:
B, C, A
The pseudo code is as follows:
Char data[4] = networkaddr;
If data[0] = = 10:
return True
Elif Data[0] = = 192 and data[1] = = 168:
return True
Elif data[0] = = 172:
If data[1] >= and data[1] <= 31:
return True
Else
Return False
#include <Windows.h>#include<stdio.h>#include<string.h>#pragmaComment (lib, "Ws2_32.lib")//inet_addr ()BOOL islocaladdr (DWORD netaddr) {unsignedChardata[4]; BOOL BRet=FALSE; memcpy (data, (Char*) &netaddr,4); if(Ten= = data[0]) {BRet=TRUE; } Else if(192= = data[0] &&168= = data[1]) {BRet=TRUE; } Else if(172= = data[0]) { if(data[1] >= -&& data[1] <= to) {BRet=TRUE; } } Else if(!MEMCMP (data, SERVADDR,4) ) {BRet=TRUE; } returnBRet;}intMain () {Char*addrlist[] = { "192.168.207.1", "192.12.1.3", "172.16.2.1", "172.33.1.1", "10.12.12.1", "43.1.31.23", }; Char*result[] = { "X Local Address", "O Local Address" }; intaddrcnt =sizeof(addrlist)/sizeof(addrlist[0]); DWORD netaddr=0; BOOL ret=FALSE; for(inti =0; i < addrcnt; i++) {netaddr=inet_addr (Addrlist[i]); RET=islocaladdr (NETADDR); printf ("%-16s [%s]\n", Addrlist[i], Result[ret]); } return 0;}
Operation Result:
Use the C language to determine if an IP address is a private address