No Description

sniffer.h 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef SNIFFER_H
  2. #define SNIFFER_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <QDebug>
  6. #include "ip_packet.h"
  7. #include <QWaitCondition>
  8. #include <QMutex>
  9. #include <pcap.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <cstring>
  15. #include <errno.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include "ethernet_hdr.h"
  21. #include "ip_hdr.h"
  22. #include "tcp_hdr.h"
  23. #include "udp_hdr.h"
  24. /* default snap length (maximum bytes per packet to capture) */
  25. #define SNAP_LEN 1518
  26. /* ethernet headers are always exactly 14 bytes [1] */
  27. #define SIZE_ETHERNET 14
  28. #define IPV4 8
  29. ///
  30. /// \brief The Sniffer class is the one that use the pcap library to
  31. /// extract the packet information. It discards any packet that is not
  32. /// Ethernet->IP->(TCP|UDP), and pass up to the GUI the packets that are
  33. /// Ethernet->IP->(TCP|UDP).
  34. ///
  35. class Sniffer : public QObject
  36. {
  37. Q_OBJECT
  38. public:
  39. /// \fn Sniffer::Sniffer()
  40. /// \~English
  41. /// \brief Constructor, does nothing
  42. /// \~Spanish
  43. /// \brief Constructor, no hace nada
  44. Sniffer() ;
  45. /// \fn Sniffer::Sniffer(QWaitCondition *pw, QMutex *mx, int *ps)
  46. /// \~English
  47. /// \brief Sets the mutexes for the inter communication between
  48. /// the sniffer and the GUI.
  49. /// \param pw used to pause the capture of packets
  50. /// \param mx used to protect critical region
  51. /// \param ps flag to pause the packet capture
  52. /// \~Spanish
  53. /// \brief Ajusta los mutexes para la inter comunicacion entre
  54. /// el sniffer y el GUI.
  55. /// \param pw se usa para pausar la captura de paquetes
  56. /// \param mx usado para proteger la region critica
  57. /// \param ps bandera para pausar la captura de paquetes
  58. Sniffer(QWaitCondition *pw, QMutex *mx, int *ps) ;
  59. /// \fn Sniffer::~Sniffer()
  60. /// \~English
  61. /// \brief Destructor, does nothing
  62. /// \~Spanish
  63. /// \brief Destructor, no hace nada
  64. ~Sniffer() ;
  65. /// \fn vector<ip_packet> *Sniffer::getPacketList()
  66. /// \~English
  67. /// \brief Returns the packet list that contains the packets that are
  68. /// Ethernet -> IP -> (TCP|UDP)
  69. /// \return vector of packets
  70. /// \~Spanish
  71. /// \brief Devuelve la lista de paquetes que contiene los paquetes que son
  72. /// Ethernet -> IP -> (TCP|UDP)
  73. /// \return vector de paquetes
  74. vector<ip_packet> * getPacketList() ;
  75. /// \fn string Sniffer::format_payload(const char *payload, int len)
  76. /// \~English
  77. /// \brief Formats the payload from a byte stream into a string of ascii
  78. /// \param payload payload of the packet
  79. /// \param len length of the packet
  80. /// \return the payload in a string of ascii
  81. /// \~Spanish
  82. /// \brief Formatea la carga de un torrente de bytes a un cadena de
  83. /// caracteres ascii
  84. /// \param payload carga del paquete
  85. /// \param len largo del paquete
  86. /// \return la carga en una cadena de caracteres ascii
  87. string format_payload(const char *payload, int len);
  88. /// \fn string Sniffer::format_hex_ascii_line(const char *payload, int len, int offset)
  89. /// \~English
  90. /// \brief Return string with the bytes of a payload line in ascii
  91. /// \param payload payload of the packet
  92. /// \param len length of the packet
  93. /// \param offset offset of packet
  94. /// \return a string with the bytes of a payload line in ascii
  95. /// \~Spanish
  96. /// \brief Devuelve una cadena de caracteres con los bytes de una linea
  97. /// de la carga en ascii
  98. /// \param payload carga del paquete
  99. /// \param len largo del paquete
  100. /// \param offset distancia del paquete
  101. /// \return a string with the bytes of a payload line in ascii
  102. string format_hex_ascii_line(const char *payload, int len, int offset);
  103. /// \fn void Sniffer::find_devices(vector<string> &devs)
  104. /// \~English
  105. /// \brief Find the network devices in the computer, and store them in vector devs.
  106. /// \param devs vector of string for device names
  107. /// \~Spanish
  108. /// \brief Encuentra los dispositivos de red en la computadora, y los almacena en
  109. /// el vector devs
  110. /// \param devs vector de cadenas de caracteres para los nombres
  111. void find_devices(vector<string> &) ;
  112. /// \fn void Sniffer::setDevice(string dev)
  113. /// \~English
  114. /// \brief Sets the device to capture packets to dev
  115. /// \param dev name of the device
  116. /// \~Spanish
  117. /// \brief Ajusta el dispositivo para capturar paquetes a dev
  118. /// \param dev nombre del dispositivo
  119. void setDevice(string) ;
  120. public slots:
  121. void process() ;
  122. signals:
  123. void resultReady(unsigned long index);
  124. private:
  125. vector<ip_packet> packet_list ;
  126. QWaitCondition *wait_pause ;
  127. QMutex * mutex ;
  128. string device ;
  129. string filter ;
  130. int *pause ;
  131. /// \fn void Sniffer::print_payload(const u_char *payload, int len)
  132. /// \~English
  133. /// \brief Prints the payload in ascii
  134. /// \param payload payload of the packet
  135. /// \param len payload length
  136. /// \~Spanish
  137. /// \brief Imprime la carga en ascii
  138. /// \param payload carga del paquete
  139. /// \param len largo de la carga
  140. void print_payload(const u_char *payload, int len);
  141. /// \fn void Sniffer::print_hex_ascii_line(const u_char *payload, int len, int offset)
  142. /// \~English
  143. /// \brief Prints to std output the a payload in ascii
  144. /// \param payload payload of the packet
  145. /// \param len length of the packet
  146. /// \param offset offset
  147. /// \~Spanish
  148. /// \brief Imprime la carga en ascii a std output
  149. /// \param payload carga del paquete
  150. /// \param len largo de la carga
  151. /// \param offset distancia
  152. void print_hex_ascii_line(const u_char *payload, int len, int offset);
  153. /// \fn void Sniffer::got_packet(const struct pcap_pkthdr *, const u_char *packet)
  154. /// \~English
  155. /// \brief Dissects the received packet. Takes out the info needed.
  156. /// \param pcap_pkthdr header of the pcap packet
  157. /// \param packet recieved packet
  158. /// \~Spanish
  159. /// \brief Secciona el paquete recibido. Extrae la informacion necesaria.
  160. /// \param pcap_pkthdr encabezado del paquete de pcap
  161. /// \param packet paquete recibido
  162. void
  163. got_packet(const struct pcap_pkthdr *header, const u_char *packet);
  164. };
  165. #endif // SNIFFER_H