PLCnext API Documentation 25.0.2.69
TlsSocket2.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Core/PimplPtr.hxx"
9#include "Arp/System/Core/event.hxx"
10#include "Arp/System/Commons/Net/Socket.hpp"
11#include "Arp/System/Commons/Net/TlsOptions.hpp"
12#include "Arp/System/Commons/Security/Certificate.hpp"
13#include "Arp/System/Commons/Net/TlsVerificationError.hpp"
14#include "Arp/System/Commons/Net/TlsAlertType.hpp"
15#include "Arp/System/Commons/Net/TlsAlertLevel.hpp"
16
17
18namespace Arp { namespace System { namespace Commons { namespace Net
19{
20
21class TlsContext;
22using TlsContextPtr = std::shared_ptr<TlsContext>;
23
31class ARP_CXX_SYMBOL_EXPORT TlsSocket2: private Loggable<TlsSocket2>
32{
33public: // Impl forward declaration
34 class Impl;
35
36public: // usings
37 using Ptr = std::shared_ptr<TlsSocket2>;
39
43 using VerifyHandler = delegate<void(bool&, TlsVerificationError&, int, const Certificate*)>;
45
46
47public: // construction/destruction/assignment
48 explicit TlsSocket2(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode);
49 TlsSocket2(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode, TlsOptions options);
50 TlsSocket2(const TlsSocket2& arg) = delete;
51 TlsSocket2(TlsSocket2&& arg)noexcept;
52 TlsSocket2(Socket::Ptr pSocket, TlsContextPtr pContext, bool isInitialized);
53 TlsSocket2& operator=(const TlsSocket2& arg) = delete;
56
57private: // Impl usings
58 using Pimpl = PimplPtr<Impl>;
59
60private:
61
62
63public: // operators
64
65public: // static operations
66
67public: // setter/getter operations
68 SocketType GetSocketType(void);
69 SocketDomain GetSocketDomain(void);
70 bool IsBlocking(void);
71 bool IsConnected(void);
72 bool IsTlsConnected(void);
73 IpAddress GetRemoteIpAddress(void);
74 int GetRemotePort(void);
75
76public: // operations
77 Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
78 SocketError Bind(const IpAddress& ip4Address, int port);
79 SocketError Bind2(const IpAddress& ip4Address, int& port);
80 SocketError Listen(size_t backlog);
81 SocketError Connect(const IpAddress& ip4Address, int port);
84 SocketError Close(void);
85 int Send(const void* pBuffer, size_t length, SocketError& error);
86 int Receive(void* pBuffer, size_t length, SocketError& error);
87 bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
88 int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
89
90 SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
91 SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t* optionLength);
92 SocketError SetOptionReuseAddress(bool enabled);
93 SocketError GetOptionReuseAddress(bool& enabled);
94 SocketError SetOptionKeepAlive(bool enabled);
95 SocketError GetOptionKeepAlive(bool& enabled);
96 SocketError SetOptionBroadcast(bool enabled);
97 SocketError GetOptionBroadcast(bool& enabled);
98 SocketError SetOptionNoDelay(bool enabled);
99 SocketError GetOptionNoDelay(bool& enabled);
100 SocketError SetOptionLinger(bool enable, size_t timeout);
101 SocketError GetOptionLinger(bool& enable, size_t& timeout);
102 SocketError SetOptionUserTimeout(size_t timeout_ms);
103 SocketError GetOptionUserTimeout(size_t& timeout_ms);
104 SocketError SetOptionKeepAliveIdleTime(int seconds);
105 SocketError GetOptionKeepAliveIdleTime(int& seconds);
106 SocketError SetOptionKeepAliveProbeInterval(int seconds);
107 SocketError GetOptionKeepAliveProbeInterval(int& seconds);
108 SocketError SetOptionKeepAliveProbeCount(int probeCount);
109 SocketError GetOptionKeepAliveProbeCount(int& probeCount);
110 SocketError SetOptionBlocking(bool enable);
111
112 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
113 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
114 void SetCipherList(const String& cipherList);
115 SocketError RenegotiateSession();
116
117 SocketError UpdateSessionKeys(bool requestUpdate = true);
118 SocketError GetPeerCertificate(Certificate& certificate);
119
120 SteadyTimePoint GetLastRenegotionTime(void);
121 SteadyTimePoint GetLastKeyUpdateTime(void);
122
123 void AddVerifyHandler(const VerifyHandler& handler);
124 void RemoveVerifyHandler(const VerifyHandler& handler);
125
126 void AddAlertReceivedHandler(const AlertHandler& handler);
127 void RemoveAlertReceivedHandler(const AlertHandler& handler);
128
129 void AddAlertSendHandler(const AlertHandler& handler);
130 void RemoveAlertSendHandler(const AlertHandler& handler);
131
132 static String GetTlsVerificationErrorString(const TlsVerificationError& error);
133
134public: // internal operations
135 Impl& GetImpl(void);
136 const Impl& GetImpl(void)const;
137
138private: // Impl fields
139 Pimpl pimpl;
140};
141
142}}}} // end of namespace Arp::System::Commons::Net
Adapter class to implement PImpl idiom.
Definition: PimplPtr.hxx:15
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Prototyping of delegate template.
Definition: delegate.hxx:14
Derive from this class to inherit logging functionality.
Definition: Loggable.hxx:28
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:32
TlsSocket2(TlsSocket2 &&arg) noexcept
Default move constructor.
~TlsSocket2(void)
Default destructor.
TlsSocket2 & operator=(TlsSocket2 &&arg) noexcept
Default move-assignment operator.
Class to handle x.509 certificates
Definition: Certificate.hpp:25
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: ChronoTypes.hpp:40
SteadyClock::time_point SteadyTimePoint
Represents the type of a monotonic clock time-point.
Definition: ChronoTypes.hpp:31
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: ChronoTypes.hpp:43
@ Shutdown
A request to send or receive data was disallowed because the socket had already been shut down in tha...
@ IsConnected
A connect request was made on an already connected socket.
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
@ Connect
Connect operation should be polled.
@ Accept
Accept operation should be polled.
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