Salīdzināt revīzijas
5 commits
| Autors | SHA1 | Datums | |
|---|---|---|---|
|
|
16d90cd258 | ||
|
|
3e93bf42f6 | ||
|
|
1c06417c48 | ||
|
|
bad1e6c08c | ||
|
|
2afe7b5bf8 |
2 mainīti faili ar 249 papildinājumiem un 42 dzēšanām
|
|
@ -27,6 +27,11 @@ struct app_buffer_entry {
|
|||
/* Condition variable used for passive wait when buffer is empty */
|
||||
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 *
|
||||
* ************************/
|
||||
|
|
@ -213,7 +218,7 @@ int mic_tcp_core_send(mic_tcp_payload buff)
|
|||
if(random > lr_tresh) {
|
||||
result = sendto(sys_socket, buff.data, buff.size, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
|
||||
} else {
|
||||
printf("[MICTCP-CORE] Perte du paquet\n");
|
||||
printf("[MICTCP-CORE] "COLOR_RED"Perte du paquet"COLOR_RESET"\n");
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
|||
216
src/mictcp.c
216
src/mictcp.c
|
|
@ -1,7 +1,129 @@
|
|||
#include <mictcp.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 DEBUG 1
|
||||
|
||||
#define LOSS_RATE 10
|
||||
#define TIMEOUT 2
|
||||
#define MAX_LOSS_RATE 5
|
||||
#define LIST_LENGTH 30
|
||||
|
||||
struct mic_tcp_sock g_sock;
|
||||
int next_msg_num;
|
||||
|
||||
struct node {
|
||||
int isSuccess;
|
||||
struct node *previous;
|
||||
};
|
||||
|
||||
struct {
|
||||
struct node *head;
|
||||
struct node *tail;
|
||||
int size;
|
||||
int errorNumber;
|
||||
} list;
|
||||
|
||||
/**
|
||||
* Gets current loss rate
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int getLossRate() {
|
||||
if (list.size > 0)
|
||||
return (100 * list.errorNumber / list.size);
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current loss rate is acceptable
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int isLossAcceptable() {
|
||||
return getLossRate() <= MAX_LOSS_RATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove last element from the list and remove a success from count if removed element was a success
|
||||
*/
|
||||
void popLast() {
|
||||
if (list.size > 0) {
|
||||
struct node *last = list.tail;
|
||||
list.tail = last->previous;
|
||||
list.size--;
|
||||
if (last->isSuccess != 1)
|
||||
list.errorNumber--;
|
||||
free(last);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new link at the front of the list.
|
||||
* If the list has reached its max length after insertion, remove last element
|
||||
*
|
||||
* @param isSuccess
|
||||
*/
|
||||
void insertFirst(int isSuccess) {
|
||||
struct node *link = (struct node*) malloc(sizeof(struct node));
|
||||
link->isSuccess = isSuccess;
|
||||
link->previous = NULL;
|
||||
|
||||
if (list.size > 0)
|
||||
list.head->previous = link;
|
||||
else
|
||||
list.tail = link;
|
||||
list.head = link;
|
||||
|
||||
if (isSuccess != 1)
|
||||
list.errorNumber++;
|
||||
|
||||
list.size++;
|
||||
|
||||
if (list.size > LIST_LENGTH)
|
||||
popLast();
|
||||
}
|
||||
|
||||
void clearList() {
|
||||
while (list.size > 0) {
|
||||
popLast();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a network error in the list
|
||||
*/
|
||||
void saveError() {
|
||||
insertFirst(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a network success in the list
|
||||
*/
|
||||
void saveSuccess() {
|
||||
insertFirst(1);
|
||||
}
|
||||
|
||||
void printList() {
|
||||
if (DEBUG) {
|
||||
printf("========= LIST =========\n");
|
||||
printf("size: %d\n", list.size);
|
||||
printf("errorNumber: %d\n", list.errorNumber);
|
||||
printf("lossRate: %d\n", getLossRate());
|
||||
printf("isLossAcceptable: %d\n", isLossAcceptable());
|
||||
|
||||
if (list.size > 0) {
|
||||
printf("head: %d\n", list.head->isSuccess);
|
||||
printf("tail: %d\n", list.tail->isSuccess);
|
||||
}
|
||||
printf("========================\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de créer un socket entre l’application et MIC-TCP.
|
||||
|
|
@ -9,14 +131,21 @@ struct mic_tcp_sock g_sock;
|
|||
*/
|
||||
int mic_tcp_socket(start_mode sm) {
|
||||
int result;
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
result = initialize_components(sm); /* Appel obligatoire */
|
||||
set_loss_rate(0);
|
||||
set_loss_rate(LOSS_RATE);
|
||||
|
||||
g_sock.fd = result;
|
||||
g_sock.state = IDLE;
|
||||
next_msg_num = 1;
|
||||
|
||||
list.size = 0;
|
||||
list.errorNumber = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -26,11 +155,13 @@ int mic_tcp_socket(start_mode sm) {
|
|||
* @return 0 si succès, et -1 en cas d’échec.
|
||||
*/
|
||||
int mic_tcp_bind(int socket, mic_tcp_sock_addr addr) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int result = 0; // TODO change in v2
|
||||
int result = 0;
|
||||
|
||||
g_sock.addr = addr;
|
||||
|
||||
|
|
@ -42,11 +173,13 @@ int mic_tcp_bind(int socket, mic_tcp_sock_addr addr) {
|
|||
* @return 0 si succès, -1 si erreur.
|
||||
*/
|
||||
int mic_tcp_accept(int socket, mic_tcp_sock_addr *addr) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int result = 0; // TODO change in v2
|
||||
int result = 0;
|
||||
|
||||
g_sock.state = ESTABLISHED;
|
||||
|
||||
|
|
@ -58,11 +191,13 @@ int mic_tcp_accept(int socket, mic_tcp_sock_addr *addr) {
|
|||
* @return 0 si la connexion est établie, et -1 en cas d’échec
|
||||
*/
|
||||
int mic_tcp_connect(int socket, mic_tcp_sock_addr addr) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int result = 0; // TODO change in v2
|
||||
int result = 0;
|
||||
|
||||
g_sock.addr = addr;
|
||||
g_sock.state = ESTABLISHED;
|
||||
|
|
@ -75,23 +210,61 @@ int mic_tcp_connect(int socket, mic_tcp_sock_addr addr) {
|
|||
* @return la taille des données envoyées, et -1 en cas d'erreur
|
||||
*/
|
||||
int mic_tcp_send(int socket, char *mesg, int mesg_size) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
int result;
|
||||
int errorDetected = 0;
|
||||
|
||||
do {
|
||||
struct mic_tcp_pdu pdu;
|
||||
pdu.payload.data = mesg;
|
||||
pdu.payload.size = mesg_size;
|
||||
|
||||
pdu.header.source_port = 0;
|
||||
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.syn = 0;
|
||||
pdu.header.ack = 0;
|
||||
pdu.header.fin = 0;
|
||||
|
||||
return IP_send(pdu, g_sock.addr);
|
||||
if (DEBUG)
|
||||
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;
|
||||
if (DEBUG)
|
||||
printf("[MIC-TCP] Waiting for ACK\n");
|
||||
|
||||
result = IP_recv(&rcv_pdu, &rcv_sock_addr, TIMEOUT);
|
||||
if (result == -1 && DEBUG)
|
||||
printf("[MIC-TCP] "COLOR_BOLD"Timeout"COLOR_RESET"\n");
|
||||
else if (rcv_pdu.header.ack_num != next_msg_num + 1) {
|
||||
if (DEBUG) {
|
||||
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 if (DEBUG)
|
||||
printf("[MIC-TCP] Received ACK: "COLOR_GREEN"VALID"COLOR_RESET"\n");
|
||||
} else if (DEBUG)
|
||||
printf("[MIC-TCP] Ip_send "COLOR_RED"ERROR"COLOR_RESET"\n");
|
||||
|
||||
if (result == -1 && errorDetected == 0) {
|
||||
errorDetected = 1;
|
||||
saveError();
|
||||
} else if (result != -1)
|
||||
saveSuccess();
|
||||
printList();
|
||||
} while (result == -1 && isLossAcceptable() == 0);
|
||||
|
||||
next_msg_num = next_msg_num + 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -101,9 +274,11 @@ int mic_tcp_send(int socket, char *mesg, int mesg_size) {
|
|||
* NB : cette fonction fait appel à la fonction app_buffer_get()
|
||||
*/
|
||||
int mic_tcp_recv(int socket, char *mesg, int max_mesg_size) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int result = -1;
|
||||
|
||||
|
|
@ -124,12 +299,14 @@ int mic_tcp_recv(int socket, char *mesg, int max_mesg_size) {
|
|||
* @return 0 si tout se passe bien et -1 en cas d'erreur
|
||||
*/
|
||||
int mic_tcp_close(int socket) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction : ");
|
||||
printf(__FUNCTION__);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
g_sock.state = CLOSED;
|
||||
|
||||
clearList();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -140,9 +317,34 @@ int mic_tcp_close(int socket) {
|
|||
* app_buffer_put().
|
||||
*/
|
||||
void process_received_PDU(mic_tcp_pdu pdu, mic_tcp_sock_addr addr) {
|
||||
if (DEBUG) {
|
||||
printf("[MIC-TCP] Appel de la fonction: ");
|
||||
printf(__FUNCTION__);
|
||||
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 = pdu.header.seq_num + 1;
|
||||
app_buffer_put(pdu.payload);
|
||||
if (DEBUG)
|
||||
printf("[MIC-TCP] Received PDU: "COLOR_GREEN"VALID"COLOR_RESET"\n");
|
||||
} else if (DEBUG) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (DEBUG)
|
||||
printf("[MIC-TCP] Sending ack: %d\n", next_msg_num);
|
||||
|
||||
ack_pdu.header.ack_num = next_msg_num;
|
||||
IP_send(ack_pdu, addr);
|
||||
}
|
||||
|
|
|
|||
Notiek ielāde…
Atsaukties uz šo jaunā problēmā