c-ipfs/test/core/test_ping.h

88 lines
2.7 KiB
C
Raw Permalink Normal View History

#pragma once
#include <stdlib.h>
#include "../test_helper.h"
#include "multiaddr/multiaddr.h"
#include "libp2p/peer/peerstore.h"
#include "libp2p/peer/peer.h"
#include "libp2p/record/message.h"
#include "ipfs/core/daemon.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "libp2p/conn/dialer.h"
2017-04-03 16:55:36 +00:00
#include "ipfs/core/daemon.h"
/**
* Testing connectivity with other nodes
*/
int test_ping() {
int retVal = 0;
struct FSRepo* fs_repo = NULL;
2017-08-30 16:10:14 +00:00
struct KademliaMessage* message = NULL;
2017-03-09 15:01:09 +00:00
//struct IpfsNode local_node;
struct Libp2pPeer* remote_peer = NULL;
struct Dialer* dialer = NULL;
2017-10-23 23:03:55 +00:00
struct Stream* conn = NULL;
unsigned char* protobuf = NULL;
size_t protobuf_size = 0;
// act like this is a normal node
drop_build_and_open_repo("/tmp/.ipfs", &fs_repo);
// create a new IpfsNode
2017-03-09 15:01:09 +00:00
/*
local_node.mode = MODE_ONLINE;
local_node.peerstore = libp2p_peerstore_new();
local_node.repo = fs_repo;
local_node.identity = fs_repo->config->identity;
2017-03-09 15:01:09 +00:00
*/
// build the ping message
message = libp2p_message_new();
message->message_type = MESSAGE_TYPE_PING;
protobuf_size = libp2p_message_protobuf_encode_size(message);
protobuf = (unsigned char*)malloc(protobuf_size);
libp2p_message_protobuf_encode(message, protobuf, protobuf_size, &protobuf_size);
libp2p_message_free(message);
message = NULL;
// ping a known node with a ping record
// create the connection
remote_peer = libp2p_peer_new();
remote_peer->id = "QmdMZoaL4azVzEPHYVH6imn3iPYYv6L1fLcH7Vd1aLbfSD";
remote_peer->id_size = strlen(remote_peer->id);
remote_peer->addr_head = libp2p_utils_linked_list_new();
remote_peer->addr_head->item = multiaddress_new_from_string("/ip4/192.168.43.234/tcp/4001/");
// connect using a dialer
2017-11-29 03:44:45 +00:00
dialer = libp2p_conn_dialer_new(fs_repo->config->identity->peer, NULL, NULL, NULL);
conn = libp2p_conn_dialer_get_connection(dialer, remote_peer->addr_head->item);
//TODO: Dialer should know the protocol
// send the record
2017-10-23 23:03:55 +00:00
struct StreamMessage msg;
msg.data = (uint8_t*)protobuf;
msg.data_size = protobuf_size;
conn->write(conn->stream_context, &msg);
struct StreamMessage* incoming_message = NULL;
conn->read(conn->stream_context, &incoming_message, 10);
libp2p_message_protobuf_decode(incoming_message->data, incoming_message->data_size, &message);
// verify the response
if (message->message_type != MESSAGE_TYPE_PING)
goto exit;
// clean up
retVal = 1;
exit:
ipfs_repo_fsrepo_free(fs_repo);
return retVal;
}
2017-04-03 16:55:36 +00:00
int test_ping_remote() {
2017-04-03 22:26:33 +00:00
char* argv[] = { "ipfs", "ping", "QmTjg669YQemhffXLrkA3as9jT8SzyRtWaLXHKwYN6wCBd" };
2017-04-03 16:55:36 +00:00
int argc = 3;
return ipfs_ping(argc, argv);
}