c-ipfs/core/ping.c

151 lines
4.3 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "libp2p/net/p2pnet.h"
#include "libp2p/net/multistream.h"
#include "libp2p/record/message.h"
2017-03-09 17:50:08 +00:00
#include "libp2p/secio/secio.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
2017-04-03 16:55:36 +00:00
#include "ipfs/core/ipfs_node.h"
#include "ipfs/routing/routing.h"
#include "ipfs/importer/resolver.h"
#include "multiaddr/multiaddr.h"
#define BUF_SIZE 4096
int ipfs_ping (int argc, char **argv)
{
unsigned char* results = NULL;
size_t results_size = 0;
2017-03-09 17:50:08 +00:00
int port = 0;
char* ip = NULL;
struct SessionContext session;
2017-04-03 16:55:36 +00:00
struct MultiAddress* address;
int addressAllocated = 0;
int retVal = 0;
struct Libp2pMessage *msg = NULL, *msg_returned = NULL;
struct IpfsNode local_node;
unsigned char* protobuf = NULL;
size_t protobuf_size = 0;
struct Stream* stream = NULL;
// sanity check
if (argc < 3)
return 0;
2017-03-09 17:50:08 +00:00
// read the configuration
struct FSRepo* fs_repo;
if (!ipfs_repo_fsrepo_new(NULL, NULL, &fs_repo))
2017-04-03 16:55:36 +00:00
goto exit;
2017-03-09 17:50:08 +00:00
// open the repository and read the file
2017-04-03 16:55:36 +00:00
if (!ipfs_repo_fsrepo_open(fs_repo))
goto exit;
local_node.identity = fs_repo->config->identity;
local_node.repo = fs_repo;
local_node.mode = MODE_ONLINE;
local_node.routing = ipfs_routing_new_online(&local_node, &fs_repo->config->identity->private_key, stream);
local_node.peerstore = libp2p_peerstore_new();
local_node.providerstore = libp2p_providerstore_new();
if (local_node.routing->Bootstrap(local_node.routing) != 0)
goto exit;
2017-03-09 17:50:08 +00:00
if (strstr(argv[2], "/ipfs/") != NULL) {
2017-04-03 16:55:36 +00:00
// resolve the peer id
struct Libp2pPeer *peer = ipfs_resolver_find_peer(argv[2], &local_node);
struct Libp2pLinkedList* current = peer->addr_head;
// try to find an IP version of the multiaddress
while (current != NULL) {
address = (struct MultiAddress*)current->item;
if (multiaddress_is_ip(address))
break;
address = NULL;
}
2017-03-09 17:50:08 +00:00
} else {
2017-04-03 16:55:36 +00:00
// perhaps they passed an IP and port
if (argc >= 3) {
char* str = malloc(strlen(argv[2]) + strlen(argv[3]) + 100);
sprintf(str, "/ip4/%s/tcp/%s", argv[2], argv[3]);
address = multiaddress_new_from_string(str);
free(str);
if (address != NULL)
addressAllocated = 1;
}
2017-03-09 17:50:08 +00:00
//TODO: Error checking
}
2017-04-03 16:55:36 +00:00
if (address == NULL || !multiaddress_is_ip(address)) {
fprintf(stderr, "Unable to find address\n");
goto exit;
}
if (!multiaddress_get_ip_address(address, &ip)) {
fprintf(stderr, "Could not convert IP address %s\n", address->string);
goto exit;
}
port = multiaddress_get_ip_port(address);
2017-03-09 17:50:08 +00:00
session.insecure_stream = libp2p_net_multistream_connect(ip, port);
session.default_stream = session.insecure_stream;
if (session.insecure_stream == NULL) {
2017-04-03 16:55:36 +00:00
fprintf(stderr, "Unable to connect to %s on port %d", ip, port);
goto exit;
}
2017-03-09 17:50:08 +00:00
// try to switch to secio
if (!libp2p_secio_handshake(&session, &fs_repo->config->identity->private_key, 0)) {
fprintf(stderr, "Unable to switch to secure connection. Attempting insecure ping...\n");
}
// prepare the PING message
2017-04-03 16:55:36 +00:00
msg = libp2p_message_new();
msg->message_type = MESSAGE_TYPE_PING;
2017-04-03 16:55:36 +00:00
protobuf_size = libp2p_message_protobuf_encode_size(msg);
protobuf = (unsigned char*)malloc(protobuf_size);
libp2p_message_protobuf_encode(msg, &protobuf[0], protobuf_size, &protobuf_size);
2017-03-09 17:50:08 +00:00
session.default_stream->write(&session, protobuf, protobuf_size);
session.default_stream->read(&session, &results, &results_size);
// see if we can unprotobuf
libp2p_message_protobuf_decode(results, results_size, &msg_returned);
if (msg_returned->message_type != MESSAGE_TYPE_PING) {
fprintf(stderr, "Ping unsuccessful. Returned message was not a PING");
2017-04-03 16:55:36 +00:00
goto exit;
2017-03-09 17:50:08 +00:00
}
if (results_size != protobuf_size) {
fprintf(stderr, "PING unsuccessful. Original size: %lu, returned size: %lu\n", protobuf_size, results_size);
2017-04-03 16:55:36 +00:00
goto exit;
}
if (memcmp(results, protobuf, protobuf_size) != 0) {
fprintf(stderr, "PING unsuccessful. Results do not match.\n");
2017-04-03 16:55:36 +00:00
goto exit;
}
2017-04-03 16:55:36 +00:00
fprintf(stdout, "Ping of %s:%d successful.\n", ip, port);
retVal = 1;
exit:
if (fs_repo != NULL)
ipfs_repo_fsrepo_free(fs_repo);
if (addressAllocated)
multiaddress_free(address);
if (ip != NULL)
free(ip);
if (msg != NULL)
libp2p_message_free(msg);
2017-04-03 16:55:36 +00:00
if (msg_returned != NULL)
libp2p_message_free(msg_returned);
if (protobuf != NULL)
free(protobuf);
return retVal;
}