diff --git a/README.md b/README.md index 7979980..b74e53f 100644 --- a/README.md +++ b/README.md @@ -86,19 +86,86 @@ Commands: `help`, `start`, `stop`, `upload`, `download`, `quit`. ### uploader / downloader -Standalone programs that demonstrate file sharing between two nodes. On one machine, start the uploader: +Standalone programs that demonstrate file sharing between two nodes. + +Optionally generate a file to upload. For example, using `dd` on nix-based systems: + +```bash +# generate a 500MB file with random contents +dd if=/dev/urandom of=myfile.txt bs=1048576 count=500 +``` + +In one terminal, start the uploader, passing in the path to a file to upload: ```bash ./build/uploader ./myfile.txt # prints: Run: downloader ./output-file ``` -On another (or the same machine), run the downloader with the printed values: +In another terminal, run the downloader with the printed values: ```bash ./build/downloader ./output-file ``` +#### Connecting to remote peers + +To run the uploader/downloader example, but connect to peers remotely, the config will need to be updated before it will work. + +##### UPnP / PMP + +The easiest way to connect to remote peers is to enable UPnP or PMP in your router. Once this is enabled, update the config to: + +```c +// uploader.c + node_config cfg = { + .api_port = 8080, + .disc_port = 9090, + .data_dir = "./uploader-data", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .nat = "upnp" // or "pmp" or "any", +}; + +// downlaoder.c +node_config cfg = { + .api_port = 8081, + .disc_port = 9091, + .data_dir = "./downloader-data", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .nat = "upnp" // or "pmp" or "any", +}; +``` + +By specifying a common bootstrap node, the uploader and downloader will be able to share DHT entries. Specifically, the uploader will announce itself as a provider for the CID in the DHT to its closes neighbours (the bootstrap node), and the downloader will be able to traverse its closest peers when finding a provider for the CID. + +#### Connecting to peers on the same network or machine + +To connect to peers locally, the same config updates need to be made, except now, `nat` should be set to `none`: + +```c +// uploader.c + node_config cfg = { + .api_port = 8080, + .disc_port = 9090, + .data_dir = "./uploader-data", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .nat = "none" +}; + +// downlaoder.c +node_config cfg = { + .api_port = 8081, + .disc_port = 9091, + .data_dir = "./downloader-data", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .nat = "none" +}; +``` + ## Testing ```bash diff --git a/easystorage.c b/easystorage.c index 2498004..11aa795 100644 --- a/easystorage.c +++ b/easystorage.c @@ -180,6 +180,10 @@ STORAGE_NODE e_storage_new(node_config config) { pos += snprintf(json + pos, sizeof(json) - pos, ",\"bootstrap-node\":[\"%s\"]", config.bootstrap_node); } + if (config.listen_addr) { + pos += snprintf(json + pos, sizeof(json) - pos, ",\"listen-addrs\":[\"%s\"]", config.listen_addr); + } + if (config.nat) { pos += snprintf(json + pos, sizeof(json) - pos, ",\"nat\":\"%s\"", config.nat); } diff --git a/easystorage.h b/easystorage.h index d952a54..820c615 100644 --- a/easystorage.h +++ b/easystorage.h @@ -13,6 +13,7 @@ typedef struct { char *data_dir; char *log_level; char *bootstrap_node; + char *listen_addr; char *nat; } node_config; diff --git a/examples/downloader.c b/examples/downloader.c index 79482c0..3018dd7 100644 --- a/examples/downloader.c +++ b/examples/downloader.c @@ -28,9 +28,9 @@ int main(int argc, char *argv[]) { .api_port = 8081, .disc_port = 9091, .data_dir = "./downloader-data", - .log_level = "INFO", - .bootstrap_node = spr, - .nat = "none", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .listen_addr = "/ip4/0.0.0.0/tcp/65032", }; STORAGE_NODE node = e_storage_new(cfg); diff --git a/examples/uploader.c b/examples/uploader.c index 883cef9..a72d6ab 100644 --- a/examples/uploader.c +++ b/examples/uploader.c @@ -24,9 +24,9 @@ int main(int argc, char *argv[]) { .api_port = 8080, .disc_port = 9090, .data_dir = "./uploader-data", - .log_level = "INFO", - .bootstrap_node = NULL, - .nat = "none", + .log_level = "TRACE", + .bootstrap_node = "spr:CiUIAhIhApIj9p6zJDRbw2NoCo-tj98Y760YbppRiEpGIE1yGaMzEgIDARpJCicAJQgCEiECkiP2nrMkNFvDY2gKj62P3xjvrRhumlGISkYgTXIZozMQvcz8wQYaCwoJBAWhF3WRAnVEGgsKCQQFoRd1kQJ1RCpGMEQCIFZB84O_nzPNuViqEGRL1vJTjHBJ-i5ZDgFL5XZxm4HAAiB8rbLHkUdFfWdiOmlencYVn0noSMRHzn4lJYoShuVzlw", + .listen_addr = "/ip4/0.0.0.0/tcp/65032", }; char *filepath = argv[1]; @@ -40,8 +40,8 @@ int main(int argc, char *argv[]) { char *spr = e_storage_spr(node); if (spr == NULL) panic("Failed to obtain node's Signed Peer Record (SPR)"); - printf("Run: downloader %s %s ./output-file\n", spr, cid); - printf("\nPress Enter to exit\n"); + printf("Run: ./build/downloader %s %s ./output-file\n", spr, cid); + printf("\nKeep running while downloading.\n\nPress Enter to exit when finished.\n"); getchar(); printf("Deleting file (this could take a while)...");