void analyze_country_enter_cb(int result, char type, int count, int ttl, void *addresses, void *arg) { struct country_state *state = arg; struct addr *src = &state->src; struct keycount tmpkey, *key; char tld[20]; if (result != DNS_ERR_NONE || count != 1 || type != DNS_PTR) { /* Enter into our negative cache */ if (!state->result_from_cache) { tmpkey.key = &src->addr_ip; tmpkey.keylen = sizeof(src->addr_ip); key = SPLAY_FIND(kctree, &country_cache, &tmpkey); if (!key) { key = keycount_new( &src->addr_ip, sizeof(src->addr_ip), NULL, NULL); SPLAY_INSERT(kctree, &country_cache, key); } count_increment(key->count, 1); } strlcpy(tld, "unknown", sizeof(tld)); } else { const char *hostname = *(char **)addresses; int i; /* Extract the country key */ for (i = strlen(hostname) - 1; i >= 0; --i) { if (hostname[i] == '.') { i += 1; break; } } strlcpy(tld, hostname + i, sizeof(tld)); for (i = 0; i < strlen(tld); i++) { if (isdigit(tld[i])) { strlcpy(tld, "unknown", sizeof(tld)); break; } tld[i] = tolower(tld[i]); } } tmpkey.key = tld; tmpkey.keylen = strlen(tmpkey.key) + 1; if ((key = SPLAY_FIND(kctree, &countries, &tmpkey)) == NULL) { key = keycount_new(tld, strlen(tld) + 1, aux_create, aux_free); SPLAY_INSERT(kctree, &countries, key); } /* If the address is new, we are going to resolve it */ if (aux_enter(key->auxilary, port_hash(&state->src, &state->dst))) count_increment(key->count, 1); free(state); }
int cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_bind_key_data *data = self->data; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; enum mode_key_cmd cmd; if ((mtab = mode_key_findtable(data->tablename)) == NULL) { ctx->error(ctx, "unknown key table: %s", data->tablename); return (-1); } cmd = mode_key_fromstring(mtab->cmdstr, data->modecmd); if (cmd == MODEKEY_NONE) { ctx->error(ctx, "unknown command: %s", data->modecmd); return (-1); } mtmp.key = data->key & ~KEYC_PREFIX; mtmp.mode = data->command_key ? 1 : 0; if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { mbind->cmd = cmd; return (0); } mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; mbind->mode = mtmp.mode; mbind->cmd = cmd; SPLAY_INSERT(mode_key_tree, mtab->tree, mbind); return (0); }
struct systrace_revalias * systrace_reverse(const char *emulation, const char *name) { struct systrace_revalias tmp, *reverse; strlcpy(tmp.emulation, emulation, sizeof(tmp.emulation)); strlcpy(tmp.name, name, sizeof(tmp.name)); reverse = SPLAY_FIND(revtr, &revroot, &tmp); if (reverse != NULL) return (reverse); reverse = calloc(1, sizeof(struct systrace_alias)); if (reverse == NULL) err(1, "%s: %s-%s: malloc", __func__, emulation, name); strlcpy(reverse->emulation, emulation, sizeof(reverse->emulation)); strlcpy(reverse->name, name, sizeof(reverse->name)); TAILQ_INIT(&reverse->revl); if (SPLAY_INSERT(revtr, &revroot, reverse) != NULL) errx(1, "%s: %s-%s: double revalias", __func__, emulation, name); return (reverse); }
int clipstats_add(CLIPSTATS_TREES *bundle, struct clipstatstree *tree, uint32_t depth, uint8_t basetype, float value) { struct clipstats refnode, *found; refnode.depth = depth; refnode.basetype = basetype; refnode.value = value; found = SPLAY_FIND(clipstatstree, tree, &refnode); if (found == NULL) { struct clipstats *newnode; newnode = clipstatstrees_alloc_node(bundle); if (newnode == NULL) return -1; memset(newnode, 0, sizeof(struct clipstats)); newnode->depth = depth; newnode->basetype = basetype; newnode->value = value; newnode->count = 1; SPLAY_INSERT(clipstatstree, tree, newnode); } else found->count++; return 0; }
int tree_iterfrom(struct tree *t, void **hdl, uint64_t k, uint64_t *id, void **data) { struct treeentry *curr = *hdl, key; if (curr == NULL) { if (k == 0) curr = SPLAY_MIN(_tree, &t->tree); else { key.id = k; curr = SPLAY_FIND(_tree, &t->tree, &key); if (curr == NULL) { SPLAY_INSERT(_tree, &t->tree, &key); curr = SPLAY_NEXT(_tree, &t->tree, &key); SPLAY_REMOVE(_tree, &t->tree, &key); } } } else curr = SPLAY_NEXT(_tree, &t->tree, curr); if (curr) { *hdl = curr; if (id) *id = curr->id; if (data) *data = curr->data; return (1); } return (0); }
struct systrace_alias * systrace_new_alias(const char *emulation, const char *name, char *aemul, char *aname) { struct systrace_alias *alias; struct systrace_revalias *reverse; alias = malloc(sizeof(struct systrace_alias)); if (alias == NULL) err(1, "%s: %s-%s: malloc", __func__, emulation, name); strlcpy(alias->emulation, emulation, sizeof(alias->emulation)); strlcpy(alias->name, name, sizeof(alias->name)); strlcpy(alias->aemul, aemul, sizeof(alias->aemul)); strlcpy(alias->aname, aname, sizeof(alias->aname)); alias->nargs = 0; if (SPLAY_INSERT(alitr, &aliasroot, alias) != NULL) errx(1, "%s: %s-%s: double alias", __func__, emulation, name); reverse = systrace_reverse(aemul, aname); alias->reverse = reverse; TAILQ_INSERT_TAIL(&reverse->revl, alias, next); return (alias); }
struct policy * systrace_findpolicy_wildcard(const char *name) { struct policy tmp, *res; static char path[PATH_MAX], lookup[PATH_MAX]; if (strlcpy(path, name, sizeof(path)) >= sizeof(path)) errx(1, "%s: path name overflow", __func__); strlcpy(lookup, "*/", sizeof(lookup)); strlcat(lookup, basename(path), sizeof(lookup)); tmp.name = lookup; res = SPLAY_FIND(policytree, &policyroot, &tmp); if (res == NULL) return (NULL); /* we found the wildcarded policy; now remove it and bind it */ SPLAY_REMOVE(policytree, &policyroot, res); free((char *)res->name); if ((res->name = strdup(name)) == NULL) err(1, "%s: strdup", __func__); SPLAY_INSERT(policytree, &policyroot, res); return (res); }
int dict_iterfrom(struct dict *d, void **hdl, const char *kfrom, const char **k, void **data) { struct dictentry *curr = *hdl, key; if (curr == NULL) { if (kfrom == NULL) curr = SPLAY_MIN(_dict, &d->dict); else { key.key = kfrom; curr = SPLAY_FIND(_dict, &d->dict, &key); if (curr == NULL) { SPLAY_INSERT(_dict, &d->dict, &key); curr = SPLAY_NEXT(_dict, &d->dict, &key); SPLAY_REMOVE(_dict, &d->dict, &key); } } } else curr = SPLAY_NEXT(_dict, &d->dict, curr); if (curr) { *hdl = curr; if (k) *k = curr->key; if (data) *data = curr->data; return (1); } return (0); }
struct policy * systrace_newpolicy(const char *emulation, const char *name) { int i; struct policy *tmp; if ((tmp = systrace_findpolicy(name)) != NULL) return (tmp); if ((tmp = systrace_findpolicy_wildcard(name)) != NULL) return (tmp); tmp = calloc(1, sizeof(struct policy)); if (tmp == NULL) return (NULL); tmp->policynr = -1; /* New policies requires initialization */ if ((tmp->name = strdup(name)) == NULL) err(1, "%s:%d: strdup", __func__, __LINE__); strlcpy(tmp->emulation, emulation, sizeof(tmp->emulation)); SPLAY_INSERT(policytree, &policyroot, tmp); SPLAY_INIT(&tmp->pflqs); TAILQ_INIT(&tmp->filters); TAILQ_INIT(&tmp->prefilters); /* Set the default policy to ask */ for (i = 0; i < INTERCEPT_MAXSYSCALLNR; i++) tmp->kerneltable[i] = ICPOLICY_ASK; return (tmp); }
struct personality * personality_new(const char *name) { struct personality *pers, tmp; tmp.name = (char *)name; if (SPLAY_FIND(perstree, &personalities, &tmp)) return (NULL); if ((pers = calloc(1, sizeof(struct personality))) == NULL) err(1, "%s: calloc", __FUNCTION__); if ((pers->name = strdup(name)) == NULL) err(1, "%s: stdup", __FUNCTION__); /* Initialize defaults */ pers->tstamphz = -1; npersons++; SPLAY_INSERT(perstree, &personalities, pers); /* Find and add the Xprobe fingerprint, if it exists */ correlate_nmap_with_xprobe(pers); return (pers); }
int cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; enum mode_key_cmd cmd; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { ctx->error(ctx, "unknown key table: %s", tablename); return (-1); } cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); if (cmd == MODEKEY_NONE) { ctx->error(ctx, "unknown command: %s", args->argv[1]); return (-1); } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { mbind->cmd = cmd; return (0); } mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; mbind->mode = mtmp.mode; mbind->cmd = cmd; SPLAY_INSERT(mode_key_tree, mtab->tree, mbind); return (0); }
void key_bindings_remove(int key) { struct key_binding *bd; if ((bd = key_bindings_lookup(key)) == NULL) return; SPLAY_REMOVE(key_bindings, &key_bindings, bd); SPLAY_INSERT(key_bindings, &dead_key_bindings, bd); }
void tree_xset(struct tree *t, uint64_t id, void *data) { struct treeentry *entry; entry = xmalloc(sizeof *entry, "tree_xset"); entry->id = id; entry->data = data; if (SPLAY_INSERT(tree, t, entry)) errx(1, "tree_xset(%p, 0x%016"PRIx64 ")", t, id); }
void dict_xset(struct dict *d, const char * k, void *data) { struct dictentry *entry; if ((entry = dict_alloc(k, data)) == NULL) err(1, "dict_xset: malloc"); if (SPLAY_INSERT(_dict, &d->dict, entry)) errx(1, "dict_xset(%p, %s)", d, k); d->count += 1; }
static struct mta_route * mta_route_for(struct envelope *e) { struct ssl ssl; struct mta_route key, *route; bzero(&key, sizeof key); key.flags = e->agent.mta.relay.flags; if (e->agent.mta.relay.flags & ROUTE_BACKUP) { key.hostname = e->dest.domain; key.backupname = e->agent.mta.relay.hostname; } else if (e->agent.mta.relay.hostname[0]) { key.hostname = e->agent.mta.relay.hostname; key.flags |= ROUTE_MX; } else key.hostname = e->dest.domain; key.port = e->agent.mta.relay.port; key.cert = e->agent.mta.relay.cert; if (!key.cert[0]) key.cert = NULL; key.auth = e->agent.mta.relay.authmap; if (!key.auth[0]) key.auth = NULL; if ((route = SPLAY_FIND(mta_route_tree, &routes, &key)) == NULL) { route = xcalloc(1, sizeof *route, "mta_route"); TAILQ_INIT(&route->tasks); route->id = generate_uid(); route->flags = key.flags; route->hostname = xstrdup(key.hostname, "mta: hostname"); route->backupname = key.backupname ? xstrdup(key.backupname, "mta: backupname") : NULL; route->port = key.port; route->cert = key.cert ? xstrdup(key.cert, "mta: cert") : NULL; route->auth = key.auth ? xstrdup(key.auth, "mta: auth") : NULL; if (route->cert) { strlcpy(ssl.ssl_name, route->cert, sizeof(ssl.ssl_name)); route->ssl = SPLAY_FIND(ssltree, env->sc_ssl, &ssl); } SPLAY_INSERT(mta_route_tree, &routes, route); route->maxconn = MTA_MAXCONN; route->maxmail = MTA_MAXMAIL; route->maxrcpt = MTA_MAXRCPT; log_trace(TRACE_MTA, "mta: new %s", mta_route_to_text(route)); stat_increment("mta.route", 1); } else { log_trace(TRACE_MTA, "mta: reusing %s", mta_route_to_text(route)); } return (route); }
void tree_merge(struct tree *dst, struct tree *src) { struct treeentry *entry; while (!SPLAY_EMPTY(src)) { entry = SPLAY_ROOT(src); SPLAY_REMOVE(tree, src, entry); if (SPLAY_INSERT(tree, dst, entry)) errx(1, "tree_merge: duplicate"); } }
static bool vm_map_insert_entry(vm_map_t *vm_map, vm_map_entry_t *entry) { rw_assert(&vm_map->rwlock, RW_WLOCKED); if (!SPLAY_INSERT(vm_map_tree, &vm_map->tree, entry)) { vm_map_entry_t *next = SPLAY_NEXT(vm_map_tree, &vm_map->tree, entry); if (next) TAILQ_INSERT_BEFORE(next, entry, map_list); else TAILQ_INSERT_TAIL(&vm_map->list, entry, map_list); vm_map->nentries++; return true; } return false; }
void tree_xset(struct tree *t, uint64_t id, void *data) { struct treeentry *entry; if ((entry = malloc(sizeof *entry)) == NULL) err(1, "tree_xset: malloc"); entry->id = id; entry->data = data; if (SPLAY_INSERT(_tree, &t->tree, entry)) errx(1, "tree_xset(%p, 0x%016"PRIx64 ")", t, id); t->count += 1; }
void dict_merge(struct dict *dst, struct dict *src) { struct dictentry *entry; while (!SPLAY_EMPTY(&src->dict)) { entry = SPLAY_ROOT(&src->dict); SPLAY_REMOVE(_dict, &src->dict, entry); if (SPLAY_INSERT(_dict, &dst->dict, entry)) errx(1, "dict_merge: duplicate"); } dst->count += src->count; src->count = 0; }
void key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist) { struct key_binding *bd; key_bindings_remove(key); bd = xmalloc(sizeof *bd); bd->key = key; SPLAY_INSERT(key_bindings, &key_bindings, bd); bd->can_repeat = can_repeat; bd->cmdlist = cmdlist; }
void tree_merge(struct tree *dst, struct tree *src) { struct treeentry *entry; while (!SPLAY_EMPTY(&src->tree)) { entry = SPLAY_ROOT(&src->tree); SPLAY_REMOVE(_tree, &src->tree, entry); if (SPLAY_INSERT(_tree, &dst->tree, entry)) errx(1, "tree_merge: duplicate"); } dst->count += src->count; src->count = 0; }
int main(int argc, char **argv) { struct node *tmp, *ins; int i, max, min; SPLAY_INIT(&root); for (i = 0; i < ITER; i++) { tmp = malloc(sizeof(struct node)); if (tmp == NULL) err(1, "malloc"); do { tmp->key = arc4random_uniform(MAX-MIN); tmp->key += MIN; } while (SPLAY_FIND(tree, &root, tmp) != NULL); if (i == 0) max = min = tmp->key; else { if (tmp->key > max) max = tmp->key; if (tmp->key < min) min = tmp->key; } if (SPLAY_INSERT(tree, &root, tmp) != NULL) errx(1, "SPLAY_INSERT failed"); } ins = SPLAY_MIN(tree, &root); if (ins->key != min) errx(1, "min does not match"); tmp = ins; ins = SPLAY_MAX(tree, &root); if (ins->key != max) errx(1, "max does not match"); if (SPLAY_REMOVE(tree, &root, tmp) != tmp) errx(1, "SPLAY_REMOVE failed"); for (i = 0; i < ITER - 1; i++) { tmp = SPLAY_ROOT(&root); if (tmp == NULL) errx(1, "SPLAY_ROOT error"); if (SPLAY_REMOVE(tree, &root, tmp) != tmp) errx(1, "SPLAY_REMOVE error"); free(tmp); } exit(0); }
int systrace_newpolicynr(int fd, struct policy *tmp) { if (tmp->policynr != -1) return (-1); if ((tmp->policynr = intercept_newpolicy(fd)) == -1) { /* XXX - maybe free policy structure here */ return (-1); } SPLAY_INSERT(polnrtree, &polnrroot, tmp); return (tmp->policynr); }
void analyze_spammer_enter(const struct addr *src, uint32_t bytes) { struct keycount tmpkey, *key; tmpkey.key = &src->addr_ip; tmpkey.keylen = sizeof(src->addr_ip); if ((key = SPLAY_FIND(kctree, &spammers, &tmpkey)) == NULL) { key = keycount_new(&src->addr_ip, sizeof(src->addr_ip), NULL, NULL); SPLAY_INSERT(kctree, &spammers, key); } count_increment(key->count, bytes); }
void options_set_number(struct options *oo, const char *name, long long value) { struct options_entry *o; if ((o = options_find1(oo, name)) == NULL) { o = xmalloc(sizeof *o); o->name = xstrdup(name); SPLAY_INSERT(options_tree, &oo->tree, o); } else if (o->type == OPTIONS_STRING) xfree(o->value.string); o->type = OPTIONS_NUMBER; o->value.number = value; }
void mode_key_init_trees(void) { const struct mode_key_table *mtab; const struct mode_key_entry *ment; struct mode_key_binding *mbind; for (mtab = mode_key_tables; mtab->name != NULL; mtab++) { SPLAY_INIT(mtab->tree); for (ment = mtab->table; ment->mode != -1; ment++) { mbind = xmalloc(sizeof *mbind); mbind->key = ment->key; mbind->mode = ment->mode; mbind->cmd = ment->cmd; SPLAY_INSERT(mode_key_tree, mtab->tree, mbind); } } }
void * tree_set(struct tree *t, uint64_t id, void *data) { struct treeentry *entry, key; key.id = id; if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) { entry = malloc(sizeof *entry); if (entry == NULL) return (NULL); entry->id = id; SPLAY_INSERT(tree, t, entry); } entry->data = data; return (entry); }
void printflike3 options_set_string(struct options *oo, const char *name, const char *fmt, ...) { struct options_entry *o; va_list ap; if ((o = options_find1(oo, name)) == NULL) { o = xmalloc(sizeof *o); o->name = xstrdup(name); SPLAY_INSERT(options_tree, &oo->tree, o); } else if (o->type == OPTIONS_STRING) xfree(o->value.string); va_start(ap, fmt); o->type = OPTIONS_STRING; xvasprintf(&o->value.string, fmt, ap); va_end(ap); }
void analyze_os_enter(const struct addr *addr, const char *osfp) { struct keycount tmpkey, *key; tmpkey.key = osfp; tmpkey.keylen = strlen(osfp) + 1; if ((key = SPLAY_FIND(kctree, &oses, &tmpkey)) == NULL) { key = keycount_new(osfp, strlen(osfp) + 1, aux_create, aux_free); SPLAY_INSERT(kctree, &oses, key); } /* If the address is new, we are going to increase the counter */ if (aux_enter(key->auxilary, addr->addr_ip)) count_increment(key->count, 1); }
int parse_associations(FILE *fp) { assoc_item *assoc = NULL; if (fp == NULL) { fprintf(stderr, "Could not open associations file!\n"); return (-1); } while (!feof(fp)) { assoc = get_assoc(fp); if (assoc != NULL) SPLAY_INSERT(assoc_tree, &associations, assoc); } return (0); }