From f8e42867405c18aebeb1ad9cf9d0387bff7f40a8 Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 22 Feb 2017 10:56:11 -0500 Subject: [PATCH] Starting to make the daemon more intelligent --- core/daemon.c | 11 ++++++++++- core/net.c | 37 +++++++++++++++++++++++++++++++++++ core/null.c | 4 ++-- include/ipfs/core/daemon.h | 6 ++++++ include/ipfs/core/ipfs_node.h | 11 ++++++----- include/ipfs/core/net.h | 35 +++++++++++++++++++++++++++++++++ namesys/namesys.c | 2 ++ 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 core/net.c create mode 100644 include/ipfs/core/net.h diff --git a/core/daemon.c b/core/daemon.c index 8ef3936..86ba2bb 100644 --- a/core/daemon.c +++ b/core/daemon.c @@ -5,18 +5,27 @@ #include #include "libp2p/net/p2pnet.h" #include "ipfs/core/daemon.h" +#include "ipfs/core/ipfs_node.h" int ipfs_daemon (int argc, char **argv) { int count_pths = 0; pthread_t work_pths[MAX]; - struct null_listen_params listen_param; + struct IpfsNodeListenParams listen_param; fprintf(stderr, "Initializing daemon...\n"); + // create a new IpfsNode + struct IpfsNode local_node; + local_node.mode = MODE_ONLINE; + //local_node.peerstore = ipfs_peerstore_new(); + //local_node.repo = fsrepo; + //local_node.peer_id = peer_id; + // Set null router param listen_param.ipv4 = 0; // ip 0.0.0.0, all interfaces listen_param.port = 4001; + listen_param.local_node = &local_node; // Create pthread for ipfs_null_listen. if (pthread_create(&work_pths[count_pths++], NULL, ipfs_null_listen, &listen_param)) { diff --git a/core/net.c b/core/net.c new file mode 100644 index 0000000..1ff638d --- /dev/null +++ b/core/net.c @@ -0,0 +1,37 @@ + +#include "ipfs/core/net.h" + +/** + * Do a socket accept + * @param listener the listener + * @returns true(1) on success, false(0) otherwise + */ +int ipfs_core_net_accept(struct IpfsListener listener) { + //TODO: Implement this + return 0; +} + +/** + * Listen using a particular protocol + * @param node the node + * @param protocol the protocol to use + * @param listener the results + * @returns true(1) on success, false(0) otherwise + */ +int ipfs_core_net_listen(struct IpfsNode* node, char* protocol, struct IpfsListener* listener){ + // TODO: Implement this + return 0; +} + +/*** + * Dial a peer + * @param node this node + * @param peer_id who to dial + * @param protocol the protocol to use + * @param stream the resultant stream + * @returns true(1) on success, otherwise false(0) + */ +int ipsf_core_net_dial(struct IpfsNode* node, char* peer_id, char* protocol, struct Stream* stream) { + //TODO: Implement this + return 0; +} diff --git a/core/null.c b/core/null.c index dcf1347..2864304 100644 --- a/core/null.c +++ b/core/null.c @@ -44,10 +44,10 @@ void *ipfs_null_listen (void *ptr) { int socketfd, s, count = 0; pthread_t pth_connection; - struct null_listen_params *listen_param; + struct IpfsNodeListenParams *listen_param; struct null_connection_params *connection_param; - listen_param = (struct null_listen_params*) ptr; + listen_param = (struct IpfsNodeListenParams*) ptr; if ((socketfd = socket_listen(socket_tcp4(), &(listen_param->ipv4), &(listen_param->port))) <= 0) { perror("fail to init null router."); diff --git a/include/ipfs/core/daemon.h b/include/ipfs/core/daemon.h index b471865..8a32b9d 100644 --- a/include/ipfs/core/daemon.h +++ b/include/ipfs/core/daemon.h @@ -15,6 +15,12 @@ uint16_t port; }; + struct IpfsNodeListenParams { + uint32_t ipv4; + uint16_t port; + struct IpfsNode* local_node; + }; + void *ipfs_null_connection (void *ptr); void *ipfs_null_listen (void *ptr); int ipfs_daemon (int argc, char **argv); diff --git a/include/ipfs/core/ipfs_node.h b/include/ipfs/core/ipfs_node.h index d88fe6a..dd9cfca 100644 --- a/include/ipfs/core/ipfs_node.h +++ b/include/ipfs/core/ipfs_node.h @@ -1,13 +1,14 @@ -#ifndef __CORE_IPFS_NODE_H__ -#define __CORE_IPFS_NODE_H__ +#pragma once + +enum NodeMode { MODE_OFFLINE, MODE_ONLINE }; struct IpfsNode { + enum NodeMode mode; //struct PeerId identity; - //struct Repo repo; + struct FSRepo* repo; + struct Peerstore* peerstore; //struct Pinner pinning; // an interface //struct Mount** mounts; //struct PrivKey* private_key; // TODO: Add more here }; - -#endif /* ipfs_node_h */ diff --git a/include/ipfs/core/net.h b/include/ipfs/core/net.h new file mode 100644 index 0000000..387f39a --- /dev/null +++ b/include/ipfs/core/net.h @@ -0,0 +1,35 @@ +#pragma once + +#include "libp2p/net/stream.h" + +struct IpfsListener { + char* conCh; + char* protocol; +}; + +/** + * Do a socket accept + * @param listener the listener + * @param stream the returned stream + * @returns true(1) on success, false(0) otherwise + */ +int ipfs_core_net_accept(struct IpfsListener* listener, struct Stream* stream); + +/** + * Listen using a particular protocol + * @param node the node + * @param protocol the protocol to use + * @param listener the results + * @returns true(1) on success, false(0) otherwise + */ +int ipfs_core_net_listen(struct IpfsNode* node, char* protocol, struct IpfsListener* listener); + +/*** + * Dial a peer + * @param node this node + * @param peer_id who to dial + * @param protocol the protocol to use + * @param stream the resultant stream + * @returns true(1) on success, otherwise false(0) + */ +int ipsf_core_net_dial(struct IpfsNode* node, char* peer_id, char* protocol, struct Stream* stream); diff --git a/namesys/namesys.c b/namesys/namesys.c index 6e2acdf..f2468cb 100644 --- a/namesys/namesys.c +++ b/namesys/namesys.c @@ -1,6 +1,8 @@ #include #include +#ifndef __USE_ISOC11 #define __USE_ISOC11 +#endif #include #include "ipfs/cid/cid.h" #include "ipfs/path/path.h"