Only one test is left to be squashed

This commit is contained in:
Ano-sys
2025-01-11 03:09:29 +01:00
parent 92581b3d4b
commit 4d6ea5c51f
4 changed files with 92 additions and 77 deletions
+16 -31
View File
@@ -7,6 +7,7 @@
#include <sys/socket.h>
#include <sys/types.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++
@@ -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->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;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(ip);
addr.sin_port = htons(port);
memset(&hints, 0, sizeof hints);
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];
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) {
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);
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);
/*
for(p = servinfo; p != NULL; p = p->ai_next) {
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;
}
*/
perror("---CALL_NETWORK---\n");
print_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) {
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);
}
/*
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;
}
@@ -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){
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);
}