-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtdns.fc
207 lines (172 loc) · 6.26 KB
/
mtdns.fc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
;;
;; Manual TON DNS contract
;;
;; Lib stuff
(slice, int) dict_get?(cell dict, int key_len, slice key) asm(key dict key_len) "DICTGET" "NULLSWAPIFNOT";
cell dict_set_ref(cell dict, int key_len, slice key, cell value) asm(value key dict key_len) "DICTSETREF";
(cell, ()) ~dict_set_ref(cell dict, int key_len, slice key, cell value) asm(value key dict key_len) "DICTSETREF";
(cell, int) dict_get_ref?(cell dict, int key_len, slice key) asm(key dict key_len) "DICTGETREF" "NULLSWAPIFNOT";
(cell, int) dict_delete?(cell dict, int key_len, slice key) asm(key dict key_len) "DICTDEL";
(cell, (int)) ~dict_delete?(cell dict, int key_len, slice key) asm(key dict key_len) "DICTDEL";
(int, slice, int) idict_min?(cell dict, int key_len) asm(-> 1 0 2) "DICTIMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT";
builder store_zeroes(builder b, int len) asm "STZEROES";
cell null() asm "PUSHNULL";
;; Dict accepts only fixed-lens keys, so we can pad string until
;; fixed size limit or calculate hash. In different situations
;; one option may be better than another (do we want to use long
;; domain names? do we want to disclose all stored domains?).
;; Using hash by default.
slice align_str(slice str) {
;; return begin_cell()
;; .store_slice(str)
;; .store_zeroes( 126 * 8 - slice_bits(str) )
;; .end_cell()
;; .begin_parse();
return begin_cell()
.store_uint(string_hash(str), 256)
.end_cell()
.begin_parse();
}
(int, int, cell) load_data() {
var ds = get_data().begin_parse();
var res = (ds~load_uint(32), ds~load_uint(256), ds~load_dict());
ds.end_parse();
return res;
}
() store_data(seqno, public_key, domains) impure {
set_data(begin_cell()
.store_uint(seqno, 32)
.store_uint(public_key, 256)
.store_dict(domains)
.end_cell());
}
() recv_internal(slice in_msg) impure {
;; do nothing for internal messages
}
_ ~process_domain_upsert(domains, msg) impure {
var records = new_dict();
var cat_id = msg~load_int(16);
var wc = msg~load_int(8);
var addr = msg~load_uint(256);
var dname = msg~load_ref().begin_parse();
var dname_len = slice_bits(dname);
msg.end_parse();
throw_unless(35, (dname_len > 0) & (dname_len <= 126 * 8));
throw_unless(35, dname_len % 8 == 0);
throw_unless(36, cat_id >= -1);
dname = align_str(dname);
var (records_cell, found) = domains.dict_get_ref?(slice_bits(dname), dname);
if (found) {
var rs = records_cell.begin_parse();
records = rs~load_dict();
rs.end_parse();
}
records~idict_set_builder(16, cat_id, begin_cell()
.store_int(wc, 8)
.store_uint(addr, 256));
domains~dict_set_ref(slice_bits(dname), dname, begin_cell()
.store_dict(records)
.end_cell());
return (domains, ());
}
_ ~process_domain_delete(domains, msg) impure {
var cat_id = msg~load_int(16);
var dname = align_str(msg~load_ref().begin_parse());
var dname_len = slice_bits(dname);
msg.end_parse();
throw_unless(35, (dname_len > 0) & (dname_len <= 126 * 8));
throw_unless(35, dname_len % 8 == 0);
throw_unless(36, cat_id >= -1);
if (cat_id == 0) {
var found = domains~dict_delete?(slice_bits(dname), dname);
throw_unless(44, found);
} else {
var (records_cell, found) = domains.dict_get_ref?(slice_bits(dname), dname);
throw_unless(44, found);
var rs = records_cell.begin_parse();
var records = rs~load_dict();
rs.end_parse();
records~idict_delete?(16, cat_id);
domains~dict_set_ref(slice_bits(dname), dname, begin_cell()
.store_dict(records)
.end_cell());
}
return (domains, ());
}
() recv_external(slice in_msg) impure {
var signature = in_msg~load_bits(512);
var msg_start = in_msg~load_ref().begin_parse();
var msg_slice = msg_start;
var (msg_seqno, op) = (msg_slice~load_uint(32), msg_slice~load_uint(32));
var (stored_seqno, public_key, domains) = load_data();
throw_unless(33, msg_seqno == stored_seqno);
throw_unless(34, check_signature(slice_hash(msg_start), signature, public_key));
if (op == 0x0) {
;; process_init();
} elseif (op == 0x2) {
domains~process_domain_upsert(msg_slice);
} elseif (op == 0x3) {
domains~process_domain_delete(msg_slice);
} elseif (op == 0x10) {
public_key = msg_slice~load_uint(256);
} elseif (op == 0x11) {
set_code(msg_slice~load_ref());
} else {
throw(50);
}
accept_message();
store_data(stored_seqno + 1, public_key, domains);
}
;; Get methods
(int, cell) dnsresolve(int cat_id, slice dname_req) method_id {
var domains = get_data().begin_parse().skip_bits(32 + 256).preload_dict();
var dname_len = slice_bits(dname_req);
var (res_len, res_cell) = (0, null());
throw_unless(35, (dname_len > 0) & (dname_len <= 126 * 8));
throw_unless(35, dname_len % 8 == 0);
throw_unless(36, cat_id >= -1);
;; Loop over higher-level domains (e.g. "a.b.c", "b.c", "c" ).
;;
;; Coding in following loops is quite weird and suboptimal due to
;; func compiler producing wrong code in case of return statements inside
;; nested contitionals. Also while loops also were broken -- need more
;; investigation.
do {
var dname = align_str(dname_req);
var (records_cell, found) = domains.dict_get_ref?(slice_bits(dname), dname);
if (found) {
var rs = records_cell.begin_parse();
var records = rs~load_dict();
rs.end_parse();
if (cat_id != 0) {
var (min_cat, rec_slice, rec_found) = records.idict_min?(16);
;; -1 has higher priority
if (min_cat.null?()) { min_cat = 0; }
ifnot (rec_found & (min_cat == -1)) {
(rec_slice, rec_found) = records.idict_get?(16, cat_id);
}
if (rec_found) {
var wc = rec_slice~load_int(8);
var addr = rec_slice~load_uint(256);
(res_len, res_cell) = (slice_bits(dname_req), begin_cell()
.store_int(wc, 8)
.store_uint(addr, 256)
.end_cell());
}
} else {
(res_len, res_cell) = (slice_bits(dname_req), records_cell);
}
}
var curr_byte = -1;
do {
curr_byte = dname_req~load_uint(8);
} until ((curr_byte == 0) | dname_req.slice_empty?() | (res_len != 0));
} until (dname_req.slice_empty?() | (res_len != 0));
return (res_len, res_cell);
}
int seqno() method_id {
return get_data().begin_parse().preload_uint(32);
}
(int, int, int) version() method_id {
return (0, 0, 1);
}