diff --git a/identify/identify.c b/identify/identify.c index 3eafbd8..92f255c 100644 --- a/identify/identify.c +++ b/identify/identify.c @@ -73,6 +73,54 @@ int libp2p_identify_receive_protocol(struct Stream* stream) { return 1; } +/** + * Create a Identify struct + * @returns the newly allocated record struct + */ +Identify* libp2p_identify_new() { + Identify* out = (Identify*)malloc(sizeof(Identify)); + if (out != NULL) { + out->PublicKey = NULL; + out->ListenAddrs = NULL; + out->Protocols = NULL; + out->ObservedAddr = NULL; + out->ProtocolVersion = IDENTIFY_PROTOCOL_VERSION; + out->AgentVersion = IDENTIFY_AGENT_VERSION; + out->XXX_unrecognized = NULL; + } + return out; +} + +/** + * Free the resources from a identify struct + * @param in the struct to free + */ +void libp2p_identify_free(Identify* in) { + int i; + + if (in != NULL) { + if (in->PublicKey != NULL) + free(in->PublicKey); + if (in->ListenAddrs != NULL) { + // free every item + for (i = 0 ; in->ListenAddrs[i] ; i++) + free(in->ListenAddrs[i]); + // free array + free(in->ListenAddrs); + } + if (in->Protocols != NULL) { + for (i = 0 ; in->Protocols[i] ; i++) + free(in->Protocols[i]); + free(in->Protocols); + } + if (in->ObservedAddr != NULL) + free(in->ObservedAddr); + if (in->XXX_unrecognized != NULL) + free(in->XXX_unrecognized); + free(in); + } +} + /** * A remote node is attempting to send us an Identify message * @param msg the message sent diff --git a/include/libp2p/identify/identify.h b/include/libp2p/identify/identify.h index 2502c45..29c2486 100644 --- a/include/libp2p/identify/identify.h +++ b/include/libp2p/identify/identify.h @@ -2,6 +2,9 @@ #include "libp2p/utils/vector.h" +#define IDENTIFY_PROTOCOL_VERSION "ipfs/0.1.0" +#define IDENTIFY_AGENT_VERSION "c-ipfs/1.0" + typedef struct { // publicKey is this node's public key (which also gives its node.ID) // - may not need to be sent, as secure channel implies it has been sent. @@ -31,6 +34,8 @@ struct IdentifyContext { int libp2p_identify_can_handle(const struct StreamMessage* msg); int libp2p_identify_send_protocol(struct Stream* stream); int libp2p_identify_receive_protocol(struct Stream* stream); +Identify* libp2p_identify_new(); +void libp2p_identify_free(Identify* in); int libp2p_identify_handle_message(const struct StreamMessage* msg, struct Stream* stream, void* protocol_context); int libp2p_identify_shutdown(void* protocol_context); struct Libp2pProtocolHandler* libp2p_identify_build_protocol_handler(struct Libp2pVector* handlers);