diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 141 |
1 files changed, 115 insertions, 26 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a0a913d2..9425afba 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -379,6 +379,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat | |||
379 | return -1; | 379 | return -1; |
380 | 380 | ||
381 | increment_nonce(conn->sent_nonce); | 381 | increment_nonce(conn->sent_nonce); |
382 | conn->last_data_packet_sent = current_time(); //TODO remove this. | ||
382 | return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); | 383 | return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); |
383 | } | 384 | } |
384 | 385 | ||
@@ -459,6 +460,7 @@ static int new_temp_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *pack | |||
459 | conn->temp_packet = temp_packet; | 460 | conn->temp_packet = temp_packet; |
460 | conn->temp_packet_length = length; | 461 | conn->temp_packet_length = length; |
461 | conn->temp_packet_sent_time = 0; | 462 | conn->temp_packet_sent_time = 0; |
463 | conn->temp_packet_num_sent = 0; | ||
462 | return 0; | 464 | return 0; |
463 | } | 465 | } |
464 | 466 | ||
@@ -480,6 +482,7 @@ static int clear_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
480 | conn->temp_packet = 0; | 482 | conn->temp_packet = 0; |
481 | conn->temp_packet_length = 0; | 483 | conn->temp_packet_length = 0; |
482 | conn->temp_packet_sent_time = 0; | 484 | conn->temp_packet_sent_time = 0; |
485 | conn->temp_packet_num_sent = 0; | ||
483 | return 0; | 486 | return 0; |
484 | } | 487 | } |
485 | 488 | ||
@@ -503,6 +506,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
503 | return -1; | 506 | return -1; |
504 | 507 | ||
505 | conn->temp_packet_sent_time = current_time(); | 508 | conn->temp_packet_sent_time = current_time(); |
509 | ++conn->temp_packet_num_sent; | ||
506 | return 0; | 510 | return 0; |
507 | } | 511 | } |
508 | 512 | ||
@@ -594,7 +598,24 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, uint | |||
594 | 598 | ||
595 | case NET_PACKET_CRYPTO_DATA: { | 599 | case NET_PACKET_CRYPTO_DATA: { |
596 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { | 600 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { |
597 | //TODO | 601 | uint8_t data[MAX_DATA_DATA_PACKET_SIZE]; |
602 | int len = handle_data_packet(c, crypt_connection_id, data, packet, length); | ||
603 | |||
604 | if (len == -1) | ||
605 | return -1; | ||
606 | |||
607 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED) { | ||
608 | if (conn->connection_status_callback) | ||
609 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); | ||
610 | |||
611 | clear_temp_packet(c, crypt_connection_id); | ||
612 | conn->status = CRYPTO_CONN_ESTABLISHED; | ||
613 | } | ||
614 | |||
615 | if (conn->connection_data_callback) | ||
616 | conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, data, len); | ||
617 | |||
618 | //TODO add buffers and packet requesting. | ||
598 | } else { | 619 | } else { |
599 | return -1; | 620 | return -1; |
600 | } | 621 | } |
@@ -870,6 +891,9 @@ int set_conection_dht_public_key(Net_Crypto *c, int crypt_connection_id, uint8_t | |||
870 | if (conn == 0) | 891 | if (conn == 0) |
871 | return -1; | 892 | return -1; |
872 | 893 | ||
894 | if (conn->dht_public_key_set == 1 && memcmp(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
895 | return -1; | ||
896 | |||
873 | memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); | 897 | memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); |
874 | conn->dht_public_key_set = 1; | 898 | conn->dht_public_key_set = 1; |
875 | 899 | ||
@@ -908,6 +932,52 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port) | |||
908 | return 0; | 932 | return 0; |
909 | } | 933 | } |
910 | 934 | ||
935 | /* Set function to be called when connection with crypt_connection_id goes connects/disconnects. | ||
936 | * | ||
937 | * The set function should return -1 on failure and 0 on success. | ||
938 | * Note that if this function is set, the connection will clear itself on disconnect. | ||
939 | * Object and id will be passed to this function untouched. | ||
940 | * status is 1 if the connection is going online, 0 if it is going offline. | ||
941 | * | ||
942 | * return -1 on failure. | ||
943 | * return 0 on success. | ||
944 | */ | ||
945 | int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, | ||
946 | int id, uint8_t status), void *object, int id) | ||
947 | { | ||
948 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | ||
949 | |||
950 | if (conn == 0) | ||
951 | return -1; | ||
952 | |||
953 | conn->connection_status_callback = connection_status_callback; | ||
954 | conn->connection_status_callback_object = object; | ||
955 | conn->connection_status_callback_id = id; | ||
956 | return 0; | ||
957 | } | ||
958 | |||
959 | /* Set function to be called when connection with crypt_connection_id receives a data packet of length. | ||
960 | * | ||
961 | * The set function should return -1 on failure and 0 on success. | ||
962 | * Object and id will be passed to this function untouched. | ||
963 | * | ||
964 | * return -1 on failure. | ||
965 | * return 0 on success. | ||
966 | */ | ||
967 | int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | ||
968 | int id, uint8_t *data, uint16_t length), void *object, int id) | ||
969 | { | ||
970 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | ||
971 | |||
972 | if (conn == 0) | ||
973 | return -1; | ||
974 | |||
975 | conn->connection_data_callback = connection_data_callback; | ||
976 | conn->connection_data_callback_object = object; | ||
977 | conn->connection_data_callback_id = id; | ||
978 | return 0; | ||
979 | } | ||
980 | |||
911 | /* Get the crypto connection id from the ip_port. | 981 | /* Get the crypto connection id from the ip_port. |
912 | * | 982 | * |
913 | * return -1 on failure. | 983 | * return -1 on failure. |
@@ -977,27 +1047,26 @@ static void send_crypto_packets(Net_Crypto *c) | |||
977 | if (conn == 0) | 1047 | if (conn == 0) |
978 | return; | 1048 | return; |
979 | 1049 | ||
980 | if ((CRYPTO_SEND_PACKET_INTERVAL * 1000UL) + conn->temp_packet_sent_time < temp_time) { | 1050 | if ((CRYPTO_SEND_PACKET_INTERVAL * 1000ULL) + conn->temp_packet_sent_time < temp_time) { |
981 | send_temp_packet(c, i); | 1051 | send_temp_packet(c, i); |
982 | } | 1052 | } |
1053 | |||
1054 | if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED | ||
1055 | && (500ULL * 1000ULL) + conn->last_data_packet_sent < temp_time) {//TODO remove this. | ||
1056 | uint8_t data[4] = {}; | ||
1057 | send_data_packet(c, i, data, 4); | ||
1058 | } | ||
983 | } | 1059 | } |
984 | } | 1060 | } |
985 | 1061 | ||
986 | /* return 0 if there is no received data in the buffer. | ||
987 | * return -1 if the packet was discarded. | ||
988 | * return length of received data if successful. | ||
989 | */ | ||
990 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | ||
991 | { | ||
992 | |||
993 | } | ||
994 | 1062 | ||
995 | /* returns the number of packet slots left in the sendbuffer. | 1063 | /* returns the number of packet slots left in the sendbuffer. |
996 | * return 0 if failure. | 1064 | * return 0 if failure. |
997 | */ | 1065 | */ |
998 | uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | 1066 | uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) |
999 | { | 1067 | { |
1000 | 1068 | //TODO | |
1069 | return 0; | ||
1001 | } | 1070 | } |
1002 | 1071 | ||
1003 | /* return 0 if data could not be put in packet queue. | 1072 | /* return 0 if data could not be put in packet queue. |
@@ -1005,28 +1074,22 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | |||
1005 | */ | 1074 | */ |
1006 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 1075 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) |
1007 | { | 1076 | { |
1077 | //TODO | ||
1078 | if (send_data_packet(c, crypt_connection_id, data, length) == 0) | ||
1079 | return 1; | ||
1008 | 1080 | ||
1009 | } | 1081 | return 0; |
1010 | |||
1011 | |||
1012 | /* Start a secure connection with other peer who has public_key and ip_port. | ||
1013 | * | ||
1014 | * return -1 if failure. | ||
1015 | * return crypt_connection_id of the initialized connection if everything went well. | ||
1016 | */ | ||
1017 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | ||
1018 | { | ||
1019 | |||
1020 | } | 1082 | } |
1021 | 1083 | ||
1022 | /* Kill a crypto connection. | 1084 | /* Kill a crypto connection. |
1023 | * | 1085 | * |
1024 | * return 0 if killed successfully. | 1086 | * return -1 on failure. |
1025 | * return 1 if there was a problem. | 1087 | * return 0 on success. |
1026 | */ | 1088 | */ |
1027 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 1089 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
1028 | { | 1090 | { |
1029 | 1091 | //TODO | |
1092 | return wipe_crypto_connection(c, crypt_connection_id); | ||
1030 | } | 1093 | } |
1031 | 1094 | ||
1032 | /* return 0 if no connection. | 1095 | /* return 0 if no connection. |
@@ -1096,9 +1159,35 @@ Net_Crypto *new_net_crypto(DHT *dht) | |||
1096 | static void kill_timedout(Net_Crypto *c) | 1159 | static void kill_timedout(Net_Crypto *c) |
1097 | { | 1160 | { |
1098 | uint32_t i; | 1161 | uint32_t i; |
1162 | uint64_t temp_time = current_time(); | ||
1099 | 1163 | ||
1100 | for (i = 0; i < c->crypto_connections_length; ++i) { | 1164 | for (i = 0; i < c->crypto_connections_length; ++i) { |
1101 | //TODO | 1165 | Crypto_Connection *conn = get_crypto_connection(c, i); |
1166 | |||
1167 | if (conn == 0) | ||
1168 | return; | ||
1169 | |||
1170 | if (conn->status == CRYPTO_CONN_NO_CONNECTION || conn->status == CRYPTO_CONN_TIMED_OUT) | ||
1171 | continue; | ||
1172 | |||
1173 | if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING || conn->status == CRYPTO_CONN_HANDSHAKE_SENT | ||
1174 | || conn->status == CRYPTO_CONN_NOT_CONFIRMED) { | ||
1175 | if (conn->temp_packet_num_sent < MAX_NUM_SENDPACKET_TRIES) | ||
1176 | continue; | ||
1177 | |||
1178 | if (conn->connection_status_callback) { | ||
1179 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 0); | ||
1180 | crypto_kill(c, i); | ||
1181 | continue; | ||
1182 | } | ||
1183 | |||
1184 | conn->status = CRYPTO_CONN_TIMED_OUT; | ||
1185 | continue; | ||
1186 | } | ||
1187 | |||
1188 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | ||
1189 | //TODO: add a timeout here? | ||
1190 | } | ||
1102 | } | 1191 | } |
1103 | } | 1192 | } |
1104 | 1193 | ||