2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifndef FORWARD_INLINE_H
26 #define FORWARD_INLINE_H
33 * Does TLS session need service?
36 check_tls (struct context
*c
)
38 #if defined(USE_CRYPTO) && defined(USE_SSL)
39 void check_tls_dowork (struct context
*c
);
46 * TLS errors are fatal in TCP mode.
47 * Also check for --tls-exit trigger.
50 check_tls_errors (struct context
*c
)
52 #if defined(USE_CRYPTO) && defined(USE_SSL)
53 void check_tls_errors_co (struct context
*c
);
54 void check_tls_errors_nco (struct context
*c
);
55 if (c
->c2
.tls_multi
&& c
->c2
.tls_exit_signal
)
57 if (link_socket_connection_oriented (c
->c2
.link_socket
))
59 if (c
->c2
.tls_multi
->n_soft_errors
)
60 check_tls_errors_co (c
);
64 if (c
->c2
.tls_multi
->n_hard_errors
)
65 check_tls_errors_nco (c
);
72 * Check for possible incoming configuration
73 * messages on the control channel.
76 check_incoming_control_channel (struct context
*c
)
79 void check_incoming_control_channel_dowork (struct context
*c
);
80 if (tls_test_payload_len (c
->c2
.tls_multi
) > 0)
81 check_incoming_control_channel_dowork (c
);
86 * Options like --up-delay need to be triggered by this function which
87 * checks for connection establishment.
90 check_connection_established (struct context
*c
)
92 void check_connection_established_dowork (struct context
*c
);
93 if (event_timeout_defined (&c
->c2
.wait_for_connect
))
94 check_connection_established_dowork (c
);
98 * Should we add routes?
101 check_add_routes (struct context
*c
)
103 void check_add_routes_dowork (struct context
*c
);
104 if (event_timeout_trigger (&c
->c2
.route_wakeup
, &c
->c2
.timeval
, ETT_DEFAULT
))
105 check_add_routes_dowork (c
);
109 * Should we exit due to inactivity timeout?
112 check_inactivity_timeout (struct context
*c
)
114 void check_inactivity_timeout_dowork (struct context
*c
);
116 if (c
->options
.inactivity_timeout
117 && event_timeout_trigger (&c
->c2
.inactivity_interval
, &c
->c2
.timeval
, ETT_DEFAULT
))
118 check_inactivity_timeout_dowork (c
);
124 check_server_poll_timeout (struct context
*c
)
126 void check_server_poll_timeout_dowork (struct context
*c
);
128 if (c
->options
.server_poll_timeout
129 && event_timeout_trigger (&c
->c2
.server_poll_interval
, &c
->c2
.timeval
, ETT_DEFAULT
))
130 check_server_poll_timeout_dowork (c
);
137 check_scheduled_exit (struct context
*c
)
139 void check_scheduled_exit_dowork (struct context
*c
);
141 if (event_timeout_defined (&c
->c2
.scheduled_exit
))
143 if (event_timeout_trigger (&c
->c2
.scheduled_exit
, &c
->c2
.timeval
, ETT_DEFAULT
))
144 check_scheduled_exit_dowork (c
);
150 * Should we write timer-triggered status file.
153 check_status_file (struct context
*c
)
155 void check_status_file_dowork (struct context
*c
);
157 if (c
->c1
.status_output
)
159 if (status_trigger_tv (c
->c1
.status_output
, &c
->c2
.timeval
))
160 check_status_file_dowork (c
);
164 #ifdef ENABLE_FRAGMENT
166 * Should we deliver a datagram fragment to remote?
169 check_fragment (struct context
*c
)
171 void check_fragment_dowork (struct context
*c
);
173 check_fragment_dowork (c
);
180 * see if we should send a push_request in response to --pull
183 check_push_request (struct context
*c
)
185 void check_push_request_dowork (struct context
*c
);
186 if (event_timeout_trigger (&c
->c2
.push_request_interval
, &c
->c2
.timeval
, ETT_DEFAULT
))
187 check_push_request_dowork (c
);
194 * Should we persist our anti-replay packet ID state to disk?
197 check_packet_id_persist_flush (struct context
*c
)
199 if (packet_id_persist_enabled (&c
->c1
.pid_persist
)
200 && event_timeout_trigger (&c
->c2
.packet_id_persist_interval
, &c
->c2
.timeval
, ETT_DEFAULT
))
201 packet_id_persist_save (&c
->c1
.pid_persist
);
206 * Set our wakeup to 0 seconds, so we will be rescheduled
210 context_immediate_reschedule (struct context
*c
)
212 c
->c2
.timeval
.tv_sec
= 0; /* ZERO-TIMEOUT */
213 c
->c2
.timeval
.tv_usec
= 0;
217 context_reschedule_sec (struct context
*c
, int sec
)
221 if (sec
< c
->c2
.timeval
.tv_sec
)
223 c
->c2
.timeval
.tv_sec
= sec
;
224 c
->c2
.timeval
.tv_usec
= 0;
228 static inline struct link_socket_info
*
229 get_link_socket_info (struct context
*c
)
231 if (c
->c2
.link_socket_info
)
232 return c
->c2
.link_socket_info
;
234 return &c
->c2
.link_socket
->info
;
238 register_activity (struct context
*c
, const int size
)
240 if (c
->options
.inactivity_timeout
)
242 c
->c2
.inactivity_bytes
+= size
;
243 if (c
->c2
.inactivity_bytes
>= c
->options
.inactivity_minimum_bytes
)
245 c
->c2
.inactivity_bytes
= 0;
246 event_timeout_reset (&c
->c2
.inactivity_interval
);
252 * Return the io_wait() flags appropriate for
253 * a point-to-point tunnel.
255 static inline unsigned int
256 p2p_iow_flags (const struct context
*c
)
258 unsigned int flags
= (IOW_SHAPER
|IOW_CHECK_RESIDUAL
|IOW_FRAG
|IOW_READ
|IOW_WAIT_SIGNAL
);
259 if (c
->c2
.to_link
.len
> 0)
260 flags
|= IOW_TO_LINK
;
261 if (c
->c2
.to_tun
.len
> 0)
267 * This is the core I/O wait function, used for all I/O waits except
268 * for TCP in server mode.
271 io_wait (struct context
*c
, const unsigned int flags
)
273 void io_wait_dowork (struct context
*c
, const unsigned int flags
);
275 if (c
->c2
.fast_io
&& (flags
& (IOW_TO_TUN
|IOW_TO_LINK
|IOW_MBUF
)))
277 /* fast path -- only for TUN/TAP/UDP writes */
278 unsigned int ret
= 0;
279 if (flags
& IOW_TO_TUN
)
281 if (flags
& (IOW_TO_LINK
|IOW_MBUF
))
283 c
->c2
.event_set_status
= ret
;
288 io_wait_dowork (c
, flags
);
292 #define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)
294 #endif /* EVENT_INLINE_H */