Skip to main content

Crate networkframework

Crate networkframework 

Source
Expand description

§networkframework-rs

Safe Rust bindings for Apple’s Network.framework, backed by a Swift bridge plus an opt-in raw FFI surface.

§What’s new in v0.9

  • SwiftPM bridge modeled after the screencapturekit-rs layout.
  • Safe modules for the requested logical areas: Connection, Listener, Browser, Parameters, Endpoint, Path, Framer, Group, Protocol, ContentContext, Resolver, Quic, PrivacyContext, ProxyConfig, and AdvertiseDescriptor.
  • raw-ffi feature for direct access to the low-level bridge symbols.
  • COVERAGE.md, per-area smoke tests, and runnable examples for every area.

§Crate layout

  • Safe API: enabled by default.
  • Raw bridge API: --features raw-ffi
  • Coverage report: COVERAGE.md

§Quick start

use networkframework::{TcpClient, TcpListener};

let listener = TcpListener::bind(0)?;
let port = listener.local_port();
let server = std::thread::spawn(move || -> Result<(), networkframework::NetworkError> {
    let connection = listener.accept()?;
    let request = connection.receive(1024)?;
    assert_eq!(request, b"ping");
    connection.send(b"pong")?;
    Ok(())
});

let client = TcpClient::connect("127.0.0.1", port)?;
client.send(b"ping")?;
let reply = client.receive(1024)?;
assert_eq!(reply, b"pong");
server.join().expect("server thread")?;

§Availability notes

Some Apple APIs are runtime-gated by the operating system:

  • Application-service browsing / advertising / parameters: macOS 13+
  • Relay and Oblivious HTTP proxy configuration: macOS 14+
  • Ultra-constrained path / parameter flags and link quality: newer SDK/runtime combinations

The safe wrappers return NetworkError::InvalidArgument when a requested API is unavailable at runtime.

§Examples

cargo run --example 01_get_example
cargo run --example 04_bonjour
cargo run --example 06_bonjour_advertise
cargo run --example framer_length_prefix
cargo run --example content_context_overview
cargo run --example resolver_overview
cargo run --example privacy_context_overview
cargo run --example quic_options

§Validation

