目前我正在研究一个研究项目,我们正在尝试运行无线网络协议,即由uppasala大学开发的aodvuu。 protocal是在Linux内核版本2.4 x中开发的,现在我们正在尝试在内核版本3.8上运行它。以下是面临问题的相应内核代码(kaodv-netlink.c)。
#include <linux/module.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#define NETLINK_USER 31
struct sock *nl_sk = NULL;
static void hello_nl_recv_msg(struct sk_buff *skb) {
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
char *msg="Hello from kernel";
int res;
printk(KERN_INFO "Entering: %s\n", __FUNCTION__);
msg_size=strlen(msg);
nlh=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */
skb_out = nlmsg_new(msg_size,0);
if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
strncpy(nlmsg_data(nlh),msg,msg_size);
res=nlmsg_unicast(nl_sk,skb_out,pid);
if(res<0)
printk(KERN_INFO "Error while sending bak to user\n");
}
static int __init hello_init(void) {
printk("Entering: %s\n",__FUNCTION__);
/* This is for 3.6 kernels and above.
struct netlink_kernel_cfg cfg = {
.input = hello_nl_recv_msg,
};
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);*/
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, 0, hello_nl_recv_msg,NULL,THIS_MODULE);
if(!nl_sk)
{
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "exiting hello module\n");
netlink_kernel_release(nl_sk);
}
module_init(hello_init); module_exit(hello_exit);
MODULE_LICENSE("GPL");
我得到的错误是
timer_queue.o aodv_socket.o aodv_hello.o aodv_neighbor.o aodv_timeout.o routing_table.o seek_list.o aodv_rreq.o aodv_rrep.o aodv_rerr.o nl.o locality.o
make -C /home/lp3/aodvuu096/lnx KERNEL_DIR=/lib/modules/3.8.0-31-generic/build KCC=gcc XDEFS=-DDEBUG
make[1]: Entering directory `/home/lp3/aodvuu096/lnx'
make -C /lib/modules/3.8.0-31-generic/build M=/home/lp3/aodvuu096/lnx modules
make[2]: Entering directory `/usr/src/linux-headers-3.8.0-31-generic'
CC [M] /home/lp3/aodvuu096/lnx/kaodv-mod.o
CC [M] /home/lp3/aodvuu096/lnx/kaodv-debug.o
CC [M] /home/lp3/aodvuu096/lnx/kaodv-netlink.o
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_init’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:372:21: warning: passing argument 3 of ‘netlink_kernel_create’ from incompatible pointer type [enabled by default]
include/linux/netlink.h:48:1: note: expected ‘struct netlink_kernel_cfg *’ but argument is of type ‘void (*)(struct sk_buff *)’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:372:21: error: too many arguments to function ‘netlink_kernel_create’
include/linux/netlink.h:48:1: note: declared here
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:374:13: error: invalid storage class for function ‘kaodv_netlink_rcv_sk’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_rcv_sk’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:378:14: error: ‘recv_cmd’ undeclared (first use in this function)
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:378:14: note: each undeclared identifier is reported only once for each function it appears in
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:382:1: error: incompatible type for argument 3 of ‘netlink_kernel_create’
include/linux/netlink.h:48:1: note: expected ‘struct netlink_kernel_cfg *’ but argument is of type ‘struct netlink_kernel_cfg’
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:386:3: warning: ‘return’ with a value, in function returning void [enabled by default]
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:389:1: warning: ‘return’ with a value, in function returning void [enabled by default]
/home/lp3/aodvuu096/lnx/kaodv-netlink.c: In function ‘kaodv_netlink_init’:
/home/lp3/aodvuu096/lnx/kaodv-netlink.c:374:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
make[3]: *** [/home/lp3/aodvuu096/lnx/kaodv-netlink.o] Error 1
make[2]: *** [_module_/home/lp3/aodvuu096/lnx] Error 2
make[2]: Leaving directory `/usr/src/linux-headers-3.8.0-31-generic'
make[1]: *** [kaodv.ko] Error 2
make[1]: Leaving directory `/home/lp3/aodvuu096/lnx'
make: *** [kaodv] Error 2
lp3@lp3-Latitude-E5420:~/aodvuu096$
答案 0 :(得分:0)
注释掉这一行以修复此错误。
nl_sk = netlink_kernel_create(&amp; init_net,NETLINK_USER,0,hello_nl_recv_msg,NULL,THIS_MODULE);