Cuberite
A lightweight, fast and extensible game server for Minecraft
LuaTCPLink.cpp
Go to the documentation of this file.
1 
2 // LuaTCPLink.cpp
3 
4 // Implements the cLuaTCPLink class representing a Lua wrapper for the cTCPLink class and the callbacks it needs
5 
6 #include "Globals.h"
7 #include "LuaTCPLink.h"
8 #include "LuaServerHandle.h"
9 #include "../mbedTLS++/X509Cert.h"
10 #include "../mbedTLS++/CryptoKey.h"
11 
12 
13 
14 
15 
17  m_Callbacks(std::move(a_Callbacks))
18 {
19 }
20 
21 
22 
23 
24 
26  m_Callbacks(std::move(a_Callbacks)),
27  m_Server(std::move(a_ServerHandle))
28 {
29 }
30 
31 
32 
33 
34 
36 {
37  // If the link is still open, close it:
38  auto link = m_Link;
39  if (link != nullptr)
40  {
41  link->Close();
42  }
43 
44  Terminated();
45 }
46 
47 
48 
49 
50 
51 bool cLuaTCPLink::Send(const AString & a_Data)
52 {
53  // Safely grab a copy of the link:
54  auto link = m_Link;
55  if (link == nullptr)
56  {
57  return false;
58  }
59 
60  // Send the data:
61  return link->Send(a_Data);
62 }
63 
64 
65 
66 
67 
69 {
70  // Safely grab a copy of the link:
71  auto link = m_Link;
72  if (link == nullptr)
73  {
74  return "";
75  }
76 
77  // Get the IP address:
78  return link->GetLocalIP();
79 }
80 
81 
82 
83 
84 
86 {
87  // Safely grab a copy of the link:
88  auto link = m_Link;
89  if (link == nullptr)
90  {
91  return 0;
92  }
93 
94  // Get the port:
95  return link->GetLocalPort();
96 }
97 
98 
99 
100 
101 
103 {
104  // Safely grab a copy of the link:
105  cTCPLinkPtr link = m_Link;
106  if (link == nullptr)
107  {
108  return "";
109  }
110 
111  // Get the IP address:
112  return link->GetRemoteIP();
113 }
114 
115 
116 
117 
118 
120 {
121  // Safely grab a copy of the link:
122  cTCPLinkPtr link = m_Link;
123  if (link == nullptr)
124  {
125  return 0;
126  }
127 
128  // Get the port:
129  return link->GetRemotePort();
130 }
131 
132 
133 
134 
135 
137 {
138  // Safely grab a copy of the link and shut it down:
139  cTCPLinkPtr link = m_Link;
140  if (link != nullptr)
141  {
142  link->Shutdown();
143  }
144 }
145 
146 
147 
148 
149 
151 {
152  // If the link is still open, close it:
153  cTCPLinkPtr link = m_Link;
154  if (link != nullptr)
155  {
156  link->Close();
157  }
158 
159  Terminated();
160 }
161 
162 
163 
164 
165 
167  const AString & a_OwnCertData,
168  const AString & a_OwnPrivKeyData,
169  const AString & a_OwnPrivKeyPassword
170 )
171 {
172  auto link = m_Link;
173  if (link != nullptr)
174  {
175  cX509CertPtr ownCert;
176  if (!a_OwnCertData.empty())
177  {
178  ownCert = std::make_shared<cX509Cert>();
179  auto res = ownCert->Parse(a_OwnCertData.data(), a_OwnCertData.size());
180  if (res != 0)
181  {
182  return Printf("Cannot parse client certificate: -0x%x", res);
183  }
184  }
185  cCryptoKeyPtr ownPrivKey;
186  if (!a_OwnPrivKeyData.empty())
187  {
188  ownPrivKey = std::make_shared<cCryptoKey>();
189  auto res = ownPrivKey->ParsePrivate(a_OwnPrivKeyData.data(), a_OwnPrivKeyData.size(), a_OwnPrivKeyPassword);
190  if (res != 0)
191  {
192  return Printf("Cannot parse client private key: -0x%x", res);
193  }
194  }
195  return link->StartTLSClient(ownCert, ownPrivKey);
196  }
197  return "";
198 }
199 
200 
201 
202 
203 
205  const AString & a_OwnCertData,
206  const AString & a_OwnPrivKeyData,
207  const AString & a_OwnPrivKeyPassword,
208  const AString & a_StartTLSData
209 )
210 {
211  auto link = m_Link;
212  if (link != nullptr)
213  {
214  // Create the peer cert:
215  auto OwnCert = std::make_shared<cX509Cert>();
216  int res = OwnCert->Parse(a_OwnCertData.data(), a_OwnCertData.size());
217  if (res != 0)
218  {
219  return Printf("Cannot parse server certificate: -0x%x", res);
220  }
221  auto OwnPrivKey = std::make_shared<cCryptoKey>();
222  res = OwnPrivKey->ParsePrivate(a_OwnPrivKeyData.data(), a_OwnPrivKeyData.size(), a_OwnPrivKeyPassword);
223  if (res != 0)
224  {
225  return Printf("Cannot parse server private key: -0x%x", res);
226  }
227 
228  return link->StartTLSServer(OwnCert, OwnPrivKey, a_StartTLSData);
229  }
230  return "";
231 }
232 
233 
234 
235 
236 
238 {
239  // Disable the callbacks:
240  if (m_Callbacks->IsValid())
241  {
242  m_Callbacks->Clear();
243  }
244 
245  // If the managing server is still alive, let it know we're terminating:
246  auto Server = m_Server.lock();
247  if (Server != nullptr)
248  {
249  Server->RemoveLink(this);
250  }
251 
252  // If the link is still open, close it:
253  {
254  auto link= m_Link;
255  if (link != nullptr)
256  {
257  link->Close();
258  m_Link.reset();
259  }
260  }
261 }
262 
263 
264 
265 
266 
267 void cLuaTCPLink::ReceivedCleartextData(const char * a_Data, size_t a_NumBytes)
268 {
269  // Call the callback:
270  m_Callbacks->CallTableFn("OnReceivedData", this, AString(a_Data, a_NumBytes));
271 }
272 
273 
274 
275 
276 
278 {
279  // Call the callback:
280  m_Callbacks->CallTableFn("OnConnected", this);
281 }
282 
283 
284 
285 
286 
287 void cLuaTCPLink::OnError(int a_ErrorCode, const AString & a_ErrorMsg)
288 {
289  // Call the callback:
290  m_Callbacks->CallTableFn("OnError", this, a_ErrorCode, a_ErrorMsg);
291 
292  // Terminate all processing on the link:
293  Terminated();
294 }
295 
296 
297 
298 
299 
301 {
302  // Store the cTCPLink for later use:
303  m_Link = a_Link;
304 }
305 
306 
307 
308 
309 
310 void cLuaTCPLink::OnReceivedData(const char * a_Data, size_t a_Length)
311 {
312  // Call the callback:
313  m_Callbacks->CallTableFn("OnReceivedData", this, AString(a_Data, a_Length));
314 }
315 
316 
317 
318 
319 
321 {
322  // Call the callback:
323  m_Callbacks->CallTableFn("OnRemoteClosed", this);
324 
325  // Terminate all processing on the link:
326  Terminated();
327 }
328 
329 
330 
331 
332 
virtual void OnReceivedData(const char *a_Data, size_t a_Length) override
Called when there&#39;s data incoming from the remote peer.
Definition: LuaTCPLink.cpp:310
Definition: FastNBT.h:131
std::unique_ptr< cTableRef > cTableRefPtr
Definition: LuaState.h:420
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
AString StartTLSServer(const AString &a_OwnCertData, const AString &a_OwnPrivKeyData, const AString &a_OwnPrivKeyPassword, const AString &a_StartTLSData)
Starts a TLS handshake as a server connection.
Definition: LuaTCPLink.cpp:204
AString GetRemoteIP(void) const
Returns the IP address of the remote endpoint of the connection.
Definition: LuaTCPLink.cpp:102
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
void Terminated(void)
Common code called when the link is considered as terminated.
Definition: LuaTCPLink.cpp:237
bool Send(const AString &a_Data)
Sends the data contained in the string to the remote peer.
Definition: LuaTCPLink.cpp:51
UInt16 GetLocalPort(void) const
Returns the port used by the local endpoint of the connection.
Definition: LuaTCPLink.cpp:85
void Shutdown(void)
Closes the link gracefully.
Definition: LuaTCPLink.cpp:136
cLuaServerHandleWPtr m_Server
The server that is responsible for this link, if any.
Definition: LuaTCPLink.h:101
void ReceivedCleartextData(const char *a_Data, size_t a_NumBytes)
Called by the SSL context when there&#39;s incoming data available in the cleartext.
Definition: LuaTCPLink.cpp:267
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:17
virtual ~cLuaTCPLink() override
Definition: LuaTCPLink.cpp:35
cTCPLinkPtr m_Link
The underlying link representing the connection.
Definition: LuaTCPLink.h:98
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
cLuaTCPLink(cLuaState::cTableRefPtr &&a_Callbacks)
Creates a new instance of the link, wrapping the callbacks that are in the specified table...
Definition: LuaTCPLink.cpp:16
std::weak_ptr< cLuaServerHandle > cLuaServerHandleWPtr
Definition: LuaTCPLink.h:20
void Close(void)
Drops the connection without any more processing.
Definition: LuaTCPLink.cpp:150
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
Called when the cTCPLink for the connection is created.
Definition: LuaTCPLink.cpp:300
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when an error is detected on the connection.
Definition: LuaTCPLink.cpp:287
unsigned short UInt16
Definition: Globals.h:114
std::string AString
Definition: StringUtils.h:13
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:33
UInt16 GetRemotePort(void) const
Returns the port used by the remote endpoint of the connection.
Definition: LuaTCPLink.cpp:119
AString StartTLSClient(const AString &a_OwnCertData, const AString &a_OwnPrivKeyData, const AString &a_OwnPrivKeyPassword)
Starts a TLS handshake as a client connection.
Definition: LuaTCPLink.cpp:166
AString GetLocalIP(void) const
Returns the IP address of the local endpoint of the connection.
Definition: LuaTCPLink.cpp:68
cLuaState::cTableRefPtr m_Callbacks
The Lua table that holds the callbacks to be invoked.
Definition: LuaTCPLink.h:94
virtual void OnRemoteClosed(void) override
Called when the remote end closes the connection.
Definition: LuaTCPLink.cpp:320
virtual void OnConnected(cTCPLink &a_Link) override
Called when the Connect call succeeds.
Definition: LuaTCPLink.cpp:277