summaryrefslogblamecommitdiffstats
path: root/src/metainfo.c
blob: 72fc2ecf08e394a8369f06004db1669607074cc9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                      
                      














                                                                                                                                                                                      
                         
                                               

                                       
                                                      

                                   
                                                




                                                

                                                   

                                



























                                                                                                                                                                                                                                 
 
 





                                                 
                                      


                                




























                                                                                                                                                                                                                                                                                                                                
                                    

























                                                                                                                                                                                                      
                                                                                                                      




























                                                                                                                                          















                                                                                        
                                                                        

                                                      
                                                                           

                                               





                                                                                             



                                                                 






                                                                                                   

                 






























































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
 
      
#include <assert.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sha1.h>
#include <sha2.h>
#include <stddef.h>
#include <stdbool.h>
#include <metainfo.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <error.h>
#include <bencoding.c>

static void inode_free (struct inode * inode) {
	if (!inode)
		return;
	inode_free(inode->child);
	inode_free(inode->next);
	free(inode);
}

/**
 * returns next file, be it file, not dir. not sure why is this useful actually. I thought I would sum filesizes with that, but I already store cached sums of dir size in dir inodes.
 * maybe for searching files only, not dirs ?!?!?!
 */

const struct inode * next_file (const struct inode * inode) {
	if (inode->child)
		return next_file(inode->child);
	if (inode->next) {
		if (inode->next->child)
			return next_file(inode->next);
		return inode->next;
	}
	const struct inode * predecesor = inode;
	while (!predecesor->next) {
		predecesor = predecesor->parent;
		if (!predecesor)
			return NULL;
	}
	if (predecesor->next->child)
		return next_file(predecesor->next);
	return predecesor->next;
}

/**
 * useful for dirlisting.
 *
 * @param inode	[in]	the inode of which next should be returned. start at metainfo->files inode to get the full tree
 * @param paths [out]	when user builds a path, this amount of paths needs to be popped from path stack before appending resulting inode's name name to it. may be NULL. writes -1 to variable if we descended into a directory.
 * @return next inode, be it dir or file, or NULL if this is the end of inodes
 */

const struct inode * next_inode (const struct inode * inode, int * paths) {
	if (paths)
		*paths = 0;
	if (inode->child) {
		if (paths)
			*paths = -1;
		return inode->child;
	}
	if (inode->next)
		return inode->next;
	const struct inode * predecesor = inode;
	while (!predecesor->next) {
		if (paths)
			(*paths)++; // OB1 possible
		predecesor = predecesor->parent;
		if (!predecesor)
			return NULL;
	}
	return predecesor->next;
}

void metainfo_free (struct metainfo * metainfo) {
	inode_free(metainfo->files);
	free(metainfo->name);
	free(metainfo->client);
	free(metainfo->source);
	free(metainfo->publisher);
	free(metainfo->publisher_url);
	free(metainfo->comment);
	free(metainfo);
}

/**
 * note that strings of path get removed from path blist and their values are changed to NULL, to prevent the need for malloc. path list itself is not freed, only string ownership is transfered and strings are NULLed. some strings may be left in path list elements. existing files are overwritten and/or changed to dirs.
 *
 * @param root	[io]	pointer to root inode of torrent -- &(metainfo->files)
 * @param path	[io]	first element of bencoding list describing the path
 * @param size	[in]	size of file to be inserted
 * @param prnt	[in]	parent/caller object -- used internally, user should call with NULL
 * @return 1 on success, 0 on failure if path element had a NULL value, 2 if a padding file was skipped
 */

