[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:
Matteo Cypriani 2010-08-03 11:05:06 +02:00
parent 45258e9f4c
commit d6a75a10fc
2 changed files with 19 additions and 27 deletions

View File

@ -71,7 +71,8 @@ void print_configuration(void) ;
void* keep_mode_monitor(char *iface) ;
#endif // USE_PTHREAD
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 print_usage(void) ;

View File

@ -10,6 +10,9 @@
char *program_name = NULL ;
unsigned char mac[6] ; // AP MAC address
int aggregation_sockfd ;
struct sockaddr_in aggregation_server ;
#ifdef USE_CONFIG_FILE
cfg_t *cfg ; // Configuration structure
@ -331,19 +334,7 @@ int capture()
{
pcap_t *handle ; // Packet capture descriptor
char errbuf[PCAP_ERRBUF_SIZE] ; // Error message
int sockfd ; // Output socket descriptor
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) ;
}
struct sockaddr_in client ;
// Start capture:
handle = pcap_open_live(GET_RTAP_IFACE(), BUFSIZ, 1, 1000, errbuf) ;
@ -355,21 +346,22 @@ int capture()
}
/* Open UDP socket to the aggregator */
sockfd = create_udp_sending_socket(GET_AGGREGATION_IP(),
GET_AGGREGATION_PORT(),
&server, &client) ;
if (sockfd < 0)
aggregation_sockfd =
create_udp_sending_socket(GET_AGGREGATION_IP(),
GET_AGGREGATION_PORT(),
&aggregation_server, &client) ;
if (aggregation_sockfd < 0)
{
perror("Error! Cannot create socket to the aggregation server");
return ERR_CREATING_SOCKET ;
}
while(run)
// Capture one packet at time, and call got_packet() on it:
pcap_loop(handle, 1, got_packet, NULL) ;
// Capture one packet at time, and call read_packet() on it:
pcap_loop(handle, 1, read_packet, NULL) ;
pcap_close(handle) ; // Stop capture
(void) close(sockfd) ; // Close socket
(void) close(aggregation_sockfd) ; // Close socket
return 0 ;
}
@ -377,12 +369,10 @@ int capture()
/*
* Treats a packet and sends it to the aggregator (on the UDP socket
* 'sockfd', to the server 'server').
* Treats a packet and sends it to the aggregator.
*/
void read_packet(u_char *args, const struct pcap_pkthdr *header,
const u_char *packet, int sockfd,
struct sockaddr_in *server)
const u_char *packet)
{
// Copy packet address into data:
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 */
nsent =
sendto(sockfd, (void *) &couple, sizeof(couple), 0,
(struct sockaddr *) server, (socklen_t) sizeof(*server)) ;
sendto(aggregation_sockfd, (void *) &couple, sizeof(couple), 0,
(struct sockaddr *) &aggregation_server,
(socklen_t) sizeof(aggregation_server)) ;
if (nsent != (ssize_t) sizeof(couple))
{
perror("Error sending couple to the aggregation server") ;