f-util.c 2.05 KB
Newer Older
1 2 3 4
/*
 *	Filters: utility functions
 *
 *	Copyright 1998 Pavel Machek <pavel@ucw.cz>
5
 *		  2017 Jan Maria Matejka <mq@ucw.cz>
6 7 8 9 10 11 12 13
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"

14 15
#define P(a,b) ((a<<8) | b)

16
struct f_inst *
17
f_new_inst(enum f_instruction_code fi_code)
18
{
19
  struct f_inst * ret;
20 21
  ret = cfg_allocz(sizeof(struct f_inst));
  ret->fi_code = fi_code;
22
  ret->lineno = ifs->lino;
23 24 25
  return ret;
}

26
struct f_inst *
27
f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
28
{
29 30 31 32 33 34 35 36 37 38 39 40 41 42
  struct f_inst *ret = f_new_inst(fi_code);
  ret->aux = da.type;
  ret->a2.i = da.ea_code;
  return ret;
}

struct f_inst *
f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa)
{
  struct f_inst *ret = f_new_inst(fi_code);
  ret->aux = sa.f_type;
  ret->a2.i = sa.sa_code;
  ret->a1.i = sa.readonly;
  return ret;
43 44
}

45 46 47 48
/*
 * Generate set_dynamic( operation( get_dynamic(), argument ) )
 */
struct f_inst *
49
f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
50
{
51 52 53
  struct f_inst *set_dyn = f_new_inst_da(FI_EA_SET, da),
                *oper = f_new_inst(operation),
                *get_dyn = f_new_inst_da(FI_EA_GET, da);
54 55 56 57

  oper->aux = operation_aux;
  oper->a1.p = get_dyn;
  oper->a2.p = argument;
58

59 60 61 62
  set_dyn->a1.p = oper;
  return set_dyn;
}

63 64 65 66 67

struct f_inst *
f_generate_roa_check(struct symbol *sym, struct f_inst *prefix, struct f_inst *asn)
{
  struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
68
  ret->i.fi_code = FI_ROA_CHECK;
69
  ret->i.lineno = ifs->lino;
70 71 72 73 74 75 76 77 78 79 80
  ret->i.arg1 = prefix;
  ret->i.arg2 = asn;
  /* prefix == NULL <-> asn == NULL */

  if ((sym->class != SYM_ROA) || ! sym->def)
    cf_error("%s is not a ROA table", sym->name);
  ret->rtc = sym->def;

  return &ret->i;
}

81 82 83 84 85 86 87
char *
filter_name(struct filter *filter)
{
  if (!filter)
    return "ACCEPT";
  else if (filter == FILTER_REJECT)
    return "REJECT";
88 89
  else if (!filter->name)
    return "(unnamed)";
90 91 92
  else
    return filter->name;
}