Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Can the internal and external networks be distinguished by IP?
Can the internal and external networks be distinguished by IP?

The internal IP is starting with 192, or starting with 172

The external IP is starting with something else

In Start--Run--enter cmd --Enter, type ipconfig/all in it, dns will be displayed, and ipaddress is the external network address

In the tcp/ip protocol, three IP address areas are reserved as private addresses. The address range is as follows :

10.0.0.0/8:10.0.0.0~10.255.255.255

172.16.0.0/12:172.16.0.0~172.31.255.255

192.168. 0.0/16: 192.168.0.0~192.168.255.255

Networks using reserved addresses can only communicate internally and cannot be interconnected with other networks. If you want to communicate with the outside world, you must communicate with the outside world through a gateway. NAT is used here, and NAPT technology is the proxy mechanism used to ensure communication.

In addition, although some broadband operators also use non-private addresses to assign to users, due to routing settings, other users on the Internet cannot access these IPs. The above two parts of IP can be called intranet IP. The following part of IP will not be discussed this time.

If the IP address of the network interface on your machine falls within the range of the above reserved addresses, you can be sure that you are in intranet mode.

NAT requires that the connection of the entire service be actively initiated from the internal network to the external network, and users on the external network cannot directly (actively) initiate connection requests to the services on the internal network unless they are in the NAT (all) Port mapping is done on the gateway for the service port. The NAT method requires that the outermost gateway has at least one public IP, and can access the external server with the displayed IP, such as: [202.108.22.5]

Trace complete.

3) Programming implementation

Get a list of all IP addresses of this machine and analyze the IP list:

1) If there is only a LAN IP in the list, it means it is on the internal network;

2) If there is a LAN IP and a public IP in the list, it means it is a gateway;

3) If there is only a public IP in the list, it means it is an independent IP.

//Other platforms are not considered here, the test is under the inet architecture, the input IP is the host byte order

// 0xa -- "10.0.0.0">>24 ; 0xc0a8--"192.168.0.0.">>16; 0x2b0--"127.17.0.1">>22

int isInnerIP( uint32_t a_ip )

{

int bValid = -1;

if( (a_ip>>24 == 0xa) || (a_ip>>16 == 0xc0a8) || (a_ip>>22 == 0x2b0) )

{

bValid = 0;

}

return bValid;

}

< p>int isInnerIP( char* a_strip )

{

return 0;

}

IP related applications

//Get a list of all IP addresses of this machine and display them in string and integer format respectively

int getHostIP() //return int

{

struct sockaddr_in localAddr, destAddr;

struct hostent* h;

char temp[128];

int nRect = gethostname(temp, 128);

printf("ipaddr src3 is: %s/n", temp);

if(nRect !=0)

{

p>

printf("error");

}

h = gethostbyname(temp);

if(h)

< p> {

for(int nAdapter=0; h->h_addr_list[nAdapter]; nAdapter++)

{

memcpy(&destAddr.sin_addr.s_addr, h->h_addr_list[nAdapter], h->h_length);

// Output the IP address of the machine.

printf("Address string: %s/n", inet_ntoa( destAddr.sin_addr)); // Display address string

printf("Address int: %d/n", destAddr.sin_addr.s_addr); // Convert to integer number

}

}

return 0;

}

//Check whether the string IP is legal

int isCheckTrue(char* strip)

{

int value;

for( int i = 0; i < strlen(strip); i++)

{

// let's check if all entered char in entered

// IP address are digits

if(strip[i] == '.')

continue;

if(isdigit(strip[i]) == 0)

{

return - 1;

}

}

return 0;

}

//Convert the string IP For shaping IP

int str2intIP(char* strip) //return int ip

{

int intIP;

if(! (intIP = inet_addr(strip)))

{

perror("inet_addr failed./n");

return -1;

}

return intIP;

}