mapidlib.c 29.5 KB
Newer Older
1
2
3
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Arne Øslebø's avatar
Arne Øslebø committed
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include "mapidlib.h"
#include "mapilibhandler.h"
15
#include "mapidflib.h"
Arne Øslebø's avatar
Arne Øslebø committed
16
17
18
#include "parseconf.h"
#include "mapidevices.h"
#include "debug.h"
's avatar
   
committed
19
#include "printfstring.h"
20
#include "mapi_errors.h"
21
#include "../trackflib/trackflib.h"
Arne Øslebø's avatar
Arne Øslebø committed
22

's avatar
committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Stores information about flows in mapidlib
typedef struct mapidlibflow {
  int fd;
  //  short active;
  enum mapi_flow_status status;
  short delete;
  flist_t *functionlist;
  flist_t *procfunctionlist;
  mapid_funct_info_t *funct_info_list; 
  mapid_hw_info_t *hwinfo; //Information about hardware using mapidlib
  char devtype[MAPI_STR_LENGTH];
  void* adapterinfo; //Pointer to adapter specific information
  char *shm; //Pointer to start of shared memory
  unsigned long long shm_size; //Size of shared memory
  key_t shm_key;
  int shm_id;
  char shm_fname[MAPI_STR_LENGTH];
  time_t start;
  time_t end;
  int err_no;
  short modifies; //1 if a function in the flow modifies packets
  unsigned char *mod_pkt; //Modified packet
  mapid_pkthdr_t mod_pkt_head;
#ifdef WITH_PRIORITIES
  int priority;
#endif

} mapidlibflow_t;

#ifdef WITH_MODIFY_PKTS
#define NIC_PKTCAP_LEN 1514
unsigned char modified_packet_buf[NIC_PKTCAP_LEN];
#endif
Arne Øslebø's avatar
Arne Øslebø committed
56

's avatar
   
committed
57
58
static short libs_loaded=0; //1 if function libraries are loaded
static char *libpath;
Arne Øslebø's avatar
Arne Øslebø committed
59

's avatar
   
committed
60
static void add_funct_info(int fid, mapiFunctArg *fargs, mapidflib_function_instance_t *f,mapid_funct_info_t **flist);
Arne Øslebø's avatar
Arne Øslebø committed
61
62
static void free_sharedmem(struct mapidlibflow *f);
static void free_functionlist(struct mapidlibflow *f);
's avatar
committed
63
static char* mapid_conf;
Arne Øslebø's avatar
Arne Øslebø committed
64
65
66
67
68
69
70

int
mapid_init(mapidlib_instance_t *i) 
//Set hw info
//infor = pointer to info
{
  char buf[1024],*str,*s;
71
72
73
  char pathname[MAPI_STR_LENGTH];
  int fd;
  
Arne Øslebø's avatar
Arne Øslebø committed
74
  //Copy devtype
75
/*
Arne Øslebø's avatar
Arne Øslebø committed
76
77
78
  i->flowlist=malloc(sizeof(flist_t));
  flist_init(i->flowlist);
  i->fcount=1;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
*/
#ifdef WITH_PRIORITIES
  int j = 0;
  i->flowlist=malloc(sizeof(flist_t*) * PRIORITIES);
  for(j = 0; j < PRIORITIES; j++)
  {
	  i->flowlist[j]=malloc(sizeof(flist_t));
	  flist_init(i->flowlist[j]);
  }
  i->fcount=1;
#else
  i->flowlist=malloc(sizeof(flist_t));
  flist_init(i->flowlist);
  i->fcount=1;
#endif
Arne Øslebø's avatar
Arne Øslebø committed
94
95

  if(!libs_loaded) {
96
97
98
    //mapid_conf = printf_string(CONF_FILE,getenv("HOME"));
    mapid_conf = printf_string( CONFDIR"/"CONF_FILE );

Arne Øslebø's avatar
Arne Øslebø committed
99
    //Load function libraries
's avatar
committed
100
    if(pc_load(mapid_conf)) {
101
102
103
104
105
      if ((str = pc_get_param(pc_get_category(""),"libs")) == NULL) {
	printf("error: %s: cannot find libs entry\n", CONF_FILE);
	fflush(stdout);
	exit(1);
      }
Arne Øslebø's avatar
Arne Øslebø committed
106
      strncpy(buf,str,1024);
107
108
109
110
111
      if ((libpath=pc_get_param(pc_get_category(""),"libpath")) == NULL) {
	printf("error: %s: cannot find libpath entry\n", CONF_FILE);
	fflush(stdout);
	exit(1);
      }
Arne Øslebø's avatar
Arne Øslebø committed
112
113
114
115
116
117
118
119
120
121
      str=buf;
      while((s=strchr(str,':'))!=NULL) {
	*s='\0';
	mapilh_load_library(libpath,str);
	str=s+1;
      }
      mapilh_load_library(libpath,str);
      pc_close();
      libs_loaded=1;
    }
's avatar
committed
122
    free(mapid_conf);
Arne Øslebø's avatar
Arne Øslebø committed
123
  }
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  //Initialized spinlock in shared memory
  //Allocate shared memory
  strncpy(pathname,FUNCTION_SHM_TEMPLATE,MAPI_STR_LENGTH);
  if(mktemp(pathname)==NULL)
    return MDLIB_SHM_ERR;
  
  umask(017);
  if((fd=open(pathname,O_CREAT|O_EXCL,FUNCTION_SHM_PERMS))<0)
    return MDLIB_SHM_ERR;
  else 
    close(fd);
  
  strncpy(i->shm_spinlock_fname,pathname,MAPI_STR_LENGTH);

  if((i->shm_spinlock_key=ftok(pathname,FUNCTION_SHM_PROJECT_ID))<0)
    return MDLIB_SHM_ERR;
  
  if((i->shm_spinlock_id=shmget(i->shm_spinlock_key,sizeof(pthread_spinlock_t),FUNCTION_SHM_PERMS | IPC_CREAT)) < 0)
    return MDLIB_SHM_ERR;
144

145
146
147
148
149
  if((i->shm_spinlock=shmat(i->shm_spinlock_id,0,0))==NULL)
    return MDLIB_SHM_ERR;
  
  //Initialize spinlock
  pthread_spin_init (i->shm_spinlock, PTHREAD_PROCESS_SHARED);	
Arne Øslebø's avatar
Arne Øslebø committed
150
  i->shm_spinlock_size=sizeof(pthread_spinlock_t);
Arne Øslebø's avatar
Arne Øslebø committed
151
152
153
154
155

  return 0;
};

