Example #1
0
static void
hsep_sanity_check(void)
{
	const GSList *sl;
	hsep_triple sum[G_N_ELEMENTS(hsep_global_table)];
	unsigned int i, j;

	ZERO(&sum);

	g_assert(1 == hsep_own[HSEP_IDX_NODES]);

	/*
	 * Iterate over all HSEP-capable nodes, and for each triple index
	 * sum up all the connections' triple values.
	 */

	for (sl = node_all_nodes() ; sl; sl = g_slist_next(sl)) {
		struct gnutella_node *n = sl->data;

		/* also consider unestablished connections here */

		if (!(n->attrs & NODE_A_CAN_HSEP))
			continue;

		g_assert(0 == n->hsep->table[0][HSEP_IDX_NODES]);	/* check nodes */
		g_assert(0 == n->hsep->table[0][HSEP_IDX_FILES]);	/* check files */
		g_assert(0 == n->hsep->table[0][HSEP_IDX_KIB]);		/* check KiB */
		g_assert(1 == n->hsep->table[1][HSEP_IDX_NODES]);	/* check nodes */

		/* check if values are monotonously increasing (skip first) */
		g_assert(
			hsep_check_monotony(cast_to_pointer(n->hsep->table[1]),
				G_N_ELEMENTS(n->hsep->table[1]) - 1)
		);

		/* sum up the values */

		for (i = 0; i < G_N_ELEMENTS(sum); i++) {
			for (j = 0; j < G_N_ELEMENTS(sum[0]); j++)
				sum[i][j] += n->hsep->table[i][j];
		}
	}

	/* check sums */

	for (i = 0; i < G_N_ELEMENTS(sum); i++) {
		for (j = 0; j < G_N_ELEMENTS(sum[0]); j++)
			g_assert(hsep_global_table[i][j] == sum[i][j]);
	}
}
Example #2
0
static void
hsep_sanity_check(void)
{
	const pslist_t *sl;
	hsep_triple sum[N_ITEMS(hsep_global_table)];
	unsigned int i, j;

	ZERO(&sum);

	g_assert(1 == hsep_own[HSEP_IDX_NODES]);

	/*
	 * Iterate over all HSEP-capable nodes, and for each triple index
	 * sum up all the connections' triple values.
	 */

	PSLIST_FOREACH(node_all_gnet_nodes(), sl) {
		gnutella_node_t *n = sl->data;

		/* also consider unestablished connections here */

		if (!(n->attrs & NODE_A_CAN_HSEP))
			continue;

		g_assert(0 == n->hsep->table[0][HSEP_IDX_NODES]);	/* check nodes */
		g_assert(0 == n->hsep->table[0][HSEP_IDX_FILES]);	/* check files */
		g_assert(0 == n->hsep->table[0][HSEP_IDX_KIB]);		/* check KiB */
		g_assert(1 == n->hsep->table[1][HSEP_IDX_NODES]);	/* check nodes */

		/* check if values are monotonously increasing (skip first) */
		g_assert(
			hsep_check_monotony(cast_to_pointer(n->hsep->table[1]),
				N_ITEMS(n->hsep->table[1]) - 1)
		);

		/* sum up the values */

		for (i = 0; i < N_ITEMS(sum); i++) {
			for (j = 0; j < N_ITEMS(sum[0]); j++)
				sum[i][j] += n->hsep->table[i][j];
		}
	}
Example #3
0
void
hsep_process_msg(struct gnutella_node *n, time_t now)
{
	unsigned int i, j, k, max, msgmax, length;
	hsep_triple *messaget;
	hsep_ctx_t *hsep;

	g_assert(n);
	g_assert(n->hsep);

	hsep = n->hsep;
	length = n->size;

	/* note the offset between message and local data by 1 triple */

	messaget = cast_to_pointer(n->data);

	if (length == 0) {   /* error, at least 1 triple must be present */
		if (GNET_PROPERTY(hsep_debug) > 1)
			printf("HSEP: Node %s sent empty message\n",
				host_addr_port_to_string(n->addr, n->port));

		return;
	}

	if (length % 24) {   /* error, # of triples not an integer */
		if (GNET_PROPERTY(hsep_debug) > 1)
			printf("HSEP: Node %s sent broken message\n",
				host_addr_port_to_string(n->addr, n->port));

		return;
	}

	/* get N_MAX of peer servent (other_n_max) */
	msgmax = length / 24;

	if (NODE_IS_LEAF(n) && msgmax > 1) {
		if (GNET_PROPERTY(hsep_debug) > 1) {
			printf(
				"HSEP: Node %s is a leaf, but sent %u triples instead of 1\n",
				host_addr_port_to_string(n->addr, n->port), msgmax);
		}
		return;
	}

	/* truncate if peer servent sent more triples than we need */
	max = MIN(msgmax, HSEP_N_MAX);
	hsep_fix_endian(messaget, max);

	/*
	 * Perform sanity check on received message.
	 */

	if (messaget[0][HSEP_IDX_NODES] != 1) { /* # of nodes for 1 hop must be 1 */
		if (GNET_PROPERTY(hsep_debug) > 1)
			printf("HSEP: Node %s's message's #nodes for 1 hop is not 1\n",
				host_addr_port_to_string(n->addr, n->port));
		return;
	}

	if (!hsep_check_monotony(messaget, max)) {
		if (GNET_PROPERTY(hsep_debug) > 1)
			printf("HSEP: Node %s's message's monotony check failed\n",
				host_addr_port_to_string(n->addr, n->port));

		return;
	}

	if (GNET_PROPERTY(hsep_debug) > 1) {
		printf("HSEP: Received %d %s from node %s (msg #%u): ", max,
		    max == 1 ? "triple" : "triples",
			host_addr_port_to_string(n->addr, n->port),
			hsep->msgs_received + 1);
	}

	/*
	 * Update global and per-connection tables.
	 */

	for (k = 0, i = 1; k < max; k++, i++) {

		if (GNET_PROPERTY(hsep_debug) > 1) {
			char buf[G_N_ELEMENTS(messaget[0])][32];

			for (j = 0; j < G_N_ELEMENTS(buf); j++)
				uint64_to_string_buf(messaget[k][j], buf[j], sizeof buf[0]);

			STATIC_ASSERT(3 == G_N_ELEMENTS(buf));
			printf("(%s, %s, %s) ", buf[0], buf[1], buf[2]);
		}

		for (j = 0; j < G_N_ELEMENTS(hsep_global_table[0]); j++) {
			hsep_global_table[i][j] += messaget[k][j] - hsep->table[i][j];
			hsep->table[i][j] = messaget[k][j];
		}
	}

	if (GNET_PROPERTY(hsep_debug) > 1)
		puts("\n");

	/*
	 * If the peer servent sent less triples than we need,
	 * repeat the last triple until we have enough triples
	 */

	/* Go back to last triple */
	if (k > 0)
		k--;

	for (/* NOTHING */; i < G_N_ELEMENTS(hsep_global_table); i++) {

		for (j = 0; j < G_N_ELEMENTS(hsep_global_table[0]); j++) {
			hsep_global_table[i][j] += messaget[k][j] - hsep->table[i][j];
			hsep->table[i][j] = messaget[k][j];
		}
	}

	/*
	 * Update counters and timestamps.
	 */

	hsep->msgs_received++;
	hsep->triples_received += msgmax;

	hsep->last_received = now;

	if (GNET_PROPERTY(hsep_debug) > 1)
		hsep_dump_table();

	hsep_fire_global_table_changed(now);
}