Exemplo n.º 1
0
/** read lock entry */
static void read_lock(rbtree_t* all, FILE* in, int val)
{
	struct order_id prev_id, now_id;
	struct lock_ref* ref;
	struct order_lock* prev, *now;
	ref = (struct lock_ref*)calloc(1, sizeof(struct lock_ref));
	if(!ref) fatal_exit("malloc failure");
	prev_id.thr = val;
	if(fread(&prev_id.instance, sizeof(int), 1, in) != 1 ||	
	   fread(&now_id.thr, sizeof(int), 1, in) != 1 ||	
	   fread(&now_id.instance, sizeof(int), 1, in) != 1 ||	
	   !readup_str(&ref->file, in) ||
	   fread(&ref->line, sizeof(int), 1, in) != 1)
		fatal_exit("fread failed");
	if(verb) printf("read lock %u %u %u %u %s %d\n", 
		(unsigned)prev_id.thr, (unsigned)prev_id.instance,
		(unsigned)now_id.thr, (unsigned)now_id.instance,
		ref->file, ref->line);
	/* find the two locks involved */
	prev = (struct order_lock*)rbtree_search(all, &prev_id);
	now = (struct order_lock*)rbtree_search(all, &now_id);
	/* if not there - insert 'em */
	if(!prev) prev = insert_lock(all, &prev_id);
	if(!now) now = insert_lock(all, &now_id);
	ref->lock = prev;
	ref->node.key = &prev->id;
	if(!rbtree_insert(now->smaller, &ref->node)) {
		free(ref->file);
		free(ref);
	}
}
Exemplo n.º 2
0
RB_NODE *
rbtree_search(RB_TREE *T, RB_NODE *x, int k)
{
    if (x == T->nil || k == x->key)
        return(x);
    if (k < x->key)
        return(rbtree_search(T, x->left, k));
    else
        return(rbtree_search(T, x->right, k));
}
Exemplo n.º 3
0
static struct diff_xfrpart*
diff_read_find_part(struct diff_zone* zp, uint32_t seq_nr)
{
	struct diff_xfrpart* xp = (struct diff_xfrpart*)
		rbtree_search(zp->parts, &seq_nr);
	return xp;
}
Exemplo n.º 4
0
unsigned getzonestatid(nsd_options_t* opt, zone_options_t* zopt)
{
#ifdef USE_ZONE_STATS
	const char* statname;
	struct zonestatname* n;
	rbnode_t* res;
	/* try to find the instantiated zonestat name */
	if(!zopt->pattern->zonestats || zopt->pattern->zonestats[0]==0)
		return 0; /* no zone stats */
	statname = config_cook_string(zopt, zopt->pattern->zonestats);
	res = rbtree_search(opt->zonestatnames, statname);
	if(res)
		return ((struct zonestatname*)res)->id;
	/* create it */
	n = (struct zonestatname*)xalloc(sizeof(*n));
	memset(n, 0, sizeof(*n));
	n->node.key = strdup(statname);
	if(!n->node.key) {
		log_msg(LOG_ERR, "malloc failed: %s", strerror(errno));
		exit(1);
	}
	n->id = (unsigned)(opt->zonestatnames->count);
	rbtree_insert(opt->zonestatnames, (rbnode_t*)n);
	return n->id;
#else /* USE_ZONE_STATS */
	(void)opt; (void)zopt;
	return 0;
#endif /* USE_ZONE_STATS */
}
Exemplo n.º 5
0
static struct list* wd_lookup(int wd) {

