Example #1
0
static inline u16
convert_state(unsigned state)
{
  /* Convert state from our BS_* values to values used in MRTDump */
  return (state == BS_CLOSE) ? 1 : state + 1;
}

void
mrt_dump_bgp_state_change(struct bgp_conn *conn, unsigned old, unsigned new)
{
  byte buf[128];
  byte *bp = buf + MRTDUMP_HDR_LENGTH;

  bp = mrt_put_bgp4_hdr(bp, conn, 1);
  put_u16(bp+0, convert_state(old));
  put_u16(bp+2, convert_state(new));
  bp += 4;
  mrt_dump_message(&conn->bgp->p, BGP4MP, BGP4MP_STATE_CHANGE_AS4, buf, bp-buf);
}

static byte *
bgp_create_notification(struct bgp_conn *conn, byte *buf)
{
  struct bgp_proto *p = conn->bgp;

  BGP_TRACE(D_PACKETS, "Sending NOTIFICATION(code=%d.%d)", conn->notify_code, conn->notify_subcode);
  buf[0] = conn->notify_code;
  buf[1] = conn->notify_subcode;
  memcpy(buf+2, conn->notify_data, conn->notify_size);
  return buf + 2 + conn->notify_size;
Example #2
0
int main() {
   struct pf_nattrack_hash *pfnth = NULL;
   struct pf_nattrack_list *item, *item2;
   struct pf_nattrack_list *lastlist = NULL, *freelist;
   struct pf_nattrack node, *nodep;
   int i, dev;

   initialize();

   dev = open("/dev/pf", O_RDWR);
   if (dev < 0) {
      printerror("open(/dev/pf)");
      return 1;
   }

   do {
      //printf("\n\n===================================\n");
      //printf("Nova rodada\n");
      //printf("===================================\n");

      freelist = lastlist;
      lastlist = NULL;
      
      struct pfioc_states ps;
      struct pfsync_state *p;
      char *inbuf = NULL, *newinbuf = NULL;
      unsigned int len = 0;
      int i, opts = 0;

      memset(&ps, 0, sizeof(ps));
      for (;;) {
         ps.ps_len = len;
         if (len) {
            newinbuf = realloc(inbuf, len);
            if (newinbuf == NULL) {
               printerror("error realloc - out of memory?");
               goto done;
            }
            ps.ps_buf = inbuf = newinbuf;
         }
         if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
            printerror("failed to get states from PF device");
            goto done;
         }
         if (ps.ps_len + sizeof(struct pfioc_states) < len)
            break;
         if (len == 0 && ps.ps_len == 0)
            goto done;
         if (len == 0 && ps.ps_len != 0)
            len = ps.ps_len;
         if (ps.ps_len == 0)
            goto done;	/* no states */
         len *= 2;
      }
      p = ps.ps_states;
      for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
         if (!convert_state(p, &node)) continue;

         pfnth = &pfnt_hash[hashkey(&node)];

         item = lfind(pfnth->list, &node);

         if (item) {
            //printf("Item found! Deleting from freelist\n");
            item2 = item->ref;
            *(item2->nt) = node;
            ldel(&freelist, item2);
         } else {
            //printf("Not found. Inserting...\n");
            nodep = (struct pf_nattrack *)malloc(sizeof(struct pf_nattrack));
            *nodep = node;
            item = (struct pf_nattrack_list *)malloc(
                  sizeof(struct pf_nattrack_list));
            item->nt = nodep;
            item2 = (struct pf_nattrack_list *)malloc(
                  sizeof(struct pf_nattrack_list));
            item2->nt = nodep;
            ladd(&pfnth->list, item);
            item->ref = item2;
         }
         ladd(&lastlist, item2);
         item2->ref = item;
      }
done:
      free(inbuf);
      free_list(&freelist);

      sleep(PFTM_INTERVAL);
   } while(1);
      /* comentando para trabalhar com o get_states
      while ( scanf("\n%d", &i) != EOF && i != 0) {
         if (!read_input(&node)) continue;

         pfnth = &pfnt_hash[hashkey(&node)];

         item = lfind(pfnth->list, &node);

         if (item) {
            //printf("Item found! Deleting from freelist\n");
            item2 = item->ref;
            ldel(&freelist, item2);
         } else {
            //printf("Not found. Inserting...\n");
            nodep = (struct pf_nattrack *)malloc(sizeof(struct pf_nattrack));
            *nodep = node;
            item = (struct pf_nattrack_list *)malloc(
                  sizeof(struct pf_nattrack_list));
            item->nt = nodep;
            item2 = (struct pf_nattrack_list *)malloc(
                  sizeof(struct pf_nattrack_list));
            item2->nt = nodep;
            ladd(&pfnth->list, item);
            item->ref = item2;
         }
         ladd(&lastlist, item2);
         item2->ref = item;
      }
      //printf("done\n");
      //printf("-> removendo itens da freelist\n");
      free_list(&freelist);
      //printf("-> items armazenados:\n");
      //for(i=0; i <= pf_hashmask; i++) {
      //   for(item=pfnt_hash[i].list; item; item=item->next) {
      //      print_nattrack(item->nt, 0);
      //   }
      //}

      //printf("Nova rodada? (1 = sim) ");
   } while(scanf("\n%d", &i) != EOF && i != 0);
   */ // comentando para get_states

   free_list(&lastlist);
   free(pfnt_hash);

   return 0;
}