Made code a bit cleaner

This commit is contained in:
Ano-sys
2025-01-12 00:00:59 +01:00
parent 4d6ea5c51f
commit d902468bb5
2 changed files with 116 additions and 160 deletions
+6 -6
View File
@@ -66,13 +66,13 @@ int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){
addr = *((struct sockaddr_in*) servinfo->ai_addr); addr = *((struct sockaddr_in*) servinfo->ai_addr);
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
// lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr); perror("\n\n---CALL_NETWORK---\n");
// lr->node_port = ntohs(addr.sin_port); perror("[Deserialized]\n");
perror("---CALL_NETWORK---\n");
print_lookup_request(lr); print_lookup_request(lr);
build_lookup_request(lr); build_lookup_request(lr);
print_lookup_request(lr); // perror("[Serialized]\n");
// print_lookup_request(lr);
fprintf(stderr, "Sending to Port ---> %u\n", port);
perror("------------------\n"); perror("------------------\n");
if (sendto(server_socket_udp, lr, sizeof(lookup_request), 0, (struct sockaddr*) &addr, sizeof(struct sockaddr_in)) == -1) { if (sendto(server_socket_udp, lr, sizeof(lookup_request), 0, (struct sockaddr*) &addr, sizeof(struct sockaddr_in)) == -1) {
@@ -90,5 +90,5 @@ void clone_lookup_request(lookup_request* dest, lookup_request* source){
} }
void print_lookup_request(lookup_request *lr){ void print_lookup_request(lookup_request *lr){
fprintf(stderr, "LOOKUP_REQUEST:\n\tMessage-type: %u\n\tNode-ID: %u\n\tNode-IP: %s\n\tNode-PORT: %u\n\tHash-ID: %u\n", lr->message_type, lr->node_id, inet_ntoa(lr->node_ip), lr->node_port, lr->hash_id); fprintf(stderr, "LOOKUP_REQUEST:\n\tMessage-type: %d\n\tNode-ID: %u\n\tNode-IP: %s\t(reversed)\n\tNode-PORT: %u\n\tHash-ID: %u\n", lr->message_type, lr->node_id, inet_ntoa(lr->node_ip), lr->node_port, lr->hash_id);
} }
+110 -154
View File
@@ -22,21 +22,26 @@
#define elif else if #define elif else if
// Getter for environmental DHT sets // Getter for environmental DHT sets
#define _PRED_ID atoi(getenv("PRED_ID")) #define _PRED_ID (uint16_t)atoi(getenv("PRED_ID"))
#define _PRED_IP getenv("PRED_IP") #define _PRED_IP getenv("PRED_IP")
#define _PRED_PORT atoi(getenv("PRED_PORT")) #define _PRED_PORT (uint16_t)atoi(getenv("PRED_PORT"))
#define _SUCC_ID atoi(getenv("SUCC_ID")) #define _SUCC_ID (uint16_t)atoi(getenv("SUCC_ID"))
#define _SUCC_IP getenv("SUCC_IP") #define _SUCC_IP getenv("SUCC_IP")
#define _SUCC_PORT atoi(getenv("SUCC_PORT")) #define _SUCC_PORT (uint16_t)atoi(getenv("SUCC_PORT"))
bool _DO_UDP = false; bool _DO_UDP = false;
int _NODE_ID; uint16_t _NODE_ID;
const char *_NODE_IP; const char *_NODE_IP;
int _NODE_PORT; uint16_t _NODE_PORT;
#define MAX_RESOURCES 100 #define MAX_RESOURCES 100
struct tuple resources[MAX_RESOURCES] = {
{"/static/foo", "Foo", sizeof "Foo" - 1},
{"/static/bar", "Bar", sizeof "Bar" - 1},
{"/static/baz", "Baz", sizeof "Baz" - 1}};
typedef enum neighbor{ typedef enum neighbor{
dunno = -1, dunno = -1,
not_res, not_res,
@@ -44,22 +49,14 @@ typedef enum neighbor{
res_303, res_303,
}neighbor; }neighbor;
struct tuple resources[MAX_RESOURCES] = {
{"/static/foo", "Foo", sizeof "Foo" - 1},
{"/static/bar", "Bar", sizeof "Bar" - 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/";
char static_[] = "/static/"; char static_[] = "/static/";
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic); if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic) + 1;
elif(strstr(uri, static_)) uri_ptr += strlen(static_); elif(strstr(uri, static_)) uri_ptr += strlen(static_) + 1;
/* /*
char *it = (char*)uri; char *it = (char*)uri;
@@ -80,6 +77,22 @@ uint16_t hash_uri(const char *uri){
return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr)); return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr));
} }
neighbor check_neighborhood(uint16_t hash) {
int pre_id = _PRED_ID;
int id = _NODE_ID;
int suc_id = _SUCC_ID;
if(hash > id && hash <= suc_id) return res_303;
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))
return not_res;
return dunno;
}
/** /**
* Sends an HTTP reply to the client based on the received request. * Sends an HTTP reply to the client based on the received request.
* *
@@ -88,7 +101,7 @@ uint16_t hash_uri(const char *uri){
* information. * information.
*/ */
void send_reply(int conn, struct request *request) { void send_reply(int conn, struct request *request) {
fprintf(stderr, "\n\n\n---%u---\n", _NODE_ID);
// Create a buffer to hold the HTTP reply // Create a buffer to hold the HTTP reply
char buffer[HTTP_MAX_SIZE]; char buffer[HTTP_MAX_SIZE];
char *reply = buffer; char *reply = buffer;
@@ -97,87 +110,65 @@ void send_reply(int conn, struct request *request) {
fprintf(stderr, "Handling %s request for %s (%lu byte payload)\n", fprintf(stderr, "Handling %s request for %s (%lu byte payload)\n",
request->method, request->uri, request->payload_length); request->method, request->uri, request->payload_length);
if (strcmp(request->method, "GET") == 0) { printf("Uri: %s\n", request->uri);
// Find the resource with the given URI in the 'resources' array. uint16_t resource_hash = hash_uri(request->uri);
size_t resource_length;
// const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
printf("Uri: %s\n", request->uri);
uint16_t resource_hash = hash_uri(request->uri);
fprintf(stderr, "Resource hash: %u\n", resource_hash); fprintf(stderr, "Resource hash: %u\n", resource_hash);
if (resource_hash == -1) if (resource_hash == -1)
{
perror("Pseudo_hash failed - 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
goto direct_send;
}
neighbor responsible = check_neighborhood(resource_hash);
lookup_request *req = find_lookup(resource_hash);
if(responsible != res){
if(responsible == res_303)
{ {
printf("404\n"); perror("Responsible 303 - 303\n");
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);
}
elif (req == NULL)
{
perror("Not Responsible - 503\n");
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
offset = strlen(reply);
lookup_request lookreq;
lookup_request_init(&lookreq, 0, _NODE_ID, ntohl(inet_addr(_NODE_IP)), _NODE_PORT, resource_hash);
call_network(&lookreq, ntohl(inet_addr(_SUCC_IP)), _SUCC_PORT);
}
elif (req->message_type == 1)
{
// DONE: Success
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(req->node_ip.s_addr);
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, ip, INET_ADDRSTRLEN);
perror("Message-type: 1 - 303\n");
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", ip, req->node_port, request->uri);
offset = strlen(reply);
}
}
elif (strcmp(request->method, "GET") == 0) {
size_t resource_length;
const char* resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
if (resource) {
size_t payload_offset = sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n", resource_length);
memcpy(reply + payload_offset, resource, resource_length);
offset = payload_offset + resource_length;
} else {
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);
goto direct_send;
}
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
{
// DONE: Success
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(req->node_ip.s_addr);
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr.sin_addr, ip, INET_ADDRSTRLEN);
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", ip, req->node_port, request->uri);
offset = strlen(reply);
printf("303\n");
}
}
else{
if (responsible == res) {
printf("Responsible - 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
elif(responsible == res_303)
{
int suc_id = _SUCC_ID;
if ((_NODE_ID < suc_id && resource_hash > _NODE_ID && resource_hash <= suc_id) || (_NODE_ID > suc_id && (resource_hash > _NODE_ID || resource_hash <= suc_id)))
{
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");
}
else
{
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
}
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");
lookup_request lookreq;
lookup_request_init(&lookreq, 0, _NODE_ID, ntohl(inet_addr(_NODE_IP)), _NODE_PORT, resource_hash);
call_network(&lookreq, ntohl(inet_addr(_SUCC_IP)), _SUCC_PORT);
}
else {
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
} }
} else if (strcmp(request->method, "PUT") == 0) { } else if (strcmp(request->method, "PUT") == 0) {
// Try to set the requested resource with the given payload in the // Try to set the requested resource with the given payload in the
@@ -185,18 +176,20 @@ void send_reply(int conn, struct request *request) {
if (set(request->uri, request->payload, request->payload_length, if (set(request->uri, request->payload, request->payload_length,
resources, MAX_RESOURCES)) { resources, MAX_RESOURCES)) {
reply = "HTTP/1.1 204 No Content\r\n\r\n"; reply = "HTTP/1.1 204 No Content\r\n\r\n";
offset = strlen(reply);
} else { } else {
reply = "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n"; reply = "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
} }
offset = strlen(reply);
} else if (strcmp(request->method, "DELETE") == 0) { } else if (strcmp(request->method, "DELETE") == 0) {
// Try to delete the requested resource from the 'resources' array // Try to delete the requested resource from the 'resources' array
if (delete (request->uri, resources, MAX_RESOURCES)) { if (delete (request->uri, resources, MAX_RESOURCES)) {
reply = "HTTP/1.1 204 No Content\r\n\r\n"; reply = "HTTP/1.1 204 No Content\r\n\r\n";
offset = strlen(reply);
} else { } else {
reply = "HTTP/1.1 404 Not Found\r\n\r\n"; reply = "HTTP/1.1 404 Not Found\r\n\r\n";
offset = strlen(reply);
} }
offset = strlen(reply);
} else { } else {
reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n"; reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
@@ -204,11 +197,13 @@ void send_reply(int conn, struct request *request) {
direct_send: // ... do goto, yes you read that right ;) direct_send: // ... do goto, yes you read that right ;)
//offset = strlen(reply);
// Send the reply back to the client // Send the reply back to the client
if (send(conn, reply, offset, 0) == -1) { if (send(conn, reply, offset, 0) == -1) {
perror("send"); perror("send");
close(conn); close(conn);
} }
perror("\n\n---HANDLED REQUEST---\n\n");
} }
/** /**
@@ -224,7 +219,7 @@ void send_reply(int conn, struct request *request) {
* malformed or an error occurs during processing, the return value is -1. * malformed or an error occurs during processing, the return value is -1.
* *
*/ */
size_t process_packet(int conn, char *buffer, size_t n) { int process_packet(int conn, char *buffer, size_t n) {
struct request request = { struct request request = {
.method = NULL, .uri = NULL, .payload = NULL, .payload_length = -1}; .method = NULL, .uri = NULL, .payload = NULL, .payload_length = -1};
ssize_t bytes_processed = parse_request(buffer, n, &request); ssize_t bytes_processed = parse_request(buffer, n, &request);
@@ -429,27 +424,7 @@ static int setup_server_socket_udp(struct sockaddr_in addr){
return sock; return sock;
} }
neighbor check_neighborhood(uint16_t hash) { static void handle_udp_request(){
int pre_id = _PRED_ID;
int id = _NODE_ID;
int suc_id = _SUCC_ID;
if(pre_id == suc_id){
if(hash > id && hash <= suc_id) return res_303;
return res;
}
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))
return not_res;
return dunno;
}
static void handle_udp_request(int _sock){
fprintf(stderr, "---%d---\n", _NODE_ID); fprintf(stderr, "---%d---\n", _NODE_ID);
size_t received_bytes = 0; size_t received_bytes = 0;
lookup_request lr = {0}; lookup_request lr = {0};
@@ -457,18 +432,18 @@ static void handle_udp_request(int _sock){
struct sockaddr_storage caller_addr; struct sockaddr_storage caller_addr;
socklen_t addrlen = sizeof caller_addr; socklen_t addrlen = sizeof caller_addr;
if((received_bytes = recvfrom(_sock, &lr, sizeof(lookup_request), 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){ if((received_bytes = recvfrom(server_socket_udp, &lr, sizeof(lookup_request), 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
perror("Func: handle_udp_request - failed to receive data from caller!"); perror("Func: handle_udp_request - failed to receive data from caller!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
perror("---HANDLE_UDP---\n"); perror("---HANDLE_UDP---\n");
print_lookup_request(&lr); // perror("[Serialized]\n");
// print_lookup_request(&lr);
extract_lookup_request(&lr); extract_lookup_request(&lr);
perror("[Deserialized]\n");
print_lookup_request(&lr); print_lookup_request(&lr);
perror("----------------\n"); perror("----------------\n");
fprintf(stderr, "Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
// DO LOOKUP // DO LOOKUP
if (lr.message_type == 0) if (lr.message_type == 0)
{ {
@@ -485,37 +460,22 @@ static void handle_udp_request(int _sock){
lookup_request backfire = {0}; lookup_request backfire = {0};
if (lr.node_id == suc_id) const neighbor responsibility = check_neighborhood(lr.hash_id);
{ if (responsibility != res) {
// Send back 404 not found // is succ responsible
clone_lookup_request(&backfire, &lr); if (responsibility == res_303)
backfire.message_type = -1; {
} lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
else
{
const neighbor responsibility = check_neighborhood(lr.hash_id);
/* res is handles upfront by not res and suc responsibility check
if (responsibility == res) {
lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID);
} }
*/ else
if (responsibility == not_res) { {
// is succ responsible clone_lookup_request(&backfire, &lr);
if (lr.hash_id <= suc_id || (suc_id < _NODE_ID && lr.hash_id <= 65535))
{
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
}
else
{
// lookup_request_init(&backfire, 0, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
clone_lookup_request(&backfire, &lr);
callback_ip = &suc_ip_s.s_addr; callback_ip = &suc_ip_s.s_addr;
callback_port = _SUCC_PORT; callback_port = _SUCC_PORT;
}
} }
} }
call_network(&backfire, *callback_ip, callback_port); call_network(&backfire, *callback_ip, callback_port);
} }
elif (lr.message_type == 1) elif (lr.message_type == 1)
@@ -527,10 +487,6 @@ static void handle_udp_request(int _sock){
perror("PUSHED "); perror("PUSHED ");
print_lookup_request(toLookup); print_lookup_request(toLookup);
} }
else
{
// Message-Type: -1
}
} }
/** /**
@@ -611,8 +567,8 @@ int main(int argc, char **argv) {
elif(s == server_socket_udp){ elif(s == server_socket_udp){
// DO UDP STUFF // DO UDP STUFF
printf("Handling UDP request\n"); printf("Handling UDP request\n");
sockets[1].events = 0; handle_udp_request();
handle_udp_request(s); // sockets[1].events = 0;
} }
else { else {
assert(s == state.sock); assert(s == state.sock);