    struct watch n, *np = &n;
    struct list s = { (void**)&np, 1 };
    n.wd = wd;
    return rbtree_search(&tree_wd_paths, &s);
}
Exemplo n.º 6
0
void
anchors_delete_insecure(struct val_anchors* anchors, uint16_t c,
        uint8_t* nm)
{
	struct trust_anchor key;
	struct trust_anchor* ta;
	key.node.key = &key;
	key.name = nm;
	key.namelabs = dname_count_size_labels(nm, &key.namelen);
	key.dclass = c;
	lock_basic_lock(&anchors->lock);
	if(!(ta=(struct trust_anchor*)rbtree_search(anchors->tree, &key))) {
		lock_basic_unlock(&anchors->lock);
		/* nothing there */
		return;
	}
	/* lock it to drive away other threads that use it */
	lock_basic_lock(&ta->lock);
	/* see if its really an insecure point */
	if(ta->keylist || ta->autr || ta->numDS || ta->numDNSKEY) {
		lock_basic_unlock(&anchors->lock);
		lock_basic_unlock(&ta->lock);
		/* its not an insecure point, do not remove it */
		return;
	}

	/* remove from tree */
	(void)rbtree_delete(anchors->tree, &ta->node);
	anchors_init_parents_locked(anchors);
	lock_basic_unlock(&anchors->lock);

	/* actual free of data */
	lock_basic_unlock(&ta->lock);
	anchors_delfunc(&ta->node, NULL);
}
Exemplo n.º 7
0
bool
rbtree_delete(struct rbtree *tree, ipaddr_t IP)
{
  struct rbtree_elem *parent, *succ, *pred, *node;

  if (!rbtree_search(tree, IP, &node)) {
    return false;
  }

  if (node->low == IP) {
    if (node->high == IP) {
      node_delete(&tree->root, node) ;
    } else {
      node->low++ ;
    }
  } else if (node->high == IP) {
    node->high--;
  } else {
    rbnode_t new_node;
    new_node = malloc(sizeof(struct rbtree_elem)) ;
    ASSERT(new_node);
    ASSERT(node->low < IP && node->high > IP) ;
    new_node->low=IP+1 ; new_node->high=node->high ;
    new_node->left=new_node->right=new_node->parent=nil ;
    new_node->color = _RED_ ;
    node->high = IP - 1;
    node_insert_at(&tree->root, node, new_node);
  }
  return true;
}
Exemplo n.º 8
0
/** read creation entry */
static void read_create(rbtree_t* all, FILE* in)
{
	struct order_lock* o = calloc(1, sizeof(struct order_lock));
	if(!o) fatal_exit("malloc failure");
	if(fread(&o->id.thr, sizeof(int), 1, in) != 1 ||	
	   fread(&o->id.instance, sizeof(int), 1, in) != 1 ||	
	   !readup_str(&o->create_file, in) ||
	   fread(&o->create_line, sizeof(int), 1, in) != 1)
		fatal_exit("fread failed");
	o->smaller = rbtree_create(order_lock_cmp);
	o->node.key = &o->id;
	if(!rbtree_insert(all, &o->node)) {
		/* already inserted */
		struct order_lock* a = (struct order_lock*)rbtree_search(all, 
			&o->id);
		log_assert(a);
		a->create_file = o->create_file;
		a->create_line = o->create_line;
		free(o->smaller);
		free(o);
		o = a;
	}
	if(verb) printf("read create %u %u %s %d\n", 
		(unsigned)o->id.thr, (unsigned)o->id.instance,
		o->create_file, o->create_line);
}
Exemplo n.º 9
0
int
main(void)
{
    //int         i;
    RB_TREE     *T;

    T = tree_init();

    node_insert(T, 23);
    node_insert(T, 94);
    node_insert(T, 32);
    node_insert(T, 84);
    node_insert(T, 12);
    node_insert(T, 8);
    node_insert(T, 82);
    node_insert(T, 31);
    node_insert(T, 59);
    node_insert(T, 41);
    node_insert(T, 73);

    //for ( i = 0 ; i < 1000 ; i++ ) {
    //    key = rand() % 1000;
    //    node_insert(T, key);
    //}


    inorder_tree_walk(T, T->root);
    printf("\n");

    node_delete(T, 32);
    node_delete(T, 8);
    node_delete(T, 4);

    printf("after delete 32, 8, 4:\n");
    inorder_tree_walk(T, T->root);
    printf("\n");

    printf("sucessor of 23 is %d\n", rbtree_successor(T, rbtree_search(T, T->root, 23))->key);
    printf("predecessor of 84 is %d\n", rbtree_predecessor(T, rbtree_search(T, T->root, 84))->key);
    printf("the maximun is %d\n", rbtree_maximum(T, T->root)->key);
    printf("the minimum is %d\n", rbtree_minimum(T, T->root)->key);

    node_destory(T, T->root);
    tree_destory(T);

    exit(0);
}
Exemplo n.º 10
0
static struct diff_zone*
diff_read_find_zone(struct diff_read_data* data, const char* name)
{
	const dname_type* dname = dname_parse(data->region, name);
	struct diff_zone* zp = (struct diff_zone*)
		rbtree_search(data->zones, dname);
	return zp;
}
Exemplo n.º 11
0
/** see if zone needs to have a hole inserted */
static int
need_hole_insert(rbtree_t* tree, struct iter_forward_zone* zone)
{
	struct iter_forward_zone k;
	if(rbtree_search(tree, zone))
		return 0; /* exact match exists */
	k = *zone;
	k.node.key = &k;
	/* search up the tree */
	do {
		dname_remove_label(&k.name, &k.namelen);
		k.namelabs --;
		if(rbtree_search(tree, &k))
			return 1; /* found an upper forward zone, need hole */
	} while(k.namelabs > 1);
	return 0; /* no forwards above, no holes needed */
}
Exemplo n.º 12
0
/** get var from store */
static struct replay_var*
macro_getvar(rbtree_t* store, char* name)
{
	struct replay_var k;
	k.node.key = &k;
	k.name = name;
	return (struct replay_var*)rbtree_search(store, &k);
}
Exemplo n.º 13
0
void
xfrd_handle_passed_packet(buffer_type* packet, int acl_num)
{
	uint8_t qnamebuf[MAXDOMAINLEN];
	uint16_t qtype, qclass;
	const dname_type* dname;
	region_type* tempregion = region_create(xalloc, free);
	xfrd_zone_t* zone;

	buffer_skip(packet, QHEADERSZ);
	if(!packet_read_query_section(packet, qnamebuf, &qtype, &qclass)) {
		region_destroy(tempregion);
		return; /* drop bad packet */
	}

	dname = dname_make(tempregion, qnamebuf, 1);
	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: got passed packet for %s, acl "
					   "%d", dname_to_string(dname,0), acl_num));

	/* find the zone */
	zone = (xfrd_zone_t*)rbtree_search(xfrd->zones, dname);
	if(!zone) {
		log_msg(LOG_INFO, "xfrd: incoming packet for unknown zone %s",
			dname_to_string(dname,0));
		region_destroy(tempregion);
		return; /* drop packet for unknown zone */
	}
	region_destroy(tempregion);

	/* handle */
	if(OPCODE(packet) == OPCODE_NOTIFY) {
		xfrd_soa_t soa;
		int have_soa = 0;
		int next;
		/* get serial from a SOA */
		if(ANCOUNT(packet) == 1 && packet_skip_dname(packet) &&
			xfrd_parse_soa_info(packet, &soa)) {
				have_soa = 1;
		}
		if(xfrd_handle_incoming_notify(zone, have_soa?&soa:NULL)) {
			if(zone->zone_handler.fd == -1
				&& zone->tcp_conn == -1 &&
				!zone->tcp_waiting && !zone->udp_waiting) {
					xfrd_set_refresh_now(zone);
			}
		}
		next = find_same_master_notify(zone, acl_num);
		if(next != -1) {
			zone->next_master = next;
			DEBUG(DEBUG_XFRD,1, (LOG_INFO,
				"xfrd: notify set next master to query %d",
				next));
		}
	}
	else {
		/* TODO handle incoming IXFR udp reply via port 53 */
	}
}
Exemplo n.º 14
0
static struct iter_forward_zone*
fwd_zone_find(struct iter_forwards* fwd, uint16_t c, uint8_t* nm)
{
	struct iter_forward_zone key;
	key.node.key = &key;
	key.dclass = c;
	key.name = nm;
	key.namelabs = dname_count_size_labels(nm, &key.namelen);
	return (struct iter_forward_zone*)rbtree_search(fwd->tree, &key);
}
Exemplo n.º 15
0
int inotify_map_get_wd(const char *path) {

    struct watch s, *n;

    s.path = path;
    n = rbtree_search(&tree_path_wd, &s);
    if (n)
        return n->wd;
    return 0;
}
Exemplo n.º 16
0
/** find a data node by exact name */
static struct local_data* 
lz_find_node(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs)
{
	struct local_data key;
	key.node.key = &key;
	key.name = nm;
	key.namelen = nmlen;
	key.namelabs = nmlabs;
	return (struct local_data*)rbtree_search(&z->data, &key.node);
}
Exemplo n.º 17
0
struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
        uint8_t* p, uint32_t len)
{
	struct ctx_query* q;
	int id;
	if(len != 2*sizeof(uint32_t)) return NULL;
	log_assert( ldns_read_uint32(p) == UB_LIBCMD_CANCEL);
	id = (int)ldns_read_uint32(p+sizeof(uint32_t));
	q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
	return q;
}
Exemplo n.º 18
0
/* 删除任意值 */
int rbtree_delete(rbtree tree,int key){
	int *val = rbtree_search(tree,key);
	if(val==NULL) return 0;
	rbtree_node root = tree->root;
	if( !is_red(root->left) && !is_red(root->right) ){
		root->color = RED;
	}
	root = _rbtree_delete(root,key);
	if(root != NULL) root->color = BLACK;
	return 1;
}
Exemplo n.º 19
0
void
xfrd_send_notify(rbtree_t* tree, const dname_type* apex, struct xfrd_soa* new_soa)
{
	/* lookup the zone */
	struct notify_zone_t* zone = (struct notify_zone_t*)
		rbtree_search(tree, apex);
	assert(zone);
	if(zone->notify_send_enable)
		notify_disable(zone);

	notify_enable(zone, new_soa);
}
Exemplo n.º 20
0
void
node_delete(RB_TREE *T, int key)
{
    RB_NODE     *node;

    if ((node = rbtree_search(T, T->root, key)) != T->nil) {
        rb_delete(T, node);
        free(node);
    }
    else
        fprintf(stderr, "can't find %d\n", key);
}
Exemplo n.º 21
0
void vint_sender_add_permission(UB sender_id, UB receiver_id, UHW irq_id){
    UW key = ((UW)receiver_id) << 16 | irq_id;

    node_t *node = rbtree_search(tree, (void *)key);
    if(node){
        UW mask = (UW)node->val | (1 << sender_id);
        rbtree_insert(tree, (void *)key, (void *)mask);
    }
    else {
        rbtree_insert(tree, (void *)key, (void *)(1 << sender_id));
    }
}
Exemplo n.º 22
0
void inotify_map(int wd, const char *path) {

    struct watch s, *n;

    s.path = path;
    n = rbtree_search(&tree_path_wd, &s);

    if (!n) {
        n = new_node(wd, path);
        map_path(n);
        map_wd(n);
    }
}
Exemplo n.º 23
0
static void map_wd(struct watch *w) {

    struct list s = { (void**)&w, 1 };
    struct list *l = rbtree_search(&tree_wd_paths, &s);

    if (l) {
        list_insert(l, w);
    } else {
        l = list_create();
        list_insert(l, w);
        rbtree_insert(&tree_wd_paths, l);
    }
}
Exemplo n.º 24
0
/** find next useful id number of 0 on error */
static int
find_id(struct ub_ctx* ctx, int* id)
{
	size_t tries = 0;
	ctx->next_querynum++;
	while(rbtree_search(&ctx->queries, &ctx->next_querynum)) {
		ctx->next_querynum++; /* numerical wraparound is fine */
		if(tries++ > NUM_ID_TRIES)
			return 0;
	}
	*id = ctx->next_querynum;
	return 1;
}
Exemplo n.º 25
0
net_interface_t *net_interface_lookup_index(const int ifindex)
{
    tree_node_t temp;
    net_interface_t itemp;
    rbtree_node_t *retval;

    itemp.ifindex = ifindex;
    temp.ni = &itemp;

    retval = rbtree_search(interface_tree, &temp.header, cmpfnif);

    return ((retval) ? (((tree_node_t *) retval)->ni) : NULL);
}
Exemplo n.º 26
0
struct local_zone* 
local_zones_find(struct local_zones* zones,
        uint8_t* name, size_t len, int labs, uint16_t dclass)
{
	struct local_zone key;
	key.node.key = &key;
	key.dclass = dclass;
	key.name = name;
	key.namelen = len;
	key.namelabs = labs;
	/* exact */
	return (struct local_zone*)rbtree_search(&zones->ztree, &key);
}
Exemplo n.º 27
0
struct mesh_state* mesh_area_find(struct mesh_area* mesh,
	struct query_info* qinfo, uint16_t qflags, int prime)
{
	struct mesh_state key;
	struct mesh_state* result;

