Implemented initial core/ping.
A fake plaintext ping/pong implementation, for now it's just to use as a POC for multistream and secio.
This commit is contained in:
parent
de6c4b2495
commit
0522bedd2a
6 changed files with 93 additions and 2 deletions
|
@ -2,7 +2,7 @@ CC = gcc
|
||||||
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-protobuf -Wall
|
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-protobuf -Wall
|
||||||
LFLAGS =
|
LFLAGS =
|
||||||
DEPS = builder.h ipfs_node.h
|
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)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
19
core/null.c
19
core/null.c
|
@ -1,20 +1,39 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "libp2p/net/p2pnet.h"
|
#include "libp2p/net/p2pnet.h"
|
||||||
#include "ipfs/core/daemon.h"
|
#include "ipfs/core/daemon.h"
|
||||||
|
|
||||||
|
#define BUF_SIZE 4096
|
||||||
|
|
||||||
void *ipfs_null_connection (void *ptr)
|
void *ipfs_null_connection (void *ptr)
|
||||||
{
|
{
|
||||||
struct null_connection_params *connection_param;
|
struct null_connection_params *connection_param;
|
||||||
|
char b[BUF_SIZE];
|
||||||
|
int len;
|
||||||
|
|
||||||
connection_param = (struct null_connection_params*) ptr;
|
connection_param = (struct null_connection_params*) ptr;
|
||||||
|
|
||||||
// TODO: multistream + secio + message.
|
// TODO: multistream + secio + message.
|
||||||
fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count));
|
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.
|
close (connection_param->socket); // close socket.
|
||||||
(*(connection_param->count))--; // update counter.
|
(*(connection_param->count))--; // update counter.
|
||||||
free (connection_param);
|
free (connection_param);
|
||||||
|
|
63
core/ping.c
Normal file
63
core/ping.c
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#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"
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
|
@ -18,4 +18,5 @@
|
||||||
void *ipfs_null_connection (void *ptr);
|
void *ipfs_null_connection (void *ptr);
|
||||||
void *ipfs_null_listen (void *ptr);
|
void *ipfs_null_listen (void *ptr);
|
||||||
int ipfs_daemon (int argc, char **argv);
|
int ipfs_daemon (int argc, char **argv);
|
||||||
|
int ipfs_ping (int argc, char **argv);
|
||||||
#endif // DAEMON_H
|
#endif // DAEMON_H
|
||||||
|
|
|
@ -7,7 +7,7 @@ OBJS = main.o \
|
||||||
../cid/cid.o \
|
../cid/cid.o \
|
||||||
../cmd/ipfs/init.o \
|
../cmd/ipfs/init.o \
|
||||||
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.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/ds_helper.o \
|
||||||
../datastore/key.o \
|
../datastore/key.o \
|
||||||
../dnslink/dnslink.o \
|
../dnslink/dnslink.o \
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "ipfs/importer/importer.h"
|
#include "ipfs/importer/importer.h"
|
||||||
#include "ipfs/importer/exporter.h"
|
#include "ipfs/importer/exporter.h"
|
||||||
#include "ipfs/dnslink/dnslink.h"
|
#include "ipfs/dnslink/dnslink.h"
|
||||||
|
#include "ipfs/core/daemon.h"
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
void bzero(void *s, size_t n)
|
void bzero(void *s, size_t n)
|
||||||
|
@ -55,6 +56,7 @@ void strip_quotes(int argc, char** argv) {
|
||||||
#define DNS 4
|
#define DNS 4
|
||||||
#define CAT 5
|
#define CAT 5
|
||||||
#define DAEMON 6
|
#define DAEMON 6
|
||||||
|
#define PING 7
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Basic parsing of command line arguments to figure out where the user wants to go
|
* 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) {
|
if (strcmp("daemon", argv[1]) == 0) {
|
||||||
return DAEMON;
|
return DAEMON;
|
||||||
}
|
}
|
||||||
|
if (strcmp("ping", argv[1]) == 0) {
|
||||||
|
return PING;
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,5 +115,8 @@ int main(int argc, char** argv) {
|
||||||
case (DAEMON):
|
case (DAEMON):
|
||||||
ipfs_daemon(argc, argv);
|
ipfs_daemon(argc, argv);
|
||||||
break;
|
break;
|
||||||
|
case (PING):
|
||||||
|
ipfs_ping(argc, argv);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue