summaryrefslogtreecommitdiff
path: root/toxcore/ping.c
blob: 49e0dba9e4c853a2e550ee20b6cb8db6b1f6133e (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
/*
 * ping.c -- Buffered pinging using cyclic arrays.
 *
 * This file is donated to the Tox Project.
 * Copyright 2013  plutooo
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdbool.h>
#include <stdint.h>

#include "DHT.h"
#include "net_crypto.h"
#include "network.h"
#include "util.h"

#define PING_NUM_MAX 256
#define PING_TIMEOUT 5 // 5s

typedef struct {
    IP_Port  ipp;
    uint64_t id;
    uint64_t timestamp;
} pinged_t;

typedef struct {
    pinged_t    pings[PING_NUM_MAX];
    size_t      num_pings;
    size_t      pos_pings;
} PING;

void *new_ping(void)
{
    return calloc(1, sizeof(PING));
}

void kill_ping(void *ping)
{
    free(ping);
}

static bool is_timeout(uint64_t time)
{
    return (time + PING_TIMEOUT) < now();
}

static void remove_timeouts(void *ping)    // O(n)
{
    PING *png = ping;
    size_t i, id;
    size_t new_pos = png->pos_pings;
    size_t new_num = png->num_pings;

    // Loop through buffer, oldest first.
    for (i = 0; i < png->num_pings; i++) {
        id = (png->pos_pings + i) % PING_NUM_MAX;

        if (is_timeout(png->pings[id].timestamp)) {
            new_pos++;
            new_num--;
        }
        // Break here because list is sorted.
        else {
            break;
        }
    }

    png->num_pings = new_num;
    png->pos_pings = new_pos % PING_NUM_MAX;
}

uint64_t add_ping(void *ping, IP_Port ipp)  // O(n)
{
    PING *png = ping;
    size_t p;

    remove_timeouts(ping);

    /* Remove oldest ping if full buffer. */
    if (png->num_pings == PING_NUM_MAX) {
        png->num_pings--;
        png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX;
    }

    /* Insert new ping at end of list. */
    p = (png->pos_pings + png->num_pings) % PING_NUM_MAX;

    png->pings[p].ipp       = ipp;
    png->pings[p].timestamp = now();
    png->pings[p].id        = random_64b();

    png->num_pings++;
    return png->pings[p].id;
}

bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id)    // O(n) TODO: Replace this with something else.
{
    PING *png = ping;

    /* shouldn't that be an OR ? */
    if (!ip_isset(&ipp.ip) && ping_id == 0)
        return false;

    size_t i, id;

    remove_timeouts(ping);

    for (i = 0; i < png->num_pings; i++) {
        id = (png->pos_pings + i) % PING_NUM_MAX;

        /* ping_id = 0 means match any id. */
        if ((!ip_isset(&ipp.ip) || ipport_equal(&png->pings[id].ipp, &ipp)) &&
                            (png->pings[id].id == ping_id || ping_id == 0)) {
            return true;
        }
    }

    return false;
}

#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING)

int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id)
{
    uint8_t   pk[DHT_PING_SIZE];
    int       rc;
    uint64_t  ping_id;

    if (is_pinging(ping, ipp, 0) || id_eq(client_id, c->self_public_key))
        return 1;

    // Generate random ping_id.
    ping_id = add_ping(ping, ipp);

    pk[0] = NET_PACKET_PING_REQUEST;
    id_cpy(pk + 1, c->self_public_key);     // Our pubkey
    random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce

    // Encrypt ping_id using recipient privkey
    rc = encrypt_data(client_id,
                      c->self_secret_key,
                      pk + 1 + CLIENT_ID_SIZE,
                      (uint8_t *) &ping_id, sizeof(ping_id),
                      pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);

    if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
        return 1;

    return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk));
}

int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id)
{
    uint8_t   pk[DHT_PING_SIZE];
    int       rc;

    if (id_eq(client_id, c->self_public_key))
        return 1;

    pk[0] = NET_PACKET_PING_RESPONSE;
    id_cpy(pk + 1, c->self_public_key);     // Our pubkey
    random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce

    // Encrypt ping_id using recipient privkey
    rc = encrypt_data(client_id,
                      c->self_secret_key,
                      pk + 1 + CLIENT_ID_SIZE,
                      (uint8_t *) &ping_id, sizeof(ping_id),
                      pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);

    if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
        return 1;

    return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk));
}

int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;
    int        rc;
    uint64_t   ping_id;

    if (length != DHT_PING_SIZE)
        return 1;

    if (id_eq(packet + 1, dht->c->self_public_key))
        return 1;

    // Decrypt ping_id
    rc = decrypt_data(packet + 1,
                      dht->c->self_secret_key,
                      packet + 1 + CLIENT_ID_SIZE,
                      packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
                      sizeof(ping_id) + ENCRYPTION_PADDING,
                      (uint8_t *) &ping_id);

    if (rc != sizeof(ping_id))
        return 1;

    // Send response
    send_ping_response(dht->c, source, packet + 1, ping_id);
    add_toping(dht, packet + 1, source);

    return 0;
}

int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
    DHT *dht = object;
    int       rc;
    uint64_t  ping_id;

    if (length != DHT_PING_SIZE)
        return 1;

    if (id_eq(packet + 1, dht->c->self_public_key))
        return 1;

    // Decrypt ping_id
    rc = decrypt_data(packet + 1,
                      dht->c->self_secret_key,
                      packet + 1 + CLIENT_ID_SIZE,
                      packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
                      sizeof(ping_id) + ENCRYPTION_PADDING,
                      (uint8_t *) &ping_id);

    if (rc != sizeof(ping_id))
        return 1;

    /* Make sure ping_id is correct. */
    if (!is_pinging(dht->ping, source, ping_id))
        return 1;

    // Associate source ip with client_id
    addto_lists(dht, source, packet + 1);
    return 0;
}