1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

updated liteserver

- new methods for liteserver/liteclient
- added ADNL/DHT client-only work mode
- fixed crash in ADNL
This commit is contained in:
ton 2020-02-02 16:53:37 +04:00
parent acf16718e6
commit 53ec9684bd
70 changed files with 816 additions and 322 deletions

View file

@ -155,6 +155,16 @@ td::Result<AdnlAddressList> AdnlAddressList::create(const tl_object_ptr<ton_api:
return A;
}
td::Status AdnlAddressList::add_udp_address(td::IPAddress addr) {
if (addr.is_ipv4()) {
auto r = td::make_ref<AdnlAddressUdp>(addr.get_ipv4(), static_cast<td::uint16>(addr.get_port()));
addrs_.push_back(std::move(r));
return td::Status::OK();
} else {
return td::Status::Error(ErrorCode::protoviolation, "only works with ipv4");
}
}
} // namespace adnl
} // namespace ton

View file

@ -88,6 +88,7 @@ class AdnlAddressList {
void add_addr(AdnlAddress addr) {
addrs_.push_back(addr);
}
void update(td::IPAddress addr);
bool public_only() const;
td::uint32 size() const {
return static_cast<td::uint32>(addrs_.size());
@ -98,6 +99,7 @@ class AdnlAddressList {
}
static td::Result<AdnlAddressList> create(const tl_object_ptr<ton_api::adnl_addressList> &addr_list);
td::Status add_udp_address(td::IPAddress addr);
};
} // namespace adnl

View file

@ -111,13 +111,15 @@ void AdnlChannelImpl::send_message(td::uint32 priority, td::actor::ActorId<AdnlN
td::actor::send_closure(conn, &AdnlNetworkConnection::send, local_id_, peer_id_, priority, std::move(B));
}
void AdnlChannelImpl::receive(td::BufferSlice data) {
void AdnlChannelImpl::receive(td::IPAddress addr, td::BufferSlice data) {
auto P = td::PromiseCreator::lambda(
[peer = peer_pair_, channel_id = channel_in_id_, id = print_id()](td::Result<AdnlPacket> R) {
[peer = peer_pair_, channel_id = channel_in_id_, addr, id = print_id()](td::Result<AdnlPacket> R) {
if (R.is_error()) {
VLOG(ADNL_WARNING) << id << ": dropping IN message: can not decrypt: " << R.move_as_error();
} else {
td::actor::send_closure(peer, &AdnlPeerPair::receive_packet_from_channel, channel_id, R.move_as_ok());
auto packet = R.move_as_ok();
packet.set_remote_addr(addr);
td::actor::send_closure(peer, &AdnlPeerPair::receive_packet_from_channel, channel_id, std::move(packet));
}
});

View file

@ -35,7 +35,7 @@ class AdnlChannel : public td::actor::Actor {
AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id,
AdnlChannelIdShort &out_id, AdnlChannelIdShort &in_id,
td::actor::ActorId<AdnlPeerPair> peer_pair);
virtual void receive(td::BufferSlice data) = 0;
virtual void receive(td::IPAddress addr, td::BufferSlice data) = 0;
virtual void send_message(td::uint32 priority, td::actor::ActorId<AdnlNetworkConnection> conn,
td::BufferSlice data) = 0;
virtual ~AdnlChannel() = default;

View file

@ -33,7 +33,7 @@ class AdnlChannelImpl : public AdnlChannel {
AdnlChannelIdShort in_id, AdnlChannelIdShort out_id, std::unique_ptr<Encryptor> encryptor,
std::unique_ptr<Decryptor> decryptor);
void decrypt(td::BufferSlice data, td::Promise<AdnlPacket> promise);
void receive(td::BufferSlice data) override;
void receive(td::IPAddress addr, td::BufferSlice data) override;
void send_message(td::uint32 priority, td::actor::ActorId<AdnlNetworkConnection> conn, td::BufferSlice data) override;
struct AdnlChannelPrintId {

View file

@ -40,13 +40,15 @@ AdnlAddressList AdnlLocalId::get_addr_list() const {
return addr_list_;
}
void AdnlLocalId::receive(td::BufferSlice data) {
void AdnlLocalId::receive(td::IPAddress addr, td::BufferSlice data) {
auto P = td::PromiseCreator::lambda(
[peer_table = peer_table_, dst = short_id_, id = print_id()](td::Result<AdnlPacket> R) {
[peer_table = peer_table_, dst = short_id_, addr, id = print_id()](td::Result<AdnlPacket> R) {
if (R.is_error()) {
VLOG(ADNL_WARNING) << id << ": dropping IN message: cannot decrypt: " << R.move_as_error();
} else {
td::actor::send_closure(peer_table, &AdnlPeerTable::receive_decrypted_packet, dst, R.move_as_ok());
auto packet = R.move_as_ok();
packet.set_remote_addr(addr);
td::actor::send_closure(peer_table, &AdnlPeerTable::receive_decrypted_packet, dst, std::move(packet));
}
});
@ -117,7 +119,7 @@ void AdnlLocalId::update_address_list(AdnlAddressList addr_list) {
}
void AdnlLocalId::publish_address_list() {
if (dht_node_.empty() || addr_list_.empty()) {
if (dht_node_.empty() || addr_list_.empty() || addr_list_.size() == 0) {
VLOG(ADNL_NOTICE) << this << ": skipping public addr list, because localid (or dht node) not fully initialized";
return;
}
@ -178,7 +180,8 @@ AdnlLocalId::AdnlLocalId(AdnlNodeIdFull id, AdnlAddressList addr_list, td::actor
id_ = std::move(id);
short_id_ = id_.compute_short_id();
addr_list_ = std::move(addr_list);
if (addr_list_.addrs().size() > 0) {
if (!addr_list_.empty()) {
addr_list_.set_reinit_date(Adnl::adnl_start_time());
addr_list_.set_version(static_cast<td::int32>(td::Clocks::system()));
}
peer_table_ = peer_table;

View file

@ -54,7 +54,7 @@ class AdnlLocalId : public td::actor::Actor {
void decrypt_message(td::BufferSlice data, td::Promise<td::BufferSlice> promise);
void deliver(AdnlNodeIdShort src, td::BufferSlice data);
void deliver_query(AdnlNodeIdShort src, td::BufferSlice data, td::Promise<td::BufferSlice> promise);
void receive(td::BufferSlice data);
void receive(td::IPAddress addr, td::BufferSlice data);
void subscribe(std::string prefix, std::unique_ptr<AdnlPeerTable::Callback> callback);
void unsubscribe(std::string prefix);

View file

@ -59,7 +59,7 @@ void AdnlNetworkManagerImpl::receive_udp_message(td::UdpMessage message) {
}
received_messages_++;
if (received_messages_ % 64 == 0) {
VLOG(ADNL_DEBUG) << this << ": received " << received_messages_ << "udp messages";
VLOG(ADNL_DEBUG) << this << ": received " << received_messages_ << " udp messages";
}
VLOG(ADNL_EXTRA_DEBUG) << this << ": received message of size " << message.data.size();

View file

@ -132,6 +132,9 @@ class AdnlPacket {
auto signature() const {
return signature_.clone();
}
auto remote_addr() const {
return remote_addr_;
}
void init_random();
@ -188,6 +191,10 @@ class AdnlPacket {
flags_ |= Flags::f_reinit_date;
}
void set_remote_addr(td::IPAddress addr) {
remote_addr_ = addr;
}
private:
td::BufferSlice rand1_;
td::uint32 flags_{0};
@ -204,6 +211,8 @@ class AdnlPacket {
td::int32 dst_reinit_date_{0};
td::BufferSlice signature_;
td::BufferSlice rand2_;
td::IPAddress remote_addr_;
};
} // namespace adnl

View file

@ -49,7 +49,7 @@ td::actor::ActorOwn<Adnl> Adnl::create(std::string db, td::actor::ActorId<keyrin
return td::actor::ActorOwn<Adnl>(td::actor::create_actor<AdnlPeerTableImpl>("PeerTable", db, keyring));
}
void AdnlPeerTableImpl::receive_packet(td::BufferSlice data) {
void AdnlPeerTableImpl::receive_packet(td::IPAddress addr, td::BufferSlice data) {
if (data.size() < 32) {
VLOG(ADNL_WARNING) << this << ": dropping IN message [?->?]: message too short: len=" << data.size();
return;
@ -60,14 +60,14 @@ void AdnlPeerTableImpl::receive_packet(td::BufferSlice data) {
auto it = local_ids_own_.find(dst);
if (it != local_ids_own_.end()) {
td::actor::send_closure(it->second, &AdnlLocalId::receive, std::move(data));
td::actor::send_closure(it->second, &AdnlLocalId::receive, addr, std::move(data));
return;
}
AdnlChannelIdShort dst_chan_id{dst.pubkey_hash()};
auto it2 = channels_.find(dst_chan_id);
if (it2 != channels_.end()) {
td::actor::send_closure(it2->second, &AdnlChannel::receive, std::move(data));
td::actor::send_closure(it2->second, &AdnlChannel::receive, addr, std::move(data));
return;
}
@ -237,7 +237,7 @@ void AdnlPeerTableImpl::register_network_manager(td::actor::ActorId<AdnlNetworkM
class Cb : public AdnlNetworkManager::Callback {
public:
void receive_packet(td::IPAddress addr, td::BufferSlice data) override {
td::actor::send_closure(id_, &AdnlPeerTableImpl::receive_packet, std::move(data));
td::actor::send_closure(id_, &AdnlPeerTableImpl::receive_packet, addr, std::move(data));
}
Cb(td::actor::ActorId<AdnlPeerTableImpl> id) : id_(id) {
}

View file

@ -89,7 +89,7 @@ class AdnlPeerTable : public Adnl {
virtual void answer_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlQueryId query_id, td::BufferSlice data) = 0;
virtual void receive_packet(td::BufferSlice data) = 0;
virtual void receive_packet(td::IPAddress addr, td::BufferSlice data) = 0;
virtual void receive_decrypted_packet(AdnlNodeIdShort dst, AdnlPacket packet) = 0;
virtual void send_message_in(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message) = 0;

View file

@ -43,7 +43,7 @@ class AdnlPeerTableImpl : public AdnlPeerTable {
void add_peer(AdnlNodeIdShort local_id, AdnlNodeIdFull id, AdnlAddressList addr_list) override;
void add_static_nodes_from_config(AdnlNodesList nodes) override;
void receive_packet(td::BufferSlice data) override;
void receive_packet(td::IPAddress addr, td::BufferSlice data) override;
void receive_decrypted_packet(AdnlNodeIdShort dst, AdnlPacket data) override;
void send_message_in(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message) override;
void send_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) override {

View file

@ -131,7 +131,12 @@ void AdnlPeerPairImpl::receive_packet_checked(AdnlPacket packet) {
}
if (packet.dst_reinit_date() > 0 && packet.dst_reinit_date() < d) {
if (!packet.addr_list().empty()) {
update_addr_list(packet.addr_list());
auto addr_list = packet.addr_list();
if (packet.remote_addr().is_valid() && addr_list.size() == 0) {
VLOG(ADNL_DEBUG) << "adding implicit address " << packet.remote_addr();
addr_list.add_udp_address(packet.remote_addr());
}
update_addr_list(std::move(addr_list));
}
if (!packet.priority_addr_list().empty()) {
update_addr_list(packet.priority_addr_list());
@ -174,7 +179,12 @@ void AdnlPeerPairImpl::receive_packet_checked(AdnlPacket packet) {
}
if (!packet.addr_list().empty()) {
update_addr_list(packet.addr_list());
auto addr_list = packet.addr_list();
if (packet.remote_addr().is_valid() && addr_list.size() == 0) {
VLOG(ADNL_DEBUG) << "adding implicit address " << packet.remote_addr();
addr_list.add_udp_address(packet.remote_addr());
}
update_addr_list(std::move(addr_list));
}
if (!packet.priority_addr_list().empty()) {
update_addr_list(packet.priority_addr_list());
@ -642,7 +652,7 @@ void AdnlPeerPairImpl::update_addr_list(AdnlAddressList addr_list) {
if (addr_list.empty()) {
return;
}
CHECK(addr_list.size() > 0);
//CHECK(addr_list.size() > 0);
if (addr_list.reinit_date() > td::Clocks::system() + 60) {
VLOG(ADNL_WARNING) << "dropping addr list with too new reinit date";