summaryrefslogtreecommitdiff
path: root/core/Lossless_UDP.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/Lossless_UDP.h')
-rw-r--r--core/Lossless_UDP.h222
1 files changed, 0 insertions, 222 deletions
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h
deleted file mode 100644
index 176e86ce..00000000
--- a/core/Lossless_UDP.h
+++ /dev/null
@@ -1,222 +0,0 @@
1/* Lossless_UDP.h
2 *
3 * An implementation of the Lossless_UDP protocol as seen in http://wiki.tox.im/index.php/Lossless_UDP
4 *
5 * Copyright (C) 2013 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#ifndef LOSSLESS_UDP_H
25#define LOSSLESS_UDP_H
26
27#include "network.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/* maximum length of the data in the data packets */
34#define MAX_DATA_SIZE 1024
35
36/* maximum data packets in sent and receive queues. */
37#define MAX_QUEUE_NUM 16
38
39/* maximum number of data packets in the buffer */
40#define BUFFER_PACKET_NUM (16-1)
41
42/* timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */
43#define CONNEXION_TIMEOUT 5
44
45/* initial amount of sync/hanshake packets to send per second. */
46#define SYNC_RATE 2
47
48/* initial send rate of data. */
49#define DATA_SYNC_RATE 30
50
51typedef struct {
52 uint8_t data[MAX_DATA_SIZE];
53 uint16_t size;
54} Data;
55
56typedef struct {
57 IP_Port ip_port;
58
59 /*
60 * 0 if connection is dead, 1 if attempting handshake,
61 * 2 if handshake is done (we start sending SYNC packets)
62 * 3 if we are sending SYNC packets and can send data
63 * 4 if the connection has timed out.
64 */
65 uint8_t status;
66
67 /*
68 * 1 or 2 if connection was initiated by someone else, 0 if not.
69 * 2 if incoming_connection() has not returned it yet, 1 if it has.
70 */
71 uint8_t inbound;
72
73 uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */
74 uint16_t data_rate; /* current data packet send rate packets per second. */
75
76 uint64_t last_SYNC; /* time our last SYNC packet was sent. */
77 uint64_t last_sent; /* time our last data or handshake packet was sent. */
78 uint64_t last_recvSYNC; /* time we last received a SYNC packet from the other */
79 uint64_t last_recvdata; /* time we last received a DATA packet from the other */
80 uint64_t killat; /* time to kill the connection */
81
82 Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
83 Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
84
85 uint32_t handshake_id1;
86 uint32_t handshake_id2;
87
88 /* number of data packets received (also used as handshake_id1) */
89 uint32_t recv_packetnum;
90
91 /* number of packets received by the other peer */
92 uint32_t orecv_packetnum;
93
94 /* number of data packets sent */
95 uint32_t sent_packetnum;
96
97 /* number of packets sent by the other peer. */
98 uint32_t osent_packetnum;
99
100 /* number of latest packet written onto the sendbuffer */
101 uint32_t sendbuff_packetnum;
102
103 /* we know all packets before that number were successfully sent */
104 uint32_t successful_sent;
105
106 /* packet number of last packet read with the read_packet function */
107 uint32_t successful_read;
108
109 /* list of currently requested packet numbers(by the other person) */
110 uint32_t req_packets[BUFFER_PACKET_NUM];
111
112 /* total number of currently requested packets(by the other person) */
113 uint16_t num_req_paquets;
114
115 uint8_t recv_counter;
116 uint8_t send_counter;
117 uint8_t timeout; /* connection timeout in seconds. */
118} Connection;
119
120typedef struct {
121 Networking_Core *net;
122 Connection *connections;
123
124 uint32_t connections_length; /* Length of connections array */
125 uint32_t connections_number; /* Number of connections in connections array */
126
127 /* table of random numbers used in handshake_id. */
128 uint32_t randtable[6][256];
129
130} Lossless_UDP;
131
132/*
133 * Initialize a new connection to ip_port
134 * Returns an integer corresponding to the connection id.
135 * Return -1 if it could not initialize the connection.
136 * Return number if there already was an existing connection to that ip_port.
137 */
138int new_connection(Lossless_UDP *ludp, IP_Port ip_port);
139
140/*
141 * Get connection id from IP_Port.
142 * Return -1 if there are no connections like we are looking for.
143 * Return id if it found it .
144 */
145int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);
146
147/*
148 * Returns an int corresponding to the next connection in our imcoming connection list
149 * Return -1 if there are no new incoming connections in the list.
150 */
151int incoming_connection(Lossless_UDP *ludp);
152
153/*
154 * Return -1 if it could not kill the connection.
155 * Return 0 if killed successfully
156 */
157int kill_connection(Lossless_UDP *ludp, int connection_id);
158
159/*
160 * Kill connection in seconds seconds.
161 * Return -1 if it can not kill the connection.
162 * Return 0 if it will kill it
163 */
164int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);
165
166/*
167 * Returns the ip_port of the corresponding connection.
168 * Return 0 if there is no such connection.
169 */
170IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);
171
172/*
173 * Returns the id of the next packet in the queue
174 * Return -1 if no packet in queue
175 */
176char id_packet(Lossless_UDP *ludp, int connection_id);
177
178/*
179 * Return 0 if there is no received data in the buffer.
180 * Return length of received packet if successful
181 */
182int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);
183
184/*
185 * Return 0 if data could not be put in packet queue
186 * Return 1 if data was put into the queue
187 */
188int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);
189
190/* Returns the number of packets in the queue waiting to be successfully sent. */
191uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);
192
193/*
194 * returns the number of packets in the queue waiting to be successfully
195 * read with read_packet(...)
196 */
197uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
198
199/* Check if connection is connected:
200 * Return 0 no.
201 * Return 1 if attempting handshake.
202 * Return 2 if handshake is done.
203 * Return 3 if fully connected.
204 * Return 4 if timed out and wating to be killed.
205 */
206int is_connected(Lossless_UDP *ludp, int connection_id);
207
208/* Call this function a couple times per second It's the main loop. */
209void do_lossless_udp(Lossless_UDP *ludp);
210
211/*
212 * This function sets up LosslessUDP packet handling.
213 */
214Lossless_UDP *new_lossless_udp(Networking_Core *net);
215
216void kill_lossless_udp(Lossless_UDP *ludp);
217
218#ifdef __cplusplus
219}
220#endif
221
222#endif