2017-10-11 16:23:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "libp2p/net/protocol.h"
|
2017-10-23 23:03:38 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
2017-11-06 18:36:11 +00:00
|
|
|
#include "libp2p/yamux/stream.h"
|
2017-10-23 23:03:38 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Declarations for the Yamux protocol
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
|
2017-11-02 19:45:17 +00:00
|
|
|
static const int yamux_default_timeout = 10;
|
|
|
|
|
2017-11-06 18:36:11 +00:00
|
|
|
static const char YAMUX_CONTEXT = 'Y';
|
|
|
|
static const char YAMUX_CHANNEL_CONTEXT = 'C';
|
|
|
|
|
2017-11-23 12:44:42 +00:00
|
|
|
struct YamuxProtocolContext {
|
|
|
|
struct Libp2pVector* protocol_handlers;
|
|
|
|
};
|
|
|
|
|
2017-11-02 19:45:17 +00:00
|
|
|
/***
|
|
|
|
* Context struct for Yamux
|
|
|
|
*/
|
|
|
|
struct YamuxContext {
|
2017-11-06 18:36:11 +00:00
|
|
|
char type;
|
2017-11-02 19:45:17 +00:00
|
|
|
struct Stream* stream;
|
|
|
|
struct yamux_session* session;
|
2017-11-06 12:27:03 +00:00
|
|
|
struct Libp2pVector* channels;
|
2017-11-19 18:37:03 +00:00
|
|
|
int am_server;
|
|
|
|
int state; // the state of the connection
|
|
|
|
struct Libp2pVector* protocol_handlers;
|
2017-11-23 11:23:50 +00:00
|
|
|
/**
|
|
|
|
* What is stored here is from a read_raw call. It could
|
|
|
|
* be garbage, but it could be a decent message. It has
|
|
|
|
* been "unframed" so contains the data portion of the
|
|
|
|
* last frame captured in a read_raw call, or if it was
|
|
|
|
* empty, the data from a new read call.
|
|
|
|
*/
|
2017-11-20 00:29:40 +00:00
|
|
|
struct StreamMessage* buffered_message;
|
|
|
|
long buffered_message_pos;
|
2017-11-02 19:45:17 +00:00
|
|
|
};
|
|
|
|
|
2017-11-06 18:36:11 +00:00
|
|
|
struct YamuxChannelContext {
|
|
|
|
char type;
|
|
|
|
struct YamuxContext* yamux_context;
|
2017-11-08 15:51:43 +00:00
|
|
|
// this stream
|
2017-11-06 21:38:55 +00:00
|
|
|
struct Stream* stream;
|
2017-11-08 15:51:43 +00:00
|
|
|
// the child protocol's stream
|
|
|
|
struct Stream* child_stream;
|
2017-11-06 21:38:55 +00:00
|
|
|
// the channel number
|
2017-11-23 11:23:50 +00:00
|
|
|
uint32_t channel;
|
2017-11-06 21:38:55 +00:00
|
|
|
// the window size for this channel
|
|
|
|
int window_size;
|
|
|
|
// the state of the connection
|
|
|
|
int state;
|
|
|
|
// whether or not the connection is closed
|
|
|
|
int closed;
|
2017-11-06 18:36:11 +00:00
|
|
|
};
|
|
|
|
|
2017-10-11 16:23:25 +00:00
|
|
|
/**
|
|
|
|
* Build a handler that can handle the yamux protocol
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
struct Libp2pProtocolHandler* libp2p_yamux_build_protocol_handler();
|
2017-10-12 15:12:22 +00:00
|
|
|
/***
|
|
|
|
* Send the yamux protocol out the default stream
|
|
|
|
* NOTE: if we initiate the connection, we should expect the same back
|
2017-11-08 15:51:43 +00:00
|
|
|
* @param stream the stream
|
2017-10-12 15:12:22 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int yamux_send_protocol(struct Stream* stream);
|
2017-10-12 17:37:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Check to see if the reply is the yamux protocol header we expect
|
|
|
|
* NOTE: if we initiate the connection, we should expect the same back
|
|
|
|
* @param context the SessionContext
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-11-06 21:38:55 +00:00
|
|
|
int yamux_receive_protocol(struct YamuxContext* context);
|
2017-10-23 23:03:38 +00:00
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/***
|
|
|
|
* Negotiate the Yamux protocol
|
|
|
|
* @param parent_stream the parent stream
|
|
|
|
* @param am_server true(1) if we are considered the server, false(0) if we are the client.
|
|
|
|
* @param protocol_handlers the protocol handlers (used when a new protocol is requested)
|
|
|
|
* @returns a Stream initialized and ready for yamux
|
|
|
|
*/
|
|
|
|
struct Stream* libp2p_yamux_stream_new(struct Stream* parent_stream, int am_server, struct Libp2pVector* protocol_handlers);
|
2017-11-06 12:27:03 +00:00
|
|
|
|
2017-11-06 18:36:11 +00:00
|
|
|
void libp2p_yamux_stream_free(struct Stream* stream);
|
|
|
|
|
2017-11-06 12:27:03 +00:00
|
|
|
/****
|
|
|
|
* Add a stream "channel" to the yamux handler
|
|
|
|
* @param ctx the context
|
|
|
|
* @param stream the stream to add
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_yamux_stream_add(struct YamuxContext* ctx, struct Stream* stream);
|
2017-11-06 18:36:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a stream that has a "YamuxChannelContext" related to this yamux protocol
|
2017-11-19 18:37:03 +00:00
|
|
|
* NOTE: If incoming_stream is not of the Yamux protocol, this "wraps" the incoming
|
|
|
|
* stream, so that the returned stream is the parent of the incoming_stream. If the
|
|
|
|
* incoming stream is of the yamux protocol, the YamuxChannelContext.child_stream
|
|
|
|
* will be NULL, awaiting an upgrade to fill it in.
|
|
|
|
* @param incoming_stream the stream of the new protocol
|
|
|
|
* @param channelNumber the channel number (0 if unknown)
|
|
|
|
* @returns a new Stream that has a YamuxChannelContext
|
2017-11-06 18:36:11 +00:00
|
|
|
*/
|
2017-11-19 18:37:03 +00:00
|
|
|
struct Stream* libp2p_yamux_channel_stream_new(struct Stream* incoming_stream, int channelNumber);
|
2017-11-06 21:38:55 +00:00
|
|
|
|
|
|
|
void libp2p_yamux_channel_free(struct YamuxChannelContext* ctx);
|
2017-11-19 18:37:03 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Prepare a new Yamux StreamMessage based on another StreamMessage
|
|
|
|
* NOTE: This is here for testing. This should normally not be used.
|
|
|
|
* @param incoming the incoming message
|
|
|
|
* @returns a new StreamMessage that has a yamux_frame
|
|
|
|
*/
|
|
|
|
struct StreamMessage* libp2p_yamux_prepare_to_send(struct StreamMessage* incoming);
|
2017-11-29 04:41:46 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Wait for yamux stream to become ready
|
|
|
|
* @param session_context the session context to check
|
|
|
|
* @param timeout_secs the number of seconds to wait for things to become ready
|
|
|
|
* @returns true(1) if it becomes ready, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_yamux_stream_ready(struct SessionContext* session_context, int timeout_secs);
|