/*
libmeshlink.h -- Tincd Library
- Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org> Saverio Proto <zioproto@gmail.com>
+ Copyright (C) 2014 Guus Sliepen <guus@meshlink.io> Saverio Proto <zioproto@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "libmeshlink.h"
+#include "system.h"
+
+#include <pthread.h>
+
+#include "connection.h"
#include "crypto.h"
#include "ecdsagen.h"
+#include "edge.h"
+#include "libmeshlink.h"
+#include "net.h"
+#include "node.h"
+#include "protocol.h"
+
char *hosts_dir = NULL;
+recvdata_cb_t recv_callback;
static char *name = NULL;
char *tinc_conf = NULL;
static bool tty = false;
+#ifdef HAVE_MLOCKALL
+/* If nonzero, disable swapping for this process. */
+static bool do_mlock = false;
+#endif
+
/* Open a file with the desired permissions, minus the umask.
Also, if we want to create an executable file, we call fchmod()
to set the executable bits. */
return true;
}
-/*
- Generate a public/private RSA keypair, and ask for a file to store
- them in.
-*/
-bool rsa_keygen(int bits, bool ask) {
- rsa_t *key;
- FILE *f;
- char *pubname, *privname;
-
- fprintf(stderr, "Generating %d bits keys:\n", bits);
-
- if(!(key = rsa_generate(bits, 0x10001))) {
- fprintf(stderr, "Error during key generation!\n");
- return false;
- } else
- fprintf(stderr, "Done.\n");
-
- xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
- f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
- free(privname);
-
- if(!f)
- return false;
-
- if(!rsa_write_pem_private_key(key, f)) {
- fprintf(stderr, "Error writing private key!\n");
- fclose(f);
- rsa_free(key);
- return false;
- }
-
- fclose(f);
-
- if(name)
- xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
- else
- xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
-
- f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
- free(pubname);
-
- if(!f)
- return false;
-
- if(!rsa_write_pem_public_key(key, f)) {
- fprintf(stderr, "Error writing public key!\n");
- fclose(f);
- rsa_free(key);
- return false;
- }
-
- fclose(f);
- rsa_free(key);
-
- return true;
-}
-
static bool try_bind(int port) {
struct addrinfo *ai = NULL;
struct addrinfo hint = {
return true;
}
-int check_port(char *name) {
+int check_port(const char *name) {
if(try_bind(655))
return 655;
}
//tinc_setup() should basically do what cmd_init() from src/tincctl.c does, except it doesn't have to generate a tinc-up script.
bool tinc_setup(const char* confbaseapi, const char* name) {
- confbase = confbaseapi;
- make_names();
+ confbase = xstrdup(confbaseapi);
xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
if(!access(tinc_conf, F_OK)) {
return false;
}
- if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
- return false;
- }
-
if(mkdir(confbase, 0777) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
return false;
fprintf(f, "Name = %s\n", name);
fclose(f);
- if(!rsa_keygen(2048, false) || !ecdsa_keygen(false))
+ if(!ecdsa_keygen(false))
return false;
check_port(name);
bool tinc_start(const char* confbaseapi) {
pthread_t tincThread;
- confbase = confbaseapi;
- pthread_create(&tincThread,NULL,tinc_main_thread,confbaseapi);
+ confbase = xstrdup(confbaseapi);
+ pthread_create(&tincThread,NULL,tinc_main_thread,confbase);
pthread_detach(tincThread);
return true;
}
-bool tinc_main_thread(void * in) {
+__attribute__((constructor)) static void meshlink_init(void) {
+ /* Slllluuuuuuurrrrp! */
-confbase = (char*) in;
-printf("Hello World %s\n",confbase);
+ gettimeofday(&now, NULL);
+ srand(now.tv_sec + now.tv_usec);
+ crypto_init();
+}
+__attribute__((destructor)) static void meshlink_exit(void) {
+ crypto_exit();
+}
+
+
+void *tinc_main_thread(void * in) {
+ static bool status = false;
+ /* If nonzero, write log entries to a separate file. */
+ bool use_logfile = false;
+
+ confbase = (char*) in;
+
+ openlogger("tinc", LOGMODE_STDERR);
+
+ init_configuration(&config_tree);
+
+ if(!read_server_config())
+ return false;
+
+ //char *priority = NULL; //shoud be not needed in libmeshlink
+
+ /* Setup sockets and open device. */
+
+ if(!setup_network())
+ goto end;
+
+ /* Start main loop. It only exits when tinc is killed. */
+
+ logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
+
+ try_outgoing_connections();
+
+ status = main_loop();
+
+ /* Shutdown properly. */
+
+end:
+ close_network_connections();
+
+ logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
+
+ //free(priority);
+
+ exit_configuration(&config_tree);
+
+ return (void *)status;
}
bool tinc_stop();
// can be called from any thread
-bool tinc_send_packet(node_t *receiver, const char* buf, unsigned int len);
+bool tinc_send_packet(node_t *receiver, const char* buf, unsigned int len) {
+
+ vpn_packet_t packet;
+ tincpackethdr* hdr = (tincpackethdr *)packet.data;
+ if (sizeof(tincpackethdr) + len > MAXSIZE) {
+
+ //log something
+ return false;
+ }
+
+ packet.probe = false;
+ memset(hdr, 0, sizeof *hdr);
+ memcpy(hdr->destination,receiver->name,sizeof(hdr->destination));
+ memcpy(hdr->source,mesh->self->name,sizeof(hdr->source));
+
+ packet.len = sizeof(tincpackethdr) + len;
+ memcpy(packet.data+sizeof(tincpackethdr),buf,len);
+
+ mesh->self->in_packets++;
+ mesh->self->in_bytes += packet.len;
+ route(mesh->self, &packet);
+
+return true;
+}
// handler runs in tinc thread and should return immediately
bool tinc_set_packet_receive_handler(void (*handler)(const char* sender, const char* buf, unsigned int len));
//It might also be a good idea to add the option of looking up hosts by public
//key (fingerprints) instead of names.
-node_t *tinc_get_host(const char *name);
+node_t *tinc_get_host(const char *name) {
+
+
+
+};
bool tinc_get_hosts(node_t** hosts);
*/
bool tinc_blacklist(node_t *host);
-
+void recvdata_register_cb(recvdata_cb_t cb) {
+recv_callback = cb;
+};