From 0af8ae396551442c8b7c554f66d5dda85789d1ae Mon Sep 17 00:00:00 2001 From: Manoj Ampalam Date: Mon, 17 Oct 2016 00:30:44 -0700 Subject: [PATCH] Unicode enabled on getaddrinfo (unicode targets are now supported) --- contrib/win32/win32compat/inc/sys/socket.h | 2 + contrib/win32/win32compat/inc/w32posix.h | 3 + contrib/win32/win32compat/socketio.c | 74 +++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/contrib/win32/win32compat/inc/sys/socket.h b/contrib/win32/win32compat/inc/sys/socket.h index 9d80321..32b3ae1 100644 --- a/contrib/win32/win32compat/inc/sys/socket.h +++ b/contrib/win32/win32compat/inc/sys/socket.h @@ -20,3 +20,5 @@ #define send(a,b,c,d) w32_send((a), (b), (c), (d)) #define shutdown(a,b) w32_shutdown((a), (b)) #define socketpair(a,b,c,d) w32_socketpair((a), (b), (c), (d)) +#define freeaddrinfo w32_freeaddrinfo +#define getaddrinfo w32_getaddrinfo diff --git a/contrib/win32/win32compat/inc/w32posix.h b/contrib/win32/win32compat/inc/w32posix.h index 0f8e55d..5531209 100644 --- a/contrib/win32/win32compat/inc/w32posix.h +++ b/contrib/win32/win32compat/inc/w32posix.h @@ -71,6 +71,9 @@ int w32_sigprocmask(int how, const sigset_t *set, sigset_t *oldset); int w32_raise(int sig); int w32_kill(int pid, int sig); int w32_gethostname(char *, size_t); +void w32_freeaddrinfo(struct addrinfo *); +int w32_getaddrinfo(const char *, const char *, + const struct addrinfo *, struct addrinfo **); FILE* w32_fopen_utf8(const char *, const char *); diff --git a/contrib/win32/win32compat/socketio.c b/contrib/win32/win32compat/socketio.c index 9d9079c..e78532d 100644 --- a/contrib/win32/win32compat/socketio.c +++ b/contrib/win32/win32compat/socketio.c @@ -999,4 +999,76 @@ w32_gethostname(char *name_utf8, size_t len) { memcpy(name_utf8, tmp_name_utf8, strlen(tmp_name_utf8) + 1); free(tmp_name_utf8); return 0; -} \ No newline at end of file +} + +void +w32_freeaddrinfo(struct addrinfo *ai) { + struct addrinfo *cur; + while (ai) { + cur = ai; + ai = ai->ai_next; + if (cur->ai_addr) + free(cur->ai_addr); + if (cur->ai_canonname) + free(cur->ai_canonname); + free(cur); + } +} + +int +w32_getaddrinfo(const char *node_utf8, const char *service_utf8, + const struct addrinfo *hints, struct addrinfo **res) { + int ret = 0; + wchar_t *node_utf16 = NULL, *service_utf16 = NULL; + struct addrinfoW *info_w = NULL; + *res = NULL; + + if ((node_utf8 && (node_utf16 = utf8_to_utf16(node_utf8)) == NULL) || + (service_utf8 && (service_utf16 = utf8_to_utf16(service_utf8)) == NULL)) { + ret = EAI_MEMORY; + goto done; + } + + if ((ret = GetAddrInfoW(node_utf16, service_utf16, (ADDRINFOW*)hints, &info_w)) != 0) + goto done; + + /* copy info_w to res */ + { + struct addrinfoW **cur_w = &info_w; + struct addrinfo **cur = res; + + while (*cur_w) { + if ((*cur = malloc(sizeof(struct addrinfo))) == NULL) { + ret = EAI_MEMORY; + goto done; + } + memcpy(*cur, *cur_w, sizeof(struct addrinfo)); + (*cur)->ai_next = NULL; + if (((*cur_w)->ai_canonname && ((*cur)->ai_canonname = utf16_to_utf8((*cur_w)->ai_canonname)) == NULL) || + ((*cur_w)->ai_addrlen && ((*cur)->ai_addr = malloc((*cur_w)->ai_addrlen)) == NULL) ) { + ret = EAI_MEMORY; + goto done; + + } + if ((*cur_w)->ai_addrlen) + memcpy((*cur)->ai_addr, (*cur_w)->ai_addr, (*cur_w)->ai_addrlen); + cur_w = &(*cur_w)->ai_next; + cur = &(*cur)->ai_next; + } + } + +done: + if (node_utf16) + free(node_utf16); + if (service_utf16) + free(service_utf16); + if (info_w) + FreeAddrInfoW(info_w); + if (ret != 0 && *res) { + w32_freeaddrinfo(*res); + *res = NULL; + } + return ret; +} + +