Hash is faulty

This commit is contained in:
t
2025-01-10 13:40:12 +01:00
parent c6c2d3c299
commit bb9c2d1d4c
3 changed files with 87 additions and 60 deletions
+26 -28
View File
@@ -100,7 +100,10 @@ void send_reply(int conn, struct request *request) {
}
else
{
// TODO: Success
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");
}
}
@@ -132,9 +135,9 @@ void send_reply(int conn, struct request *request) {
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);
lookup_request_init(&lookreq, 0, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, resource_hash);
call_network(&lookreq, _SUCC_IP, _SUCC_PORT);
call_network(&lookreq, inet_addr(_SUCC_IP), _SUCC_PORT);
}
else {
printf("404\n");
@@ -412,29 +415,17 @@ neighbor check_neighborhood(uint16_t hash) {
static void handle_udp_request(int _sock){
size_t received_bytes = 0;
char buffer[HTTP_MAX_SIZE] = {0};
lookup_request lr = {0};
struct sockaddr_storage caller_addr;
socklen_t addrlen = sizeof caller_addr;
// int sock_bak = _sock;
char *b = buffer;
size_t space_left = HTTP_MAX_SIZE - 1;
while(!strstr(buffer, "\r\n\r\n")){
b += received_bytes;
space_left -= received_bytes;
if((received_bytes = recvfrom(_sock, b, space_left, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
perror("Func: handle_udp_request - failed to receive data from caller!");
exit(EXIT_FAILURE);
}
if((received_bytes = recvfrom(_sock, &lr, sizeof(lookup_request), 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
perror("Func: handle_udp_request - failed to receive data from caller!");
exit(EXIT_FAILURE);
}
// HASH Received
// TODO: alter buffer
lookup_request lr = {0};
extract_lookup_request(&lr, buffer);
extract_lookup_request(&lr);
printf("Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
@@ -443,7 +434,13 @@ static void handle_udp_request(int _sock){
{
// Check belonging
const int suc_id = _SUCC_ID;
char *callback_ip = lr.node_ip;
in_addr_t suc_ip = inet_addr(_SUCC_IP);
struct in_addr suc_ip_s;
suc_ip_s.s_addr = suc_ip;
in_addr_t *callback_ip = &lr.node_ip.s_addr;
int callback_port = lr.node_port;
lookup_request backfire = {0};
@@ -459,23 +456,24 @@ static void handle_udp_request(int _sock){
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);
lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID);
}
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);
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
}
else
{
lookup_request_init(&backfire, 1, suc_id, _SUCC_IP, _SUCC_PORT, _NODE_ID, lr.node_request);
callback_ip = _SUCC_IP; // TODO: Check validity
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
callback_ip = &suc_ip_s.s_addr;
callback_port = _SUCC_PORT;
}
}
}
call_network(&backfire, callback_ip, callback_port);
call_network(&backfire, *callback_ip, callback_port);
}
elif (lr.message_type == 1)
{
@@ -486,7 +484,7 @@ static void handle_udp_request(int _sock){
}
else
{
// Message-Type: -1
}
}
@@ -515,7 +513,7 @@ int main(int argc, char **argv) {
// Set up a server socket.
int server_socket = setup_server_socket(addr);
int server_socket_udp = setup_server_socket_udp(addr);
server_socket_udp = setup_server_socket_udp(addr);
// Create an array of pollfd structures to monitor sockets.
struct pollfd sockets[3] = {