<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Tor",
  "identifier" : "/documentation/Tor/OnionPortMapping",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Tor"
    ],
    "preciseIdentifier" : "s:3Tor16OnionPortMappingV"
  },
  "title" : "OnionPortMapping"
}
-->

# OnionPortMapping

A single virtual-port → target mapping advertised by an onion service.

```
struct OnionPortMapping
```

## Overview

Corresponds to one `Port=<virtualPort>,<target>` entry in an
`ADD_ONION` command (control-spec.txt §3.27). One
[`OnionService`](/documentation/Tor/OnionService) can carry multiple mappings — e.g. port 80 to a web
server, port 443 to the same server for TLS termination — by passing
an array of these to
[`addOnion(key:ports:detach:)`](/documentation/Tor/TorControlClient/addOnion(key:ports:detach:)).

> Note: Conformance is `Sendable` + `Hashable` so collections of
> mappings can be deduplicated and compared verbatim.

> Important: The virtual port lives in the `.onion` namespace (what
> clients dial); the target lives in the process’s kernel address
> space. They are independent and do not need to match.

## Topics

### Creating

[`init(virtualPort:target:)`](/documentation/Tor/OnionPortMapping/init(virtualPort:target:))

Construct a mapping from an explicit virtual port and target.

[`toLocalPort(_:localPort:)`](/documentation/Tor/OnionPortMapping/toLocalPort(_:localPort:))

Convenience factory for loopback TCP mappings.

### Inspection

[`virtualPort`](/documentation/Tor/OnionPortMapping/virtualPort)

The TCP port exposed on the `.onion` address.

[`target`](/documentation/Tor/OnionPortMapping/target)

Where Tor should forward connections that arrive on
[`virtualPort`](/documentation/Tor/OnionPortMapping/virtualPort).

