OpenVPN 3 Core Library
Loading...
Searching...
No Matches
relsend.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Sender side of reliability layer
13
14#ifndef OPENVPN_RELIABLE_RELSEND_H
15#define OPENVPN_RELIABLE_RELSEND_H
16
20#include <openvpn/time/time.hpp>
22
23namespace openvpn {
24
25template <typename PACKET>
27{
28 public:
30
31 class Message : public ReliableMessageBase<PACKET>
32 {
34 using ReliableMessageBase<PACKET>::defined;
35
36 public:
37 bool ready_retransmit(const Time &now) const
38 {
39 return defined() && now >= retransmit_at_;
40 }
41
42 Time::Duration until_retransmit(const Time &now) const
43 {
44 Time::Duration ret;
45 if (now < retransmit_at_)
46 ret = retransmit_at_ - now;
47 return ret;
48 }
49
50 void reset_retransmit(const Time &now, const Time::Duration &tls_timeout)
51 {
52 retransmit_at_ = now + tls_timeout;
53 }
54
55 private:
57 };
58
60 : next(0)
61 {
62 }
63 ReliableSendTemplate(const id_t span, id_t start_at = 0)
64 {
65 init(span, start_at);
66 }
67
68 void init(const id_t span, id_t start_at = 0)
69 {
70 next = start_at;
71 window_.init(next, span);
72 }
73
74 // Return the id that the object at the head of the queue
75 // would have (even if it isn't defined yet).
76 id_t head_id() const
77 {
78 return window_.head_id();
79 }
80
81 // Return the ID of one past the end of the window
82 id_t tail_id() const
83 {
84 return window_.tail_id();
85 }
86
87 // Return the window size
88 id_t span() const
89 {
90 return window_.span();
91 }
92
93 // Return a reference to M object at id, throw exception
94 // if id is not in current window
96 {
97 return window_.ref_by_id(id);
98 }
99
100 // Return the shortest duration for any pending retransmissions
101 Time::Duration until_retransmit(const Time &now)
102 {
103 Time::Duration ret = Time::Duration::infinite();
104 for (id_t i = head_id(); i < tail_id(); ++i)
105 {
106 const Message &msg = ref_by_id(i);
107 if (msg.defined())
108 {
109 Time::Duration ut = msg.until_retransmit(now);
110 if (ut < ret)
111 ret = ut;
112 }
113 }
114 return ret;
115 }
116
117 // Return number of unacknowleged packets in send queue
118 unsigned int n_unacked()
119 {
120 unsigned int ret = 0;
121 for (id_t i = head_id(); i < tail_id(); ++i)
122 {
123 if (ref_by_id(i).defined())
124 ++ret;
125 }
126 return ret;
127 }
128
129 // Return a fresh Message object that can be used to
130 // construct the next packet in the sequence. Don't call
131 // unless ready() returns true.
132 Message &send(const Time &now, const Time::Duration &tls_timeout)
133 {
134 Message &msg = window_.ref_by_id(next);
135 msg.id_ = next++;
136 msg.reset_retransmit(now, tls_timeout);
137 return msg;
138 }
139
140 // Return true if send queue is ready to receive another packet
141 bool ready() const
142 {
143 return window_.in_window(next);
144 }
145
146 // Remove a message from send queue that has been acknowledged
147 void ack(const id_t id)
148 {
149 window_.rm_by_id(id);
150 }
151
152 private:
155};
156
157} // namespace openvpn
158
159#endif // OPENVPN_RELIABLE_RELSEND_H
Time::Duration until_retransmit(const Time &now) const
Definition relsend.hpp:42
void reset_retransmit(const Time &now, const Time::Duration &tls_timeout)
Definition relsend.hpp:50
bool ready_retransmit(const Time &now) const
Definition relsend.hpp:37
void ack(const id_t id)
Definition relsend.hpp:147
Message & ref_by_id(const id_t id)
Definition relsend.hpp:95
ReliableSendTemplate(const id_t span, id_t start_at=0)
Definition relsend.hpp:63
void init(const id_t span, id_t start_at=0)
Definition relsend.hpp:68
Message & send(const Time &now, const Time::Duration &tls_timeout)
Definition relsend.hpp:132
MessageWindow< Message, id_t > window_
Definition relsend.hpp:154
Time::Duration until_retransmit(const Time &now)
Definition relsend.hpp:101
static TimeType infinite()
Definition time.hpp:256
std::uint32_t id_t
Definition relcommon.hpp:22
std::string ret
#define msg(flags,...)