diff --git a/core/Makefile b/core/Makefile index 94a443b..231b6c9 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 +OBJS = builder.o daemon.o null.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/core/daemon.c b/core/daemon.c new file mode 100644 index 0000000..8ef3936 --- /dev/null +++ b/core/daemon.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include "libp2p/net/p2pnet.h" +#include "ipfs/core/daemon.h" + +int ipfs_daemon (int argc, char **argv) +{ + int count_pths = 0; + pthread_t work_pths[MAX]; + struct null_listen_params listen_param; + + fprintf(stderr, "Initializing daemon...\n"); + + // Set null router param + listen_param.ipv4 = 0; // ip 0.0.0.0, all interfaces + listen_param.port = 4001; + + // Create pthread for ipfs_null_listen. + if (pthread_create(&work_pths[count_pths++], NULL, ipfs_null_listen, &listen_param)) { + fprintf(stderr, "Error creating thread for ipfs_null_listen\n"); + return 1; + } + + fprintf(stderr, "Daemon is ready\n"); + + // Wait for pthreads to finish. + while (count_pths) { + if (pthread_join(work_pths[--count_pths], NULL)) { + fprintf(stderr, "Error joining thread\n"); + return 2; + } + } + + // All pthreads aborted? + return 0; +} diff --git a/core/null.c b/core/null.c new file mode 100644 index 0000000..6aa91cf --- /dev/null +++ b/core/null.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include "libp2p/net/p2pnet.h" +#include "ipfs/core/daemon.h" + +void *ipfs_null_connection (void *ptr) +{ + struct null_connection_params *connection_param; + + connection_param = (struct null_connection_params*) ptr; + + // TODO: multistream + secio + message. + fprintf(stderr, "Connection %d, count %d\n", connection_param->socket, *(connection_param->count)); + + close (connection_param->socket); // close socket. + (*(connection_param->count))--; // update counter. + free (connection_param); + return (void*) 1; +} + +void *ipfs_null_listen (void *ptr) +{ + int socketfd, s, count = 0; + pthread_t pth_connection; + struct null_listen_params *listen_param; + struct null_connection_params *connection_param; + + listen_param = (struct null_listen_params*) ptr; + + if ((socketfd = socket_listen(socket_tcp4(), &(listen_param->ipv4), &(listen_param->port))) <= 0) { + perror("fail to init null router."); + exit (1); + } + + fprintf(stderr, "Null listening on %d\n", listen_param->port); + + for (;;) { + s = socket_accept4(socketfd, &(listen_param->ipv4), &(listen_param->port)); + if (count >= CONNECTIONS) { // limit reached. + close (s); + continue; + } + + count++; + connection_param = malloc (sizeof (struct null_connection_params)); + if (connection_param) { + connection_param->socket = s; + connection_param->count = &count; + // Create pthread for ipfs_null_connection. + if (pthread_create(&pth_connection, NULL, ipfs_null_connection, connection_param)) { + fprintf(stderr, "Error creating thread for connection %d\n", count); + close (s); + } else { + pthread_detach (pth_connection); + } + } + } + + return (void*) 2; +} diff --git a/include/ipfs/core/daemon.h b/include/ipfs/core/daemon.h new file mode 100644 index 0000000..e6b7717 --- /dev/null +++ b/include/ipfs/core/daemon.h @@ -0,0 +1,21 @@ +#ifndef DAEMON_H + #define DAEMON_H + #include + + #define MAX 5 + #define CONNECTIONS 50 + + struct null_connection_params { + int socket; + int *count; + }; + + struct null_listen_params { + uint32_t ipv4; + uint16_t port; + }; + + void *ipfs_null_connection (void *ptr); + void *ipfs_null_listen (void *ptr); + int ipfs_daemon (int argc, char **argv); +#endif // DAEMON_H diff --git a/main/Makefile b/main/Makefile index 3e7a37d..da43dfe 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/builder.o ../core/daemon.o ../core/null.o \ ../datastore/ds_helper.o \ ../datastore/key.o \ ../dnslink/dnslink.o \ diff --git a/main/main.c b/main/main.c index b0c1a63..780f81f 100644 --- a/main/main.c +++ b/main/main.c @@ -54,6 +54,7 @@ void strip_quotes(int argc, char** argv) { #define OBJECT_GET 3 #define DNS 4 #define CAT 5 +#define DAEMON 6 /*** * Basic parsing of command line arguments to figure out where the user wants to go @@ -78,6 +79,9 @@ int parse_arguments(int argc, char** argv) { if (strcmp("dns", argv[1]) == 0) { return DNS; } + if (strcmp("daemon", argv[1]) == 0) { + return DAEMON; + } return -1; } @@ -103,5 +107,8 @@ int main(int argc, char** argv) { case (DNS): ipfs_dns(argc, argv); break; + case (DAEMON): + ipfs_daemon(argc, argv); + break; } }