Determine if a string is a valid IPv4 address in C

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

    I needed to figure out if incoming string "contains" a valid IP address, and to return a pointer to the portion of the incoming string that is the valid IP address, if so. If not, returns a null pointer.

    Here is code that seems to work, although not well tested yet, I just wrote it and gave it a quick try. I didn't add a check yet for limiting the numbers to one-byte values, but do check to ensure that they are limited to three digit numbers.

    int IsDigit(char ch)
    {
       int is_digit = 0;
       if ( ch >= '0' && ch <= '9' )
       {
          is_digit = 1;
       }
       return is_digit;
    }
    
    #define FIND_IP_START         0
    #define FIND_IP_DIGIT         1
    #define FIND_IP_DIG_OR_DEC    2
    #define FIND_IP_DECIMAL       3
    #define FIND_IP_DIG_OR_END    4
    #define FIND_IP_END           5
    #define FIND_IP_DONE          6
    
    char * StringContainsValidIpAddress(char * input_buf_pointer)
    {
       char * pos       = input_buf_pointer;
       int    octets    = 0; 
       int    digits    = 0;
       int    state     = FIND_IP_START;
       char * ip_string = 0;
    
       char   ch        = *pos; 
    
       while ( (ch != NULL) && (state != FIND_IP_DONE) )
       {
          switch ( state )
          {
          case FIND_IP_START:
             if ( IsDigit(ch) )
             {
                ip_string = pos;  //potential start of ip string
                digits = 1;   // first digit
                octets = 1;   // of first octet
                state = FIND_IP_DIG_OR_DEC;
             }
             break;
          case FIND_IP_DIGIT:
             if ( IsDigit(ch) )
             {
                digits = 1;    // first digit
                octets++;      // of next octet
                if ( octets == 4 )
                {
                   state = FIND_IP_DIG_OR_END;
                }
                else
                {
                       state = FIND_IP_DIG_OR_DEC;
                }
             }
             else
             {
                // Start over
                state = FIND_IP_START;
             }
             break;
          case FIND_IP_DIG_OR_DEC:
             // Here we are looking for another digit 
             // of the same octet or the decimal between 
             // octets.
             if (ch == '.')
             {
                   state = FIND_IP_DIGIT;
             }
             else if ( IsDigit(ch) )
             {
                digits++;      // next digit
                if ( digits == 3 )
                {
                   state = FIND_IP_DECIMAL;
                }
             }
             else
             {
                // Start over
                state = FIND_IP_START;
             }
             break;
          case FIND_IP_DECIMAL:
             if (ch == '.')
             {
                   state = FIND_IP_DIGIT;
             }
             break;
          case FIND_IP_DIG_OR_END:
             // Here we are looking for another digit 
             // of the same octet or the end (which could
             // be a space or CR or LF or really any 
             // non-digit).
             if ( IsDigit(ch) )
             {
                digits++;      // next digit
                if ( digits == 3 )
                {
                   state = FIND_IP_END;
                }
             }
             else
             {  
                *pos = 0;  // Null terminate the IP address string
                state = FIND_IP_DONE;
             }
             break;
          case FIND_IP_END:
             if ( !IsDigit(ch) )
             {
                *pos = 0;  // Null terminate the IP address string
                state = FIND_IP_DONE;
             }
             break;
          case FIND_IP_DONE:
             break;
          default:
             break;
          }
    
          // Fetch the next character
          ch = *++pos; 
       } 
    
       if (state == FIND_IP_DONE) 
       {
          return ip_string; 
       }
       else
       {
          return 0;
       }
    }
    

提交回复
热议问题