Ajout de la fonction d'envoie de tlv

This commit is contained in:
n07070 2020-04-01 18:43:53 +02:00
parent e1276448f1
commit b96655da72
3 changed files with 60 additions and 6 deletions

View File

@ -45,9 +45,63 @@ neighbour_peer *get_random_neighbour() {
/* ---- Fin fonctions utilitaires ---- */ /* ---- Fin fonctions utilitaires ---- */
// This function int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int sock_num){
int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size){ debug_print("Building packet to send a TLV.");
// We first need to build the packet,
char packet_buff[1024];
struct packet pack;
pack.magic = 95;
pack.version = 1;
if(sizeof(tlv_to_send) > 1020){
debug_print("Unable to send TLV, the size is bigger than 1020 bits.");
return -1;
} else {
pack.length = sizeof(tlv_to_send);
strcpy(pack.body, (char) tlv_to_send);
}
// Casting the struct to a buffer.
packet_buff = (char *) pack;
debug_print("Packet has been built.");
// Vectorized buffer
struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff };
int error_while_sending = 0;
// For every dest
for (size_t i = 0; i < dest_list_size; i++) {
// Creating the struct to send out with sendmsg
struct msghdr packet_tlv_send_out = {
.msg_name = &dest_list[i],
.msg_namelen = sizeof(dest_list[i]),
.msg_iov = &vec_buff,
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
};
response_code = sendmsg((int) sock_num, &packet_tlv_send_out, 0);
if (response_code < 0) {
debug_print("Unable to send out the packet to peer %i", i);
error_while_sending = 1;
continue;
} else if (response_code < sizeof(packet_tlv_send_out)) {
debug_print("Sent out only part of the packet.");
error_while_sending = 1;
continue;
} else {
debug_print("Send out packet to peer %i", i);
}
}
if (error_while_sending == 1) {
debug_print("Error occured while sending out a packet.");
return -1;
} else {
return 0;
}
} }
// We need to make sure the TLV announces a length that will no go onto // We need to make sure the TLV announces a length that will no go onto

View File

@ -81,14 +81,14 @@ int send_packet();
* des paires. * des paires.
* Retourne -1 en cas d'échec, 0 en cas de succès. * Retourne -1 en cas d'échec, 0 en cas de succès.
*/ */
int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
/* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste /* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste
* des paires. Chaque pair recevera la même liste de TLV. * des paires. Chaque pair recevera la même liste de TLV.
* Retourne -1 en cas d'échec, 0 en cas de succès. * Retourne -1 en cas d'échec, 0 en cas de succès.
*/ */
int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
// threaded functions // threaded functions
void t_ask_for_more_peers(); void t_ask_for_more_peers();