2017-02-02 19:10:12 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
|
|
|
|
2017-02-02 19:10:12 +00:00
|
|
|
/***
|
|
|
|
* An implementation of the libp2p multistream
|
2017-02-23 20:16:04 +00:00
|
|
|
*
|
|
|
|
* NOTE: This is a severe twist on (break from?) what is multistream. In the GO code,
|
|
|
|
* multistream does the initial connection, and has a list of protocols that
|
|
|
|
* do the work. Here, we've gotten rid of the protocols for now, in order to
|
|
|
|
* get things working. We're passing around DHT messages for now.
|
|
|
|
*
|
|
|
|
* So in short, much of this will change. But for now, think of it as a Proof of Concept.
|
2017-02-02 19:10:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-02-23 20:16:04 +00:00
|
|
|
* Read from a multistream socket
|
2017-02-02 19:10:12 +00:00
|
|
|
* @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-02-23 20:16:04 +00:00
|
|
|
int libp2p_net_multistream_read(struct Stream* stream, unsigned char** data, size_t* data_length);
|
2017-02-02 19:10:12 +00:00
|
|
|
/**
|
2017-02-23 20:16:04 +00:00
|
|
|
* Write to an open multistream host
|
2017-02-02 19:10:12 +00:00
|
|
|
* @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
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
2017-02-23 20:16:04 +00:00
|
|
|
int libp2p_net_multistream_write(struct Stream* stream, const unsigned char* data, size_t data_size);
|
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-23 16:15:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Negotiate the multistream protocol by sending and receiving the protocol id. This is a server side function.
|
|
|
|
* Servers should send the protocol ID, and then expect it back.
|
|
|
|
* @param fd the socket file descriptor
|
2017-02-23 20:16:04 +00:00
|
|
|
* @returns true(1) on success, or false(0)
|
2017-02-23 16:15:48 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* Expect to read a message, and follow its instructions
|
|
|
|
* @param fd the socket file descriptor
|
|
|
|
* @returns true(1) on success, false(0) if not
|
|
|
|
*/
|
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
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Stream* libp2p_net_multistream_stream_new(int socket_fd);
|
2017-02-23 16:15:48 +00:00
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
void libp2p_net_multistream_stream_free(struct Stream* stream);
|