//Function that can be used by MAPI functions for adding new functions to the flow
156
static int mapid_add_funct(mapidlib_instance_t *i, int fd, char *funct, ...)
Arne Øslebø's avatar
Arne Øslebø committed
157
{
158
159
160
161
//  struct mapidlibflow * fl=flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow* fl=flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
162
  struct mapidlibflow * fl=flist_get(i->flowlist,fd);
163
#endif
Arne Øslebø's avatar
Arne Øslebø committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  mapidflib_function_def_t *fdef=mapilh_get_function_def(funct,
							 fl->hwinfo->devtype);
  char *argdescr_ptr;
  int tmp;
  unsigned long long ltmp;
  char ctmp;
  va_list vl;
  mapiFunctArg *pos;
  mapiFunctArg args[FUNCTARGS_BUF_SIZE];
  
  pos=args;

  va_start(vl,funct);

  if(strncmp(fdef->argdescr, "", 1)) { // there are some args
    argdescr_ptr = fdef->argdescr;
    while(strlen(argdescr_ptr) > 0){
      switch(*argdescr_ptr) {
        case 's':
          addarg(&pos, va_arg(vl, char*), STRING);
          break;
        case 'i':
          tmp = va_arg(vl, int);
          addarg(&pos, &tmp, INT);
          break;
        case 'c':
          ctmp = va_arg(vl, int); //`char' is promoted to `int' when passed through `...'
          addarg(&pos, &ctmp, CHAR);
          break;
        case 'l':
          ltmp = va_arg(vl, unsigned long long);
          addarg(&pos, &ltmp, UNSIGNED_LONG_LONG);
          break;
        default:
's avatar
committed
198
          return -1;
Arne Øslebø's avatar
Arne Øslebø committed
199
200
201
202
203
204
          break;
      }
      argdescr_ptr++; // move to the next arg
    }
  }

205
  return mapid_apply_function(i, fd, funct, args, 0);
Arne Øslebø's avatar
Arne Øslebø committed
206
207
}

208
209
210
211
212
213
214
215
216

static void fix_funct_ref(global_function_list_t *gflist,mapidflib_function_t *ifunct) {
  flist_node_t *flow;
  flist_node_t *fnode;
  flist_t *functions;
  mapidflib_function_t *funct;

  //Loop through all flows
  for(flow=flist_head(gflist->fflist);flow!=NULL;flow=flist_next(flow)) {
217
    functions=((mapid_flow_info_t*)flist_data(flow))->flist;
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    if(functions==NULL)
      continue;
    
    for(fnode=flist_head(functions); fnode!=NULL; fnode=flist_next(fnode)) {
      funct=(mapidflib_function_t*)flist_data(fnode);
      if(funct->instance==ifunct->instance) {
	pthread_spin_lock(&gflist->lock);
	funct->ref=0;
	ifunct->ref=1;
	funct->instance->refcount--;
	pthread_spin_unlock(&gflist->lock);
	DEBUG_CMD(printf("Fixed reference for function %d [%s:%d]\n",funct->fid,__FILE__,__LINE__));
      }
    }
  }
}





//Deletes a flow and frees all resources allocated by the flow
static void delete_flow(mapidlib_instance_t *i,struct mapidlibflow *flow) {
  struct mapidlibflow *f;
  flist_t *functions;
  flist_node_t *funct_node;
  mapidflib_function_t *funct;  
  pthread_spinlock_t *lock;

  
  DEBUG_CMD(printf("Deleting flow %d [%s:%d]\n",flow->fd,__FILE__,__LINE__));
  

  //global optimization
252
  functions=((mapid_flow_info_t*)flist_get(flow->hwinfo->gflist->fflist,flow->fd))->flist;
253
254
255
256
257
258
259
  if(functions!=NULL)
    for(funct_node=flist_head(functions);funct_node!=NULL;funct_node=flist_next(funct_node)) {
      funct=(mapidflib_function_t*)flist_data(funct_node);
      if(funct->ref==0 && funct->instance->refcount>0 )
	//Check to see if other functions references this function
	fix_funct_ref(flow->hwinfo->gflist,funct);       
    }
's avatar
committed
260
261

#ifdef WITH_MODIFY_PKTS
Arne Øslebø's avatar
Arne Øslebø committed
262
263
  if(flow->mod_pkt!=NULL)
    free(flow->mod_pkt);
's avatar
committed
264
#endif
265
266
267
268
269

  lock=&flow->hwinfo->gflist->lock;
  pthread_spin_lock(lock);
  
#ifdef WITH_PRIORITIES
270
  f=flist_remove(i->flowlist[flow->priority],flow->fd);
271
#else
272
  f=flist_remove(i->flowlist,flow->fd);
273
274
275
276
277
278
279
280
281
282
283
284
#endif
  
  free_functionlist(f);	 
  free_sharedmem(f);
  free(f);
  
  pthread_spin_unlock(lock);
}



