int main (int argc, char *argv[])
{
    char          opt;             /* short options: character */
    char          optstr[] = "hp:c:P:r:stuvlf:";
    int           port;            /* port number */
    int           cport    = 4740; /* target collector port number */
    int           protocol = IPFIX_PROTO_TCP;
    int           do_log = 0; /* flag to stre whether logging to stdout was enabled */

    /** set default options
     */
    port    = 4739;
    datadir = NULL;
    snprintf( progname, sizeof(progname), "%s", basename( argv[0]) );

    /* --- command line parsing ---
     */
    int cisset = 0;
    while( ( opt = getopt( argc, argv, optstr ) ) != EOF ) {

        switch ( opt )
        {
          case 'o':
              if ((sourceid=atoi(optarg)) <0) {
                  fprintf( stderr, "Invalid -o argument!\n" );
                  exit(1);
              }
              break;

          case 'p':
              if ((port=atoi(optarg)) <0) {
                  fprintf( stderr, "Invalid -p argument!\n" );
                  exit(1);
              }
              break;

          case 'v':
              verbose_level ++;
              break;

          case 'l':
              do_log = 1;
              break;

          case 'P':
              if ((cport=atoi(optarg)) <0) {
                fprintf( stderr, "Invalid -P argument!\n" );
                exit(1);
              }
              break;

          case 'r':
              if ((degrade=atoi(optarg)) <0) {
                fprintf( stderr, "Invalid -r argument!\n" );
                exit(1);
              }
              break;

          case 'c':
              if (chost)
                  free(chost); /* executed in case of multiple "-c <collectorhost>" */
              chost = strdup(optarg);
              cisset = 1;
              break;

          case 'f':
              if (datadir) 
                  free(datadir); /* executed in case of multiple "-f <datadir>" */
              datadir = strdup(optarg);
              break;

          case 's':
              protocol = IPFIX_PROTO_SCTP;
              break;

          case 't':
              protocol = IPFIX_PROTO_TCP;
              break;

          case 'u':
              protocol = IPFIX_PROTO_UDP;
              break;

          case 'h':
          default:
              usage(progname);
              exit(1);
        }
    }

    if (!chost && !datadir && !do_log) {
        fprintf( stderr, "You must specify either an IPFIX target collector host or a datadir or enable log to stdout. Type option '-h' for help.\n");
        exit(1);
    }

    /** init hashtable structures */
    hi_init_uint32_t(&indegree,  1000);
    hi_init_uint32_t(&outdegree, 1000);
    hi_init_uint32_t(&flows,     1000);
    hi_init_str     (&templates, 100);

    /** init logging
     */
    mlog_set_vlevel( verbose_level );

    /** init ipfix lib
     */
    if ( ipfix_init() <0 ) {
        fprintf( stderr, "ipfix_init() failed: %s\n", strerror(errno) );
        exit(1);
    }
    if ( ipfix_add_vendor_information_elements( ipfix_ft_fokus ) <0 ) {
        fprintf( stderr, "ipfix_add_ie() failed: %s\n", strerror(errno) );
        exit(1);
    }
 
    /** init ipfix import/export 
     */
    if ( ipfix_open( &ipfixh, sourceid, IPFIX_VERSION ) <0 ) {
        fprintf( stderr, "ipfix_open() failed: %s\n", strerror(errno) );
        exit(1);
    }

    /** signal handler
     */
    signal( SIGKILL, exit_func );
    signal( SIGTERM, exit_func );
    signal( SIGINT,  exit_func );

    /** initialize callback methods
     */

    /** activate stdout log output
     *  if "-l" was specified on cmd line
     */
    if (do_log) {
        (void) ipfix_col_start_msglog( stdout );
    }

    /** activate file export
     *  if "-f <datadir>" was specified on cmd line
     */
    if (datadir) {
        (void) ipfix_col_init_fileexport( datadir );
    }

    if (chost) {
        if ( ipfix_add_collector( ipfixh, chost, cport, protocol ) <0 ) {
            fprintf( stderr, "ipfix_add_collector(%s,%d) failed: %s\n",
                     chost, cport, strerror(errno));
            exit(1);
        }

        /** activate callback for re-export
         */
        if ( (g_colinfo=calloc( 1, sizeof(ipfix_col_info_t))) ==NULL) {
          fprintf( stderr, "a calloc failed while initializing callback methods.\n" );
          return -1;
        }

        g_colinfo->export_newsource = export_newsource_cb;
        g_colinfo->export_newmsg    = export_newmsg_cb;
        g_colinfo->export_trecord   = export_trecord_cb;
        g_colinfo->export_drecord   = export_drecord_cb;
        g_colinfo->export_cleanup   = export_cleanup_cb;
        g_colinfo->data = NULL;

        if ( ipfix_col_register_export( g_colinfo ) <0 ) {
            fprintf( stderr, "ipfix_col_register_export() failed: %s\n", strerror(errno) );
            exit(1);
        }
    }

    /** open ipfix collector port(s)
     */
    if ( ipfix_col_listen( &ntcp_s, &tcp_s, IPFIX_PROTO_TCP, 
                           port, AF_INET, 10 ) <0 ) {
        fprintf( stderr, "[%s] ipfix_listen(tcp) failed.\n",
                 progname );
        return -1;
    }

    /** event loop
     */
    (void) mpoll_loop( -1 );

    exit(1);
}
Exemplo n.º 2
0
/* name       : export_init
 */
static int export_init( probe_t *probe )
{
    ipfix_t          *ifh =NULL;
    ipfix_template_t *t4, *t6;
    char             *p, *host;
    int              port;

    if ( (ipfix_init() <0)
         || (ipfix_add_vendor_information_elements( ipfix_ft_fokus ) <0 ) ) {
        mlogf( 0, "[%s] cannot init ipfix module\n", __func__ );
        return -1;
    }

    if ( ipfix_open( &ifh, g_par.odid, IPFIX_VERSION ) <0 ) {
        mlogf( 0, "[%s] ipfix_open() failed: %s\n", 
               __func__, strerror(errno) );
        ipfix_cleanup();
        return -1;
    }

    for ( host=g_par.collector, port=g_par.port;; ) {
        /* check if hostname has :portno suffix
         */
        if ( ((p=strrchr( host, ':' )) !=NULL) 
             && (p==strchr( host, ':' )) && ((port=atoi(p+1))>0) ) {
            *p = '\0';
        }

        if ( !port )
            port = IPFIX_PORTNO;

#ifdef SSLSUPPORT
        if ( g_par.ssl ) {
            ipfix_ssl_opts_t opts;

            opts.cafile  = g_par.cafile;
            opts.cadir   = g_par.cadir;
            opts.keyfile = g_par.keyfile;
            opts.certfile= g_par.certfile;

            if ( ipfix_add_collector_ssl( ifh, host, port,
                                          g_par.protocol, &opts ) <0 ) {
                mlogf( 0, "[%s] ipfix_add_collector_ssl() failed: %s\n",
                       __func__, strerror(errno) );
                goto err;
            }
            break;
        }
#endif
        if ( ipfix_add_collector( ifh, host, port, g_par.protocol ) <0 ) {
            mlogf( 0, "[%s] ipfix_add_collector() failed: %s\n",
                   __func__, strerror(errno) );
            goto err;
        }
        break;
    }

    if ( g_par.biflows ) {
        if ( ipfix_make_template( ifh, &t4,
                                  ipbiflow4_fields,
                                  ipbiflow4_nfields ) <0) {
            goto err;
        }

        if ( ipfix_make_template( ifh, &t6,
                                  ipbiflow6_fields,
                                  ipbiflow6_nfields ) <0) {
            ipfix_delete_template( ifh, t4 );
            goto err;
        }
    }
    else {
        if ( ipfix_make_template( ifh, &t4,
                                  ipflow4_fields,
                                  ipflow4_nfields ) <0) {
            goto err;
        }

        if ( ipfix_make_template( ifh, &t6,
                                  ipflow6_fields,
                                  ipflow6_nfields ) <0) {
            ipfix_delete_template( ifh, t4 );
            goto err;
        }
    }

    probe->ipfix  = ifh;
    probe->templ  = t4;
    probe->templ6 = t6;
    return 0;

err:
    ipfix_close( ifh );
    ipfix_cleanup();
    return -1;
}
Exemplo n.º 3
0
int main (int argc, char *argv[])
{
    int           port;         /* port number */
    char          *dbuser;      /* db username */
    char          *dbpw;        /* db password */
    char          *dbname;      /* db name     */
    char          *dbhost;      /* hostname    */

    char          arg;          /* short options: character */
    char          opt[] = "hp:v";


    /** set default options
     */
    port    = 4739;
    dbhost  = MYSQL_HOST;
    dbname  = MYSQL_DBNAME;
    dbuser  = MYSQL_USER;
    dbpw    = MYSQL_PASSWORD;

    snprintf( progname, sizeof(progname), "%s", basename( argv[0]) );

    /* --- command line parsing ---
     */
    while( (arg=getopt( argc, argv, opt )) != EOF )
    {
        switch (arg) 
        {
          case 'p':
              if ((port=atoi(optarg)) <0)
              {
                  fprintf( stderr, "invalid -p argument!\n" );
                  exit(1);
              }
              break;

          case 'v':
              verbose_level ++;
              break;

          case 'h':
          default:
              usage(progname);
              exit(1);
        }
    }

    /** init loggin
     */
    mlog_set_vlevel( verbose_level );

    /** signal handler
     */
    signal( SIGKILL, exit_func );
    signal( SIGTERM, exit_func );
    signal( SIGINT,  exit_func );

    /** init ipfix lib
     */
    if ( ipfix_init() <0 ) {
        fprintf( stderr, "ipfix_init() failed: %s\n", strerror(errno) );
        exit(1);
    }
    if ( ipfix_add_vendor_information_elements( ipfix_ft_fokus ) <0 ) {
        fprintf( stderr, "ipfix_add_ie() failed: %s\n", strerror(errno) );
        ipfix_cleanup();
        exit(1);
    }

    /** activate database export
     */
    if ( ipfix_col_init_mysqlexport( dbhost, dbuser,
                                     dbpw, dbname ) <0 ) {
        fprintf( stderr, "cannot connect to database\n" );
        ipfix_cleanup();
        exit(1);
    }

    /** open ipfix collector port(s)
     */
    if ( ipfix_col_listen( &ntcp_s, &tcp_s, IPFIX_PROTO_TCP, 
                           port, AF_INET, 10 ) <0 ) {
        fprintf( stderr, "[%s] ipfix_listen(tcp) failed.\n",
                 progname );
        return -1;
    }

    /** event loop
     */
    (void) mpoll_loop( -1 );

    exit(1);
}
Exemplo n.º 4
0
/*
 * Class:     de_fraunhofer_fokus_ants_jipfix_jIPFIX
 * Method:    ipfixInit
 * Signature: ()B
 */
JNIEXPORT jbyte JNICALL Java_de_fraunhofer_fokus_ants_jipfix_jIPFIX_ipfixInit(JNIEnv * env, jobject self) {
  return ipfix_init();
  checkException(env);
}
Exemplo n.º 5
0
int main (int argc, char *argv[])
{
    char          arg;          /* short options: character */
    int           loptidx=0;    /* long options: arg==0 and index */
    char          opt[] = "64stuhl:p:vo:f:";
#ifdef HAVE_GETOPT_LONG
    struct option lopt[] = { 
        { "dbhost", 1, 0, 0},
        { "dbname", 1, 0, 0},
        { "dbuser", 1, 0, 0},
        { "dbpw", 1, 0, 0},
        { "db", 0, 0, 0},
        { "ssl", 0, 0, 0},
        { "key", 1, 0, 0},
        { "cert", 1, 0, 0},
        { "cafile", 1, 0, 0},
        { "cadir", 1, 0, 0},
        { "help", 0, 0, 0},
        { 0, 0, 0, 0 } 
    };
#endif

    /** set default options
     */
    par.tcp     = 0;
    par.udp     = 0;
    par.sctp    = 0;
    par.ssl     = 0;
    par.cafile  = CAFILE;
    par.cadir   = CADIR;
    par.keyfile = KEYFILE;
    par.certfile= CERTFILE;
    par.port    = 0;
    par.family  = AF_UNSPEC;
    par.logfile = NULL;
    par.maxcon  = 10;
    par.datadir  = NULL;
    par.datafile = NULL;
    par.dbexport = 0;
    par.dbhost   = DFLT_MYSQL_HOST;
    par.dbname   = DFLT_MYSQL_DBNAME;
    par.dbuser   = DFLT_MYSQL_USER;
    par.dbpw     = DFLT_MYSQL_PASSWORD;

    snprintf( par.progname, sizeof(par.progname), "%s", basename( argv[0]) );

    /* --- command line parsing ---
     */
#ifdef HAVE_GETOPT_LONG
    while ((arg=getopt_long( argc, argv, opt, lopt, &loptidx)) >=0 )
#else
    while( (arg=getopt( argc, argv, opt )) != EOF )
#endif
    {
	switch (arg) 
        {
          case 0: 
              switch (loptidx) {
                case 0: /* dbhost */
                    par.dbhost = optarg;
                    break;
                case 1: /* dbname */
                    par.dbname = optarg;
                    break;
                case 2: /* dbuser */
                    par.dbuser = optarg;
                    break;
                case 3: /* dbpw */
                    par.dbpw = optarg;
                    break;
                case 4: /* db */
                    par.dbexport = 1;
                    break;
                case 5: /* ssl */
                    par.ssl = 1;
                    break;
                case 6: /* key */
                    par.keyfile = optarg;
                    break;
                case 7: /* cert */
                    par.certfile = optarg;
                    break;
                case 8: /* cafile */
                    par.cafile = optarg;
                    break;
                case 9: /* cadir */
                    par.cadir = optarg;
                    break;
                case 10:
                    usage(par.progname);
                    exit(1);
              }
              break;

          case '4':
#ifdef INET6
              par.family = (par.family==AF_INET6)? AF_UNSPEC : AF_INET;
              break;

          case '6':
              par.family = (par.family==AF_INET)? AF_UNSPEC : AF_INET6;
#endif
              break;

          case 'd':
              par.dbexport = 1;
              break;

          case 'l':
              par.logfile = optarg;
              break;

          case 's':
              par.sctp ++;
              break;

          case 't':
              par.tcp ++;
              break;

          case 'u':
              par.udp ++;
              break;

          case 'o':
              par.datadir = optarg;
              if ( access( optarg, W_OK|X_OK ) <0 ) {
                  fprintf( stderr, "cannot access dir '%s': %s!\n",
                           optarg, strerror(errno) );
                  exit(1);
              }
              break;

          case 'f':
            par.datafile = optarg;
            break;

          case 'p':
              if ((par.port=atoi(optarg)) <0)
              {
                  fprintf( stderr, "Invalid -p argument!\n" );
                  exit(1);
              }
              break;

          case 'v':
              verbose_level ++;
              break;

          case 'h':
          default:
              usage(par.progname);
              exit(1);
        }
    }

    if ( !par.udp && !par.tcp && !par.sctp )
        par.tcp++;

    if ( !par.dbexport && !par.datadir ) {
        fprintf( stderr, "info: message dump, no data storage.\n" );
        fflush( stderr );
    }

    if ( par.port==0 ) {
        par.port = par.ssl?IPFIX_TLS_PORTNO:IPFIX_PORTNO;
    }

    /** init loggin
     */
    mlog_set_vlevel( verbose_level );
    if ( par.logfile )
        (void) mlog_open( par.logfile, NULL );
    if ( (!par.dbexport && !par.datadir)
         || (verbose_level >2) )
        (void) ipfix_col_start_msglog( stderr );

    mlogf( 1, "[%s] listen on port %d, write to %s ...\n",
           par.progname, par.port,
           par.dbexport?"database":par.datadir?"files":"stdout" );

    /** init ipfix lib
     */
    if ( ipfix_init() <0 ) {
        fprintf( stderr, "ipfix_init() failed: %s\n", strerror(errno) );
        exit(1);
    }
    if ( ipfix_add_vendor_information_elements( ipfix_ft_fokus ) <0 ) {
        fprintf( stderr, "ipfix_add_ie() failed: %s\n", strerror(errno) );
        exit(1);
    }

    /** signal handler
    signal( SIGSEGV, sig_func );
     */
    signal( SIGKILL, sig_func );
    signal( SIGTERM, sig_func );
    signal( SIGINT,  sig_func );

    /** do the work
     */
    if ( do_collect() <0 )
        exit_func(1);

    exit_func(0);
    return 0;
}