コード例 #1
0
ファイル: instruction_test.c プロジェクト: ederlf/horse
void write_actions(void **state)
{
    struct flow *fl = flow_new();
    struct instruction_set is;
    instruction_set_init(&is);
    set_eth_type(fl, 0x800);
    struct write_actions wa;
    struct action_set as;
    struct action gen_act;
    action_set_init(&as);
    action_output(&gen_act, 2);
    action_set_add(&as, gen_act);
    inst_write_actions(&wa, as);
    add_write_actions(&is, wa);
    flow_add_instructions(fl, is);
    assert_int_equal(fl->insts.active, INSTRUCTION_WRITE_ACTIONS);
    unsigned int actions_num;
    actions_num = HASH_COUNT(fl->insts.write_act.actions.actions);
    assert_int_equal(actions_num, 1);
    flow_destroy(fl);
}
コード例 #2
0
ファイル: instruction_test.c プロジェクト: ederlf/horse
void apply_actions(void **state)
{
    struct flow *fl = flow_new();
    struct instruction_set is;
    instruction_set_init(&is);
    set_eth_type(fl, 0x800);
    struct apply_actions aa;
    struct action_list al;
    struct action gen_act;
    action_list_init(&al);
    action_output(&gen_act, 2);
    action_list_add(&al, gen_act);
    action_set_field_u16(&gen_act, SET_IP_PROTO, 6);
    action_list_add(&al, gen_act);
    inst_apply_actions(&aa, al);
    add_apply_actions(&is, aa);
    flow_add_instructions(fl, is);
    assert_int_equal(fl->insts.active, INSTRUCTION_APPLY_ACTIONS);
    int count = 0;
    struct action_list_elem *elem;
    LL_COUNT(fl->insts.apply_act.actions.actions, elem, count);
    assert_int_equal(count, 2);
    flow_destroy(fl);
}
コード例 #3
0
static int set_general(struct sc_stream* stream, char* stream_str)
{
  int rc = 0;
  uint8_t mac[6];
  char* key;
  char* next_field = stream_str;

  /* General format is series of key=value pairs, separated by ",". */
  while( next_field && (rc == 0) ) {
    char* value;
    char* field;

    field = strsep(&next_field, ",");

    /* Split key and value */
    value = field;
    key = strsep(&value, "=");

    if( !value ) {
      /* Handle some key-only magic values */
      if( !strcmp(key, "all") )
        rc = sc_stream_all(stream);
      /* The following needs a strncmp because we pass the stream as
       * 'sniff [0,1]' */
      else if( !strncmp(key, "sniff", strlen("sniff")) )
        rc = sc_stream_sniff(stream, key);
      else if( !strcmp(key, "ip") )
        rc = set_eth_type(stream, key);
      else if( !strcmp(key, "udp") || !strcmp(key, "tcp") )
        rc = set_protocol(stream, key);
      else {
        fprintf(stderr, "%s: ERROR: No value for key %s\n", __func__, key);
        return -EINVAL;
      }
    }
    else {
      if( !strcmp(key, "dmac") ) {
        if( parse_mac(value, mac) < 0 ) {
          fprintf(stderr, "%s: ERROR: Failed to parse mac \"%s\"\n",
                  __func__, key);
          return -EINVAL;
        }
        rc = sc_stream_eth_dhost(stream, mac);
      }
      else if( !strcmp(key, "smac") ) {
        if( parse_mac(value, mac) < 0 )
          fprintf(stderr, "%s: ERROR: Failed to parse mac \"%s\"\n",
                  __func__, key);
        return -EINVAL;
        rc = sc_stream_eth_shost(stream, mac);
      }
      else if( !strcmp(key, "vid") ) {
        rc = set_vlan_id(stream, value);
      }
      else if( !strcmp(key, "eth_type") ) {
        rc = set_eth_type(stream, value);
      }
      else if( !strcmp(key, "shost") ) {
        rc = sc_stream_ip_source_host(stream, value);
      }
      else if( !strcmp(key, "dhost") ) {
        rc = sc_stream_ip_dest_host(stream, value);
      }
      else if( !strcmp(key, "ip_protocol") ) {
        rc = set_protocol(stream, value);
      }
      else if( !strcmp(key, "sport") ) {
        rc = sc_stream_ip_source_port(stream, value);
      }
      else if( !strcmp(key, "dport") ) {
        rc = sc_stream_ip_dest_port(stream, value);
      }
      else {
        fprintf(stderr, "%s: ERROR: Unrecognised key \"%s\"\n", __func__, key);
        return -EINVAL;
      }
    }
  }

  return rc;
}