example.c 2.17 KB
Newer Older
's avatar
committed
1 2 3 4 5 6 7 8
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include "mapi.h"

9 10
#define BUFSIZE 10000

's avatar
committed
11
static char errstr[520];
12
static char filterpath[BUFSIZE];
's avatar
committed
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

static void dump_packet( FILE *f, const unsigned char *buf, unsigned int sz )
{
    unsigned int i;
    int in_string = 0;

    for( i=0; i<sz; i++ ){
	unsigned char c = buf[i];

	if( c>=' ' && c<0x7f ){
	    if( !in_string ){
		fputs( "\" ", f );
		in_string = 1;
	    }
	    fputc( c, f );
	}
	else {
	    if( in_string ){
		fputs( "\" ", f );
		in_string = 0;
	    }
	    fprintf( f, "%02X ", c );
	}
    }
    if( in_string ){
        fputc( '"', f );
    }
    fputs( "\n", f );
    fflush( f );
}

static void callback( const struct mapipkt *pkt )
{
    dump_packet( stdout, &pkt->pkt, pkt->caplen );
}

int main( int argc, char **argv )
{
    int res;
    char *interface = "eth0";
's avatar
committed
53
    int errcode;
's avatar
committed
54 55 56 57 58

    int fd = mapi_create_flow( interface );
    //int fd = mapi_create_offline_flow( "tests/tracefile", MFF_PCAP );
    if( fd<0 ){
        fprintf( stderr, "Could not create a MAPI flow for interface `%s'\n", interface );
's avatar
committed
59 60
        mapi_read_error( &errcode, errstr );
        fprintf( stderr, "Error: %d: %s\n", errcode, errstr );
's avatar
committed
61 62 63
        exit( EXIT_FAILURE );
    }

64 65 66 67
    getcwd( filterpath, BUFSIZE );
    strcat( filterpath, "/filter.rl" );

    int sid = mapi_apply_function( fd, "RULER", filterpath );
's avatar
committed
68
    if( sid<0 ){
's avatar
committed
69 70 71
        fprintf( stderr, "Could not apply function RULER: error code %d\n", sid );
        mapi_read_error( &errcode, errstr );
        fprintf( stderr, "Error: %d: %s\n", errcode, errstr );
's avatar
committed
72 73 74
        exit( EXIT_FAILURE );
    }

75
    int bufid = mapi_apply_function( fd, "TO_BUFFER", WAIT );
's avatar
committed
76
    if( bufid<0 ){
's avatar
committed
77 78 79
        fprintf( stderr, "Could not apply function TO_BUFFER\n" );
        mapi_read_error( &errcode, errstr );
        fprintf( stderr, "Error: %d: %s\n", errcode, errstr );
's avatar
committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        exit( EXIT_FAILURE );
    }

    res = mapi_connect( fd );
    if( res<0 ){
        fprintf( stderr, "Could not connect to flow %d\n", fd );
        exit( EXIT_FAILURE );
    }

    mapi_loop( fd, bufid, 50, &callback );

    mapi_close_flow( fd );

    return EXIT_SUCCESS;
}