c-ipfs/core/ping.c

105 lines
2.9 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"
2017-04-03 18:20:35 +00:00
#include "libp2p/routing/dht_protocol.h"
2017-03-09 17:50:08 +00:00
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/repo/init.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)
{
2017-04-03 22:26:33 +00:00
int retVal = 0;
2017-04-03 16:55:36 +00:00
struct MultiAddress* address;
int addressAllocated = 0;
struct IpfsNode local_node;
struct Stream* stream = NULL;
2017-04-03 22:26:33 +00:00
struct Libp2pPeer* peer_to_ping = NULL;
char* id = NULL;
struct FSRepo* fs_repo = NULL;
char* repo_path = NULL;
2017-04-03 16:55:36 +00:00
// sanity check
if (argc < 3)
2017-04-03 22:26:33 +00:00
goto exit;
2017-03-09 17:50:08 +00:00
if (!ipfs_repo_get_directory(argc, argv, &repo_path)) {
fprintf(stderr, "Unable to open repo: %s\n", repo_path);
return 0;
}
2017-03-09 17:50:08 +00:00
// read the configuration
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.identity->peer_id);
2017-04-03 16:55:36 +00:00
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
2017-04-03 22:26:33 +00:00
if (strstr(argv[2], "Qm") == &argv[2][0]) {
2017-04-03 16:55:36 +00:00
// resolve the peer id
2017-04-03 22:26:33 +00:00
peer_to_ping = ipfs_resolver_find_peer(argv[2], &local_node);
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);
if (address != NULL)
addressAllocated = 1;
2017-04-03 22:26:33 +00:00
peer_to_ping = libp2p_peer_new();
peer_to_ping->addr_head = libp2p_utils_linked_list_new();
peer_to_ping->addr_head->item = address;
peer_to_ping->id = str;
peer_to_ping->id_size = strlen(str);
free(str);
2017-04-03 16:55:36 +00:00
}
2017-03-09 17:50:08 +00:00
//TODO: Error checking
}
2017-04-03 22:26:33 +00:00
if (peer_to_ping == NULL)
2017-04-03 17:42:35 +00:00
goto exit;
2017-03-09 17:50:08 +00:00
2017-04-03 22:26:33 +00:00
if (!local_node.routing->Ping(local_node.routing, peer_to_ping)) {
id = malloc(peer_to_ping->id_size + 1);
memcpy(id, peer_to_ping->id, peer_to_ping->id_size);
id[peer_to_ping->id_size] = 0;
fprintf(stderr, "Unable to ping %s\n", id);
free(id);
2017-04-03 16:55:36 +00:00
goto exit;
2017-03-09 17:50:08 +00:00
}
2017-04-03 16:55:36 +00:00
retVal = 1;
exit:
if (addressAllocated)
multiaddress_free(address);
2017-04-03 22:26:33 +00:00
if (fs_repo != NULL)
ipfs_repo_fsrepo_free(fs_repo);
if (local_node.peerstore != NULL)
libp2p_peerstore_free(local_node.peerstore);
if (local_node.providerstore != NULL)
libp2p_providerstore_free(local_node.providerstore);
2017-04-03 16:55:36 +00:00
return retVal;
}