当前位置: 技术问答>linux和unix
请问下linux Kernel 下如何返回指针
来源: 互联网 发布时间:2016-04-12
本文导语: 相当奇怪,一旦涉及到地址返回系统就崩溃掉了,直接换成数据返回一切正常。请教高手。代码如下 红色部分是相关的函数代码 typedef struct temp1 { __u8 prot ;//the protcol __u32 sradd ;//the ip sourch address __u32...
相当奇怪,一旦涉及到地址返回系统就崩溃掉了,直接换成数据返回一切正常。请教高手。代码如下
红色部分是相关的函数代码
typedef struct temp1
{
__u8 prot ;//the protcol
__u32 sradd ;//the ip sourch address
__u32 dtadd ;//the ip destine address
__u16 destport ;//the destine port
__u16 sourport ;//the source port
}ipdata;
typedef struct temp
{
ipdata netdata;// the data we need now
unsigned char *counter;//the counter not used
unsigned char *later;//the vector not used
int key;//the key to found data
int length ;
struct temp *Next;//the pointer to the next member
}hashmember;
static ipdata ipcontent;//the variable to read the data
static hashmember hmember;
static hashmember *Chashtable[251];//the member in the hashtable
static hashmember *test;
bool FindValue(int key, hashmember *value) //The fuction to find the value;
{
bool ret = false;
int i = 0;
int num = key % 251;// decide the num in the array
if(num>0)
{
hashmember *phe = Chashtable[num]; //take the value out
while(phe)
{
if(ilength)
{
if(phe->key == key)
{
value = phe;// Get the data we want, since in Kernel I could not use & I just use the variable.has
ret = true;
}
}
else
{
return ret;
}
i++;
phe = phe->Next;
}
}
return ret;
}
static unsigned int hook_main(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
unsigned int ret = NF_ACCEPT; /* flag to decide whether we drop this packet */
struct tcphdr *thead;//TCP struct
struct udphdr *thead1;//UDP struct
ipdata testdata;
//rwlock_t ipcontent = RW_LOCK_UNLOCKED;//protech the data
struct iphdr *iph; /* ip header */
struct sk_buff *sb = *skb;
int eth=0; // ethernet interface index
/* since we use PF_INET during hook register, we only receive IP packets */
iph=(*skb)->nh.iph; /* position the ip header pointer */
// Assuming the interface name is like eth0, take the last digit as number
if(in->name != NULL){
eth=in->name[3] - '0';
}
ALERT(0, "%s=%d", in->name, eth);
if(eth MAX_INGRESS_IF)//|| eth > MAX_INGRESS_IF)
return NF_ACCEPT;
/* handle our ip packet here
* (note:version verified; checksum has been checked) */
// things need to do in PRE routing stage
if (hooknum == NF_IP_PRE_ROUTING) {
// read_lock(&ipcontent);
//write the data to the ipcontent
ALERT(0, "Ipcontent");
ipcontent.prot = iph ->protocol;
ipcontent.sradd = iph->saddr;
ipcontent.dtadd = iph->daddr;
//read_unlock(&packetInCounterLock[eth]);
if(iph ->protocol == IPPROTO_TCP)//store the data if the protocol is TCP
{
thead = (struct tcphdr *)(sb->data +(sb->nh.iph->ihl*4));
ipcontent.sourport = thead->source;
ipcontent.destport = thead->dest;
}
if(iph ->protocol == IPPROTO_UDP)//store the data if the protocol is UDP
{
thead1 = (struct udphdr *)((*skb)->data +(iph->ihl*4));
ipcontent.sourport = thead1->source;
ipcontent.destport = thead1->dest;
}
hmember.counter = "x01";//store the later used variable with 1 in very bit.
hmember.later = "x01";
}
// things need to do in POST routing stage
//else if (hooknum == NF_IP_POST_ROUTING) {
//}
//Create the hashtable and insert the data.
ALERT(0, "Createkey");
Createkey(ipcontent,hmember.key,251);
hmember.netdata = ipcontent;
//ALERT(0, "%d",hmember.counter);
//ALERT(0, "Insert");
Insert(hmember.key,hmember.netdata);
FindValue(hmember.key,test);
testdata = test->netdata;
if(iph ->protocol == IPPROTO_TCP)//store the data if the protocol is TCP
{
ALERT(0, "The proctocol is TCP");
}
else if(iph ->protocol == IPPROTO_UDP)//store the data if the protocol is UDP
{
ALERT(0, "The proctocol is UDP");
}
else
{
ALERT(0, "The proctocol is unknown");
}
[color=#FF0000]ALERT(0, "The sourseport is port %d.%d", NIPQUAD(testdata.sourport));
ALERT(0, "The destport is port %d.%d", NIPQUAD(testdata.destport));
ALERT(0, "The sourseaddress is %d.%d.%d.%d", NIPQUAD(testdata.sradd));
ALERT(0, "The destineaddress is %d.%d.%d.%d", NIPQUAD(testdata.dtadd));[/color]//
/* we can design whether we accpet or drop the packet
* to drop this skb, just return NF_DROP;
* otherwise, return NF_ACCEPT; */
return ret;
}
红色部分是相关的函数代码
typedef struct temp1
{
__u8 prot ;//the protcol
__u32 sradd ;//the ip sourch address
__u32 dtadd ;//the ip destine address
__u16 destport ;//the destine port
__u16 sourport ;//the source port
}ipdata;
typedef struct temp
{
ipdata netdata;// the data we need now
unsigned char *counter;//the counter not used
unsigned char *later;//the vector not used
int key;//the key to found data
int length ;
struct temp *Next;//the pointer to the next member
}hashmember;
static ipdata ipcontent;//the variable to read the data
static hashmember hmember;
static hashmember *Chashtable[251];//the member in the hashtable
static hashmember *test;
bool FindValue(int key, hashmember *value) //The fuction to find the value;
{
bool ret = false;
int i = 0;
int num = key % 251;// decide the num in the array
if(num>0)
{
hashmember *phe = Chashtable[num]; //take the value out
while(phe)
{
if(ilength)
{
if(phe->key == key)
{
value = phe;// Get the data we want, since in Kernel I could not use & I just use the variable.has
ret = true;
}
}
else
{
return ret;
}
i++;
phe = phe->Next;
}
}
return ret;
}
static unsigned int hook_main(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
unsigned int ret = NF_ACCEPT; /* flag to decide whether we drop this packet */
struct tcphdr *thead;//TCP struct
struct udphdr *thead1;//UDP struct
ipdata testdata;
//rwlock_t ipcontent = RW_LOCK_UNLOCKED;//protech the data
struct iphdr *iph; /* ip header */
struct sk_buff *sb = *skb;
int eth=0; // ethernet interface index
/* since we use PF_INET during hook register, we only receive IP packets */
iph=(*skb)->nh.iph; /* position the ip header pointer */
// Assuming the interface name is like eth0, take the last digit as number
if(in->name != NULL){
eth=in->name[3] - '0';
}
ALERT(0, "%s=%d", in->name, eth);
if(eth MAX_INGRESS_IF)//|| eth > MAX_INGRESS_IF)
return NF_ACCEPT;
/* handle our ip packet here
* (note:version verified; checksum has been checked) */
// things need to do in PRE routing stage
if (hooknum == NF_IP_PRE_ROUTING) {
// read_lock(&ipcontent);
//write the data to the ipcontent
ALERT(0, "Ipcontent");
ipcontent.prot = iph ->protocol;
ipcontent.sradd = iph->saddr;
ipcontent.dtadd = iph->daddr;
//read_unlock(&packetInCounterLock[eth]);
if(iph ->protocol == IPPROTO_TCP)//store the data if the protocol is TCP
{
thead = (struct tcphdr *)(sb->data +(sb->nh.iph->ihl*4));
ipcontent.sourport = thead->source;
ipcontent.destport = thead->dest;
}
if(iph ->protocol == IPPROTO_UDP)//store the data if the protocol is UDP
{
thead1 = (struct udphdr *)((*skb)->data +(iph->ihl*4));
ipcontent.sourport = thead1->source;
ipcontent.destport = thead1->dest;
}
hmember.counter = "x01";//store the later used variable with 1 in very bit.
hmember.later = "x01";
}
// things need to do in POST routing stage
//else if (hooknum == NF_IP_POST_ROUTING) {
//}
//Create the hashtable and insert the data.
ALERT(0, "Createkey");
Createkey(ipcontent,hmember.key,251);
hmember.netdata = ipcontent;
//ALERT(0, "%d",hmember.counter);
//ALERT(0, "Insert");
Insert(hmember.key,hmember.netdata);
FindValue(hmember.key,test);
testdata = test->netdata;
if(iph ->protocol == IPPROTO_TCP)//store the data if the protocol is TCP
{
ALERT(0, "The proctocol is TCP");
}
else if(iph ->protocol == IPPROTO_UDP)//store the data if the protocol is UDP
{
ALERT(0, "The proctocol is UDP");
}
else
{
ALERT(0, "The proctocol is unknown");
}
[color=#FF0000]ALERT(0, "The sourseport is port %d.%d", NIPQUAD(testdata.sourport));
ALERT(0, "The destport is port %d.%d", NIPQUAD(testdata.destport));
ALERT(0, "The sourseaddress is %d.%d.%d.%d", NIPQUAD(testdata.sradd));
ALERT(0, "The destineaddress is %d.%d.%d.%d", NIPQUAD(testdata.dtadd));[/color]//
/* we can design whether we accpet or drop the packet
* to drop this skb, just return NF_DROP;
* otherwise, return NF_ACCEPT; */
return ret;
}
|
你的Chashtable[251]是一个全局静态指针数组,我在你的代码里面好像没有看到给每个数组元素(指针)初始化的地方!
如果没有初始化,那么Chashtable[num]就为NULL,所以,你将它作为返回值使用会有段错误!
另外,你的代码中的对FindValue的指针返回值需要做一个不为空的检测!如下
FindValue(hmember.key,test);
if(NULL != test)
{
printk ("DBG: Got the data!n");
testdata = test->netdata;
...
}
...
good luck!
如果没有初始化,那么Chashtable[num]就为NULL,所以,你将它作为返回值使用会有段错误!
另外,你的代码中的对FindValue的指针返回值需要做一个不为空的检测!如下
FindValue(hmember.key,test);
if(NULL != test)
{
printk ("DBG: Got the data!n");
testdata = test->netdata;
...
}
...
good luck!
|
哦,那是因为你定义的是静态变量,只在这个文件内起作用,你想把这个地址传出去,让外界来直接访问这个静态数据是不可以的。