2017-02-01 12:52:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-01 17:14:52 +00:00
|
|
|
#include "libp2p/crypto/key.h"
|
2017-02-02 19:10:12 +00:00
|
|
|
#include "libp2p/crypto/rsa.h"
|
2017-03-19 12:42:52 +00:00
|
|
|
#include "libp2p/conn/session.h"
|
2017-07-31 21:36:08 +00:00
|
|
|
#include "libp2p/peer/peerstore.h"
|
2017-08-09 13:03:40 +00:00
|
|
|
#include "libp2p/net/protocol.h"
|
2017-02-01 17:14:52 +00:00
|
|
|
|
2017-02-01 12:52:09 +00:00
|
|
|
/**
|
2017-03-19 12:42:52 +00:00
|
|
|
* Handling of a secure connection
|
2017-02-01 12:52:09 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-29 03:44:18 +00:00
|
|
|
enum SecioStatus {
|
|
|
|
secio_status_unknown,
|
|
|
|
secio_status_initialized,
|
|
|
|
secio_status_syn,
|
|
|
|
secio_status_ack
|
|
|
|
};
|
|
|
|
|
2017-10-23 20:21:50 +00:00
|
|
|
struct SecioContext {
|
|
|
|
struct Stream* stream;
|
|
|
|
struct SessionContext* session_context;
|
|
|
|
struct RsaPrivateKey* private_key;
|
|
|
|
struct Peerstore* peer_store;
|
2017-11-02 18:43:52 +00:00
|
|
|
struct StreamMessage* buffered_message;
|
|
|
|
size_t buffered_message_pos;
|
2017-11-29 03:44:18 +00:00
|
|
|
volatile enum SecioStatus status;
|
2017-10-23 20:21:50 +00:00
|
|
|
};
|
2017-02-02 19:10:12 +00:00
|
|
|
|
2017-08-09 13:03:40 +00:00
|
|
|
struct Libp2pProtocolHandler* libp2p_secio_build_protocol_handler(struct RsaPrivateKey* private_key, struct Peerstore* peer_store);
|
|
|
|
|
2017-02-02 19:10:12 +00:00
|
|
|
/***
|
2017-10-23 23:03:38 +00:00
|
|
|
* Initiates a secio handshake. Use this method when you want to initiate a secio
|
|
|
|
* session. This should not be used to respond to incoming secio requests
|
|
|
|
* @param parent_stream the parent stream
|
2017-10-25 17:28:53 +00:00
|
|
|
* @param peerstore the peerstore
|
|
|
|
* @param rsa_private_key the local private key
|
2017-10-23 23:03:38 +00:00
|
|
|
* @returns a Secio Stream
|
2017-02-02 19:10:12 +00:00
|
|
|
*/
|
2017-11-29 03:44:18 +00:00
|
|
|
struct Stream* libp2p_secio_stream_new(struct Stream* parent_stream, struct Peerstore* peerstore, struct RsaPrivateKey* rsa_private_key);
|
2017-08-09 15:57:17 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Initiates a secio handshake. Use this method when you want to initiate a secio
|
|
|
|
* session. This should not be used to respond to incoming secio requests
|
2017-10-23 20:21:50 +00:00
|
|
|
* @param ctx the SecioContext
|
2017-08-09 15:57:17 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-10-23 20:21:50 +00:00
|
|
|
int libp2p_secio_initiate_handshake(struct SecioContext* ctx);
|
2017-08-31 21:40:35 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Send the protocol string to the remote stream
|
2017-11-08 15:51:43 +00:00
|
|
|
* @param stream stream
|
2017-08-31 21:40:35 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int libp2p_secio_send_protocol(struct Stream* stream);
|
|
|
|
|
2017-08-31 21:40:35 +00:00
|
|
|
/***
|
|
|
|
* Attempt to read the secio protocol as a reply from the remote
|
|
|
|
* @param session the context
|
|
|
|
* @returns true(1) if we received what we think we should have, false(0) otherwise
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int libp2p_secio_receive_protocol(struct Stream* stream);
|
2017-10-23 23:03:38 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* performs initial communication over an insecure channel to share
|
|
|
|
* keys, IDs, and initiate connection. This is a framed messaging system
|
|
|
|
* NOTE: session must contain a valid socket_descriptor that is a multistream.
|
2017-11-29 03:44:18 +00:00
|
|
|
* @param secio_stream a stream that is a Secio stream
|
2017-10-23 23:03:38 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-11-29 03:44:18 +00:00
|
|
|
int libp2p_secio_handshake(struct Stream* secio_stream);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Wait for secio 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_secio_ready(struct SessionContext* session_context, int timeout_secs);
|
2017-10-23 23:03:38 +00:00
|
|
|
|