diff --git a/core/Makefile b/core/Makefile index 231b6c9..a9548c1 100644 --- a/core/Makefile +++ b/core/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-protobuf -Wall LFLAGS = DEPS = builder.h ipfs_node.h -OBJS = builder.o daemon.o null.o +OBJS = builder.o daemon.o null.o ping.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/core/null.c b/core/null.c index 6aa91cf..dcf1347 100644 --- a/core/null.c +++ b/core/null.c @@ -1,20 +1,39 @@ #include #include #include +#include #include #include #include "libp2p/net/p2pnet.h" #include "ipfs/core/daemon.h" +#define BUF_SIZE 4096 + void *ipfs_null_connection (void *ptr) { struct null_connection_params *connection_param; + char b[BUF_SIZE]; + int len; connection_param = (struct null_connection_params*) ptr; // TODO: multistream + secio + message. fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count)); + for(;;) { + len = socket_read(connection_param->socket, b, sizeof(b)-1, 0); + if (len > 0) { + while (b[len-1] == '\r' || b[len-1] == '\n') len--; + b[len] = '\0'; + fprintf(stderr, "Recv: '%s'\n", b); + if (strcmp (b, "ping") == 0) { + socket_write(connection_param->socket, "pong", 4, 0); + } + } else if(len < 0) { + break; + } + } + close (connection_param->socket); // close socket. (*(connection_param->count))--; // update counter. free (connection_param); diff --git a/core/ping.c b/core/ping.c new file mode 100644 index 0000000..b915e27 --- /dev/null +++ b/core/ping.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +#include "libp2p/net/p2pnet.h" + +#define BUF_SIZE 4096 + +int ipfs_ping (int argc, char **argv) +{ + int socketfd, i, count=10, tcount = 0; + uint32_t ipv4; + uint16_t port; + char b[BUF_SIZE]; + size_t len; + struct timeval time; + long cur_time, old_time; + double ms, total = 0; + + if (inet_pton (AF_INET, argv[2], &ipv4) == 0) { + fprintf(stderr, "Unable to use '%s' as an IP address.\n", argv[1]); + return 1; + } + + if ((port = atoi(argv[3])) == 0) { + fprintf(stderr, "Unable to use '%s' port.\n", argv[2]); + return 1; + } + + if ((socketfd = socket_tcp4()) <= 0) { + perror("can't create socket"); + return 1; + } + + if (socket_connect4(socketfd, ipv4, port) < 0) { + perror("fail to connect"); + return 1; + } + + fprintf(stderr, "PING %s.\n", argv[2]); + + for (i=0 ; i < count ; i++) { + if (gettimeofday (&time, 0)) return -1; + old_time = 1000000 * time.tv_sec + time.tv_usec; + + socket_write(socketfd, "ping", 4, 0); + len = socket_read(socketfd, b, sizeof(b), 0); + + if (len == 4 && memcmp(b, "pong", 4) == 0) { + if (gettimeofday (&time, 0)) return -1; + cur_time = 1000000 * time.tv_sec + time.tv_usec; + ms = (cur_time - old_time) / 1000.0; + total += ms; tcount++; + fprintf(stderr, "Pong received: time=%.2f ms\n", ms); + } + sleep (1); + } + fprintf(stderr, "Average latency: %.2fms\n", total / tcount); + + return 0; +} diff --git a/include/ipfs/core/daemon.h b/include/ipfs/core/daemon.h index e6b7717..b471865 100644 --- a/include/ipfs/core/daemon.h +++ b/include/ipfs/core/daemon.h @@ -18,4 +18,5 @@ void *ipfs_null_connection (void *ptr); void *ipfs_null_listen (void *ptr); int ipfs_daemon (int argc, char **argv); + int ipfs_ping (int argc, char **argv); #endif // DAEMON_H diff --git a/main/Makefile b/main/Makefile index da43dfe..e5c1107 100644 --- a/main/Makefile +++ b/main/Makefile @@ -7,7 +7,7 @@ OBJS = main.o \ ../cid/cid.o \ ../cmd/ipfs/init.o \ ../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \ - ../core/builder.o ../core/daemon.o ../core/null.o \ + ../core/builder.o ../core/daemon.o ../core/null.o ../core/ping.o \ ../datastore/ds_helper.o \ ../datastore/key.o \ ../dnslink/dnslink.o \ diff --git a/main/main.c b/main/main.c index 780f81f..24e32b0 100644 --- a/main/main.c +++ b/main/main.c @@ -5,6 +5,7 @@ #include "ipfs/importer/importer.h" #include "ipfs/importer/exporter.h" #include "ipfs/dnslink/dnslink.h" +#include "ipfs/core/daemon.h" #ifdef __MINGW32__ void bzero(void *s, size_t n) @@ -55,6 +56,7 @@ void strip_quotes(int argc, char** argv) { #define DNS 4 #define CAT 5 #define DAEMON 6 +#define PING 7 /*** * Basic parsing of command line arguments to figure out where the user wants to go @@ -82,6 +84,9 @@ int parse_arguments(int argc, char** argv) { if (strcmp("daemon", argv[1]) == 0) { return DAEMON; } + if (strcmp("ping", argv[1]) == 0) { + return PING; + } return -1; } @@ -110,5 +115,8 @@ int main(int argc, char** argv) { case (DAEMON): ipfs_daemon(argc, argv); break; + case (PING): + ipfs_ping(argc, argv); + break; } }