diff --git a/conn/Makefile b/conn/Makefile index d68bc26..1e7d7aa 100644 --- a/conn/Makefile +++ b/conn/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3 LFLAGS = 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) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/conn/session.c b/conn/session.c new file mode 100644 index 0000000..5d312c0 --- /dev/null +++ b/conn/session.c @@ -0,0 +1,94 @@ +#include +#include +#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; +} diff --git a/include/libp2p/conn/session.h b/include/libp2p/conn/session.h index 54614bb..8ddd16a 100644 --- a/include/libp2p/conn/session.h +++ b/include/libp2p/conn/session.h @@ -62,3 +62,11 @@ struct SessionContext { unsigned char* remote_ephemeral_public_key; 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); diff --git a/include/libp2p/peer/peer.h b/include/libp2p/peer/peer.h index 9c82461..173b58c 100644 --- a/include/libp2p/peer/peer.h +++ b/include/libp2p/peer/peer.h @@ -54,7 +54,7 @@ int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout); * @param in what is to be copied * @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 diff --git a/include/libp2p/peer/peerstore.h b/include/libp2p/peer/peerstore.h index 47863ea..fa3e114 100644 --- a/include/libp2p/peer/peerstore.h +++ b/include/libp2p/peer/peerstore.h @@ -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) * @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 @@ -85,5 +85,5 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const * @param peerstore the peerstore to search * @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); diff --git a/include/libp2p/utils/vector.h b/include/libp2p/utils/vector.h index 0c3788f..d791732 100644 --- a/include/libp2p/utils/vector.h +++ b/include/libp2p/utils/vector.h @@ -18,7 +18,7 @@ */ struct Libp2pVector { - void **items; + void const** items; int capacity; int total; }; @@ -31,8 +31,8 @@ int libp2p_utils_vector_total(struct Libp2pVector* in); * @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. */ -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_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_free(struct Libp2pVector *vector); diff --git a/peer/peer.c b/peer/peer.c index d8c1bc0..c536916 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -133,7 +133,7 @@ void libp2p_peer_free(struct Libp2pPeer* in) { * @param in what is to be copied * @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(); if (out != NULL) { out->id_size = in->id_size; diff --git a/peer/peerstore.c b/peer/peerstore.c index 4ba38c7..d237240 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -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) * @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; char* ma_string = ""; @@ -184,7 +184,7 @@ struct Libp2pPeer* libp2p_peerstore_get_peer(struct Peerstore* peerstore, const * @param peerstore the peerstore to search * @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); if (out != NULL) return out; diff --git a/peer/providerstore.c b/peer/providerstore.c index fabbc19..809f098 100644 --- a/peer/providerstore.c +++ b/peer/providerstore.c @@ -52,7 +52,7 @@ void libp2p_providerstore_entry_free(struct ProviderEntry* in) { void libp2p_providerstore_free(struct ProviderStore* in) { if (in != NULL) { 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_utils_vector_free(in->provider_entries); diff --git a/utils/logger.c b/utils/logger.c index 3402ebc..ea8730a 100644 --- a/utils/logger.c +++ b/utils/logger.c @@ -31,7 +31,7 @@ int libp2p_logger_initialized() { int libp2p_logger_free() { if (logger_classes != NULL) { 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); } diff --git a/utils/vector.c b/utils/vector.c index 4097237..fad625d 100644 --- a/utils/vector.c +++ b/utils/vector.c @@ -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); #endif - void **items = realloc(v->items, sizeof(void *) * capacity); + void const** items = realloc(v->items, sizeof(void *) * capacity); if (items) { v->items = items; 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 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) 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; } -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) return v->items[index];