diff options
Diffstat (limited to 'core/ping.c')
-rw-r--r-- | core/ping.c | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/core/ping.c b/core/ping.c index 8a7d534f..646b0839 100644 --- a/core/ping.c +++ b/core/ping.c | |||
@@ -8,6 +8,9 @@ | |||
8 | #include <stdbool.h> | 8 | #include <stdbool.h> |
9 | #include <stdint.h> | 9 | #include <stdint.h> |
10 | 10 | ||
11 | #include "DHT.h" | ||
12 | #include "net_crypto.h" | ||
13 | #include "packets.h" | ||
11 | #include "network.h" | 14 | #include "network.h" |
12 | #include "util.h" | 15 | #include "util.h" |
13 | 16 | ||
@@ -20,9 +23,12 @@ typedef struct { | |||
20 | uint64_t timestamp; | 23 | uint64_t timestamp; |
21 | } pinged_t; | 24 | } pinged_t; |
22 | 25 | ||
23 | static pinged_t pings[PING_NUM_MAX]; | 26 | static pinged_t pings[PING_NUM_MAX]; |
24 | static size_t num_pings; | 27 | static size_t num_pings; |
25 | static size_t pos_pings; | 28 | static size_t pos_pings; |
29 | static clientid_t* self_id = (clientid_t*) &self_public_key; | ||
30 | |||
31 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c | ||
26 | 32 | ||
27 | 33 | ||
28 | void init_ping() { | 34 | void init_ping() { |
@@ -86,10 +92,63 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n) | |||
86 | for(i=0; i<num_pings; i++) { | 92 | for(i=0; i<num_pings; i++) { |
87 | id = (pos_pings + i) % PING_NUM_MAX; | 93 | id = (pos_pings + i) % PING_NUM_MAX; |
88 | 94 | ||
89 | if(ipp_eq(pings[id].ipp, ipp) && pings[id].id == ping_id) { | 95 | // ping_id = 0 means match any id |
96 | if(ipp_eq(pings[id].ipp, ipp) && (ping_id == 0 || pings[id].id == ping_id)) { | ||
90 | return true; | 97 | return true; |
91 | } | 98 | } |
92 | } | 99 | } |
93 | 100 | ||
94 | return false; | 101 | return false; |
95 | } | 102 | } |
103 | |||
104 | int send_ping_request(IP_Port ipp, clientid_t* client_id) { | ||
105 | pingreq_t pk; | ||
106 | int rc; | ||
107 | uint64_t ping_id; | ||
108 | |||
109 | if(is_pinging(ipp, 0) || id_eq(client_id, self_id)) | ||
110 | return 1; | ||
111 | |||
112 | // Generate random ping_id | ||
113 | ping_id = add_ping(ipp); | ||
114 | |||
115 | pk.magic = PACKET_PING_REQ; | ||
116 | id_cpy(&pk.client_id, self_id); // Our pubkey | ||
117 | random_nonce((uint8_t*) &pk.nonce); // Generate random nonce | ||
118 | |||
119 | // Encrypt ping_id using recipient privkey | ||
120 | rc = encrypt_data((uint8_t*) client_id, | ||
121 | self_secret_key, | ||
122 | (uint8_t*) &pk.nonce, | ||
123 | (uint8_t*) &ping_id, sizeof(ping_id), | ||
124 | (uint8_t*) &pk.ping_id); | ||
125 | |||
126 | if(rc != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
127 | return 1; | ||
128 | |||
129 | return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); | ||
130 | } | ||
131 | |||
132 | int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id) { | ||
133 | pingres_t pk; | ||
134 | int rc; | ||
135 | |||
136 | if(id_eq(client_id, self_id)) | ||
137 | return 1; | ||
138 | |||
139 | pk.magic = PACKET_PING_RES; | ||
140 | id_cpy(&pk.client_id, self_id); // Our pubkey | ||
141 | random_nonce((uint8_t*) &pk.nonce); // Generate random nonce | ||
142 | |||
143 | // Encrypt ping_id using recipient privkey | ||
144 | rc = encrypt_data((uint8_t*) client_id, | ||
145 | self_secret_key, | ||
146 | (uint8_t*) &pk.nonce, | ||
147 | (uint8_t*) &ping_id, sizeof(ping_id), | ||
148 | (uint8_t*) &pk.ping_id); | ||
149 | |||
150 | if(rc != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
151 | return 1; | ||
152 | |||
153 | return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); | ||
154 | } | ||