如何判断自己IP是内网IP还是外网IP
如何判断⾃⼰IP是内⽹IP还是外⽹IP
tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下:
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
使⽤保留地址的⽹络只能在内部进⾏通信,⽽不能与其他⽹络互连。如果要与外部通信,那么必须通过⽹关与外部通信,这⾥使⽤了NAT, NAPT技术就是⽤来保证通信的代理机制。
另外,⼀些宽带运营商尽管也使⽤了⾮私有地址分配给⽤户使⽤,但是由于路由设置的原因,Internet上的其他⽤户并不能访问到这些ip。上⾯2部分IP都可称为内⽹IP,下⾯这部分IP不列⼊本次讨论范围。
如果⾃⼰机器上⽹络接⼝的ip地址落在上述保留地址的范围内,则可以肯定⾃⼰处于内⽹模式下。
NAT要求整个服务的连接是从内⽹向外⽹主动发起的,⽽外⽹的⽤户⽆法直接(主动)向内⽹的服务发起连接请求,除⾮在NAT的(所有)⽹关上针对服务的端⼝作了端⼝映射。NAT⽅式要求最外围的⽹关
⾄少有⼀个公⽹的IP,可以访问显IP的外部服务器如:获取到外部IP,将这个IP与⾃⼰机器上⽹络接⼝的ip⽐较,即可知道⾃⼰的ip是不是内⽹IP。
判断⾃⼰IP类型,可使⽤下⾯三种任意⼀种⽅法:
1)      在windos命令台程序下,⽤ipconfig。
Eg: 下⾯内⽹IP是192.168.0.1,外⽹IP是125.34.47.25,因此是⽹关。
C:/Documents and Settings/user>ipconfig
Windows IP Configuration
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.0.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
Ethernet adapter {6C8AEC26-0EC3-40FE-812E-A46778ECA752}:
Media State . . . . . . . . . . . : Media disconnected
PPP adapter 宽带拨号:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 125.34.47.25
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 125.34.47.25
2)      ⽤tracert来判断IP类型
如果第⼀个hops不是内⽹IP,那么⾃⼰就是外⽹IP了,反之,如果⾃⼰是内⽹IP,那么第⼀个hops显⽰的就是⽹关的内⽹IP,下⾯的例⼦显然说明是外⽹IP了。
C:/Documents and Settings/user>tracert www.baidu
Tracing route to www.a.shifen [202.108.22.5]
over a maximum of 30 hops:
1    15 ms    16 ms    14 ms 125.34.40.1
2  14 ms    *        *    61.148.8.9
3    26 ms    72 ms    40 ms xd-22-5-a8.bta [202.108.22.5]
Trace complete.
3)编程实现
获取到本机所有的IP地址列表,对IP列表进⾏分析:
1)如果列表中只有局域⽹IP,那么说明是在内⽹;
2)如果列表中有局域⽹IP,也有公⽹IP,那么说明是⽹关;
3)如果列表中只有公⽹IP,那么说明是独⽴IP。
//此处不考虑其它平台,在inet架构下测试, 输⼊的ip为主机字节顺序
/
毕业自我鉴定怎么写/ 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;
}
int isInnerIP( char* a_strip )
{
施工组织设计内容
return 0;
琼瑶剧有哪些
}
IP相关的应⽤
//获取到本机所有的IP地址列表,并分别⽤字符串与整形形式来显⽰
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)
{
printf("error");
}
h = gethostbyname(temp);
if(h)
{
for(int nAdapter=0; h->h_addr_list[nAdapter]; nAdapter++)
{
memcpy(&destAddr.sin_addr.s_addr, h->h_addr_list[nAdapter], h->h_length);                    // 输出机器的IP地址.
printf("Address string: %s/n", inet_ntoa(destAddr.sin_addr)); // 显⽰地址串
printf("Address int: %d/n", destAddr.sin_addr.s_addr); // 转化为整形数字
}
}
return    0;
}
//检查字符串IP是否合法
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;
}
//将字符串IP转化为整形IP
int str2intIP(char* strip) //return int ip
{
int intIP;
六级分值分配if(!(intIP = inet_addr(strip)))
{
perror("inet_addr failed./n");
return -1;
}
return intIP;
知否张大娘子结局
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。