From 87a79e6a037c79f73743ed4aa357747760314325 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 16 Oct 2013 17:27:19 +0200 Subject: [PATCH] windows: Add utils_init/deinit functions to initialize Winsock2 --- src/libstrongswan/Makefile.am | 2 ++ src/libstrongswan/library.c | 2 ++ src/libstrongswan/utils/utils.c | 20 ++++++++++++++++++ src/libstrongswan/utils/utils.h | 10 +++++++++ src/libstrongswan/utils/windows.c | 35 +++++++++++++++++++++++++++++++ src/libstrongswan/utils/windows.h | 10 +++++++++ 6 files changed, 79 insertions(+) create mode 100644 src/libstrongswan/utils/windows.c diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 35605074a..711676a26 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -112,6 +112,8 @@ AM_YFLAGS = -v -d if USE_WINDOWS libstrongswan_la_LIBADD += -lws2_32 + libstrongswan_la_SOURCES += \ + utils/windows.c endif if USE_DBGHELP diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index f152a8c1f..b06a2d5a5 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -146,6 +146,7 @@ void library_deinit() arrays_deinit(); threads_deinit(); backtrace_deinit(); + utils_deinit(); free((void*)this->public.ns); free(this); @@ -259,6 +260,7 @@ bool library_init(char *settings, const char *namespace) ); lib = &this->public; + utils_init(); backtrace_init(); threads_init(); arrays_init(); diff --git a/src/libstrongswan/utils/utils.c b/src/libstrongswan/utils/utils.c index fe3b32f6c..81eb2acec 100644 --- a/src/libstrongswan/utils/utils.c +++ b/src/libstrongswan/utils/utils.c @@ -47,6 +47,26 @@ ENUM(status_names, SUCCESS, NEED_MORE, "NEED_MORE", ); +/** + * See header + */ +void utils_init() +{ +#ifdef WIN32 + windows_init(); +#endif /* WIN32 */ +} + +/** + * See header + */ +void utils_deinit() +{ +#ifdef WIN32 + windows_deinit(); +#endif /* WIN32 */ +} + /** * Described in header. */ diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index ca0d6b9a3..c14b9c11e 100644 --- a/src/libstrongswan/utils/utils.h +++ b/src/libstrongswan/utils/utils.h @@ -82,6 +82,16 @@ #include "enum.h" #include "utils/strerror.h" +/** + * Initialize utility functions + */ +void utils_init(); + +/** + * Deinitialize utility functions + */ +void utils_deinit(); + /** * Helper function that compares two strings for equality */ diff --git a/src/libstrongswan/utils/windows.c b/src/libstrongswan/utils/windows.c new file mode 100644 index 000000000..6627a6c32 --- /dev/null +++ b/src/libstrongswan/utils/windows.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2013 Martin Willi + * Copyright (C) 2013 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "utils.h" + +/** + * See header + */ +void windows_init() +{ + WSADATA wsad; + + /* initialize winsock2 */ + WSAStartup(MAKEWORD(2, 2), &wsad); +} + +/** + * See header + */ +void windows_deinit() +{ + WSACleanup(); +} diff --git a/src/libstrongswan/utils/windows.h b/src/libstrongswan/utils/windows.h index c0a5198a5..110498060 100644 --- a/src/libstrongswan/utils/windows.h +++ b/src/libstrongswan/utils/windows.h @@ -41,6 +41,16 @@ typedef u_int uid_t; typedef u_int gid_t; +/** + * Initialize Windows libraries + */ +void windows_init(); + +/** + * Deinitialize windows libraries + */ +void windows_deinit(); + /** * Replacement for random(3) */