summaryrefslogtreecommitdiff
path: root/toxcore/Lossless_UDP.h
blob: 01ce05f982b9fef92c62278eb7680ee34f48617a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Lossless_UDP.h
 *
 * An implementation of the Lossless_UDP protocol as seen in http://wiki.tox.im/index.php/Lossless_UDP
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef LOSSLESS_UDP_H
#define LOSSLESS_UDP_H

#include "network.h"
#include "misc_tools.h"


/* Maximum length of the data in the data packets. */
#define MAX_DATA_SIZE 1024

/* Maximum data packets in sent and receive queues. */
#define MAX_QUEUE_NUM     1024
#define DEFAULT_QUEUE_NUM     4

/* Maximum number of data packets in the buffer. */
#define MAX_REQUESTED_PACKETS 256

/* Timeout per connection is randomly set between CONNECTION_TIMEOUT and 2*CONNECTION_TIMEOUT. */
#define CONNECTION_TIMEOUT 5

/* Initial amount of sync/handshake packets to send per second. */
#define SYNC_RATE         2

/* Initial send rate of data. */
#define DATA_SYNC_RATE    30

typedef struct {
    uint8_t  data[MAX_DATA_SIZE];
    uint16_t size;
} Data;

#define LUDP_NO_CONNECTION 0
#define LUDP_HANDSHAKE_SENDING 1
#define LUDP_NOT_CONFIRMED 2
#define LUDP_ESTABLISHED 3
#define LUDP_TIMED_OUT 4

typedef struct {
    IP_Port ip_port;

    /*
     *  return LUDP_NO_CONNECTION if connection is dead.
     *  return LUDP_HANDSHAKE_SENDING if attempting handshake.
     *  return LUDP_NOT_CONFIRMED if handshake is done (we start sending SYNC packets).
     *  return LUDP_ESTABLISHED if we are sending SYNC packets and can send data.
     *  return LUDP_TIMED_OUT if the connection has timed out.
     */
    uint8_t status;

    /*
     *  return 0 if connection was not initiated by someone else.
     *  return 1 if incoming_connection() has returned.
     *  return 2 if it has not.
     */
    uint8_t inbound;

    uint16_t  SYNC_rate;     /* Current SYNC packet send rate packets per second. */
    uint32_t  data_rate;     /* Current data packet send rate packets per second. */

    uint64_t  last_SYNC;     /* Time our last SYNC packet was sent. */
    uint64_t  last_sent;     /* Time our last data or handshake packet was sent. */
    uint64_t  last_recvSYNC; /* Time we last received a SYNC packet from the other. */
    uint64_t  last_recvdata; /* Time we last received a DATA packet from the other. */
    uint64_t  killat;        /* Time to kill the connection. */

    Data      *sendbuffer; /* packet send buffer. */
    uint32_t  sendbuffer_length;
    Data      *recvbuffer; /* packet receive buffer. */
    uint32_t  recvbuffer_length;
    uint32_t  handshake_id1;
    uint32_t  handshake_id2;

    /* Number of data packets received (also used as handshake_id1). */
    uint32_t  recv_packetnum;

    /* Number of packets received by the other peer. */
    uint32_t  orecv_packetnum;

    /* Number of data packets sent. */
    uint32_t  sent_packetnum;

    /* Number of packets sent by the other peer. */
    uint32_t  osent_packetnum;

    /* Number of latest packet written onto the sendbuffer. */
    uint32_t  sendbuff_packetnum;

    /* We know all packets before that number were successfully sent. */
    uint32_t  successful_sent;

    /* Packet number of last packet read with the read_packet function. */
    uint32_t  successful_read;

    /* List of currently requested packet numbers(by the other person). */
    uint32_t  req_packets[MAX_REQUESTED_PACKETS];

    /* Total number of currently requested packets(by the other person). */
    uint16_t  num_req_paquets;

    uint8_t   recv_counter;
    uint8_t   send_counter;
    uint8_t   timeout; /* connection timeout in seconds. */

    /* Is the connection confirmed or not? 1 if yes, 0 if no */
    uint8_t   confirmed;
} Connection;

