2017-03-19 13:04:54 +00:00
|
|
|
#include <stdlib.h>
|
2017-03-19 19:39:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-19 13:04:54 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
2017-03-19 19:39:48 +00:00
|
|
|
#include "libp2p/conn/session.h"
|
|
|
|
|
|
|
|
int libp2p_nodeio_upgrade_stream(struct SessionContext* context) {
|
|
|
|
int retVal = 0;
|
|
|
|
char* protocol = "/nodeio/1.0.0\n";
|
|
|
|
unsigned char* results = NULL;
|
|
|
|
size_t results_size = 0;
|
|
|
|
if (!context->default_stream->write(context, (unsigned char*)protocol, strlen(protocol)))
|
|
|
|
goto exit;
|
2017-04-17 19:03:27 +00:00
|
|
|
if (!context->default_stream->read(context, &results, &results_size, 5))
|
2017-03-19 19:39:48 +00:00
|
|
|
goto exit;
|
|
|
|
if (results_size != strlen(protocol))
|
|
|
|
goto exit;
|
|
|
|
if (strncmp((char*)results, protocol, results_size) != 0)
|
|
|
|
goto exit;
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (results != NULL) {
|
|
|
|
free(results);
|
|
|
|
results = NULL;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
2017-03-19 13:04:54 +00:00
|
|
|
|
2017-03-19 19:39:48 +00:00
|
|
|
/**
|
|
|
|
* Called by requestor to get a protobuf'd node from a hash
|
|
|
|
* @param context the session context
|
|
|
|
* @param hash the hash
|
|
|
|
* @param hash_size the length of the hash
|
|
|
|
* @param results where to put the buffer
|
|
|
|
* @param results_size the size of the results
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_nodeio_get(struct SessionContext* context, unsigned char* hash, int hash_length, unsigned char** results, size_t* results_size) {
|
|
|
|
if (!context->default_stream->write(context, hash, hash_length))
|
|
|
|
return 0;
|
2017-04-17 19:03:27 +00:00
|
|
|
if (!context->default_stream->read(context, results, results_size, 5))
|
2017-03-19 19:39:48 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
2017-03-19 13:04:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 19:39:48 +00:00
|
|
|
int libp2p_nodeio_handshake(struct SessionContext* context) {
|
|
|
|
char* protocol = "/nodeio/1.0.0\n";
|
|
|
|
return context->default_stream->write(context, (unsigned char*)protocol, strlen(protocol));
|
2017-03-19 13:04:54 +00:00
|
|
|
}
|