自己写的一个简单的IP组类

类别:编程语言 点击:0 评论:0 推荐:

#ifndef SUNXYIPGROUP_H
#define SUNXYIPGROUP_H
#include <string>
#include <ctype.h>
#define MAXLONGIP (255*255*255*255+255*255*255+255*255+255)
using namespace std;
class SunxyIPGroup
{
 public:
  //构造函数,提供起始IP和终止IP
  SunxyIPGroup(const char* startip,const char* endip);
         ~SunxyIPGroup();
  //取得当前IP的下一个IP
         string GetNextIP(const char* curip,int offset);
  //取得起始IP
         string GetFirstIP();
  //取得终止IP
         string GetLastIP();
  //取得当前IP
  string GetCurIP();
  //相对IP总个数
         unsigned long TotalIPNum();
  //相对当前IP
         unsigned long CurIPNum();
  //取得起始IP长字
  unsigned long GetLastIPLong();
  //取得终止IP长字
  unsigned long GetFirstIPLong();
  //取得当前IP长字
  unsigned long GetCurIPLong();
  //初始化成功标志
         bool InitOK;
 private:
  //起始IP长字及其分解部分
         unsigned long startiplong;
  unsigned long startlip[4];
  string startsip[4];  
  //终止IP长字及其分解部分
         unsigned long endiplong;
  unsigned long endlip[4];
  string endsip[4];    
  //当前IP长字及其分解部分
  unsigned long curiplong;
  unsigned long curlip[4];
  string cursip[4];
  //临时分解IP缓存
  unsigned long tempiplong;
  string tempsip[4];
         unsigned long templip[4];
  //相对IP个数
  unsigned long totalipnum;  
         unsigned long curipnum;    
         //检查以字符串数组为形式的IP地址有效性,并拆分到指定的缓存内,返回值为是否拆分成功
         bool DivideIP(const char* inputip,unsigned long* inputlip,string* inputsip);
  //将指定缓存内的IP分解部分组合起来,返回组合好的以字符口串为形式的IP地址
         string AssembleIP(unsigned long inputip,unsigned long* inputlip,string* inputsip);
  //除去IP中为广播地址或网段地址等无效地址
         unsigned long ValidLongIP(unsigned long inputip);
};

//公共接口函数定义
SunxyIPGroup::SunxyIPGroup(const char* startip,const char* endip="")
{
    string stemp1(startip);
    string stemp2(endip);
 int i;
    //成员变量初始化
    InitOK=false;

 startiplong=0;
 endiplong=0;
 curiplong=0;

 totalipnum=0;
    curipnum=0;
 tempiplong=0;

    for(i=0;i<4;i++)
    {
  startlip[i]=0;
  startsip[i]="";

  endlip[i]=0;
  endsip[i]="";

  curlip[i]=0;
  cursip[i]="";

        tempsip[i]="";
        templip[i]=0;
    }
    //设置成员变量并构造IP组
    if(DivideIP(stemp1.c_str(),startlip,startsip))
    {
        startiplong=255*255*255*startlip[0]
                   +255*255*startlip[1]
                   +255*startlip[2]
                   +startlip[3];
  if(stemp2.empty ())
  {
   for(i=0;i<3;i++)
   {
    endlip[i]=startlip[i];
    endsip[i]=startsip[i];
   }
   endlip[i]=254;
   endsip[i]="254";
   endiplong=255*255*255*endlip[0]
      +255*255*endlip[1]
      +255*endlip[2]
      +endlip[3];
  }
        else if(DivideIP(stemp2.c_str(),endlip,endsip))
        {
            endiplong=255*255*255*endlip[0]
                     +255*255*endlip[1]
                     +255*endlip[2]
                     +endlip[3];

  }
        endiplong=ValidLongIP(endiplong);
        startiplong=ValidLongIP(startiplong);
        totalipnum=endiplong-startiplong+1;

        if(totalipnum>0)
        {
   //初始化成功
   curipnum=1;
   curiplong=startiplong;
   for(i=0;i<4;i++)
   {
    curlip[i]=startlip[i];
    cursip[i]=startsip[i];
   }
            InitOK=true;           
  }
        else
  {               
   //IP组可用个数为0,重新初始化
   startiplong=0;
   endiplong=0;
   curiplong=0;
   tempiplong=0;

   totalipnum=0;
   curipnum=0;

   for(i=0;i<4;i++)
   {
    startlip[i]=0;
    startsip[i]="";

    endlip[i]=0;
    endsip[i]="";

    curlip[i]=0;
    cursip[i]="";

    tempsip[i]="";
    templip[i]=0;
   }    
   InitOK=false;           
  }
    }
}

SunxyIPGroup::~SunxyIPGroup()
{
}

