2017-02-10 01:10:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "libp2p/net/p2pnet.h"
|
2017-02-22 16:48:42 +00:00
|
|
|
#include "libp2p/peer/peerstore.h"
|
2017-02-10 01:10:21 +00:00
|
|
|
#include "ipfs/core/daemon.h"
|
2017-02-22 15:56:11 +00:00
|
|
|
#include "ipfs/core/ipfs_node.h"
|
2017-03-09 17:50:08 +00:00
|
|
|
#include "ipfs/core/bootstrap.h"
|
2017-02-22 16:48:42 +00:00
|
|
|
#include "ipfs/repo/fsrepo/fs_repo.h"
|
2017-04-06 22:46:40 +00:00
|
|
|
#include "ipfs/repo/init.h"
|
2017-04-04 01:54:03 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-02-27 17:27:40 +00:00
|
|
|
int ipfs_daemon_start(char* repo_path) {
|
2017-04-17 16:58:47 +00:00
|
|
|
int count_pths = 0, retVal = 0;
|
2017-02-10 01:10:21 +00:00
|
|
|
pthread_t work_pths[MAX];
|
2017-02-22 15:56:11 +00:00
|
|
|
struct IpfsNodeListenParams listen_param;
|
2017-04-17 16:58:47 +00:00
|
|
|
struct MultiAddress* ma = NULL;
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-04-04 01:54:03 +00:00
|
|
|
libp2p_logger_info("daemon", "Initializing daemon...\n");
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
struct IpfsNode* local_node = NULL;
|
|
|
|
if (!ipfs_node_online_new(repo_path, &local_node))
|
|
|
|
goto exit;
|
2017-02-22 15:56:11 +00:00
|
|
|
|
2017-02-10 01:10:21 +00:00
|
|
|
// Set null router param
|
2017-04-20 22:56:03 +00:00
|
|
|
ma = multiaddress_new_from_string(local_node->repo->config->addresses->swarm_head->item);
|
2017-04-06 22:46:40 +00:00
|
|
|
listen_param.port = multiaddress_get_ip_port(ma);
|
2017-02-10 01:10:21 +00:00
|
|
|
listen_param.ipv4 = 0; // ip 0.0.0.0, all interfaces
|
2017-04-20 22:56:03 +00:00
|
|
|
listen_param.local_node = local_node;
|
2017-02-10 01:10:21 +00:00
|
|
|
|
2017-03-09 23:03:21 +00:00
|
|
|
// Create pthread for swarm listener.
|
2017-02-10 01:10:21 +00:00
|
|
|
if (pthread_create(&work_pths[count_pths++], NULL, ipfs_null_listen, &listen_param)) {
|
2017-04-04 01:54:03 +00:00
|
|
|
libp2p_logger_error("daemon", "Error creating thread for ipfs null listen\n");
|
2017-04-17 16:58:47 +00:00
|
|
|
goto exit;
|
2017-02-10 01:10:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:03 +00:00
|
|
|
//ipfs_bootstrap_routing(local_node);
|
2017-03-09 17:50:08 +00:00
|
|
|
|
2017-04-04 01:54:03 +00:00
|
|
|
libp2p_logger_info("daemon", "Daemon is ready\n");
|
2017-02-10 01:10:21 +00:00
|
|
|
|
|
|
|
// Wait for pthreads to finish.
|
|
|
|
while (count_pths) {
|
|
|
|
if (pthread_join(work_pths[--count_pths], NULL)) {
|
2017-04-04 01:54:03 +00:00
|
|
|
libp2p_logger_error("daemon", "Error joining thread\n");
|
2017-04-17 16:58:47 +00:00
|
|
|
goto exit;
|
2017-02-10 01:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
retVal = 1;
|
|
|
|
exit:
|
2017-04-20 22:56:03 +00:00
|
|
|
libp2p_logger_debug("daemon", "Cleaning up daemon processes\n");
|
2017-04-17 16:58:47 +00:00
|
|
|
// clean up
|
|
|
|
if (ma != NULL)
|
|
|
|
multiaddress_free(ma);
|
2017-04-20 22:56:03 +00:00
|
|
|
if (local_node != NULL) {
|
|
|
|
ipfs_node_free(local_node);
|
2017-04-17 16:58:47 +00:00
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
|
|
|
|
}
|
2017-02-27 17:27:40 +00:00
|
|
|
|
2017-04-17 16:58:47 +00:00
|
|
|
int ipfs_daemon_stop() {
|
|
|
|
return ipfs_null_shutdown();
|
2017-02-27 17:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ipfs_daemon (int argc, char **argv)
|
|
|
|
{
|
2017-04-06 22:46:40 +00:00
|
|
|
char* repo_path = NULL;
|
|
|
|
|
|
|
|
if (!ipfs_repo_get_directory(argc, argv, &repo_path)) {
|
|
|
|
fprintf(stderr, "Unable to open repo: %s\n", repo_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipfs_daemon_start(repo_path);
|
2017-02-10 01:10:21 +00:00
|
|
|
}
|