Top Related Projects
CFSSL: Cloudflare's PKI and TLS toolkit
Provides packet processing capabilities for Go
Application Kernel for Containers
ebpf-go is a pure-Go library to read, modify and load eBPF programs and attach them to various hooks in the Linux kernel.
Proxy TCP connections based on static rules, HTTP Host headers, and SNI server names (Go package or binary)
Stenographer is a packet capture solution which aims to quickly spool all packets to disk, then provide simple, fast access to subsets of those packets. Discussion/announcements at stenographer@googlegroups.com
Quick Overview
Netstack is a network stack written in Go that can be used with or without an operating system. It provides a complete implementation of the TCP/IP stack, including IPv4 and IPv6 support, and can be used to build network applications or embedded systems.
Pros
- Pure Go implementation, making it portable and easy to integrate into Go projects
- Can run without an operating system, suitable for embedded systems and specialized network applications
- Provides a complete TCP/IP stack implementation
- Actively maintained by Google
Cons
- May have performance overhead compared to native OS network stacks
- Limited documentation and examples available
- Might require more manual configuration compared to using standard Go net package
- Not as widely adopted as other networking libraries
Code Examples
- Creating a TCP listener:
import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
)
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
})
listener, err := gonet.NewListener(s, tcpip.FullAddress{}, tcp.ProtocolNumber)
if err != nil {
// Handle error
}
defer listener.Close()
- Sending a UDP packet:
import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
)
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{udp.NewProtocol},
})
conn, err := gonet.DialUDP(s, &tcpip.FullAddress{}, nil, ipv4.ProtocolNumber)
if err != nil {
// Handle error
}
defer conn.Close()
_, err = conn.Write([]byte("Hello, UDP!"))
if err != nil {
// Handle error
}
- Setting up an IP endpoint:
import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
})
ep, err := s.CreateNIC(1, /* some link endpoint implementation */)
if err != nil {
// Handle error
}
if err := s.AddAddress(1, ipv4.ProtocolNumber, tcpip.Address(net.ParseIP("192.168.1.1").To4())); err != nil {
// Handle error
}
Getting Started
To use Netstack in your Go project:
-
Install the package:
go get gvisor.dev/gvisor
-
Import the required packages in your Go code:
import ( "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/stack" "
Competitor Comparisons
CFSSL: Cloudflare's PKI and TLS toolkit
Pros of cfssl
- Focused on PKI and TLS certificate management, offering specialized tools for these tasks
- Provides a RESTful API for certificate signing and key management
- Actively maintained with regular updates and contributions
Cons of cfssl
- Limited scope compared to netstack's broader networking capabilities
- May require additional components for full network stack functionality
- Less integration with low-level networking protocols
Code Comparison
cfssl (Go):
cert, err := ocsp.ParseResponse(body, issuer)
if err != nil {
return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
}
netstack (Go):
func (e *endpoint) handlePacket(r *stack.Route, pkt *stack.PacketBuffer) {
// Process incoming packets
// ...
}
Summary
cfssl is a specialized tool for PKI and TLS certificate management, offering a RESTful API and focused functionality. netstack, on the other hand, provides a more comprehensive networking stack with lower-level protocol support. While cfssl excels in certificate-related tasks, netstack offers broader networking capabilities. The choice between the two depends on the specific requirements of the project, with cfssl being more suitable for certificate management and netstack for general networking needs.
Provides packet processing capabilities for Go
Pros of gopacket
- More flexible and versatile for packet manipulation and analysis
- Supports a wider range of protocols and packet types
- Better suited for network security and monitoring applications
Cons of gopacket
- Higher learning curve due to more complex API
- May have higher resource usage for simple networking tasks
- Less optimized for pure network stack implementation
Code comparison
netstack example:
func handlePacket(r *stack.Route, pkt *stack.PacketBuffer) {
// Process incoming packet
}
gopacket example:
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
// Process TCP packet
}
Key differences
netstack is primarily designed as a complete network stack implementation, focusing on efficiency and low-level networking operations. It's well-suited for building custom network stacks or embedded systems.
gopacket, on the other hand, excels in packet analysis, manipulation, and creation. It offers more flexibility for working with various protocols and is often used in network security tools, traffic analysis, and packet injection scenarios.
Choose netstack for building efficient network stacks, and gopacket for more complex packet processing and analysis tasks.
Application Kernel for Containers
Pros of gVisor
- Provides stronger isolation and security for containerized applications
- Offers a more comprehensive system call emulation layer
- Supports a wider range of Linux-compatible applications
Cons of gVisor
- Higher performance overhead compared to netstack
- More complex setup and configuration process
- Larger resource footprint due to its comprehensive emulation layer
Code Comparison
gVisor (runsc):
func (k *Kernel) Start() error {
k.tasks.mu.Lock()
defer k.tasks.mu.Unlock()
return k.tasks.root.start()
}
netstack:
func (s *Stack) Start() error {
s.mu.Lock()
defer s.mu.Unlock()
return s.linkAddrCache.start()
}
Both projects use Go and implement network stacks, but gVisor provides a more comprehensive system emulation layer. The code snippets show similar patterns for starting their respective components, but gVisor's implementation is more complex due to its broader scope.
gVisor is better suited for scenarios requiring enhanced security and isolation, while netstack is more lightweight and focused specifically on network stack implementation. The choice between them depends on the specific requirements of the project, balancing security needs with performance considerations.
ebpf-go is a pure-Go library to read, modify and load eBPF programs and attach them to various hooks in the Linux kernel.
Pros of ebpf
- More focused on eBPF technology, providing specialized tools and libraries
- Active development with frequent updates and contributions
- Broader ecosystem support, including integration with Cilium network project
Cons of ebpf
- Narrower scope, primarily focused on eBPF functionality
- Steeper learning curve for developers not familiar with eBPF concepts
- May require additional components for full network stack functionality
Code Comparison
netstack (Go):
func (e *endpoint) WritePacket(r *stack.Route, gso *stack.GSO, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) *tcpip.Error {
// Implementation details
}
ebpf (Go):
func (p *Program) Pin(fileName string) error {
// Implementation details
}
The code snippets highlight the different focus areas:
- netstack deals with network stack operations
- ebpf focuses on eBPF program management
Both projects use Go, but their APIs and functionalities differ significantly due to their distinct purposes.
Proxy TCP connections based on static rules, HTTP Host headers, and SNI server names (Go package or binary)
Pros of tcpproxy
- Lightweight and focused specifically on TCP proxying
- Easy to set up and use for simple TCP proxy scenarios
- Actively maintained with recent updates
Cons of tcpproxy
- Limited functionality compared to netstack's full network stack
- Less comprehensive documentation and community support
- Narrower use case, not suitable for complex networking tasks
Code Comparison
tcpproxy:
proxy := &tcpproxy.Proxy{
ListenAddr: ":80",
Destination: "example.com:80",
}
proxy.Run()
netstack:
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
})
tcpForwarder := tcp.NewForwarder(s, 0, 10, func(r *tcp.ForwarderRequest) {
// Handle incoming connection
})
s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
netstack provides a more comprehensive network stack implementation, allowing for greater control and customization of network protocols. tcpproxy, on the other hand, offers a simpler solution for basic TCP proxying needs. The choice between the two depends on the specific requirements of your project and the level of network control you need.
Stenographer is a packet capture solution which aims to quickly spool all packets to disk, then provide simple, fast access to subsets of those packets. Discussion/announcements at stenographer@googlegroups.com
Pros of Stenographer
- Specialized for packet capture and indexing, offering efficient storage and retrieval of network traffic data
- Provides a query language for precise packet filtering and extraction
- Designed for high-performance, continuous packet capture in large-scale environments
Cons of Stenographer
- Limited to packet capture and indexing, lacking general-purpose networking capabilities
- Requires significant storage resources for continuous packet capture
- Less versatile compared to Netstack's broader networking functionality
Code Comparison
Stenographer (Go):
func (s *Stenographer) WritePacket(packet gopacket.Packet) error {
// Write packet to disk and update index
}
Netstack (Go):
func (e *endpoint) WritePacket(r *stack.Route, gso *stack.GSO, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) *tcpip.Error {
// Implement network stack packet writing
}
Key Differences
- Stenographer focuses on packet capture and indexing, while Netstack provides a full network stack implementation
- Stenographer is tailored for security and forensics use cases, whereas Netstack is more general-purpose
- Netstack offers lower-level network protocol implementations, while Stenographer operates at a higher abstraction level for packet handling
Use Cases
Stenographer:
- Network security monitoring
- Forensic analysis of network traffic
- Compliance and auditing requirements
Netstack:
- Implementing custom network stacks
- Embedded systems and IoT devices
- Network simulation and testing environments
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
NOTE: This repository is no longer maintained. The Netstack code will continue to be updated and maintained as part of gVisor, which now also maintains a branch that is useable with standard Go tools.
Netstack
Netstack is a network stack written in Go.
Getting started
Try it out on Linux by installing the tun_tcp_echo demo:
go install github.com/google/netstack/tcpip/sample/tun_tcp_echo
Create a TUN device with:
[sudo] ip tuntap add user <username> mode tun <device-name>
[sudo] ip link set <device-name> up
[sudo] ip addr add <ipv4-address>/<mask-length> dev <device-name>
Then run with:
tun_tcp_echo <device-name> <ipv4-address> <port>
Contributions
Please see CONTRIBUTING.md for more details.
Issues/Bug Reports
Netstack is primarily developed as part of gVisor and any issues/bugs should be filed against the gVisor repository as this repo is not actively monitored for bug reports.
Disclaimer
This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.
Top Related Projects
CFSSL: Cloudflare's PKI and TLS toolkit
Provides packet processing capabilities for Go
Application Kernel for Containers
ebpf-go is a pure-Go library to read, modify and load eBPF programs and attach them to various hooks in the Linux kernel.
Proxy TCP connections based on static rules, HTTP Host headers, and SNI server names (Go package or binary)
Stenographer is a packet capture solution which aims to quickly spool all packets to disk, then provide simple, fast access to subsets of those packets. Discussion/announcements at stenographer@googlegroups.com
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot