packet length
This commit is contained in:
commit
f429df8c18
70
src/node.c
70
src/node.c
@ -36,10 +36,13 @@ int ask_for_peers(int socket_num) {
|
|||||||
struct in6_addr ip = peer->ip;
|
struct in6_addr ip = peer->ip;
|
||||||
int16_t port = peer->port;
|
int16_t port = peer->port;
|
||||||
|
|
||||||
int ifindex = if_nametoindex("eth0");
|
int ifindex = if_nametoindex("enp3s0");
|
||||||
if(ifindex == 0) {
|
if(ifindex == 0) {
|
||||||
perror("if_nametoindex failed");
|
int ifindex = if_nametoindex("eth0");
|
||||||
return -1;
|
if(ifindex == 0) {
|
||||||
|
perror("if_nametoindex failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize sockaddr
|
// Initialize sockaddr
|
||||||
@ -52,11 +55,20 @@ int ask_for_peers(int socket_num) {
|
|||||||
|
|
||||||
// Send neighbour request TLV
|
// Send neighbour request TLV
|
||||||
tlv neighbour_req;
|
tlv neighbour_req;
|
||||||
tlv.pad1 = NULL;
|
neighbour_req.pad1 = NULL;
|
||||||
build_neighbour_req(&neighbour_req);
|
int rc = build_neighbour_req(&neighbour_req);
|
||||||
|
if (rc < 0) {
|
||||||
return send_single_tlv(&neighbour_req, &dest, socket_num);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,15 +86,16 @@ int len_list(list *l) {
|
|||||||
|
|
||||||
// Get a random neighbour
|
// Get a random neighbour
|
||||||
neighbour_peer *get_random_neighbour() {
|
neighbour_peer *get_random_neighbour() {
|
||||||
|
printf(">> Getting random peer...\n");
|
||||||
// Get a random number
|
// Get a random number
|
||||||
time_t t;
|
time_t t;
|
||||||
srand((unsigned) time(NULL));
|
srand((unsigned) time(NULL));
|
||||||
int n = rand() % len_list(neighbour_list);
|
int n = (rand() % len_list(neighbour_list)) + 1;
|
||||||
printf("%i\n", n );
|
int l_l = len_list(neighbour_list);
|
||||||
// Get nth neighbour
|
// Get nth neighbour
|
||||||
list *tmp = neighbour_list;
|
list *tmp = neighbour_list;
|
||||||
|
|
||||||
for(int i=0; i<n; i++) {
|
for(int i=1; i < n; i++) {
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -409,7 +422,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
// Send length bytes from packet
|
// Send length bytes from packet
|
||||||
int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) {
|
int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) {
|
||||||
// Vectorized buffer
|
// Vectorized buffer
|
||||||
struct iovec vec_buff = {.iov_len = length, .iov_base = packet_buff};
|
struct iovec vec_buff = {.iov_len = length + 4, .iov_base = packet_buff};
|
||||||
|
|
||||||
int error_while_sending = 0;
|
int error_while_sending = 0;
|
||||||
|
|
||||||
@ -423,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);
|
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
||||||
if (response_code < 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;
|
error_while_sending = 1;
|
||||||
} else if (response_code < length) {
|
} 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;
|
error_while_sending = 1;
|
||||||
} else {
|
} else {
|
||||||
// debug_print("Send out packet to peer %i", i);
|
printf(">> Send out packet to peer\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error_while_sending == 1) {
|
if (error_while_sending == 1) {
|
||||||
// debug_print("Error occured while sending out a packet.");
|
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@ -444,7 +456,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in
|
|||||||
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
||||||
char type = tlv->pad1->type;
|
char type = tlv->pad1->type;
|
||||||
unsigned char len;
|
unsigned char len;
|
||||||
packet pack = (packet) {.magic = 95, .version = 1, .length = 4};
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
memset(pack.body, 0, 1020);
|
memset(pack.body, 0, 1020);
|
||||||
|
|
||||||
// Copy data from tlv into body
|
// Copy data from tlv into body
|
||||||
@ -667,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};
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
memset(pack.body, 0, 1020);
|
memset(pack.body, 0, 1020);
|
||||||
|
|
||||||
int ifindex = if_nametoindex("eth0");
|
int ifindex = if_nametoindex("enp3s0");
|
||||||
|
|
||||||
if(ifindex == 0) {
|
if(ifindex == 0) {
|
||||||
perror("if_nametoindex failed");
|
int ifindex = if_nametoindex("eth0");
|
||||||
return -1;
|
if(ifindex == 0) {
|
||||||
|
perror("if_nametoindex failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while(pos < packet_len) {
|
while(pos < packet_len) {
|
||||||
@ -986,7 +1000,7 @@ int run_node(int sock_fd){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (bytes > 0) {
|
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.
|
// Treat incoming packets.
|
||||||
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
|
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
|
||||||
if (work_tlv_status < 0) {
|
if (work_tlv_status < 0) {
|
||||||
@ -1028,20 +1042,24 @@ int bootstrap_node(int * sock_fd){
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Make the first peer*/
|
/* 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);
|
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){
|
if(inet_p < 1){
|
||||||
perror(">> Failed to create the root peer.");
|
perror(">> Failed to create the root peer.");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
root_peer.port = 1212;
|
root_peer->port = 1212;
|
||||||
root_peer.is_temporary = 0;
|
root_peer->is_temporary = 0;
|
||||||
root_peer.last_seen = root_peer_seen;
|
root_peer->last_seen = root_peer_seen;
|
||||||
|
|
||||||
// TODO: Add the first peer to the neighbourhood
|
// 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");
|
printf(">> Boostraping done.\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -62,12 +62,15 @@ int build_padn(tlv *tlv, size_t len) {
|
|||||||
|
|
||||||
int build_neighbour_req(tlv *tlv) {
|
int build_neighbour_req(tlv *tlv) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
free(tlv->pad1);
|
// if (tlv != NULL) {
|
||||||
|
// free(tlv->pad1);
|
||||||
|
// }
|
||||||
|
|
||||||
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
|
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
|
||||||
|
|
||||||
if(new == NULL)
|
if(new == NULL){
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
new->type = 2;
|
new->type = 2;
|
||||||
new->length = 0;
|
new->length = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user