#define MODULE #include #include #include #include #include #include #define MODULE_NAME "linTry" #define FOOBAR_LEN 128 #define MAX_UNSIGNED_SHORT 65535 extern float rate; /* drop percentage */ extern unsigned short cutoff; extern __u32 target; /* The above 3 variable are exported from * packet_dropper module */ static struct proc_dir_entry *example_dir, *foo_file; /* str is wrapped with '\0', the data is always valid, error check was done * in userspace * */ static void getIpRate(char * str, float * rate, unsigned *ip ) { int j,i,ipp=0; float decimal=0,dec=1; for( i=0; str[i] != '.'; i++); for ( i++; str[i] != ' '; i++ ) { dec = dec * 0.1; decimal += (str[ i ] - '0')*dec; }/* end of for loop */ for ( j=i+1; str[j] != '\0'; j++ ) { ipp = ipp*10 + str[j] - '0'; } *rate = decimal; *ip = ipp; }/* end of getipRate*/ static int proc_read_foobar(char *buf, char **start, off_t off, int count,int *eof, void *data) { int len,i; unsigned addr = target; /* target is exported from packet_dropper*/ float myRate = rate; /* rate is exported from packet_dropper*/ unsigned addr1; int digit; //MOD_INC_USE_COUNT; len = sprintf( buf, "Dropping Rate shows only the first 6 digits after '.'\n"); len += sprintf( buf+len, "Dropping Rate is: 0."); for( i=0; i<6; i++) { digit = (int) (myRate*10); myRate = myRate*10 - digit; len += sprintf( buf+len, "%1d", digit ); } len += sprintf( buf+len, "\n"); /* in kernel there is not floating point number * the following is not working */ //len = sprintf( buf, "Dropping Rate is: %f\n", rate ); addr1=addr%256; len += sprintf( buf+len, "Destination IP is: %d.", addr1); addr>>=8; addr1= addr%256; len += sprintf( buf+len, "%d.", addr1); addr>>=8; addr1= addr%256; len += sprintf( buf+len, "%d.", addr1); addr >>= 8; addr1= addr%256; len += sprintf( buf+len, "%d\n", addr1); //MOD_DEC_USE_COUNT; return len; } static int proc_write_foobar(struct file *file,const char *buffer, unsigned long count,void *data) { int len; char myData[ 256 ]; unsigned myIp; float myRate; //MOD_INC_USE_COUNT; if(count > FOOBAR_LEN) len = FOOBAR_LEN; else len = count; if(copy_from_user(myData, buffer, len)) { //MOD_DEC_USE_COUNT; return -EFAULT; } myData[len] = '\0'; printk("<1>my data is: %s\n", myData ); getIpRate( myData, &myRate, &myIp ); //MOD_DEC_USE_COUNT; rate = myRate; target = myIp; cutoff = (unsigned short) (rate*(1-rate)*MAX_UNSIGNED_SHORT); return len; } int init_module() { int rv = 0; EXPORT_NO_SYMBOLS; /* create directory */ example_dir = proc_mkdir(MODULE_NAME, NULL); if(example_dir == NULL) { printk("<1> example_dir failed\n"); rv = -ENOMEM; goto out; } example_dir->owner = THIS_MODULE; /* create foo and bar files using same callback * functions */ foo_file = create_proc_entry("foo", 0644, example_dir); if(foo_file == NULL) { rv = -ENOMEM; goto no_foo; } //foo_file->data = &foo_data; foo_file->read_proc = proc_read_foobar; foo_file->write_proc = proc_write_foobar; foo_file->owner = THIS_MODULE; /* everything OK */ printk(KERN_INFO "%s initialised\n",MODULE_NAME ); return 0; no_foo: remove_proc_entry(MODULE_NAME, NULL); out: return rv; } void cleanup_module() { remove_proc_entry("foo", example_dir); remove_proc_entry(MODULE_NAME, NULL); printk(KERN_INFO "%s removed\n",MODULE_NAME); }