PLCnext API Documentation 23.6.0.37
TlsSocket2.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/PimplPtr.hxx"
9#include "Arp/System/Commons/Net/Socket.hpp"
10#include "Arp/System/Commons/Net/TlsOptions.hpp"
11#include "Arp/System/Commons/Security/Certificate.hpp"
12#include "Arp/System/Commons/Net/TlsVerificationError.hpp"
13#include "Arp/System/Commons/Net/TlsAlertType.hpp"
14#include "Arp/System/Commons/Net/TlsAlertLevel.hpp"
15
16
17namespace Arp { namespace System { namespace Commons { namespace Net
18{
19
20
21
22
23class TlsContext;
24using TlsContextPtr = std::shared_ptr<TlsContext>;
25
33class ARP_CXX_SYMBOL_EXPORT TlsSocket2: private Loggable<TlsSocket2>
34{
35public: // Impl forward declaration
36 class Impl;
37
38public: // usings
39 using Ptr = std::shared_ptr<TlsSocket2>;
41
45 using VerifyHandler = delegate<void(bool&, TlsVerificationError&, int, const Certificate*)>;
46 using AlertHandler = delegate<void(TlsAlertType, TlsAlertLevel)>;
47
48
49public: // construction/destruction/assignment
50 explicit TlsSocket2(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode);
51 TlsSocket2(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode, TlsOptions options);
52 TlsSocket2(const TlsSocket2& arg) = delete;
53 TlsSocket2(TlsSocket2&& arg)noexcept;
54 TlsSocket2(Socket::Ptr pSocket, TlsContextPtr pContext, bool isInitialized);
55 TlsSocket2& operator=(const TlsSocket2& arg) = delete;
56 TlsSocket2& operator=(TlsSocket2&& arg)noexcept;
57 ~TlsSocket2(void);
58
59private: // Impl usings
60 using Pimpl = PimplPtr<Impl>;
61
62private:
63
64
65public: // operators
66
67public: // static operations
68
69public: // setter/getter operations
74
79
83 bool IsBlocking(void);
84
88 bool IsConnected(void);
89
93 bool IsTlsConnected(void);
94
99
105 int GetRemotePort(void);
106
107public: // operations
125 Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
126
138 SocketError Bind(const IpAddress& ip4Address, int port);
139
154 SocketError Bind2(const IpAddress& ip4Address, int& port);
155
167 SocketError Listen(size_t backlog);
168
178 SocketError Connect(const IpAddress& ip4Address, int port);
179
184
190
197
205 int Send(const void* pBuffer, size_t length, SocketError& error);
206
214 int Receive(void* pBuffer, size_t length, SocketError& error);
215
224 bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
225
239 int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
240
248 SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
249
258 SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t* optionLength);
259
271
280
292
301
311
320
331
340
356 SocketError SetOptionLinger(bool enable, size_t timeout);
357
364 SocketError GetOptionLinger(bool& enable, size_t& timeout);
365
370
388 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
389
401 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
402
411 void SetCipherList(const String& cipherList);
412
422
423
436 SocketError UpdateSessionKeys(bool requestUpdate = true);
437
452 SocketError GetPeerCertificate(Certificate& certificate);
453
454
466
467
479
480
486 void AddVerifyHandler(const VerifyHandler& handler);
487
490 void RemoveVerifyHandler(const VerifyHandler& handler);
491
492
495 void AddAlertReceivedHandler(const AlertHandler& handler);
496
499 void RemoveAlertReceivedHandler(const AlertHandler& handler);
500
503 void AddAlertSendHandler(const AlertHandler& handler);
504
507 void RemoveAlertSendHandler(const AlertHandler& handler);
508
509 static String GetTlsVerificationErrorString(const TlsVerificationError& error);
510
511public: // internal operations
512 Impl& GetImpl(void);
513 const Impl& GetImpl(void)const;
514
515
516
517private: // Impl fields
518 Pimpl pimpl;
519};
520
521}}}} // end of namespace Arp::System::Commons::Net
Unified representation for ip address schemes.
Definition: IpAddress.hpp:14
std::shared_ptr< Socket > Ptr
Contextual definition of pointer type.
Definition: Socket.hpp:122
Interface to realize TLS Connection over TCP
Definition: TlsSocket2.hpp:34
void RemoveAlertSendHandler(const AlertHandler &handler)
Removes a AlertSend handler again
SocketError GetSocketOption(SocketOptionName optionName, void *optionValue, size_t *optionLength)
Returns current value of queried socket option.
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
SocketError InitClient(const String &trustStoreName, const String &identityStoreName, const String &hostName)
SocketError SetOptionNoDelay(bool enabled)
Enables/Disables no-delay for this socket.
int Poll(PollMode mode, Milliseconds timeout, SocketError &error)
Checks if an i/o operation can be processed without blocking.
Ptr Accept(IpAddress &ip4address, int &port, SocketError &error)
Accepts a pending connection request.
bool Select(SelectMode mode, Microseconds timeout, SocketError &error)
Checks if an i/o operation can be performed without blocking the calling thread.
SocketError GetOptionBroadcast(bool &enabled)
Checks if broadcast is enabled.
SocketError Connect(const IpAddress &ip4Address, int port)
Tries to connect with a remote socket.
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
int GetRemotePort(void)
If this socket is connected this method returns the port of the connection.
SocketError SetOptionReuseAddress(bool enabled)
Enables/Disables the reuse-address option for this socket.
delegate< void(bool &, TlsVerificationError &, int, const Certificate *)> VerifyHandler
Used a for AddVerifyHandler Note: certificate is optional and can be nullptr, must be checked before ...
Definition: TlsSocket2.hpp:45
SocketError UpdateSessionKeys(bool requestUpdate=true)
IpAddress GetRemoteIpAddress(void)
Checks if this socket is connected with a remote peer.
SocketError SetOptionKeepAlive(bool enabled)
Enables/Disables the keep-alive option for this socket.
void AddAlertReceivedHandler(const AlertHandler &handler)
Adds a handler which is called when a TLS alert is received from the peer
SocketError Shutdown(ShutdownMode mode)
Shuts down a full-duplex connection.
void RemoveVerifyHandler(const VerifyHandler &handler)
Removes a VerifyHandler again
SocketError Bind2(const IpAddress &ip4Address, int &port)
Binds the socket to a specific address and port combination.
bool IsTlsConnected(void)
Checks if a TLS connection is established with a remote peer.
SocketError GetOptionKeepAlive(bool &enabled)
Checks if keep-alive is enabled.
SocketError GetOptionLinger(bool &enable, size_t &timeout)
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOp...
SocketError Close(void)
Closes the socket. This ends all communication on the socket.
bool IsBlocking(void)
Checks if the socket is in blocking mode.
void AddVerifyHandler(const VerifyHandler &handler)
Adds a handler which is called during certificate validation ot the peer certificate
SocketError Bind(const IpAddress &ip4Address, int port)
Binds the socket to a specific address and port combination.
SocketError SetOptionLinger(bool enable, size_t timeout)
Sets the amount of time a socket resides in TIME_WAIT state after active close.
SocketError SetSocketOption(SocketOptionName optionName, const void *optionValue, size_t optionLength)
Sets a single option on the socket.
void SetCipherList(const String &cipherList)
SocketDomain GetSocketDomain(void)
Returns the type of the socket.
SocketError Listen(size_t backlog)
Marks this socket as a passive socket that accepts incoming connection requests.
SteadyTimePoint GetLastKeyUpdateTime(void)
Queries the last timepoint when a TLS session resumption (TLS 1.2) or key update (TLS 1....
SocketError InitServer(const String &identityStoreName, const String &trustStoreName="")
SocketType GetSocketType(void)
Returns the type of the socket.
SocketError Shutdown(void)
Shuts down a full-duplex connection.
SocketError GetOptionNoDelay(bool &enabled)
Checks if no-delay is enabled.
SocketError GetOptionReuseAddress(bool &enabled)
Checks if reuse-address is enabled.
void AddAlertSendHandler(const AlertHandler &handler)
Adds a handler which is called when a TLS alert is send to the peer
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
bool IsConnected(void)
Checks if the socket is in blocking mode.
int Send(const void *pBuffer, size_t length, SocketError &error)
Transmit data over the socket that is in a connected state.
SteadyTimePoint GetLastRenegotionTime(void)
Queries the last timepoint when a TLS renegotiation was performed
void RemoveAlertReceivedHandler(const AlertHandler &handler)
Removes a AlertReceived handler again
Class to handle x.509 certificates
Definition: Certificate.hpp:25
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: TypeSystem.h:52
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: TypeSystem.h:49
SteadyClock::time_point SteadyTimePoint
Represents the type of a monotonic clock timepoint.
Definition: TypeSystem.h:40
@ System
System components used by the System, Device, Plc or Io domains.
TlsVerificationError
This enum list possible error codes which further specifies an certificate verification error....
Definition: TlsVerificationError.hpp:18
PollMode
This enum is used to specifiy the poll mode of the <cref name="Socket::Poll" > operation.
Definition: PollMode.hpp:15
SelectMode
Modes for Select call to check different data channels.
Definition: SelectMode.hpp:14
TlsAlertType
This enum defines the TLS alert types
Definition: TlsAlertType.hpp:16
SocketDomain
Supported communication domains, selecting the protocol for communication.
Definition: SocketDomain.hpp:14
SocketType
Enumeration of supported socket types.
Definition: SocketType.hpp:14
ShutdownMode
This enum is used to specifiy the shutdown mode of the <cref name="Socket::Shutdown(ShutdownMode)" > ...
Definition: ShutdownMode.hpp:15
SocketError
Possible error codes for socket operation results.
Definition: SocketError.hpp:15
TlsAlertLevel
This enum represents avaliable TLS alert Levels
Definition: TlsAlertLevel.hpp:16
TlsOptions
This enum is used to select different options for the TlsSocket class
Definition: TlsOptions.hpp:25
SocketOptionName
Specifies socket options to be set by the application. Copied from Eclr Socket Adaption
Definition: SocketOptionName.hpp:17
SocketBlockingMode
Supported blocking modes.
Definition: Socket.hpp:28
Root namespace for the PLCnext API