summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/BUILD.bazel1
-rw-r--r--toxcore/Makefile.inc1
-rw-r--r--toxcore/Messenger.c3
-rw-r--r--toxcore/tox.api.h6
-rw-r--r--toxcore/tox.c66
-rw-r--r--toxcore/tox.h6
-rw-r--r--toxcore/tox_private.h44
7 files changed, 109 insertions, 18 deletions
diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel
index 403fa7e8..d8d86d56 100644
--- a/toxcore/BUILD.bazel
+++ b/toxcore/BUILD.bazel
@@ -272,6 +272,7 @@ cc_library(
272 srcs = [ 272 srcs = [
273 "tox.c", 273 "tox.c",
274 "tox.h", 274 "tox.h",
275 "tox_private.h",
275 "tox_api.c", 276 "tox_api.c",
276 ], 277 ],
277 visibility = ["//c-toxcore:__subpackages__"], 278 visibility = ["//c-toxcore:__subpackages__"],
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc
index a68b2a7d..be0c243c 100644
--- a/toxcore/Makefile.inc
+++ b/toxcore/Makefile.inc
@@ -32,6 +32,7 @@ libtoxcore_la_SOURCES = ../toxcore/ccompat.h \
32 ../toxcore/state.h \ 32 ../toxcore/state.h \
33 ../toxcore/state.c \ 33 ../toxcore/state.c \
34 ../toxcore/tox.h \ 34 ../toxcore/tox.h \
35 ../toxcore/tox_private.h \
35 ../toxcore/tox.c \ 36 ../toxcore/tox.c \
36 ../toxcore/tox_api.c \ 37 ../toxcore/tox_api.c \
37 ../toxcore/util.h \ 38 ../toxcore/util.h \
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index a4bc8100..c96a330f 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1889,7 +1889,8 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
1889 return -2; 1889 return -2;
1890 } 1890 }
1891 1891
1892 if (data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) { 1892 if ((data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END)
1893 && data[0] != PACKET_ID_MSI) {
1893 return -3; 1894 return -3;
1894 } 1895 }
1895 1896
diff --git a/toxcore/tox.api.h b/toxcore/tox.api.h
index 5e42b866..3cda8126 100644
--- a/toxcore/tox.api.h
+++ b/toxcore/tox.api.h
@@ -2670,7 +2670,7 @@ namespace friend {
2670 FRIEND_NOT_CONNECTED, 2670 FRIEND_NOT_CONNECTED,
2671 /** 2671 /**
2672 * The first byte of data was not in the specified range for the packet type. 2672 * The first byte of data was not in the specified range for the packet type.
2673 * This range is 200-254 for lossy, and 160-191 for lossless packets. 2673 * This range is 192-254 for lossy, and 69, 160-191 for lossless packets.
2674 */ 2674 */
2675 INVALID, 2675 INVALID,
2676 /** 2676 /**
@@ -2692,7 +2692,7 @@ namespace friend {
2692 /** 2692 /**
2693 * Send a custom lossy packet to a friend. 2693 * Send a custom lossy packet to a friend.
2694 * 2694 *
2695 * The first byte of data must be in the range 200-254. Maximum length of a 2695 * The first byte of data must be in the range 192-254. Maximum length of a
2696 * custom packet is $MAX_CUSTOM_PACKET_SIZE. 2696 * custom packet is $MAX_CUSTOM_PACKET_SIZE.
2697 * 2697 *
2698 * Lossy packets behave like UDP packets, meaning they might never reach the 2698 * Lossy packets behave like UDP packets, meaning they might never reach the
@@ -2716,7 +2716,7 @@ namespace friend {
2716 /** 2716 /**
2717 * Send a custom lossless packet to a friend. 2717 * Send a custom lossless packet to a friend.
2718 * 2718 *
2719 * The first byte of data must be in the range 160-191. Maximum length of a 2719 * The first byte of data must be in the range 69, 160-191. Maximum length of a
2720 * custom packet is $MAX_CUSTOM_PACKET_SIZE. 2720 * custom packet is $MAX_CUSTOM_PACKET_SIZE.
2721 * 2721 *
2722 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order) 2722 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order)
diff --git a/toxcore/tox.c b/toxcore/tox.c
index fdf2b42c..7126631e 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -17,6 +17,7 @@
17#endif 17#endif
18 18
19#include "tox.h" 19#include "tox.h"
20#include "tox_private.h"
20 21
21#include <assert.h> 22#include <assert.h>
22#include <stdlib.h> 23#include <stdlib.h>
@@ -89,8 +90,10 @@ struct Tox {
89 tox_conference_title_cb *conference_title_callback; 90 tox_conference_title_cb *conference_title_callback;
90 tox_conference_peer_name_cb *conference_peer_name_callback; 91 tox_conference_peer_name_cb *conference_peer_name_callback;
91 tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback; 92 tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback;
92 tox_friend_lossy_packet_cb *friend_lossy_packet_callback; 93 tox_friend_lossy_packet_cb *friend_lossy_packet_callback_per_pktid[UINT8_MAX + 1];
93 tox_friend_lossless_packet_cb *friend_lossless_packet_callback; 94 tox_friend_lossless_packet_cb *friend_lossless_packet_callback_per_pktid[UINT8_MAX + 1];
95
96 void *toxav_object; // workaround to store a ToxAV object (setter and getter functions are available)
94}; 97};
95 98
96static void lock(const Tox *tox) 99static void lock(const Tox *tox)
@@ -309,20 +312,28 @@ static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conf
309static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, 312static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
310 const uint8_t *data, size_t length, void *user_data) 313 const uint8_t *data, size_t length, void *user_data)
311{ 314{
315 assert(data != nullptr);
316 assert(length > 0);
317
312 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; 318 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
313 319
314 if (tox_data->tox->friend_lossy_packet_callback != nullptr) { 320 if (tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id] != nullptr) {
315 tox_data->tox->friend_lossy_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); 321 tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
322 tox_data->user_data);
316 } 323 }
317} 324}
318 325
319static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, 326static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
320 const uint8_t *data, size_t length, void *user_data) 327 const uint8_t *data, size_t length, void *user_data)
321{ 328{
329 assert(data != nullptr);
330 assert(length > 0);
331
322 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; 332 struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
323 333
324 if (tox_data->tox->friend_lossless_packet_callback != nullptr) { 334 if (tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id] != nullptr) {
325 tox_data->tox->friend_lossless_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); 335 tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
336 tox_data->user_data);
326 } 337 }
327} 338}
328 339
@@ -2171,9 +2182,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
2171 return 0; 2182 return 0;
2172 } 2183 }
2173 2184
2174 // TODO(oxij): this feels ugly, this is needed only because m_send_custom_lossy_packet in Messenger.c 2185 if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
2175 // sends both AV and custom packets despite its name and this API hides those AV packets
2176 if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
2177 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID); 2186 SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID);
2178 return 0; 2187 return 0;
2179 } 2188 }
@@ -2193,7 +2202,17 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
2193 2202
2194void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) 2203void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback)
2195{ 2204{
2196 tox->friend_lossy_packet_callback = callback; 2205 /* start at PACKET_ID_RANGE_LOSSY_CUSTOM_START so ToxAV Packets are excluded */
2206 for (uint8_t i = PACKET_ID_RANGE_LOSSY_CUSTOM_START; i <= PACKET_ID_RANGE_LOSSY_END; ++i) {
2207 tox->friend_lossy_packet_callback_per_pktid[i] = callback;
2208 }
2209}
2210
2211void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid)
2212{
2213 if (pktid >= PACKET_ID_RANGE_LOSSY_START && pktid <= PACKET_ID_RANGE_LOSSY_END) {
2214 tox->friend_lossy_packet_callback_per_pktid[pktid] = callback;
2215 }
2197} 2216}
2198 2217
2199bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, 2218bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
@@ -2224,7 +2243,17 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin
2224 2243
2225void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) 2244void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback)
2226{ 2245{
2227 tox->friend_lossless_packet_callback = callback; 2246 for (uint8_t i = PACKET_ID_RANGE_LOSSLESS_CUSTOM_START; i <= PACKET_ID_RANGE_LOSSLESS_CUSTOM_END; ++i) {
2247 tox->friend_lossless_packet_callback_per_pktid[i] = callback;
2248 }
2249}
2250
2251void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid)
2252{
2253 if ((pktid >= PACKET_ID_RANGE_LOSSLESS_CUSTOM_START && pktid <= PACKET_ID_RANGE_LOSSLESS_CUSTOM_END)
2254 || pktid == PACKET_ID_MSI) {
2255 tox->friend_lossless_packet_callback_per_pktid[pktid] = callback;
2256 }
2228} 2257}
2229 2258
2230void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) 2259void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
@@ -2236,6 +2265,21 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
2236 } 2265 }
2237} 2266}
2238 2267
2268void tox_set_av_object(Tox *tox, void *object)
2269{
2270 lock(tox);
2271 tox->toxav_object = object;
2272 unlock(tox);
2273}
2274
2275void *tox_get_av_object(const Tox *tox)
2276{
2277 lock(tox);
2278 void *object = tox->toxav_object;
2279 unlock(tox);
2280 return object;
2281}
2282
2239uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) 2283uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error)
2240{ 2284{
2241 lock(tox); 2285 lock(tox);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index be6f6d34..c4759507 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -3067,7 +3067,7 @@ typedef enum TOX_ERR_FRIEND_CUSTOM_PACKET {
3067 3067
3068 /** 3068 /**
3069 * The first byte of data was not in the specified range for the packet type. 3069 * The first byte of data was not in the specified range for the packet type.
3070 * This range is 200-254 for lossy, and 160-191 for lossless packets. 3070 * This range is 192-254 for lossy, and 69, 160-191 for lossless packets.
3071 */ 3071 */
3072 TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID, 3072 TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID,
3073 3073
@@ -3092,7 +3092,7 @@ typedef enum TOX_ERR_FRIEND_CUSTOM_PACKET {
3092/** 3092/**
3093 * Send a custom lossy packet to a friend. 3093 * Send a custom lossy packet to a friend.
3094 * 3094 *
3095 * The first byte of data must be in the range 200-254. Maximum length of a 3095 * The first byte of data must be in the range 192-254. Maximum length of a
3096 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE. 3096 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
3097 * 3097 *
3098 * Lossy packets behave like UDP packets, meaning they might never reach the 3098 * Lossy packets behave like UDP packets, meaning they might never reach the
@@ -3115,7 +3115,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
3115/** 3115/**
3116 * Send a custom lossless packet to a friend. 3116 * Send a custom lossless packet to a friend.
3117 * 3117 *
3118 * The first byte of data must be in the range 160-191. Maximum length of a 3118 * The first byte of data must be in the range 69, 160-191. Maximum length of a
3119 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE. 3119 * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE.
3120 * 3120 *
3121 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order) 3121 * Lossless packet behaviour is comparable to TCP (reliability, arrive in order)
diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h
new file mode 100644
index 00000000..593282f7
--- /dev/null
+++ b/toxcore/tox_private.h
@@ -0,0 +1,44 @@
1/* SPDX-License-Identifier: GPL-3.0-or-later
2 * Copyright © 2016-2020 The TokTok team.
3 * Copyright © 2013 Tox project.
4 */
5
6#ifndef C_TOXCORE_TOXCORE_TOX_PRIVATE_H
7#define C_TOXCORE_TOXCORE_TOX_PRIVATE_H
8
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/**
18 * Set the callback for the `friend_lossy_packet` event for a specific packet ID.
19 * Pass NULL to unset.
20 *
21 * allowed packet ID range:
22 * from `PACKET_ID_RANGE_LOSSY_START` to `PACKET_ID_RANGE_LOSSY_END` (both inclusive)
23 */
24void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid);
25
26/**
27 * Set the callback for the `friend_lossless_packet` event for a specific packet ID.
28 * Pass NULL to unset.
29 *
30 * allowed packet ID range:
31 * from `PACKET_ID_RANGE_LOSSLESS_CUSTOM_START` to `PACKET_ID_RANGE_LOSSLESS_CUSTOM_END` (both inclusive)
32 * and
33 * `PACKET_ID_MSI`
34 */
35void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid);
36
37void tox_set_av_object(Tox *tox, void *object);
38void *tox_get_av_object(const Tox *tox);
39
40#ifdef __cplusplus
41}
42#endif
43
44#endif // C_TOXCORE_TOXCORE_TOX_PRIVATE_H