	key.node.key = &key;
	key.s.is_priming = prime;
	key.s.qinfo = *qinfo;
	key.s.query_flags = qflags;
	
	result = (struct mesh_state*)rbtree_search(&mesh->all, &key);
	return result;
}
Exemplo n.º 28
0
int inotify_unmap_path(const char *path) {

    int ret = 0;
    struct watch *w, s;

    s.path = path;
    w = rbtree_search(&tree_path_wd, &s);
    if (w) {
        ret = unmap_wd(w);
        unmap_path(w);
        inotify_watch_destroy(w, unmap);
    }
    return ret;
}
Exemplo n.º 29
0
GPrivate Int32_t urbt_search(RBTreeP_t pTree, Int32_t data)
{
    Int32P_t pData = &data;
    Int32P_t pResult = NULL;

    pResult = (Int32P_t)rbtree_search(pTree, pData);

    if (pResult != NULL)
    {
        return *pResult;
    }

    return 0;
}
Exemplo n.º 30
0
/**
 * Find the given data
 * @param zone: negative zone
 * @param nm: what to look for.
 * @param len: length of nm
 * @param labs: labels in nm
 * @return data or NULL if not found.
 */
static struct val_neg_data* neg_find_data(struct val_neg_zone* zone, 
	uint8_t* nm, size_t len, int labs)
{
	struct val_neg_data lookfor;
	struct val_neg_data* result;
	lookfor.node.key = &lookfor;
	lookfor.name = nm;
	lookfor.len = len;
	lookfor.labs = labs;

	result = (struct val_neg_data*)
		rbtree_search(&zone->tree, lookfor.node.key);
	return result;
}