Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
*.P
*.bat
*.exe
*.filter
*.filters
*.log
*.user
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ MySQL
There is a VS2017 project available. Needed includes and libs will be searched in .\deps\include and .\deps\libs.
Dependencies
- Boost
- OpenSSL (version < 1.1.0)
- Mpir
- MySQL Connector C (available in MYSQL package)

Expand Down
4 changes: 0 additions & 4 deletions makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ INCLUDEPATHS= \
-I"$(CURDIR)\src\hash" \
-I"$(CURDIR)\src\LLP" \
-I"C:\Deps\boost_1_57_0" \
-I"C:\Deps\openssl-1.0.1l\include" \
-I"C:\Deps\GMP\include"

LIBPATHS= \
-L"C:\Deps\boost_1_57_0\stage\lib" \
-L"C:\Deps\openssl-1.0.1l" \
-L"C:\Deps\GMP\lib"

LIBS= \
-l boost_system-mgw49-mt-s-1_57 \
-l boost_filesystem-mgw49-mt-s-1_57 \
-l boost_program_options-mgw49-mt-s-1_57 \
-l boost_thread-mgw49-mt-s-1_57 \
-l ssl \
-l crypto \
-l gmp \
-l mysqlclient

Expand Down
5 changes: 2 additions & 3 deletions makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

DEFS=-DBOOST_SPIRIT_THREADSAFE -DBOOST_THREAD_USE_LIB

