static int process_overlapping_pfx(struct bgpcorsaro_pfxmonitor_state_t *state,
                                   const bgpstream_record_t *bs_record,
                                   const bgpstream_elem_t *elem)
{
  char log_buffer[MAX_LOG_BUFFER_LEN] = "";
  bgpstream_as_path_seg_t *origin_seg = NULL;
  uint32_t origin_asn = 0;

  if (elem->type == BGPSTREAM_ELEM_TYPE_WITHDRAWAL) {
    /* remove pfx/peer from state structure */
    rm_pfx_peer(state, &elem->prefix, elem->peer_asnumber);
  } else /* (announcement or rib) */
  {
    /* get the origin asn (sets and confederations are ignored) */
    origin_seg = bgpstream_as_path_get_origin_seg(elem->aspath);
    if (origin_seg == NULL || origin_seg->type != BGPSTREAM_AS_PATH_SEG_ASN) {
      fprintf(stderr, "WARN: ignoring AS sets and confederations\n");
    } else {
      /* valid origin ASN */
      origin_asn = ((bgpstream_as_path_seg_asn_t *)origin_seg)->asn;
      if (set_pfx_peer_origin(state, &elem->prefix, elem->peer_asnumber,
                              origin_asn) != 0) {
        return -1;
      }
    }
  }

  /* we always print all the info to the log */
  bgpstream_record_elem_snprintf(log_buffer, MAX_LOG_BUFFER_LEN, bs_record,
                                 elem);
  wandio_printf(state->outfile, "%s\n", log_buffer);
  return 0;
}
Example #2
0
char *bgpstream_elem_custom_snprintf(char *buf, size_t len,
                                     bgpstream_elem_t const *elem, int print_type)
{
  assert(elem);

  size_t written = 0; /* < how many bytes we wanted to write */
  size_t c = 0; /* < how many chars were written */
  char *buf_p = buf;

  bgpstream_as_path_seg_t *seg;

  /* common fields */

  /* [message_type|]peer_asn|peer_ip| */

  if(print_type)
    {
      /* MESSAGE TYPE */
      c = bgpstream_elem_type_snprintf(buf_p, B_REMAIN, elem->type);
      written += c;
      buf_p += c;

      if(B_FULL)
        return NULL;

      ADD_PIPE;
    }

  /* PEER ASN */
  c = snprintf(buf_p, B_REMAIN, "%"PRIu32, elem->peer_asnumber);
  written += c;
  buf_p += c;
  ADD_PIPE;

  /* PEER IP */
  if(bgpstream_addr_ntop(buf_p, B_REMAIN, &elem->peer_address) == NULL)
    return NULL;
  SEEK_STR_END;
  ADD_PIPE;

  if(B_FULL)
    return NULL;

  /* conditional fields */
  switch(elem->type)
    {
    case BGPSTREAM_ELEM_TYPE_RIB:
    case BGPSTREAM_ELEM_TYPE_ANNOUNCEMENT:

      /* PREFIX */
      if(bgpstream_pfx_snprintf(buf_p, B_REMAIN,
                                (bgpstream_pfx_t*)&(elem->prefix)) == NULL)
        {
          return NULL;
        }
      SEEK_STR_END;
      ADD_PIPE;

      /* NEXT HOP */
      if(bgpstream_addr_ntop(buf_p, B_REMAIN, &elem->nexthop) == NULL)
        {
          return NULL;
        }
      SEEK_STR_END;
      ADD_PIPE;

      /* AS PATH */
      c = bgpstream_as_path_snprintf(buf_p, B_REMAIN, elem->aspath);
      written += c;
      buf_p += c;

      if(B_FULL)
        return NULL;

      ADD_PIPE;

      /* ORIGIN AS */
      if((seg = bgpstream_as_path_get_origin_seg(elem->aspath)) != NULL)
        {
          c = bgpstream_as_path_seg_snprintf(buf_p, B_REMAIN, seg);
          written += c;
          buf_p += c;
        }

      ADD_PIPE;

      /* COMMUNITIES */
      c = bgpstream_community_set_snprintf(buf_p, B_REMAIN, elem->communities);
      written += c;
      buf_p += c;

      if(B_FULL)
        return NULL;

      ADD_PIPE;

      /* OLD STATE (empty) */
      ADD_PIPE;

      /* NEW STATE (empty) */
      if(B_FULL)
        return NULL;

      /* END OF LINE */
      break;

    case BGPSTREAM_ELEM_TYPE_WITHDRAWAL:

      /* PREFIX */
      if(bgpstream_pfx_snprintf(buf_p, B_REMAIN,
                                (bgpstream_pfx_t*)&(elem->prefix)) == NULL)
        {
          return NULL;
        }
      SEEK_STR_END;
      ADD_PIPE;
      /* NEXT HOP (empty) */
      ADD_PIPE;
      /* AS PATH (empty) */
      ADD_PIPE;
      /* ORIGIN AS (empty) */
      ADD_PIPE;
      /* COMMUNITIES (empty) */
      ADD_PIPE;
      /* OLD STATE (empty) */
      ADD_PIPE;
      /* NEW STATE (empty) */
      if(B_FULL)
        return NULL;
      /* END OF LINE */
      break;

    case BGPSTREAM_ELEM_TYPE_PEERSTATE:

      /* PREFIX (empty) */
      ADD_PIPE;
      /* NEXT HOP (empty) */
      ADD_PIPE;
      /* AS PATH (empty) */
      ADD_PIPE;
      /* ORIGIN AS (empty) */
      ADD_PIPE;
      /* COMMUNITIES (empty) */
      ADD_PIPE;

      /* OLD STATE */
      c = bgpstream_elem_peerstate_snprintf(buf_p, B_REMAIN,
                                            elem->old_state);
      written += c;
      buf_p += c;

      if(B_FULL)
        return NULL;

      ADD_PIPE;

      /* NEW STATE (empty) */
      c = bgpstream_elem_peerstate_snprintf(buf_p, B_REMAIN, elem->new_state);
      written += c;
      buf_p += c;

      if(B_FULL)
        return NULL;
      /* END OF LINE */
      break;

    default:
      fprintf(stderr, "Error during elem processing\n");
      return NULL;
    }

  return buf;
}