Only one test is left to be squashed
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
// #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++
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ void build_lookup_request(lookup_request* lr){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
void lookup_request_init(lookup_request* lr, uint8_t message_type, uint16_t node_id, in_addr_t node_ip, uint16_t 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;
|
||||||
@@ -43,8 +44,8 @@ int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){
|
|||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
|
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port);
|
|
||||||
addr.sin_addr.s_addr = htonl(ip);
|
addr.sin_addr.s_addr = htonl(ip);
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
@@ -55,7 +56,7 @@ int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){
|
|||||||
|
|
||||||
char ip_str[INET_ADDRSTRLEN];
|
char ip_str[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
inet_ntop(AF_INET, &ip, ip_str, INET_ADDRSTRLEN);
|
inet_ntop(AF_INET, &addr.sin_addr, ip_str, INET_ADDRSTRLEN);
|
||||||
|
|
||||||
if ((rv = getaddrinfo(ip_str, port_str, &hints, &servinfo)) != 0) {
|
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));
|
||||||
@@ -65,41 +66,21 @@ 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);
|
// lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr);
|
||||||
// lr->node_port = ntohs(addr.sin_port);
|
// lr->node_port = ntohs(addr.sin_port);
|
||||||
|
|
||||||
/*
|
perror("---CALL_NETWORK---\n");
|
||||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
print_lookup_request(lr);
|
||||||
if ((succ_sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
|
||||||
perror("Func: call_network - socket");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p == NULL) {
|
|
||||||
fprintf(stderr, "Func: call_network - failed to create socket\n");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
build_lookup_request(lr);
|
build_lookup_request(lr);
|
||||||
|
print_lookup_request(lr);
|
||||||
|
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) {
|
||||||
perror("Func: call_network - sendto");
|
fprintf(stderr, "Func: call_network - sendto failed with errno %d: %s\n", errno, strerror(errno));
|
||||||
|
fprintf(stderr, "Request:\n\tIP:\t%s\n\tPORT:\t%d\n", ip_str, ntohs(addr.sin_port));
|
||||||
|
fprintf(stderr, "\tUDP_SOCKET: %d\n", server_socket_udp);
|
||||||
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);
|
|
||||||
// close(succ_sock);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -107,3 +88,7 @@ int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){
|
|||||||
void clone_lookup_request(lookup_request* dest, lookup_request* source){
|
void clone_lookup_request(lookup_request* dest, lookup_request* source){
|
||||||
memcpy(dest, source, sizeof(lookup_request));
|
memcpy(dest, source, sizeof(lookup_request));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ typedef struct __attribute__((packed)){
|
|||||||
uint16_t node_port;
|
uint16_t node_port;
|
||||||
}lookup_request;
|
}lookup_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 lookup_request_init(lookup_request*, uint8_t message_type, uint16_t node_id, in_addr_t node_ip, uint16_t node_port, uint16_t hash_id);
|
||||||
void build_lookup_request(lookup_request*);
|
void build_lookup_request(lookup_request*);
|
||||||
|
|
||||||
void extract_lookup_request(lookup_request*);
|
void extract_lookup_request(lookup_request*);
|
||||||
@@ -25,4 +25,6 @@ int call_network(lookup_request*, in_addr_t, uint16_t);
|
|||||||
|
|
||||||
void clone_lookup_request(lookup_request* dest, lookup_request* source);
|
void clone_lookup_request(lookup_request* dest, lookup_request* source);
|
||||||
|
|
||||||
|
void print_lookup_request(lookup_request*);
|
||||||
|
|
||||||
#endif //DHT_H
|
#endif //DHT_H
|
||||||
|
|||||||
@@ -35,8 +35,12 @@ void push_lookup(lookup_request *request)
|
|||||||
lookup_request *find_lookup(const uint16_t hash_id)
|
lookup_request *find_lookup(const uint16_t hash_id)
|
||||||
{
|
{
|
||||||
const lookup_table *current = table->head;
|
const lookup_table *current = table->head;
|
||||||
while (current != NULL && current->request->hash_id != hash_id)
|
|
||||||
|
while (current != NULL){
|
||||||
|
if((current->request->node_id < current->request->hash_id && (hash_id > current->request->hash_id && hash_id < 65535)) || hash_id <= current->request->node_id || (hash_id > current->request->hash_id && hash_id <= current->request->node_id))
|
||||||
|
break;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
}
|
||||||
return current == NULL ? NULL : current->request;
|
return current == NULL ? NULL : current->request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+40
-16
@@ -54,13 +54,20 @@ 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 static_[] = "/static/";
|
||||||
|
|
||||||
|
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic);
|
||||||
|
elif(strstr(uri, static_)) uri_ptr += strlen(static_);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
char *it = (char *)uri;
|
char *it = (char*)uri;
|
||||||
|
|
||||||
// shift uri_ptr to last '/'
|
// shift uri_ptr to last '/'
|
||||||
while(*it)
|
while(*it)
|
||||||
{
|
{
|
||||||
if (*it == '/') uri_ptr = it++;
|
if (*it == '/') uri_ptr = it;
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +101,7 @@ void send_reply(int conn, struct request *request) {
|
|||||||
// 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 = get(request->uri, resources, MAX_RESOURCES, &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);
|
uint16_t resource_hash = hash_uri(request->uri);
|
||||||
|
|
||||||
fprintf(stderr, "Resource hash: %u\n", resource_hash);
|
fprintf(stderr, "Resource hash: %u\n", resource_hash);
|
||||||
@@ -120,12 +127,19 @@ void send_reply(int conn, struct request *request) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO: Success
|
// DONE: 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);
|
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);
|
offset = strlen(reply);
|
||||||
printf("303\n");
|
printf("303\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
|
||||||
if (responsible == res) {
|
if (responsible == res) {
|
||||||
printf("Responsible - 404\n");
|
printf("Responsible - 404\n");
|
||||||
@@ -153,17 +167,18 @@ void send_reply(int conn, struct request *request) {
|
|||||||
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
|
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
|
||||||
offset = strlen(reply);
|
offset = strlen(reply);
|
||||||
printf("503\n");
|
printf("503\n");
|
||||||
// TODO: Call other dht
|
|
||||||
lookup_request lookreq;
|
|
||||||
lookup_request_init(&lookreq, 0, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, resource_hash);
|
|
||||||
|
|
||||||
call_network(&lookreq, inet_addr(_SUCC_IP), _SUCC_PORT);
|
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 {
|
else {
|
||||||
printf("404\n");
|
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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} 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
|
||||||
// 'resources' array.
|
// 'resources' array.
|
||||||
@@ -424,7 +439,6 @@ neighbor check_neighborhood(uint16_t hash) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check correctness
|
|
||||||
if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= 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;
|
||||||
|
|
||||||
@@ -436,6 +450,7 @@ neighbor check_neighborhood(uint16_t hash) {
|
|||||||
|
|
||||||
|
|
||||||
static void handle_udp_request(int _sock){
|
static void handle_udp_request(int _sock){
|
||||||
|
fprintf(stderr, "---%d---\n", _NODE_ID);
|
||||||
size_t received_bytes = 0;
|
size_t received_bytes = 0;
|
||||||
lookup_request lr = {0};
|
lookup_request lr = {0};
|
||||||
|
|
||||||
@@ -446,10 +461,13 @@ static void handle_udp_request(int _sock){
|
|||||||
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");
|
||||||
|
print_lookup_request(&lr);
|
||||||
extract_lookup_request(&lr);
|
extract_lookup_request(&lr);
|
||||||
|
print_lookup_request(&lr);
|
||||||
|
perror("----------------\n");
|
||||||
|
|
||||||
printf("Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
|
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)
|
||||||
@@ -457,7 +475,7 @@ static void handle_udp_request(int _sock){
|
|||||||
// Check belonging
|
// Check belonging
|
||||||
const int suc_id = _SUCC_ID;
|
const int suc_id = _SUCC_ID;
|
||||||
|
|
||||||
in_addr_t suc_ip = inet_addr(_SUCC_IP);
|
in_addr_t suc_ip = htonl(inet_addr(_SUCC_IP));
|
||||||
|
|
||||||
struct in_addr suc_ip_s;
|
struct in_addr suc_ip_s;
|
||||||
suc_ip_s.s_addr = suc_ip;
|
suc_ip_s.s_addr = suc_ip;
|
||||||
@@ -484,7 +502,7 @@ static void handle_udp_request(int _sock){
|
|||||||
*/
|
*/
|
||||||
if (responsibility == not_res) {
|
if (responsibility == not_res) {
|
||||||
// is succ responsible
|
// is succ responsible
|
||||||
if (lr.hash_id <= suc_id)
|
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);
|
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
|
||||||
}
|
}
|
||||||
@@ -498,7 +516,7 @@ static void handle_udp_request(int _sock){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//call_network(&backfire, *callback_ip, callback_port);
|
call_network(&backfire, *callback_ip, callback_port);
|
||||||
}
|
}
|
||||||
elif (lr.message_type == 1)
|
elif (lr.message_type == 1)
|
||||||
{
|
{
|
||||||
@@ -506,6 +524,8 @@ static void handle_udp_request(int _sock){
|
|||||||
clone_lookup_request(toLookup, &lr);
|
clone_lookup_request(toLookup, &lr);
|
||||||
|
|
||||||
push_lookup(toLookup);
|
push_lookup(toLookup);
|
||||||
|
perror("PUSHED ");
|
||||||
|
print_lookup_request(toLookup);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -526,7 +546,11 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init_lookup_table();
|
init_lookup_table();
|
||||||
|
/*
|
||||||
|
lookup_request toLookup1;
|
||||||
|
lookup_request_init(&toLookup1, 1, 65535, ntohl(inet_addr("127.0.0.1")), 4710, 1);
|
||||||
|
push_lookup(&toLookup1);
|
||||||
|
*/
|
||||||
if(argc == 3) _NODE_ID = 0;
|
if(argc == 3) _NODE_ID = 0;
|
||||||
elif(argc == 4) _NODE_ID = atoi(argv[3]);
|
elif(argc == 4) _NODE_ID = atoi(argv[3]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user