Created
May 27, 2014 03:30
-
-
Save jsfaint/0d0176f4ad738138bd33 to your computer and use it in GitHub Desktop.
Parse IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef unsigned char u_char; | |
typedef unsigned char uchar; | |
typedef unsigned long u_long; | |
typedef unsigned long ip_addr; | |
typedef unsigned short ushort; | |
char * parse_ipad(ip_addr * ipout, /* pointer to IP address to set */ | |
unsigned * sbits, /* default subnet bit number */ | |
char * stringin) /* buffer with ascii to parse */ | |
{ | |
char * cp; | |
int dots = 0; /* periods imbedded in input string */ | |
int number; | |
union { | |
u_char c[4]; | |
u_long l; | |
} retval; | |
char * toobig = "each number must be less than 255"; | |
cp = stringin; | |
while (*cp) { | |
if (*cp > '9' || *cp < '.' || *cp == '/') | |
return("all chars must be digits (0-9) or dots (.)"); | |
if (*cp == '.')dots++; | |
cp++; | |
} | |
//if( dots < 1 || dots > 3 ) | |
if ( dots != 3 ) | |
return("string must contain 1 - 3 dots (.)"); | |
cp = stringin; | |
if ((number = atoi(cp)) > 255) /* set net number */ | |
return(toobig); | |
retval.c[0] = (u_char)number; | |
while (*cp != '.')cp++; /* find dot (end of number) */ | |
cp++; /* point past dot */ | |
if (dots == 1 || dots == 2) retval.c[1] = 0; | |
else { | |
number = atoi(cp); | |
while (*cp != '.')cp++; /* find dot (end of number) */ | |
cp++; /* point past dot */ | |
if (number > 255) return(toobig); | |
retval.c[1] = (u_char)number; | |
} | |
if (dots == 1) retval.c[2] = 0; | |
else { | |
number = atoi(cp); | |
while (*cp != '.')cp++; /* find dot (end of number) */ | |
cp++; /* point past dot */ | |
if (number > 255) return(toobig); | |
retval.c[2] = (u_char)number; | |
} | |
if ((number = atoi(cp)) > 255) | |
return(toobig); | |
retval.c[3] = (u_char)number; | |
if (retval.c[0] < 128) *sbits = 8; | |
else if (retval.c[0] < 192) *sbits = 16; | |
else *sbits = 24; | |
*ipout = retval.l; /* everything went OK, return number */ | |
return(NULL); /* return OK code (no error string) */ | |
} | |
int parse_ip_address(uchar* ip_str, uchar * ip_addr) | |
{ | |
uchar j; | |
uchar ch; | |
ushort val; | |
uchar number_flag; | |
/* skip spaces */ | |
while (*ip_str == ' ') { | |
ip_str++; | |
} | |
for (j = 0; j < 4; j++) { | |
number_flag = FALSE; | |
val = 0; | |
while (TRUE) { | |
ch = *ip_str; | |
if (!(ch >= '0' && ch <= '9')) { | |
break; | |
} | |
number_flag = TRUE; | |
ip_str++; | |
val = (val * 10) + (ch - '0'); | |
if (val > 255) { | |
return FALSE; | |
} | |
} | |
ip_addr[j] = val; | |
if (j < 3) { | |
ch = *ip_str++; | |
if (ch != '.') { | |
return FALSE; | |
} | |
} | |
} | |
if ((j != 4) || !number_flag) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
int main(int argc, char** argv) | |
{ | |
union { | |
u_char c[4]; | |
u_long l; | |
} tmp; | |
char szIp[] = "192.168.16.255"; | |
ip_addr ip; | |
unsigned sbits = 0 ; | |
parse_ipad(&ip, &sbits, szIp); | |
printf("%08X\n", ip); | |
memcpy(&tmp, &ip, 4); | |
char szbuf[64]; | |
memset(szbuf, 0, sizeof(szbuf)); | |
sprintf(szbuf, "%d.%d.%d.%d", tmp.c[0], tmp.c[1], tmp.c[2], tmp.c[3]); | |
printf("%s\n", szbuf); | |
ip = 0; | |
parse_ip_address(szIp, (char*)&ip); | |
memcpy(&tmp, &ip, 4); | |
char szbuf[64]; | |
memset(szbuf, 0, sizeof(szbuf)); | |
sprintf(szbuf, "%d.%d.%d.%d", tmp.c[0], tmp.c[1], tmp.c[2], tmp.c[3]); | |
printf("%s\n", szbuf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment