summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-07-15 23:31:35 -0400
committerirungentoo <irungentoo@gmail.com>2015-07-15 23:31:35 -0400
commit17af629d1f7fd7f35f8f54bc9b24444439950886 (patch)
tree66111d1b3a56f20e6f1b7af824a529ca9206e9f7
parentf15cb89a956b78cde16bae5e411ded69cee5059a (diff)
Added function to help calculate size of packet nodes.
-rw-r--r--toxcore/DHT.c21
-rw-r--r--toxcore/DHT.h5
2 files changed, 25 insertions, 1 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index eab67c4f..ecc13063 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -180,6 +180,25 @@ int to_host_family(IP *ip)
180#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES) 180#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES)
181#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES) 181#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES)
182 182
183/* Return packet size of packed node with ip_family on success.
184 * Return -1 on failure.
185 */
186int packed_node_size(uint8_t ip_family)
187{
188 if (ip_family == AF_INET) {
189 return PACKED_NODE_SIZE_IP4;
190 } else if (ip_family == TCP_INET) {
191 return PACKED_NODE_SIZE_IP4;
192 } else if (ip_family == AF_INET6) {
193 return PACKED_NODE_SIZE_IP6;
194 } else if (ip_family == TCP_INET6) {
195 return PACKED_NODE_SIZE_IP6;
196 } else {
197 return -1;
198 }
199}
200
201
183/* Pack number of nodes into data of maxlength length. 202/* Pack number of nodes into data of maxlength length.
184 * 203 *
185 * return length of packed nodes on success. 204 * return length of packed nodes on success.
@@ -2292,7 +2311,7 @@ uint32_t DHT_size(const DHT *dht)
2292 2311
2293 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 2312 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
2294 2313
2295 return size32 + sizesubhead + (PACKED_NODE_SIZE_IP4 * numv4) + (PACKED_NODE_SIZE_IP6 * numv6); 2314 return size32 + sizesubhead + (packed_node_size(AF_INET) * numv4) + (packed_node_size(AF_INET6) * numv6);
2296} 2315}
2297 2316
2298static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 2317static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 7ea64655..eba6e1f6 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -153,6 +153,11 @@ typedef struct {
153} 153}
154Node_format; 154Node_format;
155 155
156/* Return packet size of packed node with ip_family on success.
157 * Return -1 on failure.
158 */
159int packed_node_size(uint8_t ip_family);
160
156/* Pack number of nodes into data of maxlength length. 161/* Pack number of nodes into data of maxlength length.
157 * 162 *
158 * return length of packed nodes on success. 163 * return length of packed nodes on success.