From 25cc1ee67a8c5f5d131c9ab90b0bb98a081feb36 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 20 Sep 2013 17:07:46 -0400 Subject: [PATCH] [lib] getaddrinfo() in create_udp_trx_socket() Use getaddrinfo() to resolve names in owl_create_udp_trx_socket(). The client_description parameter has been removed from the function's synopsis. --- libowlps-client/CMakeLists.txt | 2 +- libowlps-client/libowlps-client.c | 14 +++--- libowlps-client/owlps-client.h | 6 +-- libowlps/CMakeLists.txt | 2 +- libowlps/libowlps.c | 58 ++++++++++++++++--------- libowlps/owlps.h | 3 +- owlps-aggregator/owlps-aggregator.h | 6 +-- owlps-aggregator/owlps-aggregatord.c | 25 +++++------ owlps-client/owlps-client.c | 2 +- owlps-listener/owlps-listenerd.c | 9 ++-- owlps-positioner/outputnetworksocket.hh | 3 +- owlps-positioner/outputudpsocket.cc | 5 +-- 12 files changed, 70 insertions(+), 65 deletions(-) diff --git a/libowlps-client/CMakeLists.txt b/libowlps-client/CMakeLists.txt index 675ee99..31cfbae 100644 --- a/libowlps-client/CMakeLists.txt +++ b/libowlps-client/CMakeLists.txt @@ -22,7 +22,7 @@ set_target_properties( PROPERTIES OUTPUT_NAME owlps-client SOVERSION 1 - VERSION 1.1 + VERSION 1.2 ) # Add compile flags diff --git a/libowlps-client/libowlps-client.c b/libowlps-client/libowlps-client.c index 2c084f6..c06d217 100644 --- a/libowlps-client/libowlps-client.c +++ b/libowlps-client/libowlps-client.c @@ -42,13 +42,10 @@ */ int owl_create_trx_socket(const char *const dest_ip, const uint_fast16_t dest_port, - struct sockaddr_in *const server, + struct sockaddr *const server, const char *const iface) { - struct sockaddr_in client ; - - int sockfd = owl_create_udp_trx_socket(dest_ip, dest_port, - server, &client) ; + int sockfd = owl_create_udp_trx_socket(dest_ip, dest_port, server) ; if (sockfd < 0) { perror("Error! Cannot create UDP sending socket to the aggregation" @@ -107,7 +104,7 @@ void owl_use_iface(const int sockfd, const char *const iface) * standard output. */ void owl_send_request(const int sockfd, - const struct sockaddr_in *const server, + const struct sockaddr *const server, const void *const packet, const uint_fast16_t packet_size, const uint_fast16_t nb_pkt, @@ -151,14 +148,13 @@ void owl_send_request(const int sockfd, * and the standard output will be flushed. */ void owl_send_packet(const int sockfd, - const struct sockaddr_in *const server, + const struct sockaddr *const server, const void *const packet, const uint_fast16_t packet_size, const bool verbose) { ssize_t nsent = sendto(sockfd, packet, packet_size, 0, - (struct sockaddr *) server, - (socklen_t) sizeof(*server)) ; + server, (socklen_t) sizeof(*server)) ; if (nsent != (ssize_t) packet_size) { perror("Error sending data to the network") ; diff --git a/libowlps-client/owlps-client.h b/libowlps-client/owlps-client.h index 346021c..ae976df 100644 --- a/libowlps-client/owlps-client.h +++ b/libowlps-client/owlps-client.h @@ -30,13 +30,13 @@ /// Opens a transmission socket int owl_create_trx_socket(const char *const dest_ip, const uint_fast16_t dest_port, - struct sockaddr_in *const server, + struct sockaddr *const server, const char *const iface) ; /// Selects a transmission interface for the socket `sockfd` void owl_use_iface(const int sockfd, const char *const iface) ; /// Transmits a request void owl_send_request(const int sockfd, - const struct sockaddr_in *const server, + const struct sockaddr *const server, const void *const packet, const uint_fast16_t packet_size, const uint_fast16_t nb_pkt, @@ -44,7 +44,7 @@ void owl_send_request(const int sockfd, const bool verbose) ; /// Transmits a packet void owl_send_packet(const int sockfd, - const struct sockaddr_in *const server, + const struct sockaddr *const server, const void *const packet, const uint_fast16_t packet_size, const bool verbose) ; diff --git a/libowlps/CMakeLists.txt b/libowlps/CMakeLists.txt index 23fa92f..6da43e1 100644 --- a/libowlps/CMakeLists.txt +++ b/libowlps/CMakeLists.txt @@ -33,7 +33,7 @@ set_target_properties( PROPERTIES OUTPUT_NAME owlps SOVERSION 3 - VERSION 3.2 + VERSION 3.3 ) # Add compile flags diff --git a/libowlps/libowlps.c b/libowlps/libowlps.c index 77a467a..5ddcf02 100644 --- a/libowlps/libowlps.c +++ b/libowlps/libowlps.c @@ -28,6 +28,7 @@ #include #include +#include #include @@ -416,40 +417,57 @@ float owl_swap_float(const float f) * @param[in] server_port The listening port on the server. * @param[out] server_description The structure in which the server * description will be saved. - * @param[out] client_description The structure in which the client - * description will be saved. * * @returns A file descriptor to the opened socket in case of success, * or a negative error code. */ int owl_create_udp_trx_socket(const char *const server_address, const uint_fast16_t server_port, - struct sockaddr_in *const server_description, - struct sockaddr_in *const client_description) + struct sockaddr *const server_description) { - int sockfd ; // Socket descriptor + char server_port_str[6] ; + struct addrinfo + gai_hints, + *gai_results = NULL, + *gai_res = NULL ; + int gai_ret ; // Return value of getaddrinfo() + int sockfd = -1 ; // Socket descriptor - /* Create the UDP socket */ - sockfd = socket(AF_INET, SOCK_DGRAM, 0) ; - if (sockfd < 0) + /* Get the server information */ + sprintf(server_port_str, "%"PRIuFAST16, server_port) ; + memset(&gai_hints, 0, sizeof(struct addrinfo)) ; + gai_hints.ai_family = AF_INET ; // IPv4 only + gai_hints.ai_socktype = SOCK_DGRAM ; + gai_ret = getaddrinfo(server_address, server_port_str, + &gai_hints, &gai_results) ; + if (gai_ret) { - perror("UDP socket creation failed") ; + fprintf(stderr, "UDP socket creation failed: getaddrinfo(): %s\n", + gai_strerror(gai_ret)) ; return -OWL_ERR_SOCKET_CREATE ; } - /* Initialise the client structure */ - memset(client_description, 0, sizeof(*client_description)) ; - client_description->sin_family = AF_INET ; // INET socket - client_description->sin_addr.s_addr = htonl(INADDR_ANY) ; + /* Create the UDP socket: loop until socket() succeeds */ + for (gai_res = gai_results ; gai_res != NULL ; + gai_res = gai_res->ai_next) + { + sockfd = socket(gai_res->ai_family, gai_res->ai_socktype, + gai_res->ai_protocol) ; + if (sockfd != -1) + break ; + } - /* Initialise the server structure */ - memset(server_description, 0, sizeof(*server_description)) ; - server_description->sin_family = AF_INET ; // INET socket - // Server IP address: - server_description->sin_addr.s_addr = inet_addr(server_address) ; - // Listening port on the server: - server_description->sin_port = htons(server_port) ; + if (gai_res == NULL) + { + fprintf(stderr, + "UDP socket creation failed: socket().\n") ; + return -OWL_ERR_SOCKET_CREATE ; + } + /* Copy the server description */ + memcpy(server_description, gai_res->ai_addr, gai_res->ai_addrlen) ; + + freeaddrinfo(gai_results) ; return sockfd ; } diff --git a/libowlps/owlps.h b/libowlps/owlps.h index 2828623..09d50b1 100644 --- a/libowlps/owlps.h +++ b/libowlps/owlps.h @@ -403,8 +403,7 @@ float owl_swap_float(const float f) ; /// Opens a UDP transmission socket int owl_create_udp_trx_socket(const char *const server_address, const uint_fast16_t server_port, - struct sockaddr_in *const server_description, - struct sockaddr_in *const client_description) ; + struct sockaddr *const server_description) ; /// Opens a UDP reception socket int owl_create_udp_listening_socket(const uint_fast16_t port) ; //@} diff --git a/owlps-aggregator/owlps-aggregator.h b/owlps-aggregator/owlps-aggregator.h index 2267e05..ab5d8c9 100644 --- a/owlps-aggregator/owlps-aggregator.h +++ b/owlps-aggregator/owlps-aggregator.h @@ -117,14 +117,14 @@ void add_captured_request(const owl_timestamp *const reception_time, void* monitor_requests(void *NULL_value) ; void scan_request_list(FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) ; + const struct sockaddr *const serv) ; void flush_request_list(FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) ; + const struct sockaddr *const serv) ; void output_request(request_list *const request_ptr, FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) ; + const struct sockaddr *const serv) ; #ifndef NDEBUG void print_request_list(void) ; diff --git a/owlps-aggregator/owlps-aggregatord.c b/owlps-aggregator/owlps-aggregatord.c index 2d64258..ea94bde 100644 --- a/owlps-aggregator/owlps-aggregatord.c +++ b/owlps-aggregator/owlps-aggregatord.c @@ -829,8 +829,7 @@ void add_captured_request(const owl_timestamp *const reception_time, void* monitor_requests(void *NULL_value) { FILE *stream = NULL ; - struct sockaddr_in serv; - struct sockaddr_in client ; + struct sockaddr serv; int sockfd; if (VERBOSE_WARNING) @@ -839,7 +838,7 @@ void* monitor_requests(void *NULL_value) sockfd = owl_create_udp_trx_socket(cfg_getstr(cfg, "positioner_ip"), cfg_getint(cfg, "positioner_port"), - &serv, &client) ; + &serv) ; pthread_cleanup_push(&owl_close_fd, &sockfd) ; /* Open output file */ @@ -879,7 +878,7 @@ void* monitor_requests(void *NULL_value) */ void scan_request_list(FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) + const struct sockaddr *const serv) { request_list *request_ptr ; request_list *request_prev = NULL ; @@ -961,7 +960,7 @@ void scan_request_list(FILE *const stream, */ void flush_request_list(FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) + const struct sockaddr *const serv) { request_list *next_request ; @@ -1002,7 +1001,7 @@ void flush_request_list(FILE *const stream, void output_request(request_list *const request_ptr, FILE *const stream, const int sockfd, - const struct sockaddr_in *const serv) + const struct sockaddr *const serv) { char mac_str[OWL_ETHER_ADDR_STRLEN] ; char request_time_str[OWL_TIMESTAMP_STRLEN] ; @@ -1058,8 +1057,7 @@ void output_request(request_list *const request_ptr, } request.nb_info = htons(request.nb_info) ; // Send the request's main data: - sendto(sockfd, &request, sizeof(request), 0, - (const struct sockaddr *const)serv, serv_len) ; + sendto(sockfd, &request, sizeof(request), 0, serv, serv_len) ; // Send request's per-CP information to the server and delete it // from the request @@ -1073,8 +1071,7 @@ void output_request(request_list *const request_ptr, info.capture_time = request_info_ptr->capture_time ; owl_hton_timestamp(&info.capture_time) ; info.ss_dbm = request_info_ptr->ss_dbm ; - sendto(sockfd, &info, sizeof(info), - 0, (const struct sockaddr *const)serv, serv_len) ; + sendto(sockfd, &info, sizeof(info), 0, serv, serv_len) ; // Print CP info to the output file owl_mac_bytes_to_string_r(request_info_ptr->cp_mac_addr_bytes, @@ -1461,8 +1458,7 @@ void unlink_cp(cp_list *cp) void order_send(cp_list *cp) { owl_autocalibration_order message ; - struct sockaddr_in serv; - struct sockaddr_in client ; + struct sockaddr serv; socklen_t serv_len = sizeof(serv); int sockfd ; ssize_t nsent ; @@ -1474,11 +1470,10 @@ void order_send(cp_list *cp) owl_create_udp_trx_socket(cp->ip_addr, cfg_getint(cfg, "autocalibration_order_port"), - &serv, &client) ; + &serv) ; message.order = AUTOCALIBRATION_ORDER_SEND ; - nsent = sendto(sockfd, &message, sizeof(message), 0, - (struct sockaddr *)&serv, serv_len) ; + nsent = sendto(sockfd, &message, sizeof(message), 0, &serv, serv_len) ; if (nsent != (ssize_t) sizeof(message)) { perror("Error sending order to the listener") ; diff --git a/owlps-client/owlps-client.c b/owlps-client/owlps-client.c index 49cb90b..c7f3741 100644 --- a/owlps-client/owlps-client.c +++ b/owlps-client/owlps-client.c @@ -115,7 +115,7 @@ char *program_name = NULL ; bool is_calibration_request = false ; int sockfd ; // Sending socket descriptor -struct sockaddr_in server ; // Server info +struct sockaddr server ; // Server info uint8_t *packet = NULL ; // Packet to send uint_fast16_t packet_size ; // Packet size diff --git a/owlps-listener/owlps-listenerd.c b/owlps-listener/owlps-listenerd.c index d585a00..d6b354e 100644 --- a/owlps-listener/owlps-listenerd.c +++ b/owlps-listener/owlps-listenerd.c @@ -71,11 +71,11 @@ bool dump_configuration = false ; pcap_t *capture_handler = NULL ; // Packet capture descriptor int aggregation_sockfd ; -struct sockaddr_in aggregation_server ; +struct sockaddr aggregation_server ; #ifdef OWLPS_LISTENER_USES_PTHREAD int autocalibration_send_sockfd ; -struct sockaddr_in autocalibration_send_server ; +struct sockaddr autocalibration_send_server ; // true if the coordinates of the listener were provided by the user: bool coordinates_provided = false ; #endif // OWLPS_LISTENER_USES_PTHREAD @@ -1217,8 +1217,7 @@ void read_packet(const struct pcap_pkthdr *pkt_header, /* Send the request to the aggregator */ nsent = sendto(aggregation_sockfd, &request, sizeof(request), 0, - (struct sockaddr *) &aggregation_server, - (socklen_t) sizeof(aggregation_server)) ; + &aggregation_server, (socklen_t) sizeof(aggregation_server)) ; if (nsent != (ssize_t) sizeof(request)) { perror("Error sending request to the aggregation server") ; @@ -1539,7 +1538,7 @@ void get_ip_addr(const char *const iface, char ip[INET_ADDRSTRLEN]) void* autocalibrate_hello(void *NULL_value) { int send_sockfd ; - struct sockaddr_in serv; + struct sockaddr serv; owl_autocalibration_hello message ; if (VERBOSE_WARNING) diff --git a/owlps-positioner/outputnetworksocket.hh b/owlps-positioner/outputnetworksocket.hh index d781abf..35649d0 100644 --- a/owlps-positioner/outputnetworksocket.hh +++ b/owlps-positioner/outputnetworksocket.hh @@ -30,8 +30,7 @@ protected: std::string remote_host ; uint_fast16_t remote_port ; - struct sockaddr_in server_info ; - struct sockaddr_in client_info ; + struct sockaddr server_info ; /** @name Operations */ //@{ diff --git a/owlps-positioner/outputudpsocket.cc b/owlps-positioner/outputudpsocket.cc index f800e54..df755bc 100644 --- a/owlps-positioner/outputudpsocket.cc +++ b/owlps-positioner/outputudpsocket.cc @@ -49,7 +49,7 @@ OutputUDPSocket::OutputUDPSocket(const string &_remote_host, bool OutputUDPSocket::init_socket() { sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port, - &server_info, &client_info) ; + &server_info) ; return sockfd >= 0 ; } @@ -61,8 +61,7 @@ bool OutputUDPSocket::send_data(const string &data) const { unsigned int data_len = data.size() + 1 ; // +1 for the '\0' ssize_t nsent = sendto(sockfd, data.c_str(), data_len, 0, - (struct sockaddr *) &server_info, - sizeof(server_info)) ; + &server_info, sizeof(server_info)) ; if (nsent != static_cast(data_len)) { perror("Error sending result data through the UDP socket") ;