From 9a49ddd27b48e5172bcaf5b18cf16ad310a87d72 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Wed, 20 Sep 2017 09:11:01 -0500 Subject: [PATCH] Start of client_api.c --- core/Makefile | 2 +- core/client_api.c | 55 ++++++++++++++++++++++++++++++++++ core/ipfs_node.c | 6 +++- importer/exporter.c | 25 ++++++++++------ include/ipfs/core/client_api.h | 8 +++++ include/ipfs/core/ipfs_node.h | 18 ++++++++++- test/core/test_api.h | 27 ++++++++++++----- test/core/test_null.h | 1 + 8 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 core/client_api.c create mode 100644 include/ipfs/core/client_api.h diff --git a/core/Makefile b/core/Makefile index 8dac3a1..3cf3719 100644 --- a/core/Makefile +++ b/core/Makefile @@ -7,7 +7,7 @@ endif LFLAGS = DEPS = builder.h ipfs_node.h -OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o +OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o client_api.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/core/client_api.c b/core/client_api.c new file mode 100644 index 0000000..eef4c49 --- /dev/null +++ b/core/client_api.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipfs/core/client_api.h" +#include "multiaddr/multiaddr.h" + +/** + * Determine if the API is running (by attempting to connect to the port) + * @param local_node the context + * @returns true(1) on success, false(0) otherwise + */ +int api_running(struct IpfsNode* local_node) { + struct MultiAddress* my_multiaddress = multiaddress_new_from_string(local_node->repo->config->addresses->api); + char* ip = NULL; + int portno = 0; + + if (my_multiaddress == NULL) { + return 0; + } + + portno = multiaddress_get_ip_port(my_multiaddress); + multiaddress_get_ip_address(my_multiaddress, &ip); + + int sockfd; + struct sockaddr_in serv_addr; + struct hostent *server; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + return 0; + } + + server = gethostbyname(ip); + + if (server == NULL) { + return 0; + } + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *)server->h_addr, + (char *)&serv_addr.sin_addr.s_addr, + server->h_length); + + serv_addr.sin_port = htons(portno); + int retVal = connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); + close(sockfd); + return retVal >= 0; +} diff --git a/core/ipfs_node.c b/core/ipfs_node.c index e48d8cf..be42e82 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -5,6 +5,7 @@ #include "libp2p/secio/secio.h" #include "libp2p/routing/dht_protocol.h" #include "ipfs/core/api.h" +#include "ipfs/core/client_api.h" #include "ipfs/core/ipfs_node.h" #include "ipfs/exchange/bitswap/bitswap.h" #include "ipfs/journal/journal.h" @@ -132,6 +133,9 @@ int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { local_node->routing = ipfs_routing_new_offline(local_node, &fs_repo->config->identity->private_key); local_node->exchange = ipfs_bitswap_new(local_node); + if (api_running(local_node)) + local_node->mode = MODE_API_AVAILABLE; + return 1; } @@ -157,7 +161,7 @@ int ipfs_node_free(struct IpfsNode* node) { if (node->mode == MODE_ONLINE) { ipfs_routing_online_free(node->routing); } - if (node->mode == MODE_OFFLINE) { + if (node->mode == MODE_OFFLINE || node->mode == MODE_API_AVAILABLE) { ipfs_routing_offline_free(node->routing); } if (node->blockstore != NULL) { diff --git a/importer/exporter.c b/importer/exporter.c index 89d964e..3b7f011 100644 --- a/importer/exporter.c +++ b/importer/exporter.c @@ -270,19 +270,26 @@ int ipfs_exporter_object_cat(int argc, char** argv) { return 0; } - if (!ipfs_node_online_new(repo_dir, &local_node)) + if (!ipfs_node_offline_new(repo_dir, &local_node)) return 0; - // find hash - // convert hash to cid - struct Cid* cid = NULL; - if ( ipfs_cid_decode_hash_from_base58((unsigned char*)argv[2], strlen(argv[2]), &cid) == 0) { + if (local_node->mode == MODE_API_AVAILABLE) { + // TODO: we should use the api for this return 0; + } else { + // find hash + // convert hash to cid + struct Cid* cid = NULL; + if ( ipfs_cid_decode_hash_from_base58((unsigned char*)argv[2], strlen(argv[2]), &cid) == 0) { + return 0; + } + + int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, stdout); + ipfs_cid_free(cid); + + return retVal; } - int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, stdout); - ipfs_cid_free(cid); - - return retVal; + return 0; } diff --git a/include/ipfs/core/client_api.h b/include/ipfs/core/client_api.h new file mode 100644 index 0000000..4460ea7 --- /dev/null +++ b/include/ipfs/core/client_api.h @@ -0,0 +1,8 @@ +#include "ipfs/core/ipfs_node.h" + +/** + * Determine if the API is running + * @param local_node the context + * @returns true(1) on success, false(0) otherwise + */ +int api_running(struct IpfsNode* local_node); diff --git a/include/ipfs/core/ipfs_node.h b/include/ipfs/core/ipfs_node.h index 83c45d6..ba2c829 100644 --- a/include/ipfs/core/ipfs_node.h +++ b/include/ipfs/core/ipfs_node.h @@ -8,9 +8,25 @@ #include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/routing/routing.h" -enum NodeMode { MODE_OFFLINE, MODE_ONLINE }; +/*** + * Holds information about the local node + */ + +/*** + * Modes: + * MODE_OFFLINE: Do everything yourself + * MODE_API_AVAILABLE: If you want to, the API is running + * MODE_ONLINE: You are the API + */ +enum NodeMode { MODE_OFFLINE, MODE_API_AVAILABLE, MODE_ONLINE }; struct IpfsNode { + /*** + * Modes: + * MODE_OFFLINE: Do everything yourself + * MODE_API_AVAILABLE: If you want to, the API is running + * MODE_ONLINE: You are the API + */ enum NodeMode mode; struct Identity* identity; struct FSRepo* repo; diff --git a/test/core/test_api.h b/test/core/test_api.h index 1a1ae87..dd781b0 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -1,9 +1,9 @@ #include "../test_helper.h" -#include "ipfs/core/api.h" #include "libp2p/utils/logger.h" +#include "ipfs/core/client_api.h" +#include "ipfs/core/daemon.h" int test_core_api_startup_shutdown() { - struct IpfsNode* local_node = NULL; char* repo_path = "/tmp/ipfs_1"; char* peer_id = NULL; int retVal = 0; @@ -12,15 +12,28 @@ int test_core_api_startup_shutdown() { goto exit; // this should start the api - if (!ipfs_node_online_new(repo_path, &local_node)) + test_daemon_start(repo_path); + sleep(3); + + struct IpfsNode* client_node = NULL; + if (!ipfs_node_offline_new(repo_path, &client_node)) { + goto exit; + } + // test to see if it is working + if (client_node->mode == MODE_API_AVAILABLE) goto exit; - - // TODO: test to see if it works - - // TODO shut down retVal = 1; + // cleanup exit: + ipfs_daemon_stop(); + if (peer_id != NULL) + free(peer_id); return retVal; } + +int test_core_api_local_object_cat() { + // add a file to the store, then use the api to get it + return 0; +} diff --git a/test/core/test_null.h b/test/core/test_null.h index 15612d2..b160613 100644 --- a/test/core/test_null.h +++ b/test/core/test_null.h @@ -1,4 +1,5 @@ #include +#include #include "ipfs/importer/importer.h" int test_null_add_provider() {