-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathMuleUDPSocket.h
207 lines (178 loc) · 5.97 KB
/
MuleUDPSocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// This file is part of the aMule Project.
//
// Copyright (c) 2005-2011 aMule Team ( [email protected] / http://www.amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#ifndef MULEUDPSOCKET_H
#define MULEUDPSOCKET_H
#include "Types.h" // Needed for uint16 and uint32
#include "ThrottledSocket.h" // Needed for ThrottledControlSocket
#include "amuleIPV4Address.h" // Needed for amuleIPV4Address
#include <wx/thread.h> // Needed for wxMutex
class CEncryptedDatagramSocket;
class CProxyData;
class CPacket;
/***
* This class provides a UBT governed UDP-socket.
*
* The CMuleUDPSocket are created with the NOWAIT option and
* handle both INPUT and OUTPUT events.
*
* The following additional features are provided compared to CDatagramSocketProxy:
* - Goverened by the UBT.
* - Automatic sending/receiving of packets.
* - Fallover recovery for when a socket becomes invalid (error 4).
*
* @see ThrottledControlSocket
* @see CEncryptedDatagramSocket
*/
class CMuleUDPSocket : public ThrottledControlSocket
{
public:
/**
* Opens a UDP socket at the specified address.
*
* @param name Name used when logging events.
* @param id The ID used for events.
* @param address The address where the socket will listen.
* @param ProxyData ProxyData associated with the socket.
*/
CMuleUDPSocket(const wxString& name, int id, const amuleIPV4Address& address, const CProxyData* ProxyData = NULL);
/**
* Destructor, safely closes the socket if opened.
*/
virtual ~CMuleUDPSocket();
/**
* Opens the socket.
*
* The socket is bound to the address specified in
* the constructor.
*/
void Open();
/**
* Closes the socket.
*
* The socket can be reopened by calling Open. Closing a
* already closed socket is an illegal operation.
*/
void Close();
/** This function is called by aMule when the socket may send. */
virtual void OnSend(int errorCode);
/** This function is called by aMule when there are data to be received. */
virtual void OnReceive(int errorCode);
/** This function is called by aMule when there is an error while receiving. */
virtual void OnReceiveError(int errorCode, uint32 ip, uint16 port);
/** This function is called when the socket is lost (see comments in func.) */
virtual void OnDisconnected(int errorCode);
/**
* Queues a packet for sending.
*
* @param packet The packet to send.
* @param IP The target IP address.
* @param port The target port.
* @param bEncrypt If the packet must be encrypted
* @param port The target port.
* @param pachTargetClientHashORKadID The client hash or Kad ID
* @param bKad
* @param nReceiverVerifyKey
*
* Note that CMuleUDPSocket takes ownership of the packet.
*/
void SendPacket(CPacket* packet, uint32 IP, uint16 port, bool bEncrypt, const uint8* pachTargetClientHashORKadID, bool bKad, uint32 nReceiverVerifyKey);
/**
* Returns true if the socket is Ok, false otherwise.
*
* @see wxSocketBase::Ok
*/
bool Ok();
/** Read buffer size */
static const unsigned UDP_BUFFER_SIZE = 16384;
protected:
/**
* This function is called when a packet has been received.
*
* @param addr The address from where data was received.
* @param buffer The data that has been received.
* @param length The length of the data buffer.
*/
virtual void OnPacketReceived(uint32 ip, uint16 port, uint8_t* buffer, size_t length) = 0;
/** See ThrottledControlSocket::SendControlData */
SocketSentBytes SendControlData(uint32 maxNumberOfBytesToSend, uint32 minFragSize);
private:
/**
* Sends a packet to the specified address.
*
* @param buffer The data to be sent.
* @param length the length of the data buffer.
* @param ip The target ip address.
* @param port The target port.
*/
bool SendTo(uint8_t *buffer, uint32_t length, uint32_t ip, uint16_t port);
/**
* Creates a new socket.
*
* Calling this function when a socket already exists
* is an illegal operation.
*/
void CreateSocket();
/**
* Destroys the current socket, if any.
*/
void DestroySocket();
//! Specifies if the last write attempt would cause the socket to block.
bool m_busy;
//! The name of the socket, used for debugging messages.
wxString m_name;
//! The socket-ID, used for event-handling.
int m_id;
//! The address at which the socket is currently bound.
amuleIPV4Address m_addr;
//! Proxy settings used by the socket ...
const CProxyData* m_proxy;
//! Mutex needed due to the use of the UBT.
wxMutex m_mutex;
//! The currently opened socket, if any.
CEncryptedDatagramSocket* m_socket;
//! Storage struct used for queueing packets.
struct UDPPack
{
//! The packet, which at this point is owned by CMuleUDPSocket.
CPacket* packet;
//! The timestamp of when the packet was queued.
uint32 time;
//! Target IP address.
uint32 IP;
//! Target port.
uint16 port;
//! If the packet is encrypted.
bool bEncrypt;
//! Is it a kad packet?
bool bKad;
// The verification key for RC4 encryption.
uint32 nReceiverVerifyKey;
// Client hash or kad ID.
uint8 pachTargetClientHashORKadID[16];
} ;
//! The queue of packets waiting to be sent.
std::list<UDPPack> m_queue;
};
#endif // CLIENTUDPSOCKET_H
// File_checked_for_headers