2017-09-21 12:51:18 +00:00
|
|
|
/**
|
|
|
|
* Handles the command line options for "ipfs name"
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "libp2p/utils/logger.h"
|
2017-09-21 23:03:42 +00:00
|
|
|
#include "libp2p/utils/vector.h"
|
2017-09-21 12:51:18 +00:00
|
|
|
#include "ipfs/core/ipfs_node.h"
|
|
|
|
#include "ipfs/cmd/cli.h"
|
2017-09-21 22:10:41 +00:00
|
|
|
#include "ipfs/core/http_request.h"
|
2017-09-21 12:51:18 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Publish IPNS name
|
|
|
|
*/
|
2018-10-25 22:14:47 +00:00
|
|
|
int ipfs_name_publish(struct IpfsNode* local_node, char* name, char **response, size_t *response_size) {
|
2017-09-21 22:10:41 +00:00
|
|
|
// call api.
|
|
|
|
struct HttpRequest* request = ipfs_core_http_request_new();
|
|
|
|
if (request == NULL)
|
|
|
|
return 0;
|
|
|
|
request->command = "name";
|
|
|
|
request->sub_command = "publish";
|
2017-09-21 23:03:42 +00:00
|
|
|
libp2p_utils_vector_add(request->arguments, name);
|
2018-10-25 22:14:47 +00:00
|
|
|
int retVal = ipfs_core_http_request_post(local_node, request, response, response_size, "", 0);
|
2017-09-21 22:10:41 +00:00
|
|
|
ipfs_core_http_request_free(request);
|
|
|
|
return retVal;
|
2017-09-21 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 22:14:47 +00:00
|
|
|
/***
|
|
|
|
* Resolve IPNS name
|
|
|
|
*/
|
|
|
|
int ipfs_name_resolve(struct IpfsNode* local_node, char* name, char **response, size_t *response_size) {
|
2017-09-21 12:51:18 +00:00
|
|
|
// ask api
|
2018-10-25 22:14:47 +00:00
|
|
|
const char prefix[] = "/ipns/";
|
|
|
|
char *ipns = NULL;
|
2017-09-21 23:03:42 +00:00
|
|
|
struct HttpRequest* request = ipfs_core_http_request_new();
|
|
|
|
if (request == NULL)
|
|
|
|
return 0;
|
|
|
|
request->command = "name";
|
|
|
|
request->sub_command = "resolve";
|
2018-10-25 22:14:47 +00:00
|
|
|
if (memcmp(name, prefix, sizeof(prefix)-1)!=0) {
|
|
|
|
ipns = malloc(sizeof(prefix) + strlen(name));
|
|
|
|
if (ipns) {
|
|
|
|
strcpy(ipns, prefix);
|
|
|
|
strcat(ipns, name);
|
|
|
|
name = ipns;
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 23:03:42 +00:00
|
|
|
libp2p_utils_vector_add(request->arguments, name);
|
2018-10-25 22:14:47 +00:00
|
|
|
int retVal = ipfs_core_http_request_post(local_node, request, response, response_size, "", 0);
|
|
|
|
if (ipns) {
|
|
|
|
free(ipns);
|
2017-09-25 17:07:16 +00:00
|
|
|
}
|
2017-09-21 23:03:42 +00:00
|
|
|
ipfs_core_http_request_free(request);
|
|
|
|
return retVal;
|
2017-09-21 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We received a cli command "ipfs name". Do the right thing.
|
|
|
|
* @param argc number of arguments on the command line
|
|
|
|
* @param argv actual command line arguments
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int ipfs_name(struct CliArguments* args) {
|
|
|
|
int retVal = 0;
|
|
|
|
struct IpfsNode* client_node = NULL;
|
|
|
|
|
|
|
|
if (args->argc < (args->verb_index + 2)) {
|
|
|
|
libp2p_logger_error("name", "Not enough command line arguments. Should be \"name resolve\" or \"name publish\".\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* which = args->argv[args->verb_index+1];
|
|
|
|
char* path = args->argv[args->verb_index+2];
|
|
|
|
|
|
|
|
// make sure API is running
|
|
|
|
if (!ipfs_node_offline_new(args->config_dir, &client_node)) {
|
|
|
|
libp2p_logger_error("name", "Unable to create offline node.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (client_node->mode != MODE_API_AVAILABLE) {
|
|
|
|
libp2p_logger_error("name", "API must be running.\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:14:47 +00:00
|
|
|
char *response = NULL;
|
|
|
|
size_t response_size;
|
2017-09-21 12:51:18 +00:00
|
|
|
// determine what we're doing
|
|
|
|
if (strcmp(which, "publish") == 0) {
|
2018-10-25 22:14:47 +00:00
|
|
|
retVal = ipfs_name_publish(client_node, path, &response, &response_size);
|
2017-09-21 12:51:18 +00:00
|
|
|
} else if (strcmp(which, "resolve") == 0) {
|
2018-10-25 22:14:47 +00:00
|
|
|
retVal = ipfs_name_resolve(client_node, path, &response, &response_size);
|
2017-09-21 12:51:18 +00:00
|
|
|
} else {
|
|
|
|
libp2p_logger_error("name", "Nothing found on command line. Should be \"name resolve\" or \"name publish\".\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-10-25 22:14:47 +00:00
|
|
|
if (response != NULL && response_size > 0) {
|
|
|
|
fwrite(response, 1, response_size, stdout);
|
|
|
|
free(response);
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
// shut everything down
|
2017-09-25 13:55:42 +00:00
|
|
|
ipfs_node_free(client_node);
|
2017-09-21 12:51:18 +00:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|