string SunxyIPGroup::GetNextIP(const char* curip="",int offset=1)
{
    string stemp(curip);
    if(InitOK)
    {
        //add codes here
  if(offset>=0)
  {
   if(stemp.empty ())
   {
    tempiplong=curiplong;
    curiplong+=offset;
    curiplong=ValidLongIP(curiplong);
    if(tempiplong==curiplong)
     return "";
    curipnum+=curiplong-tempiplong;    
    stemp=AssembleIP(curiplong,curlip,cursip);
   }
   else
   {   
    if(DivideIP(stemp.c_str (),curlip,cursip))
    {
     curiplong=255*255*255*curlip[0]
        +255*255*curlip[1]
        +255*curlip[2]
        +curlip[3];
     tempiplong=curiplong;
     curiplong+=offset;
     curiplong=ValidLongIP(curiplong);
     if(tempiplong==curiplong)
      return "";
     curipnum+=curiplong-tempiplong;
     stemp=AssembleIP(curiplong,curlip,cursip);
    }
   }
  }
    }
    return stemp;
}

string SunxyIPGroup::GetFirstIP()
{
    string stemp;
    stemp="";
    if(InitOK)
    {
        //add codes here
  stemp=AssembleIP(startiplong,templip,tempsip);
    }
    return stemp;
}
string SunxyIPGroup::GetLastIP()
{
    string stemp;
    stemp="";
    if(InitOK)
    {
        //add codes here
  stemp=AssembleIP(endiplong,templip,tempsip);
    }
    return stemp;
}
unsigned long SunxyIPGroup::TotalIPNum()
{
    if(InitOK)
        return totalipnum;
    return 0;
}
unsigned long SunxyIPGroup::CurIPNum()
{
    if(InitOK)
        return curipnum;
    return 0;
}

unsigned long SunxyIPGroup::GetFirstIPLong()
{
 if(InitOK)
  return startiplong;
 return 0;
}

unsigned long SunxyIPGroup::GetLastIPLong ()
{
 if(InitOK)
  return endiplong;
 return 0;
}

string SunxyIPGroup::GetCurIP ()
{
 if(InitOK)
  return AssembleIP(curiplong,curlip,cursip);
 return "";
}

unsigned long SunxyIPGroup::GetCurIPLong()
{
 if(InitOK)
  return curiplong;
 return 0;
}

//私有函数定义
bool SunxyIPGroup::DivideIP(const char* inputip,unsigned long* inputlip,string* inputsip)
{
   
    string stemp(inputip);
    bool result=false;
    int index;
    int count=0;
    long ipvalue=-1;
    if(!stemp.empty())
    {
        index=stemp.find(".");
        while(count<=3&&index>0)
        {   inputsip[count]=stemp.substr(0,index);
            ipvalue=atol(inputsip[count].c_str());
   //templip[i]=strtoul(tempsip[i].c_str());此函数用法还未查清
            if(index<stemp.length()-1&&ipvalue>=0&&ipvalue<255)
            {
                inputlip[count]=ipvalue;
                count++;
            }
            else
            {
                return result;
            }
            stemp=stemp.substr(index+1,stemp.length()-1-index);
            index=stemp.find(".");
        }
        ipvalue=atol(stemp.c_str());
  //templip[i]=strtoul(tempsip[i].c_str());此函数用法还未查清
        if(ipvalue>=0&&ipvalue<255)
        {
            inputsip[count]=stemp;
            inputlip[count]=ipvalue;
            count++;
        }
        if(count==4)
            result=true;
    }
    return result;
}

string SunxyIPGroup::AssembleIP(unsigned long inputip,unsigned long* inputlip,string* inputsip)
{
    string stemp;
    stemp="";
 int i;
    if(InitOK)
    {
  inputip=ValidLongIP(inputip);
  inputlip[0]=inputip/(255*255*255);
  inputip%=255*255*255;
  inputlip[1]=inputip/(255*255);
  inputip%=255*255;
  inputlip[2]=inputip/255;
  inputip%=255;
  inputlip[3]=inputip;
  for(i=0;i<4;i++)
  {
   char ctemp[8];
   inputsip[i]=string(ultoa(inputlip[i],ctemp,10));
   stemp=stemp+inputsip[i]+".";
  }
  stemp=stemp.substr (0,stemp.length ()-1);
  
    }
    return stemp;
}

unsigned long SunxyIPGroup::ValidLongIP(unsigned long inputip)
{
     if(inputip>endiplong)
   inputip=endiplong;
  if(inputip<startiplong)
   inputip=startiplong;
  if(inputip%255==0)
         inputip++;
  return inputip;
}
#endif

本文地址:http://com.8s8s.com/it/it21898.htm