예제 #1
0
static void BGPRecord_dealloc(BGPRecordObject *self)
{
  if (self->rec != NULL) {
    bgpstream_record_destroy(self->rec);
  }
  Py_TYPE(self)->tp_free((PyObject *)self);
}
예제 #2
0
/** Clean up all state before exit */
static void clean()
{
  if(record != NULL)
    {
      bgpstream_record_destroy(record);
      record = NULL;
    }

  if(bgpcorsaro != NULL)
    {
      bgpcorsaro_finalize_output(bgpcorsaro);
    }
}
예제 #3
0
/* allocate memory for a bs_record */
bgpstream_record_t *bgpstream_record_create()
{
  bgpstream_debug("BS: create record start");
  bgpstream_record_t *bs_record;
  if ((bs_record = (bgpstream_record_t *)malloc(sizeof(bgpstream_record_t))) ==
      NULL) {
    return NULL; // can't allocate memory
  }

  bs_record->bs = NULL;
  bs_record->bd_entry = NULL;

  if ((bs_record->elem_generator = bgpstream_elem_generator_create()) == NULL) {
    bgpstream_record_destroy(bs_record);
    return NULL;
  }

  bgpstream_record_clear(bs_record);

  bgpstream_debug("BS: create record end");
  return bs_record;
}
예제 #4
0
int main(int argc, char *argv[])
{

  int opt;
  int prevoptind;

  opterr = 0;

  // variables associated with options
  char *projects[PROJECT_CMD_CNT];
  int projects_cnt = 0;

  char *types[TYPE_CMD_CNT];
  int types_cnt = 0;

  char *collectors[COLLECTOR_CMD_CNT];
  int collectors_cnt = 0;

  char *peerasns[PEERASN_CMD_CNT];
  int peerasns_cnt = 0;

  char *prefixes[PREFIX_CMD_CNT];
  int prefixes_cnt = 0;

  char *communities[COMMUNITY_CMD_CNT];
  int communities_cnt = 0;

  struct window windows[WINDOW_CMD_CNT];
  char *endp;
  int windows_cnt = 0;

  char *interface_options[OPTION_CMD_CNT];
  int interface_options_cnt = 0;

  int rib_period = 0;
  int live = 0;
  int output_info = 0;
  int record_output_on = 0;
  int record_bgpdump_output_on = 0;
  int elem_output_on = 0;

  bgpstream_data_interface_option_t *option;

  int i;

  /* required to be created before usage is called */
  bs = bgpstream_create();
  if(!bs) {
    fprintf(stderr, "ERROR: Could not create BGPStream instance\n");
    return -1;
  }
  datasource_id_default = datasource_id = bgpstream_get_data_interface_id(bs);
  datasource_info = bgpstream_get_data_interface_info(bs, datasource_id);
  assert(datasource_id != 0);

  /* allocate memory for bs_record */
  bgpstream_record_t *bs_record = bgpstream_record_create();
  if(bs_record == NULL)
    {
      fprintf(stderr, "ERROR: Could not create BGPStream record\n");
      bgpstream_destroy(bs);
      return -1;
    }

  while (prevoptind = optind,
	 (opt = getopt (argc, argv, "d:o:p:c:t:w:j:k:y:P:lrmeivh?")) >= 0)
    {
      if (optind == prevoptind + 2 && (optarg == NULL || *optarg == '-') ) {
        opt = ':';
        -- optind;
      }
      switch (opt)
	{
	case 'p':
	  if(projects_cnt == PROJECT_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d projects can be specified on "
		      "the command line\n",
		      PROJECT_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  projects[projects_cnt++] = strdup(optarg);
	  break;
	case 'c':
	  if(collectors_cnt == COLLECTOR_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d collectors can be specified on "
		      "the command line\n",
		      COLLECTOR_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  collectors[collectors_cnt++] = strdup(optarg);
	  break;
	case 't':
	  if(types_cnt == TYPE_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d types can be specified on "
		      "the command line\n",
		      TYPE_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  types[types_cnt++] = strdup(optarg);
	  break;
	case 'w':
	  if(windows_cnt == WINDOW_CMD_CNT)
            {
              fprintf(stderr,
                      "ERROR: A maximum of %d windows can be specified on "
                      "the command line\n",
                      WINDOW_CMD_CNT);
              usage();
              exit(-1);
            }
	  /* split the window into a start and end */
	  if((endp = strchr(optarg, ',')) == NULL)
	    {
              windows[windows_cnt].end = BGPSTREAM_FOREVER;
	    }
          else
            {
              *endp = '\0';
              endp++;
              windows[windows_cnt].end =  atoi(endp);
            }
	  windows[windows_cnt].start = atoi(optarg);
	  windows_cnt++;
	  break;
        case 'j':
	  if(peerasns_cnt == PEERASN_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d peer asns can be specified on "
		      "the command line\n",
		      PEERASN_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  peerasns[peerasns_cnt++] = strdup(optarg);
	  break;
        case 'k':
	  if(prefixes_cnt == PREFIX_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d peer asns can be specified on "
		      "the command line\n",
		      PREFIX_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  prefixes[prefixes_cnt++] = strdup(optarg);
	  break;
        case 'y':
	  if(communities_cnt == COMMUNITY_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d communities can be specified on "
		      "the command line\n",
		      PREFIX_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  communities[communities_cnt++] = strdup(optarg);
	  break;
        case 'P':
          rib_period = atoi(optarg);
          break;
	case 'd':
          if((datasource_id =
              bgpstream_get_data_interface_id_by_name(bs, optarg)) == 0)
            {
              fprintf(stderr, "ERROR: Invalid data interface name '%s'\n",
                      optarg);
              usage();
              exit(-1);
            }
          datasource_info =
            bgpstream_get_data_interface_info(bs, datasource_id);
	  break;
        case 'o':
          if(interface_options_cnt == OPTION_CMD_CNT)
	    {
	      fprintf(stderr,
		      "ERROR: A maximum of %d interface options can be specified\n",
		      OPTION_CMD_CNT);
	      usage();
	      exit(-1);
	    }
	  interface_options[interface_options_cnt++] = strdup(optarg);
          break;

	case 'l':
	  live = 1;
	  break;
	case 'r':
	  record_output_on = 1;
	  break;
	case 'm':
	  record_bgpdump_output_on = 1;
	  break;
	case 'e':
	  elem_output_on = 1;
	  break;
        case 'i':
	  output_info = 1;
	  break;
	case ':':
	  fprintf(stderr, "ERROR: Missing option argument for -%c\n", optopt);
	  usage();
	  exit(-1);
	  break;
	case '?':
	case 'v':
	  fprintf(stderr, "bgpreader version %d.%d.%d\n",
		  BGPSTREAM_MAJOR_VERSION,
		  BGPSTREAM_MID_VERSION,
		  BGPSTREAM_MINOR_VERSION);
	  usage();
	  exit(0);
	  break;
	default:
	  usage();
	  exit(-1);
	}
    }

  for(i=0; i<interface_options_cnt; i++)
    {
      if(*interface_options[i] == '?')
        {
          dump_if_options();
          usage();
          exit(0);
        }
      else
        {
          /* actually set this option */
          if((endp = strchr(interface_options[i], ',')) == NULL)
            {
              fprintf(stderr,
                      "ERROR: Malformed data interface option (%s)\n",
                      interface_options[i]);
              fprintf(stderr,
                      "ERROR: Expecting <option-name>,<option-value>\n");
              usage();
              exit(-1);
            }
          *endp = '\0';
          endp++;
          if((option =
              bgpstream_get_data_interface_option_by_name(bs, datasource_id,
                                                          interface_options[i])) == NULL)
            {
              fprintf(stderr,
                      "ERROR: Invalid option '%s' for data interface '%s'\n",
                      interface_options[i], datasource_info->name);
              usage();
              exit(-1);
            }
          bgpstream_set_data_interface_option(bs, option, endp);
        }
      free(interface_options[i]);
      interface_options[i] = NULL;
    }
  interface_options_cnt = 0;

  if(windows_cnt == 0)
    {
      fprintf(stderr,
              "ERROR: At least one time window must be specified using -w\n");
      usage();
      exit(-1);
    }

  /* if the user did not specify any output format
   * then the default one is per elem */
  if(record_output_on == 0 && elem_output_on == 0 && record_bgpdump_output_on == 0) {
    elem_output_on = 1;
  }

  /* the program can now start */

  /* allocate memory for interface */

  /* projects */
  for(i=0; i<projects_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_PROJECT, projects[i]);
      free(projects[i]);
    }

  /* collectors */
  for(i=0; i<collectors_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_COLLECTOR, collectors[i]);
      free(collectors[i]);
    }

  /* types */
  for(i=0; i<types_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_RECORD_TYPE, types[i]);
      free(types[i]);
    }

  /* windows */
  for(i=0; i<windows_cnt; i++)
    {
      bgpstream_add_interval_filter(bs, windows[i].start, windows[i].end);
    }

  /* peer asns */
  for(i=0; i<peerasns_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PEER_ASN, peerasns[i]);
      free(peerasns[i]);
    }

  /* prefixes */
  for(i=0; i<prefixes_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PREFIX, prefixes[i]);
      free(prefixes[i]);
    }

  /* communities */
  for(i=0; i<communities_cnt; i++)
    {
      bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_COMMUNITY, communities[i]);
      free(communities[i]);
    }

  /* frequencies */
  if(rib_period > 0)
    {
      bgpstream_add_rib_period_filter(bs, rib_period);
    }

  /* datasource */
  bgpstream_set_data_interface(bs, datasource_id);

  /* live */
  if(live != 0)
    {
      bgpstream_set_live_mode(bs);
    }

  /* turn on interface */
  if(bgpstream_start(bs) < 0) {
    fprintf(stderr, "ERROR: Could not init BGPStream\n");
    return -1;
  }

  if(output_info)
    {
      if(record_output_on)
        {
          printf(BGPSTREAM_RECORD_OUTPUT_FORMAT);
        }
      if(elem_output_on)
        {
          printf(BGPSTREAM_ELEM_OUTPUT_FORMAT);
        }
    }

  /* use the interface */
  int get_next_ret = 0;
  bgpstream_elem_t * bs_elem;
  do
    {
      get_next_ret = bgpstream_get_next_record(bs, bs_record);
      if(get_next_ret && record_output_on)
	{
	  print_bs_record(bs_record);
	}
      if(get_next_ret && bs_record->status == BGPSTREAM_RECORD_STATUS_VALID_RECORD)
	{
	  if(record_bgpdump_output_on)
	    {
	      bgpstream_record_print_mrt_data(bs_record);
	    }
	  if(elem_output_on)
	    {
              /* check if the record is of type RIB, in case extract the ID */
              if(bs_record->attributes.dump_type == BGPSTREAM_RIB)
                {

                  /* print the RIB start line */
                  if(bs_record->dump_pos == BGPSTREAM_DUMP_START)
                    {
                      print_rib_control_message(bs_record);
                    }
                }

	      while((bs_elem =
                     bgpstream_record_get_next_elem(bs_record)) != NULL)
		{
		  if(print_elem(bs_record, bs_elem) != 0)
                    {
                      goto err;
                    }
		}
              /* check if end of RIB has been reached */
              if(bs_record->attributes.dump_type == BGPSTREAM_RIB &&
                 bs_record->dump_pos == BGPSTREAM_DUMP_END)
                {
                  print_rib_control_message(bs_record);
                }
	    }
	}
  }
  while(get_next_ret > 0);

  /* de-allocate memory for bs_record */
  bgpstream_record_destroy(bs_record);

  /* turn off interface */
  bgpstream_stop(bs);

  /* deallocate memory for interface */
  bgpstream_destroy(bs);

  return 0;

 err:
  bgpstream_record_destroy(bs_record);
  bgpstream_stop(bs);
  bgpstream_destroy(bs);
  return -1;
}