forked from vanstee/stun-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapped_address.go
112 lines (89 loc) · 2.86 KB
/
mapped_address.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package stun
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
)
const (
IPv4 = 0x01
)
type MappedAddress struct {
Family uint8
Port uint16
Address uint32
}
func NewMappedAddress(family uint8, port uint16, address uint32) *MappedAddress {
return &MappedAddress{
Family: family,
Port: port,
Address: address,
}
}
func (mappedAddress *MappedAddress) IPAddress() net.IP {
address := make([]byte, 4)
binary.BigEndian.PutUint32(address, mappedAddress.Address)
ipAddress := net.IPv4(address[0], address[1], address[2], address[3])
return ipAddress
}
func (mappedAddress *MappedAddress) Length() uint16 {
switch mappedAddress.Family {
case IPv4:
return 5
}
return 0
}
func (mappedAddress *MappedAddress) Serialize() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, mappedAddress.Family)
binary.Write(buffer, binary.BigEndian, mappedAddress.Port)
binary.Write(buffer, binary.BigEndian, mappedAddress.Address)
return buffer.Bytes()
}
func ParseMappedAddress(rawMappedAddress []byte) (*MappedAddress, error) {
buffer := bytes.NewBuffer(rawMappedAddress)
mappedAddress := &MappedAddress{}
var alignment uint8
binary.Read(buffer, binary.BigEndian, &alignment)
if alignment != 0x00 {
return nil, errors.New("Mapped address padding is not empty")
}
binary.Read(buffer, binary.BigEndian, &mappedAddress.Family)
if mappedAddress.Family != IPv4 {
return nil, errors.New("Mapped address family is invalid")
}
binary.Read(buffer, binary.BigEndian, &mappedAddress.Port)
binary.Read(buffer, binary.BigEndian, &mappedAddress.Address)
return mappedAddress, nil
}
func ParseXORMappedAddress(rawMappedAddress []byte, cookie uint32) (*MappedAddress, error) {
buffer := bytes.NewBuffer(rawMappedAddress)
mappedAddress := &MappedAddress{}
var alignment uint8
binary.Read(buffer, binary.BigEndian, &alignment)
if alignment != 0x00 {
return nil, errors.New("Mapped address padding is not empty")
}
binary.Read(buffer, binary.BigEndian, &mappedAddress.Family)
if mappedAddress.Family != IPv4 {
return nil, errors.New("Mapped address family is invalid")
}
binary.Read(buffer, binary.BigEndian, &mappedAddress.Port)
binary.Read(buffer, binary.BigEndian, &mappedAddress.Address)
cookieByte := make([]byte, 4)
binary.BigEndian.PutUint32(cookieByte, cookie)
cookieBuffer := bytes.NewBuffer(cookieByte)
var cookie16 uint16
binary.Read(cookieBuffer, binary.BigEndian, &cookie16)
mappedAddress.Port = mappedAddress.Port ^ cookie16
mappedAddress.Address = mappedAddress.Address ^ cookie
return mappedAddress, nil
}
func (mappedAddress *MappedAddress) String() string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("Family: %d\n", mappedAddress.Family))
buffer.WriteString(fmt.Sprintf("Port: %d\n", mappedAddress.Port))
buffer.WriteString(fmt.Sprintf("Address: %d\n", mappedAddress.Address))
return buffer.String()
}