判断两个IP是否属于同一子网

来源:互联网 发布:有寒暑假的工作知乎 编辑:程序博客网 时间:2024/06/02 19:56

子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据。
子网掩码与IP地址结构相同,是32位二进制数,其中网络号部分全为“1”和主机号部分全为“0”。利用子网掩码可以判断两台主机是否中同一子网中。若两台主机的IP地址分别与它们的子网掩码相“与”后的结果相同,则说明这两台主机在同一子网中。

示例:
I P 地址  192.168.0.1
子网掩码  255.255.255.0

转化为二进制进行运算:

I P 地址 11000000.10101000.00000000.00000001
子网掩码 11111111.11111111.11111111.00000000

AND运算
    11000000.10101000.00000000.00000000

 转化为十进制后为:
     192.168.0.0

 

  I P 地址  192.168.0.254
  子网掩码  255.255.255.0


  转化为二进制进行运算:

  I P 地址 11000000.10101000.00000000.11111110
  子网掩码 11111111.11111111.11111111.00000000

  AND运算
       11000000.10101000.00000000.00000000

    转化为十进制后为:
         192.168.0.0

      通过以上对两台计算机IP地址与子网掩码的AND运算后,我们可以看到它运算结果是一样的。均为192.168.0.0,所以这二台计算机可视为是同一子网络。

     

/******************************************************************************
 接口说明原型:   
      int checkNetSegment(const char *ip1, const char *ip2, const char *mask)

      输入参数:    char * IP1: 计算机1的IP地址,格式:“192.168.0.254”   
      char * IP2: 计算机2的IP地址,格式:“192.168.0.1”  
      char * Mask: 子网掩码,格式:“255.255.255.0”
      返回值:      0:IP1与IP2属于同一子网络;     1:IP地址或子网掩码格式非法;    2:IP1与IP2不属于同一子网

      File Name   :

      Version        :
      Author          :
      Created       : 

      Last Modified :
      Description   :  IP子网段判断
      Function List :

      History     :
      Date         :  2014/07/21
      Author      :
      Modification: Created file

 ******************************************************************************/

#include <stdlib.h>#include <stdio.h>#include<iostream>#include <string>#define VALID_COUNTOFDOT 3void getSegmentIP(const char *ip,int *arrayIP){ std::string strIP = ip; int nIPValue = -1; for(unsigned int i = 0; i < VALID_COUNTOFDOT+1; ++i)    //取IP数字段 {  size_t nPos = -1;  nPos = strIP.find_first_of(".");  if (nPos != std::string::npos)  {   std::string tmpStrSegment = strIP.substr(0,nPos);   nIPValue = atoi(tmpStrSegment.c_str());   strIP = strIP.substr(nPos+1);  }  else                                             //取最后一段  {   nIPValue = atoi(strIP.c_str());  }  arrayIP[i] = nIPValue; }}bool checkIPValid(const char *ip){ const char *pValue = ip; if(NULL == pValue) {  return false; } int countOfDot = 0; while('\0' != *pValue) {  if('.' == *pValue)  {   countOfDot ++;   if((*(pValue+1) == '0') && (*(pValue+2) != '.') && (VALID_COUNTOFDOT != countOfDot)) //'.'后面为数字0并且非最后一位,紧接着必须为‘.',否则返回false,认为非法   {    return false;   }  }  else  {   if (((*pValue < '0') || (*pValue > '9')))                        //IP段非数字0-9,返回false,认为非法   {    return false;   }  }  pValue++; } if( VALID_COUNTOFDOT != countOfDot) {  return false; } else {  std::string tmpStrIP = ip;  for(unsigned int i = 0; i < VALID_COUNTOFDOT+1; ++i)    //取IP数字段  {   size_t nPos = -1;   int nIPValue = -1;   nPos = tmpStrIP.find_first_of(".");   if (nPos != std::string::npos)   {    std::string tmpStrSegment = tmpStrIP.substr(0,nPos);    nIPValue = atoi(tmpStrSegment.c_str());    if(tmpStrSegment.length() > 1 && nIPValue < 9)   //首位出现00x的IP,认为非法    {     return false;    }    else if(tmpStrSegment.length() > 2 && nIPValue < 100)   //首位出现0xx的IP,认为非法    {     return false;    }    if( 0 > nIPValue || nIPValue > 255)    {     return false;                             //IP段不在[0,255],返回false,认为非法    }    tmpStrIP = tmpStrIP.substr(nPos+1);   }   else                                             //取最后一段   {    nIPValue = atoi(tmpStrIP.c_str());    if(tmpStrIP.length() > 1 && nIPValue < 9)   //末IP段出现00x的IP,认为非法    {     return false;    }    else if(tmpStrIP.length() > 2 && nIPValue < 100)   //末IP段出现0xx的IP,认为非法    {     return false;    }    if( 0 > nIPValue || nIPValue > 255)    {     return false;                                 }   }  }  return true; }}int checkNetSegment(const char *ip1, const char *ip2, const char *mask){ if ((!checkIPValid(ip1)) || (!checkIPValid(ip2)) || (!checkIPValid(mask))) {  return 1; } else {  std::string strIP1 = ip1;  std::string strIP2 = ip2;  int arrayIP1[4] = {-1};  int arrayIP2[4] = {-1};  int arrayMask[4] = {-1};  int arrayAddIP1[4] = {-1};  int arrayAddIP2[4] = {-1};  for(unsigned int i = 0; i < VALID_COUNTOFDOT+1; ++i)    //取IP数字段  {   getSegmentIP(ip1,arrayIP1);   getSegmentIP(ip2,arrayIP2);   getSegmentIP(mask,arrayMask);   arrayAddIP1[i] = arrayIP1[i]&arrayMask[i];   arrayAddIP2[i] = arrayIP2[i]&arrayMask[i];   if (arrayAddIP1[i] != arrayAddIP2[i])    {    return 2;   }  }  return 0; }}

0 0
原创粉丝点击