diff --git a/src/mavlink-router/endpoint.cpp b/src/mavlink-router/endpoint.cpp index f2845f4d..f75cdce9 100644 --- a/src/mavlink-router/endpoint.cpp +++ b/src/mavlink-router/endpoint.cpp @@ -880,9 +880,25 @@ int UdpEndpoint::open(const char *ip, unsigned long port, bool to_bind) free(ip_str); } else { #endif - sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = inet_addr(ip); - 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 @@ -1111,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 } @@ -1146,29 +1161,46 @@ int TcpEndpoint::open(const char *ip, unsigned long port) free(ip_str); } else { #endif - sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = inet_addr(ip); - 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; 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)