owlps/libowlps-client/libowlps-client.c

135 lines
3.6 KiB
C

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*
* This is the main source file of libowlps-client.
*/
#include "owlps-client.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/param.h>
/*
* Opens an UDP socket to the server whose IP address is 'dest_ip'
* and which listens 'dest_port'. The server information is stored
* into 'server'. The socket will send through the network interface
* 'iface' if specified (if you want the interface to be selected,
* automatically, this parameter should be NULL or an empty string).
*/
int owl_create_trx_socket(const char *const dest_ip,
const uint_fast16_t dest_port,
struct sockaddr_in *const server,
const char *const iface)
{
struct sockaddr_in client ;
int sockfd = owl_create_udp_trx_socket(dest_ip, dest_port,
server, &client) ;
if (sockfd < 0)
{
perror("Error! Cannot create UDP sending socket to the aggregation"
" server") ;
exit(OWL_ERR_SOCKET_CREATE) ;
}
// If we specified an interface name
if (iface != NULL && iface[0] != '\0')
owl_use_iface(sockfd, iface) ;
return sockfd ;
}
/* Selects 'iface' as sending interface for the socket 'sockfd'. */
void owl_use_iface(const int sockfd, const char *const iface)
{
#if defined(linux) && defined(__GLIBC__)
if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, iface,
strlen(iface) + 1) == -1)
{
fprintf(stderr, "Error! Cannot select interface %s to send the"
" packet: ", iface) ;
perror("") ;
fprintf(stderr, "Sending through the default interface.\n") ;
}
#else // linux && __GLIBC__
fprintf(stderr, "Cannot select interface %s to send the packet:"
" this option is currently Linux and GNU libc-specific.\n"
"Sending through the default interface.\n", iface) ;
#endif // linux && __GLIBC__
}
/*
* Transmits a request.
* No packet will be transmitted if the variable owl_run is (or
* becomes) false.
* nb_pkt: number of packets to transmit.
* delay: delay between two transmissions, in milliseconds.
*/
void owl_send_request(const int sockfd,
const struct sockaddr_in *const server,
const void *const packet,
const uint_fast16_t packet_size,
const uint_fast16_t nb_pkt,
const uint_fast32_t delay)
{
uint_fast16_t i ;
if (nb_pkt == 0 || !owl_run)
return ;
#ifdef DEBUG
printf("Sent packets: ") ;
#endif // DEBUG
// Transmit first packet:
owl_send_packet(sockfd, server, packet, packet_size) ;
// Transmit remaining packets (if any):
for (i = 0 ; i < nb_pkt - 1 ; ++i)
{
owl_msleep(delay) ; // Wait during the wanted delay
if (! owl_run)
break ;
owl_send_packet(sockfd, server, packet, packet_size) ;
}
#ifdef DEBUG
putchar('\n') ;
#endif // DEBUG
}
void owl_send_packet(const int sockfd,
const struct sockaddr_in *const server,
const void *const packet,
const uint_fast16_t packet_size)
{
ssize_t nsent = sendto(sockfd, packet, packet_size, 0,
(struct sockaddr *) server,
(socklen_t) sizeof(*server)) ;
if (nsent != (ssize_t) packet_size)
{
perror("Error sending data to the aggregation server") ;
exit(OWL_ERR_SOCKET_SEND) ;
}
#ifdef DEBUG
putchar('.') ;
fflush(stdout) ;
#endif // DEBUG
}