static int insert_file (struct inode ** root, struct bencoding * path, inode_length_t size, struct inode * prnt) {
tail_recursion:
	if (!path)
		return 1;
	if (!path->value)
		return 0;
	if (!strcmp(path->value, ".pad") || !strncmp(path->value, "_____padding_file_", strlen("_____padding_file_")))
		return 2;
	if (!*root) {
		*root = calloc(1, sizeof(struct inode));
		(*root)->parent = prnt;
		(*root)->name = path->value;
		path->value = NULL;
		(*root)->length += size;
		return insert_file(&(*root)->child, path->next, size, *root);
	}
	if (!path->next) {
		struct inode * file = calloc(1, sizeof(struct inode));
		file->length = size;
		file->parent = prnt;
		file->name = path->value;
		path->value = NULL;
		file->next = (*root)->next;
		(*root)->next = file;
		return 1;
	}
	if (strcmp((*root)->name, path->value)) {
		root = &(*root)->next;
		goto tail_recursion;
	} else {
		(*root)->length += size;
		return insert_file(&(*root)->child, path->next, size, *root);
	}
}

/**
 * parses a metainfo file tree into inode file tree. same as with insert_file, values are removed -- original is destroyed, but must stil be freed -- it will be together with metainfo decoded object
 *
 * @param root	[io]	pointer to root inode of torrent -- user sets this to &metainfo->files
 * @param dict	[io]	first element of file tree dict -- user sets this to bpath(file, "info/file tree")->child
 * @param cunt	[out]	increment this variable on every added file. may be NULL.
 * @param prnt	[in]	parent/caller object -- used internally, user should call with NULL
 * @return sum length of the filetree
 */

static inode_length_t filetree (struct inode ** root, struct bencoding * dict, unsigned * cunt, struct inode * prnt) {
	if (!dict)
		return 0;
	assert(!*root);
	if (!dict->key->valuelen) {
		struct bencoding * length = bpath(dict, "length");
		if (cunt)
			(*cunt)++;
		if (length)
			return (prnt->length = length->intvalue); // XXX crash if root element is ""
		return 0;
	}
	*root = calloc(1, sizeof(struct inode));
	(*root)->parent = prnt;
	(*root)->name = dict->key->value; // XXX crash
	dict->key->value = NULL;
	return ((*root)->length = filetree(&(*root)->child, dict->child, cunt, *root) + filetree(&(*root)->next, dict->next, cunt, prnt));
}

/**
 * parses a torrent bencoding
 *
 * @param data	[in]	metainfo dict as bytes
 * @param size	[in]	length of data
 * @return allocated metainfo object on heap, memory ownership is transfered to the caller
 */

