summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-06-24 08:59:42 -0400
committerirungentoo <irungentoo@gmail.com>2013-06-24 08:59:42 -0400
commit388c0f38f7d2096efd1049b59851b00c448f940f (patch)
treedd283fbb2cd47054e34559587028ebe5b572bf8e /core
parent7e341fb171e360e7b1218fd2003e6a1d27863120 (diff)
More Functions.
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c67
-rw-r--r--core/DHT.h3
2 files changed, 56 insertions, 14 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 7c40e9fe..4b1c6f13 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -9,8 +9,42 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
9 return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); 9 return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
10} 10}
11 11
12//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist
13int addto_lists(IP_Port ip_port, char * client_id)
14{
15
16
17
18}
12 19
13 20
21//send a ping request
22//Currently incomplete: missing the ping_id part
23int pingreq(IP_Port ip_port)
24{
25 char data[37];
26 data[0] = 0;
27
28 memcpy(data + 5, self_client_id, 32);
29
30 sendpacket(ip_port, data, sizeof(data));
31
32}
33
34//send a ping response
35//Currently incomplete: missing the ping_id part
36int pingres(IP_Port ip_port, uint32_t ping_id)
37{
38 char data[37];
39 data[0] = 1;
40
41 memcpy(data + 1, &ping_id, 4);
42 memcpy(data + 5, self_client_id, 32);
43
44 sendpacket(ip_port, data, sizeof(data));
45
46}
47
14//send a getnodes request 48//send a getnodes request
15//Currently incomplete: missing the ping_id part 49//Currently incomplete: missing the ping_id part
16int getnodes(IP_Port ip_port, char * client_id) 50int getnodes(IP_Port ip_port, char * client_id)
@@ -24,26 +58,32 @@ int getnodes(IP_Port ip_port, char * client_id)
24 sendpacket(ip_port, data, sizeof(data)); 58 sendpacket(ip_port, data, sizeof(data));
25} 59}
26 60
27//send a ping request 61//send a getnodes request
28//Currently incomplete: missing the ping_id part 62//Currently incomplete: missing the ping_id part
29int ping(IP_Port ip_port) 63int sendnodes(IP_Port ip_port, char * client_id)
30{ 64{
31 char data[37]; 65 char data[325];
32 data[0] = 0; 66 data[0] = 3;
33 67
34 memcpy(data + 5, self_client_id, 32); 68 memcpy(data + 5, self_client_id, 32);
35 69 memcpy(data + 37, client_id, 32);
36 sendpacket(ip_port, data, sizeof(data)); 70
37 71 sendpacket(ip_port, data, sizeof(data));
38} 72}
39 73
40 74
75
76
41//Packet handling functions 77//Packet handling functions
42//One to handle each types of packets 78//One to handle each types of packets
43 79
44int handle_pingreq(char * packet, uint32_t length, IP_Port source) 80int handle_pingreq(char * packet, uint32_t length, IP_Port source)
45{ 81{
82 uint32_t ping_id;
46 83
84 memcpy(&ping_id, packet + 1, 4);
85
86 pingres(source, ping_id);
47 87
48} 88}
49 89
@@ -108,18 +148,20 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
108 switch (packet[0]) { 148 switch (packet[0]) {
109 case 0: 149 case 0:
110 handle_pingreq(packet, length, source); 150 handle_pingreq(packet, length, source);
151 //TODO: try to add requesting node to client_list if packet is valid
111 break; 152 break;
112 case 1: 153 case 1:
113 handle_pingres(packet, length, source); 154 handle_pingres(packet, length, source);
114 break; 155 break;
115 case 2: 156 case 2:
116 handle_getnodes(packet, length, source); 157 handle_getnodes(packet, length, source);
158 //TODO: try to add requesting node to client_list if packet is valid
117 break; 159 break;
118 case 3: 160 case 3:
119 handle_sendnodes(packet, length, source); 161 handle_sendnodes(packet, length, source);
120 break; 162 break;
121 default: 163 default:
122 break; 164 return;
123 165
124 } 166 }
125 167
@@ -141,9 +183,8 @@ void doDHT()
141 183
142void bootstrap(IP_Port ip_port) 184void bootstrap(IP_Port ip_port)
143{ 185{
144 186
145 187 getnodes(ip_port, self_client_id);
146
147 188
148} 189}
149 190
diff --git a/core/DHT.h b/core/DHT.h
index 2e6dde6f..d89d0f76 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -116,9 +116,10 @@ char self_client_id[32];
116//We only use one so it's much easier to have it as a global variable 116//We only use one so it's much easier to have it as a global variable
117int sock; 117int sock;
118 118
119//A list of the clients mathematically closest to ours.
119#define LCLIENT_LIST 32 120#define LCLIENT_LIST 32
121Client_data close_clientlist[LCLIENT_LIST];
120 122
121Client_data client_list[LCLIENT_LIST];
122 123
123//Let's start with a static array for testing. 124//Let's start with a static array for testing.
124Friend friends_list[256]; 125Friend friends_list[256];