2017-02-10 02:53:58 +00:00
|
|
|
#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"
|
2017-02-23 16:16:23 +00:00
|
|
|
#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"
|
2017-04-06 22:46:40 +00:00
|
|
|
#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"
|
2017-02-10 02:53:58 +00:00
|
|
|
|
|
|
|
#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 IpfsNode local_node;
|
2017-04-03 22:26:33 +00:00
|
|
|
struct Libp2pPeer* peer_to_ping = NULL;
|
|
|
|
char* id = NULL;
|
|
|
|
struct FSRepo* fs_repo = NULL;
|
2017-04-06 22:46:40 +00:00
|
|
|
char* repo_path = NULL;
|
2017-06-08 02:20:00 +00:00
|
|
|
struct timeval time1, time2;
|
|
|
|
int time_us;
|
2017-04-03 16:55:36 +00:00
|
|
|
|
|
|
|
// sanity check
|
2017-06-06 22:29:12 +00:00
|
|
|
local_node.peerstore = NULL;
|
|
|
|
local_node.providerstore = NULL;
|
2017-04-03 16:55:36 +00:00
|
|
|
if (argc < 3)
|
2017-04-03 22:26:33 +00:00
|
|
|
goto exit;
|
2017-03-09 17:50:08 +00:00
|
|
|
|
2017-04-06 22:46:40 +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;
|
2017-07-27 17:05:41 +00:00
|
|
|
local_node.routing = ipfs_routing_new_online(&local_node, &fs_repo->config->identity->private_key);
|
2017-05-11 12:04:54 +00:00
|
|
|
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-06-08 02:20:00 +00:00
|
|
|
fprintf (stderr, "Looking up peer %s\n", argv[2]);
|
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]);
|
2017-04-03 22:26:33 +00:00
|
|
|
peer_to_ping = libp2p_peer_new();
|
2017-06-07 00:40:10 +00:00
|
|
|
if (peer_to_ping) {
|
|
|
|
peer_to_ping->addr_head = libp2p_utils_linked_list_new();
|
|
|
|
peer_to_ping->addr_head->item = multiaddress_new_from_string(str);
|
|
|
|
peer_to_ping->id = str;
|
|
|
|
peer_to_ping->id_size = strlen(str);
|
|
|
|
}
|
2017-04-03 16:55:36 +00:00
|
|
|
}
|
2017-03-09 17:50:08 +00:00
|
|
|
//TODO: Error checking
|
|
|
|
}
|
2017-02-23 16:16:23 +00:00
|
|
|
|
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-06-08 02:20:00 +00:00
|
|
|
id = malloc(peer_to_ping->id_size + 1);
|
|
|
|
if (id) {
|
2017-04-03 22:26:33 +00:00
|
|
|
memcpy(id, peer_to_ping->id, peer_to_ping->id_size);
|
|
|
|
id[peer_to_ping->id_size] = 0;
|
2017-06-08 02:20:00 +00:00
|
|
|
fprintf (stderr, "PING %s.\n", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
gettimeofday(&time1, NULL);
|
|
|
|
if (!local_node.routing->Ping(local_node.routing, peer_to_ping)) {
|
|
|
|
fprintf(stderr, "Unable to ping %s\n", id);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
gettimeofday(&time2, NULL);
|
|
|
|
|
2017-07-17 18:05:56 +00:00
|
|
|
// calculate microseconds
|
2017-06-08 02:20:00 +00:00
|
|
|
time_us = (time2.tv_sec - time1.tv_sec) * 1000000;
|
|
|
|
time_us += (time2.tv_usec - time1.tv_usec);
|
|
|
|
fprintf (stderr, "Pong received: time=%d.%03d ms\n", time_us / 1000, time_us % 1000);
|
2017-07-17 18:05:56 +00:00
|
|
|
if (time_us < 1000000) { // if the ping took less than a second...
|
|
|
|
sleep(1);
|
|
|
|
}
|
2017-03-09 17:50:08 +00:00
|
|
|
}
|
2017-02-23 16:16:23 +00:00
|
|
|
|
2017-04-03 16:55:36 +00:00
|
|
|
retVal = 1;
|
|
|
|
exit:
|
2017-06-08 02:20:00 +00:00
|
|
|
if (id != NULL)
|
|
|
|
free(id);
|
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;
|
2017-02-23 16:16:23 +00:00
|
|
|
|
2017-02-10 02:53:58 +00:00
|
|
|
}
|