Determine if a string is a valid IPv4 address in C

后端 未结 15 2057
感情败类
感情败类 2020-12-24 08:22

What would be a good way to determine if a string contains an IPv4 address? Should I use isdigit()?

15条回答
  •  盖世英雄少女心
    2020-12-24 08:53

    Do it from scratch like this. This code contains tools to check if string contains IPv4 IP address.

    #define MAX_HEX_NUMBER_COUNT 8 
    
    int ishexdigit(char ch) 
    {
       if((ch>='0'&&ch<='9')||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F'))
          return(1);
       return(0);
    }
    
    int IsIp6str(char *str)
    { 
       int hdcount=0;
       int hncount=0;
       int err=0;
       int packed=0;
    
       if(*str==':')
       {
          str++;    
          if(*str!=':')
             return(0);
          else
          {
             packed=1;
             hncount=1;
             str++;
    
             if(*str==0)
                return(1);
          }
       }
    
       if(ishexdigit(*str)==0)
       {
          return(0);        
       }
    
       hdcount=1;
       hncount=1;
       str++;
    
       while(err==0&&*str!=0)   
       {                      
          if(*str==':')
          {
             str++;
             if(*str==':')
             {
               if(packed==1)
                  err=1;
               else
               {
                  str++;
    
              if(ishexdigit(*str)||*str==0&&hncount='0'&&*str<='9')
       {
          value=*str-'0';
          str++;
       } else
          return(0);
    
       nnumber=1;
    
       while(err==0&&*str!=0)
       {
          if(*str>='0'&&*str<='9')
          {
             if(255/value>=10)
             {
                value*=10;
    
                if(255-value>=(*str-'0'))
                {
                   value+=(*str-'0');
                   str++;
                } else
                    err=1;
             } else
               err=1;
          }  else
          {
             if(*str=='.')
             {      
                str++;
                if(*str>='0'&&*str<='9')
                {
                   if(nnumber==4)
                      err=1;
                   else
                   {
                      if(*str=='0')
                      {
                         *str++;
                         if(*str!='.'&&*str!=0)
                            err=1;
                         else
                         {
                            nnumber++;
                            value=0;
                         }
                      } else
                      {
                         nnumber++;
                         value=*str-'0';
                         str++;
                      }
                   }
                } else
                {
                   err=1;
                }
             } else
               if(*str!=0)
                 err=1;
          }
       }
    
       if(nnumber!=4)
          err=1;
    
       return(err==0);
    }
    

    Function IsIp4str(char *str) test if string contains IP address version four address format. IsIp6str(char *str) function test if string contains IP address version six address format.

    Functions IsIp4str(char *str) and IsIp6str(char *str) returns true if string str contains IP address or false if string str not contains IP address.

    If you need check if string contains IP address IPv6 format, it can done int IsIp6str(char *str) function. It works same way than

提交回复
热议问题