Modified to compiled with -ansi and -Wall

-pedantic was not set to allow for __FUNCTION__ use in printf
This commit is contained in:
Nicolas Van Wambeke 2016-06-19 15:02:32 +02:00
parent 8ff0603d6a
commit ddeca822aa
8 changed files with 215 additions and 200 deletions

View file

@ -25,7 +25,7 @@ vpath %.c $(SRC_DIR)
define make-goal define make-goal
$1/%.o: %.c $1/%.o: %.c
$(CC) -m32 -g -I $(INCLUDES) -c $$< -o $$@ $(CC) -ansi -Wall -m32 -g -I $(INCLUDES) -c $$< -o $$@
endef endef
.PHONY: all checkdirs clean .PHONY: all checkdirs clean

View file

@ -4,19 +4,24 @@
#include <mictcp.h> #include <mictcp.h>
#include <math.h> #include <math.h>
// Public core functions, can be used for implementing mictcp /**************************************************************
* Public core functions, can be used for implementing mictcp *
**************************************************************/
int initialize_components(start_mode sm); int initialize_components(start_mode sm);
int IP_send(mic_tcp_pdu, mic_tcp_sock_addr); int IP_send(mic_tcp_pdu, mic_tcp_sock_addr);
int IP_recv(mic_tcp_payload*, mic_tcp_sock_addr*, unsigned long delay); int IP_recv(mic_tcp_payload*, mic_tcp_sock_addr*, unsigned long delay);
int app_buffer_get(mic_tcp_payload); int app_buffer_get(mic_tcp_payload);
int app_buffer_set(mic_tcp_payload); void app_buffer_set(mic_tcp_payload);
void set_loss_rate(unsigned short); void set_loss_rate(unsigned short);
unsigned long get_now_time_msec(); unsigned long get_now_time_msec();
unsigned long get_now_time_usec(); unsigned long get_now_time_usec();
// Private core functions, should not be used for implementing mictcp /**********************************************************************
* Private core functions, should not be used for implementing mictcp *
**********************************************************************/
int full_send(mic_tcp_payload); int full_send(mic_tcp_payload);
int partial_send(mic_tcp_payload); int partial_send(mic_tcp_payload);
mic_tcp_payload get_full_stream(mic_tcp_pdu); mic_tcp_payload get_full_stream(mic_tcp_pdu);

View file

@ -15,18 +15,24 @@
#include <sys/time.h> #include <sys/time.h>
// Etats du protocole (les noms des états sont donnés à titre indicatif /*
// et peuvent être modifiés) * Etats du protocole (les noms des états sont donnés à titre indicatif
* et peuvent être modifiés)
*/
typedef enum protocol_state typedef enum protocol_state
{ {
IDLE, CLOSED, SYN_SENT, SYN_RECEIVED, ESTABLISHED, CLOSING IDLE, CLOSED, SYN_SENT, SYN_RECEIVED, ESTABLISHED, CLOSING
} protocol_state; } protocol_state;
// Mode de démarrage du protocole /*
// NB : nécessaire à lusage de la fonction initialize_components() * Mode de démarrage du protocole
* NB : nécessaire à lusage de la fonction initialize_components()
*/
typedef enum start_mode { CLIENT, SERVER } start_mode; typedef enum start_mode { CLIENT, SERVER } start_mode;
// Structure dune adresse de socket /*
* Structure dune adresse de socket
*/
typedef struct mic_tcp_sock_addr typedef struct mic_tcp_sock_addr
{ {
char * ip_addr; char * ip_addr;
@ -34,38 +40,46 @@ typedef struct mic_tcp_sock_addr
unsigned short port; unsigned short port;
} mic_tcp_sock_addr; } mic_tcp_sock_addr;
// Structure d'un socket /*
* Structure d'un socket
*/
typedef struct mic_tcp_sock typedef struct mic_tcp_sock
{ {
int fd; // descripteur du socket int fd; /* descripteur du socket */
protocol_state state; // état du protocole protocol_state state; /* état du protocole */
mic_tcp_sock_addr addr; // adresse du socket mic_tcp_sock_addr addr; /* adresse du socket */
} mic_tcp_sock; } mic_tcp_sock;
// Structure des données utiles dun PDU MIC-TCP /*
* Structure des données utiles dun PDU MIC-TCP
*/
typedef struct mic_tcp_payload typedef struct mic_tcp_payload
{ {
char* data; // données applicatives char* data; /* données applicatives */
int size; // taille des données int size; /* taille des données */
} mic_tcp_payload; } mic_tcp_payload;
// Structure de l'entête d'un PDU MIC-TCP /*
* Structure de l'entête d'un PDU MIC-TCP
*/
typedef struct mic_tcp_header typedef struct mic_tcp_header
{ {
unsigned short source_port; // numéro de port source unsigned short source_port; /* numéro de port source */
unsigned short dest_port; // numéro de port de destination unsigned short dest_port; /* numéro de port de destination */
unsigned int seq_num; // numéro de séquence unsigned int seq_num; /* numéro de séquence */
unsigned int ack_num; // numéro d'acquittement unsigned int ack_num; /* numéro d'acquittement */
unsigned char syn; // flag SYN (valeur 1 si activé et 0 si non) unsigned char syn; /* flag SYN (valeur 1 si activé et 0 si non) */
unsigned char ack; // flag ACK (valeur 1 si activé et 0 si non) unsigned char ack; /* flag ACK (valeur 1 si activé et 0 si non) */
unsigned char fin; // flag FIN (valeur 1 si activé et 0 si non) unsigned char fin; /* flag FIN (valeur 1 si activé et 0 si non) */
} mic_tcp_header; } mic_tcp_header;
// Structure d'un PDU MIC-TCP /*
* Structure d'un PDU MIC-TCP
*/
typedef struct mic_tcp_pdu typedef struct mic_tcp_pdu
{ {
mic_tcp_header header ; // entête du PDU mic_tcp_header header ; /* entête du PDU */
mic_tcp_payload payload; // charge utile du PDU ⇔ données applicatives mic_tcp_payload payload; /* charge utile du PDU */
} mic_tcp_pdu; } mic_tcp_pdu;
typedef struct app_buffer typedef struct app_buffer
@ -76,7 +90,9 @@ typedef struct app_buffer
} app_buffer; } app_buffer;
// Fonctions de l'interface /****************************
* Fonctions de l'interface *
****************************/
int mic_tcp_socket(start_mode sm); int mic_tcp_socket(start_mode sm);
int mic_tcp_bind(int socket, mic_tcp_sock_addr addr); int mic_tcp_bind(int socket, mic_tcp_sock_addr addr);
int mic_tcp_accept(int socket, mic_tcp_sock_addr* addr); int mic_tcp_accept(int socket, mic_tcp_sock_addr* addr);
@ -86,7 +102,9 @@ int mic_tcp_recv (int socket, char* mesg, int max_mesg_size);
void process_received_PDU(mic_tcp_pdu pdu); void process_received_PDU(mic_tcp_pdu pdu);
int mic_tcp_close(int socket); int mic_tcp_close(int socket);
// Variables globales /**********************
* Variables globales *
**********************/
mic_tcp_sock local_sock_src; mic_tcp_sock local_sock_src;
mic_tcp_sock local_sock_dest; mic_tcp_sock local_sock_dest;
unsigned long timer; unsigned long timer;

