From a9230a241a1df0328fbfbe5d1c2ee3234bf808fd Mon Sep 17 00:00:00 2001 From: Niklas Hofmann Date: Sat, 25 Oct 2014 22:17:51 +0200 Subject: [PATCH] windows compatibility --- examples/Makefile.am | 10 +++++----- examples/manynodes.c | 19 +++++++++++++++++++ src/Makefile.am | 6 +++++- src/devtools.c | 10 +++++----- src/discovery.c | 21 ++++++++++----------- src/event.h | 1 + src/net.c | 2 ++ src/net_socket.c | 4 ++++ 8 files changed, 51 insertions(+), 22 deletions(-) diff --git a/examples/Makefile.am b/examples/Makefile.am index 8653adc6..816a00b0 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -3,16 +3,16 @@ bin_PROGRAMS = meshlinkapp chat chatpp manynodes channels AM_CPPFLAGS = -I../src meshlinkapp_SOURCES = meshlinkapp.c -meshlinkapp_LDADD = ../src/libmeshlink.la +meshlinkapp_LDADD = ../src/libmeshlink.la ../catta/src/libcatta.la chat_SOURCES = chat.c -chat_LDADD = ../src/libmeshlink.la +chat_LDADD = ../src/libmeshlink.la ../catta/src/libcatta.la chatpp_SOURCES = chatpp.cc -chatpp_LDADD = ../src/libmeshlink.la +chatpp_LDADD = ../src/libmeshlink.la ../catta/src/libcatta.la manynodes_SOURCES = manynodes.cc -manynodes_LDADD = ../src/libmeshlink.la +manynodes_LDADD = ../src/libmeshlink.la ../catta/src/libcatta.la channels_SOURCES = channels.c -channels_LDADD = ../src/libmeshlink.la +channels_LDADD = ../src/libmeshlink.la ../catta/src/libcatta.la diff --git a/examples/manynodes.c b/examples/manynodes.c index 3f59ec38..393d586d 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -4,7 +4,10 @@ #include #include #include + +#ifndef _WIN32 #include +#endif #include "../src/meshlink.h" #include "../src/devtools.h" @@ -118,6 +121,7 @@ void exportmeshgraph_timer(int signum) exportmeshgraph(name); } +#ifndef _WIN32 static bool exportmeshgraph_started = false; static bool exportmeshgraph_end(const char* none) @@ -172,6 +176,17 @@ static bool exportmeshgraph_begin(const char* timeout_str) return true; } +#else +static bool exportmeshgraph_end(const char* none) +{ + return false; +} + +static bool exportmeshgraph_begin(const char* timeout_str) +{ + return false; +} +#endif static void parse_command(char *buf) { char *arg = strchr(buf, ' '); @@ -365,7 +380,11 @@ int main(int argc, char *argv[]) { mesh = calloc(n, sizeof *mesh); meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); +#ifndef _WIN32 mkdir(basebase, 0750); +#else + mkdir(basebase); +#endif char filename[PATH_MAX]; char nodename[100]; diff --git a/src/Makefile.am b/src/Makefile.am index 89ca0e8e..11dc219e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -111,7 +111,11 @@ libmeshlink_la_SOURCES = \ libmeshlink_la_CFLAGS = -fPIC -I../catta/include/ -libmeshlink_la_LIBADD = -lpthread ../catta/src/.libs/libcatta.a +if MINGW +libmeshlink_la_CFLAGS += -I../catta/src/compat/windows/include/ +endif + +libmeshlink_la_LIBADD = -lpthread ../catta/src/libcatta.la libmeshlink_la_SOURCES += \ ed25519/ecdh.c \ diff --git a/src/devtools.c b/src/devtools.c index d287f3c9..2102e9bc 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -31,7 +31,7 @@ static bool fstrwrite(const char* str, FILE* stream) return true; } -static const char* itoa(int value) +static const char* __itoa(int value) { static char buffer[sizeof(int) * 8 + 1]; // not thread safe @@ -73,10 +73,10 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) if(!fstrwrite("\t\t\t\"name\": \"", stream) || !fstrwrite(((node_t*)nodes[i])->name, stream) || !fstrwrite("\",\n", stream)) goto fail; - if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(itoa(((node_t*)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) + if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(((node_t*)nodes[i])->options), stream) || !fstrwrite(",\n", stream)) goto fail; - if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(itoa(((node_t*)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) + if(!fstrwrite("\t\t\t\"devclass\": ", stream) || !fstrwrite(__itoa(((node_t*)nodes[i])->devclass), stream) || !fstrwrite("\n", stream)) goto fail; if(!fstrwrite((i+1) != node_count ? "\t\t},\n" : "\t\t}\n", stream)) @@ -120,10 +120,10 @@ bool devtool_export_json_all_edges_state(meshlink_handle_t *mesh, FILE* stream) free(address); - if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(itoa(edges[i]->options), stream) || !fstrwrite(",\n", stream)) + if(!fstrwrite("\t\t\t\"options\": ", stream) || !fstrwrite(__itoa(edges[i]->options), stream) || !fstrwrite(",\n", stream)) goto fail; - if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(itoa(edges[i]->weight), stream) || !fstrwrite("\n", stream)) + if(!fstrwrite("\t\t\t\"weight\": ", stream) || !fstrwrite(__itoa(edges[i]->weight), stream) || !fstrwrite("\n", stream)) goto fail; if(!fstrwrite((i+1) != edge_count ? "\t\t},\n" : "\t\t}\n", stream)) diff --git a/src/discovery.c b/src/discovery.c index 08ed9aea..b6ed37c5 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -1,11 +1,3 @@ - -#include "meshlink_internal.h" -#include "discovery.h" -#include "sockaddr.h" -#include "logger.h" - -#include - #include #include #include @@ -15,6 +7,13 @@ #include #include +#include "meshlink_internal.h" +#include "discovery.h" +#include "sockaddr.h" +#include "logger.h" + +#include + #include #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" @@ -214,7 +213,7 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat pthread_mutex_unlock(&(mesh->mesh_mutex)); } -static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfIndex interface, CattaProtocol protocol, CattaResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const CattaAddress *address, uint16_t port, CattaStringList *txt, CattaLookupResultFlags flags, void* userdata) +static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfIndex interface_, CattaProtocol protocol, CattaResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const CattaAddress *address, uint16_t port, CattaStringList *txt, CattaLookupResultFlags flags, void *userdata) { meshlink_handle_t *mesh = userdata; @@ -350,7 +349,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI pthread_mutex_unlock(&(mesh->mesh_mutex)); } -static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void* userdata) +static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfIndex interface_, CattaProtocol protocol, CattaBrowserEvent event, const char *name, const char *type, const char *domain, CattaLookupResultFlags flags, void* userdata) { meshlink_handle_t *mesh = userdata; @@ -383,7 +382,7 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde function we free it. Ifthe server is terminated before the callback function is called the server will free the resolver for us. */ - if(!(catta_s_service_resolver_new(mesh->catta_server, interface, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh))) + if(!(catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh))) { logger(mesh, MESHLINK_DEBUG, "Failed to resolve service '%s': %s\n", name, catta_strerror(catta_server_errno(mesh->catta_server))); } diff --git a/src/event.h b/src/event.h index 53559f80..0b664e17 100644 --- a/src/event.h +++ b/src/event.h @@ -22,6 +22,7 @@ #include "splay_tree.h" #include "system.h" +#include #define IO_READ 1 #define IO_WRITE 2 diff --git a/src/net.c b/src/net.c index 8400b936..faa29dcf 100644 --- a/src/net.c +++ b/src/net.c @@ -33,9 +33,11 @@ #include +#if !defined(min) static const int min(int a, int b) { return a < b ? a : b; } +#endif /* Terminate a connection: diff --git a/src/net_socket.c b/src/net_socket.c index d5786e5b..55e7d315 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -36,6 +36,10 @@ #define SOL_TCP IPPROTO_TCP #endif +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif + int addressfamily = AF_UNSPEC; int seconds_till_retry = 5; int max_connection_burst = 100; -- 2.39.5