Determine if a string is a valid IPv4 address in C

后端 未结 15 1975
感情败类
感情败类 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

    This is my try with a very low level C programming ( actually used in one of my programs for a PIC microcontroller). It does not use of string.h library. It does not use pointers, as this compiler I am using does not work well with them, anyway you could use them. Taking this into account and previosly defining a variable to handle the incoming data buffer like this:

    #define isdigit(x)  isamong(x,"0123456789")
    char    IPACK_Buff[IPACK_SIZE]; 
    
    // Check if string is a valid IP
    int IPACK_is_valid_ip(int len)
    {
        int i = 0;
        int j = 0;
        int NumDots = 0;
        char number[4] = "000\0";
    
        // Check first  char is numeric
        if (!isdigit(IPACK_Buff[0])) 
            return 0;
    
        for (i = 0 ; i< len; i++)
        {
            if (isdigit(IPACK_Buff[i]))
            {
                number[j] = IPACK_Buff[i];
                j++;
                if (j>3)    
                    return 0;
            }
            else if (IPACK_Buff[i] == '.')
            {
                if (atof(number)> 255) return 0;
                memset(number, '\0', 4);
    
                j = 0;
                NumDots++;
                if(NumDots>3)
                    return 0;
            }
        }
    
        if (NumDots == 3)
        {
            return 1;
        }
        else 
            return 0;
    }//
    

    I hope this function helps you all. Again, take into account the low level this function is programmed before criticize.

    0 讨论(0)
  • 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<MAX_HEX_NUMBER_COUNT)
              {
                 packed=1;
                 hncount++;
    
                 if(ishexdigit(*str))
                 {
                    if(hncount==MAX_HEX_NUMBER_COUNT)
                    {
                       err=1;
                    } else
                    {
                       hdcount=1;
                       hncount++;
                       str++;   
                    }
                 }
              } else
              {
                 err=1;
              }
           }
        } else
        {
               if(!ishexdigit(*str))
               {
                  err=1;
               } else
               {
                  if(hncount==MAX_HEX_NUMBER_COUNT)
                  {
                     err=1;
                  } else
                  {
                      hdcount=1;
                      hncount++;
                      str++;   
                  }
               }
            }
         } else
         {  
            if(ishexdigit(*str))
            {
               if(hdcount==4)
                  err=1;
               else
               {
                  hdcount++;          
                  str++;
               }
             } else
                err=1;
         } 
       }
    
       if(hncount<MAX_HEX_NUMBER_COUNT&&packed==0)
          err=1;
    
        return(err==0);
    }
    
    int IsIp4str(char *str) 
    {
       int nnumber=0;
       int value=0;
       int err=0;
    
       if(*str>='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

    0 讨论(0)
  • 2020-12-24 08:53

    Try this code :

    int ipValid(char *ip) {
    int i,j,start,dotcount=0,x=0,end,c=0;
    int n= strlen(ip);
    start=0;
    for(i=0;i<n;i++)
    {
        if(ip[i]=='.'||i==n-1)
        {
            c=0;x=0;
            if(ip[i]=='.')
              {
                dotcount++;
                end=i-1;
               } 
            else if(i==n-1)
               end=i;
            for(j=start;j<=end;j++)
            {
                c++;
                x=x*10+(ip[j]-48);
    
            }
            if(c<4&&x>=0&&x<=255)
            {
                if(i==n-1)
                    break;
                else
                    start=i+1;
            }
            else
            {
                return 0;
            }
        }
    }
    if(dotcount==3)
       return 1;
    else
       return 0;}
    
    0 讨论(0)
提交回复
热议问题