mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-13 04:46:38 +01:00
commit
85b1505e51
105 changed files with 22391 additions and 22194 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,6 +5,8 @@ router.keys
|
||||||
i2p
|
i2p
|
||||||
libi2pd.so
|
libi2pd.so
|
||||||
netDb
|
netDb
|
||||||
|
tunnels.cfg
|
||||||
|
tests/tests
|
||||||
|
|
||||||
# Autotools
|
# Autotools
|
||||||
autom4te.cache
|
autom4te.cache
|
||||||
|
|
2
Daemon.h
2
Daemon.h
|
@ -53,6 +53,8 @@ namespace i2p
|
||||||
class DaemonLinux : public Daemon_Singleton
|
class DaemonLinux : public Daemon_Singleton
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
DaemonLinux() = default;
|
||||||
|
|
||||||
static DaemonLinux& Instance()
|
static DaemonLinux& Instance()
|
||||||
{
|
{
|
||||||
static DaemonLinux instance;
|
static DaemonLinux instance;
|
||||||
|
|
|
@ -353,7 +353,7 @@ namespace i2p
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE] = {};
|
||||||
if (HandleBuildRequestRecords (num, buf + 1, clearText))
|
if (HandleBuildRequestRecords (num, buf + 1, clearText))
|
||||||
{
|
{
|
||||||
if (clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x40) // we are endpoint of outboud tunnel
|
if (clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] & 0x40) // we are endpoint of outboud tunnel
|
||||||
|
|
|
@ -160,6 +160,7 @@ namespace tunnel
|
||||||
memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
|
memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
|
||||||
len = offset + other.GetLength ();
|
len = offset + other.GetLength ();
|
||||||
from = other.from;
|
from = other.from;
|
||||||
|
maxLen = other.maxLen;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +194,7 @@ namespace tunnel
|
||||||
struct I2NPMessageBuffer: public I2NPMessage
|
struct I2NPMessageBuffer: public I2NPMessage
|
||||||
{
|
{
|
||||||
I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; };
|
I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; };
|
||||||
uint8_t m_Buffer[sz + 16];
|
uint8_t m_Buffer[sz + 16] = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
I2NPMessage * NewI2NPMessage ();
|
I2NPMessage * NewI2NPMessage ();
|
||||||
|
|
12
Makefile
12
Makefile
|
@ -1,6 +1,7 @@
|
||||||
UNAME := $(shell uname -s)
|
UNAME := $(shell uname -s)
|
||||||
SHLIB := libi2pd.so
|
SHLIB := libi2pd.so
|
||||||
I2PD := i2p
|
I2PD := i2p
|
||||||
|
TESTS := tests/tests
|
||||||
GREP := fgrep
|
GREP := fgrep
|
||||||
DEPS := obj/make.dep
|
DEPS := obj/make.dep
|
||||||
|
|
||||||
|
@ -22,10 +23,11 @@ else # win32
|
||||||
DAEMON_SRC += DaemonWin32.cpp
|
DAEMON_SRC += DaemonWin32.cpp
|
||||||
endif
|
endif
|
||||||
|
|
||||||
all: mk_build_dir $(SHLIB) $(I2PD)
|
all: mk_build_dir $(SHLIB) $(I2PD) $(TESTS)
|
||||||
|
|
||||||
mk_build_dir:
|
mk_build_dir:
|
||||||
mkdir -p obj
|
mkdir -p obj
|
||||||
|
mkdir -p obj/tests
|
||||||
|
|
||||||
api: $(SHLIB)
|
api: $(SHLIB)
|
||||||
|
|
||||||
|
@ -38,11 +40,13 @@ api: $(SHLIB)
|
||||||
|
|
||||||
deps:
|
deps:
|
||||||
@mkdir -p obj
|
@mkdir -p obj
|
||||||
|
@mkdir -p obj/tests
|
||||||
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) -MM *.cpp > $(DEPS)
|
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) -MM *.cpp > $(DEPS)
|
||||||
@sed -i -e '/\.o:/ s/^/obj\//' $(DEPS)
|
@sed -i -e '/\.o:/ s/^/obj\//' $(DEPS)
|
||||||
|
|
||||||
obj/%.o : %.cpp
|
obj/%.o : %.cpp
|
||||||
@mkdir -p obj
|
@mkdir -p obj
|
||||||
|
@mkdir -p obj/tests
|
||||||
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(CPU_FLAGS) -c -o $@ $<
|
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(CPU_FLAGS) -c -o $@ $<
|
||||||
|
|
||||||
# '-' is 'ignore if missing' on first run
|
# '-' is 'ignore if missing' on first run
|
||||||
|
@ -56,9 +60,13 @@ ifneq ($(USE_STATIC),yes)
|
||||||
$(CXX) $(LDFLAGS) $(LDLIBS) -shared -o $@ $^
|
$(CXX) $(LDFLAGS) $(LDLIBS) -shared -o $@ $^
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
$(TESTS): $(patsubst %.cpp,obj/%.o,$(TESTS_SRC))
|
||||||
|
$(CXX) -o $@ $^ $(LDLIBS) $(LDTESTLIBS) $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf obj
|
rm -rf obj
|
||||||
$(RM) $(I2PD) $(SHLIB)
|
$(RM) $(I2PD) $(SHLIB) $(TESTS)
|
||||||
|
|
||||||
LATEST_TAG=$(shell git describe --tags --abbrev=0 master)
|
LATEST_TAG=$(shell git describe --tags --abbrev=0 master)
|
||||||
dist:
|
dist:
|
||||||
|
|
|
@ -10,3 +10,4 @@ NEEDED_CXXFLAGS = -std=c++11
|
||||||
INCFLAGS = -I/usr/include/ -I/usr/local/include/
|
INCFLAGS = -I/usr/include/ -I/usr/local/include/
|
||||||
LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
||||||
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
||||||
|
LDTESTLIBS = -lboost_unit_test_framework
|
||||||
|
|
|
@ -40,6 +40,9 @@ else
|
||||||
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# Always link with test framework dynamically
|
||||||
|
LDTESTLIBS = -lboost_unit_test_framework
|
||||||
|
|
||||||
# UPNP Support (miniupnpc 1.5 or 1.6)
|
# UPNP Support (miniupnpc 1.5 or 1.6)
|
||||||
ifeq ($(USE_UPNP),1)
|
ifeq ($(USE_UPNP),1)
|
||||||
LDFLAGS += -ldl
|
LDFLAGS += -ldl
|
||||||
|
|
|
@ -4,6 +4,7 @@ CXXFLAGS = -g -Wall -std=c++11 -DCRYPTOPP_DISABLE_ASM -DMAC_OSX
|
||||||
INCFLAGS = -I/usr/local/include
|
INCFLAGS = -I/usr/local/include
|
||||||
LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
||||||
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
LDLIBS = -lcryptopp -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_program_options -lpthread
|
||||||
|
LDTESTLIBS = -lboost_unit_test_framework
|
||||||
|
|
||||||
ifeq ($(USE_UPNP),1)
|
ifeq ($(USE_UPNP),1)
|
||||||
LDFLAGS += -ldl
|
LDFLAGS += -ldl
|
||||||
|
|
|
@ -287,7 +287,7 @@ namespace data
|
||||||
if (m_Caps & eReachable) caps += CAPS_FLAG_REACHABLE; // reachable
|
if (m_Caps & eReachable) caps += CAPS_FLAG_REACHABLE; // reachable
|
||||||
if (m_Caps & eUnreachable) caps += CAPS_FLAG_UNREACHABLE; // unreachable
|
if (m_Caps & eUnreachable) caps += CAPS_FLAG_UNREACHABLE; // unreachable
|
||||||
|
|
||||||
SetProperty ("caps", caps.c_str ());
|
SetProperty ("caps", caps);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RouterInfo::WriteToStream (std::ostream& s)
|
void RouterInfo::WriteToStream (std::ostream& s)
|
||||||
|
|
|
@ -87,6 +87,7 @@ namespace data
|
||||||
|
|
||||||
RouterInfo (const std::string& fullPath);
|
RouterInfo (const std::string& fullPath);
|
||||||
RouterInfo (): m_Buffer (nullptr) { };
|
RouterInfo (): m_Buffer (nullptr) { };
|
||||||
|
|
||||||
RouterInfo (const RouterInfo& ) = default;
|
RouterInfo (const RouterInfo& ) = default;
|
||||||
RouterInfo& operator=(const RouterInfo& ) = default;
|
RouterInfo& operator=(const RouterInfo& ) = default;
|
||||||
RouterInfo (const uint8_t * buf, int len);
|
RouterInfo (const uint8_t * buf, int len);
|
||||||
|
|
|
@ -275,7 +275,7 @@ namespace transport
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buf[320 + 18]; // 304 bytes for ipv4, 320 for ipv6
|
uint8_t buf[320 + 18] = {}; // 304 bytes for ipv4, 320 for ipv6, all set to 0
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
memcpy (payload, m_DHKeysPair->publicKey, 256); // x
|
memcpy (payload, m_DHKeysPair->publicKey, 256); // x
|
||||||
bool isV4 = m_RemoteEndpoint.address ().is_v4 ();
|
bool isV4 = m_RemoteEndpoint.address ().is_v4 ();
|
||||||
|
@ -306,7 +306,7 @@ namespace transport
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buf[96 + 18];
|
uint8_t buf[96 + 18] = {};
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
htobe32buf (payload, iTag);
|
htobe32buf (payload, iTag);
|
||||||
payload += 4;
|
payload += 4;
|
||||||
|
@ -344,7 +344,7 @@ namespace transport
|
||||||
SignedData s; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
SignedData s; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
||||||
s.Insert (x, 256); // x
|
s.Insert (x, 256); // x
|
||||||
|
|
||||||
uint8_t buf[384 + 18];
|
uint8_t buf[384 + 18] = {};
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
memcpy (payload, m_DHKeysPair->publicKey, 256);
|
memcpy (payload, m_DHKeysPair->publicKey, 256);
|
||||||
s.Insert (payload, 256); // y
|
s.Insert (payload, 256); // y
|
||||||
|
@ -408,7 +408,7 @@ namespace transport
|
||||||
|
|
||||||
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen)
|
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, size_t ourAddressLen)
|
||||||
{
|
{
|
||||||
uint8_t buf[512 + 18];
|
uint8_t buf[512 + 18] = {};
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
*payload = 1; // 1 fragment
|
*payload = 1; // 1 fragment
|
||||||
payload++; // info
|
payload++; // info
|
||||||
|
@ -475,7 +475,7 @@ namespace transport
|
||||||
void SSUSession::SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
void SSUSession::SendRelayResponse (uint32_t nonce, const boost::asio::ip::udp::endpoint& from,
|
||||||
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to)
|
const uint8_t * introKey, const boost::asio::ip::udp::endpoint& to)
|
||||||
{
|
{
|
||||||
uint8_t buf[80 + 18]; // 64 Alice's ipv4 and 80 Alice's ipv6
|
uint8_t buf[80 + 18] = {}; // 64 Alice's ipv4 and 80 Alice's ipv6
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
// Charlie's address always v4
|
// Charlie's address always v4
|
||||||
if (!to.address ().is_v4 ())
|
if (!to.address ().is_v4 ())
|
||||||
|
@ -536,7 +536,7 @@ namespace transport
|
||||||
LogPrint (eLogError, "Alice's IP must be v4");
|
LogPrint (eLogError, "Alice's IP must be v4");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t buf[48 + 18];
|
uint8_t buf[48 + 18] = {};
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
*payload = 4;
|
*payload = 4;
|
||||||
payload++; // size
|
payload++; // size
|
||||||
|
@ -616,9 +616,7 @@ namespace transport
|
||||||
htobe32buf (&(header->time), i2p::util::GetSecondsSinceEpoch ());
|
htobe32buf (&(header->time), i2p::util::GetSecondsSinceEpoch ());
|
||||||
uint8_t * encrypted = &header->flag;
|
uint8_t * encrypted = &header->flag;
|
||||||
uint16_t encryptedLen = len - (encrypted - buf);
|
uint16_t encryptedLen = len - (encrypted - buf);
|
||||||
i2p::crypto::CBCEncryption encryption;
|
i2p::crypto::CBCEncryption encryption(aesKey, iv);
|
||||||
encryption.SetKey (aesKey);
|
|
||||||
encryption.SetIV (iv);
|
|
||||||
encryption.Encrypt (encrypted, encryptedLen, encrypted);
|
encryption.Encrypt (encrypted, encryptedLen, encrypted);
|
||||||
// assume actual buffer size is 18 (16 + 2) bytes more
|
// assume actual buffer size is 18 (16 + 2) bytes more
|
||||||
memcpy (buf + len, iv, 16);
|
memcpy (buf + len, iv, 16);
|
||||||
|
@ -960,8 +958,7 @@ namespace transport
|
||||||
// toAddress is true for Alice<->Chalie communications only
|
// toAddress is true for Alice<->Chalie communications only
|
||||||
// sendAddress is false if message comes from Alice
|
// sendAddress is false if message comes from Alice
|
||||||
{
|
{
|
||||||
uint8_t buf[80 + 18];
|
uint8_t buf[80 + 18] = {};
|
||||||
uint8_t iv[16];
|
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
htobe32buf (payload, nonce);
|
htobe32buf (payload, nonce);
|
||||||
payload += 4; // nonce
|
payload += 4; // nonce
|
||||||
|
@ -995,6 +992,7 @@ namespace transport
|
||||||
|
|
||||||
// send
|
// send
|
||||||
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
||||||
|
uint8_t iv[16];
|
||||||
rnd.GenerateBlock (iv, 16); // random iv
|
rnd.GenerateBlock (iv, 16); // random iv
|
||||||
if (toAddress)
|
if (toAddress)
|
||||||
{
|
{
|
||||||
|
@ -1032,7 +1030,7 @@ namespace transport
|
||||||
{
|
{
|
||||||
if (m_State == eSessionStateEstablished)
|
if (m_State == eSessionStateEstablished)
|
||||||
{
|
{
|
||||||
uint8_t buf[48 + 18];
|
uint8_t buf[48 + 18] = {};
|
||||||
uint8_t * payload = buf + sizeof (SSUHeader);
|
uint8_t * payload = buf + sizeof (SSUHeader);
|
||||||
*payload = 0; // flags
|
*payload = 0; // flags
|
||||||
payload++;
|
payload++;
|
||||||
|
@ -1049,7 +1047,7 @@ namespace transport
|
||||||
{
|
{
|
||||||
if (m_IsSessionKey)
|
if (m_IsSessionKey)
|
||||||
{
|
{
|
||||||
uint8_t buf[48 + 18];
|
uint8_t buf[48 + 18] = {};
|
||||||
// encrypt message with session key
|
// encrypt message with session key
|
||||||
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48);
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48);
|
||||||
try
|
try
|
||||||
|
@ -1066,7 +1064,7 @@ namespace transport
|
||||||
|
|
||||||
void SSUSession::Send (uint8_t type, const uint8_t * payload, size_t len)
|
void SSUSession::Send (uint8_t type, const uint8_t * payload, size_t len)
|
||||||
{
|
{
|
||||||
uint8_t buf[SSU_MTU_V4 + 18];
|
uint8_t buf[SSU_MTU_V4 + 18] = {};
|
||||||
size_t msgSize = len + sizeof (SSUHeader);
|
size_t msgSize = len + sizeof (SSUHeader);
|
||||||
size_t paddingSize = msgSize & 0x0F; // %16
|
size_t paddingSize = msgSize & 0x0F; // %16
|
||||||
if (paddingSize > 0) msgSize += (16 - paddingSize);
|
if (paddingSize > 0) msgSize += (16 - paddingSize);
|
||||||
|
|
|
@ -15,6 +15,8 @@ namespace i2p
|
||||||
namespace transport
|
namespace transport
|
||||||
{
|
{
|
||||||
#pragma pack(1)
|
#pragma pack(1)
|
||||||
|
// Warning: do not change the order of these variables
|
||||||
|
// (or fix the unsafe casts in SSU.h)
|
||||||
struct SSUHeader
|
struct SSUHeader
|
||||||
{
|
{
|
||||||
uint8_t mac[16];
|
uint8_t mac[16];
|
||||||
|
|
13
TODO
Normal file
13
TODO
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
Short-term refactoring:
|
||||||
|
- SSUSession:637, SSUSession:635 get rid of casting to SSUHeader
|
||||||
|
- Identity.cpp:156 check for self asignment
|
||||||
|
|
||||||
|
Long-term refactoring:
|
||||||
|
- Rely on a library for TLS and SSL.
|
||||||
|
- Move parsing code out of networking code, to allow better testing.
|
||||||
|
- Separate front-end code (SAM, BOB, ...) from the back-end.
|
||||||
|
|
||||||
|
|
||||||
|
Additions:
|
||||||
|
- Write tests.
|
||||||
|
- Add documentation.
|
|
@ -491,10 +491,14 @@ namespace transport
|
||||||
|
|
||||||
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const
|
||||||
{
|
{
|
||||||
|
if(m_Peers.empty()) // ensure m.Peers.size() >= 1
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator();
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator();
|
||||||
auto it = m_Peers.begin();
|
auto it = m_Peers.begin();
|
||||||
std::advance(it, rnd.GenerateWord32(0, m_Peers.size () - 1));
|
std::advance(it, rnd.GenerateWord32(0, m_Peers.size () - 1));
|
||||||
return it != m_Peers.end () ? it->second.router : nullptr;
|
|
||||||
|
return it->second.router;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,7 @@ namespace tunnel
|
||||||
|
|
||||||
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
|
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
|
||||||
{
|
{
|
||||||
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE] = {};
|
||||||
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
||||||
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, router->GetIdentHash (), 32);
|
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, router->GetIdentHash (), 32);
|
||||||
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
||||||
|
|
6
aes.h
6
aes.h
|
@ -141,6 +141,12 @@ namespace crypto
|
||||||
public:
|
public:
|
||||||
|
|
||||||
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
CBCEncryption () { memset (m_LastBlock.buf, 0, 16); };
|
||||||
|
CBCEncryption(const AESKey& key, const uint8_t* iv)
|
||||||
|
: CBCEncryption()
|
||||||
|
{
|
||||||
|
SetKey(key);
|
||||||
|
SetIV(iv);
|
||||||
|
};
|
||||||
|
|
||||||
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
void SetKey (const AESKey& key) { m_ECBEncryption.SetKey (key); }; // 32 bytes
|
||||||
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
void SetIV (const uint8_t * iv) { memcpy (m_LastBlock.buf, iv, 16); }; // 16 bytes
|
||||||
|
|
|
@ -23,3 +23,6 @@ DAEMON_SRC = $(COMMON_SRC) \
|
||||||
|
|
||||||
LIB_SRC := $(COMMON_SRC) \
|
LIB_SRC := $(COMMON_SRC) \
|
||||||
api.cpp
|
api.cpp
|
||||||
|
|
||||||
|
TESTS_SRC := $(COMMON_SRC) \
|
||||||
|
tests/Utility.cpp tests/Identity.cpp
|
||||||
|
|
1
hmac.h
1
hmac.h
|
@ -58,4 +58,3 @@ namespace crypto
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
12
tests/Identity.cpp
Normal file
12
tests/Identity.cpp
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#define BOOST_TEST_MAIN
|
||||||
|
#define BOOST_TEST_DYN_LINK
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include "../Identity.h"
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(IdentityTests)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
91
tests/Utility.cpp
Normal file
91
tests/Utility.cpp
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include "../util.h"
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(UtilityTests)
|
||||||
|
|
||||||
|
using namespace i2p::util::http;
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(DecodeEmptyUrl)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(urlDecode(""), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(DecodeUrl)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(urlDecode("%20"), " ");
|
||||||
|
}
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlProtocol)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").protocol_, "http");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").protocol_, "http");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").protocol_, "ftp");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").protocol_, "ssh");
|
||||||
|
BOOST_CHECK_EQUAL(url("").protocol_, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlHost)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").host_, "127.0.0.1");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").host_, "site.com");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").host_, "localhost");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").host_, "localhost");
|
||||||
|
BOOST_CHECK_EQUAL(url("").host_, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlPath)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").path_, "/asdasd");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").path_, "/A/B");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").path_, "/A/B/C/D");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").path_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("").path_, "");
|
||||||
|
}
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlQuery)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").query_, "qqqqqqqqqqqq");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").query_, "q");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").query_, "x=A");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").query_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("").query_, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlPortStr)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").portstr_, "7070");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").portstr_, "err_port");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").portstr_, "123");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").portstr_, "123");
|
||||||
|
BOOST_CHECK_EQUAL(url("").portstr_, "80");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlPort)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").port_, 7070);
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").port_, 80);
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").port_, 123);
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").port_, 123);
|
||||||
|
BOOST_CHECK_EQUAL(url("").port_, 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlUser)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").user_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").user_, "user");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").user_, "user");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://@localhost:123").user_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://user:@localhost:123").user_, "user");
|
||||||
|
BOOST_CHECK_EQUAL(url("").user_, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(ParseUrlPassword)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").pass_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").pass_, "password");
|
||||||
|
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").pass_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://@localhost:123").pass_, "");
|
||||||
|
BOOST_CHECK_EQUAL(url("SSH://:password@localhost:123").pass_, "password");
|
||||||
|
BOOST_CHECK_EQUAL(url("").pass_, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
497
util.cpp
497
util.cpp
|
@ -8,7 +8,6 @@
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
#include <boost/filesystem/fstream.hpp>
|
#include <boost/filesystem/fstream.hpp>
|
||||||
#include <boost/foreach.hpp>
|
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
#include <boost/program_options/detail/config_file.hpp>
|
#include <boost/program_options/detail/config_file.hpp>
|
||||||
#include <boost/program_options/parsers.hpp>
|
#include <boost/program_options/parsers.hpp>
|
||||||
|
@ -60,13 +59,10 @@ http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p {
|
||||||
{
|
namespace util {
|
||||||
namespace util
|
|
||||||
{
|
|
||||||
|
|
||||||
namespace config
|
namespace config {
|
||||||
{
|
|
||||||
std::map<std::string, std::string> mapArgs;
|
std::map<std::string, std::string> mapArgs;
|
||||||
std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
||||||
|
|
||||||
|
@ -74,13 +70,11 @@ namespace config
|
||||||
{
|
{
|
||||||
mapArgs.clear();
|
mapArgs.clear();
|
||||||
mapMultiArgs.clear();
|
mapMultiArgs.clear();
|
||||||
for (int i = 1; i < argc; i++)
|
for(int i = 1; i < argc; ++i) {
|
||||||
{
|
|
||||||
std::string strKey (argv[i]);
|
std::string strKey (argv[i]);
|
||||||
std::string strValue;
|
std::string strValue;
|
||||||
size_t has_data = strKey.find('=');
|
size_t has_data = strKey.find('=');
|
||||||
if (has_data != std::string::npos)
|
if(has_data != std::string::npos) {
|
||||||
{
|
|
||||||
strValue = strKey.substr(has_data+1);
|
strValue = strKey.substr(has_data+1);
|
||||||
strKey = strKey.substr(0, has_data);
|
strKey = strKey.substr(0, has_data);
|
||||||
}
|
}
|
||||||
|
@ -97,13 +91,11 @@ namespace config
|
||||||
mapMultiArgs[strKey].push_back(strValue);
|
mapMultiArgs[strKey].push_back(strValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FOREACH(PAIRTYPE(const std::string,std::string)& entry, mapArgs)
|
for(auto& entry : mapArgs) {
|
||||||
{
|
|
||||||
std::string name = entry.first;
|
std::string name = entry.first;
|
||||||
|
|
||||||
// interpret --foo as -foo (as long as both are not set)
|
// interpret --foo as -foo (as long as both are not set)
|
||||||
if (name.find("--") == 0)
|
if (name.find("--") == 0) {
|
||||||
{
|
|
||||||
std::string singleDash(name.begin()+1, name.end());
|
std::string singleDash(name.begin()+1, name.end());
|
||||||
if (mapArgs.count(singleDash) == 0)
|
if (mapArgs.count(singleDash) == 0)
|
||||||
mapArgs[singleDash] = entry.second;
|
mapArgs[singleDash] = entry.second;
|
||||||
|
@ -129,9 +121,10 @@ namespace config
|
||||||
int GetArg(const std::string& strArg, int nDefault)
|
int GetArg(const std::string& strArg, int nDefault)
|
||||||
{
|
{
|
||||||
if(mapArgs.count(strArg))
|
if(mapArgs.count(strArg))
|
||||||
return atoi(mapArgs[strArg].c_str());
|
return stoi(mapArgs[strArg]);
|
||||||
return nDefault;
|
return nDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace filesystem
|
namespace filesystem
|
||||||
|
@ -159,11 +152,9 @@ namespace filesystem
|
||||||
else */
|
else */
|
||||||
path = GetDefaultDataDir();
|
path = GetDefaultDataDir();
|
||||||
|
|
||||||
if (!boost::filesystem::exists( path ))
|
if(!boost::filesystem::exists(path)) {
|
||||||
{
|
|
||||||
// Create data directory
|
// Create data directory
|
||||||
if (!boost::filesystem::create_directory( path ))
|
if(!boost::filesystem::create_directory(path)) {
|
||||||
{
|
|
||||||
LogPrint("Failed to create data directory!");
|
LogPrint("Failed to create data directory!");
|
||||||
path = "";
|
path = "";
|
||||||
return path;
|
return path;
|
||||||
|
@ -189,38 +180,18 @@ namespace filesystem
|
||||||
boost::filesystem::path GetConfigFile()
|
boost::filesystem::path GetConfigFile()
|
||||||
{
|
{
|
||||||
boost::filesystem::path pathConfigFile(i2p::util::config::GetArg("-conf", "i2p.conf"));
|
boost::filesystem::path pathConfigFile(i2p::util::config::GetArg("-conf", "i2p.conf"));
|
||||||
if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir() / pathConfigFile;
|
if(!pathConfigFile.is_complete())
|
||||||
|
pathConfigFile = GetDataDir() / pathConfigFile;
|
||||||
return pathConfigFile;
|
return pathConfigFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
|
|
||||||
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet)
|
|
||||||
{
|
|
||||||
boost::filesystem::ifstream streamConfig(GetConfigFile());
|
|
||||||
if (!streamConfig.good())
|
|
||||||
return; // No i2pd.conf file is OK
|
|
||||||
|
|
||||||
std::set<std::string> setOptions;
|
|
||||||
setOptions.insert("*");
|
|
||||||
|
|
||||||
for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
|
|
||||||
{
|
|
||||||
// Don't overwrite existing settings so command line settings override i2pd.conf
|
|
||||||
std::string strKey = std::string("-") + it->string_key;
|
|
||||||
if (mapSettingsRet.count(strKey) == 0)
|
|
||||||
{
|
|
||||||
mapSettingsRet[strKey] = it->value[0];
|
|
||||||
}
|
|
||||||
mapMultiSettingsRet[strKey].push_back(it->value[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::filesystem::path GetDefaultDataDir()
|
boost::filesystem::path GetDefaultDataDir()
|
||||||
{
|
{
|
||||||
// Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd
|
// Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd
|
||||||
// Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd
|
// Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd
|
||||||
// Mac: ~/Library/Application Support/i2pd
|
// Mac: ~/Library/Application Support/i2pd
|
||||||
// Unix: ~/.i2pd or /var/lib/i2pd is system=1
|
// Unix: ~/.i2pd or /var/lib/i2pd is system=1
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
// Windows
|
// Windows
|
||||||
char localAppData[MAX_PATH];
|
char localAppData[MAX_PATH];
|
||||||
|
@ -247,6 +218,27 @@ namespace filesystem
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
|
||||||
|
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet)
|
||||||
|
{
|
||||||
|
boost::filesystem::ifstream streamConfig(GetConfigFile());
|
||||||
|
if(!streamConfig.good())
|
||||||
|
return; // No i2pd.conf file is OK
|
||||||
|
|
||||||
|
std::set<std::string> setOptions;
|
||||||
|
setOptions.insert("*");
|
||||||
|
|
||||||
|
for(boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end;
|
||||||
|
it != end; ++it) {
|
||||||
|
// Don't overwrite existing settings so command line settings override i2pd.conf
|
||||||
|
std::string strKey = std::string("-") + it->string_key;
|
||||||
|
if(mapSettingsRet.count(strKey) == 0) {
|
||||||
|
mapSettingsRet[strKey] = it->value[0];
|
||||||
|
}
|
||||||
|
mapMultiSettingsRet[strKey].push_back(it->value[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
boost::filesystem::path GetCertificatesDir()
|
boost::filesystem::path GetCertificatesDir()
|
||||||
{
|
{
|
||||||
return GetDataDir () / "certificates";
|
return GetDataDir () / "certificates";
|
||||||
|
@ -257,8 +249,7 @@ namespace http
|
||||||
{
|
{
|
||||||
std::string httpRequest(const std::string& address)
|
std::string httpRequest(const std::string& address)
|
||||||
{
|
{
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
i2p::util::http::url u(address);
|
i2p::util::http::url u(address);
|
||||||
boost::asio::ip::tcp::iostream site;
|
boost::asio::ip::tcp::iostream site;
|
||||||
// please don't uncomment following line because it's not compatible with boost 1.46
|
// please don't uncomment following line because it's not compatible with boost 1.46
|
||||||
|
@ -266,27 +257,22 @@ namespace http
|
||||||
//site.expires_from_now (boost::posix_time::seconds(30));
|
//site.expires_from_now (boost::posix_time::seconds(30));
|
||||||
if(u.port_ == 80)
|
if(u.port_ == 80)
|
||||||
site.connect(u.host_, "http");
|
site.connect(u.host_, "http");
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
std::stringstream ss; ss << u.port_;
|
std::stringstream ss; ss << u.port_;
|
||||||
site.connect(u.host_, ss.str());
|
site.connect(u.host_, ss.str());
|
||||||
}
|
}
|
||||||
if (site)
|
if(site) {
|
||||||
{
|
|
||||||
// User-Agent is needed to get the server list routerInfo files.
|
// User-Agent is needed to get the server list routerInfo files.
|
||||||
site << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_
|
site << "GET " << u.path_ << " HTTP/1.1\r\nHost: " << u.host_
|
||||||
<< "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n" << "Connection: close\r\n\r\n";
|
<< "\r\nAccept: */*\r\n" << "User-Agent: Wget/1.11.4\r\n"
|
||||||
|
<< "Connection: close\r\n\r\n";
|
||||||
// read response and extract content
|
// read response and extract content
|
||||||
return GetHttpContent(site);
|
return GetHttpContent(site);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint("Can't connect to ", address);
|
LogPrint("Can't connect to ", address);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
} catch(const std::exception& ex) {
|
||||||
catch (std::exception& ex)
|
|
||||||
{
|
|
||||||
LogPrint("Failed to download ", address, " : ", ex.what());
|
LogPrint("Failed to download ", address, " : ", ex.what());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -299,16 +285,13 @@ namespace http
|
||||||
int status;
|
int status;
|
||||||
response >> status; // status
|
response >> status; // status
|
||||||
std::getline (response, statusMessage);
|
std::getline (response, statusMessage);
|
||||||
if (status == 200) // OK
|
if(status == 200) { // OK
|
||||||
{
|
|
||||||
bool isChunked = false;
|
bool isChunked = false;
|
||||||
std::string header;
|
std::string header;
|
||||||
while (!response.eof () && header != "\r")
|
while(!response.eof() && header != "\r") {
|
||||||
{
|
|
||||||
std::getline(response, header);
|
std::getline(response, header);
|
||||||
auto colon = header.find (':');
|
auto colon = header.find (':');
|
||||||
if (colon != std::string::npos)
|
if(colon != std::string::npos) {
|
||||||
{
|
|
||||||
std::string field = header.substr (0, colon);
|
std::string field = header.substr (0, colon);
|
||||||
if(field == i2p::util::http::TRANSFER_ENCODING)
|
if(field == i2p::util::http::TRANSFER_ENCODING)
|
||||||
isChunked = (header.find("chunked", colon + 1) != std::string::npos);
|
isChunked = (header.find("chunked", colon + 1) != std::string::npos);
|
||||||
|
@ -320,10 +303,9 @@ namespace http
|
||||||
MergeChunkedResponse(response, ss);
|
MergeChunkedResponse(response, ss);
|
||||||
else
|
else
|
||||||
ss << response.rdbuf();
|
ss << response.rdbuf();
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint("HTTP response ", status);
|
LogPrint("HTTP response ", status);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -331,14 +313,14 @@ namespace http
|
||||||
|
|
||||||
void MergeChunkedResponse(std::istream& response, std::ostream& merged)
|
void MergeChunkedResponse(std::istream& response, std::ostream& merged)
|
||||||
{
|
{
|
||||||
while (!response.eof ())
|
while(!response.eof()) {
|
||||||
{
|
|
||||||
std::string hexLen;
|
std::string hexLen;
|
||||||
int len;
|
int len;
|
||||||
std::getline(response, hexLen);
|
std::getline(response, hexLen);
|
||||||
std::istringstream iss(hexLen);
|
std::istringstream iss(hexLen);
|
||||||
iss >> std::hex >> len;
|
iss >> std::hex >> len;
|
||||||
if (!len) break;
|
if(!len)
|
||||||
|
break;
|
||||||
char* buf = new char[len];
|
char* buf = new char[len];
|
||||||
response.read(buf, len);
|
response.read(buf, len);
|
||||||
merged.write(buf, len);
|
merged.write(buf, len);
|
||||||
|
@ -350,8 +332,7 @@ namespace http
|
||||||
int httpRequestViaI2pProxy(const std::string& address, std::string &content)
|
int httpRequestViaI2pProxy(const std::string& address, std::string &content)
|
||||||
{
|
{
|
||||||
content = "";
|
content = "";
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
boost::asio::ip::tcp::iostream site;
|
boost::asio::ip::tcp::iostream site;
|
||||||
// please don't uncomment following line because it's not compatible with boost 1.46
|
// please don't uncomment following line because it's not compatible with boost 1.46
|
||||||
// 1.46 is default boost for Ubuntu 12.04 LTS
|
// 1.46 is default boost for Ubuntu 12.04 LTS
|
||||||
|
@ -360,8 +341,7 @@ namespace http
|
||||||
std::stringstream ss; ss << i2p::util::config::GetArg("-httpproxyport", 4446);
|
std::stringstream ss; ss << i2p::util::config::GetArg("-httpproxyport", 4446);
|
||||||
site.connect("127.0.0.1", ss.str());
|
site.connect("127.0.0.1", ss.str());
|
||||||
}
|
}
|
||||||
if (site)
|
if(site) {
|
||||||
{
|
|
||||||
i2p::util::http::url u(address);
|
i2p::util::http::url u(address);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "GET " << address << " HTTP/1.0" << std::endl;
|
ss << "GET " << address << " HTTP/1.0" << std::endl;
|
||||||
|
@ -378,29 +358,22 @@ namespace http
|
||||||
int status;
|
int status;
|
||||||
site >> status; // status
|
site >> status; // status
|
||||||
std::getline(site, statusMessage);
|
std::getline(site, statusMessage);
|
||||||
if (status == 200) // OK
|
if(status == 200) { // OK
|
||||||
{
|
|
||||||
std::string header;
|
std::string header;
|
||||||
while(std::getline(site, header) && header != "\r"){}
|
while(std::getline(site, header) && header != "\r"){}
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << site.rdbuf();
|
ss << site.rdbuf();
|
||||||
content = ss.str();
|
content = ss.str();
|
||||||
return status;
|
return status;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint("HTTP response ", status);
|
LogPrint("HTTP response ", status);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint("Can't connect to proxy");
|
LogPrint("Can't connect to proxy");
|
||||||
return 408;
|
return 408;
|
||||||
}
|
}
|
||||||
}
|
} catch (std::exception& ex) {
|
||||||
catch (std::exception& ex)
|
|
||||||
{
|
|
||||||
LogPrint("Failed to download ", address, " : ", ex.what());
|
LogPrint("Failed to download ", address, " : ", ex.what());
|
||||||
return 408;
|
return 408;
|
||||||
}
|
}
|
||||||
|
@ -417,47 +390,34 @@ namespace http
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// code for parser tests
|
|
||||||
//{
|
|
||||||
// i2p::util::http::url u_0("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_1("http://user:password@site.com:8080/asdasd?qqqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_2("http://user:password@site.com/asdasd?qqqqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_3("http://user:@site.com/asdasd?qqqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_4("http://user@site.com/asdasd?qqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_5("http://@site.com:800/asdasd?qqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_6("http://@site.com:err_port/asdasd?qqqqqqqqqqqq");
|
|
||||||
// i2p::util::http::url u_7("http://user:password@site.com:err_port/asdasd?qqqqqqqqqqqq");
|
|
||||||
//}
|
|
||||||
void url::parse(const std::string& url_s)
|
void url::parse(const std::string& url_s)
|
||||||
{
|
{
|
||||||
const std::string prot_end("://");
|
const std::string prot_end("://");
|
||||||
std::string::const_iterator prot_i = search(url_s.begin(), url_s.end(),
|
std::string::const_iterator prot_i = search(
|
||||||
prot_end.begin(), prot_end.end());
|
url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end()
|
||||||
|
);
|
||||||
protocol_.reserve(distance(url_s.begin(), prot_i));
|
protocol_.reserve(distance(url_s.begin(), prot_i));
|
||||||
transform(url_s.begin(), prot_i,
|
// Make portocol lowercase
|
||||||
back_inserter(protocol_),
|
transform(
|
||||||
std::ptr_fun<int,int>(tolower)); // protocol is icase
|
url_s.begin(), prot_i, back_inserter(protocol_), std::ptr_fun<int, int>(std::tolower)
|
||||||
|
);
|
||||||
if(prot_i == url_s.end())
|
if(prot_i == url_s.end())
|
||||||
return;
|
return;
|
||||||
advance(prot_i, prot_end.length());
|
advance(prot_i, prot_end.length());
|
||||||
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/');
|
std::string::const_iterator path_i = find(prot_i, url_s.end(), '/');
|
||||||
host_.reserve(distance(prot_i, path_i));
|
host_.reserve(distance(prot_i, path_i));
|
||||||
transform(prot_i, path_i,
|
// Make host lowerase
|
||||||
back_inserter(host_),
|
transform(prot_i, path_i, back_inserter(host_), std::ptr_fun<int, int>(std::tolower));
|
||||||
std::ptr_fun<int,int>(tolower)); // host is icase
|
|
||||||
|
|
||||||
// parse user/password
|
// parse user/password
|
||||||
auto user_pass_i = find(host_.begin(), host_.end(), '@');
|
auto user_pass_i = find(host_.begin(), host_.end(), '@');
|
||||||
if (user_pass_i != host_.end())
|
if(user_pass_i != host_.end()) {
|
||||||
{
|
|
||||||
std::string user_pass = std::string(host_.begin(), user_pass_i);
|
std::string user_pass = std::string(host_.begin(), user_pass_i);
|
||||||
auto pass_i = find(user_pass.begin(), user_pass.end(), ':');
|
auto pass_i = find(user_pass.begin(), user_pass.end(), ':');
|
||||||
if (pass_i != user_pass.end())
|
if (pass_i != user_pass.end()) {
|
||||||
{
|
|
||||||
user_ = std::string(user_pass.begin(), pass_i);
|
user_ = std::string(user_pass.begin(), pass_i);
|
||||||
pass_ = std::string(pass_i + 1, user_pass.end());
|
pass_ = std::string(pass_i + 1, user_pass.end());
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
user_ = user_pass;
|
user_ = user_pass;
|
||||||
|
|
||||||
host_.assign(user_pass_i + 1, host_.end());
|
host_.assign(user_pass_i + 1, host_.end());
|
||||||
|
@ -465,14 +425,12 @@ namespace http
|
||||||
|
|
||||||
// parse port
|
// parse port
|
||||||
auto port_i = find(host_.begin(), host_.end(), ':');
|
auto port_i = find(host_.begin(), host_.end(), ':');
|
||||||
if (port_i != host_.end())
|
if(port_i != host_.end()) {
|
||||||
{
|
|
||||||
portstr_ = std::string(port_i + 1, host_.end());
|
portstr_ = std::string(port_i + 1, host_.end());
|
||||||
host_.assign(host_.begin(), port_i);
|
host_.assign(host_.begin(), port_i);
|
||||||
try {
|
try {
|
||||||
port_ = boost::lexical_cast<decltype(port_)>(portstr_);
|
port_ = boost::lexical_cast<decltype(port_)>(portstr_);
|
||||||
}
|
} catch(const std::exception& e) {
|
||||||
catch (std::exception e) {
|
|
||||||
port_ = 80;
|
port_ = 80;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -487,51 +445,47 @@ namespace http
|
||||||
std::string urlDecode(const std::string& data)
|
std::string urlDecode(const std::string& data)
|
||||||
{
|
{
|
||||||
std::string res(data);
|
std::string res(data);
|
||||||
for (size_t pos = res.find('%'); pos != std::string::npos; pos = res.find('%',pos+1))
|
for(size_t pos = res.find('%'); pos != std::string::npos; pos = res.find('%', pos + 1)) {
|
||||||
{
|
const char c = strtol(res.substr(pos + 1, 2).c_str(), NULL, 16);
|
||||||
char c = strtol(res.substr(pos+1,2).c_str(), NULL, 16);
|
|
||||||
res.replace(pos, 3, 1, c);
|
res.replace(pos, 3, 1, c);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace net
|
namespace net {
|
||||||
{
|
|
||||||
int GetMTU (const boost::asio::ip::address& localAddress)
|
|
||||||
{
|
|
||||||
#if defined(__linux__) || defined(__FreeBSD_kernel__)
|
#if defined(__linux__) || defined(__FreeBSD_kernel__)
|
||||||
|
|
||||||
|
int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
|
||||||
|
{
|
||||||
ifaddrs* ifaddr, *ifa = nullptr;
|
ifaddrs* ifaddr, *ifa = nullptr;
|
||||||
if (getifaddrs(&ifaddr) == -1)
|
if(getifaddrs(&ifaddr) == -1) {
|
||||||
{
|
|
||||||
LogPrint(eLogError, "Can't excute getifaddrs");
|
LogPrint(eLogError, "Can't excute getifaddrs");
|
||||||
return 0;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
int family = 0;
|
int family = 0;
|
||||||
// loook for interface matching local address
|
// look for interface matching local address
|
||||||
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
|
for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
|
||||||
{
|
if(!ifa->ifa_addr)
|
||||||
if (!ifa->ifa_addr) continue;
|
continue;
|
||||||
|
|
||||||
family = ifa->ifa_addr->sa_family;
|
family = ifa->ifa_addr->sa_family;
|
||||||
if (family == AF_INET && localAddress.is_v4 ())
|
if(family == AF_INET && localAddress.is_v4()) {
|
||||||
{
|
|
||||||
sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
|
sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
|
||||||
if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
|
if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
|
||||||
break; // address matches
|
break; // address matches
|
||||||
}
|
} else if(family == AF_INET6 && localAddress.is_v6()) {
|
||||||
else if (family == AF_INET6 && localAddress.is_v6 ())
|
|
||||||
{
|
|
||||||
sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
|
sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
|
||||||
if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
|
if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
|
||||||
break; // address matches
|
break; // address matches
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int mtu = 0;
|
int mtu = fallback;
|
||||||
if (ifa && family) // interface found?
|
if(ifa && family) { // interface found?
|
||||||
{
|
|
||||||
int fd = socket(family, SOCK_DGRAM, 0);
|
int fd = socket(family, SOCK_DGRAM, 0);
|
||||||
if (fd > 0)
|
if(fd > 0) {
|
||||||
{
|
|
||||||
ifreq ifr;
|
ifreq ifr;
|
||||||
strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query
|
strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query
|
||||||
if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
|
if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
|
||||||
|
@ -539,25 +493,141 @@ namespace net
|
||||||
else
|
else
|
||||||
LogPrint (eLogError, "Failed to run ioctl");
|
LogPrint (eLogError, "Failed to run ioctl");
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
LogPrint(eLogError, "Failed to create datagram socket");
|
LogPrint(eLogError, "Failed to create datagram socket");
|
||||||
|
} else {
|
||||||
|
LogPrint(
|
||||||
|
eLogWarning, "Interface for local address",
|
||||||
|
localAddress.to_string(), " not found"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
LogPrint (eLogWarning, "Interface for local address", localAddress.to_string (), " not found");
|
|
||||||
|
|
||||||
freeifaddrs(ifaddr);
|
freeifaddrs(ifaddr);
|
||||||
|
|
||||||
return mtu;
|
return mtu;
|
||||||
|
}
|
||||||
|
|
||||||
#elif defined(WIN32)
|
#elif defined(WIN32)
|
||||||
|
int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback)
|
||||||
int result = 576; // fallback MTU
|
{
|
||||||
|
|
||||||
DWORD dwRetVal = 0;
|
|
||||||
ULONG outBufLen = 0;
|
ULONG outBufLen = 0;
|
||||||
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
|
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
|
||||||
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
|
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
|
||||||
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
|
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
|
||||||
|
== ERROR_BUFFER_OVERFLOW) {
|
||||||
|
FREE(pAddresses);
|
||||||
|
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD dwRetVal = GetAdaptersAddresses(
|
||||||
|
AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
|
||||||
|
);
|
||||||
|
|
||||||
|
if(dwRetVal != NO_ERROR) {
|
||||||
|
LogPrint(
|
||||||
|
eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
|
||||||
|
);
|
||||||
|
FREE(pAddresses);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
pCurrAddresses = pAddresses;
|
||||||
|
while(pCurrAddresses) {
|
||||||
|
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
||||||
|
|
||||||
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
||||||
|
if(pUnicast == nullptr) {
|
||||||
|
LogPrint(
|
||||||
|
eLogError, "GetMTU() has failed: not a unicast ipv4 address, this is not supported"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for(int i = 0; pUnicast != nullptr; ++i) {
|
||||||
|
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
||||||
|
sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr;
|
||||||
|
if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr) {
|
||||||
|
result = pAddresses->Mtu;
|
||||||
|
FREE(pAddresses);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
pUnicast = pUnicast->Next;
|
||||||
|
}
|
||||||
|
pCurrAddresses = pCurrAddresses->Next;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv4 addresses found");
|
||||||
|
FREE(pAddresses);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetMTUWindowsIpv6(sockaddr_in inputAddress, int fallback)
|
||||||
|
{
|
||||||
|
ULONG outBufLen = 0;
|
||||||
|
PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
|
||||||
|
PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
|
||||||
|
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
|
||||||
|
|
||||||
|
if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
|
||||||
|
== ERROR_BUFFER_OVERFLOW) {
|
||||||
|
FREE(pAddresses);
|
||||||
|
pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD dwRetVal = GetAdaptersAddresses(
|
||||||
|
AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
|
||||||
|
);
|
||||||
|
|
||||||
|
if(dwRetVal != NO_ERROR) {
|
||||||
|
LogPrint(
|
||||||
|
eLogError,
|
||||||
|
"GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed"
|
||||||
|
);
|
||||||
|
FREE(pAddresses);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool found_address = false;
|
||||||
|
pCurrAddresses = pAddresses;
|
||||||
|
while(pCurrAddresses) {
|
||||||
|
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
||||||
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
||||||
|
if(pUnicast == nullptr) {
|
||||||
|
LogPrint(
|
||||||
|
eLogError,
|
||||||
|
"GetMTU() has failed: not a unicast ipv6 address, this is not supported"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for(int i = 0; pUnicast != nullptr; ++i) {
|
||||||
|
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
||||||
|
sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
|
||||||
|
|
||||||
|
for (int j = 0; j != 8; ++j) {
|
||||||
|
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j]) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
found_address = true;
|
||||||
|
}
|
||||||
|
} if (found_address) {
|
||||||
|
result = pAddresses->Mtu;
|
||||||
|
FREE(pAddresses);
|
||||||
|
pAddresses = nullptr;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
pUnicast = pUnicast->Next;
|
||||||
|
}
|
||||||
|
|
||||||
|
pCurrAddresses = pCurrAddresses->Next;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrint(eLogError, "GetMTU() error: no usable unicast ipv6 addresses found");
|
||||||
|
FREE(pAddresses);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
|
||||||
|
{
|
||||||
#ifdef UNICODE
|
#ifdef UNICODE
|
||||||
string localAddress_temporary = localAddress.to_string();
|
string localAddress_temporary = localAddress.to_string();
|
||||||
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
|
wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
|
||||||
|
@ -565,131 +635,30 @@ namespace net
|
||||||
std::string localAddressUniversal = localAddress.to_string();
|
std::string localAddressUniversal = localAddress.to_string();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (localAddress.is_v4())
|
if(localAddress.is_v4()) {
|
||||||
{
|
sockaddr_in inputAddress;
|
||||||
struct sockaddr_in inputAddress;
|
|
||||||
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
|
inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
|
||||||
|
return GetMTUWindowsIpv4(inputAddress, fallback);
|
||||||
if (GetAdaptersAddresses (AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
|
} else if(localAddress.is_v6()) {
|
||||||
== ERROR_BUFFER_OVERFLOW)
|
sockaddr_in6 inputAddress;
|
||||||
{
|
|
||||||
FREE (pAddresses);
|
|
||||||
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC (outBufLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
dwRetVal = GetAdaptersAddresses (AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen);
|
|
||||||
if (dwRetVal == NO_ERROR)
|
|
||||||
{
|
|
||||||
pCurrAddresses = pAddresses;
|
|
||||||
while (pCurrAddresses)
|
|
||||||
{
|
|
||||||
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
|
||||||
|
|
||||||
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
|
||||||
if (pUnicast != nullptr)
|
|
||||||
{
|
|
||||||
for (int i = 0; pUnicast != nullptr; ++i)
|
|
||||||
{
|
|
||||||
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
|
||||||
struct sockaddr_in *localInterfaceAddress = (struct sockaddr_in*) lpAddr;
|
|
||||||
if (localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr)
|
|
||||||
{
|
|
||||||
result = pAddresses->Mtu;
|
|
||||||
FREE (pAddresses);
|
|
||||||
pAddresses = nullptr;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
pUnicast = pUnicast->Next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "GetMTU() has failed: not a unicast ipv4 address, this is not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
pCurrAddresses = pCurrAddresses->Next;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (localAddress.is_v6())
|
|
||||||
{
|
|
||||||
struct sockaddr_in6 inputAddress;
|
|
||||||
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
|
inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
|
||||||
|
return GetMTUWindowsIpv6(inputAddress, fallback);
|
||||||
if (GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
|
} else {
|
||||||
== ERROR_BUFFER_OVERFLOW)
|
|
||||||
{
|
|
||||||
FREE (pAddresses);
|
|
||||||
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC (outBufLen);
|
|
||||||
}
|
|
||||||
|
|
||||||
dwRetVal = GetAdaptersAddresses (AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen);
|
|
||||||
if (dwRetVal == NO_ERROR)
|
|
||||||
{
|
|
||||||
bool found_address = false;
|
|
||||||
pCurrAddresses = pAddresses;
|
|
||||||
while (pCurrAddresses)
|
|
||||||
{
|
|
||||||
PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
|
|
||||||
|
|
||||||
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
|
||||||
if (pUnicast != nullptr)
|
|
||||||
{
|
|
||||||
for (int i = 0; pUnicast != nullptr; ++i)
|
|
||||||
{
|
|
||||||
LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
|
|
||||||
struct sockaddr_in6 *localInterfaceAddress = (struct sockaddr_in6*) lpAddr;
|
|
||||||
|
|
||||||
for (int j = 0; j != 8; ++j)
|
|
||||||
{
|
|
||||||
if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j])
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
found_address = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found_address)
|
|
||||||
{
|
|
||||||
result = pAddresses->Mtu;
|
|
||||||
FREE (pAddresses);
|
|
||||||
pAddresses = nullptr;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
pUnicast = pUnicast->Next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "GetMTU() has failed: not a unicast ipv6 address, this is not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
pCurrAddresses = pCurrAddresses->Next;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "GetMTU() has failed: enclosed GetAdaptersAddresses() call has failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LogPrint(eLogError, "GetMTU() has failed: address family is not supported");
|
LogPrint(eLogError, "GetMTU() has failed: address family is not supported");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FREE (pAddresses);
|
}
|
||||||
pAddresses = nullptr;
|
#endif // WIN32
|
||||||
LogPrint(eLogError, "GetMTU() error: control flow should never reach this line");
|
|
||||||
return result;
|
int GetMTU(const boost::asio::ip::address& localAddress)
|
||||||
|
{
|
||||||
|
const int fallback = 576; // fallback MTU
|
||||||
|
|
||||||
|
#if defined(__linux__) || defined(__FreeBSD_kernel__)
|
||||||
|
return GetMTUUnix(localAddress, fallback);
|
||||||
|
#elif defined(WIN32)
|
||||||
|
return GetMTUWindows(localAddress, fallback);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
87
util.h
87
util.h
|
@ -18,23 +18,73 @@ namespace util
|
||||||
{
|
{
|
||||||
extern std::map<std::string, std::string> mapArgs;
|
extern std::map<std::string, std::string> mapArgs;
|
||||||
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses command line arguments, i.e. stores them in config::mapArgs.
|
||||||
|
*/
|
||||||
void OptionParser(int argc, const char* const argv[]);
|
void OptionParser(int argc, const char* const argv[]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a command line argument from config::mapArgs as an int
|
||||||
|
* @param nDefault the default value to be returned
|
||||||
|
*/
|
||||||
int GetArg(const std::string& strArg, int nDefault);
|
int GetArg(const std::string& strArg, int nDefault);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a command line argument from config::mapArgs as a std::string
|
||||||
|
* @param strDefault the default value to be returned
|
||||||
|
*/
|
||||||
std::string GetArg(const std::string& strArg, const std::string& strDefault);
|
std::string GetArg(const std::string& strArg, const std::string& strDefault);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a command line argument from config::mapArgs as a C-style string
|
||||||
|
* @param nDefault the default value to be returned
|
||||||
|
*/
|
||||||
const char* GetCharArg(const std::string& strArg, const std::string& nDefault);
|
const char* GetCharArg(const std::string& strArg, const std::string& nDefault);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace filesystem
|
namespace filesystem
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Change the application name.
|
||||||
|
*/
|
||||||
void SetAppName(const std::string& name);
|
void SetAppName(const std::string& name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the application name.
|
||||||
|
*/
|
||||||
std::string GetAppName();
|
std::string GetAppName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the path of the i2pd directory
|
||||||
|
*/
|
||||||
const boost::filesystem::path& GetDataDir();
|
const boost::filesystem::path& GetDataDir();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the full path of a file within the i2pd directory
|
||||||
|
*/
|
||||||
std::string GetFullPath(const std::string& filename);
|
std::string GetFullPath(const std::string& filename);
|
||||||
boost::filesystem::path GetDefaultDataDir();
|
|
||||||
|
/**
|
||||||
|
* @return the path of the configuration file
|
||||||
|
*/
|
||||||
boost::filesystem::path GetConfigFile();
|
boost::filesystem::path GetConfigFile();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the default directory for i2pd data
|
||||||
|
*/
|
||||||
|
boost::filesystem::path GetDefaultDataDir();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a configuration file and store its contents in the given maps.
|
||||||
|
*/
|
||||||
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
|
||||||
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
|
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the path of the certificates directory
|
||||||
|
*/
|
||||||
boost::filesystem::path GetCertificatesDir();
|
boost::filesystem::path GetCertificatesDir();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,14 +96,42 @@ namespace util
|
||||||
const char LAST_MODIFIED[] = "Last-Modified";
|
const char LAST_MODIFIED[] = "Last-Modified";
|
||||||
const char TRANSFER_ENCODING[] = "Transfer-Encoding";
|
const char TRANSFER_ENCODING[] = "Transfer-Encoding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform an HTTP request.
|
||||||
|
* @return the result of the request, or an empty string if it fails
|
||||||
|
*/
|
||||||
std::string httpRequest(const std::string& address);
|
std::string httpRequest(const std::string& address);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the content of the given HTTP stream without headers
|
||||||
|
*/
|
||||||
std::string GetHttpContent(std::istream& response);
|
std::string GetHttpContent(std::istream& response);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge chunks of a HTTP response into the gien std:ostream object.
|
||||||
|
*/
|
||||||
void MergeChunkedResponse(std::istream& response, std::ostream& merged);
|
void MergeChunkedResponse(std::istream& response, std::ostream& merged);
|
||||||
int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code
|
|
||||||
|
/**
|
||||||
|
* Send an HTTP request through the i2p proxy.
|
||||||
|
* @return the HTTP status code
|
||||||
|
*/
|
||||||
|
int httpRequestViaI2pProxy(const std::string& address, std::string &content);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the decoded url
|
||||||
|
*/
|
||||||
std::string urlDecode(const std::string& data);
|
std::string urlDecode(const std::string& data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides functionality for parsing URLs.
|
||||||
|
*/
|
||||||
struct url {
|
struct url {
|
||||||
url(const std::string& url_s); // omitted copy, ==, accessors, ...
|
/**
|
||||||
|
* Parse a url given as a string.
|
||||||
|
*/
|
||||||
|
url(const std::string& url_s);
|
||||||
private:
|
private:
|
||||||
void parse(const std::string& url_s);
|
void parse(const std::string& url_s);
|
||||||
public:
|
public:
|
||||||
|
@ -67,6 +145,9 @@ namespace util
|
||||||
|
|
||||||
namespace net
|
namespace net
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @return the maximum transmission unit, or 576 on failure
|
||||||
|
*/
|
||||||
int GetMTU(const boost::asio::ip::address& localAddress);
|
int GetMTU(const boost::asio::ip::address& localAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue