Example #1
0
/**
 * Convert magnet source to a string representation.
 *
 * @return A newly allocated string.
 */
char *
magnet_source_to_string(const struct magnet_source *s)
{
	char *url;

	g_return_val_if_fail(s, NULL);

	if (s->url) {
		url = g_strdup(s->url);
	} else {
		char *proxies = NULL;
		const char *host, *prefix;
		char prefix_buf[256];
		char port_buf[16];

		if (s->guid) {
			char guid_buf[GUID_HEX_SIZE + 1];
			
			guid_to_string_buf(s->guid, guid_buf, sizeof guid_buf);
			concat_strings(prefix_buf, sizeof prefix_buf,
				"push://", guid_buf, (void *) 0);
			prefix = prefix_buf;
		} else {
			prefix = "http://";
		}
		
		port_buf[0] = '\0';
		if (s->hostname) {
			host = s->hostname;
			if (80 != s->port) {
				str_bprintf(port_buf, sizeof port_buf, ":%u",
					(unsigned) s->port);
			}
		} else if (s->guid) {
			proxies = proxies_to_string(s->proxies);
			host = proxies;
		} else {
			host = host_addr_port_to_string(s->addr, s->port);
		}
		if (s->path) {
			url = g_strconcat(prefix, host, port_buf, s->path, (void *) 0);
		} else if (s->sha1) {
			url = g_strconcat(prefix, host, port_buf,
					"/uri-res/N2R?", bitprint_to_urn_string(s->sha1, s->tth),
					(void *) 0);
		} else {
			url = g_strconcat(prefix, host, port_buf, "/", (void *) 0);
		}

		HFREE_NULL(proxies);
	}

	return url;
}
Example #2
0
/**
 * Create a string representation of the magnet resource.
 *
 * @return A newly allocated string via halloc().
 */
char *
magnet_to_string(const struct magnet_resource *res)
{
	GSList *sl;
	str_t *s;

	g_return_val_if_fail(res, NULL);
	
	s = str_new(0);

	if (res->display_name) {
		magnet_append_item(s, TRUE, "dn", res->display_name);
	}
	if (0 != res->size) {
		char buf[UINT64_DEC_BUFLEN];

		uint64_to_string_buf(res->size, buf, sizeof buf);
		magnet_append_item(s, FALSE, "xl", buf);
	}
	if (res->sha1) {
		magnet_append_item(s, FALSE, "xt",
			bitprint_to_urn_string(res->sha1, res->tth));
	}
	if (res->parq_id) {
		magnet_append_item(s, TRUE, "x.parq-id", res->parq_id);
	}
	if (res->vendor) {
		magnet_append_item(s, TRUE, "x.vndr", res->vendor);
	}
	if (res->guid) {
		magnet_append_item(s, TRUE, "x.guid", res->guid);
	}
	if (res->dht) {
		magnet_append_item(s, TRUE, "x.dht", "1");
	}

	for (sl = res->sources; NULL != sl; sl = g_slist_next(sl)) {
		char *url;

		url = magnet_source_to_string(sl->data);
		magnet_append_item(s, TRUE, "xs", url);
		G_FREE_NULL(url);
	}

	for (sl = res->searches; NULL != sl; sl = g_slist_next(sl)) {
		magnet_append_item(s, TRUE, "kt", sl->data);
	}

	return str_s2c_null(&s);
}
Example #3
0
static void
cache_entry_print(FILE *f, const char *filename,
	const struct sha1 *sha1, const struct tth *tth,
	filesize_t size, time_t mtime)
{
	char size_buf[UINT64_DEC_BUFLEN], mtime_buf[UINT64_DEC_BUFLEN];

	g_return_if_fail(f);
	g_return_if_fail(filename);
	g_return_if_fail(sha1);
	g_return_if_fail(size > 0);

	uint64_to_string_buf(size, size_buf, sizeof size_buf);
	uint64_to_string_buf(mtime, mtime_buf, sizeof mtime_buf);

	fprintf(f, "%s\t%s\t%s\t%s\n", bitprint_to_urn_string(sha1, tth),
		size_buf, mtime_buf, filename);
}
Example #4
0
static enum shell_reply
shell_exec_download_show(struct gnutella_shell *sh,
	int argc, const char *argv[])
{
	fileinfo_t *fi;
	struct guid guid;
	const char *id, *property;
	gnet_fi_status_t status;
	gnet_fi_info_t *info;
	int i;

	shell_check(sh);
	g_assert(argv);
	g_assert(argc > 0);

	if (argc < 3) {
		shell_set_msg(sh, "parameter missing");
		goto error;
	}
	id = argv[2];

	if (!hex_to_guid(id, &guid)) {
		shell_set_msg(sh, "Unparsable ID");
		goto error;
	}

	fi = file_info_by_guid(&guid);
	if (NULL == fi) {
		shell_set_msg(sh, "Invalid ID");
		goto error;
	}

	info = guc_fi_get_info(fi->fi_handle);
	guc_fi_get_status(fi->fi_handle, &status);

	for (i = 3; i < argc; i++) {
		property = argv[i];

		if (0 == strcmp(property, "id")) {
			show_property(sh, property, guid_to_string(info->guid));
		} else if (0 == strcmp(property, "filename")) {
			show_property(sh, property, info->filename);
		} else if (0 == strcmp(property, "pathname")) {
			show_property(sh, property, fi->pathname);
		} else if (0 == strcmp(property, "size")) {
			show_property(sh, property, filesize_to_string(info->size));
		} else if (0 == strcmp(property, "sha1")) {
			show_property(sh, property,
				info->sha1 ? sha1_to_urn_string(info->sha1) : "");
		} else if (0 == strcmp(property, "tth")) {
			show_property(sh, property,
				info->tth ? tth_to_urn_string(info->tth) : "");
		} else if (0 == strcmp(property, "bitprint")) {
			show_property(sh, property,
				(info->sha1 && info->tth)
					? bitprint_to_urn_string(info->sha1, info->tth) : "");
		} else if (0 == strcmp(property, "created")) {
			show_property(sh, property,
				info->created ? timestamp_to_string(info->created) : "");
		} else if (0 == strcmp(property, "modified")) {
			show_property(sh, property,
				status.modified ? timestamp_to_string(status.modified) : "");
		} else if (0 == strcmp(property, "downloaded")) {
			show_property(sh, property, filesize_to_string(status.done));
		} else if (0 == strcmp(property, "uploaded")) {
			show_property(sh, property, uint64_to_string(status.uploaded));
		} else if (0 == strcmp(property, "paused")) {
			show_property(sh, property, boolean_to_string(status.paused));
		} else if (0 == strcmp(property, "seeding")) {
			show_property(sh, property, boolean_to_string(status.seeding));
		} else if (0 == strcmp(property, "verifying")) {
			show_property(sh, property, boolean_to_string(status.verifying));
		} else if (0 == strcmp(property, "finished")) {
			show_property(sh, property, boolean_to_string(status.finished));
		} else if (0 == strcmp(property, "complete")) {
			show_property(sh, property, boolean_to_string(status.complete));
		} else if (0 == strcmp(property, "magnet")) {
			char *magnet = file_info_build_magnet(fi->fi_handle);
			show_property(sh, property, EMPTY_STRING(magnet));
			HFREE_NULL(magnet);
		}
	}
	guc_fi_free_info(info);
	return REPLY_READY;

error:
	return REPLY_ERROR;
}