From f27cf7b59fdb7b1adcab68be28a4bfe534604723 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 23 Jan 2013 16:05:32 +0100 Subject: [PATCH] Change tinysvcmdns to use sockets instead of pipe on windows. Should make it actually work on windows. --- 3rdparty/tinysvcmdns/mdnsd.c | 74 +++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/3rdparty/tinysvcmdns/mdnsd.c b/3rdparty/tinysvcmdns/mdnsd.c index c3820c2cf..aef6c3a5e 100644 --- a/3rdparty/tinysvcmdns/mdnsd.c +++ b/3rdparty/tinysvcmdns/mdnsd.c @@ -30,7 +30,6 @@ #include #include #define LOG_ERR 3 -#define pipe(fds) _pipe(fds, 4096, _O_BINARY) #else #include #include @@ -335,6 +334,71 @@ static int process_mdns_pkt(struct mdnsd *svr, struct mdns_pkt *pkt, struct mdns return 0; } +int create_pipe(int handles[2]) { +#ifdef _WIN32 + SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) { + return -1; + } + struct sockaddr_in serv_addr; + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(0); + serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) { + closesocket(sock); + return -1; + } + if (listen(sock, 1) == SOCKET_ERROR) { + closesocket(sock); + return -1; + } + int len = sizeof(serv_addr); + if (getsockname(sock, (SOCKADDR*)&serv_addr, &len) == SOCKET_ERROR) { + closesocket(sock); + return -1; + } + if ((handles[1] = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + closesocket(sock); + return -1; + } + if (connect(handles[1], (struct sockaddr*)&serv_addr, len) == SOCKET_ERROR) { + closesocket(sock); + return -1; + } + if ((handles[0] = accept(sock, (struct sockaddr*)&serv_addr, &len)) == INVALID_SOCKET) { + closesocket((SOCKET)handles[1]); + handles[1] = INVALID_SOCKET; + closesocket(sock); + return -1; + } + closesocket(sock); + return 0; +#else + return pipe(handles); +#endif +} + +int read_pipe(int s, char* buf, int len) { +#ifdef _WIN32 + int ret = recv(s, buf, len, 0); + if (ret < 0 && WSAGetLastError() == WSAECONNRESET) { + ret = 0; + } + return ret; +#else + return read(s, buf, len); +#endif +} + +int write_pipe(int s, char* buf, int len) { +#ifdef _WIN32 + return send(s, buf, len, 0); +#else + return write(s, buf, len); +#endif +} + // main loop to receive, process and send out MDNS replies // also handles MDNS service announces static void main_loop(struct mdnsd *svr) { @@ -358,7 +422,7 @@ static void main_loop(struct mdnsd *svr) { if (FD_ISSET(svr->notify_pipe[0], &sockfd_set)) { // flush the notify_pipe - read(svr->notify_pipe[0], ¬ify_buf, 1); + read_pipe(svr->notify_pipe[0], (char*)¬ify_buf, 1); } else if (FD_ISSET(svr->sockfd, &sockfd_set)) { struct sockaddr_in fromaddr; socklen_t sockaddr_size = sizeof(struct sockaddr_in); @@ -533,7 +597,7 @@ struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_ free(inst_nlabel); // notify server - write(svr->notify_pipe[1], ".", 1); + write_pipe(svr->notify_pipe[1], ".", 1); return service; } @@ -551,7 +615,7 @@ struct mdnsd *mdnsd_start() { struct mdnsd *server = malloc(sizeof(struct mdnsd)); memset(server, 0, sizeof(struct mdnsd)); - if (pipe(server->notify_pipe) != 0) { + if (create_pipe(server->notify_pipe) != 0) { log_message(LOG_ERR, "pipe(): %m\n"); free(server); return NULL; @@ -588,7 +652,7 @@ void mdnsd_stop(struct mdnsd *s) { }; s->stop_flag = 1; - write(s->notify_pipe[1], ".", 1); + write_pipe(s->notify_pipe[1], ".", 1); while (s->stop_flag != 2) select(0, NULL, NULL, NULL, &tv);