diff options
author | dubslow <bunslow@gmail.com> | 2014-09-28 12:37:47 -0500 |
---|---|---|
committer | dubslow <bunslow@gmail.com> | 2014-09-28 12:37:47 -0500 |
commit | 57666d95bac5e070acda74020f5faca88cfa4bd5 (patch) | |
tree | 7dd17305a43ce332984034a1dec783d8e96ac780 /toxcore/friend_connection.h | |
parent | 3711b881cb6f64cf08209f70216ec75672464d1a (diff) | |
parent | 72d6a92efd8cad191282c8a2e294a768c49d5f25 (diff) |
Merge branch 'master' of https://github.com/irungentoo/toxcore
Diffstat (limited to 'toxcore/friend_connection.h')
-rw-r--r-- | toxcore/friend_connection.h | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h new file mode 100644 index 00000000..62b82dc2 --- /dev/null +++ b/toxcore/friend_connection.h | |||
@@ -0,0 +1,140 @@ | |||
1 | /* friend_connection.h | ||
2 | * | ||
3 | * Connection to friends. | ||
4 | * | ||
5 | * Copyright (C) 2014 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef FRIEND_CONNECTION_H | ||
26 | #define FRIEND_CONNECTION_H | ||
27 | |||
28 | #include "net_crypto.h" | ||
29 | #include "DHT.h" | ||
30 | #include "LAN_discovery.h" | ||
31 | #include "onion_client.h" | ||
32 | |||
33 | |||
34 | #define MAX_FRIEND_CONNECTION_CALLBACKS 2 | ||
35 | #define MESSENGER_CALLBACK_INDEX 0 | ||
36 | #define GROUPCHAT_CALLBACK_INDEX 1 | ||
37 | |||
38 | #define PACKET_ID_ALIVE 16 | ||
39 | |||
40 | /* Interval between the sending of ping packets. */ | ||
41 | #define FRIEND_PING_INTERVAL 6 | ||
42 | |||
43 | /* If no packets are received from friend in this time interval, kill the connection. */ | ||
44 | #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 3) | ||
45 | |||
46 | /* Time before friend is removed from the DHT after last hearing about him. */ | ||
47 | #define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT | ||
48 | |||
49 | |||
50 | enum { | ||
51 | FRIENDCONN_STATUS_NONE, | ||
52 | FRIENDCONN_STATUS_CONNECTING, | ||
53 | FRIENDCONN_STATUS_CONNECTED | ||
54 | }; | ||
55 | |||
56 | typedef struct { | ||
57 | uint8_t status; | ||
58 | |||
59 | uint8_t real_public_key[crypto_box_PUBLICKEYBYTES]; | ||
60 | uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; | ||
61 | uint16_t dht_lock; | ||
62 | IP_Port dht_ip_port; | ||
63 | uint64_t dht_ping_lastrecv, dht_ip_port_lastrecv; | ||
64 | |||
65 | int onion_friendnum; | ||
66 | int crypt_connection_id; | ||
67 | |||
68 | uint64_t ping_lastrecv, ping_lastsent; | ||
69 | |||
70 | struct { | ||
71 | int (*status_callback)(void *object, int id, uint8_t status); | ||
72 | void *status_callback_object; | ||
73 | int status_callback_id; | ||
74 | |||
75 | int (*data_callback)(void *object, int id, uint8_t *data, uint16_t length); | ||
76 | void *data_callback_object; | ||
77 | int data_callback_id; | ||
78 | |||
79 | int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length); | ||
80 | void *lossy_data_callback_object; | ||
81 | int lossy_data_callback_id; | ||
82 | } callbacks[MAX_FRIEND_CONNECTION_CALLBACKS]; | ||
83 | |||
84 | uint16_t lock_count; | ||
85 | } Friend_Conn; | ||
86 | |||
87 | |||
88 | typedef struct { | ||
89 | Net_Crypto *net_crypto; | ||
90 | DHT *dht; | ||
91 | Onion_Client *onion_c; | ||
92 | |||
93 | Friend_Conn *conns; | ||
94 | uint32_t num_cons; | ||
95 | |||
96 | } Friend_Connections; | ||
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 | int (*status_callback)(void *object, int id, uint8_t status), int (*data_callback)(void *object, int id, uint8_t *data, | ||
106 | uint16_t length), int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, | ||
107 | int number); | ||
108 | |||
109 | /* return the crypt_connection_id for the connection. | ||
110 | * | ||
111 | * return crypt_connection_id on success. | ||
112 | * return -1 on failure. | ||
113 | */ | ||
114 | int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id); | ||
115 | |||
116 | /* Create a new friend connection. | ||
117 | * If one to that real public key already exists, increase lock count and return it. | ||
118 | * | ||
119 | * return -1 on failure. | ||
120 | * return connection id on success. | ||
121 | */ | ||
122 | int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_key); | ||
123 | |||
124 | /* Kill a friend connection. | ||
125 | * | ||
126 | * return -1 on failure. | ||
127 | * return 0 on success. | ||
128 | */ | ||
129 | int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id); | ||
130 | |||
131 | /* Create new friend_connections instance. */ | ||
132 | Friend_Connections *new_friend_connections(Onion_Client *onion_c); | ||
133 | |||
134 | /* main friend_connections loop. */ | ||
135 | void do_friend_connections(Friend_Connections *fr_c); | ||
136 | |||
137 | /* Free everything related with friend_connections. */ | ||
138 | void kill_friend_connections(Friend_Connections *fr_c); | ||
139 | |||
140 | #endif | ||