-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
portmapping.go
60 lines (55 loc) · 2.17 KB
/
portmapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package natpmp
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net/netip"
"time"
)
var (
ErrNetworkProtocolUnknown = errors.New("network protocol is unknown")
ErrLifetimeTooLong = errors.New("lifetime is too long")
)
// Add or delete a port mapping. To delete a mapping, set both the
// requestedExternalPort and lifetime to 0.
// See https://www.ietf.org/rfc/rfc6886.html#section-3.3
func (c *Client) AddPortMapping(ctx context.Context, gateway netip.Addr,
protocol string, internalPort, requestedExternalPort uint16,
lifetime time.Duration) (durationSinceStartOfEpoch time.Duration,
assignedInternalPort, assignedExternalPort uint16, assignedLifetime time.Duration,
err error) {
lifetimeSecondsFloat := lifetime.Seconds()
const maxLifetimeSeconds = uint64(^uint32(0))
if uint64(lifetimeSecondsFloat) > maxLifetimeSeconds {
return 0, 0, 0, 0, fmt.Errorf("%w: %d seconds must at most %d seconds",
ErrLifetimeTooLong, uint64(lifetimeSecondsFloat), maxLifetimeSeconds)
}
const messageSize = 12
message := make([]byte, messageSize)
message[0] = 0 // Version 0
switch protocol {
case "udp":
message[1] = 1 // operationCode 1
case "tcp":
message[1] = 2 // operationCode 2
default:
return 0, 0, 0, 0, fmt.Errorf("%w: %s", ErrNetworkProtocolUnknown, protocol)
}
// [2:3] are reserved.
binary.BigEndian.PutUint16(message[4:6], internalPort)
binary.BigEndian.PutUint16(message[6:8], requestedExternalPort)
binary.BigEndian.PutUint32(message[8:12], uint32(lifetimeSecondsFloat))
const responseSize = 16
response, err := c.rpc(ctx, gateway, message, responseSize)
if err != nil {
return 0, 0, 0, 0, fmt.Errorf("executing remote procedure call: %w", err)
}
secondsSinceStartOfEpoch := binary.BigEndian.Uint32(response[4:8])
durationSinceStartOfEpoch = time.Duration(secondsSinceStartOfEpoch) * time.Second
assignedInternalPort = binary.BigEndian.Uint16(response[8:10])
assignedExternalPort = binary.BigEndian.Uint16(response[10:12])
lifetimeInSeconds := binary.BigEndian.Uint32(response[12:16])
assignedLifetime = time.Duration(lifetimeInSeconds) * time.Second
return durationSinceStartOfEpoch, assignedInternalPort, assignedExternalPort, assignedLifetime, nil
}