From 15352732596e6098b2c9d7adf6966d61efd677b5 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 2 Feb 2017 22:15:28 -0300 Subject: [PATCH] Initial implementation of routing/offline. --- Makefile | 2 + include/ipfs/routing/routing.h | 32 +++++++++++++ include/ipfs/util/errs.h | 3 +- routing/Makefile | 18 +++++++ routing/offline.c | 87 ++++++++++++++++++++++++++++++++++ util/errs.c | 3 +- 6 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 include/ipfs/routing/routing.h create mode 100644 routing/Makefile create mode 100644 routing/offline.c diff --git a/Makefile b/Makefile index 1d1b767..9429e18 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ all: cd thirdparty; make all; cd unixfs; make all; cd main; make all; + cd routing; make all; cd test; make all; clean: @@ -38,6 +39,7 @@ clean: cd thirdparty; make clean; cd unixfs; make clean; cd main; make clean; + cd routing; make clean; cd test; make clean; rebuild: clean all diff --git a/include/ipfs/routing/routing.h b/include/ipfs/routing/routing.h new file mode 100644 index 0000000..9373f55 --- /dev/null +++ b/include/ipfs/routing/routing.h @@ -0,0 +1,32 @@ +#ifndef ROUTING_H + #define ROUTING_H + + #include "libp2p/crypto/rsa.h" + + // offlineRouting implements the IpfsRouting interface, + // but only provides the capability to Put and Get signed dht + // records to and from the local datastore. + struct s_ipfs_routing { + struct FSRepo* datastore; + size_t ds_len; + struct RsaPrivateKey* sk; + int (*PutValue) (struct s_ipfs_routing*, char*, size_t, void*, size_t); + int (*GetValue) (struct s_ipfs_routing*, char*, size_t, void*, size_t*); + int (*FindProviders) (struct s_ipfs_routing*, char*, size_t, void*, size_t*); + int (*FindPeer) (struct s_ipfs_routing*, char*, size_t, void*, size_t*); + int (*Provide) (struct s_ipfs_routing*, char*); + int (*Ping) (struct s_ipfs_routing*, char*, size_t); + int (*Bootstrap) (struct s_ipfs_routing*); + }; + typedef struct s_ipfs_routing ipfs_routing; + + // offline routing routines. + ipfs_routing* ipfs_routing_new_offline (struct FSRepo* ds, struct RsaPrivateKey *private_key); + int ipfs_routing_offline_put_value (ipfs_routing* offlineRouting, char *key, size_t key_size, void *val, size_t vlen); + int ipfs_routing_offline_get_value (ipfs_routing* offlineRouting, char *key, size_t key_size, void *val, size_t *vlen); + int ipfs_routing_offline_find_providers (ipfs_routing* offlineRouting, char *key, size_t key_size, void *ret, size_t *rlen); + int ipfs_routing_offline_find_peer (ipfs_routing* offlineRouting, char *peer_id, size_t pid_size, void *ret, size_t *rlen); + int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, char *cid); + int ipfs_routing_offline_ping (ipfs_routing* offlineRouting, char *peer_id, size_t pid_size); + int ipfs_routing_offline_bootstrap (ipfs_routing* offlineRouting); +#endif // ROUTING_H diff --git a/include/ipfs/util/errs.h b/include/ipfs/util/errs.h index 5c15308..cfe99d4 100644 --- a/include/ipfs/util/errs.h +++ b/include/ipfs/util/errs.h @@ -27,6 +27,7 @@ ErrInvalidSignature, ErrInvalidSignatureFmt, ErrNoRecord, - ErrCidDecodeFailed + ErrCidDecodeFailed, + ErrOffline } ErrsIdx; #endif // IPFS_ERRS_H diff --git a/routing/Makefile b/routing/Makefile new file mode 100644 index 0000000..8ae3fdb --- /dev/null +++ b/routing/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-protobuf -Wall + +ifdef DEBUG +CFLAGS += -g3 +endif + +LFLAGS = +DEPS = +OBJS = offline.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +all: $(OBJS) + +clean: + rm -f $(OBJS) diff --git a/routing/offline.c b/routing/offline.c new file mode 100644 index 0000000..3f867c8 --- /dev/null +++ b/routing/offline.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include "libp2p/crypto/rsa.h" +#include "libp2p/record/record.h" +#include "ipfs/datastore/ds_helper.h" +#include "ipfs/merkledag/merkledag.h" + +ipfs_routing* ipfs_routing_new_offline (struct FSRepo* ds, struct RsaPrivateKey *private_key) +{ + ipfs_routing *offlineRouting = malloc (sizeof(ipfs_routing)); + + if (offlineRouting) { + offlineRouting->datastore = ds; + offlineRouting->sk = private_key; + + offlineRouting->PutValue = ipfs_routing_offline_put_value; + offlineRouting->GetValue = ipfs_routing_offline_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; +} + +int ipfs_routing_offline_put_value (ipfs_routing* offlineRouting, char *key, size_t key_size, void *val, size_t vlen) +{ + int err; + char *record, *nkey; + size_t len, nkey_len; + + err = libp2p_record_make_put_record (&record, &len, offlineRouting->sk, key, val, vlen, 0); + + if (err) { + return err; + } + + nkey = malloc(key_size * 2); // FIXME: size of encoded key + if (!nkey) { + free (record); + return -1; + } + + if (!ipfs_datastore_helper_ds_key_from_binary((unsigned char*)key, key_size, (unsigned char*)nkey, key_size+1, &nkey_len)) { + free (nkey); + free (record); + return -1; + } + + // TODO: Save to db as offline storage. + free (record); + return 0; // success. +} + +int ipfs_routing_offline_get_value (ipfs_routing* offlineRouting, char *key, size_t key_size, void *val, size_t *vlen) +{ + // TODO: Read from db, validate and decode before return. + return -1; +} + +int ipfs_routing_offline_find_providers (ipfs_routing* offlineRouting, char *key, size_t key_size, void *ret, size_t *rlen) +{ + return ErrOffline; +} + +int ipfs_routing_offline_find_peer (ipfs_routing* offlineRouting, char *peer_id, size_t pid_size, void *ret, size_t *rlen) +{ + return ErrOffline; +} + +int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, char *cid) +{ + return ErrOffline; +} + +int ipfs_routing_offline_ping (ipfs_routing* offlineRouting, char *peer_id, size_t pid_size) +{ + return ErrOffline; +} + +int ipfs_routing_offline_bootstrap (ipfs_routing* offlineRouting) +{ + return ErrOffline; +} diff --git a/util/errs.c b/util/errs.c index ad4884c..e30032b 100644 --- a/util/errs.c +++ b/util/errs.c @@ -29,5 +29,6 @@ char *Err[] = { NULL, "Invalid value. Not signed by PrivateKey corresponding to %s", "no usable records in given set", - "failed to decode empty key constant" + "failed to decode empty key constant", + "routing system in offline mode" };