Delete apd_test.c
git-svn-id: https://pif.pu-pm.univ-fcomte.fr/svn/loc@5 785a6c6c-259e-4ff1-8b91-dc31627914f0
This commit is contained in:
parent
29d23523ff
commit
2221ab58a0
|
@ -1,303 +0,0 @@
|
|||
/*
|
||||
* This is the rtapanalyser library, Wi-Fi packet sniffer and analyser,
|
||||
* thanks to the radiotap header of each packet.
|
||||
*/
|
||||
|
||||
|
||||
#include "ap.h"
|
||||
|
||||
extern BOOL run ;
|
||||
|
||||
unsigned char *mac ;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
struct sigaction action ; // Structure de mise en place des gestionnaires de signaux
|
||||
int sockfd ; // Descripteur de la socket vers le serveur d'aggrégation
|
||||
struct sockaddr_in server, client ;
|
||||
sockfd = create_udp_sending_socket("127.0.0.1", AGGREGATE_DEFAULT_PORT, &server, &client);
|
||||
mac = malloc(sizeof(unsigned char) * 6) ; // Adresse MAC de l'AP
|
||||
|
||||
/* Mise en place des gestionnaires de signaux */
|
||||
sigemptyset(&action.sa_mask) ;
|
||||
action.sa_handler = sigint_handler ;
|
||||
sigaction(SIGINT, &action, NULL) ;
|
||||
action.sa_handler = sigterm_handler ;
|
||||
sigaction(SIGTERM, &action, NULL) ;
|
||||
|
||||
get_mac_addr("eth1", mac) ;
|
||||
printf("ma mac est %s\n", mac_bytes_to_string(mac)) ;
|
||||
|
||||
while (run)
|
||||
{
|
||||
couple_message couple ;
|
||||
memcpy(couple.ap_mac_addr_bytes, mac, 6); // On copie la MAC de l'AP
|
||||
gettimeofday(&couple.start_time, NULL) ;
|
||||
memcpy(&couple.request_time, &couple.mobile_mac_addr_bytes[3], sizeof(struct timeval));
|
||||
couple.request_time.tv_usec = abs(couple.request_time.tv_usec) ;
|
||||
memcpy(&couple.mobile_mac_addr_bytes, &couple.start_time, 6); // Dans le cas du beacon, l'adresse MAC est 10 octets plus loin
|
||||
couple.antenna_signal_dbm = couple.mobile_mac_addr_bytes[0] ;
|
||||
couple.x_position = 4644.647 ;
|
||||
couple.y_position = 43.788 ;
|
||||
couple.z_position = 553.99 ;
|
||||
couple.direction = SOUTH ;
|
||||
|
||||
printf("\ncouple à envoyer :\nAP MAC : %s\nMobile MAC : %s\nRequest time : %ld\nsignal : %u\nX : %f\nY : %f\nZ : %f\nDirection : %hhd\n", mac_bytes_to_string(couple.ap_mac_addr_bytes), mac_bytes_to_string(couple.mobile_mac_addr_bytes), couple.request_time.tv_usec, couple.antenna_signal_dbm, couple.x_position, couple.y_position, couple.z_position, couple.direction) ;
|
||||
|
||||
/* Envoi du couple au serveur d'aggrégation */
|
||||
sendto(sockfd, (void *) &couple, sizeof(couple), 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
|
||||
|
||||
sleep(1) ;
|
||||
}
|
||||
|
||||
//capture("rtap0", TRUE) ;
|
||||
|
||||
/*
|
||||
memcpy(mac, "abcdef", 6) ;
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
memcpy(mac, "azerty", 6) ;
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
memcpy(mac, "ghijkl", 6) ;
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
printf("%lu\n", get_mobile_sequence(&known_mobiles, mac));
|
||||
*/
|
||||
|
||||
printf("apd : fin\n") ;
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Capture des paquets en utilisant l'interface "capture_iface" pendant "capture_time" mili-secondes.
|
||||
Les informations concernant les mobiles effectuant des requêtes sont conservées dans la liste "known_mobiles".
|
||||
Les données capturées sont envoyées au serveur d'aggrégation à travers la socket UDP "socket".
|
||||
*/
|
||||
int capture(char *capture_iface, BOOL print_values)
|
||||
{
|
||||
pcap_t *handle ; // Descripteur de capture de paquets
|
||||
char errbuf[PCAP_ERRBUF_SIZE] ;// Message d'erreur
|
||||
// struct timeval tbegin, tcurrent ;
|
||||
int sockfd ; // Descripteur de la socket vers le serveur d'aggrégation
|
||||
struct sockaddr_in server, client ;
|
||||
|
||||
|
||||
/* Sous-fonction de traitement des paquets capturés */
|
||||
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
|
||||
{
|
||||
read_packet(args, header, packet, sockfd, &server, print_values) ; // On appelle la fonction read_packet() avec les mêmes arguments, plus "print_values" qui indique si on doit afficher le paquet.
|
||||
}
|
||||
|
||||
|
||||
handle = pcap_open_live(capture_iface, BUFSIZ, 1, 1000, errbuf) ; // Début de la capture
|
||||
if (handle == NULL) // Le lancement de la capture a-t-il échoué ?
|
||||
{
|
||||
fprintf(stderr, "Impossible d'ouvrir l'interface « %s » : %s\n", capture_iface, errbuf) ;
|
||||
return(ERR_OPENING_IFACE) ;
|
||||
}
|
||||
|
||||
/* Ouverture de la socket UDP vers le serveur d'aggrégation */
|
||||
sockfd = create_udp_sending_socket("127.0.0.1", AGGREGATE_DEFAULT_PORT, &server, &client);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
perror("Erreur ! Impossible de créer la socket vers le serveur d'aggrégation \n");
|
||||
exit(-1); // FIXME
|
||||
}
|
||||
|
||||
while(run)
|
||||
{
|
||||
pcap_loop(handle, 1, got_packet, NULL) ; // Collecte 1 paquet et appelle la fonction got_packet quand pcaploop a recupéré des paquets
|
||||
}
|
||||
|
||||
pcap_close(handle) ; // Arrêt de la capture.
|
||||
(void) close(sockfd) ; // Fermeture de la socket
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Traite un paquet et l'envoie au serveur d'agrégation */
|
||||
void read_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet, int sockfd, struct sockaddr_in *server, BOOL print_values)
|
||||
{
|
||||
unsigned char *data = (unsigned char *) packet ; // Recopie dans data l'adresse du paquet capturé
|
||||
unsigned short int rtap_bytes ; // Taille des données reçues
|
||||
unsigned char c ;
|
||||
int i ;
|
||||
unsigned int rtap_presentflags, rtap_position ;
|
||||
couple_message couple ;
|
||||
ssize_t nsent ; // Retour de sendto
|
||||
BOOL check[15] ; // Champs présents
|
||||
char packet_type ;
|
||||
//char offset = 1 ;
|
||||
char direction ; // Direction de la calibration
|
||||
float posX, posY, posZ;
|
||||
FILE *fd ;
|
||||
|
||||
memcpy(&rtap_bytes, &data[2], sizeof(unsigned short int)) ; // Recopie les deux octets à partir du troisième octet de donnée qui est la taille de l'en-tête rtap (change avec des flags)
|
||||
c = data[rtap_bytes] ; // Au bout de l'en-tête rtap il y a celle du 802.11 dont le premier determine le type (beacon ou pas)
|
||||
if (c == 0x08 // Si le paquet est de type data,
|
||||
&& data[rtap_bytes+24+8+9] == 0x11 // et de protocole UDP (24 : en-tête 802.11, 8 : en-tête LLC, 9 : position du champ "Protocol" de l'en-tête IP),
|
||||
&& data[rtap_bytes+24+8+20+2] == 0x26 && data[rtap_bytes+24+8+20+3] == 0xac) // et le port de destination est 9900 (20 : longueur de l'en-tête IP, le port de destination étant les troisièmes et quatrièmes octet suivant).
|
||||
{
|
||||
memcpy(couple.ap_mac_addr_bytes, mac, 6); // On copie la MAC de l'AP
|
||||
memcpy(couple.mobile_mac_addr_bytes, &data[rtap_bytes+10], 6); // Dans le cas du beacon, l'adresse MAC est 10 octets plus loin
|
||||
gettimeofday(&couple.start_time, NULL) ;
|
||||
|
||||
packet_type = data[rtap_bytes+24+8+20+8] ;
|
||||
|
||||
memcpy(&couple.request_time, &data[rtap_bytes+24+8+20+8+1], sizeof(struct timeval));
|
||||
|
||||
|
||||
printf("coin\n");
|
||||
|
||||
if (packet_type == 0)
|
||||
printf("Paquet normal\n") ;
|
||||
else if (packet_type == 1)
|
||||
{
|
||||
printf("Paquet de calibration\n") ;
|
||||
|
||||
direction = data[rtap_bytes+24+8+20+8 + 9];
|
||||
|
||||
memcpy(&posX, &data[rtap_bytes+24+8+20+8+10], sizeof(float));
|
||||
memcpy(&posY, &data[rtap_bytes+24+8+20+8+14], sizeof(float));
|
||||
memcpy(&posZ, &data[rtap_bytes+24+8+20+8+18], sizeof(float));
|
||||
}
|
||||
|
||||
if (print_values)
|
||||
printf("[%s]\n", mac_bytes_to_string(couple.mobile_mac_addr_bytes)) ;
|
||||
|
||||
memcpy(&rtap_presentflags, &data[RTAP_P_PRESENTFLAGS], RTAP_L_PRESENTFLAGS); // Récupère les flags de l'en-tête rtap
|
||||
|
||||
|
||||
for (i = 0 ; i < 15 ; i++)
|
||||
check[i] = FALSE ;
|
||||
rtap_position = 8 ; // début des champs déterminés par le present flag
|
||||
|
||||
for(i=0 ; i < 14 ; i++) // on teste les 15 premiers bits du champ flag afin de valider la présence et de les copier
|
||||
{
|
||||
if((rtap_presentflags % 2) == 1)
|
||||
{
|
||||
switch(i)
|
||||
{
|
||||
case RTAP_MACTS:
|
||||
check[RTAP_MACTS] = TRUE ;
|
||||
rtap_position += RTAP_L_MACTS ;
|
||||
break ;
|
||||
case RTAP_FLAGS:
|
||||
// memcpy(&(tmp_ss->flags), &data[rtap_position], RTAP_L_FLAGS) ;
|
||||
check[RTAP_FLAGS] = TRUE;
|
||||
rtap_position += RTAP_L_FLAGS ;
|
||||
break ;
|
||||
case RTAP_RATE:
|
||||
check[RTAP_RATE] = TRUE;
|
||||
rtap_position += RTAP_L_RATE ;
|
||||
break ;
|
||||
case RTAP_CHANNEL:
|
||||
rtap_position += RTAP_L_CHANNEL ;
|
||||
rtap_position += RTAP_L_CHANNELTYPE ;
|
||||
break ;
|
||||
case RTAP_FHSS:
|
||||
check[RTAP_FHSS] = TRUE;
|
||||
rtap_position += RTAP_L_FHSS ;
|
||||
break ;
|
||||
case RTAP_ANTENNASIGNALDBM:
|
||||
memcpy(&(couple.antenna_signal_dbm), &data[rtap_position], RTAP_L_ANTENNASIGNALDBM) ;
|
||||
check[RTAP_ANTENNASIGNALDBM] = TRUE;
|
||||
if (print_values)
|
||||
printf("Antenna Signal : %d dBm\n", couple.antenna_signal_dbm - 0x100);
|
||||
rtap_position += RTAP_L_ANTENNASIGNALDBM ;
|
||||
break ;
|
||||
case RTAP_ANTENNANOISEDBM:
|
||||
check[RTAP_ANTENNANOISEDBM] = TRUE;
|
||||
rtap_position += RTAP_L_ANTENNANOISEDBM ;
|
||||
break ;
|
||||
case RTAP_LOCKQUALITY:
|
||||
check[RTAP_LOCKQUALITY] = TRUE;
|
||||
rtap_position += RTAP_L_LOCKQUALITY ;
|
||||
break ;
|
||||
case RTAP_TXATTENUATION:
|
||||
check[RTAP_TXATTENUATION] = TRUE;
|
||||
rtap_position += RTAP_L_TXATTENUATION ;
|
||||
break ;
|
||||
case RTAP_TXATTENUATIONDB:
|
||||
check[RTAP_TXATTENUATIONDB] = TRUE;
|
||||
rtap_position += RTAP_L_TXATTENUATIONDB ;
|
||||
break ;
|
||||
case RTAP_TXATTENUATIONDBM:
|
||||
check[RTAP_TXATTENUATIONDBM] = TRUE;
|
||||
rtap_position += RTAP_L_TXATTENUATIONDBM ;
|
||||
break ;
|
||||
case RTAP_ANTENNA:
|
||||
check[RTAP_ANTENNA] = TRUE;
|
||||
rtap_position += RTAP_L_ANTENNA ;
|
||||
break ;
|
||||
case RTAP_ANTENNASIGNALDB:
|
||||
check[RTAP_ANTENNASIGNALDB] = TRUE;
|
||||
rtap_position += RTAP_L_ANTENNASIGNALDB ;
|
||||
break ;
|
||||
case RTAP_ANTENNANOISEDB:
|
||||
check[RTAP_ANTENNANOISEDB] = TRUE;
|
||||
rtap_position += RTAP_L_ANTENNANOISEDB ;
|
||||
break ;
|
||||
case RTAP_FCS:
|
||||
check[RTAP_FCS] = TRUE;
|
||||
rtap_position += RTAP_L_FCS ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
rtap_presentflags /= 2 ;
|
||||
}
|
||||
|
||||
|
||||
if (packet_type == 1)
|
||||
{
|
||||
// coord direction mac_ap puissance
|
||||
printf("hey\n") ;
|
||||
fd = fopen("./calibration.out", "a") ;
|
||||
fprintf(fd, "%f, %f, %f, %d, %s, %d\n", posX, posY, posZ, direction, mac_bytes_to_string(mac), couple.antenna_signal_dbm - 0x100) ;
|
||||
}
|
||||
|
||||
if (print_values)
|
||||
printf("\n") ;
|
||||
|
||||
printf("couple à envoyer :\nAP MAC : %s\nMobile MAC : %s\nRequest time : %ld\n", mac_bytes_to_string(couple.ap_mac_addr_bytes), mac_bytes_to_string(couple.mobile_mac_addr_bytes), couple.request_time.tv_usec) ;
|
||||
|
||||
/* Envoi du couple au serveur d'aggrégation */
|
||||
nsent = sendto(sockfd, (void *) &couple, sizeof(couple), 0, (struct sockaddr *) server, (socklen_t) sizeof(*server)) ;
|
||||
if (nsent != (ssize_t) sizeof(couple))
|
||||
{
|
||||
perror("Erreur lors de l'envoi du couple au serveur ") ;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Fonction permettant de récupérer sa propre adresse MAC (interface 'eth') dans le tableau 'mac_bytes' */
|
||||
void get_mac_addr(char *eth, unsigned char mac_bytes[6])
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int sockfd ;
|
||||
|
||||
bzero(mac_bytes, sizeof(unsigned char) * 6) ; // RàZ
|
||||
|
||||
sockfd = socket(AF_INET, SOCK_DGRAM, 0) ;
|
||||
if(sockfd < 0)
|
||||
printf("Can't open socket\n") ;
|
||||
|
||||
strncpy(ifr.ifr_name, eth, IFNAMSIZ) ;
|
||||
|
||||
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
|
||||
return ;
|
||||
|
||||
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0)
|
||||
return ;
|
||||
|
||||
memcpy(mac_bytes, ifr.ifr_hwaddr.sa_data, 6) ;
|
||||
}
|
Loading…
Reference in New Issue