Skip to content
Extraits de code Groupes Projets
Valider 1f897dd6 rédigé par Vany Ingenzi's avatar Vany Ingenzi
Parcourir les fichiers

Merge branch 'main' of https://forge.uclouvain.be/sdemeesterde/project_trtp into main

parents 5e56c37f ccba8897
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -7,8 +7,8 @@ HEADERS_DIR = -Iheaders
LDFLAGS = -lz
# Adapt these as you want to fit with your project
SENDER_SOURCES = $(wildcard src/sender.c src/log.c src/packet_interface.c)
RECEIVER_SOURCES = $(wildcard src/receiver.c src/log.c src/packet_interface.c src/our_utils.c src/receiver_utils.c)
SENDER_SOURCES = $(wildcard src/sender.c src/log.c src/our_utils.c src/packet_interface.c)
RECEIVER_SOURCES = $(wildcard src/receiver.c src/log.c src/our_utils.c src/packet_interface.c src/receiver_utils.c)
SENDER_OBJECTS = $(SENDER_SOURCES:.c=.o)
RECEIVER_OBJECTS = $(RECEIVER_SOURCES:.c=.o)
......
......@@ -94,6 +94,17 @@ pkt_t* pkt_new();
* ressources associées */
void pkt_del(pkt_t*);
/**
* @brief Calculates the CRC32
*
* @param buffer : The buffer on which to calculate CRC32.
* @param len : The number of bytes of the buffer.
* @return uint32_t
*/
uint32_t calculate_crc(const char * buffer, uint32_t len);
/*
* Retourne la longueur du header en bytes.
*/
......
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include "log.h"
#include "our_utils.h"
#include "packet_interface.h"
// #define WINDOW_DEFAULT
\ No newline at end of file
......@@ -13,13 +13,6 @@ void pkt_del(pkt_t *pkt)
free(pkt);
}
/**
* @brief Calculates the CRC32
*
* @param buffer : The buffer on which to calculate CRC32.
* @param len : The number of bytes of the buffer.
* @return uint32_t
*/
uint32_t calculate_crc(const char * buffer, uint32_t len)
{
return (uint32_t) crc32(0L, (const void *) buffer, len);
......@@ -244,7 +237,7 @@ const char* pkt_get_payload(const pkt_t* pkt)
}
/** SETTER*/
/** SETTER */
pkt_status_code pkt_set_type(pkt_t *pkt, const ptypes_t type)
{
pkt->header.front |= (type << TYPE_SHIFT);
......
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include "log.h"
#include "../headers/sender.h"
int print_usage(char *prog_name) {
ERROR("Usage:\n\t%s [-f filename] [-s stats_filename] [-c] receiver_ip receiver_port", prog_name);
......@@ -24,22 +18,21 @@ int main(int argc, char **argv) {
while ((opt = getopt(argc, argv, "f:s:hc")) != -1) {
switch (opt) {
case 'f':
filename = optarg;
break;
case 'h':
return print_usage(argv[0]);
case 's':
stats_filename = optarg;
break;
case 'c':
fec_enabled = true;
break;
default:
return print_usage(argv[0]);
case 'f':
filename = optarg;
break;
case 'h':
return print_usage(argv[0]);
case 's':
stats_filename = optarg;
break;
case 'c':
fec_enabled = true;
break;
default:
return print_usage(argv[0]);
}
}
if (optind + 2 != argc) {
ERROR("Unexpected number of positional arguments");
return print_usage(argv[0]);
......@@ -56,12 +49,91 @@ int main(int argc, char **argv) {
DEBUG_DUMP("Some bytes", 11); // You can use it with any pointer type
// This is not an error per-se.
ERROR("Sender has following arguments: filename is %s, stats_filename is %s, fec_enabled is %d, receiver_ip is %s, receiver_port is %u",
ERROR("Sender has following arguments: \tfilename is %s,\n\t stats_filename is %s,\n\t fec_enabled is %d,\n\t receiver_ip is %s,\n\t receiver_port is %u",
filename, stats_filename, fec_enabled, receiver_ip, receiver_port);
DEBUG("You can only see me if %s", "you built me using `make debug`");
ERROR("This is not an error, %s", "now let's code!");
// Now let's code!
// Now let's code!
// Alright :-)
// *** Step 1: Create a socket AND connect ***
struct sockaddr_in6 receiver_addr;
if (real_address(receiver_ip, &receiver_addr) != NULL)
{
ERROR("An error occured when computing the real address from ip\n");
return EXIT_FAILURE;
}
// create_socket also create a connection using connection() !!
int socket_fd = create_socket(NULL, 0, &receiver_addr, receiver_port);
if (socket_fd == -1)
{
close(socket_fd);
ERROR("An error occured when sender trying to create its connecting socket\n");
return EXIT_FAILURE;
}
// *** Step 2: Prepare to send ***
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
close(socket_fd);
ERROR("An error occured while trying to open the file\n");
return EXIT_FAILURE;
}
// *** Step: Time for sending ***
pkt_t buffer[MAX_WINDOW_SIZE];
int buffer_space = MAX_WINDOW_SIZE;
int seqnum = 0;
pkt_t *pkt = pkt_new();
ssize_t nbr_byte_read = read(fd, pkt->payload, MAX_PAYLOAD_SIZE);
while (nbr_byte_read > 0)
{
// *** Step: Prepare the paquet to be send ***
pkt_set_type(pkt, PTYPE_DATA);
pkt_set_length(pkt, nbr_byte_read);
// We set the TR to 0 in order to calculcate the CRC on the header
char modified_header[8];
memcpy((void *) &modified_header, (void *) &pkt->header, 8);
modified_header[0] = modified_header[0] & TR_SETTER_TO_ZERO;
uint32_t crc1 = htonl(calculate_crc(modified_header, 8));
pkt_set_crc1(pkt, crc1);
uint32_t crc2 = htonl(calculate_crc(pkt->payload, (uint32_t) pkt_get_length(pkt)));
pkt_set_crc2(pkt, crc2);
char paquet_to_be_sent[PKT_MAX_LEN];
if (pkt_encode(pkt, paquet_to_be_sent, PKT_MAX_LEN) != PKT_OK)
{
close(fd);
close(socket_fd);
ERROR("An error occured when trying to encode a paquet\n");
return EXIT_FAILURE;
}
ssize_t sent = send(socket_fd, paquet_to_be_sent, PKT_MAX_LEN, 0);
if (sent == -1)
{
close(fd);
close(socket_fd);
ERROR("An error occured when trying to send a paquet\n");
return EXIT_FAILURE;
}
pkt_del(pkt);
pkt = pkt_new();
nbr_byte_read = read(fd, pkt->payload, MAX_PAYLOAD_SIZE);
}
pkt_del(pkt);
close(fd);
close(socket_fd);
return EXIT_SUCCESS;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter