/work/toxcore/tox_private.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2022 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * The Tox private API (for tests). |
8 | | */ |
9 | | #include "tox_private.h" |
10 | | |
11 | | #include <assert.h> |
12 | | |
13 | | #include "DHT.h" |
14 | | #include "ccompat.h" |
15 | | #include "crypto_core.h" |
16 | | #include "group_chats.h" |
17 | | #include "group_common.h" |
18 | | #include "mem.h" |
19 | | #include "net_crypto.h" |
20 | | #include "network.h" |
21 | | #include "tox.h" |
22 | | #include "tox_struct.h" |
23 | | |
24 | | #define SET_ERROR_PARAMETER(param, x) \ |
25 | 27.0k | do { \ |
26 | 27.0k | if (param != nullptr) { \ |
27 | 12 | *param = x; \ |
28 | 12 | } \ |
29 | 27.0k | } while (0) |
30 | | |
31 | | Tox_System tox_default_system(void) |
32 | 4.22k | { |
33 | 4.22k | const Tox_System sys = { |
34 | 4.22k | nullptr, // mono_time_callback |
35 | 4.22k | nullptr, // mono_time_user_data |
36 | 4.22k | os_random(), |
37 | 4.22k | os_network(), |
38 | 4.22k | os_memory(), |
39 | 4.22k | }; |
40 | 4.22k | return sys; |
41 | 4.22k | } |
42 | | |
43 | | void tox_lock(const Tox *tox) |
44 | 589k | { |
45 | 589k | if (tox->mutex != nullptr) { |
46 | 0 | pthread_mutex_lock(tox->mutex); |
47 | 0 | } |
48 | 589k | } |
49 | | |
50 | | void tox_unlock(const Tox *tox) |
51 | 589k | { |
52 | 589k | if (tox->mutex != nullptr) { |
53 | 0 | pthread_mutex_unlock(tox->mutex); |
54 | 0 | } |
55 | 589k | } |
56 | | |
57 | | void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid) |
58 | 0 | { |
59 | 0 | assert(tox != nullptr); |
60 | | |
61 | 0 | if (pktid >= PACKET_ID_RANGE_LOSSY_START && pktid <= PACKET_ID_RANGE_LOSSY_END) { |
62 | 0 | tox->friend_lossy_packet_callback_per_pktid[pktid] = callback; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid) |
67 | 0 | { |
68 | 0 | assert(tox != nullptr); |
69 | | |
70 | 0 | if ((pktid >= PACKET_ID_RANGE_LOSSLESS_CUSTOM_START && pktid <= PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) |
71 | 0 | || pktid == PACKET_ID_MSI) { |
72 | 0 | tox->friend_lossless_packet_callback_per_pktid[pktid] = callback; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | void tox_set_av_object(Tox *tox, void *object) |
77 | 0 | { |
78 | 0 | assert(tox != nullptr); |
79 | 0 | tox_lock(tox); |
80 | 0 | tox->toxav_object = object; |
81 | 0 | tox_unlock(tox); |
82 | 0 | } |
83 | | |
84 | | void *tox_get_av_object(const Tox *tox) |
85 | 0 | { |
86 | 0 | assert(tox != nullptr); |
87 | 0 | tox_lock(tox); |
88 | 0 | void *object = tox->toxav_object; |
89 | 0 | tox_unlock(tox); |
90 | 0 | return object; |
91 | 0 | } |
92 | | |
93 | | void tox_callback_dht_get_nodes_response(Tox *tox, tox_dht_get_nodes_response_cb *callback) |
94 | 2.33k | { |
95 | 2.33k | assert(tox != nullptr); |
96 | 2.33k | tox->dht_get_nodes_response_callback = callback; |
97 | 2.33k | } |
98 | | |
99 | | bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port, |
100 | | const uint8_t *target_public_key, Tox_Err_Dht_Get_Nodes *error) |
101 | 27.0k | { |
102 | 27.0k | assert(tox != nullptr); |
103 | | |
104 | 27.0k | tox_lock(tox); |
105 | | |
106 | 27.0k | if (tox->m->options.udp_disabled) { |
107 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_UDP_DISABLED); |
108 | 0 | tox_unlock(tox); |
109 | 0 | return false; |
110 | 0 | } |
111 | | |
112 | 27.0k | if (public_key == nullptr || ip == nullptr || target_public_key == nullptr) { |
113 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_NULL); |
114 | 0 | tox_unlock(tox); |
115 | 0 | return false; |
116 | 0 | } |
117 | | |
118 | 27.0k | if (port == 0) { |
119 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_BAD_PORT); |
120 | 0 | tox_unlock(tox); |
121 | 0 | return false; |
122 | 0 | } |
123 | | |
124 | 27.0k | IP_Port *root; |
125 | | |
126 | 27.0k | const int32_t count = net_getipport(tox->sys.mem, ip, &root, TOX_SOCK_DGRAM); |
127 | | |
128 | 27.0k | if (count < 1) { |
129 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_BAD_IP); |
130 | 0 | net_freeipport(tox->sys.mem, root); |
131 | 0 | tox_unlock(tox); |
132 | 0 | return false; |
133 | 0 | } |
134 | | |
135 | 27.0k | bool success = false; |
136 | | |
137 | 54.0k | for (int32_t i = 0; i < count; ++i) { |
138 | 27.0k | root[i].port = net_htons(port); |
139 | | |
140 | 27.0k | if (dht_getnodes(tox->m->dht, &root[i], public_key, target_public_key)) { |
141 | 26.1k | success = true; |
142 | 26.1k | } |
143 | 27.0k | } |
144 | | |
145 | 27.0k | tox_unlock(tox); |
146 | | |
147 | 27.0k | net_freeipport(tox->sys.mem, root); |
148 | | |
149 | 27.0k | if (!success) { |
150 | 900 | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_FAIL); |
151 | 900 | return false; |
152 | 900 | } |
153 | | |
154 | 26.1k | SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_OK); |
155 | | |
156 | 26.1k | return true; |
157 | 27.0k | } |
158 | | |
159 | 30 | uint16_t tox_dht_get_num_closelist(const Tox *tox) { |
160 | 30 | tox_lock(tox); |
161 | 30 | const uint16_t num_total = dht_get_num_closelist(tox->m->dht); |
162 | 30 | tox_unlock(tox); |
163 | | |
164 | 30 | return num_total; |
165 | 30 | } |
166 | | |
167 | 30 | uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox){ |
168 | 30 | tox_lock(tox); |
169 | 30 | const uint16_t num_cap = dht_get_num_closelist_announce_capable(tox->m->dht); |
170 | 30 | tox_unlock(tox); |
171 | | |
172 | 30 | return num_cap; |
173 | 30 | } |
174 | | |
175 | | size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number, uint32_t peer_id, |
176 | | Tox_Err_Group_Peer_Query *error) |
177 | 6 | { |
178 | 6 | assert(tox != nullptr); |
179 | | |
180 | 6 | tox_lock(tox); |
181 | 6 | const GC_Chat *chat = gc_get_group(tox->m->group_handler, group_number); |
182 | | |
183 | 6 | if (chat == nullptr) { |
184 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_GROUP_NOT_FOUND); |
185 | 0 | tox_unlock(tox); |
186 | 0 | return -1; |
187 | 0 | } |
188 | | |
189 | 6 | const int ret = gc_get_peer_ip_address_size(chat, peer_id); |
190 | 6 | tox_unlock(tox); |
191 | | |
192 | 6 | if (ret == -1) { |
193 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_PEER_NOT_FOUND); |
194 | 0 | return -1; |
195 | 6 | } else { |
196 | 6 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_OK); |
197 | 6 | return ret; |
198 | 6 | } |
199 | 6 | } |
200 | | |
201 | | bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_addr, |
202 | | Tox_Err_Group_Peer_Query *error) |
203 | 6 | { |
204 | 6 | assert(tox != nullptr); |
205 | | |
206 | 6 | tox_lock(tox); |
207 | 6 | const GC_Chat *chat = gc_get_group(tox->m->group_handler, group_number); |
208 | | |
209 | 6 | if (chat == nullptr) { |
210 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_GROUP_NOT_FOUND); |
211 | 0 | tox_unlock(tox); |
212 | 0 | return false; |
213 | 0 | } |
214 | | |
215 | 6 | const int ret = gc_get_peer_ip_address(chat, peer_id, ip_addr); |
216 | 6 | tox_unlock(tox); |
217 | | |
218 | 6 | if (ret == -1) { |
219 | 0 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_PEER_NOT_FOUND); |
220 | 0 | return false; |
221 | 0 | } |
222 | | |
223 | 6 | SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_OK); |
224 | 6 | return true; |
225 | 6 | } |