From 2f07cea8b7410248f36a851310e8ee65f3ad6f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Wed, 22 Feb 2023 23:42:59 +0100 Subject: =?UTF-8?q?flask=20je=20prepo=C4=8Dasen=20=3D>=20src/metainfo.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- analiza/zvezek.ipynb | 2 +- makefile | 3 ++ src/bencoding.c | 3 ++ src/metainfo.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 6 ++-- travnik.py | 25 ++++++++++++--- www/app.py | 17 +++++++--- 7 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 src/metainfo.c diff --git a/analiza/zvezek.ipynb b/analiza/zvezek.ipynb index b88677b..9486705 100644 --- a/analiza/zvezek.ipynb +++ b/analiza/zvezek.ipynb @@ -15,7 +15,7 @@ "......\n", "fixed bad single file torrent 4f269d8aefd647ee270842d53ec98aebd23a4afe\n", "fixed bad single file torrent 7b09ae0b612dafc1744562dccbbe4becf4d633c3\n", - "38021 @ 413.0262871221639 s\n" + "43143 @ 501.31311491318047 s\n" ] } ], diff --git a/makefile b/makefile index 15f54a9..f32985e 100644 --- a/makefile +++ b/makefile @@ -11,6 +11,9 @@ tmp: travnik: src/main.c $(CC) $(MYCFLAGS) $(CFLAGS) $< -o$@ $(MYLDFLAGS) $(LDFLAGS) +db: src/db.c + $(CC) $(MYCFLAGS) $(CFLAGS) $< -o$@ $(MYLDFLAGS) $(LDFLAGS) + utils: tmp/midpoint tmp/bencoding tmp/dns tmp/info tmp/%: utils/%.c src/dht.c tmp diff --git a/src/bencoding.c b/src/bencoding.c index 9ff9f89..6e8a2a2 100644 --- a/src/bencoding.c +++ b/src/bencoding.c @@ -1,3 +1,6 @@ +#include +#include +#include #include /** diff --git a/src/metainfo.c b/src/metainfo.c new file mode 100644 index 0000000..1a0adcc --- /dev/null +++ b/src/metainfo.c @@ -0,0 +1,90 @@ +#include +struct inode { + char * name; + int length; + struct inode * next; + struct inode * child; + struct inode * parent; +}; +struct inode * next (struct inode * inode) { + if (inode->child) + return next(inode->child); + if (inode->next) { + if (inode->next->child) + return next(inode->next->child); + return inode->next; + } + struct inode * predecesor = inode; + while (!predecesor->next) { + predecesor = predecesor->parent; + if (!predecesor) + return NULL; + } + return predecesor->next; +} +enum type { + UNSPEC = 0, + V1 = 1, + V2 = 2 + HYBRID = 1 | 2 +} +struct metainfo { + struct inode * files; + struct sockaddr_in6 ip; + time_t retrieved; + time_t created; + enum type type; + char sha1[20]; + char sha256[32]; + char * name; + char * client; + char * source; + char * publisher; + char * publisher_url; + char * comment; +}; +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); +} +struct metainfo * parse (const struct bencoding * metainfo) { + struct metainfo * r = calloc(1, sizeof(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, "info/creation date"); + if (retrieved && retrieved->valuelen) + r->retrieved = atoi(retrieved->value); + struct bencoding * created = bpath(metainfo, "creation date"); + if (created && created->intvalue) + r->created = created->intvalue; + struct bencoding * files = bpath(metainfo, "info/files"); + if (files) { + r->type = V1; + bforeach (files, file) { + struct attr * bpath(file, "attr"); + if (attr && attr->valuelen && strchr(attr, 'p')) + continue; + struct path * bpath(file, "path"); + } + } +} diff --git a/templates/index.html b/templates/index.html index 2547750..9c0a3f1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,7 +10,7 @@

- travnik + travnik