#include/* This is a routine designed to parse the ipv4 dot notation. Given a string with a dot notation value between 0.0.0.0 and 255.255.255.255 make sure this is a valid string of that format. */ static char ** string; char * str; int parseNum(){ int value = 0; if (*string && isdigit(*string[0])){ value = value*10+*string[0]-'0'; str = *string; str++; string = &str; if (*string && isdigit(*string[0])){ value = value*10+*string[0]-'0'; str = *string; str++; string = &str; if (*string && isdigit(*string[0])){ value = value*10+*string[0]-'0'; str = *string; str++; string = &str; } } } else { return 0; } if (value < 256) return 1; else return 0; } int parseDot(){ if (*string && *string[0] == '.') { str = *string; str++; string = &str; return 1; } else return 0; } int endOfString(){ if (*string && *string[0] == 0) return 1; else return 0; } int parseDotNotationV4 (char * stringtoparse){ string = &stringtoparse; if (parseNum()){ if (parseDot()){ if (parseNum()){ if (parseDot()){ if (parseNum()){ if (parseDot()){ if (parseNum()){ if (endOfString()){ // Good dot notation value return 1; } } } } } } } } // failure return 0; } printfParseDotNotationV4(char * string){ int returnVal = parseDotNotationV4(string); printf("%s %d\n", string, returnVal); return returnVal; } int main (){ int i,j,k,l; char string[1024]; printfParseDotNotationV4("255255.255.255"); printfParseDotNotationV4("2"); printfParseDotNotationV4("255.255.255.255.6"); printfParseDotNotationV4("255.255.256.255"); for(i=0; i<256; i=i+4){ for(j=0; j<256; j=j+4){ for(k=0; k<256; k=k+4){ for(l=0; l<256; l=l+4){ sprintf(string, "%d.%d.%d.%d", i, j, k, l); if (!parseDotNotationV4(string)) printf("%d failed when it should have been valid.", string); } } } } return 0; }
Sunday, April 29, 2012
Simple ipv4 dot notation parser I wrote once.
ip.c an ipv4 dot notation parser.
Subscribe to:
Post Comments (Atom)
Progress on the campaign manager
You can see that you can build tactical maps automatically from the world map data. You can place roads, streams, buildings. The framework ...
-
The tent smelled of ozone, sawdust, and cheap miracles. On a rickety stage, a man named Constantine adjusted the frayed cuffs of his tuxedo...
-
So, I saw this kit on Amazon. It seemed too good too be true. The kit was priced right, it was less than 2 dollars for each board. Of co...
-
My plan was to get the most use possible from the $5 raspberry Pi Zero that I managed to get my hands onto by buying a full kit that include...
No comments:
Post a Comment