/* GetLongIP() by Codex` (Hero_Number_Zero) - hero_number_zero@unixbeast.com * * Description: Works out integer (long) IP from string IP, also checks * to see if the string is a valid IP, returns 0 if it is in-valid and * returns the integer IP if it is valid. * * Note: If the string "0.0.0.0" is passed to the function, the long * IP for that is the same as the error return. There is little * we can do about that using a 4 bit integer, it's lucky nobody * has that IP. * * Note: I find * 256 to be marginaly faster than << 8, I can't speak for * all compilers/OS' though just the ones I tested on, it's only about * 1-2% faster mind you. */ #define IsDigit(c) (c >= '0' && c <= '9' ? 1 : 0) unsigned int GetLongIP(const char *s) { unsigned int ip = 0, n = 0; while (IsDigit(*s)) n = (n * 10) + (*s++ - '0'); if (n > 0xff || *s++ != '.' || !IsDigit(*s)) return 0; ip = (ip + n) * 256; n = 0; while (IsDigit(*s)) n = (n * 10) + (*s++ - '0'); if (n > 0xff || *s++ != '.' || !IsDigit(*s)) return 0; ip = (ip + n) * 256; n = 0; while (IsDigit(*s)) n = (n * 10) + (*s++ - '0'); if (n > 0xff || *s++ != '.' || !IsDigit(*s)) return 0; ip = (ip + n) * 256; n = 0; while (IsDigit(*s)) n = (n * 10) + (*s++ - '0'); if (n > 0xff || *s != '\0') return 0; return (ip + n);
}