diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-06 13:53:38 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-06 13:53:38 -0400 |
commit | fc5a2f53df06bfba185ad9868970e1fecfa0fc06 (patch) | |
tree | 549191f8bd33eabefabf6387ec1cf5aabfe49f59 /core/ping.c | |
parent | eacd12385fc775c3c246a1586047d6c2e0166977 (diff) | |
parent | 6e610749ebfc0bfe153ab88bcf76f4f9b24ff3fa (diff) |
Merge branch 'master' of https://github.com/plutooo/ProjectTox-Core into plutooo-master
Conflicts:
core/ping.c
core/util.c
Diffstat (limited to 'core/ping.c')
-rw-r--r-- | core/ping.c | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/core/ping.c b/core/ping.c index ffabe221..a687f2fb 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,10 +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; | ||
26 | 30 | ||
31 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c | ||
27 | 32 | ||
28 | void init_ping() | 33 | void init_ping() |
29 | { | 34 | { |
@@ -51,15 +56,16 @@ static void remove_timeouts() // O(n) | |||
51 | new_num--; | 56 | new_num--; |
52 | } | 57 | } |
53 | // Break here because list is sorted. | 58 | // Break here because list is sorted. |
54 | else | 59 | else { |
55 | break; | 60 | break; |
61 | } | ||
56 | } | 62 | } |
57 | 63 | ||
58 | num_pings = new_num; | 64 | num_pings = new_num; |
59 | pos_pings = new_pos % PING_NUM_MAX; | 65 | pos_pings = new_pos % PING_NUM_MAX; |
60 | } | 66 | } |
61 | 67 | ||
62 | uint64_t add_ping(IP_Port ipp) // O(n) | 68 | uint64_t add_ping(IP_Port ipp) // O(n) |
63 | { | 69 | { |
64 | size_t p; | 70 | size_t p; |
65 | 71 | ||
@@ -94,6 +100,7 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with | |||
94 | for (i=0; i<num_pings; i++) { | 100 | for (i=0; i<num_pings; i++) { |
95 | id = (pos_pings + i) % PING_NUM_MAX; | 101 | id = (pos_pings + i) % PING_NUM_MAX; |
96 | 102 | ||
103 | // ping_id = 0 means match any id | ||
97 | if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { | 104 | if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { |
98 | return true; | 105 | return true; |
99 | } | 106 | } |
@@ -101,3 +108,57 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with | |||
101 | 108 | ||
102 | return false; | 109 | return false; |
103 | } | 110 | } |
111 | |||
112 | int send_ping_request(IP_Port ipp, clientid_t* client_id) | ||
113 | { | ||
114 | pingreq_t pk; | ||
115 | int rc; | ||
116 | uint64_t ping_id; | ||
117 | |||
118 | if (is_pinging(ipp, 0) || id_eq(client_id, self_id)) | ||
119 | return 1; | ||
120 | |||
121 | // Generate random ping_id | ||
122 | ping_id = add_ping(ipp); | ||
123 | |||
124 | pk.magic = PACKET_PING_REQ; | ||
125 | id_cpy(&pk.client_id, self_id); // Our pubkey | ||
126 | random_nonce((uint8_t*) &pk.nonce); // Generate random nonce | ||
127 | |||
128 | // Encrypt ping_id using recipient privkey | ||
129 | rc = encrypt_data((uint8_t*) client_id, | ||
130 | self_secret_key, | ||
131 | (uint8_t*) &pk.nonce, | ||
132 | (uint8_t*) &ping_id, sizeof(ping_id), | ||
133 | (uint8_t*) &pk.ping_id); | ||
134 | |||
135 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
136 | return 1; | ||
137 | |||
138 | return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); | ||
139 | } | ||
140 | |||
141 | int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id) | ||
142 | { | ||
143 | pingres_t pk; | ||
144 | int rc; | ||
145 | |||
146 | if (id_eq(client_id, self_id)) | ||
147 | return 1; | ||
148 | |||
149 | pk.magic = PACKET_PING_RES; | ||
150 | id_cpy(&pk.client_id, self_id); // Our pubkey | ||
151 | random_nonce((uint8_t*) &pk.nonce); // Generate random nonce | ||
152 | |||
153 | // Encrypt ping_id using recipient privkey | ||
154 | rc = encrypt_data((uint8_t*) client_id, | ||
155 | self_secret_key, | ||
156 | (uint8_t*) &pk.nonce, | ||
157 | (uint8_t*) &ping_id, sizeof(ping_id), | ||
158 | (uint8_t*) &pk.ping_id); | ||
159 | |||
160 | if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
161 | return 1; | ||
162 | |||
163 | return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); | ||
164 | } | ||