cargo clippy --all-targets -- -D warnings
cargo test
for ex in examples/*.rs; do cargo run --example "$(basename "$ex" .rs)"; done

§API documentation

Re-exports§

pub use advertise_descriptor::advertise_with_descriptor;
pub use advertise_descriptor::AdvertiseDescriptor;
pub use advertise_descriptor::Advertiser;
pub use browser::advertise_bonjour_service;
pub use browser::start_browser;
pub use browser::start_browser_results_with_descriptor;
pub use browser::start_browser_with_descriptor;
pub use browser::BonjourAdvertiser;
pub use browser::BrowseDescriptor;
pub use browser::BrowseResult;
pub use browser::BrowseResultChange;
pub use browser::BrowseResultsBrowser;
pub use browser::Browser;
pub use browser::BrowserEvent;
pub use browser::BrowserState;
pub use browser::DiscoveredService;
pub use client::ContentContext;
pub use client::ReceivedContent;
pub use client::TcpClient;
pub use connection::Connection;
pub use connection_group::ConnectionGroup;
pub use connection_group::ConnectionGroupDescriptor;
pub use connection_group::ConnectionGroupMessage;
pub use connection_group::ConnectionGroupState;
pub use connection_report::DataTransferPathReport;
pub use connection_report::DataTransferReport;
pub use connection_report::DataTransferReportState;
pub use connection_report::EstablishmentProtocol;
pub use connection_report::EstablishmentReport;
pub use connection_report::ResolutionProtocol;
pub use connection_report::ResolutionReport;
pub use connection_report::ResolutionSource;
pub use connection_report::ResolutionStep;
pub use endpoint::Endpoint;
pub use endpoint::EndpointType;
pub use error::ErrorDomain;
pub use error::FrameworkError;
pub use error::NetworkError;
pub use ethernet_channel::EthernetChannel;
pub use ethernet_channel::EthernetChannelState;
pub use ethernet_channel::EthernetFrame;
pub use framer::Framer;
pub use framer::FramerContext;
pub use framer::FramerDefinition;
pub use framer::FramerMessage;
pub use framer::FramerMessageView;
pub use framer::FramerOptions;
pub use framer::FramerStart;
pub use group::Group;
pub use group::GroupDescriptor;
pub use group::GroupMessage;
pub use group::GroupState;
pub use interface::list_interfaces;
pub use interface::InterfaceType;
pub use interface::NetworkInterface;
pub use listener::TcpListener;
pub use parameters::ConnectionParameters;
pub use parameters::ParametersAttribution;
pub use path::LinkQuality;
pub use path::Path;
pub use path::PathStatus;
pub use path::PathUnsatisfiedReason;
pub use path_monitor::start_path_monitor;
pub use path_monitor::start_path_monitor_for_ethernet_channel;
pub use path_monitor::start_path_monitor_with_type;
pub use path_monitor::PathMonitor;
pub use path_monitor::PathUpdate;
pub use privacy::PrivacyContext;
pub use privacy::ProxyConfig;
pub use privacy::RelayHop;
pub use privacy::ResolverConfig;
pub use privacy::UrlSessionConfiguration;
pub use protocol::IpEcnFlag;
pub use protocol::IpLocalAddressPreference;
pub use protocol::IpVersion;
pub use protocol::ProtocolDefinition;
pub use protocol::ProtocolMetadata;
pub use protocol::ProtocolOptions;
pub use protocol::TcpMultipathVersion;
pub use quic::QuicConnection;
pub use quic::QuicOptions;
pub use txt_record::TxtRecord;
pub use txt_record::TxtRecordEntry;
pub use txt_record::TxtRecordFindResult;
pub use txt_record::TxtRecordLookup;
pub use udp::UdpClient;
pub use websocket::Opcode;
pub use websocket::WebSocket;
pub use websocket::WsCloseCode;
pub use websocket::WsMessage;
pub use websocket::WsRequest;
pub use websocket::WsResponse;
pub use websocket::WsResponseStatus;
pub use websocket::WsVersion;

Modules§

advertise_descriptor
Advertise-descriptor helpers.
async_apiasync
Async stream wrappers for Network.framework callback-based APIs.
browser
Browser — Bonjour and application-service discovery via nw_browser.
client
TcpClient — synchronous outbound TCP connection via Network.framework.
connection
Connection-oriented helpers and aliases.
connection_group
Connection groups built on nw_connection_group_*.
connection_report
Connection establishment and data-transfer reports.
content_context
Content-context aliases.
endpoint
Endpoint helpers backed by nw_endpoint_t.
error
Errors raised by networkframework.
ethernet_channel
Ethernet channel wrappers.
framer
Custom protocol framers built on nw_framer_*.
group
Connection-group aliases.
interface
Network interface enumeration helpers.
listener
TcpListener — synchronous TCP listener via Network.framework.
parameters
Configurable nw_parameters wrappers for advanced connections.
path
Path snapshots backed by nw_path_t.
path_monitor
PathMonitor — observe network reachability and interface changes via nw_path_monitor.
prelude
Common imports.
privacy
Privacy contexts, proxy configuration, and encrypted resolver settings.
privacy_context
Privacy-context aliases.
protocol
Protocol definitions and options.
proxy_config
Proxy-configuration aliases.
quic
QUIC helpers backed by Network.framework.
raw_ffiraw-ffi
Raw FFI ABI surface for advanced callers.
resolver
Resolver aliases.
txt_record
TXT-record helper APIs.
udp
UdpClient — connected-mode UDP via Network.framework.
websocket
WebSocket — RFC 6455 WebSocket client over Network.framework.

Structs§

ProtocolStack
Mutable wrapper around nw_protocol_stack_t.
QuicMetadata
QUIC protocol metadata attached to a connection or content context.
SecurityProtocolMetadata
Opaque sec_protocol_metadata_t extracted from QUIC metadata.
SecurityProtocolOptions
Opaque sec_protocol_options_t extracted from QUIC options.

Enums§

ExpiredDnsBehavior
Policy controlling whether expired DNS answers may be used.
InterfaceRadioType
Radio technology reported for an interface path sample.
MultipathService
Multipath policy applied to new connections.
QuicStreamType
QUIC stream direction or datagram mode.
ServiceClass
Connection service class applied to new paths.