Skip to content

Commit

Permalink
RFC5780
Browse files Browse the repository at this point in the history
  • Loading branch information
firefart committed Jan 2, 2023
1 parent af0b1a6 commit ea75708
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ tag:
git tag -a "${TAG}" -m "${TAG}"
git push origin "${TAG}"

.PHONY: windows
windows:
GOOS=windows GOARCH=amd64 go fmt ./...
GOOS=windows GOARCH=amd64 go vet ./...
GOOS=windows GOARCH=amd64 go build
12 changes: 11 additions & 1 deletion internal/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,18 @@ func printAttributes(opts InfoOpts, attr []internal.Attribute) {
headerPrinted = true
}

humanName := internal.AttributeTypeString(a.Type)
value := string(a.Value)
// checks for old RFC5780 but still implemented (for example in coturn)
if a.Type == internal.AttrResponseOrigin || a.Type == internal.AttrOtherAddress {
tmpIP, tmpPort, err := internal.ParseMappedAdress(a.Value)
if err != nil {
opts.Log.Errorf("could not parse mapped address: %02x %v", a.Value, err)
continue
}
value = fmt.Sprintf("%s:%d", tmpIP.String(), tmpPort)
}

humanName := internal.AttributeTypeString(a.Type)
if humanName == "" {
if helper.IsPrintable(value) {
opts.Log.Warnf("\tNon Standard Attribute %d returned with value %s", uint16(a.Type), value)
Expand Down
18 changes: 18 additions & 0 deletions internal/helpers_turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ func xorAddr(ip netip.Addr, port uint16, transactionID []byte) ([]byte, error) {
return buf, nil
}

func ParseMappedAdress(input []byte) (*netip.Addr, uint16, error) {
if len(input) < 5 {
return nil, 0, fmt.Errorf("invalid buffer length %d, need to be > 4", len(input))
}
family := input[0:2] // 0x0001 = ipv4, 0x0002 = ipv6
if !bytes.Equal(family, []byte{00, 01}) && !bytes.Equal(family, []byte{00, 02}) {
return nil, 0, fmt.Errorf("invalid family %02x", family)
}
portRaw := input[2:4]
payload := input[4:]
portInt := binary.BigEndian.Uint16(portRaw)
ip, ok := netip.AddrFromSlice(payload)
if !ok {
return nil, 0, fmt.Errorf("invalid IP %02x", payload)
}
return &ip, portInt, nil
}

func ConvertXORAddr(input []byte, transactionID string) (string, uint16, error) {
if len(input) < 5 {
return "", 0, fmt.Errorf("invalid buffer length %d, need to be > 4", len(input))
Expand Down
12 changes: 12 additions & 0 deletions internal/types_stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ const (
AttrAlternateServer AttributeType = 0x8023
// AttrFingerprint https://tools.ietf.org/html/rfc5389#section-15.5
AttrFingerprint AttributeType = 0x8028

// old RFC5780 https://www.rfc-editor.org/rfc/rfc5780#section-7
AttrChangeRequest AttributeType = 0x0003
AttrPadding AttributeType = 0x0026
AttrResponsePort AttributeType = 0x0027
AttrResponseOrigin AttributeType = 0x802b
AttrOtherAddress AttributeType = 0x802c
)

var attrNames = map[AttributeType]string{
Expand All @@ -280,6 +287,11 @@ var attrNames = map[AttributeType]string{
AttrSoftware: "SOFTWARE",
AttrAlternateServer: "ALTERNATE-SERVER",
AttrFingerprint: "FINGERPRINT",
AttrChangeRequest: "CHANGE-REQUEST",
AttrPadding: "PADDING",
AttrResponsePort: "RESPONSE-PORT",
AttrResponseOrigin: "RESPONSE-ORIGIN",
AttrOtherAddress: "OTHER-ADDRESS",
}

/*
Expand Down

0 comments on commit ea75708

Please sign in to comment.