@ -29,8 +29,6 @@ const (
bindingRequest = "\x00\x01"
bindingRequest = "\x00\x01"
magicCookie = "\x21\x12\xa4\x42"
magicCookie = "\x21\x12\xa4\x42"
lenFingerprint = 8 // 2+byte header + 2-byte length + 4-byte crc32
lenFingerprint = 8 // 2+byte header + 2-byte length + 4-byte crc32
ipv4Len = 4
ipv6Len = 16
headerLen = 20
headerLen = 20
)
)
@ -135,7 +133,6 @@ var (
func foreachAttr ( b [ ] byte , fn func ( attrType uint16 , a [ ] byte ) error ) error {
func foreachAttr ( b [ ] byte , fn func ( attrType uint16 , a [ ] byte ) error ) error {
for len ( b ) > 0 {
for len ( b ) > 0 {
if len ( b ) < 4 {
if len ( b ) < 4 {
return errors . New ( "effed-f1" )
return ErrMalformedAttrs
return ErrMalformedAttrs
}
}
attrType := binary . BigEndian . Uint16 ( b [ : 2 ] )
attrType := binary . BigEndian . Uint16 ( b [ : 2 ] )
@ -143,7 +140,6 @@ func foreachAttr(b []byte, fn func(attrType uint16, a []byte) error) error {
attrLenPad := attrLen % 4
attrLenPad := attrLen % 4
b = b [ 4 : ]
b = b [ 4 : ]
if attrLen + attrLenPad > len ( b ) {
if attrLen + attrLenPad > len ( b ) {
return errors . New ( "effed-f2" )
return ErrMalformedAttrs
return ErrMalformedAttrs
}
}
if err := fn ( attrType , b [ : attrLen ] ) ; err != nil {
if err := fn ( attrType , b [ : attrLen ] ) ; err != nil {
@ -161,9 +157,9 @@ func Response(txID TxID, ip net.IP, port uint16) []byte {
}
}
var fam byte
var fam byte
switch len ( ip ) {
switch len ( ip ) {
case 4 :
case net . IPv4len :
fam = 1
fam = 1
case 16 :
case net . IPv6len :
fam = 2
fam = 2
default :
default :
return nil
return nil
@ -194,8 +190,6 @@ func Response(txID TxID, ip net.IP, port uint16) []byte {
return b
return b
}
}
func beu16 ( b [ ] byte ) uint16 { return binary . BigEndian . Uint16 ( b ) }
// ParseResponse parses a successful binding response STUN packet.
// ParseResponse parses a successful binding response STUN packet.
// The IP address is extracted from the XOR-MAPPED-ADDRESS attribute.
// The IP address is extracted from the XOR-MAPPED-ADDRESS attribute.
// The returned addr slice is owned by the caller and does not alias b.
// The returned addr slice is owned by the caller and does not alias b.
@ -207,7 +201,7 @@ func ParseResponse(b []byte) (tID TxID, addr []byte, port uint16, err error) {
if b [ 0 ] != 0x01 || b [ 1 ] != 0x01 {
if b [ 0 ] != 0x01 || b [ 1 ] != 0x01 {
return tID , nil , 0 , ErrNotSuccessResponse
return tID , nil , 0 , ErrNotSuccessResponse
}
}
attrsLen := int ( beu 16( b [ 2 : 4 ] ) )
attrsLen := int ( binary . BigEndian . Uint 16( b [ 2 : 4 ] ) )
b = b [ headerLen : ] // remove STUN header
b = b [ headerLen : ] // remove STUN header
if attrsLen > len ( b ) {
if attrsLen > len ( b ) {
return tID , nil , 0 , ErrMalformedAttrs
return tID , nil , 0 , ErrMalformedAttrs
@ -272,7 +266,7 @@ func xorMappedAddress(tID TxID, b []byte) (addr []byte, port uint16, err error)
if len ( b ) < 4 {
if len ( b ) < 4 {
return nil , 0 , ErrMalformedAttrs
return nil , 0 , ErrMalformedAttrs
}
}
xorPort := beu 16( b [ 2 : 4 ] )
xorPort := binary . BigEndian . Uint 16( b [ 2 : 4 ] )
addrField := b [ 4 : ]
addrField := b [ 4 : ]
port = xorPort ^ 0x2112 // first half of magicCookie
port = xorPort ^ 0x2112 // first half of magicCookie
@ -298,9 +292,9 @@ func xorMappedAddress(tID TxID, b []byte) (addr []byte, port uint16, err error)
func familyAddrLen ( fam byte ) int {
func familyAddrLen ( fam byte ) int {
switch fam {
switch fam {
case 0x01 : // IPv4
case 0x01 : // IPv4
return ipv4L en
return net. IPv4l en
case 0x02 : // IPv6
case 0x02 : // IPv6
return ipv6L en
return net. IPv6l en
default :
default :
return 0
return 0
}
}