Refactor crypto RNG; add getrandom() support
[tinc] / src / tincctl.c
index c700f94..fe00912 100644 (file)
@@ -40,6 +40,7 @@
 #include "version.h"
 #include "subnet.h"
 #include "keys.h"
+#include "random.h"
 
 #ifndef MSG_NOSIGNAL
 #define MSG_NOSIGNAL 0
@@ -644,7 +645,7 @@ static bool stop_tincd(void) {
                // wait for tincd to close the connection...
        }
 
-       close(fd);
+       closesocket(fd);
        pid = 0;
        fd = -1;
 
@@ -717,7 +718,7 @@ bool connect_tincd(bool verbose) {
 
                if(select(fd + 1, &r, NULL, NULL, &tv)) {
                        fprintf(stderr, "Previous connection to tincd lost, reconnecting.\n");
-                       close(fd);
+                       closesocket(fd);
                        fd = -1;
                } else {
                        return true;
@@ -784,7 +785,7 @@ bool connect_tincd(bool verbose) {
                        fprintf(stderr, "Cannot connect to UNIX socket %s: %s\n", unixsocketname, sockstrerror(sockerrno));
                }
 
-               close(fd);
+               closesocket(fd);
                fd = -1;
                return false;
        }
@@ -830,7 +831,7 @@ bool connect_tincd(bool verbose) {
                        fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno));
                }
 
-               close(fd);
+               closesocket(fd);
                fd = -1;
                return false;
        }
@@ -853,7 +854,7 @@ bool connect_tincd(bool verbose) {
                        fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno));
                }
 
-               close(fd);
+               closesocket(fd);
                fd = -1;
                return false;
        }
@@ -863,7 +864,7 @@ bool connect_tincd(bool verbose) {
                        fprintf(stderr, "Could not fully establish control socket connection\n");
                }
 
-               close(fd);
+               closesocket(fd);
                fd = -1;
                return false;
        }
@@ -1528,7 +1529,7 @@ static int cmd_log(int argc, char *argv[]) {
        signal(SIGINT, SIG_DFL);
 #endif
 
-       close(fd);
+       closesocket(fd);
        fd = -1;
        return 0;
 }
@@ -2623,7 +2624,7 @@ static int switch_network(char *name) {
        }
 
        if(fd >= 0) {
-               close(fd);
+               closesocket(fd);
                fd = -1;
        }
 
@@ -3266,6 +3267,22 @@ static void cleanup(void) {
        free_names();
 }
 
+static int run_command(int argc, char *argv[]) {
+       if(optind >= argc) {
+               return cmd_shell(argc, argv);
+       }
+
+       for(int i = 0; commands[i].command; i++) {
+               if(!strcasecmp(argv[optind], commands[i].command)) {
+                       return commands[i].function(argc - optind, argv + optind);
+               }
+       }
+
+       fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);
+       usage(true);
+       return 1;
+}
+
 int main(int argc, char *argv[]) {
        program_name = argv[0];
        orig_argv = argv;
@@ -3301,20 +3318,13 @@ int main(int argc, char *argv[]) {
 #endif
 
        gettimeofday(&now, NULL);
+       random_init();
        crypto_init();
        prng_init();
 
-       if(optind >= argc) {
-               return cmd_shell(argc, argv);
-       }
+       int result = run_command(argc, argv);
 
-       for(int i = 0; commands[i].command; i++) {
-               if(!strcasecmp(argv[optind], commands[i].command)) {
-                       return commands[i].function(argc - optind, argv + optind);
-               }
-       }
+       random_exit();
 
-       fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);
-       usage(true);
-       return 1;
+       return result;
 }