snarkos_node_tcp/
lib.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![deny(missing_docs)]
17#![deny(unsafe_code)]
18
19//! **Tcp** is a simple, low-level, and customizable implementation of a TCP stack.
20
21#[cfg(feature = "metrics")]
22extern crate snarkos_node_metrics as metrics;
23
24mod helpers;
25pub use helpers::*;
26
27pub mod protocols;
28
29mod tcp;
30pub use tcp::{ConnectError, Tcp};
31
32use std::net::IpAddr;
33
34/// A trait for objects containing a [`Tcp`]; it is required to implement protocols.
35pub trait P2P {
36    /// Returns a reference to the TCP instance.
37    fn tcp(&self) -> &Tcp;
38}
39
40/// Checks if the given IP address is a bogon address.
41///
42/// A bogon address is an IP address that should not appear on the public Internet.
43/// This includes private addresses, loopback addresses, and link-local addresses.
44pub fn is_bogon_ip(ip: IpAddr) -> bool {
45    match ip {
46        IpAddr::V4(ipv4) => ipv4.is_loopback() || ipv4.is_private() || ipv4.is_link_local(),
47        IpAddr::V6(ipv6) => ipv6.is_loopback(),
48    }
49}
50
51/// Checks if the given IP address is unspecified or broadcast.
52pub fn is_unspecified_or_broadcast_ip(ip: IpAddr) -> bool {
53    match ip {
54        IpAddr::V4(ipv4) => ipv4.is_unspecified() || ipv4.is_broadcast(),
55        ipv6 => ipv6.is_unspecified(),
56    }
57}