void moloch_db_add_local_ip(char *str, MolochIpInfo_t *ii) { patricia_node_t *node; if (!ipTree) { ipTree = New_Patricia(32); } node = make_and_lookup(ipTree, str); node->data = ii; }
static void get_netroutes(void) { FILE *pnr=NULL; char lbuf[1024], intf[32]; uint32_t dest, gw, refcnt, use, mask, irtt; uint16_t metric, flags, window, mtu; char destnet[128], gwstr[128], addstr[128]; int lineno=0; pnr=fopen("/proc/net/route", "r"); if (pnr == NULL) { ERR("cant open /proc/net/route: `%s'", strerror(errno)); exit(1); } rt=New_Patricia(128); /* * Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT * eth1 0045A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0 */ for (lineno=0; fgets(lbuf, sizeof(lbuf) -1, pnr) != NULL; lineno++) { if (lineno == 0) { continue; } #if 0 #define RTF_UP 0x0001 /* route usable */ #define RTF_GATEWAY 0x0002 /* destination is a gateway */ #define RTF_HOST 0x0004 /* host entry (net otherwise) */ #define RTF_REINSTATE 0x0008 /* reinstate route after tmout */ #define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */ #define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */ #define RTF_MTU 0x0040 /* specific MTU for this route */ #define RTF_MSS RTF_MTU /* Compatibility :-( */ #define RTF_WINDOW 0x0080 /* per route window clamping */ #define RTF_IRTT 0x0100 /* Initial round trip time */ #define RTF_REJECT 0x0200 /* Reject route */ #endif /* in de gw fl ref us me ma mt wi ir */ if (sscanf(lbuf, "%31s %x %x %hx %u %u %hu %x %hu %hu %u", intf, &dest, &gw, &flags, &refcnt, &use, &metric, &mask, &mtu, &window, &irtt) == 11) { int mycidr=0; strcpy(destnet, INT_NTOA(dest)); mycidr=masktocidr(mask); strcpy(gwstr, INT_NTOA(gw)); if (flags & RTF_UP && mycidr > -1) { sa_u s_u; route_info_t ri_u; ri_u.p=xmalloc(sizeof(*ri_u.info_s)); memset(ri_u.p, 0, sizeof(*ri_u.info_s)); ri_u.info_s->intf=xstrdup(intf); ri_u.info_s->metric=metric; /* could only be 0xff anyhow */ ri_u.info_s->flags=flags; if ((flags & RTF_GATEWAY) == RTF_GATEWAY) { s_u.ss=&ri_u.info_s->gw; s_u.sin->sin_addr.s_addr=gw; s_u.sin->sin_family=AF_INET; } sprintf(addstr, "%s/%d", destnet, mycidr); DBG("net %s via %s metric %u", addstr, (flags & RTF_GATEWAY) == 0 ? intf : gwstr, metric); node=make_and_lookup(rt, addstr); if (node == NULL) { exit(1); } node->data=ri_u.p; } } else { ERR("can not parse `%s'", lbuf); } } fclose(pnr); need_netroutes=0; return; }
void moloch_rules_load_add_field(MolochRule_t *rule, int pos, char *key) { uint32_t n; char *key2; GPtrArray *rules; patricia_node_t *node; config.fields[pos]->ruleEnabled = 1; switch (config.fields[pos]->type) { case MOLOCH_FIELD_TYPE_INT: case MOLOCH_FIELD_TYPE_INT_ARRAY: case MOLOCH_FIELD_TYPE_INT_HASH: case MOLOCH_FIELD_TYPE_INT_GHASH: if (!loading.fieldsHash[pos]) loading.fieldsHash[pos] = g_hash_table_new_full(NULL, NULL, NULL, moloch_rules_free_array); n = atoi(key); g_hash_table_add(rule->hash[pos], (void *)(long)n); rules = g_hash_table_lookup(loading.fieldsHash[pos], (void *)(long)n); if (!rules) { rules = g_ptr_array_new(); g_hash_table_insert(loading.fieldsHash[pos], (void *)(long)n, rules); } g_ptr_array_add(rules, rule); break; case MOLOCH_FIELD_TYPE_IP: case MOLOCH_FIELD_TYPE_IP_GHASH: if (!loading.fieldsTree4[pos]) { loading.fieldsTree4[pos] = New_Patricia(32); loading.fieldsTree6[pos] = New_Patricia(128); } if (strchr(key, '.') != 0) { make_and_lookup(rule->tree4[pos], key); node = make_and_lookup(loading.fieldsTree4[pos], key); } else { make_and_lookup(rule->tree6[pos], key); node = make_and_lookup(loading.fieldsTree6[pos], key); } if (node->data) { rules = node->data; } else { node->data = rules = g_ptr_array_new(); } g_ptr_array_add(rules, rule); break; case MOLOCH_FIELD_TYPE_STR: case MOLOCH_FIELD_TYPE_STR_ARRAY: case MOLOCH_FIELD_TYPE_STR_HASH: case MOLOCH_FIELD_TYPE_STR_GHASH: if (!loading.fieldsHash[pos]) loading.fieldsHash[pos] = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, moloch_rules_free_array); key2 = g_strdup(key); if (!g_hash_table_add(rule->hash[pos], key2)) g_free(key2); rules = g_hash_table_lookup(loading.fieldsHash[pos], key); if (!rules) { rules = g_ptr_array_new(); g_hash_table_insert(loading.fieldsHash[pos], g_strdup(key), rules); } g_ptr_array_add(rules, rule); break; } }