struct metainfo * parse (const char * data, off_t size) {
	struct bencoding * metainfo = bdecode(data, size, 0);
	struct metainfo * r = calloc(1, sizeof(struct metainfo));
#define EXTRACT(attribute, localvar, from) \
	struct bencoding * localvar = bpath(metainfo, from); \
	if (localvar && localvar->valuelen) { \
		free(r->attribute); \
		r->attribute = localvar->value; \
		localvar->value = NULL; /* this is nonstandard, but it's a monorepo */ \
	}
	EXTRACT(name, name, "info/name");
	EXTRACT(name, nameutf8, "info/name.utf-8");
	EXTRACT(client, client, "source/v");
	EXTRACT(source, source, "info/source");
	EXTRACT(publisher, publisher, "info/publisher");
	EXTRACT(publisher, publisherutf8, "info/publisher.utf-8");
	EXTRACT(publisher_url, publisher_url, "info/publisher-url");
	EXTRACT(publisher_url, publisher_urlutf8, "info/publisher-url.utf-8");
	EXTRACT(comment, comment, "comment");
	struct bencoding * retrieved = bpath(metainfo, "creation date");
	if (retrieved && retrieved->valuelen)
		r->retrieved = atoi(retrieved->value);
	struct bencoding * created = bpath(metainfo, "info/creation date");
	if (created && created->intvalue)
		r->created = created->intvalue;
	char * ip = bpath(metainfo, "source/ip")->value; // XXX crash if non-travnik torrents
	char * slash = strchr(ip, '/');
	*slash = '\0'; // XXX crash if non-travnik torrents
	r->ip.sin6_family = AF_INET6;
	r->ip.sin6_port = htons(atoi(++slash));
	inet_pton(AF_INET6, ip, r->ip.sin6_addr.s6_addr);
	struct bencoding * files = bpath(metainfo, "info/files");
	if (files) {
		r->type = V1;
		bforeach (files, file) {
			struct bencoding * attr = bpath(file, "attr");
			if (attr && attr->valuelen && strchr(attr->value, 'p'))
			  	continue;
			inode_length_t len = bpath(file, "length")->intvalue; // XXX crash
			r->length += len;
			r->filecount++;
			insert_file(&r->files, bpath(file, "path")->child, len, NULL); // XXX crash
		}
	}
	struct bencoding * file_tree = bpath(metainfo, "info/file tree");
	if (file_tree && file_tree->child) {
		r->type |= V2;
		if (!r->files)
			r->length = filetree(&r->files, file_tree->child, &r->filecount, NULL);
	}
	if (!r->files) {
		r->filecount = 1;
		r->type = V1;
		r->files = calloc(1, sizeof(struct inode));
		r->files->name = strdup(r->name); // XXX crash
		r->length = r->files->length = bpath(metainfo, "info/length")->intvalue; // XXX crash
	}
	if (!r->files->next && !r->files->child && !r->files->length)
		r->files->length = bpath(metainfo, "info/length")->intvalue; // XXX crash
	char * start = strstr(data, "4:info")+6; // XXX crash if non-travnik torrents
	ptrdiff_t len = bpath(metainfo, "info")->after-start; // XXX crash if non-travnik torrents
	SHA1_CTX sha1; // we abuse ->after private variable. this is nonstandard, but it's a monorepo
	SHA1Init(&sha1);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
	SHA1Update(&sha1, start, len);
#pragma GCC diagnostic pop
	SHA1Final(r->sha1, &sha1);
	SHA2_CTX sha2;
	SHA256Init(&sha2);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-sign"
	SHA256Update(&sha2, start, len);
#pragma GCC diagnostic pop
	SHA256Final(r->sha256, &sha2);
	free_bencoding(metainfo);
	return r;
}

/**
 * parse all .torrent files in directory
 *
 * @param path	[in]	path to directory
 * @param out	[out]	pointer to variable into which a pointer to the array containing pointers to metainfo. you must metainfo_free() all elements of array and array itself.
 * @return number of metainfo structures created or -errno on failure
 */

int directory (const char * path, struct metainfo *** out) {
	size_t size = 65536;
	int dirfd = open(path, O_DIRECTORY | O_CLOEXEC | O_RDONLY);
	if (dirfd == -1)
		return -errno;
	int fd2 = dup(dirfd);
	if (fd2 == -1) {
		int err = errno;
		close(dirfd);
		return -err;
	}
	DIR * dir = fdopendir(fd2);
	if (!dir) {
		int err = errno;
		close(fd2);
		close(dirfd);
		return -err;
	}
	*out = malloc(65535*sizeof(*out));
	struct dirent * dirent;
	unsigned i = 0;
	while ((dirent = readdir(dir))) {
		if (!(i % 1000)) {
			fprintf(stderr, "%u\n", i);
		}
		if (dirent->d_type != DT_REG)
			continue;
		if (!strstr(dirent->d_name, ".torrent"))
			continue;
		if (i+1 >= size) {
			size *= 2;
			*out = reallocarray(*out, size, sizeof(*out));
		}
		int fd = openat(dirfd, dirent->d_name, O_CLOEXEC | O_RDONLY);
		if (fd == -1) {
			error_at_line(0, errno, __FILE__, __LINE__, "openat(dirfd, %s, O_CLOEXEC | O_RDONLY)", dirent->d_name);
			continue;
		}
		struct stat statbuf;
		if (fstat(fd, &statbuf) == -1) {
			error_at_line(0, errno, __FILE__, __LINE__, "stat(%s)", dirent->d_name);
			close(fd);
			continue;
		}
		char * content = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
		if (content == MAP_FAILED) {
			error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %lu, PROT_READ, MAP_SHARED, %s, 0)", statbuf.st_size, dirent->d_name);
			close(fd);
			continue;
		}
		(*out)[i++] = parse(content, statbuf.st_size);
		munmap(content, statbuf.st_size);
		close(fd);
	}
	closedir(dir);
	close(dirfd);
	return i;
}

