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
21mod helpers;
22pub use helpers::*;
23
24pub mod protocols;
25
26mod tcp;
27pub use tcp::{ConnectError, Tcp};
28
29use std::net::IpAddr;
30
31/// A trait for objects containing a [`Tcp`]; it is required to implement protocols.
32pub trait P2P {
33 /// Returns a reference to the TCP instance.
34 fn tcp(&self) -> &Tcp;
35}
36
37/// Checks if the given IP address is a bogon address.
38///
39/// A bogon address is an IP address that should not appear on the public Internet.
40/// This includes private addresses, loopback addresses, and link-local addresses.
41pub fn is_bogon_ip(ip: IpAddr) -> bool {
42 match ip {
43 IpAddr::V4(ipv4) => ipv4.is_loopback() || ipv4.is_private() || ipv4.is_link_local(),
44 IpAddr::V6(ipv6) => ipv6.is_loopback(),
45 }
46}
47
48/// Checks if the given IP address is unspecified or broadcast.
49pub fn is_unspecified_or_broadcast_ip(ip: IpAddr) -> bool {
50 match ip {
51 IpAddr::V4(ipv4) => ipv4.is_unspecified() || ipv4.is_broadcast(),
52 ipv6 => ipv6.is_unspecified(),
53 }
54}