LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1210|回复: 2

编写内核模块 的问题

[复制链接]
发表于 2006-3-2 12:51:10 | 显示全部楼层 |阅读模式
我想要给netfilter添加一个钩子,原理都弄明白了,但是试了一下linux magazine上 给的例子程序,编译错误太多了,主要是所包含的头文件 的错误.我的系统是REDHAT ES3
原文件:
  1. #include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <linux/netfilter_ipv4.h>
  4. #include <linux/ip.h>
  5.                                                                                                                                                
  6.                                                                                                                                                
  7. static unsigned int
  8. linuxmag_hook(unsigned int hook, struct sk_buff **pskb,
  9.                const struct net_device *indev, const
  10.                struct net_device *outdev, int
  11.                (*okfn)(struct sk_buff *))
  12. {
  13.                                                                                                                                                
  14.       /* Get a handle to the packet data */
  15.    unsigned char *data = (void *)(*pskb)->nh.iph +
  16.                          (*pskb)->nh.iph->ihl*4;
  17.                                                                                                                                                
  18.    (*pskb)->nfcache |= NFC_UNKNOWN;
  19.                                                                                                                                                
  20.    switch ((*pskb)->len) {
  21.    case 100:
  22.       printk("linuxmag: corrupting packet\n");
  23.       data[99]++;
  24.       (*pskb)->nfcache |= NFC_ALTERED;
  25.       return NF_ACCEPT;
  26.                                                                                                                                                
  27.    case 200:
  28.       printk("linuxmag: dropping packet\n");
  29.       return NF_DROP;
  30.                                                                                                                                                
  31.    default:
  32.       return NF_ACCEPT;
  33.    }
  34. }
  35.                                                                                                                                                
  36.                                                                                                                                                
  37. static struct nf_hook_ops linuxmag_ops
  38. = { { NULL, NULL }, linuxmag_hook,
  39.    PF_INET, NF_IP_LOCAL_OUT,
  40.    NF_IP_PRI_FILTER-1 };

  41. static int __init init(void)
  42. {
  43.   return nf_register_hook(&linuxmag_ops);
  44. }
  45.                                                                                                                                                
  46. static void __exit fini(void)
  47. {
  48.   nf_unregister_hook(&linuxmag_ops);
  49. }
  50.                                                                                                                                                
  51. module_init(init);
  52. module_exit(fini);

复制代码

编译错误结果:
[root@anuode root]# gcc -Wall -DMODULE -D__KERNEL__ -DLINUX -c linuxmag.c
In file included from linuxmag.c:3:
/usr/include/linux/netfilter_ipv4.h:53: `INT_MIN' undeclared here (not in a function)
/usr/include/linux/netfilter_ipv4.h:53: enumerator value for `NF_IP_PRI_FIRST' not integer constant
/usr/include/linux/netfilter_ipv4.h:59: `INT_MAX' undeclared here (not in a function)
/usr/include/linux/netfilter_ipv4.h:59: enumerator value for `NF_IP_PRI_LAST' not integer constant
linuxmag.c:11: warning: `struct net_device' declared inside parameter list
linuxmag.c:11: warning: its scope is only this definition or declaration, which is probably not what you want
linuxmag.c:11: warning: `struct sk_buff' declared inside parameter list
linuxmag.c: In function `linuxmag_hook':
linuxmag.c:15: dereferencing pointer to incomplete type
linuxmag.c:16: dereferencing pointer to incomplete type
linuxmag.c:18: dereferencing pointer to incomplete type
linuxmag.c:20: dereferencing pointer to incomplete type
linuxmag.c:22: warning: implicit declaration of function `printk'
linuxmag.c:24: dereferencing pointer to incomplete type
linuxmag.c: At top level:
linuxmag.c:38: variable `linuxmag_ops' has initializer but incomplete type
linuxmag.c:38: extra brace group at end of initializer
linuxmag.c:38: (near initialization for `linuxmag_ops')
linuxmag.c:38: `NULL' undeclared here (not in a function)
linuxmag.c:38: `NULL' undeclared here (not in a function)
linuxmag.c:38: warning: excess elements in struct initializer
linuxmag.c:38: warning: (near initialization for `linuxmag_ops')
linuxmag.c:38: warning: excess elements in struct initializer
linuxmag.c:38: warning: (near initialization for `linuxmag_ops')
linuxmag.c:39: `PF_INET' undeclared here (not in a function)
linuxmag.c:39: warning: excess elements in struct initializer
linuxmag.c:39: warning: (near initialization for `linuxmag_ops')
linuxmag.c:39: warning: excess elements in struct initializer
linuxmag.c:39: warning: (near initialization for `linuxmag_ops')
linuxmag.c:40: warning: excess elements in struct initializer
linuxmag.c:40: warning: (near initialization for `linuxmag_ops')
linuxmag.c:43: syntax error before "init"
linuxmag.c:44: warning: return type defaults to `int'
linuxmag.c: In function `init':
linuxmag.c:45: warning: implicit declaration of function `nf_register_hook'
linuxmag.c: At top level:
linuxmag.c:48: syntax error before "fini"
linuxmag.c:49: warning: return type defaults to `int'
linuxmag.c: In function `fini':
linuxmag.c:50: warning: implicit declaration of function `nf_unregister_hook'
linuxmag.c: At top level:
linuxmag.c:53: warning: type defaults to `int' in declaration of `module_init'
linuxmag.c:53: warning: parameter names (without types) in function declaration
linuxmag.c:53: warning: data definition has no type or storage class
linuxmag.c:54: warning: type defaults to `int' in declaration of `module_exit'
linuxmag.c:54: warning: parameter names (without types) in function declaration
linuxmag.c:54: warning: data definition has no type or storage class
linuxmag.c:38: storage size of `linuxmag_ops' isn't known
 楼主| 发表于 2006-3-2 13:16:37 | 显示全部楼层
发现问题所在 了
回复 支持 反对

使用道具 举报

发表于 2006-3-18 21:46:02 | 显示全部楼层
`INT_MIN' undeclared here (not in a function)
对未定义类型 ‘struct nf_hook_ops’ 的使用无效

我也遇到这样的问题.版主你怎么解决的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表