Commit 21d9f3a5 authored by Arne Øslebø's avatar Arne Øslebø
Browse files

fixed bug in configuration parser. Fixed various other bugs

git-svn-id: file:///home/svn/mapi/trunk@356 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent efe517aa
......@@ -41,8 +41,8 @@ static int interface_init(mapidflib_function_instance_t *instance,
}
static int interface_process(mapidflib_function_instance_t *instance,
const unsigned char* dev_pkt,
MAPI_UNUSED const unsigned char* link_pkt,
unsigned char* dev_pkt,
MAPI_UNUSED unsigned char* link_pkt,
MAPI_UNUSED mapid_pkthdr_t* pkt_head)
{
dag_record_t *rec=(dag_record_t*)dev_pkt;
......
......@@ -64,8 +64,8 @@ static int to_erf_init(mapidflib_function_instance_t *instance,
}
static int to_erf_process(mapidflib_function_instance_t *instance,
const unsigned char* dev_pkt,
MAPI_UNUSED const unsigned char* link_pkt,
unsigned char* dev_pkt,
MAPI_UNUSED unsigned char* link_pkt,
MAPI_UNUSED mapid_pkthdr_t* pkthdr)
{
erf_instance_t *i=instance->internal_data;
......
......@@ -159,8 +159,8 @@ static void init()
sprintf(mapi_conf,CONF_FILE,getenv("HOME"));
if (pc_load (mapi_conf))
{
path = pc_get_param ("", "libpath");
libs = pc_get_param ("", "libs");
path = pc_get_param (pc_get_category(""), "libpath");
libs = pc_get_param (pc_get_category(""), "libs");
}
else
{
......
......@@ -113,12 +113,12 @@ struct _mapidrv
mapidrv *next;
void *handle;
char *device;
char *alias;
int format;
int devid;
char *name;
int offline;
int active;
int mother;
mapi_offline_device_status_t offline_status;
char* description;
};
......@@ -555,7 +555,6 @@ cmd_get_device_info(int devicenumber,int pid, int sock)
device.deviceid = drv->devid;
strncpy(device.devicename,drv->device,sizeof(device.devicename));
device.active = drv->active;
device.mother = drv->mother;
device.status = drv->offline_status;
ok = 1;
break;
......@@ -598,7 +597,6 @@ cmd_get_offline_device_info(int devicenumber,int pid, int sock)
device.deviceid = drv->devid;
strncpy(device.devicename,drv->device,sizeof(device.devicename));
device.active = drv->active;
device.mother = drv->mother;
device.status = drv->offline_status;
buf.cmd = GET_AVAILIBLE_OFFLINE_DEVICES_ACK;
ok = 1;
......@@ -664,11 +662,13 @@ cmd_get_libpath (int pid, int sock)
char pwd[BUFSIZE];
int size = BUFSIZE;
int first = 1;
buf.cmd = GET_LIBPATH_ACK;
if (pc_load (mapid_conf))
{
path = pc_get_param ("", "libpath");
path = pc_get_param (pc_get_category(""), "libpath");
//Add current path of mapid to relative paths
if (getcwd (pwd, BUFSIZE) == NULL)
......@@ -738,7 +738,7 @@ cmd_get_libs (int pid, int sock) /*removed id, id==pid here */
if (pc_load (mapid_conf))
{
path = pc_get_param ("", "libs");
path = pc_get_param (pc_get_category(""), "libs");
strncpy ((char *)buf.data, path, sizeof (char) * 2048);
buf.cmd = GET_LIBS_ACK;
pc_close ();
......@@ -961,7 +961,6 @@ cmd_create_offline_device (char *dev, int format, int pid, int sock)
drv2->devid = -deviceid++;
drv2->offline = 1;
drv2->active = 1;
drv2->mother = 0;
drv2->description = strdup(drv->description);
drv2->offline_status = DEVICE_SETUP;
......@@ -1182,7 +1181,6 @@ cmd_create_offline_flow (char *dev, int format, int pid, uid_t uid, int sock)
drv2->devid = -deviceid++;
drv2->offline = 1;
drv2->active = 1;
drv2->mother = 0;
drv2->description = strdup(drv->description);
drv2->offline_status = DEVICE_SETUP;
fl->drv = drv2;
......@@ -1848,8 +1846,8 @@ mapidcom ()
int nbytes, len, s;
struct client *cl = NULL;
flist_node_t *tmpnode;
conf_category_t *cat=NULL;
conf_parameter_t *p=NULL;
conf_category_entry_t *cat=NULL;
char *local;
pthread_spin_init (&flowlist_lock, PTHREAD_PROCESS_SHARED);
pthread_spin_init (&clientlist_lock, PTHREAD_PROCESS_SHARED);
......@@ -1863,24 +1861,18 @@ mapidcom ()
if (pc_load (mapid_conf))
{
cat = pc_get_category ("general");
}
if(cat!=NULL && cat->params!=NULL)
p = cat->params;
if (p != NULL)
cat = pc_get_category ("");
local=pc_get_param(cat,"local");
if(local!=NULL && strcmp(local,"1")==0)
{
if (strcmp (p->name, "socket") == 0)
if(strcmp (p->value,"local") == 0)
{
free(mapidsocket);
mapidsocket=malloc(sizeof(MAPIDSOCKHOME)-2+strlen(getenv("HOME")));
sprintf(mapidsocket,MAPIDSOCKHOME,getenv("HOME"));
}
pc_close ();
free(mapidsocket);
mapidsocket=malloc(sizeof(MAPIDSOCKHOME)-2+strlen(getenv("HOME")));
sprintf(mapidsocket,MAPIDSOCKHOME,getenv("HOME"));
}
pc_close ();
}
// create the listener socket
if ((listenerfd = socket (AF_LOCAL, SOCK_STREAM, 0)) == -1)
{
......@@ -2207,9 +2199,9 @@ get_format (char *format)
static void *
load_drivers ()
{
conf_category_t *cat;
conf_category_entry_t *cat;
conf_parameter_t *p;
char *device = NULL, *driver = NULL, *drvpath = NULL, *format = NULL, *description = NULL;
char *device = NULL, *driver = NULL, *drvpath = NULL, *format = NULL, *description = NULL, *alias;
void *handle;
mapidrv *drv;
int err;
......@@ -2221,55 +2213,15 @@ load_drivers ()
if (pc_load (mapid_conf))
{
cat = pc_get_category ("driver");
drvpath = pc_get_param ("", "drvpath");
drvpath = pc_get_param (pc_get_category (""), "drvpath");
//Loop through drivers
while (cat != NULL)
{
p = cat->params;
if ((strcmp (p->name, "device") == 0) && (p->next != NULL) && (strcmp(p->next->name,"driver") == 0) )
{
device = p->value;
driver = p->next->value;
if((p->next->next!=NULL) && (strcmp(p->next->next->name, "description") == 0))
description = p->next->next->value;
else
description=NULL;
}
else
{
cat = cat->next;
continue;
}
/*
p = cat->params;
while (p != NULL)
{
if (strcmp (p->name, "device") == 0)
device = p->value;
else if (strcmp (p->name, "driver") == 0)
driver = p->value;
else if (strcmp (p->name, "description") == 0)
description = p->value;
p = p->next;
}
*/
device=pc_get_param(cat,"device");
driver=pc_get_param(cat,"driver");
description=pc_get_param(cat,"description");
alias=pc_get_param(cat,"alias");
if (strlen (device) > 0 && strlen (driver) > 0)
{
......@@ -2294,7 +2246,6 @@ load_drivers ()
drv->devid = deviceid++;
drv->offline=0;
drv->active = 0;
drv->mother = 1;
drv->offline_status = DEVICE_ONLINE;
if(description!=NULL)
drv->description = strdup(description);
......@@ -2376,7 +2327,6 @@ load_drivers ()
drv->handle = handle;
drv->offline = 1;
drv->active = 0;
drv->mother = 1;
drv->name = strdup (driver);
if(description!=NULL)
drv->description = strdup(description);
......@@ -2485,7 +2435,7 @@ parse_arguments (int argc, char **argv)
static void set_debug()
{
conf_category_t *cat=NULL;
conf_category_entry_t *cat=NULL;
conf_parameter_t *p=NULL;
int logmask = 0;
......
......@@ -137,7 +137,7 @@ process_pkts(void *buf,unsigned len, dag_instance_t *i)
unsigned c = 0;
int rlen = 0;
dag_record_t *rec;
const unsigned char *packet;
unsigned char *packet;
mapid_pkthdr_t mhdr;
rec = (dag_record_t *) buf;
......@@ -177,7 +177,7 @@ process_pkts_offline(void *buf,unsigned len, dag_instance_t *i)
unsigned c = 0;
int rlen = 0;
dag_record_t *rec;
const unsigned char *packet;
unsigned char *packet;
mapid_pkthdr_t mhdr;
rec = (dag_record_t *) buf;
......
......@@ -155,8 +155,8 @@ typedef struct mapidflib_function_def {
//Pointer to the process interface. Processes a packet.
int (*process)(mapidflib_function_instance_t* instance,
const unsigned char* dev_pkt,
const unsigned char* link_pkt,
unsigned char* dev_pkt,
unsigned char* link_pkt,
mapid_pkthdr_t* pkt_head);
//Pointer to the get_result interface. Returns the results
......
......@@ -39,7 +39,6 @@ typedef struct mapidlibflow {
short modifies; //1 if a function in the flow modifies packets
unsigned char *mod_pkt; //Modified packet
mapid_pkthdr_t mod_pkt_head;
short mod_pkt_init; // 1 if allocated space for current modpkt
#endif
#ifdef WITH_PRIORITIES
int priority;
......@@ -96,9 +95,9 @@ mapid_init(mapidlib_instance_t *i)
sprintf(mapid_conf,CONF_FILE,getenv("HOME"));
//Load function libraries
if(pc_load(mapid_conf)) {
str=pc_get_param("","libs");
str=pc_get_param(pc_get_category(""),"libs");
strncpy(buf,str,1024);
libpath=pc_get_param("","libpath");
libpath=pc_get_param(pc_get_category(""),"libpath");
str=buf;
while((s=strchr(str,':'))!=NULL) {
*s='\0';
......@@ -261,9 +260,7 @@ static void delete_flow(mapidlib_instance_t *i,struct mapidlibflow *flow) {
}
#ifdef WITH_MODIFY_PKTS
if(flow->mod_pkt != NULL && flow->mod_pkt != (unsigned char *)&modified_packet_buf[0]) {
// free(flow->mod_pkt);
}
free(flow->mod_pkt);
#endif
lock=&flow->hwinfo->gflist->lock;
......@@ -426,6 +423,11 @@ mapid_connect(mapidlib_instance_t *i,int fd)
return error;
}
#ifdef WITH_MODIFY_PKTS
//Allocate buffer for modified packets
flow->mod_pkt = malloc(sizeof(char) * flow->hwinfo->cap_length);
#endif
flow->status = FLOW_ACTIVE;
time(&flow->start);
......@@ -1006,17 +1008,6 @@ void mapid_process_pkt(mapidlib_instance_t *i,
if(flow->status==FLOW_ACTIVE) {
#ifdef WITH_MODIFY_PKTS
if(flow->modifies==1) {
if(flow->mod_pkt_init == 0) {
if(flow->mod_pkt != NULL){
free(flow->mod_pkt);
flow->mod_pkt = NULL;
}
flow->mod_pkt = malloc(sizeof(char) * flow->hwinfo->cap_length);
flow->mod_pkt_init = 1;
}
memcpy(flow->mod_pkt, link_pkt, pkt_head->caplen);
memcpy(&flow->mod_pkt_head,pkt_head,sizeof(mapid_pkthdr_t));
}
......
......@@ -9,6 +9,13 @@
int loadfile(char *file);
typedef struct conf_category {
char* name;
conf_category_entry_t *entry;
struct conf_category *next;
} conf_category_t;
conf_category_t *conf;
int loaded=0;
......@@ -41,6 +48,7 @@ int pc_load(char* filelist)
int loadfile(char *file)
{
conf_category_t *cp;
conf_category_entry_t *entry,**le,*entry2;
conf_parameter_t *param,**lp;
FILE *f;
int c;
......@@ -49,11 +57,11 @@ int loadfile(char *file)
cat[0]='\0';
if((f=fopen(file,"r"))!=NULL){
conf=malloc(sizeof(conf_category_t));
conf->category=malloc(sizeof(char));
conf->category[0]='\0';
conf->params=NULL;
conf->name=malloc(sizeof(char));
conf->name[0]='\0';
conf->entry=NULL;
conf->next=NULL;
lp=&conf->params;
le=&conf->entry;
//Parse the file
while(fgets(buf,BUFSIZE,f)!=NULL) {
if(buf[0]!=';' && buf[0]!='#') {
......@@ -63,29 +71,36 @@ int loadfile(char *file)
param->next=NULL;
param->name=strdup(tag);
param->value=strdup(val);
if(*le==NULL) {
*le=malloc(sizeof(conf_category_entry_t));
(*le)->params=NULL;
(*le)->next=NULL;
lp=&(*le)->params;
}
*lp=param;
lp=&param->next;
} else if((c=sscanf(buf,"[%[^]]", cat))==1) {
cp=pc_get_category(cat);
entry=pc_get_category(cat);
if(cp==NULL) {
if(entry==NULL) {
//Create new main category
cp=malloc(sizeof(conf_category_t));
cp->next=conf;
cp->name=strdup(cat);
cp->entry=NULL;
conf=cp;
le=&conf->entry;
} else {
//Find last category of this type
while(cp->next!=NULL)
cp=cp->next;
cp->next=malloc(sizeof(conf_category_t));
cp=cp->next;
cp->next=NULL;
entry2=malloc(sizeof(conf_category_entry_t));
entry2->params=NULL;
entry2->next=NULL;
lp=&entry2->params;
(*le)->next=entry2;
le=&entry2;
}
cp->category=strdup(cat);
cp->params=NULL;
lp=&cp->params;
}
}
}
......@@ -96,54 +111,59 @@ int loadfile(char *file)
return 0;
}
conf_category_t *pc_get_category(char *cat)
conf_category_entry_t *pc_get_category(char *cat)
{
conf_category_t *cp=conf;
while(cp!=NULL) {
if(strcmp(cp->category,cat)==0)
break;
if(strcmp(cp->name,cat)==0)
return cp->entry;
else cp=cp->next;
}
return cp;
return NULL;
}
char* pc_get_param(char *cat, char *name)
char* pc_get_param(conf_category_entry_t *entry, char *name)
{
conf_category_t *cp=pc_get_category(cat);
conf_parameter_t *p;
if(cp!=NULL) {
p=cp->params;
p=entry->params;
while(p!=NULL) {
if(strcmp(p->name,name)==0)
return p->value;
else
p=p->next;
}
}
return NULL;
}
int pc_close()
{
conf_category_t *cc2,*cc=conf;
conf_category_t *c,*c2;
conf_category_entry_t *e,*e2;
conf_parameter_t *p,*p2;
while(cc) {
p=cc->params;
while(p!=NULL) {
free(p->name);
free(p->value);
p2=p;
p=p->next;
free(p2);
c=conf;
while(c) {
e=c->entry;
while(e) {
p=e->params;
while(p) {
free(p->name);
free(p->value);
p2=p;
p=p->next;
free(p2);
}
e2=e;
e=e->next;
free(e2);
}
free(c->name);
c2=c;
c=c->next;
free(c2);
}
free(cc->category);
cc2=cc;
cc=cc->next;
free(cc2);
}
loaded=0;
return 1;
}
......@@ -7,14 +7,13 @@ typedef struct conf_parameter {
struct conf_parameter *next;
} conf_parameter_t;
typedef struct conf_category {
char* category;
typedef struct conf_category_entry {
conf_parameter_t *params;
struct conf_category* next;
} conf_category_t;
struct conf_category_entry *next;
} conf_category_entry_t;
int pc_load(char* filelist);
char* pc_get_param(char *cat, char* name);
conf_category_t* pc_get_category(char *cat);
char* pc_get_param(conf_category_entry_t *entry, char* name);
conf_category_entry_t* pc_get_category(char *cat);
int pc_close();
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment