当前位置: 技术问答>linux和unix
求救。。。linux kernel 下的结构体使用问题。。。。
来源: 互联网 发布时间:2016-05-12
本文导语: 不行了。。。我的代码如下,请高手指教 * Implementation of a cut-through target for client. *@author Ivan Lam */ //#define __KERNEL__ //#define MODULE #include #include #include #include #include //#include #include #include #includ...
不行了。。。我的代码如下,请高手指教
* Implementation of a cut-through target for client.
*@author Ivan Lam
*/
//#define __KERNEL__
//#define MODULE
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// Necessary because we use proc fs
//#include
//#include
// We scheduale tasks here
//#include
// We also need a timer
//#include
#include "test.h"
/*#include
#include
#include
#include
using namespace std;
static int debug=0;
module_param(debug, int, 0000);
static char *target_eth = "0"; // the interface to monitor
module_param(target_eth, charp, 0000);
/* System parameters */
MODULE_AUTHOR(AUTHOR);
MODULE_LICENSE("GPL"); // Just to avoid the taint message
/* Program vaiables */
// counters
static unsigned int packetInCount[MAX_INGRESS_IF] = {0};
static unsigned int packetInBytes[MAX_INGRESS_IF] = {0};
static unsigned int packetInDropCount[MAX_INGRESS_IF] = {0};
static unsigned int packetInDropBytes[MAX_INGRESS_IF] = {0};
static unsigned int packetOutCount[MAX_INGRESS_IF] = {0};
static unsigned int packetOutBytes[MAX_INGRESS_IF] = {0};
static unsigned int tablenumber = 0;
typedef struct ipdata
{
unsigened char prot = "xx";//the protcol
unsigened char *sradd = "xxxxxxxx";//the ip sourch address
unsigened char *dtadd = "xxxxxxxx";//the ip destine address
unsigened char *destport = "xxxx";//the destine port
unsigened char *sourport = "xxxx";//the source port
}
typedef struct hashmember
{
struct ipdata netdata;// the data we need now
unsigned char counter = "xx";//the counter not used
unsigned char later ="xx";//the vector not used
int key = 0;//the key to found data
int length = 0;
struct hashmember *Next = 0;//the pointer to the next member
}
// For SMP, need locks
/*
* print message to the screen
* ref: http://tldp.org/LDP/lkmpg/2.6/html/x1161.html
*/
// hash table class.Include the fuction of search data, store data.The Remove fuction has not been finished since unused now.
static struct ipdata ipcontent;//the variable to read the data
static struct hashmember hmember;
static struct hashmember *Chashtable[251] = {0};//the member in the hashtable
bool FindValue(int key,struct ipdata &value)
{
bool ret = false;
int num = key % 251;
if(num>0)
{
hashmember *phe = Chashtable[num];
while(phe)
{
if(phe->key = key)
{
value = phe ->netdata;
ret = true;
}
phe = phe->Next;
}
}
ALERT(0,"The value is d%",int(value->prot));
return ret;
}
bool Insert(int key,ipdata value)
{
bool ret = false;
int num = key % 251;
if(num>0)
{
if(Chashtable[num])
{
hashmember *phe = Chashtable[num];
while(phe->Next)
{
phe = phe->Next;
}
phe->Next = new hashmember;
phe->Next->Next = 0;
phe->Next->key = key;
phe->Next->netdata = value;
ret = true;
}
else
{
Chashtable[num] = new hashmember;
Chashtable[num]->Next = 0;
Chashtable[num]->key = key;
Chashtable[num]->netdata = value;
ret = true;
}
}
return ret;
}
钩子函数的部分就不写了问题应该不再那。这部分我一旦编译就说我结构体出错,编译错误如下:
home/reyesbear/test.c:74: error: expected specifier-qualifier-list before ‘unsigened’
/home/reyesbear/test.c:79: warning: useless storage class specifier in empty declaration
/home/reyesbear/test.c:85: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
/home/reyesbear/test.c:91: warning: useless storage class specifier in empty declaration
/home/reyesbear/test.c:108: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
/home/reyesbear/test.c:129: error: expected declaration specifiers or ‘...’ before ‘ipdata’
/home/reyesbear/test.c: In function ‘Insert’:
/home/reyesbear/test.c:137: error: ‘hashmember’ undeclared (first use in this function)
/home/reyesbear/test.c:137: error: (Each undeclared identifier is reported only once
/home/reyesbear/test.c:137: error: for each function it appears in.)
/home/reyesbear/test.c:137: error: ‘phe’ undeclared (first use in this function)
/home/reyesbear/test.c:142: error: ‘new’ undeclared (first use in this function)
/home/reyesbear/test.c:142: error: expected ‘;’ before ‘hashmember’
/home/reyesbear/test.c:145: error: ‘value’ undeclared (first use in this function)
/home/reyesbear/test.c:151: error: expected ‘;’ before ‘hashmember’
/home/reyesbear/test.c:152: error: ‘struct hashmember’ has no member named ‘Next’
/home/reyesbear/test.c:153: error: ‘struct hashmember’ has no member named ‘key’
/home/reyesbear/test.c: At top level:
/home/reyesbear/test.c:166: error: expected ‘)’ before ‘data’
/home/reyesbear/test.c: In function ‘hook_main’
有人能够指教吗?主要错误都是结构体的使用上,我觉得我没错啊,改了好多种申明方式了,谢谢。
* Implementation of a cut-through target for client.
*@author Ivan Lam
*/
//#define __KERNEL__
//#define MODULE
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// Necessary because we use proc fs
//#include
//#include
// We scheduale tasks here
//#include
// We also need a timer
//#include
#include "test.h"
/*#include
#include
#include
#include
using namespace std;
static int debug=0;
module_param(debug, int, 0000);
static char *target_eth = "0"; // the interface to monitor
module_param(target_eth, charp, 0000);
/* System parameters */
MODULE_AUTHOR(AUTHOR);
MODULE_LICENSE("GPL"); // Just to avoid the taint message
/* Program vaiables */
// counters
static unsigned int packetInCount[MAX_INGRESS_IF] = {0};
static unsigned int packetInBytes[MAX_INGRESS_IF] = {0};
static unsigned int packetInDropCount[MAX_INGRESS_IF] = {0};
static unsigned int packetInDropBytes[MAX_INGRESS_IF] = {0};
static unsigned int packetOutCount[MAX_INGRESS_IF] = {0};
static unsigned int packetOutBytes[MAX_INGRESS_IF] = {0};
static unsigned int tablenumber = 0;
typedef struct ipdata
{
unsigened char prot = "xx";//the protcol
unsigened char *sradd = "xxxxxxxx";//the ip sourch address
unsigened char *dtadd = "xxxxxxxx";//the ip destine address
unsigened char *destport = "xxxx";//the destine port
unsigened char *sourport = "xxxx";//the source port
}
typedef struct hashmember
{
struct ipdata netdata;// the data we need now
unsigned char counter = "xx";//the counter not used
unsigned char later ="xx";//the vector not used
int key = 0;//the key to found data
int length = 0;
struct hashmember *Next = 0;//the pointer to the next member
}
// For SMP, need locks
/*
* print message to the screen
* ref: http://tldp.org/LDP/lkmpg/2.6/html/x1161.html
*/
// hash table class.Include the fuction of search data, store data.The Remove fuction has not been finished since unused now.
static struct ipdata ipcontent;//the variable to read the data
static struct hashmember hmember;
static struct hashmember *Chashtable[251] = {0};//the member in the hashtable
bool FindValue(int key,struct ipdata &value)
{
bool ret = false;
int num = key % 251;
if(num>0)
{
hashmember *phe = Chashtable[num];
while(phe)
{
if(phe->key = key)
{
value = phe ->netdata;
ret = true;
}
phe = phe->Next;
}
}
ALERT(0,"The value is d%",int(value->prot));
return ret;
}
bool Insert(int key,ipdata value)
{
bool ret = false;
int num = key % 251;
if(num>0)
{
if(Chashtable[num])
{
hashmember *phe = Chashtable[num];
while(phe->Next)
{
phe = phe->Next;
}
phe->Next = new hashmember;
phe->Next->Next = 0;
phe->Next->key = key;
phe->Next->netdata = value;
ret = true;
}
else
{
Chashtable[num] = new hashmember;
Chashtable[num]->Next = 0;
Chashtable[num]->key = key;
Chashtable[num]->netdata = value;
ret = true;
}
}
return ret;
}
钩子函数的部分就不写了问题应该不再那。这部分我一旦编译就说我结构体出错,编译错误如下:
home/reyesbear/test.c:74: error: expected specifier-qualifier-list before ‘unsigened’
/home/reyesbear/test.c:79: warning: useless storage class specifier in empty declaration
/home/reyesbear/test.c:85: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
/home/reyesbear/test.c:91: warning: useless storage class specifier in empty declaration
/home/reyesbear/test.c:108: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
/home/reyesbear/test.c:129: error: expected declaration specifiers or ‘...’ before ‘ipdata’
/home/reyesbear/test.c: In function ‘Insert’:
/home/reyesbear/test.c:137: error: ‘hashmember’ undeclared (first use in this function)
/home/reyesbear/test.c:137: error: (Each undeclared identifier is reported only once
/home/reyesbear/test.c:137: error: for each function it appears in.)
/home/reyesbear/test.c:137: error: ‘phe’ undeclared (first use in this function)
/home/reyesbear/test.c:142: error: ‘new’ undeclared (first use in this function)
/home/reyesbear/test.c:142: error: expected ‘;’ before ‘hashmember’
/home/reyesbear/test.c:145: error: ‘value’ undeclared (first use in this function)
/home/reyesbear/test.c:151: error: expected ‘;’ before ‘hashmember’
/home/reyesbear/test.c:152: error: ‘struct hashmember’ has no member named ‘Next’
/home/reyesbear/test.c:153: error: ‘struct hashmember’ has no member named ‘key’
/home/reyesbear/test.c: At top level:
/home/reyesbear/test.c:166: error: expected ‘)’ before ‘data’
/home/reyesbear/test.c: In function ‘hook_main’
有人能够指教吗?主要错误都是结构体的使用上,我觉得我没错啊,改了好多种申明方式了,谢谢。
|
你struct name {}的方式定义结构,使用结构的时候就定义变量的时候就要用关键字struct如struc name variable;
typedef struct {} name;定义,定义变量的时候就可直接使用name variable;
typedef struct {} name;定义,定义变量的时候就可直接使用name variable;