-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): IP resolution in docker (#10681)
Co-authored-by: Cody Wang <[email protected]>
- Loading branch information
1 parent
3d7bcb0
commit 6764f7b
Showing
14 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -536,6 +536,7 @@ tower-http = "0.5" | |
|
||
# p2p | ||
discv5 = "0.7.0" | ||
if-addrs = "0.13" | ||
|
||
# rpc | ||
jsonrpsee = "0.24" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! IP resolution on non-host Docker network. | ||
#![cfg(not(target_os = "windows"))] | ||
|
||
use std::{io, net::IpAddr}; | ||
|
||
/// The 'eth0' interface tends to be the default interface that docker containers use to | ||
/// communicate with each other. | ||
pub const DEFAULT_NET_IF_NAME: &str = "eth0"; | ||
|
||
/// Errors resolving network interface IP. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum NetInterfaceError { | ||
/// Error reading OS interfaces. | ||
#[error("failed to read OS interfaces: {0}")] | ||
Io(io::Error), | ||
/// No interface found with given name. | ||
#[error("interface not found: {0}, found other interfaces: {1:?}")] | ||
IFNotFound(String, Vec<String>), | ||
} | ||
|
||
/// Reads IP of OS interface with given name, if exists. | ||
#[cfg(not(target_os = "windows"))] | ||
pub fn resolve_net_if_ip(if_name: &str) -> Result<IpAddr, NetInterfaceError> { | ||
match if_addrs::get_if_addrs() { | ||
Ok(ifs) => { | ||
let ip = ifs.iter().find(|i| i.name == if_name).map(|i| i.ip()); | ||
match ip { | ||
Some(ip) => Ok(ip), | ||
None => { | ||
let ifs = ifs.into_iter().map(|i| i.name.as_str().into()).collect(); | ||
Err(NetInterfaceError::IFNotFound(if_name.into(), ifs)) | ||
} | ||
} | ||
} | ||
Err(err) => Err(NetInterfaceError::Io(err)), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::net::Ipv4Addr; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn read_docker_if_addr() { | ||
const LOCALHOST_IF: [&str; 2] = ["lo0", "lo"]; | ||
|
||
let ip = resolve_net_if_ip(LOCALHOST_IF[0]) | ||
.unwrap_or_else(|_| resolve_net_if_ip(LOCALHOST_IF[1]).unwrap()); | ||
|
||
assert_eq!(ip, Ipv4Addr::LOCALHOST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters