1 /* SPDX-License-Identifier: GPL-3.0-or-later 2 * Copyright © 2016-2018 The TokTok team. 3 * Copyright © 2014 Tox project. 4 */ 5 6 /* 7 * Connection to friends. 8 */ 9 #ifndef C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H 10 #define C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H 11 12 #include "DHT.h" 13 #include "LAN_discovery.h" 14 #include "net_crypto.h" 15 #include "onion_client.h" 16 17 #define MAX_FRIEND_CONNECTION_CALLBACKS 2 18 #define MESSENGER_CALLBACK_INDEX 0 19 #define GROUPCHAT_CALLBACK_INDEX 1 20 21 #define PACKET_ID_ALIVE 16 22 #define PACKET_ID_SHARE_RELAYS 17 23 #define PACKET_ID_FRIEND_REQUESTS 18 24 25 /* Interval between the sending of ping packets. */ 26 #define FRIEND_PING_INTERVAL 8 27 28 /* If no packets are received from friend in this time interval, kill the connection. */ 29 #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 4) 30 31 /* Time before friend is removed from the DHT after last hearing about him. */ 32 #define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT 33 34 #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) 35 36 /* Max number of tcp relays sent to friends */ 37 #define MAX_SHARED_RELAYS RECOMMENDED_FRIEND_TCP_CONNECTIONS 38 39 /* Interval between the sending of tcp relay information */ 40 #define SHARE_RELAYS_INTERVAL (5 * 60) 41 42 43 typedef enum Friendconn_Status { 44 FRIENDCONN_STATUS_NONE, 45 FRIENDCONN_STATUS_CONNECTING, 46 FRIENDCONN_STATUS_CONNECTED, 47 } Friendconn_Status; 48 49 typedef struct Friend_Connections Friend_Connections; 50 51 Net_Crypto *friendconn_net_crypto(const Friend_Connections *fr_c); 52 53 /* return friendcon_id corresponding to the real public key on success. 54 * return -1 on failure. 55 */ 56 int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk); 57 58 /* Increases lock_count for the connection with friendcon_id by 1. 59 * 60 * return 0 on success. 61 * return -1 on failure. 62 */ 63 int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id); 64 65 /* return FRIENDCONN_STATUS_CONNECTED if the friend is connected. 66 * return FRIENDCONN_STATUS_CONNECTING if the friend isn't connected. 67 * return FRIENDCONN_STATUS_NONE on failure. 68 */ 69 unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id); 70 71 /* Copy public keys associated to friendcon_id. 72 * 73 * return 0 on success. 74 * return -1 on failure. 75 */ 76 int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id); 77 78 /* Set temp dht key for connection. 79 */ 80 void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk, void *userdata); 81 82 /* Add a TCP relay associated to the friend. 83 * 84 * return -1 on failure. 85 * return 0 on success. 86 */ 87 int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key); 88 89 typedef int global_status_cb(void *object, int id, uint8_t status, void *userdata); 90 91 typedef int fc_status_cb(void *object, int id, uint8_t status, void *userdata); 92 typedef int fc_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); 93 typedef int fc_lossy_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); 94 95 /* Set global status callback for friend connections. */ 96 void set_global_status_callback(Friend_Connections *fr_c, global_status_cb *global_status_callback, void *object); 97 98 /* Set the callbacks for the friend connection. 99 * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array. 100 * 101 * return 0 on success. 102 * return -1 on failure 103 */ 104 int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsigned int index, 105 fc_status_cb *status_callback, 106 fc_data_cb *data_callback, 107 fc_lossy_data_cb *lossy_data_callback, 108 void *object, int number); 109 110 /* return the crypt_connection_id for the connection. 111 * 112 * return crypt_connection_id on success. 113 * return -1 on failure. 114 */ 115 int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id); 116 117 /* Create a new friend connection. 118 * If one to that real public key already exists, increase lock count and return it. 119 * 120 * return -1 on failure. 121 * return connection id on success. 122 */ 123 int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_key); 124 125 /* Kill a friend connection. 126 * 127 * return -1 on failure. 128 * return 0 on success. 129 */ 130 int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id); 131 132 /* Send a Friend request packet. 133 * 134 * return -1 if failure. 135 * return 0 if it sent the friend request directly to the friend. 136 * return the number of peers it was routed through if it did not send it directly. 137 */ 138 int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data, 139 uint16_t length); 140 141 typedef int fr_request_cb(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len, 142 void *userdata); 143 144 /* Set friend request callback. 145 * 146 * This function will be called every time a friend request is received. 147 */ 148 void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object); 149 150 /* Create new friend_connections instance. */ 151 Friend_Connections *new_friend_connections(const Logger *logger, const Mono_Time *mono_time, Onion_Client *onion_c, 152 bool local_discovery_enabled); 153 154 /* main friend_connections loop. */ 155 void do_friend_connections(Friend_Connections *fr_c, void *userdata); 156 157 /* Free everything related with friend_connections. */ 158 void kill_friend_connections(Friend_Connections *fr_c); 159 160 #endif 161