]> git.meshlink.io Git - meshlink/commitdiff
start to implement tinc_start
authorSaverio Proto <zioproto@gmail.com>
Mon, 7 Apr 2014 08:49:50 +0000 (10:49 +0200)
committerSaverio Proto <zioproto@gmail.com>
Mon, 7 Apr 2014 15:56:06 +0000 (17:56 +0200)
src/libmeshlink.c
src/libmeshlink.h
src/net_setup.c

index 5cf366c539945df2829247b31ecc2d92c8647419..f4932cc38aebd6b539ea88da4143381dcdb7dd86 100644 (file)
 */
 
 #include "libmeshlink.h"
+#include LZO1X_H
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
 #include "crypto.h"
 #include "ecdsagen.h"
 char *hosts_dir = NULL;
@@ -25,6 +29,50 @@ 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
+
+/*
+  initialize network
+*/
+bool setup_meshlink_network(void) {
+       init_connections();
+       init_subnets();
+       init_nodes();
+       init_edges();
+       init_requests();
+
+       if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
+               if(pinginterval < 1) {
+                       pinginterval = 86400;
+               }
+       } else
+               pinginterval = 60;
+
+       if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
+               pingtimeout = 5;
+       if(pingtimeout < 1 || pingtimeout > pinginterval)
+               pingtimeout = pinginterval;
+
+       //TODO: check if this makes sense in libmeshlink
+       if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
+               maxoutbufsize = 10 * MTU;
+
+       if(!setup_myself())
+               return false;
+
+       if(!init_control())
+               return false;
+
+       /* Run subnet-up scripts for our own subnets */
+
+       subnet_update(myself, NULL, true);
+
+       return true;
+}
+
 /* 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. */
@@ -398,8 +446,103 @@ return true;
 
 bool 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;
-printf("Hello World %s\n",confbase);
+
+       openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
+
+       init_configuration(&config_tree);
+
+       /* Slllluuuuuuurrrrp! */
+
+       gettimeofday(&now, NULL);
+       srand(now.tv_sec + now.tv_usec);
+       crypto_init();
+
+       if(!read_server_config())
+               return false;
+
+#ifdef HAVE_LZO
+       if(lzo_init() != LZO_E_OK) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "Error initializing LZO compressor!");
+               return false;
+       }
+#endif
+
+       //char *priority = NULL; //shoud be not needed in libmeshlink
+
+#ifdef HAVE_MLOCKALL
+       /* Lock all pages into memory if requested.
+        * This has to be done after daemon()/fork() so it works for child.
+        * No need to do that in parent as it's very short-lived. */
+       if(do_mlock && mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "mlockall",
+                  strerror(errno));
+               return 1;
+       }
+#endif
+
+       /* Setup sockets and open device. */
+
+       if(!setup_meshlink_network())
+               goto end;
+
+       /* Change process priority */
+       //should be not needed in libmeshlink
+       //if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
+       //      if(!strcasecmp(priority, "Normal")) {
+       //              if (setpriority(NORMAL_PRIORITY_CLASS) != 0) {
+       //                      logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno));
+       //                      goto end;
+       //              }
+       //      } else if(!strcasecmp(priority, "Low")) {
+       //              if (setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) {
+       //                             logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno));
+       //                      goto end;
+       //              }
+       //      } else if(!strcasecmp(priority, "High")) {
+       //              if (setpriority(HIGH_PRIORITY_CLASS) != 0) {
+       //                      logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno));
+       //                      goto end;
+       //              }
+       //      } else {
+       //              logger(DEBUG_ALWAYS, LOG_ERR, "Invalid priority `%s`!", priority);
+       //              goto end;
+       //      }
+       //}
+
+       /* drop privileges */
+       //if (!drop_privs())
+       //      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);
+
+       crypto_exit();
+
+       exit_configuration(&config_tree);
+       free(cmdline_conf);
+       free_names();
+
+       return status;
 
 }
 
index c5e84462fba366d57b94db15023c50ec1ff996f9..2541b5f2888ea2242c863769aa2a4b3851971bcf 100644 (file)
@@ -22,6 +22,7 @@
 #include "names.h"
 //#include "tincctl.h"
 #include "xalloc.h"
+#include "logger.h"
 
 extern char *hosts_dir;
 extern FILE *fopenmask(const char *filename, const char *mode, mode_t perms);
index fdd48348d43d861c5097c2d2943e8c7073f4f2f0..4574fbce4ab358ccb822bc71bd67b87043737050 100644 (file)
@@ -723,7 +723,7 @@ static bool add_listen_address(char *address, bool bindto) {
 /*
   Configure node_t myself and set up the local sockets (listen only)
 */
-static bool setup_myself(void) {
+bool setup_myself(void) {
        char *name, *hostname, *cipher, *digest, *type;
        char *address = NULL;
        bool port_specified = false;