⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readconfiguration.c

📁 Linux下kernel与userspace的通信实现。主要功能为可修改linux的防火墙设置进行通信控制。Linux下调试通过。
💻 C
字号:
/*  Reading data */#include <linux/module.h>  /* Needed by all modules */#include <linux/kernel.h>  /* Needed for KERN_ALERT */#include <linux/proc_fs.h> #include <asm/uaccess.h>#include "../include/firewall.h"MODULE_AUTHOR ("Eike Ritter <E.Ritter@cs.bham.ac.uk>");MODULE_DESCRIPTION ("Reading data") ;MODULE_LICENSE("GPL");struct proc_dir_entry *procFirewall; /* the handler for the proc-file *//* reads the data from the buffer into the kernel */struct ConfigKernelEntry *parseBuffer (char *buffer, int count, int *errno) {  int progLength;    struct ConfigKernelEntry *entry;  printk (KERN_INFO "tableRead parseBuffer: count = %d \n", count);  *errno = 0;  if (count < 2 * sizeof (int)) {    *errno = -ENOMEM;    return NULL;  }    entry = kmalloc (sizeof (*entry), GFP_KERNEL);  if (!entry) {    *errno = -ENOMEM;    return NULL;  }    /* reads port number */  memcpy (&(entry->port), buffer, sizeof (int));  /* reads lengths of the program string */  memcpy (&progLength, buffer + sizeof (int), sizeof (int));  /* allocate space in kernel memory for program name */  entry->program = kmalloc (progLength, GFP_KERNEL);  if (!entry->program) {    kfree (entry);    *errno = -ENOMEM;    return NULL;  }  if (count < 2 *sizeof (int) + progLength + sizeof (IPAddressByteType)) {    *errno = -ENOMEM;    kfree (entry->program);    kfree (entry);    return NULL;  }  /* read program name */  memcpy (entry->program, buffer + 2*sizeof (int), progLength);  /* read IP-address */  memcpy (&(entry->IPAddress), buffer + (2 *sizeof (int) + progLength), sizeof (IPAddressByteType));    return entry;}/* This function reads in data from the user into the kernel */int fwTableRead (struct file *file, const char *userBuffer, unsigned long count, void *data) {   char *buffer;  int errno = 0;  struct ConfigKernelEntry *entry;    /* allocate temporary kernel buffer */  buffer = kmalloc (count, GFP_KERNEL);  if (!buffer) {    return -ENOMEM;  }  /* copy data from user space */  if (copy_from_user (buffer, userBuffer, count)) {     printk (KERN_INFO "tableRead copying data from user space failed \n");    kfree (buffer);    return -EFAULT;  }  entry = parseBuffer (buffer, count, &errno);  if (!entry) {    kfree (buffer);    return errno;  }  kfree (buffer); /* temporary buffer no longer needed */  /* print the results */  printk (KERN_INFO "program = %s\n", entry->program);  printk (KERN_INFO "port = %d\n", entry->port);  printk (KERN_INFO "tableRead Destination address = %u.%u.%u.%u\n", NIPQUAD(entry->IPAddress));  return count;}int init_module(void){   printk(KERN_INFO "tableRead module loaded.\n");	   /* create the proc-file */   procFirewall = create_proc_entry ("net/firewall", S_IWUSR | S_IRUGO, NULL);   if (!procFirewall) {     return -ENOMEM;   }   procFirewall->owner = THIS_MODULE; /* required for using proc-structure within module */   /* specify the procedure which reads data */   procFirewall->write_proc = fwTableRead;   return 0;}  void cleanup_module(void){  remove_proc_entry ("net/firewall", NULL); /* do this first to prevent any concurrency problems */  printk(KERN_INFO "tableRead module unloaded.\n");  }  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -