diff --git a/core/api.c b/core/api.c index ca6b96e..e83bc20 100644 --- a/core/api.c +++ b/core/api.c @@ -698,6 +698,20 @@ void *api_listen_thread (void *ptr) return NULL; } +struct ApiContext* api_context_new() { + struct ApiContext* context = (struct ApiContext*) malloc(sizeof(struct ApiContext)); + if (context != NULL) { + context->conns = NULL; + context->conns_count = 0; + context->ipv4 = 0; + context->max_conns = 0; + context->port = 0; + context->socket = 0; + context->timeout = 0; + } + return context; +} + /** * Start API interface daemon. * @param local_node the context @@ -716,6 +730,12 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout) multiaddress_get_ip_address(my_address, &ip); int port = multiaddress_get_ip_port(my_address); + local_node->api_context = api_context_new(); + if (local_node->api_context == NULL) { + multiaddress_free(my_address); + return 0; + } + local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback. local_node->api_context->port = port; diff --git a/core/ipfs_node.c b/core/ipfs_node.c index 1124ecb..cf0dff2 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -23,6 +23,7 @@ struct IpfsNode* ipfs_node_new() { node->providerstore = NULL; node->repo = NULL; node->routing = NULL; + node->api_context = NULL; } return node; } @@ -151,7 +152,7 @@ int ipfs_node_offline_new(const char* repo_path, struct IpfsNode** node) { */ int ipfs_node_free(struct IpfsNode* node) { if (node != NULL) { - if (node->api_context->api_thread != NULL) + if (node->api_context != NULL && node->api_context->api_thread != 0) api_stop(node); if (node->exchange != NULL) { node->exchange->Close(node->exchange); diff --git a/test/core/test_api.h b/test/core/test_api.h index 45c5868..bd21f8c 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -20,6 +20,7 @@ int test_core_api_startup_shutdown() { test_daemon_start(repo_path); sleep(3); + // make a client to the api struct IpfsNode* client_node = NULL; if (!ipfs_node_offline_new(repo_path, &client_node)) { goto exit;