diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 12 | ||||
-rw-r--r-- | core/LAN_discovery.c | 4 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 14 | ||||
-rw-r--r-- | core/net_crypto.c | 6 | ||||
-rw-r--r-- | core/network.h | 10 | ||||
-rw-r--r-- | core/packets.h | 10 | ||||
-rw-r--r-- | core/ping.c | 4 |
7 files changed, 32 insertions, 28 deletions
@@ -482,7 +482,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli | |||
482 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 482 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
483 | return -1; | 483 | return -1; |
484 | 484 | ||
485 | data[0] = 2; | 485 | data[0] = NET_PACKET_GET_NODES; |
486 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); | 486 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); |
487 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 487 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
488 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 488 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
@@ -524,7 +524,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
524 | if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) | 524 | if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) |
525 | return -1; | 525 | return -1; |
526 | 526 | ||
527 | data[0] = 3; | 527 | data[0] = NET_PACKET_SEND_NODES; |
528 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); | 528 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); |
529 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 529 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
530 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 530 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
@@ -1162,10 +1162,10 @@ DHT *new_DHT(Net_Crypto *c) | |||
1162 | } | 1162 | } |
1163 | 1163 | ||
1164 | temp->c = c; | 1164 | temp->c = c; |
1165 | networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); | 1165 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_REQUEST, &handle_ping_request, temp); |
1166 | networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); | 1166 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, temp); |
1167 | networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp); | 1167 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); |
1168 | networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp); | 1168 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); |
1169 | cryptopacket_registerhandler(c, 254, &handle_NATping, temp); | 1169 | cryptopacket_registerhandler(c, 254, &handle_NATping, temp); |
1170 | return temp; | 1170 | return temp; |
1171 | } | 1171 | } |
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index cbbb384b..49f52ce7 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c | |||
@@ -139,7 +139,7 @@ static int handle_LANdiscovery(void *object, IP_Port source, uint8_t *packet, ui | |||
139 | int send_LANdiscovery(uint16_t port, Net_Crypto *c) | 139 | int send_LANdiscovery(uint16_t port, Net_Crypto *c) |
140 | { | 140 | { |
141 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | 141 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; |
142 | data[0] = 33; | 142 | data[0] = NET_PACKET_LAN_DISCOVERY; |
143 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 143 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
144 | IP_Port ip_port = {broadcast_ip(), port}; | 144 | IP_Port ip_port = {broadcast_ip(), port}; |
145 | return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | 145 | return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); |
@@ -148,5 +148,5 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) | |||
148 | 148 | ||
149 | void LANdiscovery_init(DHT *dht) | 149 | void LANdiscovery_init(DHT *dht) |
150 | { | 150 | { |
151 | networking_registerhandler(dht->c->lossless_udp->net, 33, &handle_LANdiscovery, dht); | 151 | networking_registerhandler(dht->c->lossless_udp->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); |
152 | } | 152 | } |
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 15e8dea3..c30eb903 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -409,7 +409,7 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak | |||
409 | uint8_t packet[1 + 4 + 4]; | 409 | uint8_t packet[1 + 4 + 4]; |
410 | uint32_t temp; | 410 | uint32_t temp; |
411 | 411 | ||
412 | packet[0] = 16; | 412 | packet[0] = NET_PACKET_HANDSHAKE; |
413 | temp = htonl(handshake_id1); | 413 | temp = htonl(handshake_id1); |
414 | memcpy(packet + 1, &temp, 4); | 414 | memcpy(packet + 1, &temp, 4); |
415 | temp = htonl(handshake_id2); | 415 | temp = htonl(handshake_id2); |
@@ -431,7 +431,7 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) | |||
431 | uint32_t requested[BUFFER_PACKET_NUM]; | 431 | uint32_t requested[BUFFER_PACKET_NUM]; |
432 | uint32_t number = missing_packets(ludp, connection_id, requested); | 432 | uint32_t number = missing_packets(ludp, connection_id, requested); |
433 | 433 | ||
434 | packet[0] = 17; | 434 | packet[0] = NET_PACKET_SYNC; |
435 | index += 1; | 435 | index += 1; |
436 | memcpy(packet + index, &counter, 1); | 436 | memcpy(packet + index, &counter, 1); |
437 | index += 1; | 437 | index += 1; |
@@ -450,7 +450,7 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t | |||
450 | uint32_t index = packet_num % MAX_QUEUE_NUM; | 450 | uint32_t index = packet_num % MAX_QUEUE_NUM; |
451 | uint32_t temp; | 451 | uint32_t temp; |
452 | uint8_t packet[1 + 4 + MAX_DATA_SIZE]; | 452 | uint8_t packet[1 + 4 + MAX_DATA_SIZE]; |
453 | packet[0] = 18; | 453 | packet[0] = NET_PACKET_DATA; |
454 | temp = htonl(packet_num); | 454 | temp = htonl(packet_num); |
455 | memcpy(packet + 1, &temp, 4); | 455 | memcpy(packet + 1, &temp, 4); |
456 | memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data, | 456 | memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data, |
@@ -733,9 +733,9 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net) | |||
733 | return NULL; | 733 | return NULL; |
734 | 734 | ||
735 | temp->net = net; | 735 | temp->net = net; |
736 | networking_registerhandler(net, 16, &handle_handshake, temp); | 736 | networking_registerhandler(net, NET_PACKET_HANDSHAKE, &handle_handshake, temp); |
737 | networking_registerhandler(net, 17, &handle_SYNC, temp); | 737 | networking_registerhandler(net, NET_PACKET_SYNC, &handle_SYNC, temp); |
738 | networking_registerhandler(net, 18, &handle_data, temp); | 738 | networking_registerhandler(net, NET_PACKET_DATA, &handle_data, temp); |
739 | return temp; | 739 | return temp; |
740 | } | 740 | } |
741 | 741 | ||
@@ -839,4 +839,4 @@ void kill_lossless_udp(Lossless_UDP *ludp) | |||
839 | { | 839 | { |
840 | free(ludp->connections); | 840 | free(ludp->connections); |
841 | free(ludp); | 841 | free(ludp); |
842 | } \ No newline at end of file | 842 | } |
diff --git a/core/net_crypto.c b/core/net_crypto.c index e3757ffb..f421c37d 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -229,7 +229,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
229 | if (len == -1) | 229 | if (len == -1) |
230 | return -1; | 230 | return -1; |
231 | 231 | ||
232 | packet[0] = 32; | 232 | packet[0] = NET_PACKET_CRYPTO; |
233 | memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES); | 233 | memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES); |
234 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES); | 234 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES); |
235 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); | 235 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); |
@@ -277,7 +277,7 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui | |||
277 | { | 277 | { |
278 | DHT *dht = object; | 278 | DHT *dht = object; |
279 | 279 | ||
280 | if (packet[0] == 32) { | 280 | if (packet[0] == NET_PACKET_CRYPTO) { |
281 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || | 281 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || |
282 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 282 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
283 | return 1; | 283 | return 1; |
@@ -730,7 +730,7 @@ Net_Crypto *new_net_crypto(Networking_Core *net) | |||
730 | void init_cryptopackets(void *dht) | 730 | void init_cryptopackets(void *dht) |
731 | { | 731 | { |
732 | DHT *s_dht = dht; | 732 | DHT *s_dht = dht; |
733 | networking_registerhandler(s_dht->c->lossless_udp->net, 32, &cryptopacket_handle, s_dht); | 733 | networking_registerhandler(s_dht->c->lossless_udp->net, NET_PACKET_CRYPTO, &cryptopacket_handle, s_dht); |
734 | } | 734 | } |
735 | 735 | ||
736 | static void kill_timedout(Net_Crypto *c) | 736 | static void kill_timedout(Net_Crypto *c) |
diff --git a/core/network.h b/core/network.h index 87f45978..088bbb3b 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -68,6 +68,16 @@ extern "C" { | |||
68 | 68 | ||
69 | #define MAX_UDP_PACKET_SIZE 65507 | 69 | #define MAX_UDP_PACKET_SIZE 65507 |
70 | 70 | ||
71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */ | ||
72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID */ | ||
73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID */ | ||
74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */ | ||
75 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */ | ||
76 | #define NET_PACKET_SYNC 17 /* SYNC packet ID */ | ||
77 | #define NET_PACKET_DATA 18 /* Data packet ID */ | ||
78 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */ | ||
79 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */ | ||
80 | |||
71 | 81 | ||
72 | /* Current time, unix format */ | 82 | /* Current time, unix format */ |
73 | #define unix_time() ((uint64_t)time(NULL)) | 83 | #define unix_time() ((uint64_t)time(NULL)) |
diff --git a/core/packets.h b/core/packets.h index 222b1425..4f410fb3 100644 --- a/core/packets.h +++ b/core/packets.h | |||
@@ -10,15 +10,9 @@ typedef struct { | |||
10 | 10 | ||
11 | } __attribute__((packed)) clientid_t; | 11 | } __attribute__((packed)) clientid_t; |
12 | 12 | ||
13 | typedef enum { | ||
14 | PACKET_PING_REQ = 0, | ||
15 | PACKET_PING_RES = 1 | ||
16 | |||
17 | } packetid_t; | ||
18 | |||
19 | // Ping packet | 13 | // Ping packet |
20 | typedef struct { | 14 | typedef struct { |
21 | uint8_t magic; | 15 | uint8_t packet_id; |
22 | clientid_t client_id; | 16 | clientid_t client_id; |
23 | uint8_t nonce[crypto_box_NONCEBYTES]; | 17 | uint8_t nonce[crypto_box_NONCEBYTES]; |
24 | uint64_t ping_id; | 18 | uint64_t ping_id; |
@@ -28,7 +22,7 @@ typedef struct { | |||
28 | 22 | ||
29 | // Pong packet | 23 | // Pong packet |
30 | typedef struct { | 24 | typedef struct { |
31 | uint8_t magic; | 25 | uint8_t packet_id; |
32 | clientid_t client_id; | 26 | clientid_t client_id; |
33 | uint8_t nonce[crypto_box_NONCEBYTES]; | 27 | uint8_t nonce[crypto_box_NONCEBYTES]; |
34 | uint64_t ping_id; | 28 | uint64_t ping_id; |
diff --git a/core/ping.c b/core/ping.c index 4bb9c38a..55d4d261 100644 --- a/core/ping.c +++ b/core/ping.c | |||
@@ -128,7 +128,7 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client | |||
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.magic = PACKET_PING_REQ; | 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 | ||
@@ -153,7 +153,7 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64 | |||
153 | if (id_eq(client_id, (clientid_t *)c->self_public_key)) | 153 | if (id_eq(client_id, (clientid_t *)c->self_public_key)) |
154 | return 1; | 154 | return 1; |
155 | 155 | ||
156 | pk.magic = PACKET_PING_RES; | 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 | ||