PLCnext API Documentation 24.6.0.58
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
386
396
405
415
424
434
443
453
458
476 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
477
489 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
490
499 void SetCipherList(const String& cipherList);
500
510
511
524 SocketError UpdateSessionKeys(bool requestUpdate = true);
525
540 SocketError GetPeerCertificate(Certificate& certificate);
541
542
554
555
567
568
574 void AddVerifyHandler(const VerifyHandler& handler);
575
578 void RemoveVerifyHandler(const VerifyHandler& handler);
579
580
583 void AddAlertReceivedHandler(const AlertHandler& handler);
584
587 void RemoveAlertReceivedHandler(const AlertHandler& handler);
588
591 void AddAlertSendHandler(const AlertHandler& handler);
592
595 void RemoveAlertSendHandler(const AlertHandler& handler);
596
597 static String GetTlsVerificationErrorString(const TlsVerificationError& error);
598
599public: // internal operations
600 Impl& GetImpl(void);
601 const Impl& GetImpl(void)const;
602
603
604
605private: // Impl fields
606 Pimpl pimpl;
607};
608
609}}}} // 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 SetOptionKeepAliveProbeCount(int probeCount)
Sets the amount of probes to be sent, if the KeepAlive SocketOption is set to true.
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 SetOptionKeepAliveIdleTime(int seconds)
Sets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
SocketError GetOptionKeepAliveProbeCount(int &probeCount)
Gets the amount of probes to be sent, if the KeepAlive SocketOption is set to true....
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 GetOptionKeepAliveProbeInterval(int &seconds)
Gets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
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.
SocketError GetOptionKeepAliveIdleTime(int &seconds)
Gets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
bool IsBlocking(void)
Checks if the socket is in blocking mode.
SocketError SetOptionUserTimeout(size_t timeout_ms)
Sets the retransmission timeout of a socket. This only works on Linux.
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.
SocketError GetOptionUserTimeout(size_t &timeout_ms)
Gets the retransmission timeout of a socket. This only works on Linux. For more information see Arp::...
int Send(const void *pBuffer, size_t length, SocketError &error)
Transmit data over the socket that is in a connected state.
SocketError SetOptionKeepAliveProbeInterval(int seconds)
Sets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
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