diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index f90a570..a956b53 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -3,6 +3,6 @@ * smartly handle queues of local and remote requests. */ -#include "ipfs/bitswap/network.h" +#include "ipfs/exchange/bitswap/network.h" diff --git a/include/ipfs/repo/config/config.h b/include/ipfs/repo/config/config.h index d51f758..d8a7037 100644 --- a/include/ipfs/repo/config/config.h +++ b/include/ipfs/repo/config/config.h @@ -7,6 +7,7 @@ #include "bootstrap_peers.h" #include "addresses.h" #include "gateway.h" +#include "replication.h" struct MDNS { int enabled; @@ -44,6 +45,7 @@ struct RepoConfig { //struct supernode_routing supernode_client_config; //struct api api; struct Reprovider reprovider; + struct Replication* replication; }; /** diff --git a/include/ipfs/repo/config/replication.h b/include/ipfs/repo/config/replication.h new file mode 100644 index 0000000..b7f1204 --- /dev/null +++ b/include/ipfs/repo/config/replication.h @@ -0,0 +1,20 @@ +#include "libp2p/utils/vector.h" + +struct Replication { + int announce_minutes; + struct Libp2pVector* nodes; +}; + +/*** + * allocate memory and initialize the replication struct + * @param replication a pointer to the struct to be allocated + * @returns true(1) on success, false(0) otherwise + */ +int repo_config_replication_new(struct Replication** replication); + +/*** + * Frees memory of a replication struct + * @param replication the replication struct + * @returns true(1); + */ +int repo_config_replication_free(struct Replication* replication); diff --git a/main/Makefile b/main/Makefile index 2b04111..bd66805 100644 --- a/main/Makefile +++ b/main/Makefile @@ -20,9 +20,7 @@ OBJS = main.o \ ../pin/pin.o \ ../repo/init.o \ ../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \ - ../repo/config/config.o ../repo/config/identity.o \ - ../repo/config/bootstrap_peers.o ../repo/config/gateway.o \ - ../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \ + ../repo/config/*.o \ ../routing/*.o \ ../thirdparty/ipfsaddr/ipfs_addr.o \ ../unixfs/unixfs.o \ diff --git a/repo/config/Makefile b/repo/config/Makefile index 0a380f8..ba90574 100644 --- a/repo/config/Makefile +++ b/repo/config/Makefile @@ -6,8 +6,8 @@ CFLAGS += -g3 endif LFLAGS = -DEPS = config.h datastore.h identity.h -OBJS = config.o identity.o bootstrap_peers.o gateway.o addresses.o swarm.o peer.o +DEPS = config.h datastore.h identity.h replication.h +OBJS = config.o identity.o bootstrap_peers.o gateway.o addresses.o swarm.o peer.o replication.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/repo/config/config.c b/repo/config/config.c index 2ad7265..37fec20 100644 --- a/repo/config/config.c +++ b/repo/config/config.c @@ -201,6 +201,9 @@ int ipfs_repo_config_new(struct RepoConfig** config) { if (retVal == 0) return 0; + if (!repo_config_replication_new(&((*config)->replication))) + return 0; + return 1; } @@ -223,6 +226,8 @@ int ipfs_repo_config_free(struct RepoConfig* config) { repo_config_addresses_free(config->addresses); if (config->gateway != NULL) repo_config_gateway_free(config->gateway); + if (config->replication != NULL) + repo_config_replication_free(config->replication); free(config); } return 1; diff --git a/repo/config/replication.c b/repo/config/replication.c new file mode 100644 index 0000000..a3e818c --- /dev/null +++ b/repo/config/replication.c @@ -0,0 +1,39 @@ +#include +#include "multiaddr/multiaddr.h" +#include "ipfs/repo/config/replication.h" + +/*** + * allocate memory and initialize the replication struct + * @param replication a pointer to the struct to be allocated + * @returns true(1) on success, false(0) otherwise + */ +int repo_config_replication_new(struct Replication** replication) { + *replication = (struct Replication*)malloc(sizeof(struct Replication)); + if (*replication == NULL) + return 0; + struct Replication* out = *replication; + out->announce_minutes = 0; + out->nodes = NULL; + return 1; +} + +/*** + * Frees memory of a replication struct + * @param replication the replication struct + * @returns true(1); + */ +int repo_config_replication_free(struct Replication* replication) { + if (replication != NULL) { + // free the vector + if (replication->nodes != NULL) { + for(int i = 0; i < replication->nodes->total; i++) { + struct MultiAddress* currAddr = libp2p_utils_vector_get(replication->nodes, i); + multiaddress_free(currAddr); + } + libp2p_utils_vector_free(replication->nodes); + } + // free the struct + free(replication); + } + return 1; +} diff --git a/repo/fsrepo/fs_repo.c b/repo/fsrepo/fs_repo.c index 5e5033b..4a51c65 100644 --- a/repo/fsrepo/fs_repo.c +++ b/repo/fsrepo/fs_repo.c @@ -471,6 +471,35 @@ int fs_repo_open_config(struct FSRepo* repo) { free(val); } } + + // replication + curr_pos = _find_token(data, tokens, num_tokens, curr_pos, "Replication"); + if (curr_pos >= 0) { + // announce minutes + curr_pos++; + _get_json_int_value(data, tokens, num_tokens, curr_pos, "AnnounceMinutes", &repo->config->replication->announce_minutes); + // nodes list + int nodes_pos = _find_token(data, tokens, num_tokens, curr_pos, "Nodes"); + if (nodes_pos >= 0) { + if (tokens[nodes_pos].type != JSMN_ARRAY) { + free(data); + return 0; + } + int nodes_size = tokens[nodes_pos].size; + repo->config->replication->nodes = libp2p_utils_vector_new(nodes_size); + nodes_pos++; + for(int i = 0; i < nodes_size; i++) { + char* val = NULL; + if (!_get_json_string_value(data, tokens, num_tokens, nodes_pos + i, NULL, &val)) + break; + struct MultiAddress* cur = multiaddress_new_from_string(val); + if (cur == NULL) + continue; + libp2p_utils_vector_add(repo->config->replication->nodes, cur); + free(val); + } + } + } // free the memory used reading the json file free(data); free(priv_key_base64); diff --git a/repo/init.c b/repo/init.c index 7ae8ae4..d8b6b5d 100644 --- a/repo/init.c +++ b/repo/init.c @@ -6,6 +6,10 @@ #include "ipfs/repo/config/config.h" #include "ipfs/repo/fsrepo/fs_repo.h" +/** + * The basic functions for initializing an IPFS repo + */ + /** * Get the correct repo home directory. This first looks at the * command line, then the IPFS_PATH environment variable, diff --git a/test/Makefile b/test/Makefile index e13b5aa..c5cc181 100644 --- a/test/Makefile +++ b/test/Makefile @@ -20,9 +20,7 @@ OBJS = testit.o test_helper.o \ ../multibase/multibase.o \ ../repo/init.o \ ../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \ - ../repo/config/config.o ../repo/config/identity.o \ - ../repo/config/bootstrap_peers.o ../repo/config/gateway.o \ - ../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \ + ../repo/config/*.o \ ../routing/offline.o \ ../routing/online.o \ ../routing/k_routing.o \