Merge branch 'dev'

Conflicts:
	core/packets.h
	core/ping.c
This commit is contained in:
plutooo 2013-08-06 09:31:29 -07:00
commit 659a1a06a9
3 changed files with 138 additions and 118 deletions

View File

@ -7,11 +7,13 @@
typedef struct {
uint8_t id[CLIENT_ID_SIZE];
} __attribute__((packed)) clientid_t;
typedef enum {
PACKET_PING_REQ = 0,
PACKET_PING_RES = 1
} packetid_t;
// Ping packet
@ -21,6 +23,7 @@ typedef struct {
uint8_t nonce[crypto_box_NONCEBYTES];
uint64_t ping_id;
uint8_t padding[ENCRYPTION_PADDING];
} __attribute__((packed)) pingreq_t;
// Pong packet
@ -30,4 +33,5 @@ typedef struct {
uint8_t nonce[crypto_box_NONCEBYTES];
uint64_t ping_id;
uint8_t padding[ENCRYPTION_PADDING];
} __attribute__((packed)) pingres_t;

View File

@ -31,16 +31,19 @@ static clientid_t* self_id = (clientid_t*) &self_public_key;
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; // DHT.c
void init_ping() {
void init_ping()
{
num_pings = 0;
pos_pings = 0;
}
static bool is_timeout(uint64_t time) {
static bool is_timeout(uint64_t time)
{
return (time + PING_TIMEOUT) < now();
}
static void remove_timeouts() { // O(n)
static void remove_timeouts() // O(n)
{
size_t i, id;
size_t new_pos = pos_pings;
size_t new_num = num_pings;
@ -54,15 +57,17 @@ static void remove_timeouts() { // O(n)
new_num--;
}
// Break here because list is sorted.
else
else {
break;
}
}
num_pings = new_num;
pos_pings = new_pos % PING_NUM_MAX;
}
uint64_t add_ping(IP_Port ipp) { // O(n)
uint64_t add_ping(IP_Port ipp) // O(n)
{
size_t p;
remove_timeouts();
@ -84,9 +89,13 @@ uint64_t add_ping(IP_Port ipp) { // O(n)
return pings[p].id;
}
bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n)
bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n)
{
size_t i, id;
if (ipp.ip.i == 0 && ping_id == 0)
return false;
remove_timeouts();
for (i=0; i<num_pings; i++) {
@ -101,7 +110,8 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n)
return false;
}
int send_ping_request(IP_Port ipp, clientid_t* client_id) {
int send_ping_request(IP_Port ipp, clientid_t* client_id)
{
pingreq_t pk;
int rc;
uint64_t ping_id;
@ -129,7 +139,8 @@ int send_ping_request(IP_Port ipp, clientid_t* client_id) {
return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
}
int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id) {
int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
{
pingres_t pk;
int rc;

View File

@ -12,11 +12,13 @@
#include "DHT.h"
#include "packets.h"
uint64_t now() {
uint64_t now()
{
return time(NULL);
}
uint64_t random_64b() {
uint64_t random_64b()
{
uint64_t r;
// This is probably not random enough?
@ -27,14 +29,17 @@ uint64_t random_64b() {
return r;
}
bool ipp_eq(IP_Port a, IP_Port b) {
bool ipp_eq(IP_Port a, IP_Port b)
{
return (a.ip.i == b.ip.i) && (a.port == b.port);
}
bool id_eq(clientid_t* dest, clientid_t* src) {
bool id_eq(clientid_t* dest, clientid_t* src)
{
return memcmp(dest, src, sizeof(clientid_t)) == 0;
}
void id_cpy(clientid_t* dest, clientid_t* src) {
void id_cpy(clientid_t* dest, clientid_t* src)
{
memcpy(dest, src, sizeof(clientid_t));
}