Add WSAStartup() for Windows psl tool and tests.

As reported at https://github.com/rockdaboot/libpsl/issues/173,
psl_is_cookie_domain_acceptable() doesn't work properly without
WSAStartup() on Windows.

Co-authored-by: gvanem@yahoo.no
This commit is contained in:
Tim Rühsen 2021-11-26 18:45:16 +01:00
parent b2625f93f2
commit f5035b854b
4 changed files with 41 additions and 14 deletions

View File

@ -20,7 +20,6 @@ libicu_dep = notfound
libidn_dep = notfound libidn_dep = notfound
libunistring = notfound libunistring = notfound
networking_deps = notfound networking_deps = notfound
libiconv_dep = notfound
# FIXME: Cleanup this when Meson gets 'feature-combo': # FIXME: Cleanup this when Meson gets 'feature-combo':
# https://github.com/mesonbuild/meson/issues/4566 # https://github.com/mesonbuild/meson/issues/4566
@ -87,19 +86,6 @@ endif
if libidn2_dep.found() or libidn_dep.found() if libidn2_dep.found() or libidn_dep.found()
# Check for libunistring, we need it for psl_str_to_utf8lower() # Check for libunistring, we need it for psl_str_to_utf8lower()
libunistring = cc.find_library('unistring') libunistring = cc.find_library('unistring')
found_iconv = false
if cc.has_function('iconv_open')
libiconv_dep = []
found_iconv = true
endif
if not found_iconv and cc.has_header_symbol('iconv.h', 'iconv_open')
libiconv_dep = [cc.find_library('iconv')]
found_iconv = true
endif
if not found_iconv
error('iconv implementation not found')
endif
endif endif
if host_machine.system() == 'windows' if host_machine.system() == 'windows'

View File

@ -1619,6 +1619,9 @@ static int isip(const char *hostname)
* *
* Use helper function psl_str_to_utf8lower() for normalization of @hostname and @cookie_domain. * Use helper function psl_str_to_utf8lower() for normalization of @hostname and @cookie_domain.
* *
* Hint for Windows users:
* Please make sure the calling application has called WSAStartup() before calling psl_is_cookie_domain_acceptable().
*
* Examples: * Examples:
* 1. Cookie domain 'example.com' would be acceptable for hostname 'www.example.com', * 1. Cookie domain 'example.com' would be acceptable for hostname 'www.example.com',
* but '.com' or 'com' would NOT be acceptable since 'com' is a public suffix. * but '.com' or 'com' would NOT be acceptable since 'com' is a public suffix.

View File

@ -32,6 +32,10 @@
# include <config.h> # include <config.h>
#endif #endif
#ifdef _WIN32
# include <winsock2.h> // WSAStartup, WSACleanup
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -109,6 +113,18 @@ static void test_psl(void)
int main(int argc, const char * const *argv) int main(int argc, const char * const *argv)
{ {
#ifdef _WIN32
WSADATA wsa_data;
int err;
if ((err = WSAStartup(MAKEWORD(2,2), &wsa_data))) {
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
atexit((void (__cdecl*)(void)) WSACleanup);
#endif
/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */ /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
if (argc == 1) { if (argc == 1) {
const char *valgrind = getenv("TESTS_VALGRIND"); const char *valgrind = getenv("TESTS_VALGRIND");

View File

@ -36,6 +36,10 @@
# include <unistd.h> # include <unistd.h>
#endif #endif
#ifdef _WIN32
# include <winsock2.h> // WSAStartup, WSACleanup
#endif
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@ -66,6 +70,20 @@ static void usage(int err, FILE* f)
exit(err); exit(err);
} }
static void init_windows(void) {
#ifdef _WIN32
WSADATA wsa_data;
int err;
if ((err = WSAStartup(MAKEWORD(2,2), &wsa_data))) {
printf("WSAStartup failed with error: %d\n", err);
exit(EXIT_FAILURE);
}
atexit((void (__cdecl*)(void)) WSACleanup);
#endif
}
/* RFC 2822-compliant date format */ /* RFC 2822-compliant date format */
static const char *time2str(time_t t) static const char *time2str(time_t t)
{ {
@ -209,6 +227,8 @@ int main(int argc, const char *const *argv)
else if (mode == 4) { else if (mode == 4) {
char *cookie_domain_lower; char *cookie_domain_lower;
init_windows();
if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) == PSL_SUCCESS) { if ((rc = psl_str_to_utf8lower(domain, NULL, NULL, &cookie_domain_lower)) == PSL_SUCCESS) {
if (!batch_mode) if (!batch_mode)
printf("%s: ", domain); printf("%s: ", domain);
@ -253,6 +273,8 @@ int main(int argc, const char *const *argv)
} }
} }
else if (mode == 4) { else if (mode == 4) {
init_windows();
for (; arg < argv + argc; arg++) { for (; arg < argv + argc; arg++) {
if (!batch_mode) if (!batch_mode)
printf("%s: ", *arg); printf("%s: ", *arg);