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
|
/*
* Copyright (C) 2009-2010 Big Muscle, http://strongdc.sourceforge.net/
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "dcpp/AdcCommand.h"
#include "dcpp/CID.h"
#include "dcpp/FastAlloc.h"
#include "dcpp/MerkleTree.h"
#include "dcpp/Socket.h"
#include "dcpp/Thread.h"
namespace dht
{
struct Packet :
FastAlloc<Packet>
{
/** Public constructor */
Packet(const string& ip_, const string& port_, const std::string& data_, const CID& _targetCID, const CID& _udpKey) :
ip(ip_), port(port_), data(data_), targetCID(_targetCID), udpKey(_udpKey)
{
}
/** IP where send this packet to */
string ip;
/** To which port this packet should be sent */
string port;
/** Data to sent */
std::string data;
/** CID of target node */
CID targetCID;
/** Key to encrypt packet */
CID udpKey;
};
class UDPSocket :
private Thread
{
public:
UDPSocket(void);
~UDPSocket(void);
/** Disconnects UDP socket */
void disconnect() throw();
/** Starts listening to UDP socket */
void listen();
/** Returns port used to listening to UDP socket */
const std::string& getPort() const { return port; }
/** Sends command to ip and port */
void send(AdcCommand& cmd, const string& ip, const string &port, const CID& targetCID, const CID& udpKey);
private:
std::unique_ptr<Socket> socket;
/** Indicates to stop socket thread */
bool stop;
/** Port for communicating in this network */
std::string port;
/** Queue for sending packets through UDP socket */
std::deque<Packet*> sendQueue;
/** Antiflooding protection */
uint64_t delay;
/** Locks access to sending queue */
CriticalSection cs;
#ifdef _DEBUG
// debug constants to optimize bandwidth
size_t sentBytes;
size_t receivedBytes;
size_t sentPackets;
size_t receivedPackets;
#endif
/** Thread for receiving UDP packets */
int run();
void checkIncoming();
void checkOutgoing(uint64_t& timer);
void compressPacket(const string& data, uint8_t* destBuf, unsigned long& destSize);
void encryptPacket(const CID& targetCID, const CID& udpKey, uint8_t* destBuf, unsigned long& destSize);
bool decompressPacket(uint8_t* destBuf, unsigned long& destLen, const uint8_t* buf, size_t len);
bool decryptPacket(uint8_t* buf, int& len, const string& remoteIp, bool& isUdpKeyValid);
};
}
|