2017-02-03 01:15:28 +00:00
|
|
|
#include <stdlib.h>
|
2017-09-20 17:39:26 +00:00
|
|
|
#include <pthread.h>
|
2017-02-03 01:15:28 +00:00
|
|
|
#include <ipfs/routing/routing.h>
|
|
|
|
#include <ipfs/util/errs.h>
|
|
|
|
#include "libp2p/crypto/rsa.h"
|
2017-09-27 15:05:17 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-02-03 01:15:28 +00:00
|
|
|
#include "libp2p/record/record.h"
|
2017-09-27 15:05:17 +00:00
|
|
|
#include "ipfs/core/http_request.h"
|
2017-02-03 01:15:28 +00:00
|
|
|
#include "ipfs/datastore/ds_helper.h"
|
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
2017-02-23 20:15:33 +00:00
|
|
|
#include "ipfs/routing/routing.h"
|
2017-02-27 17:27:40 +00:00
|
|
|
#include "ipfs/importer/resolver.h"
|
2017-02-03 01:15:28 +00:00
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
int ipfs_routing_generic_put_value (ipfs_routing* offlineRouting, const unsigned char *key, size_t key_size, const void *val, size_t vlen)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *record, *nkey;
|
|
|
|
size_t len, nkey_len;
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
err = libp2p_record_make_put_record (&record, &len, offlineRouting->sk, (const char*)key, val, vlen, 0);
|
2017-02-03 01:15:28 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
nkey = malloc(key_size * 2); // FIXME: size of encoded key
|
|
|
|
if (!nkey) {
|
|
|
|
free (record);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
if (!ipfs_datastore_helper_ds_key_from_binary(key, key_size, (unsigned char*)nkey, key_size+1, &nkey_len)) {
|
2017-02-03 01:15:28 +00:00
|
|
|
free (nkey);
|
|
|
|
free (record);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Save to db as offline storage.
|
|
|
|
free (record);
|
|
|
|
return 0; // success.
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:02:00 +00:00
|
|
|
/***
|
|
|
|
* Get a value from the merkledag
|
|
|
|
* @param routing the context
|
|
|
|
* @param key the key
|
|
|
|
* @param key_size the size of the key
|
|
|
|
* @param val where to put the results
|
|
|
|
* @param vlen the size of the results
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-04-20 22:56:03 +00:00
|
|
|
int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *key, size_t key_size, void **val, size_t *vlen)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
2017-05-11 12:04:54 +00:00
|
|
|
struct HashtableNode* node = NULL;
|
|
|
|
*val = NULL;
|
2017-09-27 12:02:00 +00:00
|
|
|
int retVal = 0;
|
2017-05-11 12:04:54 +00:00
|
|
|
|
|
|
|
if (!ipfs_merkledag_get(key, key_size, &node, routing->local_node->repo)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:27:40 +00:00
|
|
|
// protobuf the node
|
2017-04-20 22:56:03 +00:00
|
|
|
int protobuf_size = ipfs_hashtable_node_protobuf_encode_size(node);
|
2017-02-27 17:27:40 +00:00
|
|
|
*val = malloc(protobuf_size);
|
|
|
|
|
2017-05-11 12:04:54 +00:00
|
|
|
if (ipfs_hashtable_node_protobuf_encode(node, *val, protobuf_size, vlen) == 0) {
|
2017-09-27 12:02:00 +00:00
|
|
|
goto exit;
|
2017-05-11 12:04:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = 1;
|
2017-05-11 12:04:54 +00:00
|
|
|
exit:
|
|
|
|
if (node != NULL)
|
|
|
|
ipfs_hashtable_node_free(node);
|
2017-09-27 12:02:00 +00:00
|
|
|
if (retVal != 1 && *val != NULL) {
|
2017-05-11 12:04:54 +00:00
|
|
|
free(*val);
|
|
|
|
*val = NULL;
|
|
|
|
}
|
|
|
|
return retVal;
|
2017-02-03 01:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
int ipfs_routing_offline_find_providers (ipfs_routing* offlineRouting, const unsigned char *key, size_t key_size, struct Libp2pVector** peers)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
|
|
|
return ErrOffline;
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
int ipfs_routing_offline_find_peer (ipfs_routing* offlineRouting, const unsigned char *peer_id, size_t pid_size, struct Libp2pPeer **result)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
|
|
|
return ErrOffline;
|
|
|
|
}
|
|
|
|
|
2017-09-27 15:05:17 +00:00
|
|
|
/**
|
|
|
|
* Attempt to publish that this node can provide a value
|
|
|
|
* @param offlineRouting the context
|
|
|
|
* @param incoming_hash the hash (in binary form)
|
|
|
|
* @param incoming_hash_size the length of the hash array
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned char *incoming_hash, size_t incoming_hash_size)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
2017-09-27 15:05:17 +00:00
|
|
|
if (offlineRouting->local_node->mode == MODE_API_AVAILABLE) {
|
|
|
|
//TODO: publish this through the api
|
|
|
|
unsigned char buffer[256];
|
|
|
|
if (!ipfs_cid_hash_to_base58(incoming_hash, incoming_hash_size, &buffer[0], 256)) {
|
|
|
|
libp2p_logger_error("offline", "Unable to convert hash to its Base58 representation.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* response;
|
|
|
|
struct HttpRequest* request = ipfs_core_http_request_new();
|
|
|
|
request->command = "dht";
|
|
|
|
request->sub_command = "provide";
|
|
|
|
request->arguments = libp2p_utils_vector_new(1);
|
|
|
|
libp2p_utils_vector_add(request->arguments, buffer);
|
|
|
|
if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response)) {
|
|
|
|
libp2p_logger_error("offline", "Unable to call API for dht publish.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fprintf(stdout, "%s", response);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-02-03 01:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:26:33 +00:00
|
|
|
int ipfs_routing_offline_ping (ipfs_routing* offlineRouting, struct Libp2pPeer* peer)
|
2017-02-03 01:15:28 +00:00
|
|
|
{
|
|
|
|
return ErrOffline;
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:55:36 +00:00
|
|
|
/**
|
|
|
|
* For offline, this does nothing
|
|
|
|
* @param offlineRouting the interface
|
|
|
|
* @returns 0
|
|
|
|
*/
|
2017-02-03 01:15:28 +00:00
|
|
|
int ipfs_routing_offline_bootstrap (ipfs_routing* offlineRouting)
|
|
|
|
{
|
2017-04-03 16:55:36 +00:00
|
|
|
return 0;
|
2017-02-03 01:15:28 +00:00
|
|
|
}
|
2017-02-23 20:15:33 +00:00
|
|
|
|
2017-04-03 16:55:36 +00:00
|
|
|
struct IpfsRouting* ipfs_routing_new_offline (struct IpfsNode* local_node, struct RsaPrivateKey *private_key)
|
2017-02-23 20:15:33 +00:00
|
|
|
{
|
2017-04-03 16:55:36 +00:00
|
|
|
struct IpfsRouting *offlineRouting = malloc (sizeof(struct IpfsRouting));
|
2017-02-23 20:15:33 +00:00
|
|
|
|
|
|
|
if (offlineRouting) {
|
2017-02-27 17:27:40 +00:00
|
|
|
offlineRouting->local_node = local_node;
|
2017-02-23 20:15:33 +00:00
|
|
|
offlineRouting->sk = private_key;
|
|
|
|
|
|
|
|
offlineRouting->PutValue = ipfs_routing_generic_put_value;
|
|
|
|
offlineRouting->GetValue = ipfs_routing_generic_get_value;
|
|
|
|
offlineRouting->FindProviders = ipfs_routing_offline_find_providers;
|
|
|
|
offlineRouting->FindPeer = ipfs_routing_offline_find_peer;
|
|
|
|
offlineRouting->Provide = ipfs_routing_offline_provide;
|
|
|
|
offlineRouting->Ping = ipfs_routing_offline_ping;
|
|
|
|
offlineRouting->Bootstrap = ipfs_routing_offline_bootstrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offlineRouting;
|
|
|
|
}
|
2017-09-07 16:05:56 +00:00
|
|
|
|
|
|
|
int ipfs_routing_offline_free(ipfs_routing* incoming) {
|
|
|
|
free(incoming);
|
|
|
|
return 1;
|
|
|
|
}
|