#if __INCLUDE_LEVEL__ == 0
#include <lib.c>
#include <time.h>
#include <locale.h>
#define S0(x) ((x) ? (x) : "")
int main (int argc, char ** argv) {
	setlocale(LC_ALL, "");
	int r = 0;
	char * content = NULL;
	if (argc < 1+1)
		error_at_line(1, 0, __FILE__, __LINE__, "%s metainfo.torrent", S0(argv[0]));
	int fd = open(argv[1], O_CLOEXEC | O_RDONLY);
	if (fd == -1)
		error_at_line(2, errno, __FILE__, __LINE__, "open(%s, O_CLOEXEC | O_RDONLY)", argv[1]);
	struct stat statbuf;
	if (fstat(fd, &statbuf) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "stat(%s)", argv[1]);
		r = 3;
		goto r;
	}
	if (!(content = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) {
		error_at_line(0, errno, __FILE__, __LINE__, "mmap(NULL, %lu, PROT_READ, MAP_SHARED, %s, 0)", statbuf.st_size, argv[1]);
		r = 4;
		goto r;
	}
	struct metainfo * metainfo = parse(content, statbuf.st_size);
	char bufip[INET6_ADDRSTRLEN];
	char created[256];
	char retrieved[256];
	strftime(created, 256, "%c", localtime(&metainfo->created));
	strftime(retrieved, 256, "%c", localtime(&metainfo->retrieved));
	char sha1[41];
	char sha256[65];
	bin2hex(sha1, metainfo->sha1, 20);
	bin2hex(sha256, metainfo->sha256, 32);
	sha1[40] = '\0';
	sha256[64] = '\0';
	printf("name: %s\nsha1: %s\nsha256: %s\ntype: %s\nlength: %f GiB\nfilecount: %u\nclient: %s\nsource: %s\npublisher: %s\npublisher_url: %s\ncomment: %s\nip: %s/%u\ncreated: %s\nretrieved: %s\nfiles:\n", metainfo->name, sha1, sha256, type_str[metainfo->type], (float) metainfo->length/(1024*1024*1024), metainfo->filecount, S0(metainfo->client), S0(metainfo->source), S0(metainfo->publisher), S0(metainfo->publisher_url), S0(metainfo->comment), inet_ntop(AF_INET6, metainfo->ip.sin6_addr.s6_addr, bufip, INET6_ADDRSTRLEN), ntohs(metainfo->ip.sin6_port), metainfo->created ? created : "", retrieved); // XXX crash if non-travnik torrent
	int leap = 0;
	int depth = 0;
	for (const struct inode * inode = metainfo->files; inode; inode = next_inode(inode, &leap)) {
		depth -= leap;
		putchar('\t');
		for (int i = 0; i < depth; i++)
			fwrite("|\t", 1, 2, stdout);
		printf("%s %s (%f MiB)\n", inode->child ? "\\" : "*", inode->name, (float) inode->length/(1024*1024));
	}
	metainfo_free(metainfo);
	metainfo = NULL;
	struct metainfo ** metainfos;
	directory(".", &metainfos);
	r:
	if (content && munmap(content, statbuf.st_size))
		error_at_line(0, errno, __FILE__, __LINE__, "munmap(%s, %lu)", argv[1], statbuf.st_size);
	if (close(fd) == -1)
		error_at_line(0, errno, __FILE__, __LINE__, "close(%s)", argv[1]);
	return r;
}
#endif