Skip to content

Commit

Permalink
chore: Use Ipv#Addr constants (vectordotdev#17627)
Browse files Browse the repository at this point in the history
* chore: Fix a few `const fn` functions

* Use `Ipv#Addr` constants instead of hard-coded addresses

* Fix IPv6 unspecified => localhost typo

* Reflect change to MSRV
  • Loading branch information
bruceg committed Aug 30, 2023
1 parent 314a37b commit 7a6365e
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 28 deletions.
5 changes: 1 addition & 4 deletions lib/vector-config/tests/integration/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,7 @@ const fn default_simple_source_timeout() -> Duration {
}

fn default_simple_source_listen_addr() -> SocketListenAddr {
SocketListenAddr::SocketAddr(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(127, 0, 0, 1),
9200,
)))
SocketListenAddr::SocketAddr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9200)))
}

/// A sink for sending events to the `simple` service.
Expand Down
8 changes: 4 additions & 4 deletions src/config/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const fn default_enabled() -> bool {
/// `vector top` will use it to determine which to connect to by default, if no URL
/// override is provided.
pub fn default_address() -> Option<SocketAddr> {
Some(SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 8686))
Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8686))
}

/// Default GraphQL API address
Expand Down Expand Up @@ -107,7 +107,7 @@ fn bool_merge() {

#[test]
fn bind_merge() {
let address = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 9000);
let address = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 9000);
let mut a = Options {
enabled: true,
address: Some(address),
Expand All @@ -129,12 +129,12 @@ fn bind_merge() {
#[test]
fn bind_conflict() {
let mut a = Options {
address: Some(SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 9000)),
address: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 9000)),
..Options::default()
};

let b = Options {
address: Some(SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 9001)),
address: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 9001)),
..Options::default()
};

Expand Down
2 changes: 1 addition & 1 deletion src/enrichment_tables/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Geoip {
let dbkind = DatabaseKind::from(dbreader.metadata.database_type.as_str());

// Check if we can read database with dummy Ip.
let ip = IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0));
let ip = IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED);
let result = match dbkind {
DatabaseKind::Asn | DatabaseKind::Isp => dbreader.lookup::<Isp>(ip).map(|_| ()),
DatabaseKind::ConnectionType => dbreader.lookup::<ConnectionType>(ip).map(|_| ()),
Expand Down
6 changes: 2 additions & 4 deletions src/sinks/prometheus/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
convert::Infallible,
hash::Hash,
mem::{discriminant, Discriminant},
net::SocketAddr,
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{Arc, RwLock},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -168,9 +168,7 @@ impl Default for PrometheusExporterConfig {
}

const fn default_address() -> SocketAddr {
use std::net::{IpAddr, Ipv4Addr};

SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9598)
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 9598)
}

const fn default_distributions_as_summaries() -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/sinks/statsd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Mode {
}

const fn default_address() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8125)
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8125)
}

impl GenerateConfig for StatsdSinkConfig {
Expand Down
11 changes: 2 additions & 9 deletions src/sinks/util/service/net/udp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::net::SocketAddr;

use snafu::ResultExt;
use tokio::net::UdpSocket;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl UdpConnector {
.ok_or(NetError::NoAddresses)?;

let addr = SocketAddr::new(ip, self.address.port);
let bind_address = find_bind_address(&addr);
let bind_address = crate::sinks::util::udp::find_bind_address(&addr);

let socket = UdpSocket::bind(bind_address).await.context(FailedToBind)?;

Expand All @@ -74,10 +74,3 @@ impl UdpConnector {
Ok(socket)
}
}

const fn find_bind_address(remote_addr: &SocketAddr) -> SocketAddr {
match remote_addr {
SocketAddr::V4(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
SocketAddr::V6(_) => SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),
}
}
2 changes: 1 addition & 1 deletion src/sinks/util/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async fn udp_send(socket: &mut UdpSocket, buf: &[u8]) -> tokio::io::Result<()> {
Ok(())
}

const fn find_bind_address(remote_addr: &SocketAddr) -> SocketAddr {
pub(super) const fn find_bind_address(remote_addr: &SocketAddr) -> SocketAddr {
match remote_addr {
SocketAddr::V4(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
SocketAddr::V6(_) => SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),
Expand Down
2 changes: 1 addition & 1 deletion src/sources/splunk_hec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Default for SplunkConfig {
}

fn default_socket_address() -> SocketAddr {
SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 8088)
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 8088)
}

#[async_trait::async_trait]
Expand Down
2 changes: 1 addition & 1 deletion src/sources/statsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl GenerateConfig for StatsdConfig {
fn generate_config() -> toml::Value {
toml::Value::try_from(Self::Udp(UdpConfig::from_address(
SocketListenAddr::SocketAddr(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(127, 0, 0, 1),
Ipv4Addr::LOCALHOST,
8125,
))),
)))
Expand Down
4 changes: 2 additions & 2 deletions src/test_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ pub fn next_addr_for_ip(ip: IpAddr) -> SocketAddr {
}

pub fn next_addr() -> SocketAddr {
next_addr_for_ip(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))
next_addr_for_ip(IpAddr::V4(Ipv4Addr::LOCALHOST))
}

pub fn next_addr_v6() -> SocketAddr {
next_addr_for_ip(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
next_addr_for_ip(IpAddr::V6(Ipv6Addr::LOCALHOST))
}

pub fn trace_init() {
Expand Down

0 comments on commit 7a6365e

Please sign in to comment.