forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadnl-packet.cpp
135 lines (120 loc) · 5.34 KB
/
adnl-packet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "adnl-packet.h"
#include "td/utils/Random.h"
namespace ton {
namespace adnl {
/*adnl.packetContents rand1:bytes flags:# from:flags.0?PublicKey from_short:flags.1?adnl.id.short
message:flags.2?adnl.Message messages:flags.3?(vector adnl.Message)
address:flags.6?adnl.addressList seqno:flags.8?long recv_addr_list_version:flags.9?int
confirm_seqno:flags.10?long reinit_date:flags.11?int dst_reinit_date:flags.11?int
signature:flags.7?bytes rand2:bytes = adnl.PacketContents;*/
td::Result<AdnlPacket> AdnlPacket::create(tl_object_ptr<ton_api::adnl_packetContents> packet) {
AdnlPacket R;
R.rand1_ = std::move(packet->rand1_);
R.flags_ = packet->flags_;
if (R.flags_ & Flags::f_from) {
TRY_RESULT(F, AdnlNodeIdFull::create(packet->from_));
R.from_ = std::move(F);
}
if (R.flags_ & Flags::f_from_short) {
R.from_short_ = AdnlNodeIdShort{packet->from_short_->id_};
} else if (packet->flags_ & Flags::f_from) {
R.from_short_ = R.from_.compute_short_id();
}
if (R.flags_ & Flags::f_one_message) {
R.messages_ = AdnlMessageList{std::move(packet->message_)};
}
if (R.flags_ & Flags::f_mult_messages) {
// may override messages_ if (flags & 0x4)
// but this message will fail in run_basic_checks()
// so it doesn't matter
R.messages_ = AdnlMessageList{std::move(packet->messages_)};
}
if (R.flags_ & Flags::f_address) {
TRY_RESULT(addr_list, AdnlAddressList::create(std::move(packet->address_)));
R.addr_ = std::move(addr_list);
}
if (R.flags_ & Flags::f_priority_address) {
TRY_RESULT(addr_list, AdnlAddressList::create(std::move(packet->address_)));
R.priority_addr_ = std::move(addr_list);
}
if (R.flags_ & Flags::f_seqno) {
R.seqno_ = packet->seqno_;
}
if (R.flags_ & Flags::f_confirm_seqno) {
R.confirm_seqno_ = packet->confirm_seqno_;
}
if (R.flags_ & Flags::f_recv_addr_version) {
R.recv_addr_list_version_ = packet->recv_addr_list_version_;
}
if (R.flags_ & Flags::f_recv_priority_addr_version) {
R.recv_priority_addr_list_version_ = packet->recv_priority_addr_list_version_;
}
if (R.flags_ & Flags::f_reinit_date) {
R.reinit_date_ = packet->reinit_date_;
R.dst_reinit_date_ = packet->dst_reinit_date_;
}
if (R.flags_ & Flags::f_signature) {
R.signature_ = std::move(packet->signature_);
}
R.rand2_ = std::move(packet->rand2_);
TRY_STATUS(R.run_basic_checks());
return std::move(R);
}
td::Status AdnlPacket::run_basic_checks() const {
if ((flags_ & Flags::f_all) != flags_) {
return td::Status::Error(ErrorCode::protoviolation, "bad flags");
}
if ((flags_ & Flags::f_one_message) && (flags_ & Flags::f_mult_messages)) {
return td::Status::Error(ErrorCode::protoviolation, "both flags 0x4 and 0x8 set");
}
if ((flags_ & Flags::f_from) && (flags_ & Flags::f_from_short) && from_.compute_short_id() != from_short_) {
return td::Status::Error(ErrorCode::protoviolation, "source and short source mismatch");
}
if ((flags_ & Flags::f_address) && addr_.empty()) {
return td::Status::Error(ErrorCode::protoviolation, "bad addr list");
}
if ((flags_ & Flags::f_priority_address) && priority_addr_.empty()) {
return td::Status::Error(ErrorCode::protoviolation, "bad addr list");
}
return td::Status::OK();
}
tl_object_ptr<ton_api::adnl_packetContents> AdnlPacket::tl() const {
return create_tl_object<ton_api::adnl_packetContents>(
rand1_.clone(), flags_ & ~Flags::f_priority, (flags_ & Flags::f_from) ? from_.tl() : nullptr,
(flags_ & Flags::f_from_short) ? from_short_.tl() : nullptr,
(flags_ & Flags::f_one_message) ? messages_.one_message() : nullptr,
(flags_ & Flags::f_mult_messages) ? messages_.mult_messages() : messages_.empty_vector(),
(flags_ & Flags::f_address) ? addr_.tl() : nullptr,
(flags_ & Flags::f_priority_address) ? priority_addr_.tl() : nullptr, seqno_, confirm_seqno_,
recv_addr_list_version_, recv_priority_addr_list_version_, reinit_date_, dst_reinit_date_, signature_.clone(),
rand2_.clone());
}
td::BufferSlice AdnlPacket::to_sign() const {
auto obj = tl();
obj->signature_.clear();
obj->flags_ &= ~Flags::f_signature;
CHECK(obj->signature_.size() == 0);
return serialize_tl_object(obj, true);
}
void AdnlPacket::init_random() {
rand1_ = td::BufferSlice{(td::Random::fast_uint32() & 1) ? 7u : 15u};
rand2_ = td::BufferSlice{(td::Random::fast_uint32() & 1) ? 7u : 15u};
td::Random::secure_bytes(rand1_.as_slice());
td::Random::secure_bytes(rand2_.as_slice());
}
} // namespace adnl
} // namespace ton