Added ipns support for cat/get commands.

master
Jose Marcial Vieira Bisneto 2018-10-25 19:14:47 -03:00
parent 94b782cfaa
commit 9491e69d76
No known key found for this signature in database
GPG Key ID: 103E935E7E6E831E
4 changed files with 54 additions and 19 deletions

View File

@ -11,6 +11,8 @@
#include "ipfs/repo/init.h"
#include "ipfs/core/ipfs_node.h"
#include "libp2p/utils/logger.h"
#include "ipfs/namesys/name.h"
#include "ipfs/repo/fsrepo/jsmn.h"
/**
* pull objects from ipfs
@ -286,8 +288,23 @@ int ipfs_exporter_object_cat(struct CliArguments* args, FILE* output_file) {
}
if (local_node->mode == MODE_API_AVAILABLE) {
const char ipns_prefix[] = "/ipns/";
const char ipfs_prefix[] = "/ipfs/";
char* hash = args->argv[args->verb_index + 1];
libp2p_logger_debug("exporter", "We're attempting to use the API for this object get of %s.\n", hash);
if (memcmp(hash, ipfs_prefix, sizeof(ipfs_prefix)-1) == 0) {
// skip ipfs_prefix;
hash += sizeof(ipfs_prefix)-1;
} else if (memcmp(hash, ipns_prefix, sizeof(ipns_prefix)-1) == 0) {
char *response = NULL;
size_t response_size;
if (ipfs_name_resolve(local_node, hash, &response, &response_size) && response && response_size > 0) {
hash = jsmn_simple_parser(response, response_size, "Path");
if (!hash) {
return 0;
}
}
}
struct HttpRequest* request = ipfs_core_http_request_new();
char* response = NULL;
request->command = "object";

View File

@ -2,6 +2,16 @@
#include "ipfs/cmd/cli.h"
/***
* Publish IPNS name
*/
int ipfs_name_publish(struct IpfsNode* local_node, char* name, char **response, size_t *response_size);
/***
* Resolve IPNS name
*/
int ipfs_name_resolve(struct IpfsNode* local_node, char* name, char **response, size_t *response_size);
/**
* We received a cli command "ipfs name". Do the right thing.
* @param argc number of arguments on the command line

View File

@ -11,39 +11,43 @@
/***
* Publish IPNS name
*/
int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
int ipfs_name_publish(struct IpfsNode* local_node, char* name, char **response, size_t *response_size) {
// call api.
char* response = NULL;
struct HttpRequest* request = ipfs_core_http_request_new();
if (request == NULL)
return 0;
request->command = "name";
request->sub_command = "publish";
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
}
int retVal = ipfs_core_http_request_post(local_node, request, response, response_size, "", 0);
ipfs_core_http_request_free(request);
return retVal;
}
int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
/***
* Resolve IPNS name
*/
int ipfs_name_resolve(struct IpfsNode* local_node, char* name, char **response, size_t *response_size) {
// ask api
char* response = NULL;
const char prefix[] = "/ipns/";
char *ipns = NULL;
struct HttpRequest* request = ipfs_core_http_request_new();
if (request == NULL)
return 0;
request->command = "name";
request->sub_command = "resolve";
if (memcmp(name, prefix, sizeof(prefix)-1)!=0) {
ipns = malloc(sizeof(prefix) + strlen(name));
if (ipns) {
strcpy(ipns, prefix);
strcat(ipns, name);
name = ipns;
}
}
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
int retVal = ipfs_core_http_request_post(local_node, request, response, response_size, "", 0);
if (ipns) {
free(ipns);
}
ipfs_core_http_request_free(request);
return retVal;
@ -77,15 +81,21 @@ int ipfs_name(struct CliArguments* args) {
goto exit;
}
char *response = NULL;
size_t response_size;
// determine what we're doing
if (strcmp(which, "publish") == 0) {
retVal = ipfs_name_publish(client_node, path);
retVal = ipfs_name_publish(client_node, path, &response, &response_size);
} else if (strcmp(which, "resolve") == 0) {
retVal = ipfs_name_resolve(client_node, path);
retVal = ipfs_name_resolve(client_node, path, &response, &response_size);
} else {
libp2p_logger_error("name", "Nothing found on command line. Should be \"name resolve\" or \"name publish\".\n");
goto exit;
}
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
}
exit:
// shut everything down

View File

@ -35,8 +35,6 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSys
}
}*/
const int DefaultResolverCacheTTL = 60;
// ipfs_namesys_resolve implements Resolver.
int ipfs_namesys_resolve(char **path, char *name)
{