lokinet/llarp/service.cpp

256 lines
6.1 KiB
C++
Raw Normal View History

#include <llarp/service.hpp>
2018-07-06 16:08:30 +00:00
#include "buffer.hpp"
#include "ini.hpp"
2018-07-09 17:32:11 +00:00
#include "router.hpp"
namespace llarp
{
namespace service
{
2018-07-06 16:08:30 +00:00
std::string
AddressToString(const Address& addr)
{
char tmp[(1 + 32) * 2] = {0};
std::string str = Base32Encode(addr, tmp);
return str + ".loki";
}
ServiceInfo::ServiceInfo()
{
vanity.Zero();
}
ServiceInfo::~ServiceInfo()
{
}
bool
ServiceInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))
return false;
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, val))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, key, val))
return false;
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, val))
return false;
return read;
}
bool
ServiceInfo::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictEntry("e", enckey, buf))
return false;
if(!BEncodeWriteDictEntry("s", signkey, buf))
return false;
if(!BEncodeWriteDictInt(buf, "v", LLARP_PROTO_VERSION))
return false;
if(!vanity.IsZero())
{
if(!BEncodeWriteDictEntry("x", vanity, buf))
return false;
}
return bencode_end(buf);
}
2018-07-06 16:08:30 +00:00
bool
ServiceInfo::CalculateAddress(byte_t* addr) const
2018-07-06 16:08:30 +00:00
{
byte_t tmp[128];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!BEncode(&buf))
return false;
return crypto_generichash(addr, 32, buf.base, buf.cur - buf.base, nullptr,
0)
2018-07-06 16:08:30 +00:00
!= -1;
}
IntroSet::~IntroSet()
{
2018-07-06 16:08:30 +00:00
if(W)
delete W;
}
bool
2018-07-09 17:32:11 +00:00
IntroSet::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
{
2018-07-09 17:32:11 +00:00
bool read = false;
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
2018-07-09 17:32:11 +00:00
return false;
if(llarp_buffer_eq(key, "i"))
2018-07-09 17:32:11 +00:00
{
return BEncodeReadList(I, buf);
}
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
2018-07-09 17:32:11 +00:00
return false;
if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf))
2018-07-09 17:32:11 +00:00
return false;
return read;
}
bool
IntroSet::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
2018-07-06 16:08:30 +00:00
if(!BEncodeWriteDictEntry("a", A, buf))
return false;
// start introduction list
if(!bencode_write_bytestring(buf, "i", 1))
return false;
if(!BEncodeWriteList(I.begin(), I.end(), buf))
return false;
// end introduction list
// write version
2018-07-09 17:32:11 +00:00
if(!BEncodeWriteDictInt(buf, "v", version))
2018-07-06 16:08:30 +00:00
return false;
/*
2018-07-06 16:08:30 +00:00
if(W)
{
if(!BEncodeWriteDictEntry("w", *W, buf))
return false;
}
*/
2018-07-06 16:08:30 +00:00
if(!BEncodeWriteDictEntry("z", Z, buf))
return false;
return bencode_end(buf);
}
2018-07-06 16:08:30 +00:00
Introduction::~Introduction()
{
}
bool
2018-07-09 17:32:11 +00:00
Introduction::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
2018-07-06 16:08:30 +00:00
{
2018-07-09 17:32:11 +00:00
bool read = false;
if(!BEncodeMaybeReadDictEntry("k", router, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("l", latency, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("p", pathID, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("x", expiresAt, read, key, buf))
return false;
return read;
2018-07-06 16:08:30 +00:00
}
bool
Introduction::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
2018-07-09 17:32:11 +00:00
2018-07-06 16:08:30 +00:00
if(!BEncodeWriteDictEntry("k", router, buf))
return false;
2018-07-09 17:32:11 +00:00
if(latency)
{
if(!BEncodeWriteDictInt(buf, "l", latency))
return false;
}
2018-07-06 16:08:30 +00:00
if(!BEncodeWriteDictEntry("p", pathID, buf))
return false;
if(!BEncodeWriteDictInt(buf, "v", version))
return false;
if(!BEncodeWriteDictInt(buf, "x", expiresAt))
return false;
return bencode_end(buf);
}
Identity::~Identity()
{
}
bool
Identity::BEncode(llarp_buffer_t* buf) const
{
/// TODO: implement me
return false;
}
bool
Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
{
/// TODO: implement me
return false;
}
void
Identity::RegenerateKeys(llarp_crypto* crypto)
{
crypto->encryption_keygen(enckey);
crypto->identity_keygen(signkey);
pub.enckey = llarp::seckey_topublic(enckey);
pub.signkey = llarp::seckey_topublic(signkey);
pub.vanity.Zero();
2018-07-06 16:08:30 +00:00
}
2018-07-10 11:36:55 +00:00
bool
Identity::EnsureKeys(const std::string& fname, llarp_crypto* c)
{
// TODO: implement me
return false;
}
2018-07-06 16:08:30 +00:00
bool
Identity::SignIntroSet(IntroSet& i, llarp_crypto* crypto) const
{
if(i.I.size() == 0)
return false;
i.A = pub;
// zero out signature for signing process
i.Z.Zero();
byte_t tmp[MAX_INTROSET_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!i.BEncode(&buf))
return false;
// rewind and resize buffer
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
return crypto->sign(i.Z, signkey, buf);
}
bool
IntroSet::VerifySignature(llarp_crypto* crypto) const
{
byte_t tmp[MAX_INTROSET_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
IntroSet copy;
copy = *this;
copy.Z.Zero();
if(!copy.BEncode(&buf))
return false;
// rewind and resize buffer
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
return crypto->verify(A.signkey, buf, Z);
}
2018-07-10 11:36:55 +00:00
bool
Config::Load(const std::string& fname)
{
// TODO: implement me
ini::Parser parser(fname);
for(const auto& sec : parser.top().ordered_sections)
{
services.push_back({sec->first, sec->second.values});
}
return services.size() > 0;
2018-07-10 11:36:55 +00:00
}
} // namespace service
} // namespace llarp