libp2p/services/nat/portmapper

Search:
Group by:
Source   Edit  

Types

MapperResponse = object
  success*: bool
  errorLen*: int
  errorBuf*: array[ErrBufLen, char]
  ip*: Opt[IpAddress]
  externalPort*: uint16
POD payload returned by a worker thread to the dispatch caller. All fields are fixed-size value types so the response is safe to copy across the thread boundary without crossing the Nim GC heap. Source   Edit  
MapProto = enum
  mpTcp, mpUdp
Source   Edit  
PortMapper = ref object of RootObj
Abstract base for a NAT port-mapping client (UPnP / NAT-PMP / mock). All operations are async because real implementations dispatch the underlying sync C calls to a dedicated worker thread. Source   Edit  

Consts

ErrBufLen = 256
Source   Edit  

Procs

proc fireSyncOrErr(signal: ThreadSignalPtr): Result[void, string] {....raises: [],
    tags: [], forbids: [].}
Wraps fireSync so both failure modes (underlying error or completion within the internal timeout returning false) surface as a single Result — callers branch once instead of unfolding isErr/not get() twice. Source   Edit  
proc free[T](ctx: ptr T) {....raises: [].}
Releases a worker ctx's reqSignal/respSignal and the shared allocation. Tolerates partial initialization — a signal field that was never assigned (or that was already closed in-place) is left as nil and skipped here. Source   Edit  
proc getError(resp: MapperResponse): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc initSignals[T](ctx: ptr T; owner: static string) {.
    ...raises: [ResourceExhaustedError], raises: [].}
Allocates reqSignal + respSignal on the worker ctx; on either failure frees what was already allocated (via free) and raises with owner as the message prefix so the caller knows which mapper hit the limit. Source   Edit  
proc safeRelease(lock: AsyncLock; owner: static string) {....raises: [].}
Releases an AsyncLock from a defer block. AsyncLockError (release without acquire) is a developer error, not a runtime mode — surface it as an assertion so the bug is caught loud instead of silently warned. Source   Edit  
proc setError(resp: var MapperResponse; msg: string) {....raises: [], tags: [],
    forbids: [].}
Source   Edit  

Methods

method close(self: PortMapper): InternalRaisesFuture[void, void] {.base, ...gcsafe,
    stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
Source   Edit  
method discover(self: PortMapper; timeout: Duration): InternalRaisesFuture[
    Result[IpAddress, string], (CancelledError,)] {.base, ...gcsafe,
    stackTrace: false, raises: [], gcsafe, raises: [], tags: [RootEffect],
    forbids: [].}
Source   Edit  
method map(self: PortMapper; internalPort: Port; externalPort: Port;
           proto: MapProto; lease: uint32): InternalRaisesFuture[
    Result[Port, string], (CancelledError,)] {.base, ...gcsafe, stackTrace: false,
    raises: [], gcsafe, raises: [], tags: [RootEffect], forbids: [].}
Source   Edit  
method unmap(self: PortMapper; externalPort: Port; proto: MapProto): InternalRaisesFuture[
    Result[void, string], (CancelledError,)] {.base, ...gcsafe, stackTrace: false,
    raises: [], gcsafe, raises: [], tags: [RootEffect], forbids: [].}
Source   Edit