Merge 2855c573630a71cea2b2de10123cadb0541c4cf3 into 41f5bb42d7171bc20bf32d0e96ea766e022a5fcd

This commit is contained in:
Eric 2026-02-23 21:25:27 +11:00 committed by GitHub
commit f749ccbb1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 82 additions and 10 deletions

View File

@ -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 <SPR> <CID> ./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 <SPR> <CID> ./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

View File

@ -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);
}

View File

@ -13,6 +13,7 @@ typedef struct {
char *data_dir;
char *log_level;
char *bootstrap_node;
char *listen_addr;
char *nat;
} node_config;

View File

@ -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);

View File

@ -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)...");