2017-03-19 19:39:48 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libp2p/utils/vector.h"
|
2017-04-04 01:54:41 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-03-19 19:39:48 +00:00
|
|
|
|
|
|
|
struct ProviderEntry {
|
|
|
|
unsigned char* hash;
|
|
|
|
int hash_size;
|
|
|
|
unsigned char* peer_id;
|
|
|
|
int peer_id_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProviderStore {
|
|
|
|
struct Libp2pVector* provider_entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Stores hashes, and peers where you can possibly get them
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new ProviderStore
|
|
|
|
* @returns a ProviderStore struct
|
|
|
|
*/
|
|
|
|
struct ProviderStore* libp2p_providerstore_new() {
|
|
|
|
struct ProviderStore* out = (struct ProviderStore*)malloc(sizeof(struct ProviderStore));
|
|
|
|
if (out != NULL) {
|
|
|
|
out->provider_entries = libp2p_utils_vector_new(4);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:03:27 +00:00
|
|
|
void libp2p_providerstore_entry_free(struct ProviderEntry* in) {
|
|
|
|
if (in != NULL) {
|
|
|
|
if (in->hash != NULL) {
|
|
|
|
free(in->hash);
|
|
|
|
in->hash_size = 0;
|
|
|
|
}
|
|
|
|
if (in->peer_id != NULL) {
|
|
|
|
free(in->peer_id);
|
|
|
|
in->peer_id_size = 0;
|
|
|
|
}
|
|
|
|
free(in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 19:39:48 +00:00
|
|
|
/***
|
|
|
|
* Clean resources used by a ProviderStore
|
|
|
|
* @param in the ProviderStore to clean up
|
|
|
|
*/
|
|
|
|
void libp2p_providerstore_free(struct ProviderStore* in) {
|
|
|
|
if (in != NULL) {
|
2017-04-17 19:03:27 +00:00
|
|
|
for(int i = 0; i < in->provider_entries->total; i++) {
|
2017-07-26 12:37:28 +00:00
|
|
|
struct ProviderEntry* entry = (struct ProviderEntry*) libp2p_utils_vector_get(in->provider_entries, i);
|
2017-04-17 19:03:27 +00:00
|
|
|
libp2p_providerstore_entry_free(entry);
|
|
|
|
}
|
2017-03-19 19:39:48 +00:00
|
|
|
libp2p_utils_vector_free(in->provider_entries);
|
|
|
|
free(in);
|
|
|
|
in = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 12:04:28 +00:00
|
|
|
int libp2p_providerstore_add(struct ProviderStore* store, unsigned char* hash, int hash_size, const unsigned char* peer_id, int peer_id_size) {
|
2017-04-04 01:54:41 +00:00
|
|
|
char hash_str[hash_size + 1];
|
|
|
|
memcpy(hash_str, hash, hash_size);
|
|
|
|
hash_str[hash_size] = 0;
|
|
|
|
char peer_str[peer_id_size + 1];
|
|
|
|
memcpy(peer_str, peer_id, peer_id_size);
|
|
|
|
peer_str[peer_id_size] = 0;
|
2017-04-06 14:32:54 +00:00
|
|
|
libp2p_logger_debug("providerstore", "Adding hash %s to providerstore. It can be retrieved from %s\n", hash_str, peer_str);
|
2017-03-19 19:39:48 +00:00
|
|
|
struct ProviderEntry* entry = (struct ProviderEntry*)malloc(sizeof(struct ProviderEntry));
|
|
|
|
entry->hash = malloc(hash_size);
|
|
|
|
memcpy(entry->hash, hash, hash_size);
|
|
|
|
entry->hash_size = hash_size;
|
|
|
|
entry->peer_id = malloc(peer_id_size);
|
|
|
|
memcpy(entry->peer_id, peer_id, peer_id_size);
|
|
|
|
entry->peer_id_size = peer_id_size;
|
|
|
|
libp2p_utils_vector_add(store->provider_entries, entry);
|
2017-04-03 23:13:42 +00:00
|
|
|
return 1;
|
2017-03-19 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:55:18 +00:00
|
|
|
int libp2p_providerstore_get(struct ProviderStore* store, const unsigned char* hash, int hash_size, unsigned char** peer_id, int *peer_id_size) {
|
2017-03-19 19:39:48 +00:00
|
|
|
struct ProviderEntry* current = NULL;
|
|
|
|
for (int i = 0; i < store->provider_entries->total; i++) {
|
|
|
|
current = (struct ProviderEntry*)libp2p_utils_vector_get(store->provider_entries, i);
|
|
|
|
if (current->hash_size == hash_size && memcmp(current->hash, hash, hash_size) == 0) {
|
|
|
|
*peer_id = malloc(current->peer_id_size);
|
|
|
|
memcpy(*peer_id, current->peer_id, current->peer_id_size);
|
|
|
|
*peer_id_size = current->peer_id_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|