Files
ExploreDNS/internal/traverse/stack.go
T
9aa85d8e5d
CI / test (pull_request) Failing after 2m13s
docs: comprehensive documentation for ExploreDNS
- Rewrite README.md with overview, features, installation (go install +
  build from source), quick start, full CLI flag reference table,
  output section descriptions, project structure, and development guide

- Add package-level doc comments to all five internal packages:
  config, dns, fingerprint, output, traverse (via doc.go or existing
  package-declaration files)

- Add GoDoc comments on every exported type, constant, function, and
  method across all packages:
  - internal/config: Config struct fields, all Parse*/Default/Validate
  - internal/dns: QueryConfig, Resolver, BasicResolver, CachingResolver,
    ExchangeFunc, RootServer, RootDiscoveryConfig, DecodedResponse,
    ResponseClassification, all exported helpers
  - internal/fingerprint: Fingerprinter, New, NewWithTimeout, Query,
    FingerprintAll
  - internal/traverse: Traverser, TraverserConfig, TraversalResult,
    Referral, ResolutionState, Response, ResponseType, InfoCache,
    Stack, TraverserHooks, EventStage, TraversalEvent, EventHandler,
    CircularReferralError, UnresolvableNameserverError
  - internal/output: Format, Config, Formatter, SummaryStats,
    NewFormatter, AttachHooks, RunTraversal, DefaultConfig,
    ComputeSummary

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-06-08 04:01:51 +10:00

72 lines
1.7 KiB
Go

package traverse
// DefaultMaxDepth is the maximum traversal depth used when no explicit depth is configured.
const DefaultMaxDepth = 20
// Stack is a depth-limited LIFO queue of Referrals used to drive iterative
// DNS traversal. Pushing a Referral whose Depth exceeds maxDepth fails
// silently and returns false, preventing unbounded traversal.
type Stack struct {
items []*Referral
maxDepth int
}
// NewStack creates a Stack with the given maximum depth.
// When maxDepth is ≤ 0, DefaultMaxDepth is used.
func NewStack(maxDepth int) *Stack {
if maxDepth <= 0 {
maxDepth = DefaultMaxDepth
}
return &Stack{
items: make([]*Referral, 0),
maxDepth: maxDepth,
}
}
// Push adds r to the stack. Returns false (and does not add) when r is nil
// or r.Depth >= maxDepth.
func (s *Stack) Push(r *Referral) bool {
if r == nil {
return false
}
if r.Depth >= s.maxDepth {
return false
}
s.items = append(s.items, r)
return true
}
// Pop removes and returns the top Referral, or nil when the stack is empty.
func (s *Stack) Pop() *Referral {
if len(s.items) == 0 {
return nil
}
idx := len(s.items) - 1
item := s.items[idx]
s.items = s.items[:idx]
return item
}
// Peek returns the top Referral without removing it, or nil when empty.
func (s *Stack) Peek() *Referral {
if len(s.items) == 0 {
return nil
}
return s.items[len(s.items)-1]
}
// Len returns the current number of items in the stack.
func (s *Stack) Len() int {
return len(s.items)
}
// MaxDepth returns the configured maximum depth for this stack.
func (s *Stack) MaxDepth() int {
return s.maxDepth
}
// IsEmpty reports whether the stack has no items.
func (s *Stack) IsEmpty() bool {
return len(s.items) == 0
}