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);
}
示例#2
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;
}