//Frees resources allocated by mapidlib, should be called by the drivers when devices are deleted
Arne Øslebø's avatar
Arne Øslebø committed
285
286
287
288
289
290
void mapid_destroy(mapidlib_instance_t *i)
{
  flist_node_t *n;
  struct mapidlibflow *f;
  
  if (i!=NULL) {
291
    //    pthread_spin_lock(i->shm_spinlock);
292
293
294
295
296
297
298
 //  n=flist_head(i->flowlist);
#ifdef WITH_PRIORITIES
	int j = 0;
	for(j = 0; j < PRIORITIES; j++)
	{
	n=flist_head(i->flowlist[j]);
#else
Arne Øslebø's avatar
Arne Øslebø committed
299
    n=flist_head(i->flowlist);
300
301
#endif
 
302
    while (n!=NULL) {
Arne Øslebø's avatar
Arne Øslebø committed
303
304
      f=flist_data(n);
      n=flist_next(n); 
305
      delete_flow(i,f);
Arne Øslebø's avatar
Arne Øslebø committed
306
307
    }
   
308
//    flist_destroy(i->flowlist);
309
#ifdef WITH_PRIORITIES
310
	flist_destroy(i->flowlist[j]);
311
312
313
	free(i->flowlist[j]);
	}
#else
314
    flist_destroy(i->flowlist);
315
#endif
Arne Øslebø's avatar
Arne Øslebø committed
316
    free(i->flowlist);
317
    //  pthread_spin_unlock(i->shm_spinlock);
318
319
320
321
322
323
324
325
326

    if (shmdt((const void*)i->shm_spinlock)<0) {
      WARNING_CMD(printf("Warning: Could not detach shared mem (%s) [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    }

    if (shmctl(i->shm_spinlock_id,IPC_RMID,NULL)<0) {
      WARNING_CMD(printf("Warning: Could not free shared mem (%s) [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    }

's avatar
committed
327
328
  } 
  else if (libs_loaded>0) {
Arne Øslebø's avatar
Arne Øslebø committed
329
330
331
332
333
334
335
336
337
338
    mapilh_free_libraries();
    libs_loaded=0;
  }
}

int
mapid_connect(mapidlib_instance_t *i,int fd)
//Connect to a mapi flow
//fd = flow descriptor
{
339
340
341
342
//  struct mapidlibflow *flow=flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow *flow=flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
343
  struct mapidlibflow *flow=flist_get(i->flowlist,fd);
344
#endif 
Arne Øslebø's avatar
Arne Øslebø committed
345
  flist_node_t *n;
346
347
  mapidflib_function_t *f;
  mapidflib_function_instance_t *fi;
Arne Øslebø's avatar
Arne Øslebø committed
348
349
  unsigned long offset=0;
  int error=0;
350
  int id, fdn;
Arne Øslebø's avatar
Arne Øslebø committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  char pathname[MAPI_STR_LENGTH];

  /* NULL-> invalid flow descriptor, set error */
  if(flow==NULL)
    {
      //we can't know the flow-id, so we can't set an error */
      return -1;
    }
    
  DEBUG_CMD(printf ("Connect to flow %d [%s:%d]\n", fd, __FILE__,__LINE__));

  /* Don't allow connect() for a second time at the same flow*/
  if(flow->status == FLOW_ACTIVE) {
    return MDLIB_FLOW_ALREADY_ACTIVE;
  }
  
  if(flow->shm_size>0) {
    //Allocate shared memory
    strncpy(pathname,FUNCTION_SHM_TEMPLATE,MAPI_STR_LENGTH);
    if(mktemp(pathname)==NULL)
      return MDLIB_SHM_ERR;
    
    umask(017);
374
    if((fdn=open(pathname,O_CREAT|O_EXCL,FUNCTION_SHM_PERMS))<0)
Arne Øslebø's avatar
Arne Øslebø committed
375
376
      return MDLIB_SHM_ERR;
    else 
377
      close(fdn);
Arne Øslebø's avatar
Arne Øslebø committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

    strncpy(flow->shm_fname,pathname,MAPI_STR_LENGTH);

    if((flow->shm_key=ftok(pathname,FUNCTION_SHM_PROJECT_ID))<0)
      return MDLIB_SHM_ERR;
      
    if((id=shmget(flow->shm_key,flow->shm_size,FUNCTION_SHM_PERMS | IPC_CREAT)) < 0)
      return MDLIB_SHM_ERR;

    flow->shm_id=id;

    if((flow->shm=shmat(id,0,0))==NULL)
      return MDLIB_SHM_ERR;

    //Initialize memory to 0
    memset(flow->shm,0,flow->shm_size);
  }

  //Initialize flow functions
  n=flist_head(flow->functionlist);
  while(n && !error) {
    f=flist_data(n);
400
401
    fi=f->instance;
    if(fi->def->restype==MAPIRES_SHM) {
Arne Øslebø's avatar
Arne Øslebø committed
402
      //Sett results info
403
404
405
406
407
408
      fi->result.info.shm.key=flow->shm_key;
      fi->result.info.shm.buf_size=flow->shm_size;
      fi->result.info.shm.res_size=fi->result.data_size;
      fi->result.info.shm.offset=offset;
      fi->result.data=flow->shm+offset;
      offset+=fi->result.data_size;
409
410
411
412
      
      //Set information about spinlock
      fi->result.info.shm_spinlock.key=i->shm_spinlock_key;
      fi->result.info.shm_spinlock.buf_size=i->shm_spinlock_size;
Arne Øslebø's avatar
Arne Øslebø committed
413
      fi->result.info.shm_spinlock.offset=0;
414
      
Arne Øslebø's avatar
Arne Øslebø committed
415
    } 
416
417
418
    if(fi->def->init!=NULL) {
      DEBUG_CMD(printf("Initializing function %s\tfid=%d [%s:%d]\n",fi->def->name,f->fid,__FILE__,__LINE__));
      error=fi->def->init(fi,fd);    
Arne Øslebø's avatar
Arne Øslebø committed
419
420
421
    }
    
    if (error==0)
422
      fi->status=MAPIFUNC_INIT;
Arne Øslebø's avatar
Arne Øslebø committed
423
424
425
426
427
428
429
430
431
432
433
      
    n=flist_next(n);
  }

  if(error) {
    free_functionlist(flow);
    free_sharedmem(flow);
    flow->delete=2;
    return error;
  }

434
435
436
437
438
#ifdef WITH_MODIFY_PKTS
  //Allocate buffer for modified packets
  flow->mod_pkt = malloc(sizeof(char) * flow->hwinfo->cap_length);
#endif

Arne Øslebø's avatar
Arne Øslebø committed
439
  flow->status = FLOW_ACTIVE;
440
441
442
443
444
  
  //Update global function list
  ((mapid_flow_info_t*)flist_get(flow->hwinfo->gflist->fflist,flow->fd))->status=FLOW_ACTIVE;
  
  
Arne Øslebø's avatar
Arne Øslebø committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  time(&flow->start);

  return 0;
}

int
mapid_add_flow(mapidlib_instance_t *i,
	       int fd,
	       mapid_hw_info_t* hwinfo,
	       void *info)
//Add new flow
//fd = flow descriptor
//info = adapter specific info
{
  struct mapidlibflow* fl;
460
  mapid_flow_info_t *flow=malloc(sizeof(mapid_flow_info_t));
Arne Øslebø's avatar
Arne Øslebø committed
461
462
463
464
465
466
467
468
469
470
471
472

  fl=malloc(sizeof (struct mapidlibflow));

  fl->fd=fd;
  fl->status=FLOW_INIT;
  fl->delete=0;
  fl->err_no=0;
  fl->shm=NULL;
  fl->shm_size=0;
  fl->shm_fname[0] = '\0';
  fl->adapterinfo=info;
  fl->funct_info_list=NULL;
Arne Øslebø's avatar
Arne Øslebø committed
473
  fl->mod_pkt=NULL;
474
475

  //Add new flow to the global function list
Arne Øslebø's avatar
Arne Øslebø committed
476
477
  fl->functionlist=malloc(sizeof(flist_t));
  flist_init(fl->functionlist);
478
  pthread_spin_lock(&hwinfo->gflist->lock);
479
480
481
482
  flow->status=FLOW_INIT;
  
  flow->flist=fl->functionlist;
  flist_append(hwinfo->gflist->fflist,fd,flow);
483
484
  pthread_spin_unlock(&hwinfo->gflist->lock);

Arne Øslebø's avatar
Arne Øslebø committed
485
486
  fl->procfunctionlist=malloc(sizeof(flist_t));
  flist_init(fl->procfunctionlist);
487
488
489
490

#ifdef WITH_PRIORITIES
  fl->priority = fl->fd % PRIORITIES;
#endif
Arne Øslebø's avatar
Arne Øslebø committed
491
492
493
494
 
  fl->hwinfo=hwinfo;
  fl->start=0;
  fl->end=0;
495
496
497
#ifdef WITH_PRIORITIES
  flist_append(i->flowlist[fl->priority],fd,fl);
#else
Arne Øslebø's avatar
Arne Øslebø committed
498
  flist_append(i->flowlist,fd,fl);
499
500
#endif
//  flist_append(i->flowlist,fd,fl);
Arne Øslebø's avatar
Arne Øslebø committed
501
502
503
504
505
506

#ifdef WITH_MODIFY_PKTS
  fl->modifies=0;
#endif

  DEBUG_CMD(printf ("Added new flow, fd=%d [%s:%d]\n", fd, __FILE__,__LINE__));
507
508
509
510
#ifdef WITH_PRIORITIES
  printf("priority %d\n", fl->priority);
#endif

Arne Øslebø's avatar
Arne Øslebø committed
511
512
513
514
515
516
517
518
519
  return 0; /* no error possible */
}

/* FIXME:
 * This just scratches the surface of resources which need to be freed. 
 */
static void
free_functionlist (struct mapidlibflow *fl)
{
520
  mapidflib_function_t *fn;
Arne Øslebø's avatar
Arne Øslebø committed
521
522
523
524
  mapid_funct_info_t *fi, *fi2;
  flist_node_t *cur;

  //Remove from ff_list
525
  flist_remove(fl->hwinfo->gflist->fflist,fl->fd);
526

Arne Øslebø's avatar
Arne Øslebø committed
527
528
  cur=flist_head(fl->functionlist);
  while(cur) {
's avatar
committed
529
  	
Arne Øslebø's avatar
Arne Øslebø committed
530
    fn=flist_data(cur);
531
    if(fn->instance->def->cleanup!=NULL && fn->instance->status==MAPIFUNC_INIT)
532
      fn->instance->def->cleanup(fn->instance);
Arne Øslebø's avatar
Arne Øslebø committed
533

534
    free(fn->instance->def);
Arne Øslebø's avatar
Arne Øslebø committed
535
536
    //    if(fn->internal_data!=NULL)
    //  free(fn->internal_data);
537
    free(fn->instance);
Arne Øslebø's avatar
Arne Øslebø committed
538
539
540
541
    free(fn);
    cur=cur->next;
  }
  
542
  flist_destroy(fl->functionlist);
Arne Øslebø's avatar
Arne Øslebø committed
543
544
545
546
547
  free(fl->functionlist);

  fi=fl->funct_info_list;
  while(fi) {
    fi2=fi->next;
548
    free(fi);
Arne Øslebø's avatar
Arne Øslebø committed
549
550
551
    fi=fi2;
  }

552
  flist_destroy(fl->procfunctionlist);     
Arne Øslebø's avatar
Arne Øslebø committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  free(fl->procfunctionlist);

  //free(fl);
}

static void free_sharedmem(struct mapidlibflow *f)
{
  if (f->shm_size>0 && f->shm!=NULL) {
    if (shmdt(f->shm)<0) {
      WARNING_CMD(printf("Warning: Could not detach shared mem (%s) [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    }
 
    if (shmctl(f->shm_id,IPC_RMID,NULL)<0) {
      WARNING_CMD(printf("Warning: Could not free shared mem (%s) [%s:%d]\n",strerror(errno),__FILE__,__LINE__));
    }
    
    if (remove(f->shm_fname)<0) {
      WARNING_CMD(printf("Could not remove semaphore file %s (%s) [%s:%d]\n",f->shm_fname,strerror(errno),__FILE__,__LINE__));
    }
  }
}

575
576


Arne Øslebø's avatar
Arne Øslebø committed
577
578
579
580
581
582
583
584
int
mapid_close_flow(mapidlib_instance_t *i,int fd)
{
  struct mapidlibflow *f;
  //flist_node_t *n2;
  //  mapidflib_function_instance_t *funct;

  DEBUG_CMD(printf("Marking flow %d for deletion [%s:%d]\n",fd,__FILE__,__LINE__));
585
586
587
#ifdef WITH_PRIORITIES
  f=flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
588
  f=flist_get(i->flowlist,fd);
589
590
591
#endif

//  f=flist_get(i->flowlist,fd);
592
  if(f->status==FLOW_FINISHED) 
Arne Øslebø's avatar
Arne Øslebø committed
593
    f->delete=2;
594
  else if (f->delete==0) 
Arne Øslebø's avatar
Arne Øslebø committed
595
    f->delete=1;
596
  
597
598


Arne Øslebø's avatar
Arne Øslebø committed
599
  f->status=FLOW_CLOSED;
600
601
602
  //Update global function list
  ((mapid_flow_info_t*)flist_get(f->hwinfo->gflist->fflist,f->fd))->status=FLOW_CLOSED;
  
Arne Øslebø's avatar
Arne Øslebø committed
603
604
  time(&f->end);

605

Arne Øslebø's avatar
Arne Øslebø committed
606
607
608
  return 0;
}

's avatar
committed
609
/* Deprecated
Arne Øslebø's avatar
Arne Øslebø committed
610
611
612
613
614
615
static mapidflib_function_instance_t *
get_function(mapidlib_instance_t *i,int fd,int fid)
//Get function information
//fd = flow descriptor
//fid = function ID
{
616
617
618
619
//  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow *flow = flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
620
  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
621
622
#endif

Arne Øslebø's avatar
Arne Øslebø committed
623
624
  return flist_get(flow->functionlist,fid);
}
's avatar
committed
625
*/
Arne Øslebø's avatar
Arne Øslebø committed
626
627
628
629
630
631
632
633
634
635
int
mapid_read_results(mapidlib_instance_t *i,
		   int fd,
		   int fid,
		   mapid_result_t **result)
//Get pointer to shared memory where results are stored
//fd: flow descriptor
//fid: ID of function
//result: pointer to structure where info about shared memory is stored
{
636
637
  flist_t *functs;
  mapidflib_function_t *funct;
Arne Øslebø's avatar
Arne Øslebø committed
638
  mapidflib_result_t *res;
639

640
#ifdef WITH_PRIORITIES
641
  struct mapidlibflow *flow=flist_priorities_get(i->flowlist,fd);
642
#else
643
644
645
  struct mapidlibflow *flow=flist_get(i->flowlist,fd);
#endif 

646
  functs=((mapid_flow_info_t*)flist_get(flow->hwinfo->gflist->fflist,fd))->flist;
647
648
649
650
651

  if(functs==NULL)
    return MDLIB_INVALID_FLID;
  else if((funct=flist_get(functs,fid))==NULL)
    return MDLIB_INVALID_FUNCID;
Arne Øslebø's avatar
Arne Øslebø committed
652

653
654
  if(funct->instance->def->get_result!=NULL) {
    funct->instance->def->get_result(funct->instance,&res);
Arne Øslebø's avatar
Arne Øslebø committed
655
656
    *result=&res->info;
  } else 
657
    *result=&funct->instance->result.info;
Arne Øslebø's avatar
Arne Øslebø committed
658
  
Arne Øslebø's avatar
Arne Øslebø committed
659
660
  

Arne Øslebø's avatar
Arne Øslebø committed
661
662
663
664
665
  return 0;
}

int mapid_get_flow_info(mapidlib_instance_t *i,int fd,mapi_flow_info_t *info) 
{  
666
667
668
669
//  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow *flow = flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
670
  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
671
672
#endif

Arne Øslebø's avatar
Arne Øslebø committed
673
674
675
676
677
  info->fd=flow->fd;
  info->start=flow->start;
  info->end=flow->end;
  info->status=flow->status;
  info->num_functions=flist_size(flow->functionlist);
678
679
680
#ifdef WITH_PRIORITIES
  info->priority = flow->priority;
#endif
Arne Øslebø's avatar
Arne Øslebø committed
681
682
683
684
685
686

  return 0;
}

mapid_funct_info_t* mapid_get_flow_functions(mapidlib_instance_t *i,int fd)
{
687
688
689
690
//  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow *flow = flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
691
  struct mapidlibflow *flow = flist_get(i->flowlist,fd);
692
#endif
Arne Øslebø's avatar
Arne Øslebø committed
693
694
695
696
  return flow->funct_info_list;
}


697
//Finds and identical function that can be used for global optimization
's avatar
committed
698
static mapidflib_function_instance_t *find_identical_function(MAPI_UNUSED mapidlib_instance_t *i,global_function_list_t *gflist, int fd, char *name, char *args, int devid) {
699
700
701
702
703
  flist_node_t *flow;
  flist_node_t *fnode,*fnode2;
  mapidflib_function_t *funct,*funct2;
  
  //Get flow functions already applied
704
  flist_t *functs=((mapid_flow_info_t*)flist_get(gflist->fflist,fd))->flist;
Arne Øslebø's avatar
Arne Øslebø committed
705

706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#ifdef WITH_PRIORITIES
  mapidlibflow_t *iflow;
  mapidlibflow_t *fl=flist_priorities_get(i->flowlist,fd);
#endif

  //Loop through all registered flows
  for(flow=flist_head(gflist->fflist);flow!=NULL;flow=flist_next(flow)) {
    //Check to see if flows belong to same adapter
#ifdef WITH_PRIORITIES
    iflow=flist_priorities_get(i->flowlist,flist_id(flow));
#endif
    if(flist_head((flist_t*)flist_data(flow))==NULL)
      continue;
    funct=flist_data(flist_head((flist_t*)flist_data(flow)));
    if(funct->instance->hwinfo->devid!=devid)
      continue;
    if(funct->instance->status==MAPIFUNC_UNINIT)
      continue;
    
    fnode=flist_head(functs);
    for(fnode2=flist_head((flist_t*)flist_data(flow)); fnode2!=NULL; fnode2=flist_next(fnode2)) {
      funct2=(mapidflib_function_t*)flist_data(fnode2);
      if(fnode!=NULL) {
	funct=flist_data(fnode);	
	if(!funct2->instance->def->filters_pkts || (strcmp(funct->instance->def->name,funct2->instance->def->name)!=0 &&
	   strcmp((char *)funct->instance->args,(char*)funct2->instance->args)!=0))
Arne Øslebø's avatar
Arne Øslebø committed
732
	  break;
733
734
735
736
737
738
739
740
741
742
	fnode=flist_next(fnode);
      } else if(strcmp(name,funct2->instance->def->name)==0 &&
		strcmp(args,(char*)funct2->instance->args)==0 && 
		((funct2->instance->def->restype==MAPIRES_NONE && funct2->instance->hwinfo->offline==0) || 
		 funct2->instance->hwinfo->offline!=0))
#ifdef WITH_PRIORITIES
	if(iflow->priority<fl->priority)
#endif
	  return funct2->instance;
    }
Arne Øslebø's avatar
Arne Øslebø committed
743
744
  }

745
  return NULL;
Arne Øslebø's avatar
Arne Øslebø committed
746
747
748
749
750
751
}

int
mapid_apply_function(mapidlib_instance_t *i,
		     int fd,
		     char* function,
752
753
		     mapiFunctArg *fargs,
		     unsigned char internal)
Arne Øslebø's avatar
Arne Øslebø committed
754
755
756
757
758
759
760
//Apply function to a flow
//fd =flow descriptor
//function = info about function
//fptr = pointer to function
//fargs = pointer to arguments buffer
{
  mapidflib_function_def_t *f2;
761
762
  mapidflib_function_instance_t *funct_instance=NULL;
  mapidflib_function_t *funct;
763
764
765
766
//  struct mapidlibflow * fl=flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow * fl=flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
767
  struct mapidlibflow * fl=flist_get(i->flowlist,fd);
768
769
#endif
 
Arne Øslebø's avatar
Arne Øslebø committed
770
771
772
773
  int fid;
  mapidflib_flow_mod_t flow_mod;
  int error=0;
  int c;
774
  int similar=0;
Arne Øslebø's avatar
Arne Øslebø committed
775
776
777
778
779
780
781
782
783
784
785
786
787
  
  flow_mod.delete_size=0;
  //Get function definition
  f2=mapilh_get_function_def(function,fl->hwinfo->devtype);

  if(f2==NULL) {
    //Change so that it returns error message to the application
    DEBUG_CMD(printf("ERROR: Could not find/match function %s [%s:%d]\n",function,__FILE__,__LINE__));
    error=MDLIB_FUNCTION_NOT_FOUND;
    fl->err_no=error;
    return -1;
  }

788
789
  //Create function
  funct=malloc(sizeof(mapidflib_function_t));
Arne Øslebø's avatar
Arne Øslebø committed
790
  funct->fd=fd;
791
  funct->fid=i->fcount;
Arne Øslebø's avatar
Arne Øslebø committed
792

793
#ifdef WITH_GLOBAL_OPT
794
795
796
797
798
799
800
801
802
803
  if(f2->optimize==MAPIOPT_AUTO) {
    //Check to see if there is an existing instance of this function that can be used
    funct_instance=find_identical_function(i,fl->hwinfo->gflist,fd,function,(char*)fargs,fl->hwinfo->devid);
    if(funct_instance!=NULL) {
      DEBUG_CMD(printf("Added referece to existing %s function [%s:%d]\n",function,__FILE__,__LINE__));
      funct->instance=funct_instance;
      funct->ref=1;
      funct_instance->refcount++;
      
      //Add function to list
804
805
806
807
808
809
810
811
812
813
      if(!internal)
	      flist_append(fl->functionlist,fid,funct);
      else
	      flist_prepend(fl->functionlist, fid, funct);
      
      if(f2->process!=NULL)
	      if(!internal)
		flist_append(fl->procfunctionlist,fid,funct);
              else
		flist_prepend(fl->procfunctionlist, fid, funct);
814
815
      similar=1;
    } 
Arne Øslebø's avatar
Arne Øslebø committed
816
  }
817
#endif
Arne Øslebø's avatar
Arne Øslebø committed
818

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  if(funct_instance==NULL) {
    funct->ref=0;
    //Create new function instance
    funct_instance=malloc(sizeof(mapidflib_function_instance_t));
    funct->instance=funct_instance;
    funct_instance->hwinfo=fl->hwinfo;
    funct_instance->status=MAPIFUNC_UNINIT;
    funct_instance->result.data_size = 0;
    funct_instance->result.info.shm.buf_size = 0;
    funct_instance->result.info.shm.res_size = 0;
    funct_instance->result.info.funct_res_size = 0;
    funct_instance->result.data=NULL;
    funct_instance->internal_data=NULL;
    funct_instance->refcount=0;
    funct_instance->def=malloc(sizeof(mapidflib_function_def_t));
    memcpy(funct_instance->def,f2,sizeof(mapidflib_function_def_t));
    memcpy(funct_instance->args,fargs,FUNCTARGS_BUF_SIZE);
    flow_mod.reorder=fid=i->fcount++;
    flow_mod.identical=0;
    flow_mod.delete=NULL;
    flow_mod.delete_size=0;
    flow_mod.add_funct=&mapid_add_funct;
    flow_mod.mi=i;
    
    
    //Add function to list
845
    if(internal == 1)
846
847
848
	    flist_prepend(fl->functionlist, fid, funct);
    else
	    flist_append(fl->functionlist,fid,funct);
's avatar
   
committed
849
    if(f2->process!=NULL){
850
	    if(internal == 1)
851
852
853
		    flist_prepend(fl->procfunctionlist, fid, funct);
    	    else
		    flist_append(fl->procfunctionlist,fid,funct);
's avatar
   
committed
854
    }
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
    
    //Check arguments
    if(f2->instance!=NULL)
      error=f2->instance(funct->instance,
			 fd,
			 &flow_mod);
  }
    
   
    if(error==MFUNCT_COULD_NOT_APPLY_FUNCT && strcmp(f2->devtype,MAPI_DEVICE_ALL)!=0
       && strchr(function,'!')==NULL) {
      //TODO: Add support for optimization
      error=0;
      //Try software function
      DEBUG_CMD(printf("Trying MAPI_DEVICE_ALL devtype because of failure to instance function [%s:%d]\n",__FILE__,__LINE__));
      f2=mapilh_get_function_def(function,MAPI_DEVICE_ALL);
      
      if(f2==NULL) {
	//Change so that it returns error message to the application
	DEBUG_CMD(printf("ERROR: Could not find/match function %s [%s:%d]\n",function,__FILE__,__LINE__));
	error=MDLIB_FUNCTION_NOT_FOUND;
      } 
      else {   
878
879
	flist_remove(fl->functionlist,fid);
	flist_remove(fl->procfunctionlist,fid);
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
	memcpy(funct_instance->def,f2,sizeof(mapidflib_function_def_t));
	flist_append(fl->functionlist,fid,funct);
	//Check arguments
	if(f2->instance!=NULL)
	  error=f2->instance(funct_instance,
			     fd,
			     &flow_mod);
	
	//Add function to process list
	if(f2->process!=NULL) 
	  flist_append(fl->procfunctionlist,fid,funct);		       
      }
    }
 

Arne Øslebø's avatar
Arne Øslebø committed
895
#ifndef WITH_MODIFY_PKTS
896
  if(funct_instance->def->modifies_pkts==1) 
Arne Øslebø's avatar
Arne Øslebø committed
897
898
899
900
901
902
903
904
905
906
907
    error=MDLIB_NO_MODIFYING;
#endif
  if(error!=0)
  {
    /* function not initialized, error */
    if(fl==NULL)
    {
      return -1;
    }
    else
    {
908
909
      flist_remove(fl->functionlist,fid);
      flist_remove(fl->procfunctionlist,fid);
Arne Øslebø's avatar
Arne Øslebø committed
910
      fl->err_no=error;
911
912
      free(funct_instance->def);
      free(funct_instance);
Arne Øslebø's avatar
Arne Øslebø committed
913
914
915
916
917
      free(funct);
      return -1;
    }
  }

918
919
920
921
922

  if(!similar){
  fl->shm_size+=funct->instance->def->shm_size;
  if(funct->instance->def->restype==MAPIRES_SHM)
    funct->instance->result.data_size=funct->instance->def->shm_size;
Arne Øslebø's avatar
Arne Øslebø committed
923
924
925
926
927
928
929
930
931

  if(flow_mod.reorder!=fid) {
    DEBUG_CMD(printf("REORDER [%s:%d]\n",__FILE__,__LINE__));
    flist_move_before(fl->functionlist,flow_mod.reorder,fid);
    if(f2->process!=NULL)
      flist_move_before(fl->procfunctionlist,flow_mod.reorder,fid);
  }

  //Check if some functions should be deleted
932
  if(flow_mod.delete) {
Arne Øslebø's avatar
Arne Øslebø committed
933
934
    for(c=0;c<flow_mod.delete_size;c++) { 
      DEBUG_CMD(printf("Deleted function [%s:%d]\n",__FILE__,__LINE__));
935
      flist_remove(fl->functionlist,flow_mod.delete[c]);
Arne Øslebø's avatar
Arne Øslebø committed
936
937
938
939
940
    //No need to call cleanup since init has not yet been called  
    }
    free(flow_mod.delete);
  }

941
  
Arne Øslebø's avatar
Arne Øslebø committed
942
  //Copy information to funct_info structure
943
  add_funct_info(fid,fargs,funct->instance,&fl->funct_info_list);
Arne Øslebø's avatar
Arne Øslebø committed
944

945
  DEBUG_CMD(printf("Added function: %s library:%s devtype=%s [%s:%d]\n",funct->instance->def->name,funct->instance->def->libname,funct->instance->def->devtype,__FILE__,__LINE__));
Arne Øslebø's avatar
Arne Øslebø committed
946
947

#ifdef WITH_MODIFY_PKTS
948
  if(funct->instance->def->modifies_pkts==1)
Arne Øslebø's avatar
Arne Øslebø committed
949
950
951
    fl->modifies=1;
#endif

952
953
954
955
956
  strncpy(function,funct->instance->def->devtype,FUNCT_NAME_LENGTH);


  }

Arne Øslebø's avatar
Arne Øslebø committed
957
958
959
960
961
962
963
964

  return fid;
}

int mapid_finished(mapidlib_instance_t *i)
{
  flist_node_t *n;
  struct mapidlibflow *flow;
965
966
967
968
969
970
971
972
973
974
#ifdef WITH_PRIORITIES
  int j = 0;

  for(j = 0; j < PRIORITIES; j++)
  {
	  n = flist_head(i->flowlist[j]);
	  while (n)
      {
      	  flow=flist_data(n);
	      flow->status=FLOW_FINISHED;
975
976
977
	       //Update global function list
		  ((mapid_flow_info_t*)flist_get(flow->hwinfo->gflist->fflist,flow->fd))->status=FLOW_FINISHED;
	      
978
979
980
981
982
983
984
985
986
    	  n = flist_next(n);
	 }
  }
#else
  n = flist_head(i->flowlist);
  while (n)
    {
      flow=flist_data(n);
      flow->status=FLOW_FINISHED;
987
988
989
      //Update global function list
	  ((mapid_flow_info_t*)flist_get(flow->hwinfo->gflist->fflist,flow->fd))->status=FLOW_FINISHED;
      
990
991
992
      n = flist_next(n);
    }
#endif
Arne Øslebø's avatar
Arne Øslebø committed
993

994
/*
Arne Øslebø's avatar
Arne Øslebø committed
995
996
997
998
999
1000
1001
  n = flist_head(i->flowlist);
  while (n)
    {
      flow=flist_data(n);
      flow->status=FLOW_FINISHED;
      n = flist_next(n);
    }
1002
*/
Arne Øslebø's avatar
Arne Øslebø committed
1003
1004
1005
  return 0;
}

's avatar
committed
1006
1007


1008
/*mapi_function_def_mini_t*
's avatar
committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
mapid_get_function_info(int libnumber,int functionnumber)
{
	return mapidflib_get_function_info(libnumber, functionnumber);
}

char*
mapid_get_lib_name(int libnumber)
{
	return mapidflib_get_lib_name(libnumber);
}

1020
*/
1021
#include <netinet/in.h>
1022
void mapid_process_pkt(mapidlib_instance_t *i,
's avatar
committed
1023
1024
		  unsigned char* dev_pkt,
		  unsigned char* link_pkt,
Arne Øslebø's avatar
Arne Øslebø committed
1025
1026
1027
1028
1029
1030
1031
		  mapid_pkthdr_t* pkt_head)		  
//Process a single packet by applying functions to it
//pkt = pointer to packet
//pkt_head = pointer to packet header
{
  flist_node_t *n,*n2;
  struct mapidlibflow *flow;
1032
  mapidflib_function_t *funct;
Arne Øslebø's avatar
Arne Øslebø committed
1033
1034
  char ret;

1035
1036

#ifdef WITH_PRIORITIES
1037
  int j = 0;
1038

1039
1040
1041
  for(j = 0; j < PRIORITIES; j++)
  {
	  n = flist_head(i->flowlist[j]);
1042
#else
1043
  n = flist_head(i->flowlist);
1044
#endif
Arne Øslebø's avatar
Arne Øslebø committed
1045

1046
1047
1048
1049
  // a color var to help trackers from continunsly searching application
  // strings 
  color = 0;
  
1050
1051
  while (n)
    {
Arne Øslebø's avatar
Arne Øslebø committed
1052
      flow=flist_data(n);
1053
	  
Arne Øslebø's avatar
Arne Øslebø committed
1054
1055
      if(flow->status==FLOW_ACTIVE) {
#ifdef WITH_MODIFY_PKTS
1056
		 if(flow->modifies==1) {
's avatar
committed
1057
			 memcpy(flow->mod_pkt, link_pkt, pkt_head->caplen);
1058
1059
			 memcpy(&flow->mod_pkt_head,pkt_head,sizeof(mapid_pkthdr_t));
	  	 }
's avatar
committed
1060

Arne Øslebø's avatar
Arne Øslebø committed
1061
#endif
1062
1063
	n2=flist_head(flow->procfunctionlist);
	ret=1;
Arne Øslebø's avatar
Arne Øslebø committed
1064
	while (n2 && ret!=0 && flow->status==FLOW_ACTIVE) {
1065
1066
1067
1068
	  funct = flist_data(n2);
	  if(funct->ref==1)
	    ret=funct->instance->ret;
	  else {
1069
1070
1071
		  if(funct->instance->def->restype == MAPIRES_SHM) {
			  pthread_spin_lock(i->shm_spinlock);
		  }
Arne Øslebø's avatar
Arne Øslebø committed
1072
#ifdef WITH_MODIFY_PKTS
1073
1074
1075
1076
	    if(flow->modifies==1)
	      ret=funct->instance->def->process(funct->instance,dev_pkt,flow->mod_pkt,&flow->mod_pkt_head);
	    else
	      ret=funct->instance->def->process(funct->instance,dev_pkt,link_pkt,pkt_head);
Arne Øslebø's avatar
Arne Øslebø committed
1077
#else
1078
	    ret=funct->instance->def->process(funct->instance,dev_pkt,link_pkt,pkt_head);
Arne Øslebø's avatar
Arne Øslebø committed
1079
#endif
1080
1081
1082
		if(funct->instance->def->restype == MAPIRES_SHM) {
			pthread_spin_unlock(i->shm_spinlock);
		}
Arne Øslebø's avatar
Arne Øslebø committed
1083
#ifdef WITH_FUNCT_STATS
1084
1085
1086
	    funct->instance->pkts++;
	    if(ret)
	      funct->instance->processed_pkts++;
Arne Øslebø's avatar
Arne Øslebø committed
1087
#endif
1088
1089
	    funct->instance->ret=ret;
	  }
1090
	  n2 = flist_next(n2);	      	
1091
	}
1092
1093
1094
	n = flist_next(n);
      } else if(flow->delete==1) {
	n = flist_next(n);
1095
	delete_flow(i,flow);
1096
      }
Arne Øslebø's avatar
Arne Øslebø committed
1097
    }
1098
#ifdef WITH_PRIORITIES
1099
	}
1100
#endif
Arne Øslebø's avatar
Arne Øslebø committed
1101
1102
1103
1104
1105
}

int mapid_get_errno(mapidlib_instance_t *i,int fid)
 {
   struct mapidlibflow * fl;
1106
1107
1108
1109
//   fl=flist_get(i->flowlist,fid);
#ifdef WITH_PRIORITIES
   fl=flist_priorities_get(i->flowlist,fid);
#else
Arne Øslebø's avatar
Arne Øslebø committed
1110
   fl=flist_get(i->flowlist,fid);
1111
#endif
Arne Øslebø's avatar
Arne Øslebø committed
1112
1113
1114
1115
1116
1117
   if(fl==NULL)
     return MDLIB_INVALID_FLID;
   return fl->err_no;

 }

's avatar
   
committed
1118
static void add_funct_info(int fid, mapiFunctArg *fargs, mapidflib_function_instance_t *f,mapid_funct_info_t **flist)
Arne Øslebø's avatar
Arne Øslebø committed
1119
{
's avatar
committed
1120
1121
  mapid_funct_info_t *fi = (mapid_funct_info_t *)malloc(sizeof(mapid_funct_info_t));
  mapid_funct_info_t *l = *flist;
Arne Øslebø's avatar
Arne Øslebø committed
1122
1123
1124
1125
1126
1127

  fi->fid=fid;
  fi->name=f->def->name;
  fi->libname=f->def->libname;
  fi->devtype=f->def->devtype;
  fi->argdescr=f->def->argdescr;
's avatar
committed
1128
  strncpy((char *)fi->args, (char *)fargs, FUNCTARGS_BUF_SIZE);
Arne Øslebø's avatar
Arne Øslebø committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  fi->pkts=&f->pkts;
  fi->passed_pkts=&f->processed_pkts;
  fi->next=NULL;

  if(*flist==NULL)
    *flist=fi;
  else {
    while(l->next!=NULL)
      l=l->next;
    l->next=fi;
  }
}

int mapid_get_devid(mapidlib_instance_t *i,int fd)
{
1144
1145
1146
1147
//  struct mapidlibflow *flow=flist_get(i->flowlist,fd);
#ifdef WITH_PRIORITIES
  struct mapidlibflow *flow=flist_priorities_get(i->flowlist,fd);
#else
Arne Øslebø's avatar
Arne Øslebø committed
1148
  struct mapidlibflow *flow=flist_get(i->flowlist,fd);
1149
#endif
Arne Øslebø's avatar
Arne Øslebø committed
1150
1151
1152
1153
1154
1155
1156
1157
  return flow->hwinfo->devid;
}

int mapid_load_library(char *lib)
{
  return mapilh_load_library(libpath,lib);
}

1158