51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <sys/un.h>
-
#include "system.h"
#include "conf.h"
#include "control.h"
#include "control_common.h"
#include "graph.h"
#include "logger.h"
+#include "utils.h"
#include "xalloc.h"
static int control_socket = -1;
bool init_control() {
int result;
+
+#ifdef HAVE_MINGW
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof addr);
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(0x7f000001);
+ addr.sin_port = htons(55555);
+
+ control_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+#else
struct sockaddr_un addr;
char *lastslash;
logger(LOG_ERR, "Control socket directory ownership/permissions insecure.");
goto bail;
}
+#endif
result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
- if(result < 0 && errno == EADDRINUSE) {
+ if(result < 0 && sockinuse(sockerrno)) {
result = connect(control_socket, (struct sockaddr *)&addr, sizeof addr);
+#ifndef HAVE_MINGW
if(result < 0) {
logger(LOG_WARNING, "Removing old control socket.");
unlink(controlsocketname);
result = bind(control_socket, (struct sockaddr *)&addr, sizeof addr);
- } else {
+ } else
+#endif
+ {
if(netname)
logger(LOG_ERR, "Another tincd is already running for net `%s'.", netname);
else
bail:
if(control_socket != -1) {
- close(control_socket);
+ closesocket(control_socket);
control_socket = -1;
}
return false;
void exit_control() {
event_del(&control_event);
- close(control_socket);
+ closesocket(control_socket);
unlink(controlsocketname);
}
timeout_set(&timeout_event, timeout_handler, &timeout_event);
event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
+
+#ifdef SIGHUP
signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
signal_add(&sighup_event, NULL);
+#endif
+#ifdef SIGTERM
signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
signal_add(&sigterm_event, NULL);
+#endif
+#ifdef SIGQUIT
signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
signal_add(&sigquit_event, NULL);
+#endif
if(event_loop(0) < 0) {
logger(LOG_ERR, "Error while waiting for input: %s", strerror(errno));
#include "system.h"
-#include <sys/un.h>
#include <getopt.h>
#include "xalloc.h"
#include "protocol.h"
#include "control_common.h"
#include "rsagen.h"
+#include "utils.h"
/* The name this program was run with. */
char *program_name = NULL;
char *netname = NULL;
char *confbase = NULL;
+#ifdef HAVE_MINGW
+static struct WSAData wsa_state;
+#endif
+
static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'},
{"net", required_argument, NULL, 'n'},
#ifdef HAVE_MINGW
if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
- if(!logfilename)
- xasprintf(&logfilename, "%s/log/%s.log", identname);
if(!confbase) {
if(netname)
xasprintf(&confbase, "%s/%s", installdir, netname);
else if(rv == -1)
return rv;
else if(rv == 0) {
+#ifdef HAVE_MINGW
+ errno = 0;
+#else
errno = ENODATA;
+#endif
return -1;
}
len += rv;
size_t *indatalen_p) {
tinc_ctl_request_t req;
int rv;
+ void *indata;
+
+ memset(&req, 0, sizeof req);
+ req.length = sizeof req + outdatalen;
+ req.type = type;
+ req.res_errno = 0;
+
+#ifdef HAVE_MINGW
+ if(send(fd, (void *)&req, sizeof req, 0) != sizeof req || send(fd, outdata, outdatalen, 0) != outdatalen)
+ return -1;
+#else
struct iovec vector[2] = {
{&req, sizeof req},
{(void*) outdata, outdatalen}
};
- void *indata;
if(res_errno_p == NULL)
return -1;
- memset(&req, 0, sizeof req);
- req.length = sizeof req + outdatalen;
- req.type = type;
- req.res_errno = 0;
-
while((rv = writev(fd, vector, 2)) == -1 && errno == EINTR) ;
if(rv != req.length)
return -1;
+#endif
if(fullread(fd, &req, sizeof req) == -1)
return -1;
}
int main(int argc, char *argv[], char *envp[]) {
- struct sockaddr_un addr;
tinc_ctl_greeting_t greeting;
int fd;
int result;
* ancestors are writable only by trusted users, which we don't verify.
*/
+#ifdef HAVE_MINGW
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return 1;
+ }
+
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof addr);
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(0x7f000001);
+ addr.sin_port = htons(55555);
+
+ fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if(fd < 0) {
+ fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno));
+ return 1;
+ }
+
+ fprintf(stderr, "Got socket %d\n", fd);
+
+ unsigned long arg = 0;
+
+ if(ioctlsocket(fd, FIONBIO, &arg) != 0) {
+ fprintf(stderr, "ioctlsocket failed: %s", sockstrerror(sockerrno));
+ }
+#else
+ struct sockaddr_un addr;
struct stat statbuf;
char *lastslash = strrchr(controlsocketname, '/');
if(lastslash != NULL) {
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, controlsocketname, sizeof addr.sun_path - 1);
+#endif
if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
- fprintf(stderr, "Cannot connect to %s: %s\n", controlsocketname, strerror(errno));
+
+ fprintf(stderr, "Cannot connect to %s: %s\n", controlsocketname, sockstrerror(sockerrno));
return 1;
}
+ fprintf(stderr, "Connected!\n");
+
if(fullread(fd, &greeting, sizeof greeting) == -1) {
fprintf(stderr, "Cannot read greeting from control socket: %s\n",
- strerror(errno));
+ sockstrerror(sockerrno));
return 1;
}