Added ping functionality to multistream
This commit is contained in:
parent
daefe7604f
commit
ae48e058dd
6 changed files with 147 additions and 7 deletions
14
core/null.c
14
core/null.c
|
@ -5,6 +5,8 @@
|
|||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include "libp2p/net/p2pnet.h"
|
||||
#include "libp2p/record/message.h"
|
||||
#include "libp2p/net/multistream.h"
|
||||
#include "ipfs/core/daemon.h"
|
||||
|
||||
#define BUF_SIZE 4096
|
||||
|
@ -12,8 +14,8 @@
|
|||
void *ipfs_null_connection (void *ptr)
|
||||
{
|
||||
struct null_connection_params *connection_param;
|
||||
char b[BUF_SIZE];
|
||||
int len;
|
||||
//char b[BUF_SIZE];
|
||||
//int len;
|
||||
|
||||
connection_param = (struct null_connection_params*) ptr;
|
||||
|
||||
|
@ -21,6 +23,13 @@ void *ipfs_null_connection (void *ptr)
|
|||
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
|
||||
|
||||
for(;;) {
|
||||
if (libp2p_net_multistream_negotiate(connection_param->socket)) {
|
||||
// we negotiated, now find out what they want
|
||||
libp2p_net_multistream_handle_message(connection_param->socket);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
/*
|
||||
len = socket_read(connection_param->socket, b, sizeof(b)-1, 0);
|
||||
if (len > 0) {
|
||||
while (b[len-1] == '\r' || b[len-1] == '\n') len--;
|
||||
|
@ -32,6 +41,7 @@ void *ipfs_null_connection (void *ptr)
|
|||
} else if(len < 0) {
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
close (connection_param->socket); // close socket.
|
||||
|
|
44
core/ping.c
44
core/ping.c
|
@ -5,11 +5,54 @@
|
|||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "libp2p/net/p2pnet.h"
|
||||
#include "libp2p/net/multistream.h"
|
||||
#include "libp2p/record/message.h"
|
||||
|
||||
#define BUF_SIZE 4096
|
||||
|
||||
int ipfs_ping (int argc, char **argv)
|
||||
{
|
||||
char* results = NULL;
|
||||
size_t results_size = 0;
|
||||
//TODO: handle multiaddress
|
||||
|
||||
// the way using multistream
|
||||
//TODO: Error checking
|
||||
char* ip = argv[2];
|
||||
int port = atoi(argv[3]);
|
||||
int socket_fd = libp2p_net_multistream_connect(ip, port);
|
||||
if (socket_fd < 0) {
|
||||
fprintf(stderr, "Unable to connect to %s on port %s", ip, argv[3]);
|
||||
}
|
||||
|
||||
// prepare the PING message
|
||||
struct Libp2pMessage* msg = libp2p_message_new();
|
||||
msg->message_type = MESSAGE_TYPE_PING;
|
||||
|
||||
size_t protobuf_size = libp2p_message_protobuf_encode_size(msg);
|
||||
unsigned char protobuf[protobuf_size];
|
||||
libp2p_message_protobuf_encode(msg, &protobuf[0], protobuf_size, &protobuf_size);
|
||||
libp2p_net_multistream_send(socket_fd, protobuf, protobuf_size);
|
||||
libp2p_net_multistream_receive(socket_fd, &results, &results_size);
|
||||
|
||||
if (results_size != protobuf_size) {
|
||||
fprintf(stderr, "PING unsuccessful. Original size: %lu, returned size: %lu\n", protobuf_size, results_size);
|
||||
return 0;
|
||||
}
|
||||
if (memcmp(results, protobuf, protobuf_size) != 0) {
|
||||
fprintf(stderr, "PING unsuccessful. Results do not match.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (msg != NULL)
|
||||
libp2p_message_free(msg);
|
||||
|
||||
fprintf(stdout, "Ping of %s:%s successful.\n", ip, argv[3]);
|
||||
|
||||
return 0;
|
||||
|
||||
// the old way
|
||||
/*
|
||||
int socketfd, i, count=10, tcount = 0;
|
||||
uint32_t ipv4;
|
||||
uint16_t port;
|
||||
|
@ -60,4 +103,5 @@ int ipfs_ping (int argc, char **argv)
|
|||
fprintf(stderr, "Average latency: %.2fms\n", total / tcount);
|
||||
|
||||
return 0;
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue