NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
************************************************************************************************
* AnalyseurTCP.c *
* Analyse un fichier PCAP afin d'en extraire les paquets TCP et de calculer leurs débits *
* *
* Auteurs : *
* Katia AGNAMAZIAN, Loïc FAIZANT *
* *
* Ecole Polytech' Nice Sophia Antipolis *
* Sciences Informatiques - 4e année *
************************************************************************************************
*/

#include <pcap/pcap.h>
#define __FAVOR_BSD
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <string.h>
#include <sys/time.h>



#define MAX_DEBITS 256
#define TAILLE_BUFFER_ERREUR 256
#define MAX_FLUX 256

// --- Structure debit
typedef struct debit
{
struct timeval temps;
double debit;
} debit;

// --- Structure flux
typedef struct flux
{
struct in_addr adresse_source;
unsigned int port_source;
struct in_addr adresse_destination;
unsigned int port_destination;
double (*debits)[2];
int nb_debits;
} flux;

// --- Tableau des flux
flux tabFlux[MAX_FLUX];
// --- Nombre de flux stockés
int nb_flux = 0;


// --- Fonction comparerFlux
int comparerFlux(struct ip * entete_ip, struct tcphdr * entete_tcp)
{
int i;

// Parcourir le tableau des flux
for(i = 0; i < nb_flux; i++)
{
// Comparer les adresses IP source et destination et les ports source et destination
if(strcmp(inet_ntoa(tabFlux[i].adresse_source), inet_ntoa(entete_ip->ip_src)) == 0 && tabFlux[0].port_source == ntohs(entete_tcp->th_sport)
&& strcmp(inet_ntoa(tabFlux[i].adresse_destination), inet_ntoa(entete_ip->ip_dst)) == 0 && tabFlux[0].port_destination == ntohs(entete_tcp->th_dport))
return i;
}

return -1;
}

// --- Fonction ecrireFichier
void ecrireFichier(char * fichier, double (*debits)[2], int nbDebits)
{
FILE * f;
int i;

// Ouvrir le fichier passé en paramètre en écriture
if((f = fopen(fichier, "w")) == NULL)
{
perror("fopen");
exit(-1);
}

for(i = 0; i < nbDebits; i++)
fprintf(f, "%ft%fn", debits[i][0], debits[i][1]);

fclose(f);
}

// --- Fonction genererGraphique
void genererGraphique(char * fichier, double (*valeurs)[2], int nbValeurs)
{
// Définir les arguments pour la génération du graphe
char * arguments[2];
arguments[0] = malloc(sizeof(char)*50);
arguments[1] = malloc(sizeof(char)*50);
sprintf(arguments[0], "set title "%s"", fichier);
sprintf(arguments[1], "plot[*:*][*:100000]'%s'with linespoints", fichier);

// Ouvrir un fichier temporaire pour le passage des arguments
FILE * temp = fopen(fichier, "w");
// Lancer un processus pour la génération du graphe
FILE * gnuplotPipe = popen("gnuplot -persistent", "w");

// Ecrire les valeurs à tracer dans le fichier temporaire
int i;
for (i = 0; i < nbValeurs; i++)
fprintf(temp, "%lf %lf n", valeurs[i][0], valeurs[i][1]);

// Passer les arguments au processus de génération du graphe
for (i = 0; i < 2; i++)
fprintf(gnuplotPipe, "%s n", arguments[i]);

// Fermer le fichier temporaire et le processus de génération
fclose(temp);
fclose(gnuplotPipe);

}


// --- Fonction main
int main(int argc, char *argv[])
{
// Contrôler la validité du paramètre
if(argc != 2)
{
printf("Erreur de syntaxe : AnalyseurTCP <fichier_pcap>n");
exit(-1);
}

// Ouvrir le fichier PCAP passé en paramètre
char *chemin_fichier = argv[1];
char buffer_erreur[TAILLE_BUFFER_ERREUR];
pcap_t * fichier_pcap;

fichier_pcap = pcap_open_offline(chemin_fichier, buffer_erreur);
if(fichier_pcap == NULL)
{
perror("pcap_open_offline");
exit(-1);
}

// Parcourir les paquets contenus dans le fichier PCAP
struct pcap_pkthdr entete_pkt;
const u_char * paquet;
struct ip * entete_ip = NULL;
struct tcphdr * entete_tcp = NULL;
struct timeval instant_zero;
struct timeval instant_courant;
int numeroFlux;

while((paquet = pcap_next(fichier_pcap, &entete_pkt)) != NULL)
{
// Obtenir l'en-tête IP du paquet
entete_ip = (struct ip *)(paquet + 14);
// Contrôler la version d'IP du paquet : IPv4
if(entete_ip->ip_v != 4)
continue;
// Contrôler le protocole du paquet : TCP
if(entete_ip->ip_p != 6)
continue;
// Obtenir l'en-tête TCP du paquet
entete_tcp = (struct tcphdr *)(paquet + 14 + 20);

// Traiter le cas du premier flux
if(nb_flux == 0)
{
// Définir l'instant 0
instant_zero = entete_pkt.ts;

// Stocker le flux
tabFlux[0].adresse_source = entete_ip->ip_src;
tabFlux[0].port_source = ntohs(entete_tcp->th_sport);
tabFlux[0].adresse_destination = entete_ip->ip_dst;
tabFlux[0].port_destination = ntohs(entete_tcp->th_dport);
tabFlux[0].debits = malloc(sizeof(double *) * MAX_DEBITS);
tabFlux[0].debits[0][0] = 0;
tabFlux[0].debits[0][1] = 0;
tabFlux[0].nb_debits = 1;

nb_flux++;
continue;
}

// Contrôler l'éventuelle existence du flux
if((numeroFlux = comparerFlux(entete_ip, entete_tcp)) >= 0)
{
// Obtenir le nombre de débits stockés pour le flux
int numeroDebit = tabFlux[numeroFlux].nb_debits - 1;
// Contrôler le nombre de débits stockés pour le flux
if(numeroDebit >= MAX_DEBITS)
continue;
// Calculer l'instant du débit
timersub(&(entete_pkt.ts), &instant_zero, &instant_courant);
// Calculer la charge utile
int chargeUtile = ntohs(entete_ip->ip_len) - ((entete_ip->ip_hl*4) - (entete_tcp->th_off*4));
// Contrôler l'intervalle d'évaluation : 1 seconde
double intervalle;
intervalle = (instant_courant.tv_sec + (instant_courant.tv_usec / 1000000)) - tabFlux[numeroFlux].debits[numeroDebit][0];
if(intervalle < 1)
{
// Ajouter la charge utile au flux
tabFlux[numeroFlux].debits[numeroDebit][1] += chargeUtile;
}
else
{
// Calculer le débit
tabFlux[numeroFlux].debits[numeroDebit][1] = (tabFlux[numeroFlux].debits[numeroDebit][1] * 8) / 1024;
// Ajouter la charge utile au flux
numeroDebit++;
tabFlux[numeroFlux].debits[numeroDebit][0] = instant_courant.tv_sec + (instant_courant.tv_usec / 1000000);
tabFlux[numeroFlux].debits[numeroDebit][1] = chargeUtile;
tabFlux[numeroFlux].nb_debits++;
}
}
else
{
// Contrôler le nombre de flux stockés
if(nb_flux >= MAX_FLUX)
continue;

// Stocker le flux
tabFlux[nb_flux].adresse_source = entete_ip->ip_src;
tabFlux[nb_flux].port_source = ntohs(entete_tcp->th_sport);
tabFlux[nb_flux].adresse_destination = entete_ip->ip_dst;
tabFlux[nb_flux].port_destination = ntohs(entete_tcp->th_dport);
tabFlux[nb_flux].debits = malloc(sizeof(double *) * MAX_DEBITS);
tabFlux[nb_flux].debits[0][0] = 0;
tabFlux[nb_flux].debits[0][1] = 0;
tabFlux[nb_flux].nb_debits = 1;

nb_flux++;
}
}

// Parcourir les flux stockés
char * fichier_flux = malloc(sizeof(char)*10);
char * graphique_flux = malloc(sizeof(char)*10);
int i;
for(i = 0; i < nb_flux; i++)
{
// Contrôler le nombre de débits stockés dans le flux : au moins 2
if(tabFlux[i].nb_debits <= 1)
continue;

// Construire le chemin du fichier de flux
sprintf(fichier_flux, "th-%d.txt", i + 1);
// Ecrire le fichier de flux
ecrireFichier(fichier_flux, tabFlux[i].debits, tabFlux[i].nb_debits);

// Construire le chemin du graphique des débits
sprintf(graphique_flux, "th-%d.bmp", i + 1);
// Générer le graphe de débits du flux
genererGraphique(graphique_flux, tabFlux[i].debits, tabFlux[i].nb_debits);

// Réinitialiser les chemins des fichiers
memset(fichier_flux, 0, 10);
memset(graphique_flux, 0, 10);
}

// Fermer le fichier PCAP
pcap_close(fichier_pcap);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.