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;
28 class cServerHandle;
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 
115  cX509CertPtr a_OwnCert,
116  cCryptoKeyPtr a_OwnPrivKey,
117  cX509CertPtr a_TrustedRootCAs
118  ) = 0;
119 
129  cX509CertPtr a_OwnCert,
130  cCryptoKeyPtr a_OwnPrivKey,
131  const AString & a_StartTLSData
132  ) = 0;
133 
135  cCallbacksPtr GetCallbacks(void) const { return m_Callbacks; }
136 
137 protected:
140 
141 
143  cTCPLink(cCallbacksPtr a_Callbacks):
144  m_Callbacks(std::move(a_Callbacks))
145  {
146  }
147 };
148 
149 
150 
151 
152 
155 {
156  friend class cNetwork;
157 public:
158 
159  // Force a virtual destructor for all descendants:
160  virtual ~cServerHandle() {}
161 
164  virtual void Close(void) = 0;
165 
167  virtual bool IsListening(void) const = 0;
168 };
169 
170 
171 
172 
173 
176 {
177 public:
180  {
181  public:
182  // Force a virtual destructor in all descendants:
183  virtual ~cCallbacks() {}
184 
186  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
187 
189  virtual void OnReceivedData(const char * a_Data, size_t a_Size, const AString & a_RemoteHost, UInt16 a_RemotePort) = 0;
190  };
191 
192 
193  // Force a virtual destructor for all descendants:
194  virtual ~cUDPEndpoint() {}
195 
198  virtual void Close(void) = 0;
199 
201  virtual bool IsOpen(void) const = 0;
202 
204  virtual UInt16 GetPort(void) const = 0;
205 
208  virtual bool Send(const AString & a_Payload, const AString & a_Host, UInt16 a_Port) = 0;
209 
212  virtual void EnableBroadcasts(void) = 0;
213 
214 protected:
217 
218 
220  cUDPEndpoint(cCallbacks & a_Callbacks):
221  m_Callbacks(a_Callbacks)
222  {
223  }
224 };
225 
226 typedef std::shared_ptr<cUDPEndpoint> cUDPEndpointPtr;
227 
228 
229 
230 
231 
232 class cNetwork
233 {
234 public:
237  {
238  public:
239  // Force a virtual destructor for all descendants:
240  virtual ~cConnectCallbacks() {}
241 
244  virtual void OnConnected(cTCPLink & a_Link) = 0;
245 
247  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
248  };
249  typedef std::shared_ptr<cConnectCallbacks> cConnectCallbacksPtr;
250 
251 
254  {
255  public:
256  // Force a virtual destructor for all descendants:
257  virtual ~cListenCallbacks() {}
258 
263  virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort) = 0;
264 
268  virtual void OnAccepted(cTCPLink & a_Link) = 0;
269 
271  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
272  };
273  typedef std::shared_ptr<cListenCallbacks> cListenCallbacksPtr;
274 
275 
278  {
279  public:
280  // Force a virtual destructor for all descendants:
282 
287  virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) = 0;
288 
293  virtual bool OnNameResolvedV4(const AString & a_Name, const sockaddr_in * a_IP) { return true; }
294 
299  virtual bool OnNameResolvedV6(const AString & a_Name, const sockaddr_in6 * a_IP) { return true; }
300 
303  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
304 
307  virtual void OnFinished(void) = 0;
308  };
309  typedef std::shared_ptr<cResolveNameCallbacks> cResolveNameCallbacksPtr;
310 
311 
318  static bool Connect(
319  const AString & a_Host,
320  UInt16 a_Port,
321  cConnectCallbacksPtr a_ConnectCallbacks,
322  cTCPLink::cCallbacksPtr a_LinkCallbacks
323  );
324 
325 
331  static cServerHandlePtr Listen(
332  UInt16 a_Port,
333  cListenCallbacksPtr a_ListenCallbacks
334  );
335 
336 
342  static bool HostnameToIP(
343  const AString & a_Hostname,
344  cResolveNameCallbacksPtr a_Callbacks
345  );
346 
347 
353  static bool IPToHostName(
354  const AString & a_IP,
355  cResolveNameCallbacksPtr a_Callbacks
356  );
357 
362 
364  static AStringVector EnumLocalIPAddresses(void);
365 };
366 
367 
368 
unsigned short UInt16
Definition: Globals.h:158
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:25
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: Network.h:31
std::vector< cTCPLinkPtr > cTCPLinkPtrs
Definition: Network.h:27
std::shared_ptr< cUDPEndpoint > cUDPEndpointPtr
Definition: Network.h:226
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: Network.h:33
std::shared_ptr< cServerHandle > cServerHandlePtr
Definition: Network.h:28
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Network.h:30
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: FastNBT.h:132
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:42
virtual UInt16 GetLocalPort(void) const =0
Returns the port used by the local endpoint of the connection.
bool Send(const AString &a_Data)
Queues the specified data for sending to the remote peer.
Definition: Network.h:83
virtual UInt16 GetRemotePort(void) const =0
Returns the port used by the remote endpoint of the connection.
cCallbacksPtr GetCallbacks(void) const
Returns the callbacks that are used.
Definition: Network.h:135
virtual ~cTCPLink()
Definition: Network.h:75
virtual AString StartTLSClient(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, cX509CertPtr a_TrustedRootCAs)=0
Starts a TLS handshake as a client connection.
virtual AString GetLocalIP(void) const =0
Returns the IP address of the local endpoint of the connection.
virtual bool Send(const void *a_Data, size_t a_Length)=0
Queues the specified data for sending to the remote peer.
virtual void Close(void)=0
Drops the connection without any more processing.
cTCPLink(cCallbacksPtr a_Callbacks)
Creates a new link, with the specified callbacks.
Definition: Network.h:143
virtual void Shutdown(void)=0
Closes the link gracefully.
virtual AString StartTLSServer(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData)=0
Starts a TLS handshake as a server connection.
cCallbacksPtr m_Callbacks
Callbacks to be used for the various situations.
Definition: Network.h:139
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:71
virtual AString GetRemoteIP(void) const =0
Returns the IP address of the remote endpoint of the connection.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error is detected on the connection.
virtual void OnReceivedData(const char *a_Data, size_t a_Length)=0
Called when there's data incoming from the remote peer.
virtual void OnRemoteClosed(void)=0
Called when the remote end closes the connection.
virtual void OnTlsHandshakeCompleted(void)
Called when the TLS handshake has been completed and communication can continue regularly.
Definition: Network.h:66
virtual void OnLinkCreated(cTCPLinkPtr a_Link)=0
Called when the cTCPLink for the connection is created.
virtual ~cCallbacks()
Definition: Network.h:50
Interface that provides the methods available on a listening server socket.
Definition: Network.h:155
virtual ~cServerHandle()
Definition: Network.h:160
virtual bool IsListening(void) const =0
Returns true if the server has been started correctly and is currently listening for incoming connect...
virtual void Close(void)=0
Stops the server, no more incoming connections will be accepted.
Interface that provides methods available on UDP communication endpoints.
Definition: Network.h:176
virtual void Close(void)=0
Closes the underlying socket.
virtual bool IsOpen(void) const =0
Returns true if the endpoint is open.
cCallbacks & m_Callbacks
The callbacks used for various events on the endpoint.
Definition: Network.h:216
cUDPEndpoint(cCallbacks &a_Callbacks)
Creates a new instance of an endpoint, with the specified callbacks.
Definition: Network.h:220
virtual void EnableBroadcasts(void)=0
Marks the socket as capable of sending broadcast, using whatever OS API is needed.
virtual UInt16 GetPort(void) const =0
Returns the local port to which the underlying socket is bound.
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.
virtual ~cUDPEndpoint()
Definition: Network.h:194
Interface for the callbacks for events that can happen on the endpoint.
Definition: Network.h:180
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.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error occurs on the endpoint.
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.
static AStringVector EnumLocalIPAddresses(void)
Returns all local IP addresses for network interfaces currently available.
static cServerHandlePtr Listen(UInt16 a_Port, cListenCallbacksPtr a_ListenCallbacks)
Opens up the specified port for incoming connections.
std::shared_ptr< cListenCallbacks > cListenCallbacksPtr
Definition: Network.h:273
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.
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
Definition: Network.h:249
static bool HostnameToIP(const AString &a_Hostname, cResolveNameCallbacksPtr a_Callbacks)
Queues a DNS query to resolve the specified hostname to IP address.
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
std::shared_ptr< cResolveNameCallbacks > cResolveNameCallbacksPtr
Definition: Network.h:309
Callbacks used for connecting to other servers as a client.
Definition: Network.h:237
virtual void OnConnected(cTCPLink &a_Link)=0
Called when the Connect call succeeds.
virtual ~cConnectCallbacks()
Definition: Network.h:240
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when the Connect call fails.
Callbacks used when listening for incoming connections as a server.
Definition: Network.h:254
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.
virtual ~cListenCallbacks()
Definition: Network.h:257
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when the socket fails to listen on the specified port.
virtual void OnAccepted(cTCPLink &a_Link)=0
Called when the TCP server created with Listen() creates a new link for an incoming connection.
Callbacks used when resolving names to IPs.
Definition: Network.h:278
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:299
virtual void OnFinished(void)=0
Called when all the addresses resolved have been reported via the OnNameResolved() callback.
virtual void OnNameResolved(const AString &a_Name, const AString &a_IP)=0
Called when the hostname is successfully resolved into an IP address.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error is encountered while resolving.
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:293