Fixing sending packets

This commit is contained in:
n07070 2020-04-27 17:22:41 +02:00
parent 723466fba8
commit 822b344a57
2 changed files with 48 additions and 26 deletions

View File

@ -36,10 +36,13 @@ int ask_for_peers(int socket_num) {
struct in6_addr ip = peer->ip;
int16_t port = peer->port;
int ifindex = if_nametoindex("eth0");
int ifindex = if_nametoindex("enp3s0");
if(ifindex == 0) {
perror("if_nametoindex failed");
return -1;
int ifindex = if_nametoindex("eth0");
if(ifindex == 0) {
perror("if_nametoindex failed");
return -1;
}
}
// Initialize sockaddr
@ -52,10 +55,20 @@ int ask_for_peers(int socket_num) {
// Send neighbour request TLV
tlv neighbour_req;
build_neighbour_req(&neighbour_req);
return send_single_tlv(&neighbour_req, &dest, socket_num);
neighbour_req.pad1 = NULL;
int rc = build_neighbour_req(&neighbour_req);
if (rc < 0) {
printf(">> Failed to build neighbour_req\n");
}
rc = send_single_tlv(&neighbour_req, &dest, socket_num);
if (rc < 0) {
printf(">> Error while sending a TLV.\n");
return -1;
} else {
printf(">> Send TLV.\n");
return 0;
}
}
}
@ -73,15 +86,16 @@ int len_list(list *l) {
// Get a random neighbour
neighbour_peer *get_random_neighbour() {
printf(">> Getting random peer...\n");
// Get a random number
time_t t;
srand((unsigned) time(NULL));
int n = rand() % len_list(neighbour_list);
printf("%i\n", n );
int n = (rand() % len_list(neighbour_list)) + 1;
int l_l = len_list(neighbour_list);
// Get nth neighbour
list *tmp = neighbour_list;
for(int i=0; i<n; i++) {
for(int i=1; i < n; i++) {
tmp = tmp->next;
}
@ -408,7 +422,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
// Send length bytes from packet
int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) {
// Vectorized buffer
struct iovec vec_buff = {.iov_len = length, .iov_base = packet_buff};
struct iovec vec_buff = {.iov_len = sizeof(packet_buff), .iov_base = packet_buff};
int error_while_sending = 0;
@ -422,17 +436,16 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
if (response_code < 0) {
// debug_print("Unable to send out the packet to peer %i", i);
printf(">> Unable to send out the packet to peer.\n");
error_while_sending = 1;
} else if (response_code < length) {
// debug_print("Sent out only part of the packet.");
printf(">> Sent out only part of the packet.\n");
error_while_sending = 1;
} else {
// debug_print("Send out packet to peer %i", i);
printf(">> Send out packet to peer\n");
}
if (error_while_sending == 1) {
// debug_print("Error occured while sending out a packet.");
return -1;
} else {
return 0;
@ -666,11 +679,13 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender,
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
memset(pack.body, 0, 1020);
int ifindex = if_nametoindex("eth0");
int ifindex = if_nametoindex("enp3s0");
if(ifindex == 0) {
perror("if_nametoindex failed");
return -1;
int ifindex = if_nametoindex("eth0");
if(ifindex == 0) {
perror("if_nametoindex failed");
return -1;
}
}
while(pos < packet_len) {
@ -985,7 +1000,7 @@ int run_node(int sock_fd){
break;
}
if (bytes > 0) {
printf("Received: %.*s\r", (int)bytes, output_buffer);
printf("Received %i bytes as : %s\n", (int)bytes, output_buffer);
// Treat incoming packets.
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
if (work_tlv_status < 0) {
@ -1027,20 +1042,24 @@ int bootstrap_node(int * sock_fd){
}
/* Make the first peer*/
struct neighbour_peer root_peer;
struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer));
time_t root_peer_seen = time(NULL);
int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer.ip);
int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer->ip);
if(inet_p < 1){
perror(">> Failed to create the root peer.");
return -3;
}
root_peer.port = 1212;
root_peer.is_temporary = 0;
root_peer.last_seen = root_peer_seen;
root_peer->port = 1212;
root_peer->is_temporary = 0;
root_peer->last_seen = root_peer_seen;
// TODO: Add the first peer to the neighbourhood
printf(">> Adding the first root peer to the list...\n");
neighbour_list = malloc(sizeof(struct list));
neighbour_list->data = (void *) root_peer;
neighbour_list->next = NULL;
printf(">> Boostraping done.\n");
return 0;

View File

@ -62,12 +62,15 @@ int build_padn(tlv *tlv, size_t len) {
int build_neighbour_req(tlv *tlv) {
// Free the previously allocated memory
free(tlv->pad1);
// if (tlv != NULL) {
// free(tlv->pad1);
// }
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
if(new == NULL)
if(new == NULL){
return -1;
}
new->type = 2;
new->length = 0;