]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/ip/torrent.c
ip/torrent: avoid peerid collision using truerand() instead of time(0)
[plan9front.git] / sys / src / cmd / ip / torrent.c
index 8eb858ccf67800f7bcf7f9bb27265a69a96d24da..fc324977bcfca04ce8a51184529c49f02f47a1cf 100644 (file)
@@ -49,10 +49,12 @@ enum {
 };
 
 int debug;
+int nproc = 1;
 int killgroup = -1;
 int port = 6881;
 char *deftrack = "http://exodus.desync.com/announce";
 char *mntweb = "/mnt/web";
+char *useragent = "torrent";
 uchar infohash[20];
 uchar peerid[20];
 int blocksize;
@@ -191,11 +193,11 @@ rwpiece(int wr, int index, uchar *data, int len, int poff)
        int n, m;
        File *f;
 
-       if(len <= 0 || poff >= pieces[index].len)
+       if(len <= 0 || poff < 0 || poff >= pieces[index].len)
                return 0;
        if(len+poff > pieces[index].len)
                len = pieces[index].len - poff;
-       off = (vlong)index * blocksize;
+       off = (vlong)index * (vlong)blocksize;
        off += poff;
        for(f = files; f; f = f->next)
                if((f->off+f->len) > off)
@@ -449,11 +451,15 @@ peer(int fd, int incoming, char *addr)
                }
                if(!hechoking && mewant){
                        x = workpiece;
-                       if(x >= 0 && pieces[x].brk < pieces[x].len)
-                               {}
-                       else x = pickpiece(map);
+                       if(x < 0 || (havemap[x>>3]&(0x80>>(x&7))) != 0)
+                               x = pickpiece(map);
                        if(x >= 0){
+                               workpiece = x;
                                o = pieces[x].brk;
+                               if(o < 0 || o >= pieces[x].len){
+                                       pieces[x].brk = 0;
+                                       o = 0;
+                               }
                                l = pieces[x].len - o;
                                if(l > MAXIO)
                                        l = MAXIO;
@@ -461,7 +467,6 @@ peer(int fd, int incoming, char *addr)
                                n = pack(buf, sizeof(buf), "lblll", 1+4+4+4, 0x06, x, o, l);
                                if(write(fd, buf, n) != n)
                                        goto Out;
-                               workpiece = x;
                        }
                }
                if(mechoking && hewant){
@@ -549,13 +554,22 @@ peer(int fd, int incoming, char *addr)
                        if(debug) fprint(2, "peer %s: <- piece %d %d %d\n", addr, x, o, n);
                        if(x < 0 || x >= npieces)
                                continue;
-                       if((pieces[x].brk != o) || (havemap[x>>3]&(0x80>>(x&7))))
+                       if((havemap[x>>3]&(0x80>>(x&7))) != 0)
                                continue;
-                       if(rwpiece(1, x, p, n, o) == n){
-                               if((pieces[x].brk = o+n) == pieces[x].len){
-                                       if(!havepiece(x))
-                                               pieces[x].brk = 0;
-                               }
+                       if(o < 0 || o >= pieces[x].len)
+                               continue;
+                       if(o+n > pieces[x].len)
+                               n = o - pieces[x].len;
+                       if((o > pieces[x].brk) || (o+n <= pieces[x].brk))
+                               continue;
+                       n = rwpiece(1, x, p, n, o);
+                       if(n <= 0)
+                               continue;
+                       pieces[x].brk = o+n;
+                       if(o+n >= pieces[x].len && !havepiece(x)){
+                               /* backoff from this piece for a while */
+                               if(x == workpiece)
+                                       workpiece = -1;
                        }
                        break;
                case 0x08:      // Cancel <index> <begin> <length>
@@ -627,12 +641,12 @@ server(void)
 void
 client(char *ip, char *port)
 {
-       static Dict *peers, *peerqh, *peerqt;
+       static Dict *peerqh, *peerqt;
        static QLock peerslk;
        static int nprocs;
-       int try, fd;
        char *addr;
        Dict *d;
+       int fd;
 
        if(ip == nil || port == nil)
                return;
@@ -640,7 +654,7 @@ client(char *ip, char *port)
        d = mallocz(sizeof(*d) + 64, 1);
        snprint(addr = d->str, 64, "tcp!%s!%s", ip, port);
        qlock(&peerslk);
-       if(dlook(peers, addr) || dlook(peerqh, addr)){
+       if(dlook(peerqh, addr)){
                qunlock(&peerslk);
                free(d);
                return;
@@ -648,28 +662,31 @@ client(char *ip, char *port)
        d->len = strlen(addr);
        d->typ = 'd';
        d->val = d;
-       d->next = nil;
-       if(peerqt == nil)
-               peerqh = d;
-       else
-               peerqt->next = d;
-       peerqt = d;
+       /* enqueue to front */
+       if((d->next = peerqh) == nil)
+               peerqt = d;
+       peerqh = d;
        if(nprocs >= CLIPROCS){
                qunlock(&peerslk);
                return;
        }
        nprocs++;
        qunlock(&peerslk);
-
        if(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT))
                return;
+
        for(;;){
                qlock(&peerslk);
+               /* dequeue and put to tail */
                if(d = peerqh){
                        if((peerqh = d->next) == nil)
                                peerqt = nil;
-                       d->next = peers;
-                       peers = d;
+                       d->next = nil;
+                       if(peerqt)
+                               peerqt->next = d;
+                       else
+                               peerqh = d;
+                       peerqt = d;
                } else
                        nprocs--;
                qunlock(&peerslk);
@@ -677,14 +694,11 @@ client(char *ip, char *port)
                        exits(0);
                addr = d->str;
                if(debug) fprint(2, "client %s\n", addr);
-               for(try = 0; try < 5; try++){
-                       if((fd = dial(addr, nil, nil, nil)) >= 0){
-                               if(!peer(fd, 0, addr))
-                                       break;
-                               close(fd);
-                       }
-                       sleep((1000<<try)+nrand(5000));
+               if((fd = dial(addr, nil, nil, nil)) >= 0){
+                       peer(fd, 0, addr);
+                       close(fd);
                }
+               sleep(1000+nrand(5000));
        }
 }
 
@@ -713,6 +727,10 @@ hopen(char *url, ...)
                close(ctlfd);
                return -1;
        }
+       if(useragent != nil && useragent[0] != '\0'){
+               n = snprint(buf, sizeof buf, "useragent %s", useragent);
+               write(ctlfd, buf, n);
+       }
        snprint(buf, sizeof buf, "%s/%d/body", mntweb, conn);
        if((fd = open(buf, OREAD)) < 0)
                goto ErrOut;
@@ -840,6 +858,15 @@ webtracker(char *url)
                        bparse(p, p+n, &d);
                        free(p);
                } else if(debug) fprint(2, "tracker %s: %r\n", url);
+               /* check errors and warnings */
+               if(p = dstr(dlook(d, "failure reason"))) {
+                       if(debug)
+                               fprint(2, "tracker failure: %s\n", p);
+                       exits(0);
+               }
+               if(p = dstr(dlook(d, "warning message")))
+                       if(debug)
+                               fprint(2, "tracker warning: %s\n", p);
                if(l = dlook(d, "peers")){
                        if(l->typ == 's')
                                clients4((uchar*)l->str, l->len);
@@ -1022,7 +1049,6 @@ mktorrent(int fd, Dict *alist, Dict *wlist)
                werrstr("empty file");
                return -1;
        }
-       npieces = 1;
        for(blocksize = 256*1024;;blocksize<<=1){
                npieces = (d->length + blocksize-1) / blocksize;
                if(npieces <= 8*1024 || blocksize >= 2*1024*1024)
@@ -1136,7 +1162,7 @@ void
 usage(void)
 {
        fprint(2, "usage: %s [ -vsdpc ] [ -m mtpt ] [ -t tracker-url ] "
-                 "[ -w webseed-url ] [ file ]\n", argv0);
+                 "[ -w webseed-url ] [ -i peerid ] [ -A useragent ] [ file ]\n", argv0);
        exits("usage");
 }
 
@@ -1193,10 +1219,22 @@ main(int argc, char *argv[])
        case 'd':
                debug++;
                break;
+       case 'i':
+               strncpy((char*)peerid, EARGF(usage()), sizeof(peerid));
+               break;
+       case 'A':
+               useragent = EARGF(usage());
+               break;
        default:
                usage();
        } ARGEND;
 
+       if((s = getenv("NPROC")) != 0){
+               if((nproc = atoi(s)) <= 0)
+                       nproc = 1;
+               free(s);
+       }
+
        fd = 0;
        if(*argv)
                if((fd = open(*argv, OREAD)) < 0)
@@ -1301,17 +1339,28 @@ main(int argc, char *argv[])
        if(len)
                sysfatal("pieces do not match file length");
 
-       for(i = 0; i<npieces; i++)
-               havepiece(i);
+       for(i=0; i<nproc; i++){
+               switch(rfork(RFPROC|RFMEM)){
+               case -1:
+                       sysfatal("fork: %r");
+               case 0:
+                       for(; i<npieces; i+=nproc)
+                               havepiece(i);
+                       exits(0);
+               }
+       }
+       while(waitpid() >= 0)
+               ;
 
-       srand(time(0));
+       srand(truerand());
        atnotify(catch, 1);
        switch(i = rfork(RFPROC|RFMEM|RFNOTEG)){
        case -1:
                sysfatal("fork: %r");
        case 0:
-               memmove(peerid, "-NF9001-", 8);
-               for(i=8; i<sizeof(peerid); i++)
+               if(peerid[0] == 0)
+                       strncpy((char*)peerid, "-NF9001-", 9);
+               for(i=sizeof(peerid)-1; i >= 0 && peerid[i] == 0; i--)
                        peerid[i] = nrand(10)+'0';
                server();
                for(; alist; alist = alist->next)