Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
static int save_history_from_tree(const char *fname) {
	FILE *f;
	node_print_mode_t info;
	char s[256];

	info.kn=0;
	_dprintf("%s: fname=%s\n", __FUNCTION__, fname);

	unlink(uncomp_fn);
	if ((f = fopen(uncomp_fn, "wb")) != NULL) {
		info.mode=0;
		info.stream=f;
		TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_save, &info);
		fclose(f);

		sprintf(s, "%s.gz", fname);
		unlink(s);

		if (rename(uncomp_fn, fname) == 0) {
			sprintf(s, "gzip %s", fname);
			system(s);
		}
	}
	return info.kn;
}
Exemplo n.º 3
0
void Tree_info(void) {
	_dprintf("Tree = ");
	TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_printer, stdout);
	_dprintf("\n");
	_dprintf("Tree depth = %d\n", TREE_DEPTH(&tree, linkage));
}
Exemplo n.º 4
0
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);
}