Implemented DHT
This commit is contained in:
+92
-45
@@ -16,6 +16,8 @@
|
||||
#include "data.h"
|
||||
#include "http.h"
|
||||
#include "util.h"
|
||||
#include "dht.h"
|
||||
#include "lookup.h"
|
||||
|
||||
#define elif else if
|
||||
|
||||
@@ -31,7 +33,7 @@ bool _DO_UDP = false;
|
||||
|
||||
int _NODE_ID;
|
||||
const char *_NODE_IP;
|
||||
const char *_NODE_PORT;
|
||||
int _NODE_PORT;
|
||||
|
||||
#define MAX_RESOURCES 100
|
||||
|
||||
@@ -87,6 +89,21 @@ void send_reply(int conn, struct request *request) {
|
||||
uint16_t resource_hash = hash_uri(request->uri);
|
||||
neighbor responsible = check_neighborhood(resource_hash);
|
||||
|
||||
lookup_request *req = find_lookup(resource_hash);
|
||||
if (req != NULL)
|
||||
{
|
||||
if (req->message_type != 1)
|
||||
{
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (responsible == res) {
|
||||
sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||
offset = strlen(reply);
|
||||
@@ -94,9 +111,19 @@ void send_reply(int conn, struct request *request) {
|
||||
}
|
||||
elif(responsible == res_303)
|
||||
{
|
||||
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri);
|
||||
offset = strlen(reply);
|
||||
printf("303\n");
|
||||
int suc_id = _SUCC_ID;
|
||||
if (resource_hash <= suc_id || suc_id >= 0 && suc_id < _NODE_ID)
|
||||
{
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri);
|
||||
offset = strlen(reply);
|
||||
printf("303\n");
|
||||
}
|
||||
}
|
||||
elif(responsible == not_res)
|
||||
{
|
||||
@@ -104,6 +131,10 @@ void send_reply(int conn, struct request *request) {
|
||||
offset = strlen(reply);
|
||||
printf("503\n");
|
||||
// TODO: Call other dht
|
||||
lookup_request lookreq;
|
||||
lookup_request_init(&lookreq, 0, _NODE_ID, (char*)_NODE_IP, _NODE_PORT, resource_hash, NULL);
|
||||
|
||||
call_network(&lookreq, _SUCC_IP, _SUCC_PORT);
|
||||
}
|
||||
else {
|
||||
printf("404\n");
|
||||
@@ -364,16 +395,15 @@ neighbor check_neighborhood(uint16_t hash) {
|
||||
int suc_id = _SUCC_ID;
|
||||
|
||||
if(pre_id == suc_id){
|
||||
if(hash <= id && hash > suc_id) return res_303;
|
||||
if(hash > id && hash <= suc_id) return not_res;
|
||||
return res_303; // hash is in between suc and 0
|
||||
if(hash > id && hash <= suc_id) return res_303;
|
||||
return res;
|
||||
}
|
||||
|
||||
// TODO: check correctness
|
||||
if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id))
|
||||
return res;
|
||||
|
||||
if ((id < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
|
||||
if ((id < suc_id && hash > id && hash <= suc_id) || (id > suc_id && (hash > id || hash <= suc_id)))
|
||||
return not_res;
|
||||
|
||||
return dunno;
|
||||
@@ -403,45 +433,60 @@ static void handle_udp_request(int _sock){
|
||||
|
||||
// HASH Received
|
||||
// TODO: alter buffer
|
||||
uint16_t hash = atoi(buffer);
|
||||
printf("Hash: %u\n", hash);
|
||||
// Check belonging
|
||||
int pre_id = _PRED_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
neighbor responsibility = check_neighborhood(hash);
|
||||
lookup_request lr = {0};
|
||||
extract_lookup_request(&lr, buffer);
|
||||
|
||||
if (responsibility == res) {
|
||||
char res[] = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
|
||||
if (sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1) {
|
||||
perror("Func: handle_udp_request - failed to send 200 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
}
|
||||
/*
|
||||
TODO: return 404
|
||||
elif (responsibility == res_303) {
|
||||
fprintf(stderr, "Sent 303!\n");
|
||||
char res[1024] = {0};
|
||||
snprintf(res, 1024, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%s/%d\r\nContent-Length: 0\r\n\r\n", _NODE_IP, _NODE_PORT, suc_id);
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
|
||||
perror("Func: handle_udp_request - failed to send 303 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
}
|
||||
*/
|
||||
elif (responsibility == not_res) {
|
||||
// TODO: DIRECT TO NEXT NODE
|
||||
const char res[] = "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n";
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1)
|
||||
printf("Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
|
||||
|
||||
// DO LOOKUP
|
||||
if (lr.message_type == 0)
|
||||
{
|
||||
// Check belonging
|
||||
const int suc_id = _SUCC_ID;
|
||||
char *callback_ip = lr.node_ip;
|
||||
int callback_port = lr.node_port;
|
||||
|
||||
lookup_request backfire = {0};
|
||||
|
||||
if (lr.node_id == suc_id)
|
||||
{
|
||||
perror("Func: handle_udp_request - failed to send 503 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
// Send back 404 not found
|
||||
clone_lookup_request(&backfire, &lr);
|
||||
backfire.message_type = -1;
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
else
|
||||
{
|
||||
const neighbor responsibility = check_neighborhood(lr.hash_id);
|
||||
|
||||
if (responsibility == res) {
|
||||
lookup_request_init(&backfire, 1, _NODE_ID, (char*)_NODE_IP, _NODE_PORT, _PRED_ID, lr.node_request);
|
||||
}
|
||||
elif (responsibility == not_res) {
|
||||
// is succ responsible
|
||||
if (lr.hash_id <= suc_id)
|
||||
{
|
||||
lookup_request_init(&backfire, 1, suc_id, _SUCC_IP, _SUCC_PORT, _NODE_ID, lr.node_request);
|
||||
}
|
||||
else
|
||||
{
|
||||
lookup_request_init(&backfire, 1, suc_id, _SUCC_IP, _SUCC_PORT, _NODE_ID, lr.node_request);
|
||||
callback_ip = _SUCC_IP; // TODO: Check validity
|
||||
callback_port = _SUCC_PORT;
|
||||
}
|
||||
}
|
||||
}
|
||||
call_network(&backfire, callback_ip, callback_port);
|
||||
}
|
||||
elif (lr.message_type == 1)
|
||||
{
|
||||
lookup_request *toLookup = malloc(sizeof(lookup_request));
|
||||
clone_lookup_request(toLookup, &lr);
|
||||
|
||||
push_lookup(toLookup);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//call_other_dht();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,11 +502,13 @@ int main(int argc, char **argv) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
init_lookup_table();
|
||||
|
||||
if(argc == 3) _NODE_ID = 0;
|
||||
elif(argc == 4) _NODE_ID = atoi(argv[3]);
|
||||
|
||||
// Set this DHT-Nodes ID
|
||||
_NODE_PORT = argv[2];
|
||||
_NODE_PORT = atoi(argv[2]);
|
||||
_NODE_IP = argv[1];
|
||||
|
||||
struct sockaddr_in addr = derive_sockaddr(argv[1], argv[2]);
|
||||
@@ -534,6 +581,6 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free_lookup_table();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user