Use usleep() instead of sleep(), MinGW complained.
[tinc] / src / control.c
index 277f7cb..7f46a70 100644 (file)
@@ -26,6 +26,7 @@
 #include "logger.h"
 #include "meta.h"
 #include "net.h"
+#include "netutl.h"
 #include "protocol.h"
 #include "route.h"
 #include "splay_tree.h"
@@ -33,7 +34,7 @@
 #include "xalloc.h"
 
 char controlcookie[65];
-extern char *controlcookiename;
+extern char *pidfilename;
 
 static bool control_return(connection_t *c, int type, int error) {
        return send_request(c, "%d %d %d", CONTROL, type, error);
@@ -133,26 +134,37 @@ bool control_h(connection_t *c, char *request) {
 bool init_control(void) {
        randomize(controlcookie, sizeof controlcookie / 2);
        bin2hex(controlcookie, controlcookie, sizeof controlcookie / 2);
-       controlcookie[sizeof controlcookie - 1] = 0;
 
-       FILE *f = fopen(controlcookiename, "w");
+       FILE *f = fopen(pidfilename, "w");
        if(!f) {
-               logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", controlcookiename, strerror(errno));
+               logger(LOG_ERR, "Cannot write control socket cookie file %s: %s", pidfilename, strerror(errno));
                return false;
        }
 
 #ifdef HAVE_FCHMOD
        fchmod(fileno(f), 0600);
 #else
-       chmod(controlcookiename, 0600);
+       chmod(pidfilename, 0600);
 #endif
+       // Get the address and port of the first listening socket
 
-       fprintf(f, "%s %s %d\n", controlcookie, myport, getpid());
+       char *localhost = NULL;
+       sockaddr_t sa;
+       socklen_t len = sizeof sa;
+
+       if(getsockname(listen_socket[0].tcp, (struct sockaddr *)&sa, &len))
+               xasprintf(&localhost, "127.0.0.1 port %d", myport);
+       else
+               localhost = sockaddr2hostname(&sa);
+
+       fprintf(f, "%d %s %s\n", (int)getpid(), controlcookie, localhost);
+
+       free(localhost);
        fclose(f);
 
        return true;
 }
 
 void exit_control(void) {
-       unlink(controlcookiename);
+       unlink(pidfilename);
 }