#include "owlps-client.h" #include #include #include #include #define DEBUG /* * 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 owlclient_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(ERR_CREATING_SOCKET) ; } // If we specified an interface name if (iface != NULL && iface[0] != '\0') owlclient_use_iface(sockfd, iface) ; return sockfd ; } /* Selects 'iface' as sending interface for the socket 'sockfd'. */ void owlclient_use_iface(const int sockfd, const char *const iface) { 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") ; } } void owlclient_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 ; #ifdef DEBUG printf("Sent packets: ") ; #endif // DEBUG // Transmit first packet: owlclient_send_packet(sockfd, server, packet, packet_size) ; // Transmit remaining packets (if any): for (i = 0 ; i < nb_pkt - 1 ; ++i) { usleep(delay) ; // Wait during the wanted delay owlclient_send_packet(sockfd, server, packet, packet_size) ; } #ifdef DEBUG putchar('\n') ; #endif // DEBUG } void owlclient_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(ERR_SENDING_INFO) ; } #ifdef DEBUG putchar('.') ; fflush(stdout) ; #endif // DEBUG }