2017-08-09 13:03:40 +00:00
|
|
|
#include <stdlib.h>
|
2017-08-30 16:09:28 +00:00
|
|
|
#include <string.h>
|
2017-08-16 13:14:42 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-08-09 13:03:40 +00:00
|
|
|
#include "libp2p/net/protocol.h"
|
|
|
|
|
2017-10-23 20:21:50 +00:00
|
|
|
/*
|
|
|
|
* Handle the different protocols
|
|
|
|
*/
|
|
|
|
|
2017-08-09 13:03:40 +00:00
|
|
|
/***
|
|
|
|
* Compare incoming to see if they are requesting a protocol upgrade
|
|
|
|
* @param incoming the incoming string
|
|
|
|
* @param incoming_size the size of the incoming string
|
|
|
|
* @param test the protocol string to compare it with (i.e. "/secio" or "/nodeio"
|
|
|
|
* @returns true(1) if there was a match, false(0) otherwise
|
|
|
|
*/
|
2017-10-23 20:21:50 +00:00
|
|
|
const struct Libp2pProtocolHandler* protocol_compare(struct StreamMessage* msg, struct Libp2pVector* protocol_handlers) {
|
2017-11-20 00:29:40 +00:00
|
|
|
if (protocol_handlers != NULL) {
|
|
|
|
for(int i = 0; i < protocol_handlers->total; i++) {
|
|
|
|
const struct Libp2pProtocolHandler* handler = (const struct Libp2pProtocolHandler*) libp2p_utils_vector_get(protocol_handlers, i);
|
|
|
|
if (handler->CanHandle(msg)) {
|
|
|
|
return handler;
|
|
|
|
}
|
2017-08-09 13:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-31 21:40:35 +00:00
|
|
|
/**
|
|
|
|
* Allocate resources for a new Libp2pProtocolHandler
|
|
|
|
* @returns an allocated struct
|
|
|
|
*/
|
|
|
|
struct Libp2pProtocolHandler* libp2p_protocol_handler_new() {
|
|
|
|
struct Libp2pProtocolHandler* h = (struct Libp2pProtocolHandler*) malloc(sizeof(struct Libp2pProtocolHandler));
|
|
|
|
if (h != NULL) {
|
|
|
|
h->CanHandle = NULL;
|
|
|
|
h->HandleMessage = NULL;
|
|
|
|
h->Shutdown = NULL;
|
|
|
|
h->context = NULL;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:08:57 +00:00
|
|
|
/***
|
|
|
|
* Handle an incoming message
|
2017-11-06 22:31:30 +00:00
|
|
|
* @param message the incoming message
|
2017-11-19 18:37:03 +00:00
|
|
|
* @param stream the stream the message came in on
|
2017-08-09 17:08:57 +00:00
|
|
|
* @param handlers a Vector of protocol handlers
|
|
|
|
* @returns -1 on error, 0 if everything was okay, but the daemon should no longer handle this connection, 1 on success
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int libp2p_protocol_marshal(struct StreamMessage* msg, struct Stream* stream, struct Libp2pVector* handlers) {
|
2017-10-23 20:21:50 +00:00
|
|
|
const struct Libp2pProtocolHandler* handler = protocol_compare(msg, handlers);
|
2017-09-04 16:01:17 +00:00
|
|
|
|
2017-08-16 12:33:43 +00:00
|
|
|
if (handler == NULL) {
|
2017-11-20 00:29:40 +00:00
|
|
|
// set the msg->error code
|
|
|
|
msg->error_number = 100;
|
2017-08-16 12:33:43 +00:00
|
|
|
return -1;
|
2017-08-09 13:03:40 +00:00
|
|
|
}
|
2017-09-04 16:01:17 +00:00
|
|
|
|
2017-11-08 15:51:43 +00:00
|
|
|
return handler->HandleMessage(msg, stream, handler->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Shut down all protocol handlers and free vector
|
|
|
|
* @param handlers vector of Libp2pProtocolHandler
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int libp2p_protocol_handlers_shutdown(struct Libp2pVector* handlers) {
|
|
|
|
for(int i = 0; i < handlers->total; i++) {
|
|
|
|
struct Libp2pProtocolHandler* handler = (struct Libp2pProtocolHandler*)libp2p_utils_vector_get(handlers, i);
|
|
|
|
handler->Shutdown(handler->context);
|
|
|
|
}
|
|
|
|
libp2p_utils_vector_free(handlers);
|
|
|
|
return 1;
|
2017-08-09 13:03:40 +00:00
|
|
|
}
|