Update to OpenVPN 2.1rc17
[tomato.git] / release / src / router / openvpn / reliable.h
blob6a62c3a7326225999e43576f2997442cb8b2f005
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.
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
26 * This routines implement a reliability layer on top of UDP,
27 * so that TLS can be run over UDP.
30 #if defined(USE_CRYPTO) && defined(USE_SSL)
32 #ifndef RELIABLE_H
33 #define RELIABLE_H
35 #include "basic.h"
36 #include "buffer.h"
37 #include "packet_id.h"
38 #include "session_id.h"
39 #include "mtu.h"
41 /* #define EXPONENTIAL_BACKOFF */
43 #define RELIABLE_ACK_SIZE 8
45 struct reliable_ack
47 int len;
48 packet_id_type packet_id[RELIABLE_ACK_SIZE];
51 /* no active buffers? */
52 static inline bool
53 reliable_ack_empty (struct reliable_ack *ack)
55 return !ack->len;
58 /* get a packet_id from buf */
59 bool reliable_ack_read_packet_id (struct buffer *buf, packet_id_type *pid);
61 /* acknowledge a packet_id by adding it to a struct reliable_ack */
62 bool reliable_ack_acknowledge_packet_id (struct reliable_ack *ack, packet_id_type pid);
64 /* read a packet ID acknowledgement record from buf */
65 bool reliable_ack_read (struct reliable_ack *ack,
66 struct buffer *buf, const struct session_id *sid);
68 /* write a packet ID acknowledgement record to buf */
69 bool reliable_ack_write (struct reliable_ack *ack,
70 struct buffer *buf,
71 const struct session_id *sid, int max, bool prepend);
73 /* print a reliable ACK record coming off the wire */
74 const char *reliable_ack_print (struct buffer *buf, bool verbose, struct gc_arena *gc);
76 /* add to extra_frame the maximum number of bytes we will need for reliable_ack_write */
77 void reliable_ack_adjust_frame_parameters (struct frame* frame, int max);
79 void reliable_ack_debug_print (const struct reliable_ack *ack, char *desc);
81 #define RELIABLE_CAPACITY 8
83 struct reliable_entry
85 bool active;
86 interval_t timeout;
87 time_t next_try;
88 packet_id_type packet_id;
89 int opcode;
90 struct buffer buf;
93 struct reliable
95 int size;
96 interval_t initial_timeout;
97 packet_id_type packet_id;
98 int offset;
99 bool hold; /* don't xmit until reliable_schedule_now is called */
100 struct reliable_entry array[RELIABLE_CAPACITY];
103 void reliable_debug_print (const struct reliable *rel, char *desc);
105 /* set sending timeout (after this time we send again until ACK) */
106 static inline void
107 reliable_set_timeout (struct reliable *rel, interval_t timeout)
109 rel->initial_timeout = timeout;
112 void reliable_init (struct reliable *rel, int buf_size, int offset, int array_size, bool hold);
114 void reliable_free (struct reliable *rel);
116 /* no active buffers? */
117 bool reliable_empty (const struct reliable *rel);
119 /* in how many seconds should we wake up to check for timeout */
120 interval_t reliable_send_timeout (const struct reliable *rel);
122 /* del acknowledged items from send buf */
123 void reliable_send_purge (struct reliable *rel, struct reliable_ack *ack);
125 /* true if at least one free buffer available */
126 bool reliable_can_get (const struct reliable *rel);
128 /* make sure that incoming packet ID isn't a replay */
129 bool reliable_not_replay (const struct reliable *rel, packet_id_type id);
131 /* make sure that incoming packet ID won't deadlock the receive buffer */
132 bool reliable_wont_break_sequentiality (const struct reliable *rel, packet_id_type id);
134 /* grab a free buffer */
135 struct buffer *reliable_get_buf (struct reliable *rel);
137 /* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */
138 struct buffer *reliable_get_buf_output_sequenced (struct reliable *rel);
140 /* get active buffer for next sequentially increasing key ID */
141 struct buffer *reliable_get_buf_sequenced (struct reliable *rel);
143 /* return true if reliable_send would return a non-NULL result */
144 bool reliable_can_send (const struct reliable *rel);
146 /* return next buffer to send to remote */
147 struct buffer *reliable_send (struct reliable *rel, int *opcode);
149 /* schedule all pending packets for immediate retransmit */
150 void reliable_schedule_now (struct reliable *rel);
152 /* enable an incoming buffer previously returned by a get function as active */
153 void reliable_mark_active_incoming (struct reliable *rel, struct buffer *buf,
154 packet_id_type pid, int opcode);
156 /* enable an outgoing buffer previously returned by a get function as active. */
157 void reliable_mark_active_outgoing (struct reliable *rel, struct buffer *buf, int opcode);
159 /* delete a buffer previously activated by reliable_mark_active() */
160 void reliable_mark_deleted (struct reliable *rel, struct buffer *buf, bool inc_pid);
162 #endif /* RELIABLE_H */
163 #endif /* USE_CRYPTO && USE_SSL */