DEFS += $(addprefix -I,$(CURDIR)/src $(CURDIR)/src/build $(CURDIR)/src/hash $(CURDIR)/src/json $(BOOST_INCLUDE_PATH) $(OPENSSL_INCLUDE_PATH))
LIBS = -lc -lpthread $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH) $(OPENSSL_LIB_PATH))
DEFS += $(addprefix -I,$(CURDIR)/src $(CURDIR)/src/build $(CURDIR)/src/hash $(CURDIR)/src/json $(BOOST_INCLUDE_PATH)
LIBS = -lc -lpthread $(addprefix -L,$(BOOST_LIB_PATH) $(BDB_LIB_PATH))

#For Boost 1.55 Builds Uncomment the Following
#BOOST_LIB_PATH=/usr/local/lib
Expand All @@ -18,7 +18,6 @@ LIBS += \
-l boost_program_options$(BOOST_LIB_SUFFIX) \
-l boost_thread$(BOOST_LIB_SUFFIX) \
-l boost_serialization$(BOOST_LIB_SUFFIX) \
-l crypto \
-l gmp \
-l mysqlclient

Expand Down
2 changes: 1 addition & 1 deletion pool.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

"daemon_threads" : 10,
"pool_threads" : 20,
"enable_ddos" : "true",
"enable_ddos" : true,
"ddos_rscore" : 20,
"ddos_cscore" : 2,
"min_share" : 40000000,
Expand Down
15 changes: 10 additions & 5 deletions src/LLD/database.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#ifndef COINSHIELD_LLD_DATABASE
#define COINSHIELD_LLD_DATABASE
#ifndef NEXUS_LLD_DATABASE
#define NEXUS_LLD_DATABASE

#include <fstream>
#include <string>
#include <mutex>
#include <map>
#include "../util.h"

namespace LLD
{

/** Main Class to be inherited Creating Customized Database Handles. Contains only the Key. **/
template<typename KeyType> class Record { public: KeyType cKey; };

template<typename KeyType>
class Record
{
public: KeyType cKey;
};

/** Base Template Class for the Database Handle. Processes main Lower Level Disk Communications. **/
template<typename KeyType, class RecordType> class Database
{
/** Mutex for Thread Synchronization. **/
boost::mutex DATABASE_MUTEX;
std::mutex DATABASE_MUTEX;

std::string strFilename;
typename std::map<KeyType, RecordType> mapRecords;
Expand Down
48 changes: 48 additions & 0 deletions src/LLP/block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef NEXUS_LLP_BLOCK_H
#define NEXUS_LLP_BLOCK_H

#include "../hash/uint1024.h"
#include "../hash/templates.h"
#include <memory>

#define BEGIN(a) ((char*)&(a))
#define END(a) ((char*)&((&(a))[1]))

namespace LLP {

/** Mock Class for Building Block Hash. **/
class CBlock
{
public:
using Uptr = std::unique_ptr<CBlock>;
using Sptr = std::shared_ptr<CBlock>;

/** Begin of Header. BEGIN(nVersion) **/
unsigned int nVersion;
uint1024 hashPrevBlock;
uint512 hashMerkleRoot;
unsigned int nChannel;
unsigned int nHeight;
unsigned int nBits;
uint64 nNonce;
/** End of Header. END(nNonce).
All the components to build an SK1024 Block Hash. **/


CBlock()
{
nVersion = 0;
hashPrevBlock = 0;
hashMerkleRoot = 0;
nChannel = 0;
nHeight = 0;
nBits = 0;
nNonce = 0;
}

inline uint1024 GetHash() const { return SK1024(BEGIN(nVersion), END(nBits)); }
inline uint1024 GetPrime() const { return GetHash() + nNonce; }
};
}

#endif
130 changes: 130 additions & 0 deletions src/LLP/connection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "connection.h"
#include "ddos.h"
#include "socket.h"
#include <algorithm>

namespace LLP
{
/** Connection Constructors **/
Connection::Connection() : SOCKET(), DDOS(NULL), INCOMING(), CONNECTED(false)
{
INCOMING.SetNull();
}

Connection::Connection(std::shared_ptr<Socket> SOCKET_IN, DDOS_Filter* DDOS_IN)
: SOCKET(SOCKET_IN)
, DDOS(DDOS_IN)
, INCOMING()
, CONNECTED(true)
{
TIMER.Start();
}

/** Checks for any flags in the Error Handle. **/
bool Connection::Errors()
{
return SOCKET->Error() != 0;
}

/** Determines if nTime seconds have elapsed since last Read / Write. **/
bool Connection::Timeout(unsigned int nTime)
{
return (TIMER.Elapsed() >= nTime);
}

/** Flag to determine if TCP Connection is still alive. **/
bool Connection::Connected()
{
return CONNECTED;
}

/** Handles two types of packets, requests which are of header >= 128, and data which are of header < 128. **/
bool Connection::PacketComplete()
{
return INCOMING.Complete();
}

/** Used to reset the packet to Null after it has been processed. This then flags the Connection to read another packet. **/
void Connection::ResetPacket()
{
INCOMING.SetNull();
}

/** Write a single packet to the TCP stream. **/
void Connection::WritePacket(Packet PACKET)
{
if (Errors())
return;

Write(PACKET.GetBytes());
}

/** Non-Blocking Packet reader to build a packet from TCP Connection.
This keeps thread from spending too much time for each Connection. **/
void Connection::ReadPacket()
{
/** Handle Reading Packet Type Header. **/
if (SOCKET->Available() >= 1 && INCOMING.IsNull())
{
std::vector<uint8_t> HEADER(1, 255);
if (Read(HEADER, 1) == 1)
INCOMING.HEADER = HEADER[0];

}

if (!INCOMING.IsNull() && !INCOMING.Complete())
{

/** Handle Reading Packet Length Header. **/
if (SOCKET->Available() >= 4 && INCOMING.LENGTH == 0)
{
std::vector<uint8_t> BYTES(4, 0);
if (Read(BYTES, 4) == 4)
{
INCOMING.SetLength(BYTES);
Event(0);
}
}

/** Handle Reading Packet Data. **/
uint32_t nAvailable = SOCKET->Available();
if (nAvailable > 0 && INCOMING.LENGTH > 0 && INCOMING.DATA.size() < INCOMING.LENGTH)
{
std::vector<uint8_t> DATA( std::min( std::min(nAvailable, 512u), (uint32_t)(INCOMING.LENGTH - INCOMING.DATA.size())), 0);

if (Read(DATA, DATA.size()) == DATA.size())
{
INCOMING.DATA.insert(INCOMING.DATA.end(), DATA.begin(), DATA.end());
//Event(EVENT_PACKET, DATA.size());
}
}
}
}

/** Disconnect Socket. **/
void Connection::Disconnect()
{
SOCKET->Close();

CONNECTED = false;
}

/** Lower level network communications: Read. Interacts with OS sockets. **/
size_t Connection::Read(std::vector<uint8_t> &DATA, size_t nBytes)
{
TIMER.Reset();

return SOCKET->Read(DATA, nBytes);
}

/** Lower level network communications: Write. Interacts with OS sockets. **/
void Connection::Write(std::vector<uint8_t> DATA)
{
SOCKET->Write(DATA, DATA.size());
}

std::string Connection::GetRemoteIPAddress() const
{
SOCKET->addr.ToStringIP();
}
}
88 changes: 88 additions & 0 deletions src/LLP/connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef NEXUS_LLP_CONNECTION_H
#define NEXUS_LLP_CONNECTION_H

#include "timer.h"
#include "packet.h"
#include <memory>

namespace LLP
{
/* forward declarations */
class DDOS_Filter;
class Socket;

/** Base Template class to handle outgoing / incoming LLP data for both Client and Server. **/
class Connection
{
protected:
/** Incoming Packet Being Built. **/
Packet INCOMING;

/** Basic Connection Variables. **/
Timer TIMER;
std::shared_ptr<Socket> SOCKET;

/** Connected Flag. **/
bool CONNECTED;

/**
Virtual Event Function to be Overridden allowing Custom Read Events.
Each event fired on Header Complete, and each time data is read to fill packet.
Useful to check Header length to maximum size of packet type for DDOS protection,
sending a keep-alive ping while downloading large files, etc.

nSize == 0 : Header Is Complete for Data Packet
nSize > 0 : Read nSize Bytes into Data Packet
**/
virtual inline void Event(unsigned int nSize = 0) { }

public:

/** DDOS Score if a Incoming Server Connection. **/
DDOS_Filter *DDOS;

/** Connection Constructors **/
Connection();

Connection(std::shared_ptr<Socket> SOCKET_IN, DDOS_Filter* DDOS_IN);

/** Checks for any flags in the Error Handle. **/
bool Errors();

/** Determines if nTime seconds have elapsed since last Read / Write. **/
bool Timeout(unsigned int nTime);

/** Flag to determine if TCP Connection is still alive. **/
bool Connected();

/** Handles two types of packets, requests which are of header >= 128, and data which are of header < 128. **/
bool PacketComplete();

/** Used to reset the packet to Null after it has been processed. This then flags the Connection to read another packet. **/
void ResetPacket();

/** Write a single packet to the TCP stream. **/
void WritePacket(Packet PACKET);

/** Non-Blocking Packet reader to build a packet from TCP Connection.
This keeps thread from spending too much time for each Connection. **/
void ReadPacket();

/** Disconnect Socket. **/
void Disconnect();

/** Get the IP address from remote endpoint **/
inline std::string GetRemoteIPAddress() const;

private:

/** Lower level network communications: Read. Interacts with OS sockets. **/
size_t Read(std::vector<unsigned char> &DATA, size_t nBytes);

/** Lower level network communications: Write. Interacts with OS sockets. **/
void Write(std::vector<unsigned char> DATA);

};
}

#endif
Loading