summaryrefslogtreecommitdiff
path: root/toxcore/ping.c
diff options
context:
space:
mode:
authorpete <petewicken@gmail.com>2013-08-29 22:17:51 +0100
committerpete <petewicken@gmail.com>2013-08-29 22:17:51 +0100
commit82b8927af7f68bbfbf83bbb5ffbc747de7bc288f (patch)
treeba83cda597e8146c0d02128fb8424bec9201d730 /toxcore/ping.c
parent792709e4e091ea587a642c548889ddbb866e9b5e (diff)
Correct a lot of the grammar and spelling. Also spent a few hours fixing the comments so they follow a standard.
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r--toxcore/ping.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 55d4d261..1c2de777 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -51,7 +51,7 @@ static void remove_timeouts(void *ping) // O(n)
51 size_t new_pos = png->pos_pings; 51 size_t new_pos = png->pos_pings;
52 size_t new_num = png->num_pings; 52 size_t new_num = png->num_pings;
53 53
54 // Loop through buffer, oldest first 54 // Loop through buffer, oldest first.
55 for (i = 0; i < png->num_pings; i++) { 55 for (i = 0; i < png->num_pings; i++) {
56 id = (png->pos_pings + i) % PING_NUM_MAX; 56 id = (png->pos_pings + i) % PING_NUM_MAX;
57 57
@@ -76,13 +76,13 @@ uint64_t add_ping(void *ping, IP_Port ipp) // O(n)
76 76
77 remove_timeouts(ping); 77 remove_timeouts(ping);
78 78
79 // Remove oldest ping if full buffer 79 /* Remove oldest ping if full buffer. */
80 if (png->num_pings == PING_NUM_MAX) { 80 if (png->num_pings == PING_NUM_MAX) {
81 png->num_pings--; 81 png->num_pings--;
82 png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX; 82 png->pos_pings = (png->pos_pings + 1) % PING_NUM_MAX;
83 } 83 }
84 84
85 // Insert new ping at end of list 85 /* Insert new ping at end of list. */
86 p = (png->pos_pings + png->num_pings) % PING_NUM_MAX; 86 p = (png->pos_pings + png->num_pings) % PING_NUM_MAX;
87 87
88 png->pings[p].ipp = ipp; 88 png->pings[p].ipp = ipp;
@@ -93,7 +93,7 @@ uint64_t add_ping(void *ping, IP_Port ipp) // O(n)
93 return png->pings[p].id; 93 return png->pings[p].id;
94} 94}
95 95
96bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. 96bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Replace this with something else.
97{ 97{
98 PING *png = ping; 98 PING *png = ping;
99 99
@@ -107,7 +107,7 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: repl
107 for (i = 0; i < png->num_pings; i++) { 107 for (i = 0; i < png->num_pings; i++) {
108 id = (png->pos_pings + i) % PING_NUM_MAX; 108 id = (png->pos_pings + i) % PING_NUM_MAX;
109 109
110 // ping_id = 0 means match any id 110 /* ping_id = 0 means match any id. */
111 if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) { 111 if ((ipp_eq(png->pings[id].ipp, ipp) || ipp.ip.i == 0) && (png->pings[id].id == ping_id || ping_id == 0)) {
112 return true; 112 return true;
113 } 113 }
@@ -125,14 +125,14 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client
125 if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) 125 if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key))
126 return 1; 126 return 1;
127 127
128 // Generate random ping_id 128 // Generate random ping_id.
129 ping_id = add_ping(ping, ipp); 129 ping_id = add_ping(ping, ipp);
130 130
131 pk.packet_id = NET_PACKET_PING_REQUEST; 131 pk.packet_id = NET_PACKET_PING_REQUEST;
132 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey 132 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey.
133 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce 133 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce.
134 134
135 // Encrypt ping_id using recipient privkey 135 /* Encrypt ping_id using recipient privkey. */
136 rc = encrypt_data((uint8_t *) client_id, 136 rc = encrypt_data((uint8_t *) client_id,
137 c->self_secret_key, 137 c->self_secret_key,
138 (uint8_t *) &pk.nonce, 138 (uint8_t *) &pk.nonce,
@@ -154,10 +154,10 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64
154 return 1; 154 return 1;
155 155
156 pk.packet_id = NET_PACKET_PING_RESPONSE; 156 pk.packet_id = NET_PACKET_PING_RESPONSE;
157 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey 157 id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey.
158 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce 158 random_nonce((uint8_t *) &pk.nonce); // Generate random nonce.
159 159
160 // Encrypt ping_id using recipient privkey 160 /* Encrypt ping_id using recipient privkey */
161 rc = encrypt_data((uint8_t *) client_id, 161 rc = encrypt_data((uint8_t *) client_id,
162 c->self_secret_key, 162 c->self_secret_key,
163 (uint8_t *) &pk.nonce, 163 (uint8_t *) &pk.nonce,
@@ -180,7 +180,7 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t
180 if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) 180 if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key))
181 return 1; 181 return 1;
182 182
183 // Decrypt ping_id 183 /* Decrypt ping_id. */
184 rc = decrypt_data((uint8_t *) &p->client_id, 184 rc = decrypt_data((uint8_t *) &p->client_id,
185 dht->c->self_secret_key, 185 dht->c->self_secret_key,
186 (uint8_t *) &p->nonce, 186 (uint8_t *) &p->nonce,
@@ -191,7 +191,7 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t
191 if (rc != sizeof(ping_id)) 191 if (rc != sizeof(ping_id))
192 return 1; 192 return 1;
193 193
194 // Send response 194 /* Send response. */
195 send_ping_response(dht->c, source, &p->client_id, ping_id); 195 send_ping_response(dht->c, source, &p->client_id, ping_id);
196 add_toping(dht, (uint8_t *) &p->client_id, source); 196 add_toping(dht, (uint8_t *) &p->client_id, source);
197 197
@@ -208,7 +208,7 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t
208 if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) 208 if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key))
209 return 1; 209 return 1;
210 210
211 // Decrypt ping_id 211 /* Decrypt ping_id. */
212 rc = decrypt_data((uint8_t *) &p->client_id, 212 rc = decrypt_data((uint8_t *) &p->client_id,
213 dht->c->self_secret_key, 213 dht->c->self_secret_key,
214 (uint8_t *) &p->nonce, 214 (uint8_t *) &p->nonce,
@@ -219,11 +219,11 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t
219 if (rc != sizeof(ping_id)) 219 if (rc != sizeof(ping_id))
220 return 1; 220 return 1;
221 221
222 // Make sure ping_id is correct 222 /* Make sure ping_id is correct. */
223 if (!is_pinging(dht->ping, source, ping_id)) 223 if (!is_pinging(dht->ping, source, ping_id))
224 return 1; 224 return 1;
225 225
226 // Associate source ip with client_id 226 /* Associate source ip with client_id. */
227 addto_lists(dht, source, (uint8_t *) &p->client_id); 227 addto_lists(dht, source, (uint8_t *) &p->client_id);
228 return 0; 228 return 0;
229} 229}