/* this program runs in user space by root*/ #include #include #include #include int buildIp( char * ipStr, unsigned * IP ) { unsigned int r = 0; unsigned fact[] = { 1, 256, 65536,16777216 }; int i=0, dotPos = -1; int justPoint=0; /* used to check error of ".." */ int numDot=0; /* used to check error of 1.2.2.3.3 */ unsigned int a=0; while (ipStr[i] != '\0') { if ( ipStr[i] == '.' ) { if (justPoint) return -1; justPoint=1; if ( numDot >=3 ) return -1; numDot++; dotPos++; r += a*fact[ dotPos ]; a = 0; i++; continue; } justPoint = 0; if (( ipStr[i] > '9' ) || (ipStr[i]<'0') ) return -1; a = a*10 + ipStr[i] - '0'; if ( a > 255 ) return -1; i++; }/* end of while */ if ( numDot != 3 ) return -1; dotPos++; r += a*fact[ dotPos ]; *IP = r; return 0; }/* end of buildIP*/ int main(int argc, char *argv[] ) { //unsigned int rate; float rate; unsigned int IP; FILE* procEntry=NULL; if ( argc != 3 ) { printf("usage: %s DropRate DestIp\n", argv[0] ); return -1; } rate = (float) atof( argv[1] ); if ( (rate > 1) || (rate<0) ) { printf("Rate should be in the range [0.0, 1.0]\n" ); return -1; } if ( buildIp( argv[2], &IP ) ) { printf("bad format of IP address\n" ); return -1; } procEntry = fopen( "/proc/linTry/foo", "w" ); if ( procEntry == NULL ) { printf("Error opening: /proc/linTry/foo for writing\n" ); exit( 1 ); } fprintf( procEntry, "%f %u", rate, IP ); fclose( procEntry ); return 0; }/* end of main */