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

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

#include "DHT.h"
#include "net_crypto.h"
#include "packets.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;

static pinged_t    pings[PING_NUM_MAX];
static size_t      num_pings;
static size_t      pos_pings;
static clientid_t* self_id = (clientid_t*) &self_public_key;

extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c


void init_ping()
{
    num_pings = 0;
    pos_pings = 0;
}

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

static void remove_timeouts() // O(n)
{
    size_t i, id;
    size_t new_pos = pos_pings;
    size_t new_num = num_pings;

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

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

  num_pings = new_num;
  pos_pings = new_pos % PING_NUM_MAX;
}

uint64_t add_ping(IP_Port ipp) // O(n)
{
    size_t p;

    remove_timeouts();

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

    // Insert new ping at end of list
    p = (pos_pings + num_pings) % PING_NUM_MAX;

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

    num_pings++;
    return pings[p].id;
}

bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n)
{
    size_t i, id;

    if (ipp.ip.i == 0 && ping_id == 0)
        return false;

    remove_timeouts();

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

        // ping_id = 0 means match any id
        if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
            return true;
        }
    }

    return false;
}

int send_ping_request(IP_Port ipp, clientid_t* client_id)
{
    pingreq_t pk;
    int       rc;
    uint64_t  ping_id;

    if (is_pinging(ipp, 0) || id_eq(client_id, self_id))
        return 1;

    // Generate random ping_id
    ping_id = add_ping(ipp);

    pk.magic = PACKET_PING_REQ;
    id_cpy(&pk.client_id, self_id);     // Our pubkey
    random_nonce((uint8_t*) &pk.nonce); // Generate random nonce

    // Encrypt ping_id using recipient privkey
    rc = encrypt_data((uint8_t*) client_id,
                      self_secret_key,
                      (uint8_t*) &pk.nonce,
                      (uint8_t*) &ping_id, sizeof(ping_id),
                      (uint8_t*) &pk.ping_id);

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

    return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
}

int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
{
    pingres_t pk;
    int       rc;

    if (id_eq(client_id, self_id))
        return 1;

    pk.magic = PACKET_PING_RES;
    id_cpy(&pk.client_id, self_id);     // Our pubkey
    random_nonce((uint8_t*) &pk.nonce); // Generate random nonce

    // Encrypt ping_id using recipient privkey
    rc = encrypt_data((uint8_t*) client_id,
                      self_secret_key,
                      (uint8_t*) &pk.nonce,
                      (uint8_t*) &ping_id, sizeof(ping_id),
                      (uint8_t*) &pk.ping_id);

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

    return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
}