diff --git a/src/node.c b/src/node.c index dff0c29..fbe2830 100644 --- a/src/node.c +++ b/src/node.c @@ -45,9 +45,63 @@ neighbour_peer *get_random_neighbour() { /* ---- Fin fonctions utilitaires ---- */ -// This function -int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size){ +int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int sock_num){ + 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 diff --git a/src/node.h b/src/node.h index 38fbc1b..87fe047 100644 --- a/src/node.h +++ b/src/node.h @@ -81,14 +81,14 @@ int send_packet(); * des paires. * 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 * des paires. Chaque pair recevera la même liste de TLV. * 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 void t_ask_for_more_peers(); diff --git a/src/tlv.c b/src/tlv.c index 2284815..e19cfd9 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -180,9 +180,9 @@ int build_warning(tlv *tlv, char *message) { new->type = 9; new->length = len; - memcpy(new->message, message, len); + memcpy(new->message, message, len); tlv->warning = new; return 0; -} \ No newline at end of file +}