Can now compare 2 sessions for equality
This commit is contained in:
parent
c660a6eb32
commit
93bb78169a
11 changed files with 117 additions and 15 deletions
|
@ -2,7 +2,7 @@ CC = gcc
|
||||||
CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3
|
CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3
|
||||||
LFLAGS =
|
LFLAGS =
|
||||||
DEPS =
|
DEPS =
|
||||||
OBJS = dialer.o transport_dialer.o connection.o tcp_transport_dialer.o
|
OBJS = dialer.o transport_dialer.o connection.o tcp_transport_dialer.o session.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
94
conn/session.c
Normal file
94
conn/session.c
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "multiaddr/multiaddr.h"
|
||||||
|
#include "libp2p/conn/session.h"
|
||||||
|
#include "libp2p/net/stream.h"
|
||||||
|
|
||||||
|
struct SessionContext* libp2p_session_context_new() {
|
||||||
|
struct SessionContext* context = (struct SessionContext*) malloc(sizeof(struct SessionContext));
|
||||||
|
if (context != NULL) {
|
||||||
|
context->aes_decode_nonce_offset = 0;
|
||||||
|
memset(&context->aes_decode_stream_block[0], 0, 16);
|
||||||
|
context->aes_encode_nonce_offset = 0;
|
||||||
|
memset(&context->aes_encode_stream_block[0], 0, 16);
|
||||||
|
context->chosen_cipher = NULL;
|
||||||
|
context->chosen_curve = NULL;
|
||||||
|
context->chosen_hash = NULL;
|
||||||
|
context->datastore = NULL;
|
||||||
|
context->default_stream = NULL;
|
||||||
|
context->ephemeral_private_key = NULL;
|
||||||
|
context->filestore = NULL;
|
||||||
|
context->host = NULL;
|
||||||
|
context->insecure_stream = NULL;
|
||||||
|
memset(&context->local_nonce[0], 0, 16);
|
||||||
|
context->local_stretched_key = NULL;
|
||||||
|
context->mac_function = NULL;
|
||||||
|
context->port = 0;
|
||||||
|
context->remote_ephemeral_public_key = NULL;
|
||||||
|
context->remote_ephemeral_public_key_size = 0;
|
||||||
|
context->remote_key.data_size = 0;
|
||||||
|
context->remote_key.data = NULL;
|
||||||
|
context->remote_key.type = KEYTYPE_INVALID;
|
||||||
|
memset(&context->remote_nonce[0], 0, 16);
|
||||||
|
context->remote_peer_id = NULL;
|
||||||
|
context->remote_stretched_key = NULL;
|
||||||
|
context->secure_stream = NULL;
|
||||||
|
context->shared_key = NULL;
|
||||||
|
context->shared_key_size = 0;
|
||||||
|
context->traffic_type = TCP;
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
int libp2p_session_context_free(struct SessionContext* context) {
|
||||||
|
if (context != NULL) {
|
||||||
|
//TODO: Be more intelligent
|
||||||
|
free(context);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int libp2p_session_context_compare_streams(const struct Stream* a, const struct Stream* b) {
|
||||||
|
if (a == NULL && b == NULL)
|
||||||
|
return 0;
|
||||||
|
if (a == NULL && b != NULL)
|
||||||
|
return -1;
|
||||||
|
if (a != NULL && b == NULL)
|
||||||
|
return 1;
|
||||||
|
return multiaddress_compare(a->address, b->address);
|
||||||
|
}
|
||||||
|
|
||||||
|
int libp2p_session_context_compare_remote_key(const struct PublicKey* a, const struct PublicKey* b) {
|
||||||
|
if (a == NULL && b == NULL)
|
||||||
|
return 0;
|
||||||
|
if (a == NULL && b != NULL)
|
||||||
|
return -1;
|
||||||
|
if (a != NULL && b == NULL)
|
||||||
|
return 1;
|
||||||
|
int total = b->data_size - a->data_size;
|
||||||
|
if (total != 0)
|
||||||
|
return total;
|
||||||
|
for(size_t i = 0; i < b->data_size; i++) {
|
||||||
|
total = b->data[i] - a->data[i];
|
||||||
|
if (total != 0)
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int libp2p_session_context_compare(const struct SessionContext* a, const struct SessionContext* b) {
|
||||||
|
int total = 0;
|
||||||
|
if (a == NULL && b == NULL)
|
||||||
|
return 0;
|
||||||
|
if (a == NULL && b != NULL)
|
||||||
|
return -1;
|
||||||
|
if (a != NULL && b == NULL)
|
||||||
|
return 1;
|
||||||
|
// streams
|
||||||
|
total = libp2p_session_context_compare_streams(a->default_stream, b->default_stream);
|
||||||
|
if (total != 0)
|
||||||
|
return total;
|
||||||
|
// remote key
|
||||||
|
total = libp2p_session_context_compare_remote_key(&a->remote_key, &b->remote_key);
|
||||||
|
return total;
|
||||||
|
}
|
|
@ -62,3 +62,11 @@ struct SessionContext {
|
||||||
unsigned char* remote_ephemeral_public_key;
|
unsigned char* remote_ephemeral_public_key;
|
||||||
size_t remote_ephemeral_public_key_size;
|
size_t remote_ephemeral_public_key_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Compare 2 SessionContext structs for equality
|
||||||
|
* @param a side A
|
||||||
|
* @param b side B
|
||||||
|
* @returns 0 if equal, <0 if A wins, >0 if B wins
|
||||||
|
*/
|
||||||
|
int libp2p_session_context_compare(const struct SessionContext* a, const struct SessionContext* b);
|
||||||
|
|
|
@ -54,7 +54,7 @@ int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout);
|
||||||
* @param in what is to be copied
|
* @param in what is to be copied
|
||||||
* @returns a new struct, that does not rely on the old
|
* @returns a new struct, that does not rely on the old
|
||||||
*/
|
*/
|
||||||
struct Libp2pPeer* libp2p_peer_copy(struct Libp2pPeer* in);
|
struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in);
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Determine if the passed in peer and id match
|
* Determine if the passed in peer and id match
|
||||||
|
|
|
@ -59,7 +59,7 @@ int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntr
|
||||||
* @param peer the peer to add (will be wrapped in PeerEntry struct)
|
* @param peer the peer to add (will be wrapped in PeerEntry struct)
|
||||||
* @returns true(1) on success, otherwise false
|
* @returns true(1) on success, otherwise false
|
||||||
*/
|
*/
|
||||||
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* peer);
|
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* peer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a peer from the peerstore based on the peer id
|
* Retrieve a peer from the peerstore based on the peer id
|
||||||
|
@ -85,5 +85,5 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const
|
||||||
* @param peerstore the peerstore to search
|
* @param peerstore the peerstore to search
|
||||||
* @param in the peer to search for
|
* @param in the peer to search for
|
||||||
*/
|
*/
|
||||||
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* in);
|
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* in);
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct Libp2pVector {
|
struct Libp2pVector {
|
||||||
void **items;
|
void const** items;
|
||||||
int capacity;
|
int capacity;
|
||||||
int total;
|
int total;
|
||||||
};
|
};
|
||||||
|
@ -31,8 +31,8 @@ int libp2p_utils_vector_total(struct Libp2pVector* in);
|
||||||
* @param vector the vector to add the item to.
|
* @param vector the vector to add the item to.
|
||||||
* @param value the value to be added NOTE: this only saves the pointer, it does not copy.
|
* @param value the value to be added NOTE: this only saves the pointer, it does not copy.
|
||||||
*/
|
*/
|
||||||
void libp2p_utils_vector_add(struct Libp2pVector *vector, void * value);
|
void libp2p_utils_vector_add(struct Libp2pVector *vector, const void * value);
|
||||||
void libp2p_utils_vector_set(struct Libp2pVector *vector, int pos, void *value);
|
void libp2p_utils_vector_set(struct Libp2pVector *vector, int pos, void *value);
|
||||||
void *libp2p_utils_vector_get(struct Libp2pVector *vector, int);
|
const void *libp2p_utils_vector_get(struct Libp2pVector *vector, int);
|
||||||
void libp2p_utils_vector_delete(struct Libp2pVector *vector, int pos);
|
void libp2p_utils_vector_delete(struct Libp2pVector *vector, int pos);
|
||||||
void libp2p_utils_vector_free(struct Libp2pVector *vector);
|
void libp2p_utils_vector_free(struct Libp2pVector *vector);
|
||||||
|
|
|
@ -133,7 +133,7 @@ void libp2p_peer_free(struct Libp2pPeer* in) {
|
||||||
* @param in what is to be copied
|
* @param in what is to be copied
|
||||||
* @returns a new struct, that does not rely on the old
|
* @returns a new struct, that does not rely on the old
|
||||||
*/
|
*/
|
||||||
struct Libp2pPeer* libp2p_peer_copy(struct Libp2pPeer* in) {
|
struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in) {
|
||||||
struct Libp2pPeer* out = libp2p_peer_new();
|
struct Libp2pPeer* out = libp2p_peer_new();
|
||||||
if (out != NULL) {
|
if (out != NULL) {
|
||||||
out->id_size = in->id_size;
|
out->id_size = in->id_size;
|
||||||
|
|
|
@ -105,7 +105,7 @@ int libp2p_peerstore_add_peer_entry(struct Peerstore* peerstore, struct PeerEntr
|
||||||
* @param peer the peer to add (will be wrapped in PeerEntry struct)
|
* @param peer the peer to add (will be wrapped in PeerEntry struct)
|
||||||
* @returns true(1) on success, otherwise false
|
* @returns true(1) on success, otherwise false
|
||||||
*/
|
*/
|
||||||
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* peer) {
|
int libp2p_peerstore_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* peer) {
|
||||||
int retVal = 0;
|
int retVal = 0;
|
||||||
|
|
||||||
char* ma_string = "";
|
char* ma_string = "";
|
||||||
|
@ -184,7 +184,7 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const
|
||||||
* @param peerstore the peerstore to search
|
* @param peerstore the peerstore to search
|
||||||
* @param in the peer to search for
|
* @param in the peer to search for
|
||||||
*/
|
*/
|
||||||
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, struct Libp2pPeer* in) {
|
struct Libp2pPeer* libp2p_peerstore_get_or_add_peer(struct Peerstore* peerstore, const struct Libp2pPeer* in) {
|
||||||
struct Libp2pPeer* out = libp2p_peerstore_get_peer(peerstore, (unsigned char*)in->id, in->id_size);
|
struct Libp2pPeer* out = libp2p_peerstore_get_peer(peerstore, (unsigned char*)in->id, in->id_size);
|
||||||
if (out != NULL)
|
if (out != NULL)
|
||||||
return out;
|
return out;
|
||||||
|
|
|
@ -52,7 +52,7 @@ void libp2p_providerstore_entry_free(struct ProviderEntry* in) {
|
||||||
void libp2p_providerstore_free(struct ProviderStore* in) {
|
void libp2p_providerstore_free(struct ProviderStore* in) {
|
||||||
if (in != NULL) {
|
if (in != NULL) {
|
||||||
for(int i = 0; i < in->provider_entries->total; i++) {
|
for(int i = 0; i < in->provider_entries->total; i++) {
|
||||||
struct ProviderEntry* entry = libp2p_utils_vector_get(in->provider_entries, i);
|
struct ProviderEntry* entry = (struct ProviderEntry*) libp2p_utils_vector_get(in->provider_entries, i);
|
||||||
libp2p_providerstore_entry_free(entry);
|
libp2p_providerstore_entry_free(entry);
|
||||||
}
|
}
|
||||||
libp2p_utils_vector_free(in->provider_entries);
|
libp2p_utils_vector_free(in->provider_entries);
|
||||||
|
|
|
@ -31,7 +31,7 @@ int libp2p_logger_initialized() {
|
||||||
int libp2p_logger_free() {
|
int libp2p_logger_free() {
|
||||||
if (logger_classes != NULL) {
|
if (logger_classes != NULL) {
|
||||||
for(int i = 0; i < logger_classes->total; i++) {
|
for(int i = 0; i < logger_classes->total; i++) {
|
||||||
free(libp2p_utils_vector_get(logger_classes, i));
|
free((char*)libp2p_utils_vector_get(logger_classes, i));
|
||||||
}
|
}
|
||||||
libp2p_utils_vector_free(logger_classes);
|
libp2p_utils_vector_free(logger_classes);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ static void libp2p_utils_vector_resize(struct Libp2pVector *v, int capacity)
|
||||||
printf("vector_resize: %d to %d\n", v->capacity, capacity);
|
printf("vector_resize: %d to %d\n", v->capacity, capacity);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void **items = realloc(v->items, sizeof(void *) * capacity);
|
void const** items = realloc(v->items, sizeof(void *) * capacity);
|
||||||
if (items) {
|
if (items) {
|
||||||
v->items = items;
|
v->items = items;
|
||||||
v->capacity = capacity;
|
v->capacity = capacity;
|
||||||
|
@ -35,7 +35,7 @@ static void libp2p_utils_vector_resize(struct Libp2pVector *v, int capacity)
|
||||||
* @param v the vector to add to
|
* @param v the vector to add to
|
||||||
* @param item the item to add
|
* @param item the item to add
|
||||||
*/
|
*/
|
||||||
void libp2p_utils_vector_add(struct Libp2pVector *v, void *item)
|
void libp2p_utils_vector_add(struct Libp2pVector *v, const void *item)
|
||||||
{
|
{
|
||||||
if (v->capacity == v->total)
|
if (v->capacity == v->total)
|
||||||
libp2p_utils_vector_resize(v, v->capacity * 2);
|
libp2p_utils_vector_resize(v, v->capacity * 2);
|
||||||
|
@ -48,7 +48,7 @@ void libp2p_utils_vector_set(struct Libp2pVector *v, int index, void *item)
|
||||||
v->items[index] = item;
|
v->items[index] = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *libp2p_utils_vector_get(struct Libp2pVector *v, int index)
|
const void *libp2p_utils_vector_get(struct Libp2pVector *v, int index)
|
||||||
{
|
{
|
||||||
if (index >= 0 && index < v->total)
|
if (index >= 0 && index < v->total)
|
||||||
return v->items[index];
|
return v->items[index];
|
||||||
|
|
Loading…
Reference in a new issue