2017-02-02 19:10:12 +00:00
|
|
|
#include <stdlib.h>
|
2017-02-23 20:16:04 +00:00
|
|
|
#include <unistd.h>
|
2017-02-09 08:34:12 +00:00
|
|
|
#include <stdio.h>
|
2017-02-02 19:10:12 +00:00
|
|
|
#include <string.h>
|
2017-02-09 08:34:12 +00:00
|
|
|
#include <errno.h>
|
2017-02-23 16:15:48 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2017-02-02 19:10:12 +00:00
|
|
|
#include "libp2p/net/p2pnet.h"
|
2017-02-23 16:15:48 +00:00
|
|
|
#include "libp2p/record/message.h"
|
2017-03-09 15:00:45 +00:00
|
|
|
#include "libp2p/secio/secio.h"
|
2017-02-02 19:10:12 +00:00
|
|
|
#include "varint.h"
|
2017-02-23 20:16:04 +00:00
|
|
|
#include "libp2p/net/multistream.h"
|
2017-04-04 01:54:41 +00:00
|
|
|
#include "multiaddr/multiaddr.h"
|
2017-02-02 19:10:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* An implementation of the libp2p multistream
|
|
|
|
*/
|
|
|
|
|
2017-03-09 15:00:45 +00:00
|
|
|
int libp2p_net_multistream_close(void* stream_context) {
|
2017-03-19 12:42:52 +00:00
|
|
|
struct SessionContext* secure_context = (struct SessionContext*)stream_context;
|
2017-03-09 15:00:45 +00:00
|
|
|
struct Stream* stream = secure_context->insecure_stream;
|
2017-02-23 20:16:04 +00:00
|
|
|
close((intptr_t)stream->socket_descriptor);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-02 19:10:12 +00:00
|
|
|
/**
|
|
|
|
* Write to an open multistream host
|
|
|
|
* @param socket_fd the socket file descriptor
|
|
|
|
* @param data the data to send
|
|
|
|
* @param data_length the length of the data
|
|
|
|
* @returns the number of bytes written
|
|
|
|
*/
|
2017-03-09 15:00:45 +00:00
|
|
|
int libp2p_net_multistream_write(void* stream_context, const unsigned char* data, size_t data_length) {
|
2017-04-13 14:30:28 +00:00
|
|
|
struct SessionContext* session_context = (struct SessionContext*)stream_context;
|
|
|
|
struct Stream* stream = session_context->insecure_stream;
|
2017-02-02 19:10:12 +00:00
|
|
|
int num_bytes = 0;
|
|
|
|
|
|
|
|
if (data_length > 0) { // only do this is if there is something to send
|
|
|
|
// first send the size
|
|
|
|
unsigned char varint[12];
|
|
|
|
size_t varint_size = 0;
|
|
|
|
varint_encode(data_length, &varint[0], 12, &varint_size);
|
2017-02-23 20:16:04 +00:00
|
|
|
num_bytes = socket_write(*((int*)stream->socket_descriptor), (char*)varint, varint_size, 0);
|
2017-02-02 19:10:12 +00:00
|
|
|
if (num_bytes == 0)
|
|
|
|
return 0;
|
|
|
|
// then send the actual data
|
2017-02-23 20:16:04 +00:00
|
|
|
num_bytes += socket_write(*((int*)stream->socket_descriptor), (char*)data, data_length, 0);
|
2017-02-02 19:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return num_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read from a multistream socket
|
|
|
|
* @param socket_fd the socket file descriptor
|
|
|
|
* @param results where to put the results. NOTE: this memory is allocated
|
|
|
|
* @param results_size the size of the results in bytes
|
2017-04-17 19:03:27 +00:00
|
|
|
* @param timeout_secs the seconds before a timeout
|
2017-02-09 03:51:35 +00:00
|
|
|
* @returns number of bytes received
|
2017-02-02 19:10:12 +00:00
|
|
|
*/
|
2017-04-17 19:03:27 +00:00
|
|
|
int libp2p_net_multistream_read(void* stream_context, unsigned char** results, size_t* results_size, int timeout_secs) {
|
2017-04-13 14:30:28 +00:00
|
|
|
struct SessionContext* session_context = (struct SessionContext*)stream_context;
|
|
|
|
struct Stream* stream = session_context->insecure_stream;
|
2017-02-02 19:10:12 +00:00
|
|
|
int bytes = 0;
|
|
|
|
size_t buffer_size = 65535;
|
|
|
|
char buffer[buffer_size];
|
2017-02-09 03:51:35 +00:00
|
|
|
char* pos = buffer;
|
2017-02-09 08:34:12 +00:00
|
|
|
size_t num_bytes_requested = 0, left = 0, already_read = 0;
|
2017-02-09 03:51:35 +00:00
|
|
|
|
|
|
|
// first read the varint
|
|
|
|
while(1) {
|
|
|
|
unsigned char c;
|
2017-04-17 19:03:27 +00:00
|
|
|
bytes = socket_read(*((int*)stream->socket_descriptor), (char*)&c, 1, 0, timeout_secs);
|
|
|
|
if (bytes == 0) { // timeout
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-09 03:51:35 +00:00
|
|
|
pos[0] = c;
|
|
|
|
if (c >> 7 == 0) {
|
|
|
|
pos[1] = 0;
|
2017-02-09 08:34:12 +00:00
|
|
|
num_bytes_requested = varint_decode((unsigned char*)buffer, strlen(buffer), NULL);
|
2017-02-09 03:51:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
2017-02-09 08:34:12 +00:00
|
|
|
if (num_bytes_requested <= 0)
|
2017-02-09 03:51:35 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-02-09 08:34:12 +00:00
|
|
|
left = num_bytes_requested;
|
|
|
|
do {
|
2017-04-17 19:03:27 +00:00
|
|
|
bytes = socket_read(*((int*)stream->socket_descriptor), &buffer[already_read], left, 0, timeout_secs);
|
2017-02-09 08:34:12 +00:00
|
|
|
if (bytes < 0) {
|
|
|
|
bytes = 0;
|
2017-04-17 19:03:27 +00:00
|
|
|
if ( (errno == EAGAIN)) {
|
2017-02-09 08:34:12 +00:00
|
|
|
// do something intelligent
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
left = left - bytes;
|
|
|
|
already_read += bytes;
|
|
|
|
} while (left > 0);
|
2017-02-02 19:10:12 +00:00
|
|
|
|
2017-02-09 08:34:12 +00:00
|
|
|
if (already_read != num_bytes_requested)
|
2017-02-02 19:10:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// parse the results, removing the leading size indicator
|
2017-02-09 08:34:12 +00:00
|
|
|
*results = malloc(num_bytes_requested);
|
|
|
|
if (*results == NULL)
|
|
|
|
return 0;
|
|
|
|
memcpy(*results, buffer, num_bytes_requested);
|
|
|
|
*results_size = num_bytes_requested;
|
|
|
|
return num_bytes_requested;
|
2017-02-02 19:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a multistream host, and this includes the multistream handshaking.
|
|
|
|
* @param hostname the host
|
|
|
|
* @param port the port
|
|
|
|
* @returns the socket file descriptor of the connection, or -1 on error
|
|
|
|
*/
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Stream* libp2p_net_multistream_connect(const char* hostname, int port) {
|
2017-02-02 20:43:35 +00:00
|
|
|
int retVal = -1, return_result = -1, socket = -1;
|
2017-02-23 20:16:04 +00:00
|
|
|
unsigned char* results = NULL;
|
2017-02-02 19:10:12 +00:00
|
|
|
size_t results_size;
|
|
|
|
size_t num_bytes = 0;
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Stream* stream = NULL;
|
2017-02-02 19:10:12 +00:00
|
|
|
|
|
|
|
uint32_t ip = hostname_to_ip(hostname);
|
|
|
|
socket = socket_open4();
|
|
|
|
|
|
|
|
// connect
|
|
|
|
if (socket_connect4(socket, ip, port) != 0)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
// send the multistream handshake
|
2017-02-02 20:43:35 +00:00
|
|
|
char* protocol_buffer = "/multistream/1.0.0\n";
|
|
|
|
|
2017-04-04 01:54:41 +00:00
|
|
|
stream = libp2p_net_multistream_stream_new(socket, hostname, port);
|
2017-02-23 20:16:04 +00:00
|
|
|
if (stream == NULL)
|
|
|
|
goto exit;
|
|
|
|
|
2017-03-19 12:42:52 +00:00
|
|
|
struct SessionContext session;
|
2017-03-09 17:49:47 +00:00
|
|
|
session.insecure_stream = stream;
|
|
|
|
session.default_stream = stream;
|
|
|
|
|
2017-02-08 17:32:41 +00:00
|
|
|
// try to receive the protocol id
|
2017-04-17 19:03:27 +00:00
|
|
|
return_result = libp2p_net_multistream_read(&session, &results, &results_size, 5);
|
2017-02-08 17:32:41 +00:00
|
|
|
if (return_result == 0 || results_size < 1)
|
|
|
|
goto exit;
|
|
|
|
|
2017-03-09 17:49:47 +00:00
|
|
|
num_bytes = libp2p_net_multistream_write(&session, (unsigned char*)protocol_buffer, strlen(protocol_buffer));
|
2017-03-02 21:14:52 +00:00
|
|
|
if (num_bytes <= 0)
|
|
|
|
goto exit;
|
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
if (strstr((char*)results, "multistream") == NULL)
|
2017-02-08 17:32:41 +00:00
|
|
|
goto exit;
|
|
|
|
|
2017-02-02 23:27:57 +00:00
|
|
|
// we are now in the loop, so we can switch to another protocol (i.e. /secio/1.0.0)
|
2017-02-02 19:10:12 +00:00
|
|
|
|
|
|
|
retVal = socket;
|
|
|
|
exit:
|
|
|
|
if (results != NULL)
|
|
|
|
free(results);
|
2017-02-23 20:16:04 +00:00
|
|
|
if (retVal < 0 && stream != NULL) {
|
|
|
|
libp2p_net_multistream_stream_free(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
|
|
|
return stream;
|
2017-02-02 19:10:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
int libp2p_net_multistream_negotiate(struct Stream* stream) {
|
2017-02-23 16:15:48 +00:00
|
|
|
const char* protocolID = "/multistream/1.0.0\n";
|
2017-02-23 20:16:04 +00:00
|
|
|
unsigned char* results = NULL;
|
2017-02-23 16:15:48 +00:00
|
|
|
size_t results_length = 0;
|
2017-02-23 20:16:04 +00:00
|
|
|
int retVal = 0;
|
2017-02-23 16:15:48 +00:00
|
|
|
// send the protocol id
|
2017-03-19 12:42:52 +00:00
|
|
|
struct SessionContext secure_session;
|
2017-03-09 17:49:47 +00:00
|
|
|
secure_session.insecure_stream = stream;
|
|
|
|
secure_session.default_stream = stream;
|
|
|
|
if (!libp2p_net_multistream_write(&secure_session, (unsigned char*)protocolID, strlen(protocolID)))
|
2017-02-23 20:16:04 +00:00
|
|
|
goto exit;
|
2017-02-23 16:15:48 +00:00
|
|
|
// expect the same back
|
2017-04-17 19:03:27 +00:00
|
|
|
libp2p_net_multistream_read(&secure_session, &results, &results_length, 5);
|
2017-02-23 16:15:48 +00:00
|
|
|
if (results_length == 0)
|
2017-02-23 20:16:04 +00:00
|
|
|
goto exit;
|
|
|
|
if (strncmp((char*)results, protocolID, strlen(protocolID)) != 0)
|
|
|
|
goto exit;
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (results != NULL)
|
|
|
|
free(results);
|
|
|
|
return retVal;
|
2017-02-23 16:15:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:49:47 +00:00
|
|
|
|
2017-02-23 16:15:48 +00:00
|
|
|
/**
|
2017-02-23 20:16:04 +00:00
|
|
|
* Expect to read a message
|
2017-02-23 16:15:48 +00:00
|
|
|
* @param fd the socket file descriptor
|
2017-02-23 20:16:04 +00:00
|
|
|
* @returns the retrieved message, or NULL
|
2017-02-23 16:15:48 +00:00
|
|
|
*/
|
2017-03-09 17:49:47 +00:00
|
|
|
/*
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Libp2pMessage* libp2p_net_multistream_get_message(struct Stream* stream) {
|
2017-02-23 16:15:48 +00:00
|
|
|
int retVal = 0;
|
|
|
|
unsigned char* results = NULL;
|
|
|
|
size_t results_size = 0;
|
|
|
|
struct Libp2pMessage* msg = NULL;
|
|
|
|
// read what they sent
|
2017-02-23 20:16:04 +00:00
|
|
|
libp2p_net_multistream_read(stream, &results, &results_size);
|
2017-02-23 16:15:48 +00:00
|
|
|
// unprotobuf it
|
|
|
|
if (!libp2p_message_protobuf_decode(results, results_size, &msg))
|
|
|
|
goto exit;
|
|
|
|
// clean up
|
|
|
|
retVal = 1;
|
|
|
|
exit:
|
|
|
|
if (results != NULL)
|
|
|
|
free(results);
|
2017-02-23 20:16:04 +00:00
|
|
|
if (retVal != 1 && msg != NULL)
|
2017-02-23 16:15:48 +00:00
|
|
|
libp2p_message_free(msg);
|
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
return msg;
|
|
|
|
}
|
2017-03-09 17:49:47 +00:00
|
|
|
*/
|
2017-02-23 20:16:04 +00:00
|
|
|
|
|
|
|
void libp2p_net_multistream_stream_free(struct Stream* stream) {
|
|
|
|
if (stream != NULL) {
|
|
|
|
if (stream->socket_descriptor != NULL)
|
|
|
|
free(stream->socket_descriptor);
|
2017-04-04 01:54:41 +00:00
|
|
|
if (stream->address != NULL)
|
|
|
|
multiaddress_free(stream->address);
|
2017-02-23 20:16:04 +00:00
|
|
|
free(stream);
|
|
|
|
}
|
2017-02-23 16:15:48 +00:00
|
|
|
}
|
2017-02-23 20:16:04 +00:00
|
|
|
|
2017-04-04 01:54:41 +00:00
|
|
|
/**
|
|
|
|
* Create a new MultiStream structure
|
|
|
|
* @param socket_fd the file descriptor
|
|
|
|
* @param ip the IP address
|
|
|
|
* @param port the port
|
|
|
|
*/
|
|
|
|
struct Stream* libp2p_net_multistream_stream_new(int socket_fd, const char* ip, int port) {
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Stream* out = (struct Stream*)malloc(sizeof(struct Stream));
|
|
|
|
if (out != NULL) {
|
|
|
|
out->socket_descriptor = malloc(sizeof(int));
|
|
|
|
*((int*)out->socket_descriptor) = socket_fd;
|
|
|
|
int res = *((int*)out->socket_descriptor);
|
|
|
|
if (res != socket_fd) {
|
|
|
|
libp2p_net_multistream_stream_free(out);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
out->close = libp2p_net_multistream_close;
|
|
|
|
out->read = libp2p_net_multistream_read;
|
|
|
|
out->write = libp2p_net_multistream_write;
|
2017-04-04 01:54:41 +00:00
|
|
|
char str[strlen(ip) + 50];
|
|
|
|
sprintf(str, "/ip4/%s/tcp/%d", ip, port);
|
|
|
|
out->address = multiaddress_new_from_string(str);
|
2017-02-23 20:16:04 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|