Skip to content

Commit

Permalink
Merge pull request #1693 from etungsten/hostname-format
Browse files Browse the repository at this point in the history
netdog: remove IP string formatting when setting as hostname
  • Loading branch information
etungsten authored Aug 4, 2021
2 parents e251517 + 0de2d11 commit 3cd2da4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
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

0 comments on commit 3cd2da4

Please sign in to comment.