traceroute

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2025 License: MIT Imports: 6 Imported by: 0

README

🛰️ GoTraceroute

A flexible, cross-platform traceroute library and CLI tool written in Go. Supports IPv4 & IPv6, and works on Linux, macOS, and Windows.

I personally tested only on Linux. Although it works, if didn't open an issue.


✏️ Features

✅ IPv4 and IPv6 support

✅ Works transparently across Unix and Windows (using golang.org/x/sys + build tags)

✅ Simple context-aware API for use as a library or standalone CLI tool


⚙️ Usage

CLI:
  • Build and run:
git clone https://github.com/0ne-zero/traceroute.git
cd traceroute/cmd/
go build -o gotraceroute ./gotraceroute.go
sudo ./gotraceroute -h
  • Download binary release: Download the appropriate binary for your OS and architecture from the releases page.

Note: On Linux and macOS, raw sockets require sudo. On Windows, run from an elevated terminal.


Library:

  • Import and call:
import (
    "context"
    "fmt"
    "log"

    "github.com/0ne-zero/traceroute"
)

func main() {
    // The options have default value, but you can change them based on your need
    opts := &traceroute.Options{}
    hopCh := make(chan traceroute.Hop)

    // Using context to set a timeout for the traceroute
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    // The TracerouteContext returns complete results and it also could take a channel as fourth argument and stream the results into it
    // Passing the channel to the function is optional
    result, err := traceroute.TracerouteContext(ctx, "example.com", opts, hopCh)
    if err != nil {
        log.Fatal(err)
    }

    // Consuming results from the passed channel 
    go func() {
        for hop := range hopCh {
            fmt.Printf("TTL %d\t%s\t%v\n", hop.TTL, hop.Address, hop.ElapsedTime)
        }
    }()

    // Use the final aggregated result if you need
    fmt.Println("Traceroute finished. Total hops:", len(result.Hops))
}

📚 References

RFC 792 — Internet Control Message Protocol (ICMP)

Documentation

Index

Constants

View Source
const (
	DefaultSrcPort             = 33777
	DefaultDestPort            = 33434
	DefaultMaxHops             = 64
	DefaultFirstHop            = 1
	DefaultTimeoutMs           = 500
	DefaultRetries             = 3
	DefaultPacketSize          = 512
	DefaultPreferAddressFamily = trace_socket.AF_INET
)

Variables

This section is empty.

Functions

func NewTracerouteOptions

func NewTracerouteOptions() *tracerouteOptions

NewTracerouteOptions returns TracerouteOptions initialized with defaults.

Types

type TracerouteHop

type TracerouteHop struct {
	Success     bool          // Whether the probe succeeded
	Address     net.IP        // IP address of the node
	Host        string        // Resolved hostname of the node (if available)
	Bytes       int           // Number of bytes received (optional)
	ElapsedTime time.Duration // Round-trip time for the probe
	TTL         int           // Time-To-Live value for this hop
}

TracerouteHop represents a single hop in a traceroute operation.

func (*TracerouteHop) AddressString

func (hop *TracerouteHop) AddressString() string

AddressString returns the string representation of the IP address or empty string if nil.

func (*TracerouteHop) HostOrAddressString

func (hop *TracerouteHop) HostOrAddressString() string

HostOrAddressString returns the hostname if available, otherwise the IP address string.

type TracerouteResult

type TracerouteResult struct {
	DestinationAddress net.IP
	Hops               []TracerouteHop
}

func Traceroute

func Traceroute(dest string, options *tracerouteOptions, chans ...chan TracerouteHop) (TracerouteResult, error)

Traceroute performs a traceroute to the destination using the given options. This is the default entry point without cancellation.

func TracerouteContext

func TracerouteContext(ctx context.Context, dest string, options *tracerouteOptions, chans ...chan TracerouteHop) (TracerouteResult, error)

TracerouteContext performs a traceroute that can be cancelled via context. It works by sending UDP packets with increasing TTL/hop limit and listening for ICMP Time Exceeded or Destination Unreachable replies.

Directories

Path Synopsis
cmd
net

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL