This commit is contained in:
t
2025-01-09 22:20:27 +01:00
parent 4f69f0987e
commit b06a71186a
3 changed files with 71 additions and 33 deletions
+14
View File
@@ -0,0 +1,14 @@
#include "dht.h"
#define cpy(x, y, z) char *x_c = x; char *y_c = y; while(*y_c && z--) *x_c++ = *y_c++
void lookup_request_init(lookup_request* lr, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request)
{
int size = NODE_IP_MAX_SIZE;
lr->node_id = node_id;
{ cpy(lr->node_ip, node_ip, size); }
lr->node_port = node_port;
lr->hash_id = hash_id;
size = HTTP_MAX_SIZE;
cpy(lr->node_request, node_request, size);
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef DHT_H
#define DHT_H
#include "http.h"
#define NODE_IP_MAX_SIZE 16
typedef struct{
int node_id;
char node_ip[NODE_IP_MAX_SIZE];
int node_port;
uint16_t hash_id;
char node_request[HTTP_MAX_SIZE];
}lookup_request;
void lookup_request_init(lookup_request*, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request);
void create_lookup_request(lookup_request*, char *request);
#endif //DHT_H
+36 -32
View File
@@ -48,6 +48,8 @@ struct tuple resources[MAX_RESOURCES] = {
{"/static/bar", "Bar", sizeof "Bar" - 1}, {"/static/bar", "Bar", sizeof "Bar" - 1},
{"/static/baz", "Baz", sizeof "Baz" - 1}}; {"/static/baz", "Baz", sizeof "Baz" - 1}};
neighbor check_neighborhood(uint16_t);
uint16_t hash_uri(const char *uri){ uint16_t hash_uri(const char *uri){
char *uri_ptr = (char*)uri; char *uri_ptr = (char*)uri;
char dynamic[] = "/dynamic/"; char dynamic[] = "/dynamic/";
@@ -55,8 +57,9 @@ uint16_t hash_uri(const char *uri){
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic); if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic);
elif(strstr(uri, static_)) uri_ptr += strlen(static_); elif(strstr(uri, static_)) uri_ptr += strlen(static_);
elif(uri[0] == '/') uri_ptr++;
return pseudo_hash((unsigned char *)uri, strlen(uri)); return pseudo_hash((unsigned char *)uri_ptr, strlen(uri_ptr));
} }
/** /**
@@ -79,16 +82,31 @@ void send_reply(int conn, struct request *request) {
if (strcmp(request->method, "GET") == 0) { if (strcmp(request->method, "GET") == 0) {
// Find the resource with the given URI in the 'resources' array. // Find the resource with the given URI in the 'resources' array.
size_t resource_length; size_t resource_length;
const char *resource = // const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
get(request->uri, resources, MAX_RESOURCES, &resource_length);
if (resource) { uint16_t resource_hash = hash_uri(request->uri);
size_t payload_offset = neighbor responsible = check_neighborhood(resource_hash);
sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n",
resource_length); if (responsible == res) {
memcpy(reply + payload_offset, resource, resource_length); sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
offset = payload_offset + resource_length; offset = strlen(reply);
} else { printf("200\n");
}
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");
}
elif(responsible == not_res)
{
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
offset = strlen(reply);
printf("503\n");
// TODO: Call other dht
}
else {
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} }
@@ -352,29 +370,11 @@ neighbor check_neighborhood(uint16_t hash) {
} }
// TODO: check correctness // TODO: check correctness
if (pre_id == id && suc_id == id) { if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id))
return res; return res;
}
if (id < pre_id) { if ((id < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
if ((hash > pre_id && hash < 65535) || hash <= id) {
return res;
}
} else {
if (hash > pre_id && hash <= id) {
return res;
}
}
if (id < suc_id) {
if (hash > id && hash <= suc_id) {
return not_res; return not_res;
}
} else {
if (hash > id || hash <= suc_id) {
return not_res;
}
}
return dunno; return dunno;
} }
@@ -402,6 +402,7 @@ static void handle_udp_request(int _sock){
} }
// HASH Received // HASH Received
// TODO: alter buffer
uint16_t hash = atoi(buffer); uint16_t hash = atoi(buffer);
printf("Hash: %u\n", hash); printf("Hash: %u\n", hash);
// Check belonging // Check belonging
@@ -417,7 +418,10 @@ static void handle_udp_request(int _sock){
} }
printf("Sent: %s", res); printf("Sent: %s", res);
} }
/*
TODO: return 404
elif (responsibility == res_303) { elif (responsibility == res_303) {
fprintf(stderr, "Sent 303!\n");
char res[1024] = {0}; 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); 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){ if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
@@ -426,9 +430,9 @@ static void handle_udp_request(int _sock){
} }
printf("Sent: %s", res); printf("Sent: %s", res);
} }
*/
elif (responsibility == not_res) { elif (responsibility == not_res) {
// TODO: DIRECT TO NEXT NODE // 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"; 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) if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1)
{ {
@@ -436,8 +440,8 @@ static void handle_udp_request(int _sock){
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Sent: %s", res); printf("Sent: %s", res);
//call_other_dht(); //call_other_dht();
*/
} }
} }