OpenVPN
probe.c
Go to the documentation of this file.
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.
7 *
8 * Copyright (C) 2002-2026 OpenVPN 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "probe.h"
30#include "control_msg.h"
31
32bool
33probe_request_write(struct buffer *buf, const struct probe_request *r)
34{
36 && buf_write_u64(buf, r->request_id) && buf_write_u64(buf, r->timestamp)
37 && buf_write_u32(buf, r->flags);
38}
39
40bool
42{
43 /* One bounds check covers the whole value: every field read below then fits
44 * by construction, PROBE_REQUEST_LEN being the sum of their sizes, so none
45 * of them needs its own error handling. Trailing bytes this version does
46 * not understand are simply left unread. */
47 if (buf_len(buf) < PROBE_REQUEST_LEN)
48 {
49 return false;
50 }
51 r->request_id = buf_read_u64(buf, NULL);
52 r->timestamp = buf_read_u64(buf, NULL);
53 r->flags = buf_read_u32(buf, NULL);
54 return true;
55}
56
57bool
58probe_reply_write(struct buffer *buf, const struct probe_reply *r)
59{
61 && buf_write_u64(buf, r->request_id) && buf_write_u16(buf, r->priority)
63 && buf_write_u16(buf, r->connect_lifetime) && buf_write_u16(buf, r->flags);
64}
65
66bool
67probe_reply_read(struct buffer *buf, struct probe_reply *r)
68{
69 /* One bounds check for the whole value, as in probe_request_read(). */
70 if (buf_len(buf) < PROBE_REPLY_LEN)
71 {
72 return false;
73 }
74 r->request_id = buf_read_u64(buf, NULL);
75 r->priority = (uint16_t)buf_read_u16(buf);
76 r->weight = (uint16_t)buf_read_u16(buf);
77 r->max_latency_diff = (uint16_t)buf_read_u16(buf);
78 r->connect_lifetime = (uint16_t)buf_read_u16(buf);
79 r->flags = (uint16_t)buf_read_u16(buf);
80
81 /* The other addresses of the remote cannot be both the same server and
82 * distinct ones. And LIFETIME_SHARED extends the connect_lifetime to those
83 * other addresses, which says nothing unless ADDRS_EQUAL has declared them
84 * the same server to begin with. */
86 {
87 return false;
88 }
90 {
91 return false;
92 }
93 return true;
94}
static bool buf_write_u16(struct buffer *dest, uint16_t data)
Append a uint16_t to a buffer in network byte order.
Definition buffer.h:1302
static bool buf_write_u32(struct buffer *dest, uint32_t data)
Append a uint32_t to a buffer in network byte order.
Definition buffer.h:1317
static int buf_read_u16(struct buffer *buf)
Read and consume a uint16_t from the front of a buffer.
Definition buffer.h:1516
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:462
static uint64_t buf_read_u64(struct buffer *buf, bool *good)
Read and consume a uint64_t from the front of a buffer.
Definition buffer.h:1571
static uint32_t buf_read_u32(struct buffer *buf, bool *good)
Read and consume a uint32_t from the front of a buffer.
Definition buffer.h:1538
static bool buf_write_u64(struct buffer *dest, uint64_t data)
Append a uint64_t to a buffer in network byte order.
Definition buffer.h:1332
bool ctrl_msg_tlv_write_header(struct buffer *buf, uint16_t type, bool optional, uint16_t value_len)
Write a TLV header (type + optional flag + value length) to buf.
Definition control_msg.c:32
TLV framing shared by the TLV-based control messages of the wire protocol.
#define TLV_TYPE_PROBE_REQUEST
Definition control_msg.h:44
#define TLV_TYPE_PROBE_REPLY
Definition control_msg.h:45
bool probe_reply_write(struct buffer *buf, const struct probe_reply *r)
Write a complete probe reply TLV, header and value, to buf.
Definition probe.c:58
bool probe_request_read(struct buffer *buf, struct probe_request *r)
Read a probe request TLV value from buf.
Definition probe.c:41
bool probe_request_write(struct buffer *buf, const struct probe_request *r)
Write a complete probe request TLV, header and value, to buf.
Definition probe.c:33
bool probe_reply_read(struct buffer *buf, struct probe_reply *r)
Read a probe reply TLV value from buf.
Definition probe.c:67
Encoding and decoding of the server probe TLVs.
#define PROBE_REPLY_FLAG_LIFETIME_SHARED
the connect_lifetime holds against those other addresses too, so a reply from one of them can skip th...
Definition probe.h:56
#define PROBE_REPLY_FLAG_ADDRS_EQUAL
the other addresses of this remote are the same server, no need to probe them
Definition probe.h:52
#define PROBE_REPLY_LEN
request_id (u64), then priority, weight, max_latency_diff, connect_lifetime and flags (u16 each)
Definition probe.h:98
#define PROBE_REQUEST_LEN
request_id (u64), timestamp (u64), flags (u32)
Definition probe.h:94
#define PROBE_REPLY_FLAG_PROBE_MORE
the other addresses of this remote are distinct servers, so probe them too
Definition probe.h:58
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
probe reply TLV, a server's answer to a probe request
Definition probe.h:74
uint16_t priority
DNS SRV style priority, lower is preferred.
Definition probe.h:76
uint16_t connect_lifetime
For how many seconds, counted from the moment the client sent its own probe request,...
Definition probe.h:84
uint16_t weight
DNS SRV style weight.
Definition probe.h:77
uint64_t request_id
echoes the request_id of the request
Definition probe.h:75
uint16_t max_latency_diff
candidate band in ms around the fastest server
Definition probe.h:78
uint16_t flags
PROBE_REPLY_FLAG_*.
Definition probe.h:85
probe request TLV, sent by a client to probe a server
Definition probe.h:62
uint32_t flags
PROBE_REQUEST_FLAG_*.
Definition probe.h:69
uint64_t timestamp
The client clock as a UNIX timestamp in seconds.
Definition probe.h:68
uint64_t request_id
identifies this probe, echoed in the reply
Definition probe.h:63