typedef struct {
    Networking_Core *net;

    tox_array connections;

    /* Table of random numbers used in handshake_id. */
    /* IPv6 (16) + port (2)*/
    uint32_t randtable[18][256];
} Lossless_UDP;

/*
 * Initialize a new connection to ip_port.
 *
 *  return an integer corresponding to the connection id.
 *  return -1 if it could not initialize the connection.
 *  return number if there already was an existing connection to that ip_port.
 */
int new_connection(Lossless_UDP *ludp, IP_Port ip_port);

/*
 * Get connection id from IP_Port.
 *
 *  return -1 if there are no connections like we are looking for.
 *  return id if it found it .
 */
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port);

/*
 *  return an integer corresponding to the next connection in our incoming connection list with at least numpackets in the receive queue.
 *  return -1 if there are no new incoming connections in the list.
 */
int incoming_connection(Lossless_UDP *ludp, uint32_t numpackets);

/*  return -1 if it could not kill the connection.
 *  return 0 if killed successfully.
 */
int kill_connection(Lossless_UDP *ludp, int connection_id);

/*
 * timeout connection in seconds seconds.
 *
 *  return -1 if it can not kill the connection.
 *  return 0 if it will kill it.
 */
int timeout_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds);


/* Check if connection is confirmed.
 *
 *  returns 1 if yes.
 *  returns 0 if no.
 */
int connection_confirmed(Lossless_UDP *ludp, int connection_id);

/* Confirm an incoming connection.
 * Also disables the auto kill timeout on incomming connections.
 *
 *  return 0 on success
 *  return -1 on failure.
 */
int confirm_connection(Lossless_UDP *ludp, int connection_id);

/*  returns the ip_port of the corresponding connection.
 *  return 0 if there is no such connection.
 */
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id);

/*  returns the id of the next packet in the queue.
 *  return -1 if no packet in queue.
 */
uint8_t id_packet(Lossless_UDP *ludp, int connection_id);

/*  return 0 if there is no received data in the buffer.
 *  return length of received packet if successful.
 */
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);

/* Like read_packet() but does leaves the queue as is.
 *  return 0 if there is no received data in the buffer.
 *  return length of received packet if successful.
 */
int read_packet_silent(Lossless_UDP *ludp, int connection_id, uint8_t *data);

/* Discard the next packet to be read from the queue
 *  return 0 if success.
 *  return -1 if failure.
 */
int discard_packet(Lossless_UDP *ludp, int connection_id);

/* returns the number of packet slots left in the sendbuffer.
 * return 0 if failure.
 */
uint32_t num_free_sendqueue_slots(Lossless_UDP *ludp, int connection_id);

/*  return 0 if data could not be put in packet queue.
 *  return 1 if data was put into the queue.
 */
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length);

/*  return number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id);

/*  return number of packets in all queues waiting to be successfully sent. */
uint32_t sendqueue_total(Lossless_UDP *ludp);

/*
 *  return number of packets in the queue waiting to be successfully
 *  read with read_packet(...).
 */
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);

/* Check if connection is connected:
 *
 *  return LUDP_NO_CONNECTION if not.
 *  return LUDP_HANDSHAKE_SENDING if attempting handshake.
 *  return LUDP_NOT_CONFIRMED if handshake is done.
 *  return LUDP_ESTABLISHED if fully connected.
 *  return LUDP_TIMED_OUT if timed out and wating to be killed.
 */
int is_connected(Lossless_UDP *ludp, int connection_id);

/* Call this function a couple times per second. It is the main loop. */
void do_lossless_udp(Lossless_UDP *ludp);

/* This function sets up LosslessUDP packet handling. */
Lossless_UDP *new_lossless_udp(Networking_Core *net);

void kill_lossless_udp(Lossless_UDP *ludp);


#endif