diff --git a/db/datastore.c b/db/datastore.c index 873ca04..10c5164 100644 --- a/db/datastore.c +++ b/db/datastore.c @@ -41,7 +41,7 @@ int libp2p_datastore_new(struct Datastore** datastore) { if (*datastore == NULL) return 0; (*datastore)->path = NULL; - (*datastore)->datastore_handle = NULL; + (*datastore)->datastore_context = NULL; (*datastore)->type = NULL; (*datastore)->storage_max = NULL; (*datastore)->gc_period = NULL; @@ -67,7 +67,7 @@ int libp2p_datastore_free(struct Datastore* datastore) { free(datastore->gc_period); if (datastore->params != NULL) free(datastore->params); - if (datastore->datastore_handle != NULL) + if (datastore->datastore_context != NULL) datastore->datastore_close(datastore); free(datastore); } diff --git a/include/libp2p/db/datastore.h b/include/libp2p/db/datastore.h index d9d13a8..6bc40a9 100644 --- a/include/libp2p/db/datastore.h +++ b/include/libp2p/db/datastore.h @@ -36,7 +36,7 @@ struct Datastore { int (*datastore_cursor_close)(struct Datastore* datastore); int (*datastore_cursor_get)(unsigned char** key, int* key_length, unsigned char** value, int* value_length, enum DatastoreCursorOp op, struct Datastore* datastore); // generic connection and status variables for the datastore - void* datastore_handle; // a handle to the database + void* datastore_context; // a handle to a context that holds connectivity information }; /*** diff --git a/include/libp2p/os/utils.h b/include/libp2p/os/utils.h index 9e2b0e0..d2ec91f 100644 --- a/include/libp2p/os/utils.h +++ b/include/libp2p/os/utils.h @@ -83,4 +83,13 @@ int os_utils_is_directory(const char* file_name); */ unsigned long long os_utils_gmtime(); +/** + * String search for platforms without it + * @haystack where to look + * @needle what you're looking for + * @len when to stop looking + * @returns a pointer to where the needle is found in the haystack or NULL if not found + */ +char *strnstr(const char *haystack, const char *needle, size_t len); + #endif /* utils_h */ diff --git a/os/utils.c b/os/utils.c index 8779fdc..5f6649e 100644 --- a/os/utils.c +++ b/os/utils.c @@ -191,3 +191,29 @@ unsigned long long os_utils_gmtime() { struct tm *gmt = gmtime(&local); return (unsigned long long)mktime(gmt); } + +/** + * String search for platforms without it + * @haystack where to look + * @needle what you're looking for + * @len when to stop looking + * @returns a pointer to where the needle is found in the haystack or NULL if not found + */ +char *strnstr(const char *haystack, const char *needle, size_t len) +{ + int i; + size_t needle_len; + + if (0 == (needle_len = strnlen(needle, len))) + return (char *)haystack; + + for (i=0; i<=(int)(len-needle_len); i++) + { + if ((haystack[0] == needle[0]) && + (0 == strncmp(haystack, needle, needle_len))) + return (char *)haystack; + + haystack++; + } + return NULL; +} diff --git a/routing/dht_protocol.c b/routing/dht_protocol.c index dbaea27..dd34903 100644 --- a/routing/dht_protocol.c +++ b/routing/dht_protocol.c @@ -3,6 +3,7 @@ #include "libp2p/crypto/encoding/base58.h" #include "libp2p/net/stream.h" +#include "libp2p/os/utils.h" #include "libp2p/routing/dht_protocol.h" #include "libp2p/record/message.h" #include "libp2p/utils/linked_list.h" @@ -22,7 +23,7 @@ struct DhtContext { int libp2p_routing_dht_can_handle(const uint8_t* incoming, size_t incoming_size) { if (incoming_size < 8) return 0; - char* result = strstr((char*)incoming, "/ipfs/kad"); + char* result = strnstr((char*)incoming, "/ipfs/kad", incoming_size); if (result != NULL && result == (char*)incoming) return 1; return 0; diff --git a/secio/secio.c b/secio/secio.c index 756081f..1570056 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -16,6 +16,7 @@ #include "libp2p/secio/exchange.h" #include "libp2p/net/multistream.h" #include "libp2p/net/p2pnet.h" +#include "libp2p/os/utils.h" #include "libp2p/crypto/ephemeral.h" #include "libp2p/crypto/sha1.h" #include "libp2p/crypto/sha256.h" @@ -39,10 +40,11 @@ struct SecioContext { }; int libp2p_secio_can_handle(const uint8_t* incoming, size_t incoming_size) { + const char* protocol = "/secio/1.0.0"; // sanity checks - if (incoming_size < 11) + if (incoming_size < 12) return 0; - char* result = strstr((char*)incoming, "/secio/1.0.0"); + char* result = strnstr((char*)incoming, protocol, incoming_size); if (result != NULL && result == (char*)incoming) return 1; return 0;