P1 Tests run, P2 test 9/16

This commit is contained in:
t
2025-01-07 20:40:02 +01:00
parent 34b8083b1c
commit 9f28d3eb0d
+68 -13
View File
@@ -1,5 +1,6 @@
#include <arpa/inet.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
@@ -20,18 +21,27 @@
// Getter for environmental DHT sets
#define _PRED_ID atoi(getenv("PRED_ID"))
#define _PRED_IP getenv("PRED_IP"))
#define _PRED_IP getenv("PRED_IP")
#define _PRED_PORT atoi(getenv("PRED_PORT"))
#define _SUCC_ID atoi(getenv("SUCC_ID"))
#define _SUCC_IP getenv("SUCC_IP"))
#define _SUCC_IP getenv("SUCC_IP")
#define _SUCC_PORT atoi(getenv("SUCC_PORT"))
bool _DO_UDP = false;
int _NODE_ID;
const char *_NODE_IP;
const char *_NODE_PORT;
#define MAX_RESOURCES 100
typedef enum neighbor{
not_res = 0,
res = 1,
dunno = 2,
}neighbor;
struct tuple resources[MAX_RESOURCES] = {
{"/static/foo", "Foo", sizeof "Foo" - 1},
{"/static/bar", "Bar", sizeof "Bar" - 1},
@@ -318,6 +328,39 @@ static int setup_server_socket_udp(struct sockaddr_in addr){
return sock;
}
neighbor check_neighborhood(uint16_t hash) {
int pre_id = _PRED_ID;
int id = _NODE_ID;
int suc_id = _SUCC_ID;
if (pre_id == id && suc_id == id) {
return res;
}
if (id < pre_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;
}
} else {
if (hash > id || hash <= suc_id) {
return not_res;
}
}
return dunno;
}
static void handle_udp_request(int _sock){
size_t received_bytes = 0;
char buffer[HTTP_MAX_SIZE] = {0};
@@ -325,6 +368,8 @@ static void handle_udp_request(int _sock){
struct sockaddr_storage caller_addr;
socklen_t addrlen = sizeof caller_addr;
int sock_bak = _sock;
if((received_bytes = recvfrom(_sock, buffer, HTTP_MAX_SIZE, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
perror("Func: handle_udp_request - failed to receive data from caller!");
exit(EXIT_FAILURE);
@@ -332,26 +377,36 @@ static void handle_udp_request(int _sock){
else{
// HASH Received
uint16_t hash = pseudo_hash((uint8_t*)buffer, strlen(buffer));
printf("Hash: %u\n", hash);
// Check belonging
int pre_id = _PRED_ID;
int suc_id = _SUCC_ID;
if ((pre_id < hash && hash <= _NODE_ID) || (pre_id > _NODE_ID && (hash > pre_id || hash <= _NODE_ID))) {
neighbor responsibility = check_neighborhood(hash);
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 data to caller!");
perror("Func: handle_udp_request - failed to send 200 data to caller!");
exit(EXIT_FAILURE);
}
}
else{
else if (responsibility == not_res) {
// TODO: 303 - See other
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 data to caller!");
perror("Func: handle_udp_request - failed to send 303 data to caller!");
exit(EXIT_FAILURE);
}
}
else {
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)
{
perror("Func: handle_udp_request - failed to send 503 data to caller!");
exit(EXIT_FAILURE);
}
//call_other_dht();
}
}
}
@@ -381,7 +436,7 @@ int main(int argc, char **argv) {
int server_socket_udp = setup_server_socket_udp(addr);
// Create an array of pollfd structures to monitor sockets.
struct pollfd sockets[2] = {
struct pollfd sockets[3] = {
{.fd = server_socket, .events = POLLIN},
{.fd = server_socket_udp, .events = POLLIN}
};
@@ -419,15 +474,15 @@ int main(int argc, char **argv) {
// limit to one connection at a time
sockets[0].events = 0;
sockets[1].fd = connection;
sockets[1].events = POLLIN;
sockets[2].fd = connection;
sockets[2].events = POLLIN;
}
}
elif(s == server_socket_udp){
// DO UDP STUFF
sockets[1].revents = 0;
handle_udp_request(s);
}
else {
assert(s == state.sock);
@@ -436,8 +491,8 @@ int main(int argc, char **argv) {
bool cont = handle_connection(&state);
if (!cont) { // get ready for a new connection
sockets[0].events = POLLIN;
sockets[1].fd = -1;
sockets[1].events = 0;
sockets[2].fd = -1;
sockets[2].events = 0;
}
}
}