コード例 #1
0
ファイル: net.c プロジェクト: gvsurenderreddy/cbgp
/**
 * context: {}
 * tokens: {src-addr, src-iface-id, dst-addr, dst-iface-id}
 */
int cli_net_add_linkptp(cli_ctx_t * ctx, cli_cmd_t * cmd)
{
  const char * arg_src   = cli_get_arg_value(cmd, 0);
  const char * arg_src_if= cli_get_arg_value(cmd, 1);
  const char * arg_dst   = cli_get_arg_value(cmd, 2);
  const char * arg_dst_if= cli_get_arg_value(cmd, 3);
  const char * opt;
  net_node_t * src_node;
  net_node_t * dst_node;
  net_iface_id_t src_if;
  net_iface_id_t dst_if;
  int result;
  net_iface_t * iface;
  net_link_delay_t delay= 0;
  net_link_load_t capacity= 0;

  // Get source node
  if (str2node(arg_src, &src_node)) {
    cli_set_user_error(cli_get(), "could not find node \"%s\"", arg_src);
    return CLI_ERROR_COMMAND_FAILED;
  }

  // Get source address
  if (net_iface_str2id(arg_src_if, &src_if)) {
    cli_set_user_error(cli_get(), "invalid interface id \"%\"", arg_src_if);
    return CLI_ERROR_COMMAND_FAILED;
  }

  // Get destination node
  if (str2node(arg_dst, &dst_node)) {
    cli_set_user_error(cli_get(), "could not find node \"%s\"", arg_dst);
    return CLI_ERROR_COMMAND_FAILED;
  }

  // get destination address
  if (net_iface_str2id(arg_dst_if, &dst_if)) {
    cli_set_user_error(cli_get(), "invalid interface id \"%\"", arg_dst_if);
    return CLI_ERROR_COMMAND_FAILED;
  }

  // Get optional link capacity
  opt= cli_get_opt_value(cmd, "bw");
  if (opt != NULL) {
    if (str2capacity(opt, &capacity)) {
      cli_set_user_error(cli_get(), "invalid link capacity \"%s\"", opt);
      return CLI_ERROR_COMMAND_FAILED;
    }
  }

  // Get optional link delay
  opt= cli_get_opt_value(cmd, "delay");
  if (opt != NULL) {
    if (str2delay(opt, &delay)) {
      cli_set_user_error(cli_get(), "invalid link delay \"%s\"", opt);
      return CLI_ERROR_CMD_FAILED;
    }
  }
  
  // Create link
  result= net_link_create_ptp(src_node, src_if, dst_node, dst_if,
			      BIDIR, &iface);
  if (result != ESUCCESS) {
    cli_set_user_error(cli_get(), "could not create ptp link "
		       "from %s (%s) to %s (%s) (%s)",
		       arg_src, arg_src_if, arg_dst, arg_dst_if,
		       network_strerror(result));
    return CLI_ERROR_COMMAND_FAILED;
  }

  result= net_link_set_phys_attr(iface, delay, capacity, BIDIR);
  if (result != ESUCCESS) {
    cli_set_user_error(cli_get(), "could not set physical attributes (%s)",
		       network_strerror(result));
    return CLI_ERROR_COMMAND_FAILED;
  }

  return CLI_SUCCESS;
}
コード例 #2
0
ファイル: specreaderimpl.cpp プロジェクト: magcius/apngasm
      // Read parameter from spec file.
      // Return true if read succeeded.
      bool XMLSpecReaderImpl::read(const std::string& filePath)
      {
        // Read XML file.
        boost::property_tree::ptree root;
        boost::property_tree::read_xml(filePath, root);

        // Set current directory.
        const boost::filesystem::path oldPath = boost::filesystem::current_path();
        const boost::filesystem::path currentPath = boost::filesystem::path(filePath);
        if(currentPath.has_parent_path())
          boost::filesystem::current_path(currentPath.parent_path());

        // Read fields.
        // name
        if( boost::optional<std::string> value = root.get_optional<std::string>("animation.<xmlattr>.name") )
        {
          _name = value.get();
        }

        // loops
        if( boost::optional<unsigned int> value = root.get_optional<unsigned int>("animation.<xmlattr>.loops") )
        {
          _loops = value.get();
        }

        // skip_first
        if( boost::optional<bool> value = root.get_optional<bool>("animation.<xmlattr>.skip_first") )
        {
          _skipFirst = value.get();
        }

        // delay
        Delay defaultDelay = { DEFAULT_FRAME_NUMERATOR, DEFAULT_FRAME_DENOMINATOR };
        if( boost::optional<std::string> value = root.get_optional<std::string>("animation.<xmlattr>.default_delay") )
        {
          if( !str2delay(value.get(), &defaultDelay) )
          {
            defaultDelay.num = DEFAULT_FRAME_NUMERATOR;
            defaultDelay.den = DEFAULT_FRAME_DENOMINATOR;
          }
        }

        // frames
        if( boost::optional<boost::property_tree::ptree&> child = root.get_child_optional("animation") )
        {
          BOOST_FOREACH(const boost::property_tree::ptree::value_type& current, child.get())
          {
            std::string file;
            Delay delay;
            const boost::property_tree::ptree& frame = current.second;

            // filepath
            if( boost::optional<std::string> value = frame.get_optional<std::string>("<xmlattr>.src") )
            {
              file = value.get();
            }
            if(file.empty())
              continue;

            // delay
            if( boost::optional<std::string> value = frame.get_optional<std::string>("<xmlattr>.delay") )
            {
              if( !str2delay(value.get(), &delay) )
                delay = defaultDelay;
            }
            else
            {
              delay = defaultDelay;
            }

            // Add frame informations.
            const FrameInfo frameInfo = { boost::filesystem::absolute(file).string(), delay };
            _frameInfos.push_back(frameInfo);
          }
        }
コード例 #3
0
ファイル: net.c プロジェクト: gvsurenderreddy/cbgp
/**
 * context: {}
 * tokens: {addr-src, prefix-dst}
 * options: [--depth=, --capacity=, delay=]
 */
int cli_net_add_link(cli_ctx_t * ctx, cli_cmd_t * cmd)
{
  const char * arg_src= cli_get_arg_value(cmd, 0);
  const char * arg_dst= cli_get_arg_value(cmd, 1);
  const char * opt;
  net_node_t * src_node;
  net_node_t * dst_node;
  ip_dest_t dest;
  int result;
  uint8_t depth= 1;
  net_link_delay_t delay= 0;
  net_link_load_t capacity= 0;
  net_iface_dir_t dir;
  net_iface_t * iface;
  net_subnet_t * subnet;

  // Get optional link depth
  opt= cli_get_opt_value(cmd, "depth");
  if (opt != NULL) {
    if (str2depth(opt, &depth)) {
      cli_set_user_error(cli_get(), "invalid link depth \"%s\"", opt);
      return CLI_ERROR_COMMAND_FAILED;
    }
  }
  
  // Get optional link capacity
  opt= cli_get_opt_value(cmd, "bw");
  if (opt != NULL) {
    if (str2capacity(opt, &capacity)) {
      cli_set_user_error(cli_get(), "invalid link capacity \"%s\"", opt);
      return CLI_ERROR_COMMAND_FAILED;
    }
  }

  // Get optional link delay
  opt= cli_get_opt_value(cmd, "delay");
  if (opt != NULL) {
    if (str2delay(opt, &delay)) {
      cli_set_user_error(cli_get(), "invalid link delay \"%s\"", opt);
      return CLI_ERROR_CMD_FAILED;
    }
  }

  // Get source node
  if (str2node(arg_src, &src_node)) {
    cli_set_user_error(cli_get(), "could not find node \"%s\"", arg_src);
    return CLI_ERROR_COMMAND_FAILED;
  }
  
  // Get destination: can be a node / a subnet
  if ((ip_string_to_dest(arg_dst, &dest) < 0) ||
      (dest.type == NET_DEST_ANY)) {
    cli_set_user_error(cli_get(), "invalid destination \"%s\".", arg_dst);
    return CLI_ERROR_COMMAND_FAILED;
  }
  
  // Add link: RTR (to node) / PTMP (to subnet)
  if (dest.type == NET_DEST_ADDRESS) {
    dst_node= network_find_node(network_get_default(), dest.addr);
    if (dst_node == NULL) {
      cli_set_user_error(cli_get(), "tail-end \"%s\" does not exist.",
			 arg_dst);
      return CLI_ERROR_COMMAND_FAILED;
    }
    dir= BIDIR;
    result= net_link_create_rtr(src_node, dst_node, dir, &iface);
  } else {
    subnet= network_find_subnet(network_get_default(), dest.prefix);
    if (subnet == NULL) {
      cli_set_user_error(cli_get(), "tail-end \"%s\" does not exist.",
			 arg_dst);
      return CLI_ERROR_COMMAND_FAILED;
    }
    dir= UNIDIR;
    result= net_link_create_ptmp(src_node, subnet,
				 dest.prefix.network,
				 &iface);
  }
  if (result != ESUCCESS) {
    cli_set_user_error(cli_get(), "could not add link %s -> %s (%s)",
		       arg_src, arg_dst, network_strerror(result));
    return CLI_ERROR_COMMAND_FAILED;
  }

  result= net_link_set_phys_attr(iface, delay, capacity, dir);
  if (result != ESUCCESS) {
    cli_set_user_error(cli_get(), "could not set physical attributes (%s)",
		       network_strerror(result));
    return CLI_ERROR_COMMAND_FAILED;
  }  

  return CLI_SUCCESS;
}
コード例 #4
0
ファイル: rexford.c プロジェクト: gvsurenderreddy/cbgp
/**
 * Load an AS-level topology in the Subramanian et al format. The
 * file format is as follows. Each line describes a directed
 * relationship between a two ASes:
 *   <AS1-number> <AS2-number> <peering-type>
 * where peering type can be
 *   0 for a peer-to-peer relationship
 *   1 for a provider (AS1) to customer (AS2) relationship
 * A relationship among two ASes is described in one direction only.
 */
int rexford_parser(FILE * file, as_level_topo_t * topo,
		   int * line_number)
{
  char line[80];
  asn_t asn1, asn2;
  unsigned int relation;
  int error= 0;
  net_link_delay_t delay;
  gds_tokenizer_t * tokenizer;
  const gds_tokens_t * tokens;
  as_level_domain_t * domain1, * domain2;
  peer_type_t peer_type;

  *line_number= 0;

  // Parse input file
  tokenizer= tokenizer_create(" \t", NULL, NULL);
  
  while ((!feof(file)) && (!error)) {
    if (fgets(line, sizeof(line), file) == NULL)
      break;
    (*line_number)++;
    
    // Skip comments starting with '#'
    if (line[0] == '#')
      continue;
    
    if (tokenizer_run(tokenizer, line) != TOKENIZER_SUCCESS) {
      error= ASLEVEL_ERROR_UNEXPECTED;
      break;
    }
    
    tokens= tokenizer_get_tokens(tokenizer);
    
    // Set default value for optional parameters
    delay= 0;
    
    // Get and check mandatory parameters
    if (tokens_get_num(tokens) < 3) {
      error= ASLEVEL_ERROR_NUM_PARAMS;
      break;
    }

    // Get and check ASNs
    if (str2asn(tokens_get_string_at(tokens, 0), &asn1) ||
	str2asn(tokens_get_string_at(tokens, 1), &asn2)) {
      error= ASLEVEL_ERROR_INVALID_ASNUM;
      break;
    }
    
    // Get and check business relationship
    if ((tokens_get_uint_at(tokens, 2, &relation) != 0) ||
	(_rexford_relation_to_peer_type(relation, &peer_type) != 0)) {
      error= ASLEVEL_ERROR_INVALID_RELATION;
      break;
    }
    
    // Get optional parameters
    if (tokens_get_num(tokens) > 3) {
      if (str2delay(tokens_get_string_at(tokens, 3), &delay)) {
	error= ASLEVEL_ERROR_INVALID_DELAY;
	break;
      }
    }
    
    // Limit number of parameters
    if (tokens_get_num(tokens) > 4) {
      STREAM_ERR(STREAM_LEVEL_SEVERE,
	      "Error: too many arguments in topology, line %u\n",
	      *line_number);
      error= ASLEVEL_ERROR_NUM_PARAMS;
      break;
    }
    
    // Add/get domain 1
    if (!(domain1= aslevel_topo_get_as(topo, asn1)) &&
	!(domain1= aslevel_topo_add_as(topo, asn1))) {
      error= ASLEVEL_ERROR_UNEXPECTED;
      break;
    }
    
    // Add/get domain 2
    if (!(domain2= aslevel_topo_get_as(topo, asn2)) &&
	!(domain2= aslevel_topo_add_as(topo, asn2))) {
      error= ASLEVEL_ERROR_UNEXPECTED;
      break;
    }
    
    // Add link in both directions
    error= aslevel_as_add_link(domain1, domain2, peer_type, NULL);
    if (error != ASLEVEL_SUCCESS)
      break;
    peer_type= aslevel_reverse_relation(peer_type);
    error= aslevel_as_add_link(domain2, domain1, peer_type, NULL);
    if (error != ASLEVEL_SUCCESS)
      break;
    
  }

  tokenizer_destroy(&tokenizer);
  return error;
}