I am applying one formula over ip address. but it gives me some random output sometimes, which is weired. sometimes it does not tokenize properly, and sometimes it gives 0 value. Ratio of error regenaration is almost 2-5 in a 1000 sample.
calling function = formula(IPADDRESS);
which I am calling with hundreds of different IPs per second.
int formula ( char ip[]){
char *token = NULL;
unsigned long value;
int finalv;
char ipaddress[16];
char delims[]=".";
int octet=3;
value = 0;
FILE *fp;
fp = fopen(LOGFILE, "a");
strcpy(ipaddress,ip);
fprintf(fp, "%s\n",ipaddress);
token = strtok( ipaddress,delims);
while( token != NULL ) {
fprintf(fp,"%d",(atoi(token));
if ( atoi(token) != 0 ){
if(octet ==3)
value = value + (255 * 255 * 255 * (atoi(token)));
else if(octet ==2)
value = value + (255 * 255 * (atoi(token)));
else if(octet ==1)
value = value + (255 * (atoi(token)));
else if(octet ==0)
value = value + (atoi(token));
}
octet--;
token = strtok( NULL,delims);
}
finalv = value % 9999;
fprintf(fp, " -- %d \n",(int)finalv);
if(fp)
fclose(fp);
return (int)v;
}
OUTPUTFILE : (I have Given only false outputs)
172.17.82.255
172 0 1 -- 1983
--
172.254.254.254
172 0 0 0 -- 1728
--
172.255.255.225
172 0 -- 1728
--
172.255.255.255
172 21 0 1 -- 7390
--
172.255.84.255
172 0 8 -- 3768
Instead of looping over strtok()
, the
int a=0, b=0, c=0, d=0 ;
int value ;
sscanf( ip, "%d.%d.%d.%d", &a, &b, &c, &d );
value = ( ( a * 255 + b ) * 255 + c ) * 255 + d ;
finalv = value % 9999
should, I think, give you the same answers. (Although as pointed out in the comments, the 255
s may be meant to be 256
).