Botan 3.9.0
Crypto and TLS for C&
socket.h
Go to the documentation of this file.
1/*
2* OS specific utility functions
3* (C) 2015,2016,2017 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_SOCKET_H_
9#define BOTAN_SOCKET_H_
10
11#include <botan/types.h>
12#include <chrono>
13#include <memory>
14#include <span>
15#include <string_view>
16
17namespace Botan::OS {
18
19/*
20* This header is internal (not installed) and these functions are not
21* intended to be called by applications. However they are given public
22* visibility (using BOTAN_TEST_API macro) for the tests. This also probably
23* allows them to be overridden by the application on ELF systems, but
24* this hasn't been tested.
25*/
26
27/**
28* A wrapper around a simple blocking TCP socket
29*/
30class BOTAN_TEST_API Socket /* NOLINT(*-special-member-functions) */ {
31 public:
32 /**
33 * The socket will be closed upon destruction
34 */
35 virtual ~Socket() = default;
36
37 /**
38 * Write to the socket. Blocks until all bytes sent.
39 * Throws on error.
40 */
41 virtual void write(std::span<const uint8_t> bytes) = 0;
42
43 /**
44 * Reads up to len bytes, returns bytes written to buf.
45 * Returns 0 on EOF. Throws on error.
46 */
47 virtual size_t read(uint8_t buf[], size_t len) = 0;
48};
49
50/**
51* Open up a socket. Will throw on error. Returns null if sockets are
52* not available on this platform.
53*/
54std::unique_ptr<Socket> BOTAN_TEST_API open_socket(std::string_view hostname,
55 std::string_view service,
56 std::chrono::milliseconds timeout);
57
58} // namespace Botan::OS
59
60#endif
#define BOTAN_TEST_API
Definition api.h:41
virtual void write(std::span< const uint8_t > bytes)=0
virtual size_t read(uint8_t buf[], size_t len)=0
virtual ~Socket()=default
std::unique_ptr< Socket > BOTAN_TEST_API open_socket(std::string_view hostname, std::string_view service, std::chrono::milliseconds timeout)
Definition socket.cpp:355