Modified to compiled with -ansi and -Wall
-pedantic was not set to allow for __FUNCTION__ use in printf
This commit is contained in:
parent
8ff0603d6a
commit
ddeca822aa
8 changed files with 215 additions and 200 deletions
2
Makefile
2
Makefile
|
@ -25,7 +25,7 @@ vpath %.c $(SRC_DIR)
|
|||
|
||||
define make-goal
|
||||
$1/%.o: %.c
|
||||
$(CC) -m32 -g -I $(INCLUDES) -c $$< -o $$@
|
||||
$(CC) -ansi -Wall -m32 -g -I $(INCLUDES) -c $$< -o $$@
|
||||
endef
|
||||
|
||||
.PHONY: all checkdirs clean
|
||||
|
|
|
@ -4,19 +4,24 @@
|
|||
#include <mictcp.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 IP_send(mic_tcp_pdu, mic_tcp_sock_addr);
|
||||
int IP_recv(mic_tcp_payload*, mic_tcp_sock_addr*, unsigned long delay);
|
||||
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);
|
||||
unsigned long get_now_time_msec();
|
||||
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 partial_send(mic_tcp_payload);
|
||||
mic_tcp_payload get_full_stream(mic_tcp_pdu);
|
||||
|
|
|
@ -15,18 +15,24 @@
|
|||
#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
|
||||
{
|
||||
IDLE, CLOSED, SYN_SENT, SYN_RECEIVED, ESTABLISHED, CLOSING
|
||||
} protocol_state;
|
||||
|
||||
// Mode de démarrage du protocole
|
||||
// NB : nécessaire à l’usage de la fonction initialize_components()
|
||||
/*
|
||||
* Mode de démarrage du protocole
|
||||
* NB : nécessaire à l’usage de la fonction initialize_components()
|
||||
*/
|
||||
typedef enum start_mode { CLIENT, SERVER } start_mode;
|
||||
|
||||
// Structure d’une adresse de socket
|
||||
/*
|
||||
* Structure d’une adresse de socket
|
||||
*/
|
||||
typedef struct mic_tcp_sock_addr
|
||||
{
|
||||
char * ip_addr;
|
||||
|
@ -34,38 +40,46 @@ typedef struct mic_tcp_sock_addr
|
|||
unsigned short port;
|
||||
} mic_tcp_sock_addr;
|
||||
|
||||
// Structure d'un socket
|
||||
/*
|
||||
* Structure d'un socket
|
||||
*/
|
||||
typedef struct mic_tcp_sock
|
||||
{
|
||||
int fd; // descripteur du socket
|
||||
protocol_state state; // état du protocole
|
||||
mic_tcp_sock_addr addr; // adresse du socket
|
||||
int fd; /* descripteur du socket */
|
||||
protocol_state state; /* état du protocole */
|
||||
mic_tcp_sock_addr addr; /* adresse du socket */
|
||||
} mic_tcp_sock;
|
||||
|
||||
// Structure des données utiles d’un PDU MIC-TCP
|
||||
/*
|
||||
* Structure des données utiles d’un PDU MIC-TCP
|
||||
*/
|
||||
typedef struct mic_tcp_payload
|
||||
{
|
||||
char* data; // données applicatives
|
||||
int size; // taille des données
|
||||
char* data; /* données applicatives */
|
||||
int size; /* taille des données */
|
||||
} 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
|
||||
{
|
||||
unsigned short source_port; // numéro de port source
|
||||
unsigned short dest_port; // numéro de port de destination
|
||||
unsigned int seq_num; // numéro de séquence
|
||||
unsigned int ack_num; // numéro d'acquittement
|
||||
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 fin; // flag FIN (valeur 1 si activé et 0 si non)
|
||||
unsigned short source_port; /* numéro de port source */
|
||||
unsigned short dest_port; /* numéro de port de destination */
|
||||
unsigned int seq_num; /* numéro de séquence */
|
||||
unsigned int ack_num; /* numéro d'acquittement */
|
||||
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 fin; /* flag FIN (valeur 1 si activé et 0 si non) */
|
||||
} mic_tcp_header;
|
||||
|
||||
// Structure d'un PDU MIC-TCP
|
||||
/*
|
||||
* Structure d'un PDU MIC-TCP
|
||||
*/
|
||||
typedef struct mic_tcp_pdu
|
||||
{
|
||||
mic_tcp_header header ; // entête du PDU
|
||||
mic_tcp_payload payload; // charge utile du PDU ⇔ données applicatives
|
||||
mic_tcp_header header ; /* entête du PDU */
|
||||
mic_tcp_payload payload; /* charge utile du PDU */
|
||||
} mic_tcp_pdu;
|
||||
|
||||
typedef struct app_buffer
|
||||
|
@ -76,7 +90,9 @@ typedef struct app_buffer
|
|||
} app_buffer;
|
||||
|
||||
|
||||
// Fonctions de l'interface
|
||||
/****************************
|
||||
* Fonctions de l'interface *
|
||||
****************************/
|
||||
int mic_tcp_socket(start_mode sm);
|
||||
int mic_tcp_bind(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);
|
||||
int mic_tcp_close(int socket);
|
||||
|
||||
// Variables globales
|
||||
/**********************
|
||||
* Variables globales *
|
||||
**********************/
|
||||
mic_tcp_sock local_sock_src;
|
||||
mic_tcp_sock local_sock_dest;
|
||||
unsigned long timer;
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
#include <api/mictcp_core.h>
|
||||
#include <pthread.h>
|
||||
#include <strings.h>
|
||||
|
||||
// API Variables
|
||||
/*****************
|
||||
* API Variables *
|
||||
*****************/
|
||||
int first_free = 0;
|
||||
int initialized = -1;
|
||||
int sys_socket;
|
||||
|
@ -27,11 +30,13 @@ app_buffer* app_buffer_last = NULL;
|
|||
unsigned int app_buffer_size = 0;
|
||||
unsigned int app_buffer_count = 0;
|
||||
|
||||
// Fonctions Utilitaires
|
||||
|
||||
/*************************
|
||||
* Fonctions Utilitaires *
|
||||
*************************/
|
||||
int initialize_components(start_mode mode)
|
||||
{
|
||||
int s;
|
||||
int bnd;
|
||||
struct hostent * hp;
|
||||
if(initialized != -1) return initialized;
|
||||
if((sys_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1;
|
||||
else initialized = 1;
|
||||
|
@ -42,7 +47,7 @@ int initialize_components(start_mode mode)
|
|||
local_addr.sin_family = AF_INET;
|
||||
local_addr.sin_port = htons(API_CS_Port);
|
||||
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)
|
||||
{
|
||||
|
@ -53,7 +58,7 @@ int initialize_components(start_mode mode)
|
|||
memset((char *) &remote_addr, 0, sizeof(local_addr));
|
||||
remote_addr.sin_family = AF_INET;
|
||||
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);
|
||||
remote_size = sizeof(remote_addr);
|
||||
initialized = 1;
|
||||
|
@ -68,7 +73,7 @@ int initialize_components(start_mode mode)
|
|||
memset((char *) &remote_addr, 0, sizeof(local_addr));
|
||||
remote_addr.sin_family = AF_INET;
|
||||
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);
|
||||
remote_size = sizeof(remote_addr);
|
||||
|
||||
|
@ -76,7 +81,7 @@ int initialize_components(start_mode mode)
|
|||
local_addr.sin_family = AF_INET;
|
||||
local_addr.sin_port = htons(API_SC_Port);
|
||||
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,10 +112,10 @@ 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)
|
||||
{
|
||||
// Send data over a fake IP
|
||||
if(initialized == -1) return -1;
|
||||
|
||||
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;}
|
||||
else {tv.tv_sec = 0; tv.tv_usec = delay;}
|
||||
|
@ -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)
|
||||
{
|
||||
// Get a full packet from data and header
|
||||
/* Get a full packet from data and header */
|
||||
mic_tcp_payload tmp;
|
||||
tmp.size = 15 + pk.payload.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)
|
||||
{
|
||||
// Get a struct header from an incoming packet
|
||||
/* Get a struct header from an incoming packet */
|
||||
mic_tcp_header tmp;
|
||||
memcpy(&tmp, packet, 15);
|
||||
return tmp;
|
||||
|
@ -199,14 +204,14 @@ int partial_send(mic_tcp_payload buff)
|
|||
|
||||
int app_buffer_get(mic_tcp_payload app_buff)
|
||||
{
|
||||
mic_tcp_payload tmp;
|
||||
app_buffer* current;
|
||||
|
||||
while(app_buffer_count == 0)
|
||||
{
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
mic_tcp_payload tmp;
|
||||
|
||||
app_buffer* current;
|
||||
if(app_buffer_count > 0)
|
||||
{
|
||||
pthread_mutex_lock(&lock);
|
||||
|
@ -232,11 +237,12 @@ int app_buffer_get(mic_tcp_payload app_buff)
|
|||
|
||||
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);
|
||||
|
@ -268,18 +274,19 @@ int app_buffer_set(mic_tcp_payload bf)
|
|||
|
||||
void* listening(void* arg)
|
||||
{
|
||||
pthread_mutex_init(&lock, NULL);
|
||||
|
||||
printf("[MICTCP-CORE] Demarrage du thread de reception reseau...\n");
|
||||
|
||||
mic_tcp_payload tmp_buff;
|
||||
tmp_buff.size = 1500;
|
||||
tmp_buff.data = malloc(1500);
|
||||
|
||||
mic_tcp_pdu pdu_tmp;
|
||||
int recv_size;
|
||||
mic_tcp_sock_addr remote;
|
||||
mic_tcp_payload tmp_buff;
|
||||
|
||||
pthread_mutex_init(&lock, NULL);
|
||||
|
||||
printf("[MICTCP-CORE] Demarrage du thread de reception reseau...\n");
|
||||
|
||||
tmp_buff.size = 1500;
|
||||
tmp_buff.data = malloc(1500);
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
tmp_buff.size = 1500;
|
||||
|
@ -292,7 +299,6 @@ void* listening(void* arg)
|
|||
process_received_PDU(pdu_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ int main()
|
|||
{
|
||||
|
||||
int sockfd = 0;
|
||||
char chaine[MAX_SIZE];
|
||||
mic_tcp_sock_addr addr;
|
||||
addr.ip_addr = "127.0.0.1";
|
||||
addr.port = 1234;
|
||||
|
@ -32,7 +33,6 @@ int main()
|
|||
printf("[TSOCK] Connexion du socket MICTCP: OK\n");
|
||||
}
|
||||
|
||||
char chaine[MAX_SIZE];
|
||||
memset(chaine, 0, MAX_SIZE);
|
||||
|
||||
printf("[TSOCK] Entrez vos message a envoyer, CTRL+D pour quitter\n");
|
||||
|
|
|
@ -24,19 +24,22 @@ extern errno;
|
|||
*/
|
||||
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;
|
||||
|
||||
// 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];
|
||||
|
||||
/* 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 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);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
// 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
|
||||
/* Main activity loop, we never exit this, user terminates with SIGKILL */
|
||||
while(1) {
|
||||
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) {
|
||||
// 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) {
|
||||
printf("Simulating TCP loss\n");
|
||||
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);
|
||||
if (n < 0) {
|
||||
perror(0);
|
||||
}
|
||||
}
|
||||
|
||||
// We never execute this but anyway, for sanity
|
||||
/* We never execute this but anyway, for sanity */
|
||||
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) {
|
||||
|
||||
// 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;
|
||||
|
||||
// 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];
|
||||
|
||||
// Addresses for the work to be performed
|
||||
struct sockaddr_in serv_addr = listen_on;
|
||||
/* Addresses for the work to be performed */
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t len = sizeof(cliaddr);
|
||||
|
||||
// We construct the socket to be used by this function
|
||||
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
|
||||
/* Initialize a packet count variable used when loss emulation is active */
|
||||
int count = 0;
|
||||
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;
|
||||
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)) {
|
||||
bzero(buffer,MAX_UDP_SEGMENT_SIZE);
|
||||
|
||||
n = fread(¤tTime, 1, sizeof(struct timespec), fd);
|
||||
if(firstValue > 0) {
|
||||
// We need to sleep a while
|
||||
/* We need to sleep a while */
|
||||
struct timespec difference = tsSubtract(currentTime, lastTime);
|
||||
nanosleep(&difference, &rem);
|
||||
} else {
|
||||
|
@ -164,7 +153,7 @@ void file_to_tcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to, i
|
|||
}
|
||||
|
||||
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) {
|
||||
printf("Simulating TCP loss\n");
|
||||
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);
|
||||
if (n < 0) {
|
||||
perror(0);
|
||||
}
|
||||
}
|
||||
|
||||
// We never execute this but anyway, for sanity
|
||||
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) {
|
||||
|
||||
// Define the socket on which we listen
|
||||
/* Define the socket on which we listen */
|
||||
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];
|
||||
|
||||
// Addresses for the work to be performed
|
||||
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;
|
||||
/* Initialize a packet count variable used when loss emulation is active */
|
||||
ssize_t n = -1;
|
||||
|
||||
FILE * fd = fopen("../video/video.bin", "rb");
|
||||
|
||||
struct timespec currentTimeFile;
|
||||
struct timespec firstTimeFile;
|
||||
struct timespec lastTimeFile;
|
||||
struct timespec timeFirstPacket;
|
||||
struct timespec currentTime;
|
||||
struct timespec rem;
|
||||
int firstValue = 0;
|
||||
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) {
|
||||
bzero(buffer, MAX_UDP_SEGMENT_SIZE);
|
||||
|
||||
n = fread(¤tTimeFile, 1, sizeof(struct timespec), fd);
|
||||
if(firstValue > 0) {
|
||||
// We need to sleep a while
|
||||
/* We need to sleep a while */
|
||||
if( clock_gettime( CLOCK_REALTIME, ¤tTime) == -1 ) {
|
||||
perror( "clock gettime" );
|
||||
}
|
||||
|
@ -264,7 +237,6 @@ void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to
|
|||
}
|
||||
firstValue++;
|
||||
}
|
||||
lastTimeFile = currentTimeFile;
|
||||
n = fread(buffer, 1, sizeof(n), fd);
|
||||
n = fread(buffer, 1, *((int *)buffer), fd);
|
||||
if (n <= 0) {
|
||||
|
@ -274,17 +246,17 @@ void file_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to
|
|||
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);
|
||||
if (n <= 0) {
|
||||
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);
|
||||
|
||||
// Same for MICTCP
|
||||
/* Same for MICTCP */
|
||||
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) {
|
||||
|
||||
// Define the socket on which we listen
|
||||
/* Define the socket on which we listen */
|
||||
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];
|
||||
|
||||
// Addresses for the work to be performed
|
||||
struct sockaddr_in serv_addr = listen_on;
|
||||
/* Addresses for the work to be performed */
|
||||
struct sockaddr_in cliaddr;
|
||||
struct sockaddr_in serv_addr = listen_on;
|
||||
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;
|
||||
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
|
||||
/* 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
|
||||
/* 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");
|
||||
}
|
||||
|
@ -334,12 +309,7 @@ void udp_to_mictcp(struct sockaddr_in listen_on, struct sockaddr_in transmit_to)
|
|||
|
||||
listen(listen_sockfd,5);
|
||||
|
||||
|
||||
// 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
|
||||
/* Main activity loop, we never exit this, user terminates with SIGKILL */
|
||||
while(1) {
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
if (n <= 0) {
|
||||
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);
|
||||
|
||||
// Same for MICTCP
|
||||
/* Same for MICTCP */
|
||||
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) {
|
||||
|
||||
// Define the socket on which we listen
|
||||
/* Define the socket on which we listen */
|
||||
int sending_sockfd;
|
||||
|
||||
// A buffer used to store received segments before they are forwarded
|
||||
char buffer[MAX_UDP_SEGMENT_SIZE];
|
||||
int rcv_values [CUMULATED_BUFF_NB];
|
||||
/* A buffer used to store received segments before they are forwarded */
|
||||
char* Buff [CUMULATED_BUFF_NB];
|
||||
int 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);
|
||||
}
|
||||
|
||||
// Addresses for the work to be performed
|
||||
struct sockaddr_in serv_addr = listen_on;
|
||||
/* Addresses for the work to be performed */
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t len = sizeof(cliaddr);
|
||||
|
||||
// MICTCP stuff
|
||||
/* MICTCP stuff */
|
||||
int mic_tcp_sockfd;
|
||||
start_mode start_mode_mic_tcp = SERVER;
|
||||
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.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);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
// Initialize a packet count variable used when loss emulation is active
|
||||
int count = 0;
|
||||
/* Initialize a packet count variable used when loss emulation is active */
|
||||
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) {
|
||||
int 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++)
|
||||
{
|
||||
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);
|
||||
|
||||
// Same for MICTCP
|
||||
/* Same for MICTCP */
|
||||
mic_tcp_close(mic_tcp_sockfd);
|
||||
}
|
||||
|
||||
|
@ -455,20 +421,20 @@ static void usage(void) {
|
|||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
// Should losses be emulated?
|
||||
/* Should losses be emulated? */
|
||||
int loss = 1;
|
||||
int transport = 0;
|
||||
int puits = -1;
|
||||
|
||||
// What sockaddr should this program listen on?
|
||||
// Always on port 1234 (data from VLC arrives there using UDP)
|
||||
/* What sockaddr should this program listen on? */
|
||||
/* Always on port 1234 (data from VLC arrives there using UDP) */
|
||||
struct sockaddr_in serv_addr;
|
||||
bzero((char *) &serv_addr, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
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;
|
||||
bzero((char *) &dest_addr, sizeof(dest_addr));
|
||||
dest_addr.sin_family = AF_INET;
|
||||
|
@ -530,19 +496,20 @@ int main(int argc, char ** argv) {
|
|||
|
||||
if(transport == 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);
|
||||
} else {
|
||||
printf("No gateway needed for puits using UDP\n");
|
||||
}
|
||||
} else if(transport == 1) {
|
||||
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);
|
||||
} 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);
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,10 +7,12 @@ int main()
|
|||
{
|
||||
int sockfd;
|
||||
mic_tcp_sock_addr addr;
|
||||
mic_tcp_sock_addr remote_addr;
|
||||
char chaine[MAX_SIZE];
|
||||
|
||||
addr.ip_addr = "127.0.0.1";
|
||||
addr.port = 1234;
|
||||
|
||||
mic_tcp_sock_addr remote_addr;
|
||||
|
||||
if ((sockfd = mic_tcp_socket(SERVER)) == -1)
|
||||
{
|
||||
|
@ -43,14 +45,14 @@ int main()
|
|||
}
|
||||
|
||||
|
||||
char chaine[MAX_SIZE];
|
||||
memset(chaine, 0, MAX_SIZE);
|
||||
|
||||
printf("[TSOCK] Appuyez sur CTRL+C pour quitter ...\n");
|
||||
|
||||
while(1) {
|
||||
int rcv_size = 0;
|
||||
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] Message Recu : %s", chaine);
|
||||
}
|
||||
|
|
63
src/mictcp.c
63
src/mictcp.c
|
@ -1,71 +1,88 @@
|
|||
#include <mictcp.h>
|
||||
#include <api/mictcp_core.h>
|
||||
|
||||
/*
|
||||
* Permet de créer un socket entre l’application et MIC-TCP
|
||||
* Retourne le descripteur du socket ou bien -1 en cas d'erreur
|
||||
*/
|
||||
int mic_tcp_socket(start_mode sm)
|
||||
// Permet de créer un socket entre l’application 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");
|
||||
initialize_components(sm); // Appel obligatoire
|
||||
initialize_components(sm); /* Appel obligatoire */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Permet d’attribuer 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)
|
||||
// Permet d’attribuer 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");
|
||||
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)
|
||||
// Met l’application en état d'acceptation d’une requête de connexion entrante
|
||||
// Retourne 0 si succès, -1 si erreur
|
||||
{
|
||||
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Permet de réclamer l’établissement d’une 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)
|
||||
// Permet de réclamer l’établissement d’une 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");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Permet de réclamer l’envoi d’une 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)
|
||||
// Permet de réclamer l’envoi d’une 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");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Permet à l’application réceptrice de réclamer la récupération d’une donnée
|
||||
* stockée dans les buffers de réception du socket
|
||||
* Retourne le nombre d’octets lu ou bien -1 en cas d’erreur
|
||||
* NB : cette fonction fait appel à la fonction app_buffer_get()
|
||||
*/
|
||||
int mic_tcp_recv (int socket, char* mesg, int max_mesg_size)
|
||||
// Permet à l’application réceptrice de réclamer la récupération d’une donnée
|
||||
// stockée dans les buffers de réception du socket
|
||||
// Retourne le nombre d’octets lu ou bien -1 en cas d’erreur
|
||||
// NB : cette fonction fait appel à la fonction app_buffer_get()
|
||||
{
|
||||
printf("[MIC-TCP] Appel de la fonction: "); printf(__FUNCTION__); printf("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Permet de réclamer la destruction d’un 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)
|
||||
// Permet de réclamer la destruction d’un 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");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Traitement d’un 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)
|
||||
// Gère le traitement d’un 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");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue