shithub: riscv

Download patch

ref: abb4bad701e25836ebff6d8c8797710eb2a5ac27
parent: ed9e9f98e9cc502c72b27c68612e9e187ec11e10
author: mischief <[email protected]>
date: Sat Feb 1 07:04:30 EST 2014

ip/torrent: print tracker errors/warnings in debug mode and allow setting peerid

--- a/sys/man/1/torrent
+++ b/sys/man/1/torrent
@@ -23,6 +23,9 @@
 ] [
 .B -c
 ] [
+.B -i
+.I peer-id
+] [
 .I file
 ]
 .SH DESCRIPTION
@@ -104,6 +107,17 @@
 option can be given to cause the completed and total number of
 pieces written as a line of text to standard-output in one
 second intervals.
+.PP
+The
+.B -i
+option allows you to set the 20-byte
+.I peer-id
+that is sent to trackers and peers. If less than 20 bytes, the
+.I peer-id
+will be padded on the right with random ASCII numbers. This is
+useful to fool trackers that filter clients based on the
+.I peer-id
+.
 .SH EXAMPLES
 Create new torrent file
 .EX
--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -853,6 +853,15 @@
 			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);
@@ -1206,6 +1215,9 @@
 	case 'd':
 		debug++;
 		break;
+	case 'i':
+		strncpy((char*)peerid, EARGF(usage()), sizeof(peerid));
+		break;
 	default:
 		usage();
 	} ARGEND;
@@ -1339,8 +1351,9 @@
 	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)