diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2016-12-05 18:45:14 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2016-12-05 19:50:27 +0100 |
| commit | aade5cd0f87affb44073109bf5783d333983f60e (patch) | |
| tree | eb551b1617ca9b7978d696b9e5ee916fac44373d /src | |
| parent | 22da2098045bdf69a178eb7e4eaaaf38989235af (diff) | |
| download | mullvadvpn-aade5cd0f87affb44073109bf5783d333983f60e.tar.xz mullvadvpn-aade5cd0f87affb44073109bf5783d333983f60e.zip | |
Add net module with initial RemoteAddr
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/net.rs | 40 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs index fec2e1da5e..c65c221aec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,9 @@ /// Working with processes. pub mod process; +/// Network primitives. +pub mod net; + #[cfg(test)] mod tests { #[test] diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000000..0906426f7b --- /dev/null +++ b/src/net.rs @@ -0,0 +1,40 @@ +/// Representation of a TCP or UDP endpoint. The host is represented as a String since it can be +/// both a hostname/domain as well as an IP. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct RemoteAddr { + address: String, + port: u16, +} + +impl RemoteAddr { + /// Constructs a new `RemoteAddr` from the given address and port. + pub fn new(address: &str, port: u16) -> Self { + RemoteAddr { + address: address.to_owned(), + port: port, + } + } + + /// Returns the address associated with this `RemoteAddr`. + pub fn address(&self) -> &str { + &self.address + } + + /// Returns the port associated with this `RemoteAddr`. + pub fn port(&self) -> u16 { + self.port + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn remote_addr_new_and_getters() { + let remote_addr = RemoteAddr::new("a_domain", 543); + assert_eq!("a_domain", remote_addr.address()); + assert_eq!(543, remote_addr.port()); + } +} |
