summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtoxav/msi.h2
-rwxr-xr-xtoxav/rtp.c7
-rwxr-xr-xtoxav/rtp.h1
-rw-r--r--toxcore/Messenger.c46
-rw-r--r--toxcore/Messenger.h9
-rw-r--r--toxcore/net_crypto.c70
-rw-r--r--toxcore/net_crypto.h36
-rw-r--r--toxcore/network.h6
8 files changed, 125 insertions, 52 deletions
diff --git a/toxav/msi.h b/toxav/msi.h
index 39a9c792..052126d2 100755
--- a/toxav/msi.h
+++ b/toxav/msi.h
@@ -40,7 +40,7 @@ typedef void ( *MSICallback ) ( void *arg );
40 * @brief Call type identifier. Also used as rtp callback prefix. 40 * @brief Call type identifier. Also used as rtp callback prefix.
41 */ 41 */
42typedef enum { 42typedef enum {
43 type_audio = 70, 43 type_audio = 192,
44 type_video 44 type_video
45} MSICallType; 45} MSICallType;
46 46
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 9462a467..d4aa5476 100755
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -491,7 +491,7 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
491 * @retval -1 Error occurred. 491 * @retval -1 Error occurred.
492 * @retval 0 Success. 492 * @retval 0 Success.
493 */ 493 */
494int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t length ) 494int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
495{ 495{
496 RTPSession *_session = object; 496 RTPSession *_session = object;
497 RTPMessage *_msg; 497 RTPMessage *_msg;
@@ -545,11 +545,6 @@ int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t l
545 545
546 if ( !_msg ) return -1; 546 if ( !_msg ) return -1;
547 547
548 /* Hopefully this goes well
549 * NOTE: Is this even used?
550 */
551 memcpy(&_msg->from, &ip_port, sizeof(IP_Port));
552
553 /* Check if message came in late */ 548 /* Check if message came in late */
554 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */ 549 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
555 _session->rsequnum = _msg->header->sequnum; 550 _session->rsequnum = _msg->header->sequnum;
diff --git a/toxav/rtp.h b/toxav/rtp.h
index 58b16ab1..40532391 100755
--- a/toxav/rtp.h
+++ b/toxav/rtp.h
@@ -75,7 +75,6 @@ typedef struct _RTPMessage {
75 75
76 uint8_t data[MAX_RTP_SIZE]; 76 uint8_t data[MAX_RTP_SIZE];
77 uint32_t length; 77 uint32_t length;
78 IP_Port from;
79 78
80 struct _RTPMessage *next; 79 struct _RTPMessage *next;
81} RTPMessage; 80} RTPMessage;
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 1e6e6c06..aefa3a68 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1699,46 +1699,35 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
1699 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); 1699 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
1700} 1700}
1701 1701
1702static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port) 1702static int handle_custom_user_packet(void *object, int friend_num, uint8_t *packet, uint16_t length)
1703{
1704 uint32_t i;
1705
1706 for (i = 0; i < m->numonline_friends; ++i) {
1707 if (ipport_equal(&m->online_friendlist[i].ip_port, &ip_port))
1708 return m->online_friendlist[i].friend_num;
1709 }
1710
1711 return -1;
1712}
1713
1714static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1715{ 1703{
1716 Messenger *m = object; 1704 Messenger *m = object;
1717 int32_t friend_num = friendnum_from_ip_port(m, source);
1718 1705
1719 if (friend_num == -1) 1706 if (friend_not_valid(m, friend_num))
1720 return 1; 1707 return 1;
1721 1708
1722 if (m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function) 1709 if (m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function)
1723 return m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function( 1710 return m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function(
1724 m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].object, source, packet, length); 1711 m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length);
1725 1712
1726 return 1; 1713 return 1;
1727} 1714}
1728 1715
1729 1716
1730int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, 1717int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1731 void *object) 1718 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object)
1732{ 1719{
1733 if (friend_not_valid(m, friendnumber)) 1720 if (friend_not_valid(m, friendnumber))
1734 return -1; 1721 return -1;
1735 1722
1736 if (byte < NET_PACKET_CUSTOM_RANGE_START || byte >= NET_PACKET_CUSTOM_RANGE_END) 1723 if (byte < PACKET_ID_LOSSY_RANGE_START)
1724 return -1;
1725
1726 if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
1737 return -1; 1727 return -1;
1738 1728
1739 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].function = cb; 1729 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback;
1740 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].object = object; 1730 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object;
1741 networking_registerhandler(m->net, byte, handle_custom_user_packet, m);
1742 return 0; 1731 return 0;
1743} 1732}
1744 1733
@@ -1750,12 +1739,10 @@ int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, u
1750 if (m->friendlist[friendnumber].status != FRIEND_ONLINE) 1739 if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
1751 return -1; 1740 return -1;
1752 1741
1753 IP_Port ip_port = get_friend_ipport(m, friendnumber); 1742 if (m->friendlist[friendnumber].crypt_connection_id == -1)
1754
1755 if (ip_port.port == 0)
1756 return -1; 1743 return -1;
1757 1744
1758 return sendpacket(m->net, ip_port, data, length); 1745 return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length);
1759} 1746}
1760 1747
1761 1748
@@ -1794,6 +1781,7 @@ static int handle_new_connections(void *object, New_Connection *n_c)
1794 int id = accept_crypto_connection(m->net_crypto, n_c); 1781 int id = accept_crypto_connection(m->net_crypto, n_c);
1795 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id); 1782 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id);
1796 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id); 1783 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id);
1784 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friend_id);
1797 m->friendlist[friend_id].crypt_connection_id = id; 1785 m->friendlist[friend_id].crypt_connection_id = id;
1798 set_friend_status(m, friend_id, FRIEND_CONFIRMED); 1786 set_friend_status(m, friend_id, FRIEND_CONFIRMED);
1799 return 0; 1787 return 0;
@@ -2218,8 +2206,8 @@ static int friend_new_connection(Messenger *m, int32_t friendnumber, uint8_t *re
2218 m->friendlist[friendnumber].crypt_connection_id = id; 2206 m->friendlist[friendnumber].crypt_connection_id = id;
2219 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber); 2207 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber);
2220 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber); 2208 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber);
2209 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friendnumber);
2221 return 0; 2210 return 0;
2222
2223} 2211}
2224 2212
2225/* TODO: Make this function not suck. */ 2213/* TODO: Make this function not suck. */
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index fb070607..e9447977 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -167,7 +167,10 @@ typedef struct {
167 int invited_groups[MAX_INVITED_GROUPS]; 167 int invited_groups[MAX_INVITED_GROUPS];
168 uint16_t invited_groups_num; 168 uint16_t invited_groups_num;
169 169
170 Packet_Handles packethandlers[TOTAL_USERPACKETS]; 170 struct {
171 int (*function)(void *object, uint8_t *data, uint32_t len);
172 void *object;
173 } packethandlers[PACKET_ID_LOSSY_RANGE_SIZE];
171} Friend; 174} Friend;
172 175
173typedef struct { 176typedef struct {
@@ -697,8 +700,8 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
697 * return -1 on failure. 700 * return -1 on failure.
698 * return 0 on success. 701 * return 0 on success.
699 */ 702 */
700int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, 703int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
701 void *object); 704 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object);
702 705
703/* High level function to send custom user packets. 706/* High level function to send custom user packets.
704 * 707 *
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 875c639f..2269e041 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1079,7 +1079,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin
1079 } else if (real_data[0] == PACKET_ID_KILL) { 1079 } else if (real_data[0] == PACKET_ID_KILL) {
1080 conn->killed = 1; 1080 conn->killed = 1;
1081 return 0; 1081 return 0;
1082 } else { 1082 } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) {
1083 Packet_Data dt; 1083 Packet_Data dt;
1084 dt.time = current_time_monotonic(); 1084 dt.time = current_time_monotonic();
1085 dt.length = real_length; 1085 dt.length = real_length;
@@ -1096,6 +1096,16 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin
1096 1096
1097 /* Packet counter. */ 1097 /* Packet counter. */
1098 ++conn->packet_counter; 1098 ++conn->packet_counter;
1099 } else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START &&
1100 real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) {
1101
1102 if (conn->connection_lossy_data_callback)
1103 conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object,
1104 conn->connection_lossy_data_callback_id, real_data, real_length);
1105
1106 set_buffer_end(&conn->recv_array, num);
1107 } else {
1108 return -1;
1099 } 1109 }
1100 1110
1101 if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { 1111 if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) {
@@ -2050,6 +2060,28 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
2050 return 0; 2060 return 0;
2051} 2061}
2052 2062
2063/* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
2064 *
2065 * The set function should return -1 on failure and 0 on success.
2066 * Object and id will be passed to this function untouched.
2067 *
2068 * return -1 on failure.
2069 * return 0 on success.
2070 */
2071int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
2072 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id)
2073{
2074 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2075
2076 if (conn == 0)
2077 return -1;
2078
2079 conn->connection_lossy_data_callback = connection_lossy_data_callback;
2080 conn->connection_lossy_data_callback_object = object;
2081 conn->connection_lossy_data_callback_id = id;
2082 return 0;
2083}
2084
2053/* Get the crypto connection id from the ip_port. 2085/* Get the crypto connection id from the ip_port.
2054 * 2086 *
2055 * return -1 on failure. 2087 * return -1 on failure.
@@ -2211,8 +2243,12 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
2211 2243
2212 2244
2213 2245
2214/* return -1 if data could not be put in packet queue. 2246/* Sends a lossless cryptopacket.
2215 * return positive packet number if data was put into the queue. 2247 *
2248 * return -1 if data could not be put in packet queue.
2249 * return positive packet number if data was put into the queue.
2250 *
2251 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
2216 */ 2252 */
2217int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 2253int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
2218{ 2254{
@@ -2222,6 +2258,9 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
2222 if (data[0] < CRYPTO_RESERVED_PACKETS) 2258 if (data[0] < CRYPTO_RESERVED_PACKETS)
2223 return -1; 2259 return -1;
2224 2260
2261 if (data[0] >= PACKET_ID_LOSSY_RANGE_START)
2262 return -1;
2263
2225 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2264 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2226 2265
2227 if (conn == 0) 2266 if (conn == 0)
@@ -2243,6 +2282,31 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
2243 return ret; 2282 return ret;
2244} 2283}
2245 2284
2285/* return -1 on failure.
2286 * return 0 on success.
2287 *
2288 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
2289 */
2290int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
2291{
2292 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
2293 return -1;
2294
2295 if (data[0] < PACKET_ID_LOSSY_RANGE_START)
2296 return -1;
2297
2298 if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
2299 return -1;
2300
2301 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2302
2303 if (conn == 0)
2304 return -1;
2305
2306 return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data,
2307 length);
2308}
2309
2246/* Kill a crypto connection. 2310/* Kill a crypto connection.
2247 * 2311 *
2248 * return -1 on failure. 2312 * return -1 on failure.
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 25f8c2f7..938046f1 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -69,6 +69,10 @@
69#define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */ 69#define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */
70#define STATUS_TCP_ONLINE 3 70#define STATUS_TCP_ONLINE 3
71 71
72/* All packets starting with a byte in this range are considered lossy packets. */
73#define PACKET_ID_LOSSY_RANGE_START 192
74#define PACKET_ID_LOSSY_RANGE_SIZE 63
75
72typedef struct { 76typedef struct {
73 uint64_t time; 77 uint64_t time;
74 uint16_t length; 78 uint16_t length;
@@ -119,6 +123,10 @@ typedef struct {
119 void *connection_data_callback_object; 123 void *connection_data_callback_object;
120 int connection_data_callback_id; 124 int connection_data_callback_id;
121 125
126 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length);
127 void *connection_lossy_data_callback_object;
128 int connection_lossy_data_callback_id;
129
122 uint64_t last_request_packet_sent; 130 uint64_t last_request_packet_sent;
123 131
124 uint32_t packet_counter; 132 uint32_t packet_counter;
@@ -231,7 +239,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port);
231int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, 239int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object,
232 int id, uint8_t status), void *object, int id); 240 int id, uint8_t status), void *object, int id);
233 241
234/* Set function to be called when connection with crypt_connection_id receives a data packet of length. 242/* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length.
235 * 243 *
236 * The set function should return -1 on failure and 0 on success. 244 * The set function should return -1 on failure and 0 on success.
237 * Object and id will be passed to this function untouched. 245 * Object and id will be passed to this function untouched.
@@ -243,16 +251,38 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
243 int id, uint8_t *data, uint16_t length), void *object, int id); 251 int id, uint8_t *data, uint16_t length), void *object, int id);
244 252
245 253
254/* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
255 *
256 * The set function should return -1 on failure and 0 on success.
257 * Object and id will be passed to this function untouched.
258 *
259 * return -1 on failure.
260 * return 0 on success.
261 */
262int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
263 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id);
264
246/* returns the number of packet slots left in the sendbuffer. 265/* returns the number of packet slots left in the sendbuffer.
247 * return 0 if failure. 266 * return 0 if failure.
248 */ 267 */
249uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id); 268uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id);
250 269
251/* return -1 if data could not be put in packet queue. 270/* Sends a lossless cryptopacket.
252 * return positive packet number if data was put into the queue. 271 *
272 * return -1 if data could not be put in packet queue.
273 * return positive packet number if data was put into the queue.
274 *
275 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
253 */ 276 */
254int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 277int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
255 278
279/* return -1 on failure.
280 * return 0 on success.
281 *
282 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
283 */
284int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
285
256/* Add a tcp relay, associating it to a crypt_connection_id. 286/* Add a tcp relay, associating it to a crypt_connection_id.
257 * 287 *
258 * return 0 if it was added. 288 * return 0 if it was added.
diff --git a/toxcore/network.h b/toxcore/network.h
index 0eb7bbd0..633f8010 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -122,12 +122,6 @@ typedef int sock_t;
122#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ 122#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */
123#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ 123#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */
124 124
125/* Range of ids that custom user packets can use. */
126#define NET_PACKET_CUSTOM_RANGE_START 64
127#define NET_PACKET_CUSTOM_RANGE_END 96
128
129#define TOTAL_USERPACKETS (NET_PACKET_CUSTOM_RANGE_END - NET_PACKET_CUSTOM_RANGE_START)
130
131/* See: docs/Prevent_Tracking.txt and onion.{c, h} */ 125/* See: docs/Prevent_Tracking.txt and onion.{c, h} */
132#define NET_PACKET_ONION_SEND_INITIAL 128 126#define NET_PACKET_ONION_SEND_INITIAL 128
133#define NET_PACKET_ONION_SEND_1 129 127#define NET_PACKET_ONION_SEND_1 129