Win32/MinGW support.

This commit is contained in:
John Maguire 2013-01-17 16:24:10 +01:00
parent 3acbda1c5d
commit 3aedea8798
4 changed files with 52 additions and 4 deletions

6
mdns.c
View File

@ -32,7 +32,13 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#ifdef _WIN32
#include <winsock.h>
#include <in6addr.h>
#else
#include <netinet/in.h> #include <netinet/in.h>
#endif
#define DEFAULT_TTL 120 #define DEFAULT_TTL 120

5
mdns.h
View File

@ -32,7 +32,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef _WIN32
#include <winsock.h>
#else
#include <arpa/inet.h> #include <arpa/inet.h>
#endif
#define MALLOC_ZERO_STRUCT(x, type) \ #define MALLOC_ZERO_STRUCT(x, type) \
x = malloc(sizeof(struct type)); \ x = malloc(sizeof(struct type)); \

20
mdnsd.c
View File

@ -26,15 +26,23 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define LOG_ERR 3
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#else
#include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <net/if.h> #include <net/if.h>
#include <syslog.h>
#endif
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/select.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
@ -44,7 +52,6 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <pthread.h> #include <pthread.h>
#include <syslog.h>
/* /*
* Define a proper IP socket level if not already done. * Define a proper IP socket level if not already done.
@ -426,7 +433,11 @@ static void main_loop(struct mdnsd *svr) {
free(pkt_buffer); free(pkt_buffer);
#ifdef _WIN32
closesocket(svr->sockfd);
#else
close(svr->sockfd); close(svr->sockfd);
#endif
svr->stop_flag = 2; svr->stop_flag = 2;
} }
@ -582,8 +593,13 @@ void mdnsd_stop(struct mdnsd *s) {
while (s->stop_flag != 2) while (s->stop_flag != 2)
select(0, NULL, NULL, NULL, &tv); select(0, NULL, NULL, NULL, &tv);
#ifdef _WIN32
closesocket(s->notify_pipe[0]);
closesocket(s->notify_pipe[1]);
#else
close(s->notify_pipe[0]); close(s->notify_pipe[0]);
close(s->notify_pipe[1]); close(s->notify_pipe[1]);
#endif
pthread_mutex_destroy(&s->data_lock); pthread_mutex_destroy(&s->data_lock);
rr_group_destroy(s->group); rr_group_destroy(s->group);

View File

@ -26,8 +26,16 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifdef _WIN32
#include <winsock2.h>
#include <in6addr.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#endif
#include <stdio.h> #include <stdio.h>
#include "mdns.h" #include "mdns.h"
#include "mdnsd.h" #include "mdnsd.h"
@ -52,8 +60,21 @@ int main(int argc, char *argv[]) {
mdnsd_add_rr(svr, a2_e); mdnsd_add_rr(svr, a2_e);
struct rr_entry *aaaa_e = NULL; struct rr_entry *aaaa_e = NULL;
struct in6_addr v6addr;
inet_pton(AF_INET6, "fe80::e2f8:47ff:fe20:28e0", &v6addr); struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
struct addrinfo* results;
getaddrinfo(
"fe80::e2f8:47ff:fe20:28e0",
NULL,
&hints,
&results);
struct sockaddr_in6* addr = (struct sockaddr_in6*)results->ai_addr;
struct in6_addr v6addr = addr->sin6_addr;
freeaddrinfo(results);
aaaa_e = rr_create_aaaa(create_nlabel(hostname), &v6addr); aaaa_e = rr_create_aaaa(create_nlabel(hostname), &v6addr);
mdnsd_add_rr(svr, aaaa_e); mdnsd_add_rr(svr, aaaa_e);