View file

@ -1,7 +1,10 @@
#include <api/mictcp_core.h> #include <api/mictcp_core.h>
#include <pthread.h> #include <pthread.h>
#include <strings.h>
// API Variables /*****************
* API Variables *
*****************/
int first_free = 0; int first_free = 0;
int initialized = -1; int initialized = -1;
int sys_socket; int sys_socket;
@ -27,11 +30,13 @@ app_buffer* app_buffer_last = NULL;
unsigned int app_buffer_size = 0; unsigned int app_buffer_size = 0;
unsigned int app_buffer_count = 0; unsigned int app_buffer_count = 0;
// Fonctions Utilitaires /*************************
* Fonctions Utilitaires *
*************************/
int initialize_components(start_mode mode) int initialize_components(start_mode mode)
{ {
int s; int bnd;
struct hostent * hp;
if(initialized != -1) return initialized; if(initialized != -1) return initialized;
if((sys_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1; if((sys_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
else initialized = 1; else initialized = 1;
@ -42,7 +47,7 @@ int initialize_components(start_mode mode)
local_addr.sin_family = AF_INET; local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(API_CS_Port); local_addr.sin_port = htons(API_CS_Port);
local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int bnd = bind(sys_socket, (struct sockaddr *) &local_addr, sizeof(local_addr)); bnd = bind(sys_socket, (struct sockaddr *) &local_addr, sizeof(local_addr));
if (bnd == -1) if (bnd == -1)
{ {
@ -53,7 +58,7 @@ int initialize_components(start_mode mode)
memset((char *) &remote_addr, 0, sizeof(local_addr)); memset((char *) &remote_addr, 0, sizeof(local_addr));
remote_addr.sin_family = AF_INET; remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(API_SC_Port); remote_addr.sin_port = htons(API_SC_Port);
struct hostent * hp = gethostbyname("localhost"); hp = gethostbyname("localhost");
bcopy ( hp->h_addr, &(remote_addr.sin_addr.s_addr), hp->h_length); bcopy ( hp->h_addr, &(remote_addr.sin_addr.s_addr), hp->h_length);
remote_size = sizeof(remote_addr); remote_size = sizeof(remote_addr);
initialized = 1; initialized = 1;
@ -68,7 +73,7 @@ int initialize_components(start_mode mode)
memset((char *) &remote_addr, 0, sizeof(local_addr)); memset((char *) &remote_addr, 0, sizeof(local_addr));
remote_addr.sin_family = AF_INET; remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(API_CS_Port); remote_addr.sin_port = htons(API_CS_Port);
struct hostent * hp = gethostbyname("localhost"); hp = gethostbyname("localhost");
bcopy ( hp->h_addr, &(remote_addr.sin_addr.s_addr), hp->h_length); bcopy ( hp->h_addr, &(remote_addr.sin_addr.s_addr), hp->h_length);
remote_size = sizeof(remote_addr); remote_size = sizeof(remote_addr);
@ -76,7 +81,7 @@ int initialize_components(start_mode mode)
local_addr.sin_family = AF_INET; local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(API_SC_Port); local_addr.sin_port = htons(API_SC_Port);
local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int bnd = bind(sys_socket, (struct sockaddr *) &local_addr, sizeof(local_addr)); bnd = bind(sys_socket, (struct sockaddr *) &local_addr, sizeof(local_addr));
} }
} }
@ -107,11 +112,11 @@ int IP_send(mic_tcp_pdu pk, mic_tcp_sock_addr addr)
int IP_recv(mic_tcp_payload* pk,mic_tcp_sock_addr* addr, unsigned long delay) int IP_recv(mic_tcp_payload* pk,mic_tcp_sock_addr* addr, unsigned long delay)
{ {
// Send data over a fake IP
if(initialized == -1) return -1;
struct timeval tv; struct timeval tv;
/* Send data over a fake IP */
if(initialized == -1) return -1;
if(delay == 0) {tv.tv_sec = 3600; tv.tv_usec = 0;} if(delay == 0) {tv.tv_sec = 3600; tv.tv_usec = 0;}
else {tv.tv_sec = 0; tv.tv_usec = delay;} else {tv.tv_sec = 0; tv.tv_usec = delay;}
if (setsockopt(sys_socket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) { if (setsockopt(sys_socket, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
@ -129,7 +134,7 @@ int IP_recv(mic_tcp_payload* pk,mic_tcp_sock_addr* addr, unsigned long delay)
mic_tcp_payload get_full_stream(mic_tcp_pdu pk) mic_tcp_payload get_full_stream(mic_tcp_pdu pk)
{ {
// Get a full packet from data and header /* Get a full packet from data and header */
mic_tcp_payload tmp; mic_tcp_payload tmp;
tmp.size = 15 + pk.payload.size; tmp.size = 15 + pk.payload.size;
tmp.data = malloc (tmp.size); tmp.data = malloc (tmp.size);
@ -151,7 +156,7 @@ mic_tcp_payload get_data_stream(mic_tcp_payload buff)
mic_tcp_header get_header(char* packet) mic_tcp_header get_header(char* packet)
{ {
// Get a struct header from an incoming packet /* Get a struct header from an incoming packet */
mic_tcp_header tmp; mic_tcp_header tmp;
memcpy(&tmp, packet, 15); memcpy(&tmp, packet, 15);
return tmp; return tmp;
@ -199,14 +204,14 @@ int partial_send(mic_tcp_payload buff)
int app_buffer_get(mic_tcp_payload app_buff) int app_buffer_get(mic_tcp_payload app_buff)
{ {
mic_tcp_payload tmp;
app_buffer* current;
while(app_buffer_count == 0) while(app_buffer_count == 0)
{ {
usleep(1000); usleep(1000);
} }
mic_tcp_payload tmp;
app_buffer* current;
if(app_buffer_count > 0) if(app_buffer_count > 0)
{ {
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
@ -232,11 +237,12 @@ int app_buffer_get(mic_tcp_payload app_buff)
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return tmp.size;
} }
return tmp.size;
} }
int app_buffer_set(mic_tcp_payload bf) void app_buffer_set(mic_tcp_payload bf)
{ {
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
@ -268,17 +274,18 @@ int app_buffer_set(mic_tcp_payload bf)
void* listening(void* arg) void* listening(void* arg)
{ {
mic_tcp_pdu pdu_tmp;
int recv_size;
mic_tcp_sock_addr remote;
mic_tcp_payload tmp_buff;
pthread_mutex_init(&lock, NULL); pthread_mutex_init(&lock, NULL);
printf("[MICTCP-CORE] Demarrage du thread de reception reseau...\n"); printf("[MICTCP-CORE] Demarrage du thread de reception reseau...\n");
mic_tcp_payload tmp_buff;
tmp_buff.size = 1500; tmp_buff.size = 1500;
tmp_buff.data = malloc(1500); tmp_buff.data = malloc(1500);
mic_tcp_pdu pdu_tmp;
int recv_size;
mic_tcp_sock_addr remote;
while(1) while(1)
{ {
@ -292,7 +299,6 @@ void* listening(void* arg)
process_received_PDU(pdu_tmp); process_received_PDU(pdu_tmp);
} }
} }
} }

View file

@ -8,6 +8,7 @@ int main()
{ {
int sockfd = 0; int sockfd = 0;
char chaine[MAX_SIZE];
mic_tcp_sock_addr addr; mic_tcp_sock_addr addr;
addr.ip_addr = "127.0.0.1"; addr.ip_addr = "127.0.0.1";
addr.port = 1234; addr.port = 1234;
@ -32,7 +33,6 @@ int main()
printf("[TSOCK] Connexion du socket MICTCP: OK\n"); printf("[TSOCK] Connexion du socket MICTCP: OK\n");
} }
char chaine[MAX_SIZE];
memset(chaine, 0, MAX_SIZE); memset(chaine, 0, MAX_SIZE);
printf("[TSOCK] Entrez vos message a envoyer, CTRL+D pour quitter\n"); printf("[TSOCK] Entrez vos message a envoyer, CTRL+D pour quitter\n");

View file

@ -24,19 +24,22 @@ extern errno;
*/ */
void udp_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, int loss) { void udp_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, int loss) {
// Define the socket on which we listen and which we use for sending packets out /* Define the socket on which we listen and which we use for sending packets out */
int listen_sockfd; int listen_sockfd;
// A buffer used to store received segments before they are forwarded /* A buffer used to store received segments before they are forwarded */
char buffer[MAX_UDP_SEGMENT_SIZE]; char buffer[MAX_UDP_SEGMENT_SIZE];
/* Initialize a packet count variable used when loss emulation is active */
int count = 0;
ssize_t n = -1;
// Addresses for the work to be performed /* Addresses for the work to be performed */
struct sockaddr_in serv_addr = listen_on; struct sockaddr_in serv_addr = listen_on;
struct sockaddr_in cliaddr; struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr); socklen_t len = sizeof(cliaddr);
// We construct the socket to be used by this function /* We construct the socket to be used by this function */
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0); listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (bind(listen_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { if (bind(listen_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
@ -48,11 +51,7 @@ void udp_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, in
listen(listen_sockfd,5); listen(listen_sockfd,5);
// Initialize a packet count variable used when loss emulation is active /* Main activity loop, we never exit this, user terminates with SIGKILL */
int count = 0;
ssize_t n = -1;
// Main activity loop, we never exit this, user terminates with SIGKILL
while(1) { while(1) {
bzero(buffer,MAX_UDP_SEGMENT_SIZE); bzero(buffer,MAX_UDP_SEGMENT_SIZE);
@ -62,7 +61,7 @@ void udp_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, in
} }
if(loss == 1) { if(loss == 1) {
// We emulate losses every 600 packets by delaying the processing by 2 seconds. /* We emulate losses every 600 packets by delaying the processing by 2 seconds */
if(count++ == 600) { if(count++ == 600) {
printf("Simulating TCP loss\n"); printf("Simulating TCP loss\n");
sleep(2); sleep(2);
@ -70,14 +69,14 @@ void udp_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, in
} }
} }
// We forward the packet to its final destination /* We forward the packet to its final destination */
n = sendto(listen_sockfd, buffer, n, 0, (struct sockaddr *) &transmit_to, len); n = sendto(listen_sockfd, buffer, n, 0, (struct sockaddr *) &transmit_to, len);
if (n < 0) { if (n < 0) {
perror(0); perror(0);
} }
} }
// We never execute this but anyway, for sanity /* We never execute this but anyway, for sanity */
close(listen_sockfd); close(listen_sockfd);
} }
@ -110,30 +109,17 @@ struct timespec tsSubtract (struct timespec time1, struct timespec time2) {
*/ */
void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, int loss) { void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, int loss) {
// Define the socket on which we listen and which we use for sending packets out /* Define the socket on which we listen and which we use for sending packets out */
int listen_sockfd; int listen_sockfd;
// A buffer used to store received segments before they are forwarded /* A buffer used to store received segments before they are forwarded */
char buffer[MAX_UDP_SEGMENT_SIZE]; char buffer[MAX_UDP_SEGMENT_SIZE];
// Addresses for the work to be performed /* Addresses for the work to be performed */
struct sockaddr_in serv_addr = listen_on;
struct sockaddr_in cliaddr; struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr); socklen_t len = sizeof(cliaddr);
// We construct the socket to be used by this function /* Initialize a packet count variable used when loss emulation is active */
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//if (bind(listen_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
// printf("ERROR on binding: ");
// perror(0);
// printf("\n");
//}
//listen(listen_sockfd,5);
// Initialize a packet count variable used when loss emulation is active
int count = 0; int count = 0;
ssize_t n = -1; ssize_t n = -1;
@ -144,13 +130,16 @@ void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, i
struct timespec rem; struct timespec rem;
int firstValue = 0; int firstValue = 0;
// Main activity loop, we never exit this, user terminates with SIGKILL /* We construct the socket to be used by this function */
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
/* Main activity loop, we never exit this, user terminates with SIGKILL */
while(!feof(fd)) { while(!feof(fd)) {
bzero(buffer,MAX_UDP_SEGMENT_SIZE); bzero(buffer,MAX_UDP_SEGMENT_SIZE);
n = fread(&currentTime, 1, sizeof(struct timespec), fd); n = fread(&currentTime, 1, sizeof(struct timespec), fd);
if(firstValue > 0) { if(firstValue > 0) {
// We need to sleep a while /* We need to sleep a while */
struct timespec difference = tsSubtract(currentTime, lastTime); struct timespec difference = tsSubtract(currentTime, lastTime);
nanosleep(&difference, &rem); nanosleep(&difference, &rem);
} else { } else {
@ -164,7 +153,7 @@ void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, i
} }
if(loss == 1) { if(loss == 1) {
// We emulate losses every 600 packets by delaying the processing by 2 seconds. /* We emulate losses every 600 packets by delaying the processing by 2 seconds */
if(count++ == 600) { if(count++ == 600) {
printf("Simulating TCP loss\n"); printf("Simulating TCP loss\n");
sleep(2); sleep(2);
@ -172,14 +161,13 @@ void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, i
} }
} }
// We forward the packet to its final destination /* We forward the packet to its final destination */
n = sendto(listen_sockfd, buffer, n, 0, (struct sockaddr *) &transmit_to, len); n = sendto(listen_sockfd, buffer, n, 0, (struct sockaddr *) &transmit_to, len);
if (n < 0) { if (n < 0) {
perror(0); perror(0);
} }
} }
// We never execute this but anyway, for sanity
close(listen_sockfd); close(listen_sockfd);
} }
@ -189,67 +177,52 @@ void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, i
*/ */
void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) { void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) {
// Define the socket on which we listen /* Define the socket on which we listen */
int listen_sockfd; int listen_sockfd;
// A buffer used to store received segments before they are forwarded /* A buffer used to store received segments before they are forwarded */
char buffer[MAX_UDP_SEGMENT_SIZE]; char buffer[MAX_UDP_SEGMENT_SIZE];
// Addresses for the work to be performed /* Initialize a packet count variable used when loss emulation is active */
struct sockaddr_in serv_addr = listen_on;
struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr);
// MICTCP stuff
int mic_tcp_sockfd;
start_mode start_mode_mic_tcp = CLIENT;
mic_tcp_sock_addr mic_tcp_dest_addr;
mic_tcp_dest_addr.ip_addr = (char* ) &(transmit_to.sin_addr.s_addr);
mic_tcp_dest_addr.port = transmit_to.sin_port;
// We construct the UDP and MICTCP sockets to be used by this function
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) {
printf("ERROR creating the MICTCP socket\n");
}
// We now connect the MICTCP socket
if(mic_tcp_connect(mic_tcp_sockfd, mic_tcp_dest_addr) == -1) {
printf("ERROR connecting the MICTCP socket\n");
}
// if (bind(listen_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
// printf("ERROR on binding the UDP socket: ");
// perror(0);
// printf("\n");
// }
// listen(listen_sockfd,5);
// Initialize a packet count variable used when loss emulation is active
int count = 0;
ssize_t n = -1; ssize_t n = -1;
FILE * fd = fopen("../video/video.bin", "rb"); FILE * fd = fopen("../video/video.bin", "rb");
struct timespec currentTimeFile; struct timespec currentTimeFile;
struct timespec firstTimeFile; struct timespec firstTimeFile;
struct timespec lastTimeFile;
struct timespec timeFirstPacket; struct timespec timeFirstPacket;
struct timespec currentTime; struct timespec currentTime;
struct timespec rem; struct timespec rem;
int firstValue = 0; int firstValue = 0;
int active = 1; int active = 1;
// Main activity loop, we exit this at the end of the file
/* MICTCP stuff */
int mic_tcp_sockfd;
start_mode start_mode_mic_tcp = CLIENT;
mic_tcp_sock_addr mic_tcp_dest_addr;
mic_tcp_dest_addr.ip_addr = (char* ) &(transmit_to.sin_addr.s_addr);
mic_tcp_dest_addr.port = transmit_to.sin_port;
/* We construct the UDP and MICTCP sockets to be used by this function */
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) {
printf("ERROR creating the MICTCP socket\n");
}
/* We now connect the MICTCP socket */
if(mic_tcp_connect(mic_tcp_sockfd, mic_tcp_dest_addr) == -1) {
printf("ERROR connecting the MICTCP socket\n");
}
/* Main activity loop, we exit this at the end of the file */
while(active) { while(active) {
bzero(buffer, MAX_UDP_SEGMENT_SIZE); bzero(buffer, MAX_UDP_SEGMENT_SIZE);
n = fread(&currentTimeFile, 1, sizeof(struct timespec), fd); n = fread(&currentTimeFile, 1, sizeof(struct timespec), fd);
if(firstValue > 0) { if(firstValue > 0) {
// We need to sleep a while /* We need to sleep a while */
if( clock_gettime( CLOCK_REALTIME, &currentTime) == -1 ) { if( clock_gettime( CLOCK_REALTIME, &currentTime) == -1 ) {
perror( "clock gettime" ); perror( "clock gettime" );
} }
@ -264,7 +237,6 @@ void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to
} }
firstValue++; firstValue++;
} }
lastTimeFile = currentTimeFile;
n = fread(buffer, 1, sizeof(n), fd); n = fread(buffer, 1, sizeof(n), fd);
n = fread(buffer, 1, *((int *)buffer), fd); n = fread(buffer, 1, *((int *)buffer), fd);
if (n <= 0) { if (n <= 0) {
@ -274,17 +246,17 @@ void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to
active = 0; active = 0;
} }
// We forward the packet to its final destination /* We forward the packet to its final destination */
n = mic_tcp_send(mic_tcp_sockfd, buffer, n); n = mic_tcp_send(mic_tcp_sockfd, buffer, n);
if (n <= 0) { if (n <= 0) {
printf("ERROR on MICTCP send\n"); printf("ERROR on MICTCP send\n");
} }
} }
// We execute this when the file has finished being replayed /* We execute this when the file has finished being replayed */
close(listen_sockfd); close(listen_sockfd);
// Same for MICTCP /* Same for MICTCP */
mic_tcp_close(mic_tcp_sockfd); mic_tcp_close(mic_tcp_sockfd);
} }
@ -296,32 +268,35 @@ void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to
*/ */
void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) { void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) {
// Define the socket on which we listen /* Define the socket on which we listen */
int listen_sockfd; int listen_sockfd;
// A buffer used to store received segments before they are forwarded /* A buffer used to store received segments before they are forwarded */
char buffer[MAX_UDP_SEGMENT_SIZE]; char buffer[MAX_UDP_SEGMENT_SIZE];
// Addresses for the work to be performed /* Addresses for the work to be performed */
struct sockaddr_in serv_addr = listen_on;
struct sockaddr_in cliaddr; struct sockaddr_in cliaddr;
struct sockaddr_in serv_addr = listen_on;
socklen_t len = sizeof(cliaddr); socklen_t len = sizeof(cliaddr);
// MICTCP stuff /* Initialize a packet count variable used when loss emulation is active */
ssize_t n = -1;
/* MICTCP stuff */
int mic_tcp_sockfd; int mic_tcp_sockfd;
start_mode start_mode_mic_tcp = CLIENT; start_mode start_mode_mic_tcp = CLIENT;
mic_tcp_sock_addr mic_tcp_dest_addr; mic_tcp_sock_addr mic_tcp_dest_addr;
mic_tcp_dest_addr.ip_addr = (char* ) &(transmit_to.sin_addr.s_addr); mic_tcp_dest_addr.ip_addr = (char* ) &(transmit_to.sin_addr.s_addr);
mic_tcp_dest_addr.port = transmit_to.sin_port; mic_tcp_dest_addr.port = transmit_to.sin_port;
// We construct the UDP and MICTCP sockets to be used by this function /* We construct the UDP and MICTCP sockets to be used by this function */
listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0); listen_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) { if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) {
printf("ERROR creating the MICTCP socket\n"); printf("ERROR creating the MICTCP socket\n");
} }
// We now connect the MICTCP socket /* We now connect the MICTCP socket */
if(mic_tcp_connect(mic_tcp_sockfd, mic_tcp_dest_addr) == -1) { if(mic_tcp_connect(mic_tcp_sockfd, mic_tcp_dest_addr) == -1) {
printf("ERROR connecting the MICTCP socket\n"); printf("ERROR connecting the MICTCP socket\n");
} }
@ -334,12 +309,7 @@ void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
listen(listen_sockfd,5); listen(listen_sockfd,5);
/* Main activity loop, we never exit this, user terminates with SIGKILL */
// Initialize a packet count variable used when loss emulation is active
int count = 0;
ssize_t n = -1;
// Main activity loop, we never exit this, user terminates with SIGKILL
while(1) { while(1) {
bzero(buffer, MAX_UDP_SEGMENT_SIZE); bzero(buffer, MAX_UDP_SEGMENT_SIZE);
@ -348,17 +318,17 @@ void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
perror(0); perror(0);
} }
// We forward the packet to its final destination /* We forward the packet to its final destination */
n = mic_tcp_send(mic_tcp_sockfd, buffer, n); n = mic_tcp_send(mic_tcp_sockfd, buffer, n);
if (n <= 0) { if (n <= 0) {
printf("ERROR on MICTCP send\n"); printf("ERROR on MICTCP send\n");
} }
} }
// We never execute this but anyway, for sanity /* We never execute this but anyway, for sanity */
close(listen_sockfd); close(listen_sockfd);
// Same for MICTCP /* Same for MICTCP */
mic_tcp_close(mic_tcp_sockfd); mic_tcp_close(mic_tcp_sockfd);
} }
@ -368,12 +338,10 @@ void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
*/ */
void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) { void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to) {
// Define the socket on which we listen /* Define the socket on which we listen */
int sending_sockfd; int sending_sockfd;
// A buffer used to store received segments before they are forwarded /* A buffer used to store received segments before they are forwarded */
char buffer[MAX_UDP_SEGMENT_SIZE];
int rcv_values [CUMULATED_BUFF_NB];
char* Buff [CUMULATED_BUFF_NB]; char* Buff [CUMULATED_BUFF_NB];
int iii; int iii;
for (iii=0; iii<CUMULATED_BUFF_NB; iii++) for (iii=0; iii<CUMULATED_BUFF_NB; iii++)
@ -381,19 +349,18 @@ void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
Buff[iii] = malloc(MAX_UDP_SEGMENT_SIZE); Buff[iii] = malloc(MAX_UDP_SEGMENT_SIZE);
} }
// Addresses for the work to be performed /* Addresses for the work to be performed */
struct sockaddr_in serv_addr = listen_on;
struct sockaddr_in cliaddr; struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr); socklen_t len = sizeof(cliaddr);
// MICTCP stuff /* MICTCP stuff */
int mic_tcp_sockfd; int mic_tcp_sockfd;
start_mode start_mode_mic_tcp = SERVER; start_mode start_mode_mic_tcp = SERVER;
mic_tcp_sock_addr mic_tcp_listen_addr, mic_tcp_remote_addr; mic_tcp_sock_addr mic_tcp_listen_addr, mic_tcp_remote_addr;
mic_tcp_listen_addr.ip_addr = (char* ) &(listen_on.sin_addr.s_addr); mic_tcp_listen_addr.ip_addr = (char* ) &(listen_on.sin_addr.s_addr);
mic_tcp_listen_addr.port = listen_on.sin_port; mic_tcp_listen_addr.port = listen_on.sin_port;
// We construct the UDP and MICTCP sockets to be used by this function /* We construct the UDP and MICTCP sockets to be used by this function */
sending_sockfd = socket(AF_INET, SOCK_DGRAM, 0); sending_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) { if((mic_tcp_sockfd = mic_tcp_socket(start_mode_mic_tcp)) == -1) {
@ -408,11 +375,10 @@ void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
printf("ERROR on accept on the MICTCP socket\n"); printf("ERROR on accept on the MICTCP socket\n");
} }
// Initialize a packet count variable used when loss emulation is active /* Initialize a packet count variable used when loss emulation is active */
int count = 0;
ssize_t n = -1; ssize_t n = -1;
// Main activity loop, we never exit this, user terminates with SIGKILL /* Main activity loop, we never exit this, user terminates with SIGKILL */
while(1) { while(1) {
int k; int k;
for(k=0; k<CUMULATED_BUFF_NB; k++) for(k=0; k<CUMULATED_BUFF_NB; k++)
@ -428,7 +394,7 @@ void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
} }
} }
// We forward the packet to its final destination /* We forward the packet to its final destination */
for(k=0; k<CUMULATED_BUFF_NB; k++) for(k=0; k<CUMULATED_BUFF_NB; k++)
{ {
n = sendto(sending_sockfd, Buff[k], n, 0, (struct sockaddr *) &transmit_to, len); n = sendto(sending_sockfd, Buff[k], n, 0, (struct sockaddr *) &transmit_to, len);
@ -437,13 +403,13 @@ void mictcp_to_udp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
} }
} }
//sleep(1); /*sleep(1); */
} }
// We never execute this but anyway, for sanity /* We never execute this but anyway, for sanity */
close(sending_sockfd); close(sending_sockfd);
// Same for MICTCP /* Same for MICTCP */
mic_tcp_close(mic_tcp_sockfd); mic_tcp_close(mic_tcp_sockfd);
} }
@ -455,20 +421,20 @@ static void usage(void) {
int main(int argc, char ** argv) { int main(int argc, char ** argv) {
// Should losses be emulated? /* Should losses be emulated? */
int loss = 1; int loss = 1;
int transport = 0; int transport = 0;
int puits = -1; int puits = -1;
// What sockaddr should this program listen on? /* What sockaddr should this program listen on? */
// Always on port 1234 (data from VLC arrives there using UDP) /* Always on port 1234 (data from VLC arrives there using UDP) */
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr)); bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(1234); serv_addr.sin_port = htons(1234);
// Where should this program send the received data? /* Where should this program send the received data? */
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
bzero((char *) &dest_addr, sizeof(dest_addr)); bzero((char *) &dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET; dest_addr.sin_family = AF_INET;
@ -530,19 +496,20 @@ int main(int argc, char ** argv) {
if(transport == 0) { if(transport == 0) {
if(puits == 0) { if(puits == 0) {
// We receive on UDP and emulate TCP behavior before sending the data out /* We receive on UDP and emulate TCP behavior before sending the data out */
file_to_tcp(serv_addr, dest_addr, loss); file_to_tcp(serv_addr, dest_addr, loss);
} else { } else {
printf("No gateway needed for puits using UDP\n"); printf("No gateway needed for puits using UDP\n");
} }
} else if(transport == 1) { } else if(transport == 1) {
if(puits == 0) { if(puits == 0) {
// We receive on UDP and send the data using MICTCP /* We receive on UDP and send the data using MICTCP */
file_to_mictcp(serv_addr, dest_addr); file_to_mictcp(serv_addr, dest_addr);
} else { } else {
// We receive on MICTCP and send the data using UDP /* We receive on MICTCP and send the data using UDP */
mictcp_to_udp(serv_addr, dest_addr); mictcp_to_udp(serv_addr, dest_addr);
} }
} }
return 0;
} }

View file

@ -7,10 +7,12 @@ int main()
{ {
int sockfd; int sockfd;
mic_tcp_sock_addr addr; mic_tcp_sock_addr addr;
mic_tcp_sock_addr remote_addr;
char chaine[MAX_SIZE];
addr.ip_addr = "127.0.0.1"; addr.ip_addr = "127.0.0.1";
addr.port = 1234; addr.port = 1234;
mic_tcp_sock_addr remote_addr;
if ((sockfd = mic_tcp_socket(SERVER)) == -1) if ((sockfd = mic_tcp_socket(SERVER)) == -1)
{ {
@ -43,14 +45,14 @@ int main()
} }
char chaine[MAX_SIZE];
memset(chaine, 0, MAX_SIZE); memset(chaine, 0, MAX_SIZE);
printf("[TSOCK] Appuyez sur CTRL+C pour quitter ...\n"); printf("[TSOCK] Appuyez sur CTRL+C pour quitter ...\n");
while(1) { while(1) {
int rcv_size = 0;
printf("[TSOCK] Attente d'une donnee, appel de mic_recv ...\n"); printf("[TSOCK] Attente d'une donnee, appel de mic_recv ...\n");
int rcv_size = mic_tcp_recv(sockfd, chaine, MAX_SIZE); rcv_size = mic_tcp_recv(sockfd, chaine, MAX_SIZE);
printf("[TSOCK] Reception d'un message de taille : %d\n", rcv_size); printf("[TSOCK] Reception d'un message de taille : %d\n", rcv_size);
printf("[TSOCK] Message Recu : %s", chaine); printf("[TSOCK] Message Recu : %s", chaine);
} }

View file

@ -1,71 +1,88 @@
#include <mictcp.h> #include <mictcp.h>
#include <api/mictcp_core.h> #include <api/mictcp_core.h>
/*
* Permet de créer un socket entre lapplication et MIC-TCP
* Retourne le descripteur du socket ou bien -1 en cas d'erreur
*/
int mic_tcp_socket(start_mode sm) int mic_tcp_socket(start_mode sm)
// Permet de créer un socket entre lapplication et MIC-TCP
// Retourne le descripteur du socket ou bien -1 en cas d'erreur
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
initialize_components(sm); // Appel obligatoire initialize_components(sm); /* Appel obligatoire */
return -1; return -1;
} }
/*
* Permet dattribuer une adresse à un socket.
* Retourne 0 si succès, et -1 en cas déchec
*/
int mic_tcp_bind(int socket, mic_tcp_sock_addr addr) int mic_tcp_bind(int socket, mic_tcp_sock_addr addr)
// Permet dattribuer une adresse à un socket. Retourne 0 si succès, et -1 en cas déchec
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
return -1;
} }
/*
* Met le socket en état d'acceptation de connexions
* Retourne 0 si succès, -1 si erreur
*/
int mic_tcp_accept(int socket, mic_tcp_sock_addr* addr) int mic_tcp_accept(int socket, mic_tcp_sock_addr* addr)
// Met lapplication en état d'acceptation dune requête de connexion entrante
// Retourne 0 si succès, -1 si erreur
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
return -1; return -1;
} }
/*
* Permet de réclamer létablissement dune connexion
* Retourne 0 si la connexion est établie, et -1 en cas déchec
*/
int mic_tcp_connect(int socket, mic_tcp_sock_addr addr) int mic_tcp_connect(int socket, mic_tcp_sock_addr addr)
// Permet de réclamer létablissement dune connexion
// Retourne 0 si la connexion est établie, et -1 en cas déchec
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
return -1; return -1;
} }
/*
* Permet de réclamer lenvoi dune donnée applicative
* Retourne la taille des données envoyées, et -1 en cas d'erreur
* Dans le cas de la vidéo, seul la source va envoyer au puits
*/
int mic_tcp_send (int mic_sock, char* mesg, int mesg_size) int mic_tcp_send (int mic_sock, char* mesg, int mesg_size)
// Permet de réclamer lenvoi dune donnée applicative
// Retourne la taille des données envoyées, et -1 en cas d'erreur
// Dans le cas de la vidéo, seul la source va envoyer au puits
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
return -1; return -1;
} }
/*
* Permet à lapplication réceptrice de réclamer la récupération dune donnée
* stockée dans les buffers de réception du socket
* Retourne le nombre doctets lu ou bien -1 en cas derreur
* NB : cette fonction fait appel à la fonction app_buffer_get()
*/
int mic_tcp_recv (int socket, char* mesg, int max_mesg_size) int mic_tcp_recv (int socket, char* mesg, int max_mesg_size)
// Permet à lapplication réceptrice de réclamer la récupération dune donnée
// stockée dans les buffers de réception du socket
// Retourne le nombre doctets lu ou bien -1 en cas derreur
// NB : cette fonction fait appel à la fonction app_buffer_get()
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
return -1; return -1;
} }
/*
* Permet de réclamer la destruction dun socket.
* Engendre la fermeture de la connexion suivant le modèle de TCP.
* Retourne 0 si tout se passe bien et -1 en cas d'erreur
*/
int mic_tcp_close (int socket) int mic_tcp_close (int socket)
// Permet de réclamer la destruction dun socket.
// Engendre la fermeture de la connexion suivant le modèle de TCP.
// Retourne 0 si tout se passe bien et -1 en cas d'erreur
{ {
printf("[MIC-TCP] Appel de la fonction : "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction : "); printf(__FUNCTION__); printf("\n");
return -1; return -1;
} }
/*
* Traitement dun PDU MIC-TCP reçu (mise à jour des numéros de séquence
* et d'acquittement, etc.) puis insère les données utiles du PDU dans
* le buffer de réception du socket. Cette fonction utilise la fonction
* app_buffer_add().
*/
void process_received_PDU(mic_tcp_pdu pdu) void process_received_PDU(mic_tcp_pdu pdu)
// Gère le traitement dun PDU MIC-TCP reçu (mise à jour des numéros de séquence
// et d'acquittement, etc.) puis insère les données utiles du PDU dans le buffer
// de réception du socket. Cette fonction utilise la fonction app_buffer_add().
{ {
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n"); printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
} }