summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c70
1 files changed, 67 insertions, 3 deletions
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.