Compare commits

..

3 commits
master ... v2

Author SHA1 Message Date
keplyx
1c06417c48 Improved timeout 2020-05-07 08:58:12 +02:00
keplyx
bad1e6c08c Implemented basic stop and wait 2020-05-07 08:28:32 +02:00
keplyx
2afe7b5bf8 First try at stop and wait 2020-05-04 12:06:48 +02:00
2 changed files with 75 additions and 17 deletions

View file

@ -27,6 +27,11 @@ struct app_buffer_entry {
/* Condition variable used for passive wait when buffer is empty */ /* Condition variable used for passive wait when buffer is empty */
pthread_cond_t buffer_empty_cond; pthread_cond_t buffer_empty_cond;
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_BOLD "\x1b[1m"
#define COLOR_RESET "\x1b[0m"
/* ************************ /* ************************
* Fonctions Utilitaires * * Fonctions Utilitaires *
* ************************/ * ************************/
@ -213,7 +218,7 @@ int mic_tcp_core_send(mic_tcp_payload buff)
if(random > lr_tresh) { if(random > lr_tresh) {
result = sendto(sys_socket, buff.data, buff.size, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)); result = sendto(sys_socket, buff.data, buff.size, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
} else { } else {
printf("[MICTCP-CORE] Perte du paquet\n"); printf("[MICTCP-CORE] "COLOR_RED"Perte du paquet"COLOR_RESET"\n");
} }
return result; return result;

View file

@ -1,7 +1,16 @@
#include <mictcp.h> #include <mictcp.h>
#include <api/mictcp_core.h> #include <api/mictcp_core.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_BOLD "\x1b[1m"
#define COLOR_RESET "\x1b[0m"
#define LOSS_RATE 5
#define TIMEOUT 2
struct mic_tcp_sock g_sock; struct mic_tcp_sock g_sock;
int next_msg_num;
/** /**
* Permet de créer un socket entre lapplication et MIC-TCP. * Permet de créer un socket entre lapplication et MIC-TCP.
@ -13,10 +22,11 @@ int mic_tcp_socket(start_mode sm) {
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
result = initialize_components(sm); /* Appel obligatoire */ result = initialize_components(sm); /* Appel obligatoire */
set_loss_rate(0); set_loss_rate(LOSS_RATE);
g_sock.fd = result; g_sock.fd = result;
g_sock.state = IDLE; g_sock.state = IDLE;
next_msg_num = 1;
return result; return result;
} }
@ -30,7 +40,7 @@ int mic_tcp_bind(int socket, mic_tcp_sock_addr addr) {
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
int result = 0; // TODO change in v2 int result = 0;
g_sock.addr = addr; g_sock.addr = addr;
@ -46,7 +56,7 @@ int mic_tcp_accept(int socket, mic_tcp_sock_addr *addr) {
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
int result = 0; // TODO change in v2 int result = 0;
g_sock.state = ESTABLISHED; g_sock.state = ESTABLISHED;
@ -62,7 +72,7 @@ int mic_tcp_connect(int socket, mic_tcp_sock_addr addr) {
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
int result = 0; // TODO change in v2 int result = 0;
g_sock.addr = addr; g_sock.addr = addr;
g_sock.state = ESTABLISHED; g_sock.state = ESTABLISHED;
@ -78,20 +88,43 @@ int mic_tcp_send(int socket, char *mesg, int mesg_size) {
printf("[MIC-TCP] Appel de la fonction: "); printf("[MIC-TCP] Appel de la fonction: ");
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
int result = -1;
while (result == -1) {
struct mic_tcp_pdu pdu; struct mic_tcp_pdu pdu;
pdu.payload.data = mesg; pdu.payload.data = mesg;
pdu.payload.size = mesg_size; pdu.payload.size = mesg_size;
pdu.header.source_port = 0; pdu.header.source_port = 0;
pdu.header.dest_port = g_sock.addr.port; pdu.header.dest_port = g_sock.addr.port;
pdu.header.seq_num = 0; pdu.header.seq_num = next_msg_num;
pdu.header.ack_num = 0; pdu.header.ack_num = 0;
pdu.header.syn = 0; pdu.header.syn = 0;
pdu.header.ack = 0; pdu.header.ack = 0;
pdu.header.fin = 0; pdu.header.fin = 0;
return IP_send(pdu, g_sock.addr); printf("\n[MIC-TCP] Sending PDU\n");
if ((result = IP_send(pdu, g_sock.addr)) != -1) {
struct mic_tcp_pdu rcv_pdu;
struct mic_tcp_sock_addr rcv_sock_addr;
printf("[MIC-TCP] Waiting for ACK\n");
result = IP_recv(&rcv_pdu, &rcv_sock_addr, TIMEOUT);
if (result == -1)
printf("[MIC-TCP] "COLOR_BOLD"Timeout"COLOR_RESET"\n");
else if (rcv_pdu.header.ack_num != next_msg_num + 1) {
printf("[MIC-TCP] Received ACK: %d | "COLOR_RED"INVALID"COLOR_RESET"\n", rcv_pdu.header.ack_num);
printf("[MIC-TCP] | Expected: %d\n", next_msg_num + 1);
printf("[MIC-TCP] | Received: %d\n", rcv_pdu.header.ack_num);
result = -1;
} else
printf("[MIC-TCP] Received ACK: "COLOR_GREEN"VALID"COLOR_RESET"\n");
} else
printf("[MIC-TCP] Ip_send "COLOR_RED"ERROR"COLOR_RESET"\n");
}
next_msg_num = next_msg_num + 1;
return result;
} }
/** /**
@ -144,5 +177,25 @@ void process_received_PDU(mic_tcp_pdu pdu, mic_tcp_sock_addr addr) {
printf(__FUNCTION__); printf(__FUNCTION__);
printf("\n"); printf("\n");
struct mic_tcp_pdu ack_pdu;
ack_pdu.header.source_port = 0;
ack_pdu.header.dest_port = addr.port;
ack_pdu.header.ack_num = 0;
ack_pdu.header.syn = 0;
ack_pdu.header.ack = 0;
ack_pdu.header.fin = 0;
if (pdu.header.seq_num == next_msg_num) {
next_msg_num = next_msg_num + 1;
app_buffer_put(pdu.payload); app_buffer_put(pdu.payload);
printf("[MIC-TCP] Received PDU: "COLOR_GREEN"VALID"COLOR_RESET"\n");
} else {
printf("[MIC-TCP] Received PDU: "COLOR_RED"INVALID"COLOR_RESET"\n");
printf("[MIC-TCP] | Expected: %d\n", next_msg_num);
printf("[MIC-TCP] | Received: %d\n", pdu.header.seq_num);
}
ack_pdu.header.ack_num = next_msg_num;
printf("[MIC-TCP] Sending ack: %d\n", next_msg_num);
IP_send(ack_pdu, addr);
} }