int main(int argc, char **argv) { int i; Tree tree= TREE_INITIALIZER(Node_compare); mt_random_init(); for (i= 0; i < 100; ++i) { Node v= { mt_random() % 1000, i }; Node *vv= TREE_FIND(&tree, _Node, tree, &v); if (vv) { printf("already inserted "); Node_print(vv, stdout); printf("\n"); } else { printf("insert " ); Node_print(&v, stdout); printf("\n"); TREE_INSERT(&tree, _Node, tree, Node_new(v.key, v.value)); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); for (i= 0; i < 1000; ++i) { Node *v= Node_new(mt_random() % 1000, 0); Node *vv= TREE_FIND(&tree, _Node, tree, v); printf("looking for %d - ", v->key); if (vv) { printf("found "); Node_print(vv, stdout); printf("\n"); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } else { printf("not found\n"); } TREE_REMOVE(&tree, _Node, tree, v); } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); return 0; }
static bool ucl_schema_array_is_unique (const ucl_object_t *obj, struct ucl_schema_error *err) { ucl_compare_tree_t tree = TREE_INITIALIZER (ucl_schema_elt_compare); ucl_object_iter_t iter = NULL; const ucl_object_t *elt; struct ucl_compare_node *node, test, *nodes = NULL, *tmp; bool ret = true; while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { test.obj = elt; node = TREE_FIND (&tree, ucl_compare_node, link, &test); if (node != NULL) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, elt, "duplicate values detected while uniqueItems is true"); ret = false; break; } node = calloc (1, sizeof (*node)); if (node == NULL) { ucl_schema_create_error (err, UCL_SCHEMA_UNKNOWN, elt, "cannot allocate tree node"); ret = false; break; } node->obj = elt; TREE_INSERT (&tree, ucl_compare_node, link, node); LL_PREPEND (nodes, node); } LL_FOREACH_SAFE (nodes, node, tmp) { free (node); }
static int load_history_to_tree(const char *fname) { int n; FILE *f; char s[256]; Node tmp; Node *ptr; char *exclude; exclude = nvram_safe_get("cstats_exclude"); _dprintf("%s: cstats_exclude='%s'\n", __FUNCTION__, exclude); _dprintf("%s: fname=%s\n", __FUNCTION__, fname); unlink(uncomp_fn); n = -1; // Initial value, will be returned if we failed to parse a data file sprintf(s, "gzip -dc %s > %s", fname, uncomp_fn); if (system(s) == 0) { if ((f = fopen(uncomp_fn, "rb")) != NULL) { n = 0; // Initial counter while (fread(&tmp, sizeof(Node), 1, f) > 0) { if ((find_word(exclude, tmp.ipaddr))) { _dprintf("%s: not loading excluded ip '%s'\n", __FUNCTION__, tmp.ipaddr); continue; } if (tmp.id == CURRENT_ID) { _dprintf("%s: found data for ip %s\n", __FUNCTION__, tmp.ipaddr); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); if (ptr) { _dprintf("%s: removing/reloading new data for ip %s\n", __FUNCTION__, ptr->ipaddr); TREE_REMOVE(&tree, _Node, linkage, ptr); free(ptr); ptr = NULL; } TREE_INSERT(&tree, _Node, linkage, Node_new(tmp.ipaddr)); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); memcpy(ptr->daily, &tmp.daily, sizeof(data_t) * MAX_NDAILY); ptr->dailyp = tmp.dailyp; memcpy(ptr->monthly, &tmp.monthly, sizeof(data_t) * MAX_NMONTHLY); ptr->monthlyp = tmp.monthlyp; ptr->utime = tmp.utime; #ifdef SPEED_SUPPORT memcpy(ptr->speed, &tmp.speed, sizeof(uint64_t) * MAX_NSPEED * MAX_COUNTER); ptr->tail = tmp.tail; #endif memcpy(ptr->last, &tmp.last, sizeof(uint64_t) * MAX_COUNTER); // ptr->sync = tmp.sync; ptr->sync = -1; if (ptr->utime > current_uptime) { ptr->utime = current_uptime; ptr->sync = 1; } ++n; } else { _dprintf("%s: data for ip '%s' version %d not loaded (current version is %d)\n", __FUNCTION__, tmp.ipaddr, tmp.id, CURRENT_ID); } } fclose(f); } } else { _dprintf("%s: %s != 0\n", __FUNCTION__, s); } unlink(uncomp_fn); if (n == -1) _dprintf("%s: Failed to parse the data file!\n", __FUNCTION__); else _dprintf("%s: Loaded %d records\n", __FUNCTION__, n); return n; }
void Mainloop() { long closedset_sz = 0; long openset_sz = 0; //long start_time = clock(); time_t time_start,time_end; time(&time_start); Tree tree= TREE_INITIALIZER(Node_compare); _Node p0 = {0}; p0.uXYZ.lXYZ[0] = 0; p0.uXYZ.lXYZ[1] = 0; ComputeF(&p0); _Node* temp = Node_new(&p0); temp->uXYZ.lXYZ[0] = 0; temp->uXYZ.lXYZ[1] = 0; _Node* p = Node_new(temp); TREE_INSERT(&tree, _Node, linkage, p); HeapInsert(p); openset_sz++; while(true) { _Node* p = HeapRemove(); p->expended = true; closedset_sz++; openset_sz--; if (p->h == 0) { printf("\nThe MLCS is:\n"); int l = CommonSeq(p); printf("\n\n|MLCS|=%d\n", l); break; } for(int i=0; i<ABC_len; i++) { int j=0; if (NUMBYTE == 1) { while (j<MAXSTR) { temp->uXYZ.cXYZ[j] = cur_T[j][p->uXYZ.cXYZ[j]+1][i]; if (temp->uXYZ.cXYZ[j]==str_lngth[j]+1) break; j++; } } else { while (j<MAXSTR) { temp->uXYZ.sXYZ[j] = cur_T[j][p->uXYZ.sXYZ[j]+1][i]; if (temp->uXYZ.sXYZ[j]==str_lngth[j]+1) break; j++; } } if (j < MAXSTR) continue; _Node *q = TREE_FIND(&tree, _Node, linkage, temp); if (q) { //node is already in tree if (!q->expended && (q->fg & 0xffff) < (p->fg & 0xffff)+1) { q->parent = p; q->fg = ((q->h+(p->fg & 0xffff)+1) << 16) | ((p->fg & 0xffff)+1); } } else { q = Node_new(temp); q->parent = p; q->fg = p->fg+1; ComputeF(q); TREE_INSERT(&tree, _Node, linkage, q); HeapInsert(q); openset_sz++; } } } printf("|closedset|=%d\n|openset|=%d\n\n", closedset_sz, openset_sz+1); time(&time_end); double elapsedTime = difftime(time_end,time_start); printf("The time is %6.2f seconds\n",elapsedTime); }
void iptraffic_conntrack_init() { unsigned int a_time, a_proto; char a_src[INET_ADDRSTRLEN]; char a_dst[INET_ADDRSTRLEN]; char b_src[INET_ADDRSTRLEN]; char b_dst[INET_ADDRSTRLEN]; char sa[256]; char sb[256]; FILE *a; char *p; int x; Node tmp; Node *ptr; unsigned long rip[4]; unsigned long lan[4]; unsigned long mask[4]; unsigned short int br; for(br=0 ; br<=3 ; br++) { char bridge[2] = "0"; if (br!=0) bridge[0]+=br; else strcpy(bridge, ""); sprintf(sa, "lan%s_ifname", bridge); if (strcmp(nvram_safe_get(sa), "") != 0) { sprintf(sa, "lan%s_ipaddr", bridge); rip[br] = inet_addr(nvram_safe_get(sa)); sprintf(sa, "lan%s_netmask", bridge); mask[br] = inet_addr(nvram_safe_get(sa)); lan[br] = rip[br] & mask[br]; // _dprintf("rip[%d]=%lu\n", br, rip[br]); // _dprintf("mask[%d]=%lu\n", br, mask[br]); // _dprintf("lan[%d]=%lu\n", br, lan[br]); } else { mask[br] = 0; rip[br] = 0; lan[br] = 0; } } const char conntrack[] = "/proc/net/ip_conntrack"; if ((a = fopen(conntrack, "r")) == NULL) return; ctvbuf(a); // if possible, read in one go while (fgets(sa, sizeof(sa), a)) { if (sscanf(sa, "%*s %u %u", &a_proto, &a_time) != 2) continue; if ((a_proto != 6) && (a_proto != 17)) continue; if ((p = strstr(sa, "src=")) == NULL) continue; if (sscanf(p, "src=%s dst=%s %n", a_src, a_dst, &x) != 2) continue; p += x; if ((p = strstr(p, "src=")) == NULL) continue; if (sscanf(p, "src=%s dst=%s", b_src, b_dst) != 2) continue; snprintf(sb, sizeof(sb), "%s %s %s %s", a_src, a_dst, b_src, b_dst); remove_dups(sb, sizeof(sb)); char ipaddr[INET_ADDRSTRLEN], *next = NULL; char skip; foreach(ipaddr, sb, next) { skip = 1; for(br=0 ; br<=3 ; br++) { if ((mask[br] != 0) && ((inet_addr(ipaddr) & mask[br]) == lan[br])) { skip = 0; break; } } if (skip == 1) continue; strncpy(tmp.ipaddr, ipaddr, INET_ADDRSTRLEN); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); if (!ptr) { _dprintf("%s: new ip: %s\n", __FUNCTION__, ipaddr); TREE_INSERT(&tree, _Node, linkage, Node_new(ipaddr)); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); } if (a_proto == 6) ++ptr->tcp_conn; if (a_proto == 17) ++ptr->udp_conn; } }
void asp_iptraffic(int argc, char **argv) { char comma; char sa[256]; FILE *a; char ip[INET_ADDRSTRLEN]; char *exclude; unsigned long tx_bytes, rx_bytes; unsigned long tp_tcp, rp_tcp; unsigned long tp_udp, rp_udp; unsigned long tp_icmp, rp_icmp; unsigned int ct_tcp, ct_udp; exclude = nvram_safe_get("cstats_exclude"); Node tmp; Node *ptr; iptraffic_conntrack_init(); char br; char name[] = "/proc/net/ipt_account/lanX"; web_puts("\n\niptraffic=["); comma = ' '; for(br=0 ; br<=3 ; br++) { char bridge[2] = "0"; if (br!=0) bridge[0]+=br; else strcpy(bridge, ""); sprintf(name, "/proc/net/ipt_account/lan%s", bridge); if ((a = fopen(name, "r")) == NULL) continue; fgets(sa, sizeof(sa), a); // network while (fgets(sa, sizeof(sa), a)) { if(sscanf(sa, "ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dst = %lu %*u %*u %*u %*u packets_dst = %*u %lu %lu %lu %*u time = %*u", ip, &tx_bytes, &tp_tcp, &tp_udp, &tp_icmp, &rx_bytes, &rp_tcp, &rp_udp, &rp_icmp) != 9 ) continue; if (find_word(exclude, ip)) continue ; if ((tx_bytes > 0) || (rx_bytes > 0)){ strncpy(tmp.ipaddr, ip, INET_ADDRSTRLEN); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); if (!ptr) { ct_tcp = 0; ct_udp = 0; } else { ct_tcp = ptr->tcp_conn; ct_udp = ptr->udp_conn; } web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]", comma, ip, rx_bytes, tx_bytes, rp_tcp, tp_tcp, rp_udp, tp_udp, rp_icmp, tp_icmp, ct_tcp, ct_udp); comma = ','; } } fclose(a); } web_puts("];\n"); TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_housekeeping, NULL); TREE_INIT(&tree, Node_compare); }