From ac3e4889242e0c40f8edefd02a2a024a0e663edc Mon Sep 17 00:00:00 2001 From: Chintan Parikh Date: Tue, 26 Dec 2017 14:20:12 -0800 Subject: [PATCH] Compile DIAL client and server for macOS. Changes: * Added method to get mac and ip address for OSX. Current Linux method of query via SIOCGIFCONF doesn't work. Added code to read via getifaddrs() API. * Put non OSX specific code under #ifdef flags. * Removed few linker libraries which are not required (lcares and lrt). * Fixed many warnings. Tested on: OSX 10.12 --- client/DialConformance.h | 1 - client/DialDiscovery.h | 4 +--- client/DialServer.cpp | 2 +- client/main.cpp | 2 +- client/makefile | 2 +- server/LinuxInterfaces.c | 1 + server/LinuxInterfaces.h | 7 +++++-- server/makefile | 8 +++++--- server/mongoose.c | 4 ++++ server/nf_callbacks.c | 1 + server/quick_ssdp.c | 37 +++++++++++++++++++++++++++++++++++-- 11 files changed, 55 insertions(+), 14 deletions(-) diff --git a/client/DialConformance.h b/client/DialConformance.h index ae87919..f419a12 100644 --- a/client/DialConformance.h +++ b/client/DialConformance.h @@ -65,7 +65,6 @@ public: string &outputFile ); private: DialConformance(); - DialServer* _pServer; static DialConformance* sConformance; DialClientInput _input; diff --git a/client/DialDiscovery.h b/client/DialDiscovery.h index f33ec75..0cc14f3 100644 --- a/client/DialDiscovery.h +++ b/client/DialDiscovery.h @@ -28,6 +28,7 @@ #include "DialServer.h" #include +#include using namespace std; @@ -82,9 +83,6 @@ private: void cleanServerList(); void resetDiscovery(); - pthread_t _mcastThread; - pthread_t _responseThread; - typedef map ServerMap; ServerMap mServerMap; diff --git a/client/DialServer.cpp b/client/DialServer.cpp index d6592e7..11e9f2f 100644 --- a/client/DialServer.cpp +++ b/client/DialServer.cpp @@ -226,7 +226,7 @@ int DialServer::getStatus( if (!status) return 0; ATRACE("Body: %s\n", responseBody.c_str()); - unsigned found = responseBody.find("href="); + size_t found = responseBody.find("href="); if( found != string::npos ) { // The start of href is after href= and the quote diff --git a/client/main.cpp b/client/main.cpp index 8959ecd..0c4f778 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -136,7 +136,7 @@ static void runConformance() else { printf("DIAL server not found\n"); - printf("%Zu available server(s): \n", list.size()); + printf("%zu available server(s): \n", list.size()); printServerList(list); } } diff --git a/client/makefile b/client/makefile index 29e9131..c35cb16 100644 --- a/client/makefile +++ b/client/makefile @@ -8,7 +8,7 @@ OBJS := main.cpp DialServer.cpp DialDiscovery.cpp DialConformance.cpp DialClient # You may not need all these libraries. This example uses a build of curl that needs crypto, ssl, cares, and zlib dialclient: $(OBJS) ${includes} - $(CC) -Wall -Werror -g $(OBJS) $(INCLUDES) $(LDFLAGS) -ldl -lpthread -lcurl -lz -lcrypto -lssl -lcares -o dialclient + $(CC) -Wall -Werror -g $(OBJS) $(INCLUDES) $(LDFLAGS) -ldl -lpthread -lcurl -lz -lcrypto -lssl -o dialclient dialclient_debug: $(OBJS) ${includes} $(CC) -DDEBUG -Wall -Werror -g $(OBJS) $(INCLUDES) $(LDFLAGS) -ldl -lpthread -lcurl -lz -lcrypto -lssl -lcares -o dialclient_debug diff --git a/server/LinuxInterfaces.c b/server/LinuxInterfaces.c index ba9153a..0225e3e 100644 --- a/server/LinuxInterfaces.c +++ b/server/LinuxInterfaces.c @@ -77,3 +77,4 @@ NetInterface getDefaultNetworkInterfaces() } return defaultNet; } + diff --git a/server/LinuxInterfaces.h b/server/LinuxInterfaces.h index 1ec41ee..f552cbb 100644 --- a/server/LinuxInterfaces.h +++ b/server/LinuxInterfaces.h @@ -1,10 +1,13 @@ #ifndef LINUXINTERFACES_H_ #define LINUXINTERFACES_H_ -#include -#include +#ifndef __APPLE__ #include #include +#endif + +#include +#include #include #include #include diff --git a/server/makefile b/server/makefile index eae6c7a..fd21ae4 100644 --- a/server/makefile +++ b/server/makefile @@ -3,7 +3,7 @@ CC=$(TARGET)gcc .PHONY: clean .DEFAULT_GOAL=all -OBJS := main.o dial_server.o mongoose.o quick_ssdp.o url_lib.o dial_data.o LinuxInterfaces.o +OBJS := main.o dial_server.o mongoose.o quick_ssdp.o url_lib.o dial_data.o HEADERS := $(wildcard *.h) %.c: $(HEADERS) @@ -15,10 +15,12 @@ HEADERS := $(wildcard *.h) all: dialserver test nf_callbacks_lib: nf_callbacks.o - $(CC) -Wall -Werror -g nf_callbacks.o -o libnfCallbacks.so --shared +# $(CC) -Wall -Werror -g nf_callbacks.o -o libnfCallbacks.so --shared + $(CC) -Wall -Werror -Wl,-undefined -Wl,dynamic_lookup -g nf_callbacks.o -o libnfCallbacks.so --shared + dialserver: nf_callbacks_lib $(OBJS) - $(CC) -Wall -Werror -Wl,-rpath,. -g $(OBJS) -ldl -lpthread -lrt -L. -lnfCallbacks -o dialserver + $(CC) -Wall -Werror -Wl,-rpath,. -g $(OBJS) -ldl -lpthread -L. -lnfCallbacks -o dialserver test: make -C tests diff --git a/server/mongoose.c b/server/mongoose.c index cee543e..bf08b87 100644 --- a/server/mongoose.c +++ b/server/mongoose.c @@ -800,7 +800,9 @@ static void worker_thread(struct mg_context *ctx) { // required in the DIAL specification. int buf_size = MAX_REQUEST_SIZE; +#ifndef __APPLE__ pthread_setname_np( pthread_self(), __func__); +#endif conn = (struct mg_connection *) calloc(1, sizeof(*conn) + buf_size); conn->buf_size = buf_size; conn->buf = (char *) (conn + 1); @@ -860,7 +862,9 @@ static void produce_socket(struct mg_context *ctx, const struct socket *sp) { static void master_thread(struct mg_context *ctx) { struct socket accepted; +#ifndef __APPLE__ pthread_setname_np( pthread_self(), __func__); +#endif socklen_t sock_len = sizeof(accepted.local_addr); memcpy(&accepted.local_addr, &ctx->local_address, sock_len); diff --git a/server/nf_callbacks.c b/server/nf_callbacks.c index 6e51f73..fa702a9 100644 --- a/server/nf_callbacks.c +++ b/server/nf_callbacks.c @@ -8,6 +8,7 @@ #include #include "dial_server.h" #include "url_lib.h" +#include "nf_callbacks.h" extern char *spAppNetflix; extern char spNetflix[]; diff --git a/server/quick_ssdp.c b/server/quick_ssdp.c index e641741..07b8e24 100644 --- a/server/quick_ssdp.c +++ b/server/quick_ssdp.c @@ -35,6 +35,13 @@ #include "mongoose.h" #include +#ifdef __APPLE__ +#include +#include +#include +#endif + + // TODO: Partners should define this port #define SSDP_PORT (56790) static char gBuf[4096]; @@ -108,6 +115,30 @@ static void *request_handler(enum mg_event event, return NULL; } +#ifdef __APPLE__ +static void get_local_address() { + struct ifaddrs* iflist; + char *if_name= "en0"; + char buf[INET6_ADDRSTRLEN]; + + if (getifaddrs(&iflist) == 0) { + for (struct ifaddrs* cur = iflist; cur; cur = cur->ifa_next) { + if (strcmp(cur->ifa_name, if_name) == 0) { + if ((cur->ifa_addr->sa_family == AF_LINK) && cur->ifa_addr) { + struct sockaddr_dl* sdl = (struct sockaddr_dl*)cur->ifa_addr; + memcpy(hw_addr, LLADDR(sdl), sdl->sdl_alen); + } + + if (cur->ifa_addr->sa_family == AF_INET) { + void *tmp = &((struct sockaddr_in *)cur->ifa_addr)->sin_addr; + strcpy(ip_addr, inet_ntop(cur->ifa_addr->sa_family, tmp, buf, sizeof(buf))); + } + } + } + freeifaddrs(iflist); + } +} +#else static void get_local_address() { struct ifconf ifc; char buf[4096]; @@ -152,6 +183,7 @@ static void get_local_address() { } close(s); } +#endif static void handle_mcast() { int s, one = 1, bytes; @@ -174,8 +206,9 @@ static void handle_mcast() { exit(1); } saddr.sin_family = AF_INET; - saddr.sin_addr.s_addr = inet_addr("239.255.255.250"); + saddr.sin_addr.s_addr = INADDR_ANY; //inet_addr("239.255.255.250"); saddr.sin_port = htons(1900); + if (-1 == bind(s, (struct sockaddr *)&saddr, sizeof(saddr))) { perror("bind"); exit(1); @@ -211,7 +244,7 @@ static void handle_mcast() { printf("\n##### End of DROP #######\n"); #endif continue; - } + } printf("Sending SSDP reply to %s:%d\n", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); if (-1 == sendto(s, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {