Hash is faulty
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -9,36 +10,41 @@
|
|||||||
|
|
||||||
// #define cpy(x, y, z) char *x_c = x; char *y_c = y; while(*y_c && z--) *x_c++ = *y_c++
|
// #define cpy(x, y, z) char *x_c = x; char *y_c = y; while(*y_c && z--) *x_c++ = *y_c++
|
||||||
|
|
||||||
|
int server_socket_udp;
|
||||||
|
|
||||||
void build_lookup_request(lookup_request* lr){
|
void build_lookup_request(lookup_request* lr){
|
||||||
char *lrreq = lr->node_request;
|
lr->hash_id = ntohs(lr->hash_id);
|
||||||
snprintf(lr->node_request, HTTP_MAX_SIZE, "%d|%hd|%d|%s|%d\r\n\r\n", lr->message_type, htons(lr->hash_id), lr->node_id, lr->node_ip, lr->node_port);
|
lr->node_id = ntohs(lr->node_id);
|
||||||
|
lr->node_ip.s_addr = ntohl(lr->node_ip.s_addr);
|
||||||
|
lr->node_port = ntohs(lr->node_port);
|
||||||
|
// snprintf(lr->node_request, HTTP_MAX_SIZE, "%d|%hd|%d|%u|%d\r\n\r\n", lr->message_type, ntohs(lr->hash_id), ntohs(lr->node_id), ntohl(ip.s_addr), ntohs(lr->node_port));
|
||||||
}
|
}
|
||||||
|
|
||||||
void lookup_request_init(lookup_request* lr, int message_type, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request)
|
|
||||||
|
|
||||||
|
void lookup_request_init(lookup_request* lr, int message_type, int node_id, in_addr_t node_ip, int node_port, uint16_t hash_id)
|
||||||
{
|
{
|
||||||
lr->message_type = message_type;
|
lr->message_type = message_type;
|
||||||
lr->node_id = node_id;
|
lr->node_id = node_id;
|
||||||
strncpy(lr->node_ip, node_ip, NODE_IP_MAX_SIZE);
|
lr->node_ip.s_addr = node_ip;
|
||||||
lr->node_port = node_port;
|
lr->node_port = node_port;
|
||||||
lr->hash_id = hash_id;
|
lr->hash_id = hash_id;
|
||||||
if(node_request != NULL)
|
}
|
||||||
strncpy(lr->node_request, node_request, HTTP_MAX_SIZE);
|
|
||||||
|
void extract_lookup_request(lookup_request* lr){
|
||||||
build_lookup_request(lr);
|
build_lookup_request(lr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void extract_lookup_request(lookup_request* lr, char* request){
|
int call_network(lookup_request* lr, in_addr_t ip, int port){
|
||||||
sscanf(request, "%d|%hd|%d|%s|%d\r\n\r\n", &lr->message_type, &lr->hash_id, &lr->node_id, lr->node_ip, &lr->node_port);
|
// int succ_sock;
|
||||||
// lr->message_type = ntohs(lr->message_type);
|
struct addrinfo hints, *servinfo; // *p;
|
||||||
lr->hash_id = ntohs(lr->hash_id);
|
|
||||||
// lr->node_id = ntohs(lr->node_id);
|
|
||||||
// lr->node_port = ntohs(lr->node_port);
|
|
||||||
}
|
|
||||||
|
|
||||||
int call_network(lookup_request* lr, char *ip, int port){
|
|
||||||
int succ_sock;
|
|
||||||
struct addrinfo hints, *servinfo, *p;
|
|
||||||
int rv;
|
int rv;
|
||||||
int numbytes;
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
addr.sin_addr.s_addr = htonl(ip);
|
||||||
|
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
@@ -47,11 +53,22 @@ int call_network(lookup_request* lr, char *ip, int port){
|
|||||||
char port_str[6];
|
char port_str[6];
|
||||||
snprintf(port_str, sizeof port_str, "%d", port);
|
snprintf(port_str, sizeof port_str, "%d", port);
|
||||||
|
|
||||||
if ((rv = getaddrinfo(ip, port_str, &hints, &servinfo)) != 0) {
|
char ip_str[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &ip, ip_str, INET_ADDRSTRLEN);
|
||||||
|
|
||||||
|
if ((rv = getaddrinfo(ip_str, port_str, &hints, &servinfo)) != 0) {
|
||||||
fprintf(stderr, "Func: call_network - getaddrinfo: %s\n", gai_strerror(rv));
|
fprintf(stderr, "Func: call_network - getaddrinfo: %s\n", gai_strerror(rv));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addr = *((struct sockaddr_in*) servinfo->ai_addr);
|
||||||
|
freeaddrinfo(servinfo);
|
||||||
|
|
||||||
|
lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr);
|
||||||
|
lr->node_port = ntohs(addr.sin_port);
|
||||||
|
|
||||||
|
/*
|
||||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
if ((succ_sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
if ((succ_sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
||||||
perror("Func: call_network - socket");
|
perror("Func: call_network - socket");
|
||||||
@@ -64,14 +81,25 @@ int call_network(lookup_request* lr, char *ip, int port){
|
|||||||
fprintf(stderr, "Func: call_network - failed to create socket\n");
|
fprintf(stderr, "Func: call_network - failed to create socket\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if ((numbytes = sendto(succ_sock, lr->node_request, strlen(lr->node_request), 0, p->ai_addr, p->ai_addrlen)) == -1) {
|
build_lookup_request(lr);
|
||||||
|
|
||||||
|
if (sendto(server_socket_udp, lr, sizeof(lookup_request), 0, (struct sockaddr*) &addr, sizeof(struct sockaddr_in)) == -1) {
|
||||||
perror("Func: call_network - sendto");
|
perror("Func: call_network - sendto");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
size_t addrlen = sizeof(addr);
|
||||||
|
if (sendto(server_socket_udp, lr, sizeof(lr), 0, (struct sockaddr*)&addr, addrlen) == -1)
|
||||||
|
{
|
||||||
|
perror("Func: call_network - sendto");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
freeaddrinfo(servinfo);
|
// freeaddrinfo(servinfo);
|
||||||
close(succ_sock);
|
// close(succ_sock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,27 @@
|
|||||||
#ifndef DHT_H
|
#ifndef DHT_H
|
||||||
#define DHT_H
|
#define DHT_H
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
|
|
||||||
#define NODE_IP_MAX_SIZE 16
|
#define NODE_IP_MAX_SIZE 16
|
||||||
|
|
||||||
typedef struct{
|
extern int server_socket_udp;
|
||||||
int message_type;
|
|
||||||
int node_id;
|
|
||||||
char node_ip[NODE_IP_MAX_SIZE];
|
|
||||||
int node_port;
|
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)){
|
||||||
|
uint8_t message_type;
|
||||||
uint16_t hash_id;
|
uint16_t hash_id;
|
||||||
|
uint16_t node_id;
|
||||||
char node_request[HTTP_MAX_SIZE];
|
struct in_addr node_ip;
|
||||||
|
uint16_t node_port;
|
||||||
}lookup_request;
|
}lookup_request;
|
||||||
|
|
||||||
void lookup_request_init(lookup_request*, int message_type, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request);
|
void lookup_request_init(lookup_request*, int message_type, int node_id, in_addr_t node_ip, int node_port, uint16_t hash_id);
|
||||||
void build_lookup_request(lookup_request*);
|
void build_lookup_request(lookup_request*);
|
||||||
|
|
||||||
void extract_lookup_request(lookup_request*, char*);
|
void extract_lookup_request(lookup_request*);
|
||||||
|
|
||||||
int call_network(lookup_request*, char*, int);
|
int call_network(lookup_request*, in_addr_t, int);
|
||||||
|
|
||||||
void clone_lookup_request(lookup_request* dest, lookup_request* source);
|
void clone_lookup_request(lookup_request* dest, lookup_request* source);
|
||||||
|
|
||||||
|
|||||||
+26
-28
@@ -100,7 +100,10 @@ void send_reply(int conn, struct request *request) {
|
|||||||
}
|
}
|
||||||
else
|
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");
|
printf("503\n");
|
||||||
// TODO: Call other dht
|
// TODO: Call other dht
|
||||||
lookup_request lookreq;
|
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 {
|
else {
|
||||||
printf("404\n");
|
printf("404\n");
|
||||||
@@ -412,29 +415,17 @@ neighbor check_neighborhood(uint16_t hash) {
|
|||||||
|
|
||||||
static void handle_udp_request(int _sock){
|
static void handle_udp_request(int _sock){
|
||||||
size_t received_bytes = 0;
|
size_t received_bytes = 0;
|
||||||
char buffer[HTTP_MAX_SIZE] = {0};
|
lookup_request lr = {0};
|
||||||
|
|
||||||
struct sockaddr_storage caller_addr;
|
struct sockaddr_storage caller_addr;
|
||||||
socklen_t addrlen = sizeof caller_addr;
|
socklen_t addrlen = sizeof caller_addr;
|
||||||
|
|
||||||
// int sock_bak = _sock;
|
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!");
|
||||||
char *b = buffer;
|
exit(EXIT_FAILURE);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HASH Received
|
extract_lookup_request(&lr);
|
||||||
// TODO: alter buffer
|
|
||||||
lookup_request lr = {0};
|
|
||||||
extract_lookup_request(&lr, buffer);
|
|
||||||
|
|
||||||
printf("Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
|
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
|
// Check belonging
|
||||||
const int suc_id = _SUCC_ID;
|
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;
|
int callback_port = lr.node_port;
|
||||||
|
|
||||||
lookup_request backfire = {0};
|
lookup_request backfire = {0};
|
||||||
@@ -459,23 +456,24 @@ static void handle_udp_request(int _sock){
|
|||||||
const neighbor responsibility = check_neighborhood(lr.hash_id);
|
const neighbor responsibility = check_neighborhood(lr.hash_id);
|
||||||
|
|
||||||
if (responsibility == res) {
|
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) {
|
elif (responsibility == not_res) {
|
||||||
// is succ responsible
|
// is succ responsible
|
||||||
if (lr.hash_id <= suc_id)
|
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
|
else
|
||||||
{
|
{
|
||||||
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);
|
||||||
callback_ip = _SUCC_IP; // TODO: Check validity
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
@@ -486,7 +484,7 @@ static void handle_udp_request(int _sock){
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Message-Type: -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,7 +513,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
// Set up a server socket.
|
// Set up a server socket.
|
||||||
int server_socket = setup_server_socket(addr);
|
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.
|
// Create an array of pollfd structures to monitor sockets.
|
||||||
struct pollfd sockets[3] = {
|
struct pollfd sockets[3] = {
|
||||||
|
|||||||
Reference in New Issue
Block a user