From 4400faded27e61efae9be7d8510718f5b0787aaf Mon Sep 17 00:00:00 2001 From: Kyle Usbeck Date: Thu, 19 May 2022 11:40:19 -0400 Subject: [PATCH 1/2] WIP: PR for allowing hostnames (rather than just IP addresses) in config files --- src/mavlink-router/endpoint.cpp | 12 ++++++++++-- src/mavlink-router/main.cpp | 10 ---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/mavlink-router/endpoint.cpp b/src/mavlink-router/endpoint.cpp index f2845f4d..5a427148 100644 --- a/src/mavlink-router/endpoint.cpp +++ b/src/mavlink-router/endpoint.cpp @@ -881,7 +881,11 @@ int UdpEndpoint::open(const char *ip, unsigned long port, bool to_bind) } else { #endif sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = inet_addr(ip); + struct hostent *server; + server = gethostbyname(ip); + bcopy((char *)server->h_addr, + (char *)&sockaddr.sin_addr.s_addr, + server->h_length); sockaddr.sin_port = htons(port); #ifdef ENABLE_IPV6 } @@ -1147,7 +1151,11 @@ int TcpEndpoint::open(const char *ip, unsigned long port) } else { #endif sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = inet_addr(ip); + struct hostent *server; + server = gethostbyname(ip); + bcopy((char *)server->h_addr, + (char *)&sockaddr.sin_addr.s_addr, + server->h_length); sockaddr.sin_port = htons(port); #ifdef ENABLE_IPV6 } diff --git a/src/mavlink-router/main.cpp b/src/mavlink-router/main.cpp index ffd3ce55..0fbe7116 100644 --- a/src/mavlink-router/main.cpp +++ b/src/mavlink-router/main.cpp @@ -797,14 +797,9 @@ static int parse_confs(ConfFile &conf) log_error("Expected 'port' key for section %.*s", (int)iter.name_len, iter.name); ret = -EINVAL; } else { - if (validate_ip(opt_udp.addr) < 0) { - log_error("Invalid IP address in section %.*s: %s", (int)iter.name_len, iter.name, opt_udp.addr); - ret = -EINVAL; - } else { ret = add_udp_endpoint_address(iter.name + offset, iter.name_len - offset, opt_udp.addr, opt_udp.port, opt_udp.eavesdropping, opt_udp.filter, opt_udp.coalesce_bytes, opt_udp.coalesce_ms, opt_udp.coalesce_nodelay, opt_udp.dropout_percentage); - } } } @@ -823,13 +818,8 @@ static int parse_confs(ConfFile &conf) ret = conf.extract_options(&iter, option_table_tcp, ARRAY_SIZE(option_table_tcp), &opt_tcp); if (ret == 0) { - if (validate_ip(opt_tcp.addr) < 0) { - log_error("Invalid IP address in section %.*s: %s", (int)iter.name_len, iter.name, opt_tcp.addr); - ret = -EINVAL; - } else { ret = add_tcp_endpoint_address(iter.name + offset, iter.name_len - offset, opt_tcp.addr, opt_tcp.port, opt_tcp.timeout); - } } free(opt_tcp.addr); if (ret < 0) From ffd9780855117b4390b7cc7b443c922b04c44679 Mon Sep 17 00:00:00 2001 From: Kyle Usbeck Date: Wed, 1 Jun 2022 20:50:52 -0400 Subject: [PATCH 2/2] removing deprecated gethostbyname() in favor of getaddrinfo() --- src/mavlink-router/endpoint.cpp | 78 +++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/mavlink-router/endpoint.cpp b/src/mavlink-router/endpoint.cpp index 5a427148..f75cdce9 100644 --- a/src/mavlink-router/endpoint.cpp +++ b/src/mavlink-router/endpoint.cpp @@ -880,13 +880,25 @@ int UdpEndpoint::open(const char *ip, unsigned long port, bool to_bind) free(ip_str); } else { #endif - sockaddr.sin_family = AF_INET; - struct hostent *server; - server = gethostbyname(ip); - bcopy((char *)server->h_addr, - (char *)&sockaddr.sin_addr.s_addr, - server->h_length); - sockaddr.sin_port = htons(port); + + struct addrinfo hints = {0}, *addrs; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + const int status = getaddrinfo(ip, std::to_string(port).c_str(), &hints, &addrs); + if (status != 0) + { + // fprintf(stderr, "%s: %s\n", hostname, gai_strerror(status)); + exit(1); + } + + for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next) + { + fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + break; + } + #ifdef ENABLE_IPV6 } #endif @@ -1115,7 +1127,6 @@ int TcpEndpoint::open(const char *ip, unsigned long port) fd = socket(AF_INET6, SOCK_STREAM, 0); } else { #endif - fd = socket(AF_INET, SOCK_STREAM, 0); #ifdef ENABLE_IPV6 } @@ -1150,33 +1161,46 @@ int TcpEndpoint::open(const char *ip, unsigned long port) free(ip_str); } else { #endif - sockaddr.sin_family = AF_INET; - struct hostent *server; - server = gethostbyname(ip); - bcopy((char *)server->h_addr, - (char *)&sockaddr.sin_addr.s_addr, - server->h_length); - sockaddr.sin_port = htons(port); -#ifdef ENABLE_IPV6 + struct addrinfo hints = {0}, *addrs; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + const int status = getaddrinfo(ip, std::to_string(port).c_str(), &hints, &addrs); + if (status != 0) + { + // fprintf(stderr, "%s: %s\n", hostname, gai_strerror(status)); + exit(1); } -#endif -#ifdef ENABLE_IPV6 - if (this->is_ipv6) { - if (connect(fd, (struct sockaddr *)&sockaddr6, sizeof(sockaddr6)) < 0) { - log_error("Error connecting to IPv6 socket (%m)"); - goto fail; + for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next) + { + fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) { + break; } - } else { -#endif - if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) { - log_error("Error connecting to socket (%m)"); - goto fail; } + #ifdef ENABLE_IPV6 } #endif +// #ifdef ENABLE_IPV6 +// if (this->is_ipv6) { +// if (connect(fd, (struct sockaddr *)&sockaddr6, sizeof(sockaddr6)) < 0) { +// log_error("Error connecting to IPv6 socket (%m)"); +// goto fail; +// } +// } else { +// #endif +// if (connect(fd, addrs[0]->ai_addr, addrs[0]->ai_addrlen) < 0) { +// log_error("Error connecting to socket (%m)"); +// goto fail; +// } +// #ifdef ENABLE_IPV6 +// } +// #endif + if (fcntl(fd, F_SETFL, O_NONBLOCK | FASYNC) < 0) { log_error("Error setting socket fd as non-blocking (%m)"); goto fail;