summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c54
-rw-r--r--core/DHT.h17
2 files changed, 58 insertions, 13 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 9f2d1f56..911e23e7 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -1,6 +1,9 @@
1#include "DHT.h" 1#include "DHT.h"
2 2
3 3
4//Basic network functions:
5//TODO: put them somewhere else than here
6
4//Function to send packet(data) of length length to ip_port 7//Function to send packet(data) of length length to ip_port
5int sendpacket(IP_Port ip_port, char * data, uint32_t length) 8int sendpacket(IP_Port ip_port, char * data, uint32_t length)
6{ 9{
@@ -9,6 +12,26 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
9 return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); 12 return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
10} 13}
11 14
15//Function to recieve data, ip and port of sender is put into ip_port
16//the packet data into data
17//the packet length into length.
18int recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
19{
20 ADDR addr;
21 uint32_t addrlen = sizeof(addr);
22 (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
23 if(*(int *)length == -1)
24 {
25 //nothing recieved
26 return -1;
27 }
28 ip_port->ip = addr.ip;
29 ip_port->port = addr.port;
30 return 0;
31}
32
33
34
12//Compares client_id1 and client_id2 with client_id 35//Compares client_id1 and client_id2 with client_id
13//return 0 if both are same distance 36//return 0 if both are same distance
14//return 1 if client_id1 is closer. 37//return 1 if client_id1 is closer.
@@ -288,7 +311,7 @@ int sendnodes(IP_Port ip_port, char * client_id)
288 311
289 312
290//Packet handling functions 313//Packet handling functions
291//One to handle each types of packets 314//One to handle each types of packets we recieve
292 315
293int handle_pingreq(char * packet, uint32_t length, IP_Port source) 316int handle_pingreq(char * packet, uint32_t length, IP_Port source)
294{ 317{
@@ -340,16 +363,16 @@ int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
340 363
341} 364}
342 365
343 366//END of packet handling functions
344 367
345 368
346 369
347void addfriend(char * client_id) 370void addfriend(char * client_id)
348{ 371{
349 372 //TODO: Make the array of friends dynamic instead of a static array with 256 places..
350 373 //WARNING:This will segfault if the number of friends exceeds 256.
351 374 memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE);
352 375 num_friends++;
353} 376}
354 377
355 378
@@ -358,10 +381,17 @@ void addfriend(char * client_id)
358 381
359char delfriend(char * client_id) 382char delfriend(char * client_id)
360{ 383{
361 384 uint32_t i;
362 385 for(i = 0; i < num_friends; i++)
363 386 {
364 387 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal
388 {
389 memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE);
390 num_friends--;
391 return 0;
392 }
393 }
394 return 1;
365} 395}
366 396
367 397
@@ -378,7 +408,7 @@ IP_Port getfriendip(char * client_id)
378 408
379 409
380 410
381void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) 411int DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
382{ 412{
383 switch (packet[0]) { 413 switch (packet[0]) {
384 case 0: 414 case 0:
@@ -394,7 +424,7 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
394 handle_sendnodes(packet, length, source); 424 handle_sendnodes(packet, length, source);
395 break; 425 break;
396 default: 426 default:
397 return; 427 return 1;
398 428
399 } 429 }
400 430
diff --git a/core/DHT.h b/core/DHT.h
index 0acc6602..7493761a 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -21,8 +21,11 @@
21//Current time, unix format 21//Current time, unix format
22#define unix_time() ((uint32_t)time(NULL)) 22#define unix_time() ((uint32_t)time(NULL))
23 23
24//size of the client_id in bytes
24#define CLIENT_ID_SIZE 32 25#define CLIENT_ID_SIZE 32
25 26
27#define MAX_UDP_PACKET_SIZE 65507
28
26typedef union 29typedef union
27{ 30{
28 uint8_t c[4]; 31 uint8_t c[4];
@@ -100,7 +103,9 @@ IP_Port getfriendip(char * client_id);
100void doDHT(); 103void doDHT();
101 104
102//if we recieve a DHT packet we call this function so it can be handled. 105//if we recieve a DHT packet we call this function so it can be handled.
103void DHT_recvpacket(char * packet, uint32_t length, IP_Port source); 106//Return 0 if packet is handled correctly or if the packet was shit.
107//return 1 if it didn't handle the packet.
108int DHT_recvpacket(char * packet, uint32_t length, IP_Port source);
104 109
105//Use this function to bootstrap the client 110//Use this function to bootstrap the client
106//Sends a get nodes request to the given ip port 111//Sends a get nodes request to the given ip port
@@ -139,3 +144,13 @@ Pinged pings[LPING_ARRAY];
139Pinged send_nodes[LSEND_NODES_ARRAY]; 144Pinged send_nodes[LSEND_NODES_ARRAY];
140 145
141 146
147//Basic network functions:
148//TODO: put them somewhere else than here
149
150//Function to send packet(data) of length length to ip_port
151int sendpacket(IP_Port ip_port, char * data, uint32_t length);
152
153//Function to recieve data, ip and port of sender is put into ip_port
154//the packet data into data
155//the packet length into length.
156int recievepacket(IP_Port * ip_port, char * data, uint32_t * length); \ No newline at end of file