svn cleanup
[anytun.git] / openvpn / forward-inline.h
blob0ba0b23f225291b4acd1031f0deb47427a7dab87
1 /*
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
6 * packet compression.
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@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
29 * Inline functions
33 * Does TLS session need service?
35 static inline void
36 check_tls (struct context *c)
38 #if defined(USE_CRYPTO) && defined(USE_SSL)
39 void check_tls_dowork (struct context *c);
40 if (c->c2.tls_multi)
41 check_tls_dowork (c);
42 #endif
46 * TLS errors are fatal in TCP mode.
47 * Also check for --tls-exit trigger.
49 static inline void
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);
62 else
64 if (c->c2.tls_multi->n_hard_errors)
65 check_tls_errors_nco (c);
68 #endif
72 * Check for possible incoming configuration
73 * messages on the control channel.
75 static inline void
76 check_incoming_control_channel (struct context *c)
78 #if P2MP
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);
82 #endif
86 * Options like --up-delay need to be triggered by this function which
87 * checks for connection establishment.
89 static inline void
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?
100 static inline void
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?
111 static inline void
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);
121 #if P2MP
123 * Scheduled exit?
125 static inline void
126 check_scheduled_exit (struct context *c)
128 void check_scheduled_exit_dowork (struct context *c);
130 if (event_timeout_defined (&c->c2.scheduled_exit))
132 if (event_timeout_trigger (&c->c2.scheduled_exit, &c->c2.timeval, ETT_DEFAULT))
133 check_scheduled_exit_dowork (c);
136 #endif
139 * Should we write timer-triggered status file.
141 static inline void
142 check_status_file (struct context *c)
144 void check_status_file_dowork (struct context *c);
146 if (c->c1.status_output)
148 if (status_trigger_tv (c->c1.status_output, &c->c2.timeval))
149 check_status_file_dowork (c);
153 #ifdef ENABLE_FRAGMENT
155 * Should we deliver a datagram fragment to remote?
157 static inline void
158 check_fragment (struct context *c)
160 void check_fragment_dowork (struct context *c);
161 if (c->c2.fragment)
162 check_fragment_dowork (c);
164 #endif
166 #if P2MP
169 * see if we should send a push_request in response to --pull
171 static inline void
172 check_push_request (struct context *c)
174 void check_push_request_dowork (struct context *c);
175 if (event_timeout_trigger (&c->c2.push_request_interval, &c->c2.timeval, ETT_DEFAULT))
176 check_push_request_dowork (c);
179 #endif
181 #ifdef USE_CRYPTO
183 * Should we persist our anti-replay packet ID state to disk?
185 static inline void
186 check_packet_id_persist_flush (struct context *c)
188 if (packet_id_persist_enabled (&c->c1.pid_persist)
189 && event_timeout_trigger (&c->c2.packet_id_persist_interval, &c->c2.timeval, ETT_DEFAULT))
190 packet_id_persist_save (&c->c1.pid_persist);
192 #endif
195 * Set our wakeup to 0 seconds, so we will be rescheduled
196 * immediately.
198 static inline void
199 context_immediate_reschedule (struct context *c)
201 c->c2.timeval.tv_sec = 0; /* ZERO-TIMEOUT */
202 c->c2.timeval.tv_usec = 0;
205 static inline void
206 context_reschedule_sec (struct context *c, int sec)
208 if (sec < 0)
209 sec = 0;
210 if (sec < c->c2.timeval.tv_sec)
212 c->c2.timeval.tv_sec = sec;
213 c->c2.timeval.tv_usec = 0;
217 static inline struct link_socket_info *
218 get_link_socket_info (struct context *c)
220 if (c->c2.link_socket_info)
221 return c->c2.link_socket_info;
222 else
223 return &c->c2.link_socket->info;
226 static inline void
227 register_activity (struct context *c)
229 if (c->options.inactivity_timeout)
230 event_timeout_reset (&c->c2.inactivity_interval);
234 * Return the io_wait() flags appropriate for
235 * a point-to-point tunnel.
237 static inline unsigned int
238 p2p_iow_flags (const struct context *c)
240 unsigned int flags = (IOW_SHAPER|IOW_CHECK_RESIDUAL|IOW_FRAG|IOW_READ|IOW_WAIT_SIGNAL);
241 if (c->c2.to_link.len > 0)
242 flags |= IOW_TO_LINK;
243 if (c->c2.to_tun.len > 0)
244 flags |= IOW_TO_TUN;
245 return flags;
249 * This is the core I/O wait function, used for all I/O waits except
250 * for TCP in server mode.
252 static inline void
253 io_wait (struct context *c, const unsigned int flags)
255 void io_wait_dowork (struct context *c, const unsigned int flags);
257 if (c->c2.fast_io && (flags & (IOW_TO_TUN|IOW_TO_LINK|IOW_MBUF)))
259 /* fast path -- only for TUN/TAP/UDP writes */
260 unsigned int ret = 0;
261 if (flags & IOW_TO_TUN)
262 ret |= TUN_WRITE;
263 if (flags & (IOW_TO_LINK|IOW_MBUF))
264 ret |= SOCKET_WRITE;
265 c->c2.event_set_status = ret;
267 else
269 /* slow path */
270 io_wait_dowork (c, flags);
274 #define CONNECTION_ESTABLISHED(c) (get_link_socket_info(c)->connection_established)
276 #endif /* EVENT_INLINE_H */