int securesoho_copy(const char *src, const char *dst){ char dirname[256], basename[256]; DIR *sdir; struct dirent *sdirent; if(!src || !dst) { fprintf(stderr, "%s:%d The src:0x%p or dst:%p is NULL\n", __FUNCTION__, __LINE__, src, dst); return -1; } if (path_split(src, dirname, basename) < 0){ fprintf(stderr, "%s:%d, Failed to get the dirname and basename\n", __FUNCTION__, __LINE__); return -1; } if (is_wildcard(dirname)){ fprintf(stderr, "%s:%d Don't support wildcard for directory\n", __FUNCTION__, __LINE__); return -1; } //FIXME: to make simple, we just consider following situaltion //The source file are wildcard string //The dest are directory if (!is_wildcard(src)){ if (is_dir(dst)){ char fullpath[512]; sprintf(fullpath, "%s/%s", dst, basename); return file_copy(src, fullpath); }else{ return file_copy(src, dst); } } sdir = opendir(dirname); if(NULL == sdir) { fprintf(stderr, "%s:%d Failed to open dir:%s\n", __FUNCTION__, __LINE__, dirname); return -1; } while((sdirent = readdir(sdir)) != NULL){ if (is_dir(sdirent->d_name)) { continue; } if (wild_match(basename, sdirent->d_name)){ char sfile[512], dfile[512]; sprintf(dfile, "%s/%s", dst, sdirent->d_name); sprintf(sfile, "%s/%s", dirname, sdirent->d_name); file_copy(sfile, dfile); } } if(sdir) closedir(sdir); return 0; }
void reorder_units( std::vector<unsigned int>& units, const unsigned int& idx, unsigned int& stride) { if (units[idx] != root_.num_units()) { return; } const UnitSpecies& usp(root_.at(idx)); units[idx] = stride; ++stride; for (UnitSpecies::container_type::const_iterator i(usp.begin()); i != usp.end(); ++i) { if ((*i).second.second == "" || is_wildcard((*i).second.second)) { continue; } // const std::vector<unit_species_comparerator::site_type>& // pair((*connections_.find((*i).second.second)).second); const std::vector<unit_species_comparerator::site_type>& pair(connections_[(*i).second.second]); const unit_species_comparerator::site_type& tgt((pair[0].first == idx && pair[0].second == (*i).first)? pair[1] : pair[0]); reorder_units(units, tgt.first, stride); } }
string concretize (url u) { // This routine transforms a resolved url into a system file name. // In the case of distant files from the web, a local copy is created. if (is_rooted (u, "default") || is_rooted (u, "file") || is_rooted (u, "blank")) return as_string (reroot (u, "default")); if (is_rooted_web (u)) return concretize (get_from_web (u)); if (is_rooted_tmfs (u)) return concretize (get_from_server (u)); if (is_ramdisc (u)) return concretize (get_from_ramdisc (u)); if (is_here (u)) return as_string (url_pwd ()); if (is_parent (u)) return as_string (url_pwd () * url_parent ()); if (is_wildcard (u, 1)) return u->t[1]->label; std_warning << "Couldn't concretize " << u->t << LF; // failed_error << "u= " << u << LF; // FAILED ("url has no root"); return "xxx"; }
void initialize() { connections_.clear(); for (index_type idx(0); idx < root_.num_units(); ++idx) { const UnitSpecies usp(root_.at(idx)); for (UnitSpecies::container_type::const_iterator i(usp.begin()); i != usp.end(); ++i) { if ((*i).second.second == "" || is_wildcard((*i).second.second)) { continue; } if (connections_.find((*i).second.second) == connections_.end()) { connections_.insert(std::make_pair( (*i).second.second, std::vector<site_type>())); } connections_[(*i).second.second].push_back( std::make_pair(idx, (*i).first)); } } }
BOOST_AUTO_TEST_SUITE(mpls_bos_test) BOOST_DATA_TEST_CASE( no_mask_create_success_test , bdata::make(std::vector<std::uint8_t>{0x00, 0x01}) , value) { auto const sut = match::mpls_bos::create(value); BOOST_TEST(sut.oxm_length() == sizeof(std::uint8_t)); BOOST_TEST(sut.oxm_value() == value); BOOST_TEST(!sut.oxm_hasmask()); BOOST_TEST(sut.oxm_mask() == boost::none); BOOST_TEST(sut.length() == sizeof(std::uint32_t) + sizeof(std::uint8_t)); BOOST_TEST(!sut.is_wildcard()); BOOST_TEST(sut.is_exact()); } BOOST_DATA_TEST_CASE( has_mask_create_success_test , bdata::make(std::vector<std::uint8_t>{0x00, 0x01}) ^ bdata::make(std::vector<std::uint8_t>{0x01, 0x0f}) , value, mask) { auto const sut = match::mpls_bos::create(value, mask); BOOST_TEST(sut.oxm_length() == 2 * sizeof(std::uint8_t)); BOOST_TEST(sut.oxm_value() == value); BOOST_TEST_REQUIRE(sut.oxm_hasmask()); BOOST_TEST(*sut.oxm_mask() == mask);
static int bnxt_hwrm_cfa_flow_alloc(struct bnxt *bp, struct bnxt_tc_flow *flow, __le16 ref_flow_handle, __le32 tunnel_handle, __le16 *flow_handle) { struct hwrm_cfa_flow_alloc_output *resp = bp->hwrm_cmd_resp_addr; struct bnxt_tc_actions *actions = &flow->actions; struct bnxt_tc_l3_key *l3_mask = &flow->l3_mask; struct bnxt_tc_l3_key *l3_key = &flow->l3_key; struct hwrm_cfa_flow_alloc_input req = { 0 }; u16 flow_flags = 0, action_flags = 0; int rc; bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_FLOW_ALLOC, -1, -1); req.src_fid = cpu_to_le16(flow->src_fid); req.ref_flow_handle = ref_flow_handle; if (actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_DECAP || actions->flags & BNXT_TC_ACTION_FLAG_TUNNEL_ENCAP) { req.tunnel_handle = tunnel_handle; flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_TUNNEL; action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_TUNNEL; } req.ethertype = flow->l2_key.ether_type; req.ip_proto = flow->l4_key.ip_proto; if (flow->flags & BNXT_TC_FLOW_FLAGS_ETH_ADDRS) { memcpy(req.dmac, flow->l2_key.dmac, ETH_ALEN); memcpy(req.smac, flow->l2_key.smac, ETH_ALEN); } if (flow->l2_key.num_vlans > 0) { flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_NUM_VLAN_ONE; /* FW expects the inner_vlan_tci value to be set * in outer_vlan_tci when num_vlans is 1 (which is * always the case in TC.) */ req.outer_vlan_tci = flow->l2_key.inner_vlan_tci; } /* If all IP and L4 fields are wildcarded then this is an L2 flow */ if (is_wildcard(l3_mask, sizeof(*l3_mask)) && is_wildcard(&flow->l4_mask, sizeof(flow->l4_mask))) { flow_flags |= CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_L2; } else { flow_flags |= flow->l2_key.ether_type == htons(ETH_P_IP) ? CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV4 : CFA_FLOW_ALLOC_REQ_FLAGS_FLOWTYPE_IPV6; if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV4_ADDRS) { req.ip_dst[0] = l3_key->ipv4.daddr.s_addr; req.ip_dst_mask_len = inet_mask_len(l3_mask->ipv4.daddr.s_addr); req.ip_src[0] = l3_key->ipv4.saddr.s_addr; req.ip_src_mask_len = inet_mask_len(l3_mask->ipv4.saddr.s_addr); } else if (flow->flags & BNXT_TC_FLOW_FLAGS_IPV6_ADDRS) { memcpy(req.ip_dst, l3_key->ipv6.daddr.s6_addr32, sizeof(req.ip_dst)); req.ip_dst_mask_len = ipv6_mask_len(&l3_mask->ipv6.daddr); memcpy(req.ip_src, l3_key->ipv6.saddr.s6_addr32, sizeof(req.ip_src)); req.ip_src_mask_len = ipv6_mask_len(&l3_mask->ipv6.saddr); } } if (flow->flags & BNXT_TC_FLOW_FLAGS_PORTS) { req.l4_src_port = flow->l4_key.ports.sport; req.l4_src_port_mask = flow->l4_mask.ports.sport; req.l4_dst_port = flow->l4_key.ports.dport; req.l4_dst_port_mask = flow->l4_mask.ports.dport; } else if (flow->flags & BNXT_TC_FLOW_FLAGS_ICMP) { /* l4 ports serve as type/code when ip_proto is ICMP */ req.l4_src_port = htons(flow->l4_key.icmp.type); req.l4_src_port_mask = htons(flow->l4_mask.icmp.type); req.l4_dst_port = htons(flow->l4_key.icmp.code); req.l4_dst_port_mask = htons(flow->l4_mask.icmp.code); } req.flags = cpu_to_le16(flow_flags); if (actions->flags & BNXT_TC_ACTION_FLAG_DROP) { action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_DROP; } else { if (actions->flags & BNXT_TC_ACTION_FLAG_FWD) { action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_FWD; req.dst_fid = cpu_to_le16(actions->dst_fid); } if (actions->flags & BNXT_TC_ACTION_FLAG_PUSH_VLAN) { action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE; req.l2_rewrite_vlan_tpid = actions->push_vlan_tpid; req.l2_rewrite_vlan_tci = actions->push_vlan_tci; memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN); memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN); } if (actions->flags & BNXT_TC_ACTION_FLAG_POP_VLAN) { action_flags |= CFA_FLOW_ALLOC_REQ_ACTION_FLAGS_L2_HEADER_REWRITE; /* Rewrite config with tpid = 0 implies vlan pop */ req.l2_rewrite_vlan_tpid = 0; memcpy(&req.l2_rewrite_dmac, &req.dmac, ETH_ALEN); memcpy(&req.l2_rewrite_smac, &req.smac, ETH_ALEN); } } req.action_flags = cpu_to_le16(action_flags); mutex_lock(&bp->hwrm_cmd_lock); rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); if (!rc) *flow_handle = resp->flow_handle; mutex_unlock(&bp->hwrm_cmd_lock); if (rc == HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR) rc = -ENOSPC; else if (rc) rc = -EIO; return rc; }
Species format_species(const Species& sp) { unit_species_comparerator comp(sp); std::vector<unit_species_comparerator::index_type> units; for (unit_species_comparerator::index_type i(0); i < sp.num_units(); ++i) { units.push_back(i); } std::sort(units.begin(), units.end(), comp); std::vector<unit_species_comparerator::index_type> next(sp.num_units(), sp.num_units()); unsigned int stride(0); for (unit_species_comparerator::index_type i(0); i < sp.num_units(); ++i) { const unit_species_comparerator::index_type idx(units[i]); comp.reorder_units(next, idx, stride); } for (unsigned int i(0); i < sp.num_units(); ++i) { units[next[i]] = i; } Species newsp; utils::get_mapper_mf<std::string, std::string>::type cache; stride = 1; std::stringstream ss; for (std::vector<unit_species_comparerator::index_type>::const_iterator i(units.begin()); i != units.end(); ++i) { UnitSpecies usp(sp.at(*i)); for (UnitSpecies::container_type::size_type j(0); j < static_cast<UnitSpecies::container_type::size_type>(usp.num_sites()); ++j) { UnitSpecies::container_type::value_type& site(usp.at(j)); if (site.second.second == "" || is_wildcard(site.second.second)) { continue; } utils::get_mapper_mf<std::string, std::string>::type::const_iterator it(cache.find(site.second.second)); if (it == cache.end()) { ss << stride; cache.insert(std::make_pair(site.second.second, ss.str())); site.second.second = ss.str(); ++stride; ss.clear(); ss.str(""); } else { site.second.second = (*it).second; } } newsp.add_unit(usp); } return newsp; }
/* * Add a client to the tree. */ int client_add(RADCLIENT_LIST *clients, RADCLIENT *client) { RADCLIENT *old; char buffer[INET6_ADDRSTRLEN + 3]; if (!client) { return 0; } /* * Hack to fixup wildcard clients */ if (is_wildcard(&client->ipaddr)) client->ipaddr.prefix = 0; fr_ntop(buffer, sizeof(buffer), &client->ipaddr); DEBUG3("Adding client %s (%s) to prefix tree %i", buffer, client->longname, client->ipaddr.prefix); /* * If "clients" is NULL, it means add to the global list. */ if (!clients) { /* * Initialize it, if not done already. */ if (!root_clients) { root_clients = clients_init(NULL); if (!root_clients) return 0; } clients = root_clients; } /* * Create a tree for it. */ if (!clients->trees[client->ipaddr.prefix]) { clients->trees[client->ipaddr.prefix] = rbtree_create(client_ipaddr_cmp, NULL, 0); if (!clients->trees[client->ipaddr.prefix]) { return 0; } } #define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0))) /* * Cannot insert the same client twice. */ old = rbtree_finddata(clients->trees[client->ipaddr.prefix], client); if (old) { /* * If it's a complete duplicate, then free the new * one, and return "OK". */ if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) && (old->ipaddr.prefix == client->ipaddr.prefix) && namecmp(longname) && namecmp(secret) && namecmp(shortname) && namecmp(nas_type) && namecmp(login) && namecmp(password) && namecmp(server) && #ifdef WITH_DYNAMIC_CLIENTS (old->lifetime == client->lifetime) && namecmp(client_server) && #endif #ifdef WITH_COA namecmp(coa_name) && (old->coa_server == client->coa_server) && (old->coa_pool == client->coa_pool) && #endif (old->message_authenticator == client->message_authenticator)) { WARN("Ignoring duplicate client %s", client->longname); client_free(client); return 1; } ERROR("Failed to add duplicate client %s", client->shortname); return 0; } #undef namecmp /* * Other error adding client: likely is fatal. */ if (!rbtree_insert(clients->trees[client->ipaddr.prefix], client)) { return 0; } #ifdef WITH_STATS if (!tree_num) { tree_num = rbtree_create(client_num_cmp, NULL, 0); } #ifdef WITH_DYNAMIC_CLIENTS /* * More catching of clients added by rlm_sql. * * The sql modules sets the dynamic flag BEFORE calling * us. The client_from_request() function sets it AFTER * calling us. */ if (client->dynamic && (client->lifetime == 0)) { RADCLIENT *network; /* * If there IS an enclosing network, * inherit the lifetime from it. */ network = client_find(clients, &client->ipaddr, client->proto); if (network) { client->lifetime = network->lifetime; } } #endif client->number = tree_num_max; tree_num_max++; if (tree_num) rbtree_insert(tree_num, client); #endif if (client->ipaddr.prefix < clients->min_prefix) { clients->min_prefix = client->ipaddr.prefix; } (void) talloc_steal(clients, client); /* reparent it */ return 1; }
url complete (url base, url u, string filter, bool flag) { // cout << "complete " << base << " |||| " << u << LF; if (!is_rooted(u)) { if (is_none (base)) return base; if (is_none (u)) return u; if ((!is_root (base)) && (!is_rooted_name (base))) { failed_error << "base= " << base << LF; FAILED ("invalid base url"); } } if (is_name (u) || (is_concat (u) && is_root (u[1]) && is_name (u[2]))) { url comp= base * u; if (is_rooted (comp, "default") || is_rooted (comp, "file")) { if (is_of_type (comp, filter)) return reroot (u, "default"); return url_none (); } if (is_rooted_web (comp) || is_rooted_tmfs (comp) || is_ramdisc (comp)) { if (is_of_type (comp, filter)) return u; return url_none (); } failed_error << "base= " << base << LF; failed_error << "u= " << u << LF; ASSERT (is_rooted (comp), "unrooted url"); FAILED ("bad protocol in url"); } if (is_root (u)) { // FIXME: test filter flags here return u; } if (is_concat (u) && is_wildcard (u[1], 0) && is_wildcard (u[2], 1)) { // FIXME: ret= ret | ... is unefficient (quadratic) in main loop if (!(is_rooted (base, "default") || is_rooted (base, "file"))) { failed_error << "base= " << base << LF; FAILED ("wildcards only implemented for files"); } url ret= url_none (); bool error_flag; array<string> dir= read_directory (base, error_flag); int i, n= N(dir); for (i=0; i<n; i++) { if ((!is_none (ret)) && flag) return ret; if ((dir[i] == ".") || (dir[i] == "..")) continue; if (starts (dir[i], "http://") || starts (dir[i], "ftp://")) if (is_directory (base * dir[i])) continue; ret= ret | (dir[i] * complete (base * dir[i], u, filter, flag)); if (match_wildcard (dir[i], u[2][1]->t->label)) ret= ret | complete (base, dir[i], filter, flag); } return ret; } if (is_concat (u)) { url sub= complete (base, u[1], "", false); // "" should often be faster than the more correct "d" here return complete (base, sub, u[2], filter, flag); } if (is_or (u)) { url res1= complete (base, u[1], filter, flag); if ((!is_none (res1)) && flag) return res1; return res1 | complete (base, u[2], filter, flag); } if (is_wildcard (u)) { // FIXME: ret= ret | ... is unefficient (quadratic) in main loop if (!(is_rooted (base, "default") || is_rooted (base, "file"))) { failed_error << "base= " << base << LF; FAILED ("wildcards only implemented for files"); } url ret= url_none (); if (is_wildcard (u, 0) && is_of_type (base, filter)) ret= url_here (); bool error_flag; array<string> dir= read_directory (base, error_flag); int i, n= N(dir); for (i=0; i<n; i++) { if ((!is_none (ret)) && flag) return ret; if ((dir[i] == ".") || (dir[i] == "..")) continue; if (starts (dir[i], "http://") || starts (dir[i], "ftp://")) if (is_directory (base * dir[i])) continue; if (is_wildcard (u, 0)) ret= ret | (dir[i] * complete (base * dir[i], u, filter, flag)); else if (match_wildcard (dir[i], u[1]->t->label)) ret= ret | complete (base, dir[i], filter, flag); } return ret; } failed_error << "url= " << u << LF; FAILED ("bad url"); return u; }
string as_string (url u, int type) { // This routine pritty prints an url as a string. // FIXME: the current algorithm is quadratic in time. if (is_none (u)) return "{}"; if (is_atomic (u)) return u->t->label; if (is_concat (u)) { int stype= type; if (is_root (u[1]) && (!is_root (u[1], "default"))) stype= URL_STANDARD; string sep= (stype==URL_SYSTEM? string (URL_CONCATER): string ("/")); string s1 = as_string (u[1], type); string s2 = as_string (u[2], stype); if (is_root (u[1], "default")) s1= ""; if ((!is_name (u[1])) && (!is_root (u[1]))) s1= "{" * s1 * "}"; if ((!is_concat (u[2])) && (!is_atomic (u[2])) && (!is_wildcard (u[2], 1))) s2= "{" * s2 * "}"; #ifdef WINPATHS if (((is_root (u[1],"default") && type == URL_SYSTEM) || is_root (u[1],"file"))) { // have to return the windows format string root,remain; if (is_concat (u[2])) { root = as_string (u[2][1], type); // root might be unit letter or hostname. It depends on the length remain = as_string (u[2][2], type); } else { root = s2; remain = ""; } if (is_root (u[1],"default")) { if (N(root) == 1) return root * ":\\" * remain; //drive letter else return "\\\\" * root * "\\" * remain; } else { if (N(root) == 1) return s1 * "/" * root * ":/" * remain; //local file else return s1 * root * "/" * remain; //remote } } #endif return s1 * sep * s2; } if (is_or (u)) { string s1= as_string (u[1], type); string s2= as_string (u[2], type); if (!is_name_in_path (u[1])) s1= "{" * s1 * "}"; if ((!is_or (u[2])) && (!is_name_in_path (u[2]))) s2= "{" * s2 * "}"; #ifdef WINPATHS if (type == URL_STANDARD) return s1 * ":" * s2; else return s1 * string (URL_SEPARATOR) * s2; #else return s1 * string (URL_SEPARATOR) * s2; #endif } #ifdef WINPATHS if (is_root (u, "default")) { int stype= type; if (is_root (u[1]) && (!is_root (u[1], "default"))) stype= URL_STANDARD; if (stype == URL_SYSTEM) return ""; else return "/"; } #else if (is_root (u, "default")) return "/"; #endif if (is_root (u, "blank")) return "/"; if (is_root (u, "file")) return u[1]->t->label * "://"; if (is_root (u)) return u[1]->t->label * ":/"; if (is_wildcard (u, 0)) return "**"; if (is_wildcard (u, 1)) return u->t[1]->label; FAILED ("bad url"); return ""; }