Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: PR for allowing hostnames in config files #42

Open
wants to merge 2 commits into
base: auterion-router-latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions src/mavlink-router/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a UdpEndpoint, shouldn't that be a SOCK_DGRAM?

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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Comment on lines 1184 to 1186

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that compile when ENABLE_IPV6 is enabled? Feels like you removed the opening bracket 🤔


// #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
Comment on lines +1188 to +1202

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #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;
Expand Down
10 changes: 0 additions & 10 deletions src/mavlink-router/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens now if the ip/hostname is invalid? Does it crash somewhere, or does it just not work? Wouldn't it be nicer to have an error message similar to the one removed here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that it would be better to keep the validation check, but it looks like the check for valid hostnames can be quite extensive per RFC1123; maybe better to defer it to the hostname lookup stage?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then won't it fail in the getaddrinfo call (line 1169)? Shouldn't it print an error message (some log_error()) there? Right now it feels like it just does exit(1), so we won't know what happened from the logs, right? Or did I miss something?

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);
}
}
}

Expand All @@ -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)
Expand Down