diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 83 |
1 files changed, 73 insertions, 10 deletions
@@ -1,12 +1,27 @@ | |||
1 | #include "DHT.h" | 1 | #include "DHT.h" |
2 | 2 | ||
3 | //send a getnodes request | 3 | |
4 | int getnodes() | 4 | //Function to send packet(data) of length length to ip_port |
5 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | ||
5 | { | 6 | { |
7 | ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port}; | ||
6 | 8 | ||
7 | 9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | |
8 | 10 | } | |
9 | 11 | ||
12 | |||
13 | |||
14 | //send a getnodes request | ||
15 | //Currently incomplete: missing the ping_id part | ||
16 | int getnodes(IP_Port ip_port, char * client_id) | ||
17 | { | ||
18 | char data[69]; | ||
19 | data[0] = 2; | ||
20 | |||
21 | memcpy(data + 5, self_client_id, 32); | ||
22 | memcpy(data + 37, client_id, 32); | ||
23 | |||
24 | sendpacket(ip_port, data, sizeof(data)); | ||
10 | } | 25 | } |
11 | 26 | ||
12 | //send a ping request | 27 | //send a ping request |
@@ -14,15 +29,45 @@ int getnodes() | |||
14 | int ping(IP_Port ip_port) | 29 | int ping(IP_Port ip_port) |
15 | { | 30 | { |
16 | char data[37]; | 31 | char data[37]; |
17 | data[0] = 00; | 32 | data[0] = 0; |
33 | |||
18 | memcpy(data + 5, self_client_id, 32); | 34 | memcpy(data + 5, self_client_id, 32); |
19 | //ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port}; | ||
20 | 35 | ||
21 | //return sendto(sock, data, sizeof(data) - 1, 0, (struct sockaddr *)&addr, addrlen); | 36 | sendpacket(ip_port, data, sizeof(data)); |
22 | //sendto(int socket_descriptor, char *buffer, int buffer_length, int flags, struct sockaddr *destination_address, int address_length); | 37 | |
23 | } | 38 | } |
24 | 39 | ||
25 | 40 | ||
41 | //Packet handling functions | ||
42 | //One to handle each types of packets | ||
43 | |||
44 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) | ||
45 | { | ||
46 | |||
47 | |||
48 | } | ||
49 | |||
50 | int handle_pingres(char * packet, uint32_t length, IP_Port source) | ||
51 | { | ||
52 | |||
53 | |||
54 | } | ||
55 | |||
56 | int handle_getnodes(char * packet, uint32_t length, IP_Port source) | ||
57 | { | ||
58 | |||
59 | |||
60 | } | ||
61 | |||
62 | int handle_sendnodes(char * packet, uint32_t length, IP_Port source) | ||
63 | { | ||
64 | |||
65 | |||
66 | } | ||
67 | |||
68 | |||
69 | |||
70 | |||
26 | 71 | ||
27 | void addfriend(char * client_id) | 72 | void addfriend(char * client_id) |
28 | { | 73 | { |
@@ -58,8 +103,26 @@ IP_Port getfriendip(char * client_id) | |||
58 | 103 | ||
59 | 104 | ||
60 | 105 | ||
61 | void DHT_recvpacket(char * packet, uint32_t length) | 106 | void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) |
62 | { | 107 | { |
108 | switch (packet[0]) { | ||
109 | case 0: | ||
110 | handle_pingreq(packet, length, source); | ||
111 | break; | ||
112 | case 1: | ||
113 | handle_pingres(packet, length, source); | ||
114 | break; | ||
115 | case 2: | ||
116 | handle_getnodes(packet, length, source); | ||
117 | break; | ||
118 | case 3: | ||
119 | handle_sendnodes(packet, length, source); | ||
120 | break; | ||
121 | default: | ||
122 | break; | ||
123 | |||
124 | } | ||
125 | |||
63 | 126 | ||
64 | } | 127 | } |
65 | 128 | ||