diff --git a/core/DHT.c b/core/DHT.c index ab631604..60e4704f 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -53,6 +53,9 @@ /*Ping newly announced nodes to ping per TIME_TOPING seconds*/ #define TIME_TOPING 5 +#define NAT_PING_REQUEST 0 +#define NAT_PING_RESPONSE 1 + Client_data *DHT_get_close_list(DHT *dht) { @@ -482,7 +485,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) return -1; - data[0] = 2; + data[0] = NET_PACKET_GET_NODES; memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); @@ -524,7 +527,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) return -1; - data[0] = 3; + data[0] = NET_PACKET_SEND_NODES; memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); @@ -933,7 +936,7 @@ static int send_NATping(DHT *dht, uint8_t *public_key, uint64_t ping_id, uint8_t memcpy(data + 1, &ping_id, sizeof(uint64_t)); /* 254 is NAT ping request packet id */ int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, data, - sizeof(uint64_t) + 1, 254); + sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); if (len == -1) return -1; @@ -963,12 +966,12 @@ static int handle_NATping(void *object, IP_Port source, uint8_t *source_pubkey, DHT_Friend *friend = &dht->friends_list[friendnumber]; - if (packet[0] == 0) { + if (packet[0] == NAT_PING_REQUEST) { /* 1 is reply */ - send_NATping(dht, source_pubkey, ping_id, 1); + send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); friend->recvNATping_timestamp = unix_time(); return 0; - } else if (packet[0] == 1) { + } else if (packet[0] == NAT_PING_RESPONSE) { if (friend->NATping_id == ping_id) { friend->NATping_id = ((uint64_t)random_int() << 32) + random_int(); friend->hole_punching = 1; @@ -1059,7 +1062,7 @@ static void do_NAT(DHT *dht) continue; if (dht->friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { - send_NATping(dht, dht->friends_list[i].client_id, dht->friends_list[i].NATping_id, 0); /*0 is request*/ + send_NATping(dht, dht->friends_list[i].client_id, dht->friends_list[i].NATping_id, NAT_PING_REQUEST); dht->friends_list[i].NATping_timestamp = temp_time; } @@ -1162,11 +1165,11 @@ DHT *new_DHT(Net_Crypto *c) } temp->c = c; - networking_registerhandler(c->lossless_udp->net, 0, &handle_ping_request, temp); - networking_registerhandler(c->lossless_udp->net, 1, &handle_ping_response, temp); - networking_registerhandler(c->lossless_udp->net, 2, &handle_getnodes, temp); - networking_registerhandler(c->lossless_udp->net, 3, &handle_sendnodes, temp); - cryptopacket_registerhandler(c, 254, &handle_NATping, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_REQUEST, &handle_ping_request, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); + cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); return temp; } 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 int send_LANdiscovery(uint16_t port, Net_Crypto *c) { uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; - data[0] = 33; + data[0] = NET_PACKET_LAN_DISCOVERY; memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); IP_Port ip_port = {broadcast_ip(), port}; 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) void LANdiscovery_init(DHT *dht) { - networking_registerhandler(dht->c->lossless_udp->net, 33, &handle_LANdiscovery, dht); + networking_registerhandler(dht->c->lossless_udp->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); } 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 uint8_t packet[1 + 4 + 4]; uint32_t temp; - packet[0] = 16; + packet[0] = NET_PACKET_HANDSHAKE; temp = htonl(handshake_id1); memcpy(packet + 1, &temp, 4); temp = htonl(handshake_id2); @@ -431,7 +431,7 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id) uint32_t requested[BUFFER_PACKET_NUM]; uint32_t number = missing_packets(ludp, connection_id, requested); - packet[0] = 17; + packet[0] = NET_PACKET_SYNC; index += 1; memcpy(packet + index, &counter, 1); index += 1; @@ -450,7 +450,7 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t uint32_t index = packet_num % MAX_QUEUE_NUM; uint32_t temp; uint8_t packet[1 + 4 + MAX_DATA_SIZE]; - packet[0] = 18; + packet[0] = NET_PACKET_DATA; temp = htonl(packet_num); memcpy(packet + 1, &temp, 4); memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data, @@ -733,9 +733,9 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net) return NULL; temp->net = net; - networking_registerhandler(net, 16, &handle_handshake, temp); - networking_registerhandler(net, 17, &handle_SYNC, temp); - networking_registerhandler(net, 18, &handle_data, temp); + networking_registerhandler(net, NET_PACKET_HANDSHAKE, &handle_handshake, temp); + networking_registerhandler(net, NET_PACKET_SYNC, &handle_SYNC, temp); + networking_registerhandler(net, NET_PACKET_DATA, &handle_data, temp); return temp; } @@ -839,4 +839,4 @@ void kill_lossless_udp(Lossless_UDP *ludp) { free(ludp->connections); free(ludp); -} \ No newline at end of file +} diff --git a/core/friend_requests.c b/core/friend_requests.c index ee2da633..d8c6858b 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -39,7 +39,7 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8 uint8_t packet[MAX_DATA_SIZE]; int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp, length + sizeof(nospam_num), - 32); /* 32 is friend request packet id */ + CRYPTO_PACKET_FRIEND_REQ); if (len == -1) return -1; @@ -137,5 +137,5 @@ static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_ void friendreq_init(Friend_Requests *fr, Net_Crypto *c) { - cryptopacket_registerhandler(c, 32, &friendreq_handlepacket, fr); + cryptopacket_registerhandler(c, CRYPTO_PACKET_FRIEND_REQ, &friendreq_handlepacket, fr); } 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 * if (len == -1) return -1; - packet[0] = 32; + packet[0] = NET_PACKET_CRYPTO; memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES); memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES); 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 { DHT *dht = object; - if (packet[0] == 32) { + if (packet[0] == NET_PACKET_CRYPTO) { if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || length > MAX_DATA_SIZE + ENCRYPTION_PADDING) return 1; @@ -730,7 +730,7 @@ Net_Crypto *new_net_crypto(Networking_Core *net) void init_cryptopackets(void *dht) { DHT *s_dht = dht; - networking_registerhandler(s_dht->c->lossless_udp->net, 32, &cryptopacket_handle, s_dht); + networking_registerhandler(s_dht->c->lossless_udp->net, NET_PACKET_CRYPTO, &cryptopacket_handle, s_dht); } static void kill_timedout(Net_Crypto *c) diff --git a/core/net_crypto.h b/core/net_crypto.h index 46bcf250..81670993 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h @@ -32,6 +32,9 @@ extern "C" { #define MAX_INCOMING 64 +#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID */ +#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID */ + typedef struct { uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ 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" { #define MAX_UDP_PACKET_SIZE 65507 +#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */ +#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID */ +#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID */ +#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */ +#define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */ +#define NET_PACKET_SYNC 17 /* SYNC packet ID */ +#define NET_PACKET_DATA 18 /* Data packet ID */ +#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */ +#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */ + /* Current time, unix format */ #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 { } __attribute__((packed)) clientid_t; -typedef enum { - PACKET_PING_REQ = 0, - PACKET_PING_RES = 1 - -} packetid_t; - // Ping packet typedef struct { - uint8_t magic; + uint8_t packet_id; clientid_t client_id; uint8_t nonce[crypto_box_NONCEBYTES]; uint64_t ping_id; @@ -28,7 +22,7 @@ typedef struct { // Pong packet typedef struct { - uint8_t magic; + uint8_t packet_id; clientid_t client_id; uint8_t nonce[crypto_box_NONCEBYTES]; 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 // Generate random ping_id ping_id = add_ping(ping, ipp); - pk.magic = PACKET_PING_REQ; + pk.packet_id = NET_PACKET_PING_REQUEST; id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey random_nonce((uint8_t *) &pk.nonce); // Generate random nonce @@ -153,7 +153,7 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64 if (id_eq(client_id, (clientid_t *)c->self_public_key)) return 1; - pk.magic = PACKET_PING_RES; + pk.packet_id = NET_PACKET_PING_RESPONSE; id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey random_nonce((uint8_t *) &pk.nonce); // Generate random nonce diff --git a/other/CMakeLists.txt b/other/CMakeLists.txt index 871a9e92..7e6c9eb9 100644 --- a/other/CMakeLists.txt +++ b/other/CMakeLists.txt @@ -1,13 +1,10 @@ cmake_minimum_required(VERSION 2.6.0) cmake_policy(SET CMP0011 NEW) -SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName) - -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake) if(WIN32) - file(MAKE_DIRECTORY "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox") - file(INSTALL DHTservers DESTINATION "C:/Users/${USER_NAME}/AppData/Roaming/.config/tox") + file(MAKE_DIRECTORY "$ENV{APPDATA}/.config/tox") + file(INSTALL DHTservers DESTINATION "$ENV{APPDATA}/.config/tox") else() set(HOME "$ENV{HOME}") if(APPLE) @@ -19,6 +16,8 @@ else() endif() endif() +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake) + if(LINUX) add_subdirectory(bootstrap_serverdaemon) endif()