Cuberite
A lightweight, fast and extensible game server for Minecraft
Network.h
Go to the documentation of this file.
1 
2 // Network.h
3 
4 // Declares the classes used for the Network API
5 
6 
7 
8 
9 
10 #pragma once
11 
12 
13 
14 
15 
16 #ifdef __FreeBSD__
17  #include <netinet/in.h>
18 #endif
19 
20 
21 
22 
23 
24 // fwd:
25 class cTCPLink;
26 typedef std::shared_ptr<cTCPLink> cTCPLinkPtr;
27 typedef std::vector<cTCPLinkPtr> cTCPLinkPtrs;
29 typedef std::shared_ptr<cServerHandle> cServerHandlePtr;
30 typedef std::vector<cServerHandlePtr> cServerHandlePtrs;
31 class cCryptoKey;
32 typedef std::shared_ptr<cCryptoKey> cCryptoKeyPtr;
33 class cX509Cert;
34 typedef std::shared_ptr<cX509Cert> cX509CertPtr;
35 
36 
37 
38 
39 
41 class cTCPLink
42 {
43  friend class cNetwork;
44 
45 public:
46  class cCallbacks
47  {
48  public:
49  // Force a virtual destructor for all descendants:
50  virtual ~cCallbacks() {}
51 
54  virtual void OnLinkCreated(cTCPLinkPtr a_Link) = 0;
55 
57  virtual void OnReceivedData(const char * a_Data, size_t a_Length) = 0;
58 
62  virtual void OnRemoteClosed(void) = 0;
63 
66  virtual void OnTlsHandshakeCompleted(void) {}
67 
69  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
70  };
71  typedef std::shared_ptr<cCallbacks> cCallbacksPtr;
72 
73 
74  // Force a virtual destructor for all descendants:
75  virtual ~cTCPLink() {}
76 
79  virtual bool Send(const void * a_Data, size_t a_Length) = 0;
80 
83  bool Send(const AString & a_Data)
84  {
85  return Send(a_Data.data(), a_Data.size());
86  }
87 
89  virtual AString GetLocalIP(void) const = 0;
90 
92  virtual UInt16 GetLocalPort(void) const = 0;
93 
95  virtual AString GetRemoteIP(void) const = 0;
96 
98  virtual UInt16 GetRemotePort(void) const = 0;
99 
103  virtual void Shutdown(void) = 0;
104 
107  virtual void Close(void) = 0;
108 
114  virtual AString StartTLSClient(
115  cX509CertPtr a_OwnCert,
116  cCryptoKeyPtr a_OwnPrivKey
117  ) = 0;
118 
127  virtual AString StartTLSServer(
128  cX509CertPtr a_OwnCert,
129  cCryptoKeyPtr a_OwnPrivKey,
130  const AString & a_StartTLSData
131  ) = 0;
132 
134  cCallbacksPtr GetCallbacks(void) const { return m_Callbacks; }
135 
136 protected:
139 
140 
142  cTCPLink(cCallbacksPtr a_Callbacks):
143  m_Callbacks(std::move(a_Callbacks))
144  {
145  }
146 };
147 
148 
149 
150 
151 
154 {
155  friend class cNetwork;
156 public:
157 
158  // Force a virtual destructor for all descendants:
159  virtual ~cServerHandle() {}
160 
163  virtual void Close(void) = 0;
164 
166  virtual bool IsListening(void) const = 0;
167 };
168 
169 
170 
171 
172 
175 {
176 public:
179  {
180  public:
181  // Force a virtual destructor in all descendants:
182  virtual ~cCallbacks() {}
183 
185  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
186 
188  virtual void OnReceivedData(const char * a_Data, size_t a_Size, const AString & a_RemoteHost, UInt16 a_RemotePort) = 0;
189  };
190 
191 
192  // Force a virtual destructor for all descendants:
193  virtual ~cUDPEndpoint() {}
194 
197  virtual void Close(void) = 0;
198 
200  virtual bool IsOpen(void) const = 0;
201 
203  virtual UInt16 GetPort(void) const = 0;
204 
207  virtual bool Send(const AString & a_Payload, const AString & a_Host, UInt16 a_Port) = 0;
208 
211  virtual void EnableBroadcasts(void) = 0;
212 
213 protected:
216 
217 
219  cUDPEndpoint(cCallbacks & a_Callbacks):
220  m_Callbacks(a_Callbacks)
221  {
222  }
223 };
224 
225 typedef std::shared_ptr<cUDPEndpoint> cUDPEndpointPtr;
226 
227 
228 
229 
230 
231 class cNetwork
232 {
233 public:
236  {
237  public:
238  // Force a virtual destructor for all descendants:
239  virtual ~cConnectCallbacks() {}
240 
243  virtual void OnConnected(cTCPLink & a_Link) = 0;
244 
246  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
247  };
248  typedef std::shared_ptr<cConnectCallbacks> cConnectCallbacksPtr;
249 
250 
253  {
254  public:
255  // Force a virtual destructor for all descendants:
256  virtual ~cListenCallbacks() {}
257 
262  virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort) = 0;
263 
267  virtual void OnAccepted(cTCPLink & a_Link) = 0;
268 
270  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
271  };
272  typedef std::shared_ptr<cListenCallbacks> cListenCallbacksPtr;
273 
274 
277  {
278  public:
279  // Force a virtual destructor for all descendants:
281 
286  virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) = 0;
287 
292  virtual bool OnNameResolvedV4(const AString & a_Name, const sockaddr_in * a_IP) { return true; }
293 
298  virtual bool OnNameResolvedV6(const AString & a_Name, const sockaddr_in6 * a_IP) { return true; }
299 
302  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
303 
306  virtual void OnFinished(void) = 0;
307  };
308  typedef std::shared_ptr<cResolveNameCallbacks> cResolveNameCallbacksPtr;
309 
310 
317  static bool Connect(
318  const AString & a_Host,
319  UInt16 a_Port,
320  cConnectCallbacksPtr a_ConnectCallbacks,
321  cTCPLink::cCallbacksPtr a_LinkCallbacks
322  );
323 
324 
330  static cServerHandlePtr Listen(
331  UInt16 a_Port,
332  cListenCallbacksPtr a_ListenCallbacks
333  );
334 
335 
341  static bool HostnameToIP(
342  const AString & a_Hostname,
343  cResolveNameCallbacksPtr a_Callbacks
344  );
345 
346 
352  static bool IPToHostName(
353  const AString & a_IP,
354  cResolveNameCallbacksPtr a_Callbacks
355  );
356 
361 
363  static AStringVector EnumLocalIPAddresses(void);
364 };
365 
366 
367 
cNetwork::cResolveNameCallbacksPtr
std::shared_ptr< cResolveNameCallbacks > cResolveNameCallbacksPtr
Definition: Network.h:308
cNetwork::CreateUDPEndpoint
static cUDPEndpointPtr CreateUDPEndpoint(UInt16 a_Port, cUDPEndpoint::cCallbacks &a_Callbacks)
Opens up an UDP endpoint for sending and receiving UDP datagrams on the specified port.
Definition: UDPEndpointImpl.cpp:612
cX509CertPtr
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
cUDPEndpoint::Close
virtual void Close(void)=0
Closes the underlying socket.
cTCPLink
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:41
cNetwork::Connect
static bool Connect(const AString &a_Host, UInt16 a_Port, cConnectCallbacksPtr a_ConnectCallbacks, cTCPLink::cCallbacksPtr a_LinkCallbacks)
Queues a TCP connection to be made to the specified host.
Definition: TCPLinkImpl.cpp:695
cNetwork::cListenCallbacks
Callbacks used when listening for incoming connections as a server.
Definition: Network.h:252
cUDPEndpoint::cCallbacks
Interface for the callbacks for events that can happen on the endpoint.
Definition: Network.h:178
cNetwork::cResolveNameCallbacks::OnError
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error is encountered while resolving.
cTCPLink::StartTLSServer
virtual AString StartTLSServer(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData)=0
Starts a TLS handshake as a server connection.
cTCPLink::cCallbacks::OnError
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error is detected on the connection.
cTCPLink::GetLocalIP
virtual AString GetLocalIP(void) const =0
Returns the IP address of the local endpoint of the connection.
cNetwork::cResolveNameCallbacks::OnNameResolved
virtual void OnNameResolved(const AString &a_Name, const AString &a_IP)=0
Called when the hostname is successfully resolved into an IP address.
UInt16
unsigned short UInt16
Definition: Globals.h:155
cServerHandle::IsListening
virtual bool IsListening(void) const =0
Returns true if the server has been started correctly and is currently listening for incoming connect...
cTCPLink::GetLocalPort
virtual UInt16 GetLocalPort(void) const =0
Returns the port used by the local endpoint of the connection.
cServerHandle
Interface that provides the methods available on a listening server socket.
Definition: Network.h:153
cTCPLinkPtrs
std::vector< cTCPLinkPtr > cTCPLinkPtrs
Definition: Network.h:27
cTCPLinkPtr
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:25
cNetwork::cConnectCallbacks
Callbacks used for connecting to other servers as a client.
Definition: Network.h:235
cX509CertPtr
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: Network.h:33
cUDPEndpoint::Send
virtual bool Send(const AString &a_Payload, const AString &a_Host, UInt16 a_Port)=0
Sends the specified payload in a single UDP datagram to the specified host + port combination.
cNetwork::IPToHostName
static bool IPToHostName(const AString &a_IP, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified IP address to a hostname.
Definition: IPLookup.cpp:91
cNetwork::cConnectCallbacks::OnError
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when the Connect call fails.
cUDPEndpoint::EnableBroadcasts
virtual void EnableBroadcasts(void)=0
Marks the socket as capable of sending broadcast, using whatever OS API is needed.
cTCPLink::cCallbacks::~cCallbacks
virtual ~cCallbacks()
Definition: Network.h:50
cUDPEndpoint::cCallbacks::OnReceivedData
virtual void OnReceivedData(const char *a_Data, size_t a_Size, const AString &a_RemoteHost, UInt16 a_RemotePort)=0
Called when there is an incoming datagram from a remote host.
cNetwork::cConnectCallbacks::~cConnectCallbacks
virtual ~cConnectCallbacks()
Definition: Network.h:239
cNetwork::cResolveNameCallbacks::~cResolveNameCallbacks
virtual ~cResolveNameCallbacks()
Definition: Network.h:280
cTCPLink::Send
virtual bool Send(const void *a_Data, size_t a_Length)=0
Queues the specified data for sending to the remote peer.
cUDPEndpoint::m_Callbacks
cCallbacks & m_Callbacks
The callbacks used for various events on the endpoint.
Definition: Network.h:215
std
Definition: FastNBT.h:131
cTCPLink::GetRemotePort
virtual UInt16 GetRemotePort(void) const =0
Returns the port used by the remote endpoint of the connection.
cTCPLink::StartTLSClient
virtual AString StartTLSClient(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey)=0
Starts a TLS handshake as a client connection.
cNetwork::cResolveNameCallbacks::OnNameResolvedV6
virtual bool OnNameResolvedV6(const AString &a_Name, const sockaddr_in6 *a_IP)
Called when the hostname is successfully resolved into an IPv6 address.
Definition: Network.h:298
cTCPLink::Send
bool Send(const AString &a_Data)
Queues the specified data for sending to the remote peer.
Definition: Network.h:83
cTCPLink::m_Callbacks
cCallbacksPtr m_Callbacks
Callbacks to be used for the various situations.
Definition: Network.h:138
cX509Cert
Definition: X509Cert.h:18
cUDPEndpoint::GetPort
virtual UInt16 GetPort(void) const =0
Returns the local port to which the underlying socket is bound.
cUDPEndpoint::IsOpen
virtual bool IsOpen(void) const =0
Returns true if the endpoint is open.
cCryptoKeyPtr
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
cNetwork::cListenCallbacks::OnAccepted
virtual void OnAccepted(cTCPLink &a_Link)=0
Called when the TCP server created with Listen() creates a new link for an incoming connection.
cTCPLink::cTCPLink
cTCPLink(cCallbacksPtr a_Callbacks)
Creates a new link, with the specified callbacks.
Definition: Network.h:142
cUDPEndpoint::cCallbacks::OnError
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error occurs on the endpoint.
cTCPLink::cCallbacksPtr
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:71
cUDPEndpoint::cUDPEndpoint
cUDPEndpoint(cCallbacks &a_Callbacks)
Creates a new instance of an endpoint, with the specified callbacks.
Definition: Network.h:219
cCryptoKeyPtr
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: Network.h:31
cUDPEndpointPtr
std::shared_ptr< cUDPEndpoint > cUDPEndpointPtr
Definition: Network.h:225
cNetwork::EnumLocalIPAddresses
static AStringVector EnumLocalIPAddresses(void)
Returns all local IP addresses for network interfaces currently available.
Definition: NetworkInterfaceEnum.cpp:91
cTCPLink::cCallbacks
Definition: Network.h:46
cNetwork::cListenCallbacks::OnIncomingConnection
virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString &a_RemoteIPAddress, UInt16 a_RemotePort)=0
Called when the TCP server created with Listen() receives a new incoming connection.
cNetwork::cListenCallbacks::OnError
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when the socket fails to listen on the specified port.
cTCPLink::cCallbacks::OnReceivedData
virtual void OnReceivedData(const char *a_Data, size_t a_Length)=0
Called when there's data incoming from the remote peer.
cUDPEndpoint::~cUDPEndpoint
virtual ~cUDPEndpoint()
Definition: Network.h:193
cUDPEndpoint::cCallbacks::~cCallbacks
virtual ~cCallbacks()
Definition: Network.h:182
cNetwork::cResolveNameCallbacks
Callbacks used when resolving names to IPs.
Definition: Network.h:276
cNetwork::Listen
static cServerHandlePtr Listen(UInt16 a_Port, cListenCallbacksPtr a_ListenCallbacks)
Opens up the specified port for incoming connections.
Definition: ServerHandleImpl.cpp:364
cCryptoKey
Definition: CryptoKey.h:19
cTCPLink::cCallbacks::OnRemoteClosed
virtual void OnRemoteClosed(void)=0
Called when the remote end closes the connection.
cServerHandle::~cServerHandle
virtual ~cServerHandle()
Definition: Network.h:159
cUDPEndpoint
Interface that provides methods available on UDP communication endpoints.
Definition: Network.h:174
cServerHandlePtr
std::shared_ptr< cServerHandle > cServerHandlePtr
Definition: Network.h:28
cTCPLink::Close
virtual void Close(void)=0
Drops the connection without any more processing.
cNetwork::cResolveNameCallbacks::OnFinished
virtual void OnFinished(void)=0
Called when all the addresses resolved have been reported via the OnNameResolved() callback.
cNetwork::cConnectCallbacksPtr
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
Definition: Network.h:248
cTCPLink::GetRemoteIP
virtual AString GetRemoteIP(void) const =0
Returns the IP address of the remote endpoint of the connection.
cNetwork::cListenCallbacksPtr
std::shared_ptr< cListenCallbacks > cListenCallbacksPtr
Definition: Network.h:272
cTCPLink::~cTCPLink
virtual ~cTCPLink()
Definition: Network.h:75
cNetwork::cResolveNameCallbacks::OnNameResolvedV4
virtual bool OnNameResolvedV4(const AString &a_Name, const sockaddr_in *a_IP)
Called when the hostname is successfully resolved into an IPv4 address.
Definition: Network.h:292
AString
std::string AString
Definition: StringUtils.h:11
cTCPLink::GetCallbacks
cCallbacksPtr GetCallbacks(void) const
Returns the callbacks that are used.
Definition: Network.h:134
cNetwork::cConnectCallbacks::OnConnected
virtual void OnConnected(cTCPLink &a_Link)=0
Called when the Connect call succeeds.
cServerHandlePtrs
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Network.h:30
cServerHandle::Close
virtual void Close(void)=0
Stops the server, no more incoming connections will be accepted.
cTCPLink::cCallbacks::OnLinkCreated
virtual void OnLinkCreated(cTCPLinkPtr a_Link)=0
Called when the cTCPLink for the connection is created.
cTCPLink::Shutdown
virtual void Shutdown(void)=0
Closes the link gracefully.
cNetwork::HostnameToIP
static bool HostnameToIP(const AString &a_Hostname, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified hostname to IP address.
Definition: HostnameLookup.cpp:125
cTCPLink::cCallbacks::OnTlsHandshakeCompleted
virtual void OnTlsHandshakeCompleted(void)
Called when the TLS handshake has been completed and communication can continue regularly.
Definition: Network.h:66
cNetwork::cListenCallbacks::~cListenCallbacks
virtual ~cListenCallbacks()
Definition: Network.h:256
cNetwork
Definition: Network.h:231
AStringVector
std::vector< AString > AStringVector
Definition: StringUtils.h:12