interface.c 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include "mapidflib.h"
#include "mapidlib.h"
#include "mapidevices.h"
#include "mapid.h"
#include "fhelp.h"
#include "mapi_errors.h"

typedef struct {
  int ifindex;
} interface_instance_t;

static int interface_instance(mapidflib_function_instance_t *instance,
                              MAPI_UNUSED int fd,
                              MAPI_UNUSED mapidflib_flow_mod_t *flow_mod)
{
	mapiFunctArg* fargs;
	int ifindex;
	int interface_f;

	fargs = instance->args;

	ifindex = getargint(&fargs);

	/* refuse: bad port specification */
	if(ifindex < 0)
		return MFUNCT_INVALID_ARGUMENT;

	/* refuse: the same function was already applied to this flow */
	fhlp_filtering_funct_is_in_list_but_INTERFACE(instance->hwinfo->gflist, fd, &interface_f);
	if (interface_f == INTERFACE_FUNCT_EXISTS)
		return MFUNCT_COULD_NOT_APPLY_FUNCT;

	return 0;
}

static int interface_init(mapidflib_function_instance_t *instance,
                          MAPI_UNUSED int fd)
{
	interface_instance_t *internal_data_ptr;
	mapiFunctArg* fargs;
	int ifindex;

	if((instance->internal_data = malloc(sizeof(interface_instance_t))) == NULL) {
		fprintf(stderr, "interface_init(): could not allocate internal data.\n");
		return MAPID_MEM_ALLOCATION_ERROR;
	}

	internal_data_ptr = (interface_instance_t *) (instance->internal_data);

	fargs = instance->args;
	ifindex = getargint(&fargs);

	internal_data_ptr->ifindex = ifindex;

	return 0;
}

static int interface_process(mapidflib_function_instance_t *instance,
	unsigned char* dev_pkt,
	MAPI_UNUSED unsigned char* link_pkt,
	MAPI_UNUSED mapid_pkthdr_t* pkt_head)  
{
	return 1;
	//return 0;
}

static int interface_cleanup(mapidflib_function_instance_t* instance)
{
	if (instance->internal_data != NULL) {
		free(instance->internal_data);
		instance->internal_data = NULL;
	}
	return 0;
}

static mapidflib_function_def_t finfo={
	"", //libname
	"INTERFACE", //name
	"Filters packets from specific interfaces on an adapter", //descr
	"i", //argdescr
	MAPI_DEVICE_DAG, //devtype
	MAPIRES_NONE, //Method for returning results
	0, //shm size
	0, //modifies_pkts
	1, //filters packets
	MAPIOPT_AUTO,
	interface_instance, //instance
	interface_init, //init
	interface_process, //process
	NULL, //get_result,
	NULL, //reset
	interface_cleanup, //cleanup
	NULL, //client_init
	NULL, //client_read_result
	NULL  //client_cleanup
};

mapidflib_function_def_t* interface_get_funct_info();
mapidflib_function_def_t* interface_get_funct_info() {
	return &finfo;
};