OpenVPN
win32.h
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2017 OpenVPN Technologies, Inc. <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef _WIN32
25 #ifndef OPENVPN_WIN32_H
26 #define OPENVPN_WIN32_H
27 
28 #include "mtu.h"
29 
30 /* location of executables */
31 #define SYS_PATH_ENV_VAR_NAME "SystemRoot" /* environmental variable name that normally contains the system path */
32 #define NETSH_PATH_SUFFIX "\\system32\\netsh.exe"
33 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
34 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
35 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
36 
37 /*
38  * Win32-specific OpenVPN code, targetted at the mingw
39  * development environment.
40  */
41 
42 /* MSVC headers do not define this macro, so do it here */
43 #ifndef IN6_ARE_ADDR_EQUAL
44 #define IN6_ARE_ADDR_EQUAL(a,b) \
45  (memcmp((const void *)(a), (const void *)(b), sizeof(struct in6_addr)) == 0)
46 #endif
47 
48 void init_win32(void);
49 
50 void uninit_win32(void);
51 
52 void set_pause_exit_win32(void);
53 
55 {
56  SECURITY_ATTRIBUTES sa;
57  SECURITY_DESCRIPTOR sd;
58 };
59 
60 #define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
61 
62 /*
63  * Save old window title.
64  */
66 {
67  bool saved;
68  char old_window_title [256];
69 };
70 
71 struct rw_handle {
72  HANDLE read;
73  HANDLE write;
74 };
75 
76 /*
77  * Event-based notification of incoming TCP connections
78  */
79 
80 #define NE32_PERSIST_EVENT (1<<0)
81 #define NE32_WRITE_EVENT (1<<1)
82 
83 static inline bool
84 defined_net_event_win32(const struct rw_handle *event)
85 {
86  return event->read != NULL;
87 }
88 
89 void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags);
90 
92 
93 void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
94 
95 /*
96  * A stateful variant of the net_event_win32 functions above
97  */
98 
100 {
101  struct rw_handle handle;
104 };
105 
106 void net_event_win32_init(struct net_event_win32 *ne);
107 
108 void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
109 
110 void net_event_win32_reset(struct net_event_win32 *ne);
111 
113 
114 void net_event_win32_stop(struct net_event_win32 *ne);
115 
116 void net_event_win32_close(struct net_event_win32 *ne);
117 
118 static inline bool
120 {
121  return defined_net_event_win32(&ne->handle);
122 }
123 
124 static inline struct rw_handle *
126 {
127  return &ne->handle;
128 }
129 
130 static inline long
132 {
133  return ne->event_mask;
134 }
135 
136 static inline void
138 {
139  ne->event_mask &= ~selected_events;
140 }
141 
142 /*
143  * Signal handling
144  */
145 struct win32_signal {
146 #define WSO_MODE_UNDEF 0
147 #define WSO_MODE_SERVICE 1
148 #define WSO_MODE_CONSOLE 2
149  int mode;
150  struct rw_handle in;
153 };
154 
155 extern struct win32_signal win32_signal; /* static/global */
156 extern struct window_title window_title; /* static/global */
157 
158 void win32_signal_clear(struct win32_signal *ws);
159 
160 /* win32_signal_open startup type */
161 #define WSO_NOFORCE 0
162 #define WSO_FORCE_SERVICE 1
163 #define WSO_FORCE_CONSOLE 2
164 
165 void win32_signal_open(struct win32_signal *ws,
166  int force, /* set to WSO force parm */
167  const char *exit_event_name,
168  bool exit_event_initial_state);
169 
170 void win32_signal_close(struct win32_signal *ws);
171 
172 int win32_signal_get(struct win32_signal *ws);
173 
174 void win32_pause(struct win32_signal *ws);
175 
176 bool win32_service_interrupt(struct win32_signal *ws);
177 
178 /*
179  * Set the text on the window title bar
180  */
181 
182 void window_title_clear(struct window_title *wt);
183 
184 void window_title_save(struct window_title *wt);
185 
186 void window_title_restore(const struct window_title *wt);
187 
188 void window_title_generate(const char *title);
189 
190 /*
191  * We try to do all Win32 I/O using overlapped
192  * (i.e. asynchronous) I/O for a performance win.
193  */
195 #define IOSTATE_INITIAL 0
196 #define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
197 #define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
198  int iostate;
199  OVERLAPPED overlapped;
200  DWORD size;
201  DWORD flags;
202  int status;
204  union {
205  struct sockaddr_in addr;
206  struct sockaddr_in6 addr6;
207  };
208  int addrlen;
209  struct buffer buf_init;
210  struct buffer buf;
211 };
212 
213 void overlapped_io_init(struct overlapped_io *o,
214  const struct frame *frame,
215  BOOL event_state,
216  bool tuntap_buffer);
217 
218 void overlapped_io_close(struct overlapped_io *o);
219 
220 static inline bool
222 {
224 }
225 
226 char *overlapped_io_state_ascii(const struct overlapped_io *o);
227 
228 /*
229  * Use to control access to resources that only one
230  * OpenVPN process on a given machine can access at
231  * a given time.
232  */
233 
234 struct semaphore
235 {
236  const char *name;
237  bool locked;
238  HANDLE hand;
239 };
240 
241 void semaphore_clear(struct semaphore *s);
242 
243 void semaphore_open(struct semaphore *s, const char *name);
244 
245 bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
246 
247 void semaphore_release(struct semaphore *s);
248 
249 void semaphore_close(struct semaphore *s);
250 
251 /*
252  * Special global semaphore used to protect network
253  * shell commands from simultaneous instantiation.
254  *
255  * It seems you can't run more than one instance
256  * of netsh on the same machine at the same time.
257  */
258 
259 extern struct semaphore netcmd_semaphore;
260 void netcmd_semaphore_init(void);
261 
262 void netcmd_semaphore_close(void);
263 
264 void netcmd_semaphore_lock(void);
265 
266 void netcmd_semaphore_release(void);
267 
268 /* Set Win32 security attributes structure to allow all access */
270 
271 /* return true if filename is safe to be used on Windows */
272 bool win_safe_filename(const char *fn);
273 
274 /* add constant environmental variables needed by Windows */
275 struct env_set;
276 
277 /* get and set the current windows system path */
278 void set_win_sys_path(const char *newpath, struct env_set *es);
279 
280 void set_win_sys_path_via_env(struct env_set *es);
281 
282 char *get_win_sys_path(void);
283 
284 /* call self in a subprocess */
285 void fork_to_self(const char *cmdline);
286 
287 /* Find temporary directory */
288 const char *win_get_tempdir(void);
289 
290 /* Convert a string from UTF-8 to UCS-2 */
291 WCHAR *wide_string(const char *utf8, struct gc_arena *gc);
292 
293 bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel);
294 
295 bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
296 
297 #define WIN_XP 0
298 #define WIN_VISTA 1
299 #define WIN_7 2
300 #define WIN_8 3
301 
302 int win32_version_info(void);
303 
304 /*
305  * String representation of Windows version number and name, see
306  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
307  */
308 const char *win32_version_string(struct gc_arena *gc, bool add_name);
309 
310 #endif /* ifndef OPENVPN_WIN32_H */
311 #endif /* ifdef _WIN32 */
int status
Definition: win32.h:202
void window_title_restore(const struct window_title *wt)
Definition: win32.c:723
void semaphore_close(struct semaphore *s)
Definition: win32.c:828
void init_win32(void)
Definition: win32.c:105
void window_title_clear(struct window_title *wt)
Definition: win32.c:700
HANDLE read
Definition: win32.h:72
void win32_signal_close(struct win32_signal *ws)
Definition: win32.c:599
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition: win32.c:326
Packet geometry parameters.
Definition: mtu.h:93
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition: win32.c:778
struct gc_arena * gc
Definition: misc.h:50
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition: win32.h:125
int win32_signal_get(struct win32_signal *ws)
Definition: win32.c:633
DWORD size
Definition: win32.h:200
OVERLAPPED overlapped
Definition: win32.h:199
void uninit_win32(void)
Definition: win32.c:116
void window_title_generate(const char *title)
Definition: win32.c:732
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition: win32.c:257
void set_win_sys_path_via_env(struct env_set *es)
Definition: win32.c:1221
void win32_signal_clear(struct win32_signal *ws)
Definition: win32.c:442
static bool overlapped_io_active(struct overlapped_io *o)
Definition: win32.h:221
void win32_pause(struct win32_signal *ws)
Definition: win32.c:684
void net_event_win32_stop(struct net_event_win32 *ne)
Definition: win32.c:359
void netcmd_semaphore_close(void)
Definition: win32.c:854
const char * win_get_tempdir(void)
Definition: win32.c:1238
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition: win32.c:1213
void overlapped_io_close(struct overlapped_io *o)
Definition: win32.c:184
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:218
void semaphore_release(struct semaphore *s)
Definition: win32.c:812
int iostate
Definition: win32.h:198
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition: win32.c:448
void netcmd_semaphore_release(void)
Definition: win32.c:876
bool win_safe_filename(const char *fn)
Definition: win32.c:936
bool console_mode_save_defined
Definition: win32.h:152
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition: win32.c:197
void net_event_win32_reset(struct net_event_win32 *ne)
Definition: win32.c:353
#define IOSTATE_IMMEDIATE_RETURN
Definition: win32.h:197
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1443
void netcmd_semaphore_lock(void)
Definition: win32.c:860
void set_pause_exit_win32(void)
Definition: win32.c:140
#define IOSTATE_QUEUED
Definition: win32.h:196
SECURITY_ATTRIBUTES sa
Definition: win32.h:56
void net_event_win32_init(struct net_event_win32 *ne)
Definition: win32.c:319
void window_title_save(struct window_title *wt)
Definition: win32.c:706
int addrlen
Definition: win32.h:208
static bool defined_net_event_win32(const struct rw_handle *event)
Definition: win32.h:84
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1374
void netcmd_semaphore_init(void)
Definition: win32.c:848
long event_mask
Definition: win32.h:103
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition: win32.h:137
bool win32_service_interrupt(struct win32_signal *ws)
Definition: win32.c:619
struct rw_handle handle
Definition: win32.h:101
bool init_security_attributes_allow_all(struct security_attributes *obj)
Definition: win32.c:146
SECURITY_DESCRIPTOR sd
Definition: win32.h:57
bool locked
Definition: win32.h:237
HANDLE write
Definition: win32.h:73
char * get_win_sys_path(void)
Definition: win32.c:1206
int win32_version_info(void)
Definition: win32.c:1401
int mode
Definition: win32.h:149
SOCKET socket_descriptor_t
Definition: syshead.h:487
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition: win32.h:119
bool addr_defined
Definition: win32.h:203
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
struct semaphore netcmd_semaphore
Definition: win32.c:97
DWORD flags
Definition: win32.h:201
#define buf_init(buf, offset)
Definition: buffer.h:198
bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1323
DWORD console_mode_save
Definition: win32.h:151
void net_event_win32_close(struct net_event_win32 *ne)
Definition: win32.c:370
const char * name
Definition: win32.h:236
HANDLE hand
Definition: win32.h:238
Definition: misc.h:49
void fork_to_self(const char *cmdline)
Definition: win32.c:1166
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition: win32.h:131
void semaphore_clear(struct semaphore *s)
Definition: win32.c:748
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:272
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition: win32.c:335
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition: win32.c:1154
socket_descriptor_t sd
Definition: win32.h:102
bool saved
Definition: win32.h:67
void semaphore_open(struct semaphore *s, const char *name)
Definition: win32.c:754
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state, bool tuntap_buffer)
Definition: win32.c:165