Skip to content

Commit

Permalink
Merge pull request #26 from orignal/master
Browse files Browse the repository at this point in the history
Merge pull request from orignal/master
  • Loading branch information
chertov committed Apr 6, 2014
2 parents 57dd362 + 1862896 commit 85884a5
Show file tree
Hide file tree
Showing 26 changed files with 547 additions and 246 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ local.properties
[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/

Expand Down Expand Up @@ -200,7 +199,6 @@ $RECYCLE.BIN/
*.egg
*.egg-info
dist/
build/
eggs/
parts/
var/
Expand Down
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
language: cpp
compiler: gcc
cache: apt
branches:
only:
- master
before_install:
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test # GCC 4.7
- sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu/ quantal main universe" # Boost 1.50
- sudo apt-get update -qq
- sudo apt-get install -qq libboost1.50-all-dev libcrypto++9 libcrypto++-dev
script:
- make
notifications:
email:
recipients:
- meeh@sigterm.no
on_success: change
on_failure: always
irc:
channels:
- "irc.freenode.net#i2p-dev"
template:
- "%{repository}/%{branch} (%{commit} - %{author}): %{message}"
on_failure: always
on_success: change


70 changes: 70 additions & 0 deletions AddressBook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <string.h>
#include <string>
#include <map>
#include "base64.h"
#include "util.h"
#include "Identity.h"
#include "Log.h"
#include "AddressBook.h"

namespace i2p
{
namespace data
{

AddressBook::AddressBook (): m_IsLoaded (false)
{
}


const IdentHash * AddressBook::FindAddress (const std::string& address)
{
if (!m_IsLoaded)
LoadHosts ();
auto it = m_Addresses.find (address);
if (it != m_Addresses.end ())
return &it->second;
else
return nullptr;
}


void AddressBook::LoadHosts ()
{
m_IsLoaded = true;
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
if (!f.is_open ())
{
LogPrint ("hosts.txt not found");
return;
}
int numAddresses = 0;

std::string s;

while (!f.eof ())
{
getline(f, s);

if (!s.length())
break;

size_t pos = s.find('=');

if (pos != std::string::npos)
{
std::string name = s.substr(0, pos++);
std::string addr = s.substr(pos);

Identity ident;
Base64ToByteStream (addr.c_str(), addr.length(), (uint8_t *)&ident, sizeof (ident));
m_Addresses[name] = CalculateIdentHash (ident);
numAddresses++;
}
}
LogPrint (numAddresses, " addresses loaded");
}

}
}

44 changes: 3 additions & 41 deletions AddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,12 @@ namespace data
{
public:

AddressBook (): m_IsLoaded (false) {};

const IdentHash * FindAddress (const std::string& address)
{
if (!m_IsLoaded)
LoadHosts ();
auto it = m_Addresses.find (address);
if (it != m_Addresses.end ())
return &it->second;
else
return nullptr;
}
AddressBook ();
const IdentHash * FindAddress (const std::string& address);

private:

void LoadHosts ()
{
m_IsLoaded = true;
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
if (!f.is_open ())
{
LogPrint ("hosts.txt not found");
return;
}
int numAddresses = 0;
char str[1024];
while (!f.eof ())
{
f.getline (str, 1024);
char * key = strchr (str, '=');
if (key)
{
*key = 0;
key++;
Identity ident;
Base64ToByteStream (key, strlen(key), (uint8_t *)&ident, sizeof (ident));
m_Addresses[str] = CalculateIdentHash (ident);
numAddresses++;
}
}
LogPrint (numAddresses, " addresses loaded");
}

private:
void LoadHosts ();

std::map<std::string, IdentHash> m_Addresses;
bool m_IsLoaded;
Expand Down
4 changes: 2 additions & 2 deletions HTTPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ namespace util
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
{
it->GetTunnelConfig ()->Print (s);
if (it->GetTunnelPool ())
if (it->GetTunnelPool () && !it->GetTunnelPool ()->IsExploratory ())
s << " " << "Pool";
if (it->IsFailed ())
s << " " << "Failed";
Expand All @@ -255,7 +255,7 @@ namespace util
for (auto it: i2p::tunnel::tunnels.GetInboundTunnels ())
{
it.second->GetTunnelConfig ()->Print (s);
if (it.second->GetTunnelPool ())
if (it.second->GetTunnelPool () && !it.second->GetTunnelPool ()->IsExploratory ())
s << " " << "Pool";
if (it.second->IsFailed ())
s << " " << "Failed";
Expand Down
6 changes: 5 additions & 1 deletion I2PEndian.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef I2PENDIAN_H__
#define I2PENDIAN_H__

#ifndef _WIN32
#ifdef __linux__
#include <endian.h>
#elif __FreeBSD__
#include <sys/endian.h>
#elif __MACH__ // Mac OS X
#include <machine/endian.h>
#else
#include <cstdint>

Expand Down
8 changes: 8 additions & 0 deletions Identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ namespace data
return keys;
}

void CreateRandomDHKeysPair (DHKeysPair * keys)
{
if (!keys) return;
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(rnd, keys->privateKey, keys->publicKey);
}

RoutingKey CreateRoutingKey (const IdentHash& ident)
{
uint8_t buf[41]; // ident + yyyymmdd
Expand Down
12 changes: 11 additions & 1 deletion Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace data
{
#pragma pack(1)

struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};

struct Keys
{
uint8_t privateKey[256];
Expand Down Expand Up @@ -71,6 +77,7 @@ namespace data

IdentHash CalculateIdentHash (const Identity& identity);
Keys CreateRandomKeys ();
void CreateRandomDHKeysPair (DHKeysPair * keys); // for transport sessions

// kademlia
struct RoutingKey
Expand Down Expand Up @@ -118,7 +125,10 @@ namespace data
{
public:

virtual void UpdateLeaseSet () = 0; // LeaseSet must be update
virtual const IdentHash& GetIdentHash () const = 0;
virtual const uint8_t * GetEncryptionPrivateKey () const = 0;
virtual const uint8_t * GetEncryptionPublicKey () const = 0;
virtual void UpdateLeaseSet () = 0; // LeaseSet must be updated
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OBJECTS = obj/i2p.o obj/base64.o obj/NTCPSession.o obj/RouterInfo.o obj/Transpor
obj/RouterContext.o obj/NetDb.o obj/LeaseSet.o obj/Tunnel.o obj/TunnelEndpoint.o \
obj/TunnelGateway.o obj/TransitTunnel.o obj/I2NPProtocol.o obj/Log.o obj/Garlic.o \
obj/HTTPServer.o obj/Streaming.o obj/Identity.o obj/SSU.o obj/util.o obj/Reseed.o \
obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o
obj/UPnP.o obj/TunnelPool.o obj/HTTPProxy.o obj/AddressBook.o
INCFLAGS =
LDFLAGS = -Wl,-rpath,/usr/local/lib -lcryptopp -lboost_system -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
LIBS =
Expand Down
27 changes: 21 additions & 6 deletions NTCPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ namespace ntcp
m_Socket (service), m_TerminationTimer (service), m_IsEstablished (false),
m_RemoteRouterInfo (in_RemoteRouterInfo), m_ReceiveBufferOffset (0), m_NextMessage (nullptr)
{
m_DHKeysPair = i2p::transports.GetNextDHKeysPair ();
}

NTCPSession::~NTCPSession ()
{
delete m_DHKeysPair;
}

void NTCPSession::CreateAESKey (uint8_t * pubKey, uint8_t * aesKey)
{
CryptoPP::DH dh (elgp, elgg);
CryptoPP::SecByteBlock secretKey(dh.AgreedValueLength());
if (!dh.Agree (secretKey, i2p::context.GetPrivateKey (), pubKey))
if (!dh.Agree (secretKey, m_DHKeysPair->privateKey, pubKey))
{
LogPrint ("Couldn't create shared key");
Terminate ();
Expand All @@ -50,11 +56,19 @@ namespace ntcp
{
m_IsEstablished = false;
m_Socket.close ();
i2p::transports.RemoveNTCPSession (this);
int numDelayed = 0;
for (auto it :m_DelayedMessages)
delete it;
{
// try to send them again
i2p::transports.SendMessage (m_RemoteRouterInfo.GetIdentHash (), it);
numDelayed++;
}
m_DelayedMessages.clear ();
if (numDelayed > 0)
LogPrint ("NTCP session ", numDelayed, " not sent");
// TODO: notify tunnels
i2p::transports.RemoveNTCPSession (this);

delete this;
LogPrint ("NTCP session terminated");
}
Expand All @@ -78,7 +92,7 @@ namespace ntcp
void NTCPSession::ClientLogin ()
{
// send Phase1
const uint8_t * x = i2p::context.GetRouterIdentity ().publicKey;
const uint8_t * x = m_DHKeysPair->publicKey;
memcpy (m_Phase1.pubKey, x, 256);
CryptoPP::SHA256().CalculateDigest(m_Phase1.HXxorHI, x, 256);
const uint8_t * ident = m_RemoteRouterInfo.GetIdentHash ();
Expand Down Expand Up @@ -143,7 +157,7 @@ namespace ntcp

void NTCPSession::SendPhase2 ()
{
const uint8_t * y = i2p::context.GetRouterIdentity ().publicKey;
const uint8_t * y = m_DHKeysPair->publicKey;
memcpy (m_Phase2.pubKey, y, 256);
uint8_t xy[512];
memcpy (xy, m_Phase1.pubKey, 256);
Expand Down Expand Up @@ -200,7 +214,7 @@ namespace ntcp
m_Decryption.ProcessData((uint8_t *)&m_Phase2.encrypted, (uint8_t *)&m_Phase2.encrypted, sizeof(m_Phase2.encrypted));
// verify
uint8_t xy[512], hxy[32];
memcpy (xy, i2p::context.GetRouterIdentity ().publicKey, 256);
memcpy (xy, m_DHKeysPair->publicKey, 256);
memcpy (xy + 256, m_Phase2.pubKey, 256);
CryptoPP::SHA256().CalculateDigest(hxy, xy, 512);
if (memcmp (hxy, m_Phase2.encrypted.hxy, 32))
Expand Down Expand Up @@ -321,6 +335,7 @@ namespace ntcp
if (ecode)
{
LogPrint ("Phase 4 read error: ", ecode.message ());
GetRemoteRouterInfo ().SetUnreachable (true); // this router doesn't like us
Terminate ();
}
else
Expand Down
4 changes: 3 additions & 1 deletion NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
#include <cryptopp/adler32.h>
#include "Identity.h"
#include "RouterInfo.h"
#include "I2NPProtocol.h"

Expand Down Expand Up @@ -66,7 +67,7 @@ namespace ntcp
public:

NTCPSession (boost::asio::io_service& service, i2p::data::RouterInfo& in_RemoteRouterInfo);
virtual ~NTCPSession () {};
virtual ~NTCPSession ();

boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
Expand Down Expand Up @@ -120,6 +121,7 @@ namespace ntcp
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished;
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server

CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption m_Decryption;
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption m_Encryption;
Expand Down
11 changes: 7 additions & 4 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,11 @@ namespace data

if (dest->IsExploratory ())
{
if (!FindRouter (router)) // router with ident not found
auto r = FindRouter (router);
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
{
LogPrint ("Found new router. Requesting RouterInfo ...");
// router with ident not found or too old (1 hour)
LogPrint ("Found new/outdated router. Requesting RouterInfo ...");
if (outbound && inbound)
{
RequestedDestination * d1 = CreateRequestedDestination (router, false, false);
Expand Down Expand Up @@ -532,8 +534,9 @@ namespace data

void NetDb::Explore ()
{
auto outbound = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
auto inbound = i2p::tunnel::tunnels.GetNextInboundTunnel ();
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
if (outbound && inbound)
{
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on Windows

Requires msvs2013, boost 1.46 and higher, crypto++

[![Build Status](https://travis-ci.org/orignal/i2pd.svg?branch=master)](https://travis-ci.org/orignal/i2pd)


Testing
-------
Expand Down
Loading

0 comments on commit 85884a5

Please sign in to comment.