Vine/Server/ValidateSocketAddress.rs
1//! Validate a socket address string before binding.
2//!
3//! Catches three classes of error before the embedder spawns the bind
4//! task:
5//!
6//! 1. Empty address strings (operator typo / missing env var).
7//! 2. Address strings longer than 256 characters (defensive cap).
8//! 3. Addresses that fail `parse::<SocketAddr>()` (malformed).
9//!
10//! Logs a warning - but does not reject - when the port is in the
11//! privileged range (< 1024), since Land's defaults all live above
12//! 50 000.
13
14use std::net::SocketAddr;
15
16use crate::{Error::VineError, dev_log};
17
18/// Parses and validates `AddressString` for use as a Vine gRPC bind address.
19///
20/// # Parameters
21///
22/// * `AddressString` - the address string to validate (e.g. `"[::1]:50051"`).
23/// * `ServerName` - human-readable name of the server, used in error
24/// messages and the privileged-port warning (e.g. `"MountainService"`).
25///
26/// # Errors
27///
28/// Returns `VineError::InvalidMessageFormat` for empty or oversize strings,
29/// `VineError::AddressParseError` for malformed addresses.
30pub fn Fn(AddressString:&str, ServerName:&str) -> Result<SocketAddr, VineError> {
31 if AddressString.is_empty() {
32 return Err(VineError::InvalidMessageFormat(format!(
33 "{} address cannot be empty",
34 ServerName
35 )));
36 }
37
38 if AddressString.len() > 256 {
39 return Err(VineError::InvalidMessageFormat(format!(
40 "{} address exceeds maximum length (256 characters)",
41 ServerName
42 )));
43 }
44
45 match AddressString.parse::<SocketAddr>() {
46 Ok(Address) => {
47 if Address.port() < 1024 {
48 dev_log!(
49 "grpc",
50 "warn: [Vine::Server] {} using privileged port {}, this may require elevated privileges",
51 ServerName,
52 Address.port()
53 );
54 }
55
56 Ok(Address)
57 },
58
59 Err(Error) => Err(VineError::AddressParseError(Error)),
60 }
61}