refactoring packet IDs

This commit is contained in:
Michael Rose 2013-08-21 09:55:43 +02:00
parent 55dc9486a7
commit 3868a5326c
7 changed files with 32 additions and 28 deletions

View File

@ -482,7 +482,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 +524,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);
@ -1162,10 +1162,10 @@ 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);
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, 254, &handle_NATping, temp);
return temp;
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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))

View File

@ -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;

View File

@ -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