[Listener] Use global variables for the socket
+ Do not use sub-function got-packet() anymore in function capture(): read_packet() is called directly.
This commit is contained in:
parent
45258e9f4c
commit
d6a75a10fc
|
@ -71,7 +71,8 @@ void print_configuration(void) ;
|
||||||
void* keep_mode_monitor(char *iface) ;
|
void* keep_mode_monitor(char *iface) ;
|
||||||
#endif // USE_PTHREAD
|
#endif // USE_PTHREAD
|
||||||
int capture(void) ;
|
int capture(void) ;
|
||||||
void read_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet, int sockfd, struct sockaddr_in *server) ;
|
void read_packet(u_char *args, const struct pcap_pkthdr *header,
|
||||||
|
const u_char *packet) ;
|
||||||
void get_mac_addr(char *eth, unsigned char mac_bytes[6]) ;
|
void get_mac_addr(char *eth, unsigned char mac_bytes[6]) ;
|
||||||
void print_usage(void) ;
|
void print_usage(void) ;
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
char *program_name = NULL ;
|
char *program_name = NULL ;
|
||||||
unsigned char mac[6] ; // AP MAC address
|
unsigned char mac[6] ; // AP MAC address
|
||||||
|
|
||||||
|
int aggregation_sockfd ;
|
||||||
|
struct sockaddr_in aggregation_server ;
|
||||||
|
|
||||||
#ifdef USE_CONFIG_FILE
|
#ifdef USE_CONFIG_FILE
|
||||||
cfg_t *cfg ; // Configuration structure
|
cfg_t *cfg ; // Configuration structure
|
||||||
|
|
||||||
|
@ -331,19 +334,7 @@ int capture()
|
||||||
{
|
{
|
||||||
pcap_t *handle ; // Packet capture descriptor
|
pcap_t *handle ; // Packet capture descriptor
|
||||||
char errbuf[PCAP_ERRBUF_SIZE] ; // Error message
|
char errbuf[PCAP_ERRBUF_SIZE] ; // Error message
|
||||||
int sockfd ; // Output socket descriptor
|
struct sockaddr_in client ;
|
||||||
struct sockaddr_in server, client ;
|
|
||||||
|
|
||||||
|
|
||||||
/* Sub-function that treats captured packets */
|
|
||||||
void got_packet(u_char *args, const struct pcap_pkthdr *header,
|
|
||||||
const u_char *packet)
|
|
||||||
{
|
|
||||||
// Call function read_packet() with the same arguments, plus server
|
|
||||||
// information:
|
|
||||||
read_packet(args, header, packet, sockfd, &server) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Start capture:
|
// Start capture:
|
||||||
handle = pcap_open_live(GET_RTAP_IFACE(), BUFSIZ, 1, 1000, errbuf) ;
|
handle = pcap_open_live(GET_RTAP_IFACE(), BUFSIZ, 1, 1000, errbuf) ;
|
||||||
|
@ -355,21 +346,22 @@ int capture()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Open UDP socket to the aggregator */
|
/* Open UDP socket to the aggregator */
|
||||||
sockfd = create_udp_sending_socket(GET_AGGREGATION_IP(),
|
aggregation_sockfd =
|
||||||
GET_AGGREGATION_PORT(),
|
create_udp_sending_socket(GET_AGGREGATION_IP(),
|
||||||
&server, &client) ;
|
GET_AGGREGATION_PORT(),
|
||||||
if (sockfd < 0)
|
&aggregation_server, &client) ;
|
||||||
|
if (aggregation_sockfd < 0)
|
||||||
{
|
{
|
||||||
perror("Error! Cannot create socket to the aggregation server");
|
perror("Error! Cannot create socket to the aggregation server");
|
||||||
return ERR_CREATING_SOCKET ;
|
return ERR_CREATING_SOCKET ;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(run)
|
while(run)
|
||||||
// Capture one packet at time, and call got_packet() on it:
|
// Capture one packet at time, and call read_packet() on it:
|
||||||
pcap_loop(handle, 1, got_packet, NULL) ;
|
pcap_loop(handle, 1, read_packet, NULL) ;
|
||||||
|
|
||||||
pcap_close(handle) ; // Stop capture
|
pcap_close(handle) ; // Stop capture
|
||||||
(void) close(sockfd) ; // Close socket
|
(void) close(aggregation_sockfd) ; // Close socket
|
||||||
|
|
||||||
return 0 ;
|
return 0 ;
|
||||||
}
|
}
|
||||||
|
@ -377,12 +369,10 @@ int capture()
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Treats a packet and sends it to the aggregator (on the UDP socket
|
* Treats a packet and sends it to the aggregator.
|
||||||
* 'sockfd', to the server 'server').
|
|
||||||
*/
|
*/
|
||||||
void read_packet(u_char *args, const struct pcap_pkthdr *header,
|
void read_packet(u_char *args, const struct pcap_pkthdr *header,
|
||||||
const u_char *packet, int sockfd,
|
const u_char *packet)
|
||||||
struct sockaddr_in *server)
|
|
||||||
{
|
{
|
||||||
// Copy packet address into data:
|
// Copy packet address into data:
|
||||||
unsigned char *data = (unsigned char *) packet ;
|
unsigned char *data = (unsigned char *) packet ;
|
||||||
|
@ -684,8 +674,9 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
|
||||||
|
|
||||||
/* Send couple to the aggregator */
|
/* Send couple to the aggregator */
|
||||||
nsent =
|
nsent =
|
||||||
sendto(sockfd, (void *) &couple, sizeof(couple), 0,
|
sendto(aggregation_sockfd, (void *) &couple, sizeof(couple), 0,
|
||||||
(struct sockaddr *) server, (socklen_t) sizeof(*server)) ;
|
(struct sockaddr *) &aggregation_server,
|
||||||
|
(socklen_t) sizeof(aggregation_server)) ;
|
||||||
if (nsent != (ssize_t) sizeof(couple))
|
if (nsent != (ssize_t) sizeof(couple))
|
||||||
{
|
{
|
||||||
perror("Error sending couple to the aggregation server") ;
|
perror("Error sending couple to the aggregation server") ;
|
||||||
|
|
Loading…
Reference in New Issue