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

netdog: remove IP string formatting when setting as hostname #1693

Merged
merged 1 commit into from
Aug 4, 2021
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ For [host-container](#host-containers-settings) and [bootstrap-container](#boots

Most users don't need to change this setting as the following defaults work for the majority of use cases.
If this setting isn't set we attempt to use DNS reverse lookup for the hostname.
If the lookup is unsuccessful, the IP of the node is used in the format `ip-X-X-X-X`.
If the lookup is unsuccessful, the IP of the node is used.

##### Proxy settings

Expand Down
3 changes: 1 addition & 2 deletions sources/api/netdog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ file.

It contains two subcommands meant for use as settings generators:
* `node-ip`: returns the node's current IP address in JSON format
* `generate-hostname`: returns the node's hostname in JSON format (it is the resolved IP or the IP
in format "ip-x-x-x-x" if resolving fails)
* `generate-hostname`: returns the node's hostname in JSON format. If the lookup is unsuccessful, the IP of the node is used.

The subcommand `set-hostname` sets the hostname for the system.

Expand Down
19 changes: 4 additions & 15 deletions sources/api/netdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ file.

It contains two subcommands meant for use as settings generators:
* `node-ip`: returns the node's current IP address in JSON format
* `generate-hostname`: returns the node's hostname in JSON format (it is the resolved IP or the IP
in format "ip-x-x-x-x" if resolving fails)
* `generate-hostname`: returns the node's hostname in JSON format. If the lookup is unsuccessful, the IP of the node is used.

The subcommand `set-hostname` sets the hostname for the system.
*/
Expand Down Expand Up @@ -266,28 +265,18 @@ fn node_ip() -> Result<()> {
Ok(print_json(ip_string)?)
}

/// Attempt to resolve assigned IP address, if unsuccessful use "ip-X-X-X-X" where X's are the
/// octets of the IP. For example, IP address 1.2.3.4 becomes the hostname "ip-1-2-3-4". No dots
/// in the hostname (hopefully) avoids any confusion for programs that may read this value.
/// Attempt to resolve assigned IP address, if unsuccessful use the IP as the hostname.
///
/// The result is returned as JSON. (intended for use as a settings generator)
fn generate_hostname() -> Result<()> {
let ip_string =
fs::read_to_string(CURRENT_IP).context(error::CurrentIpReadFailed { path: CURRENT_IP })?;
let ip = IpAddr::from_str(&ip_string).context(error::IpFromString { ip: &ip_string })?;
let hostname = match lookup_addr(&ip) {
Ok(hostname) => {
// if `lookup_addr()` returns the same IP as we passed it we didn't resolve anything,
// so return the string "ip-x-x-x-x" in this case
if hostname == ip_string {
format!("ip-{}", ip_string.replace(".", "-"))
} else {
hostname
}
}
Ok(hostname) => hostname,
Err(e) => {
eprintln!("Reverse DNS lookup failed: {}", e);
format!("ip-{}", ip_string.replace(".", "-"))
ip_string
}
};

Expand Down