OpenVPN 3 Core Library
Loading...
Searching...
No Matches
bufread.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#ifndef OPENVPN_BUFFER_BUFREAD_H
13#define OPENVPN_BUFFER_BUFREAD_H
14
15#include <unistd.h>
16#include <errno.h>
17
18#include <string>
19#include <cstring>
20
25
26namespace openvpn {
27OPENVPN_EXCEPTION(buf_read_error);
28
29inline bool buf_read(const int fd, Buffer &buf, const std::string &title)
30{
31 const ssize_t status = ::read(fd, buf.data_end(), buf.remaining(0));
32 if (status < 0)
33 {
34 const int eno = errno;
35 OPENVPN_THROW(buf_read_error, "on " << title << " : " << strerror_str(eno));
36 }
37 else if (!status)
38 return false;
39 buf.inc_size(status);
40 return true;
41}
42
43inline BufferList buf_read(const int fd, const std::string &title)
44{
45 BufferList buflist;
46 while (true)
47 {
48 BufferAllocated buf(1024, 0);
49 if (!buf_read(fd, buf, title))
50 break;
51 buflist.put_consume(buf);
52 }
53 return buflist;
54}
55} // namespace openvpn
56
57#endif
T * data_end()
Get a mutable pointer to the end of the array.
Definition buffer.hpp:1439
void inc_size(const size_t delta)
Increment the size of the array (usually used in a similar context to set_size such as after mutable_...
Definition buffer.hpp:1375
size_t remaining(const size_t tailroom=0) const
Return the number of additional T objects that can be added before capacity is reached (without consi...
Definition buffer.hpp:1451
#define OPENVPN_EXCEPTION(C)
#define OPENVPN_THROW(exc, stuff)
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
bool buf_read(const int fd, Buffer &buf, const std::string &title)
Definition bufread.hpp:29
std::string strerror_str(const int errnum)
Definition strerror.hpp:21
void put_consume(BufferAllocated &buf, const size_t tailroom=0)
Definition buflist.hpp:86