package stun
import (
"errors"
"fmt"
"io"
)
type ErrorCodeAttribute struct {
Code ErrorCode
Reason []byte
}
func (c ErrorCodeAttribute ) String () string {
return fmt .Sprintf ("%d: %s" , c .Code , c .Reason )
}
const (
errorCodeReasonStart = 4
errorCodeClassByte = 2
errorCodeNumberByte = 3
errorCodeReasonMaxB = 763
errorCodeModulo = 100
)
func (c ErrorCodeAttribute ) AddTo (m *Message ) error {
value := make ([]byte , 0 , errorCodeReasonStart +errorCodeReasonMaxB )
if err := CheckOverflow (AttrErrorCode ,
len (c .Reason )+errorCodeReasonStart ,
errorCodeReasonMaxB +errorCodeReasonStart ,
); err != nil {
return err
}
value = value [:errorCodeReasonStart +len (c .Reason )]
number := byte (c .Code % errorCodeModulo )
class := byte (c .Code / errorCodeModulo )
value [errorCodeClassByte ] = class
value [errorCodeNumberByte ] = number
copy (value [errorCodeReasonStart :], c .Reason )
m .Add (AttrErrorCode , value )
return nil
}
func (c *ErrorCodeAttribute ) GetFrom (m *Message ) error {
v , err := m .Get (AttrErrorCode )
if err != nil {
return err
}
if len (v ) < errorCodeReasonStart {
return io .ErrUnexpectedEOF
}
var (
class = uint16 (v [errorCodeClassByte ])
number = uint16 (v [errorCodeNumberByte ])
code = int (class *errorCodeModulo + number )
)
c .Code = ErrorCode (code )
c .Reason = v [errorCodeReasonStart :]
return nil
}
type ErrorCode int
var ErrNoDefaultReason = errors .New ("no default reason for ErrorCode" )
func (c ErrorCode ) AddTo (m *Message ) error {
reason := errorReasons [c ]
if reason == nil {
return ErrNoDefaultReason
}
a := &ErrorCodeAttribute {
Code : c ,
Reason : reason ,
}
return a .AddTo (m )
}
const (
CodeTryAlternate ErrorCode = 300
CodeBadRequest ErrorCode = 400
CodeUnauthorized ErrorCode = 401
CodeUnknownAttribute ErrorCode = 420
CodeStaleNonce ErrorCode = 438
CodeRoleConflict ErrorCode = 487
CodeServerError ErrorCode = 500
)
const (
CodeUnauthorised = CodeUnauthorized
)
const (
CodeForbidden ErrorCode = 403
CodeAllocMismatch ErrorCode = 437
CodeWrongCredentials ErrorCode = 441
CodeUnsupportedTransProto ErrorCode = 442
CodeAllocQuotaReached ErrorCode = 486
CodeInsufficientCapacity ErrorCode = 508
)
const (
CodeConnAlreadyExists ErrorCode = 446
CodeConnTimeoutOrFailure ErrorCode = 447
)
const (
CodeAddrFamilyNotSupported ErrorCode = 440
CodePeerAddrFamilyMismatch ErrorCode = 443
)
var errorReasons = map [ErrorCode ][]byte {
CodeTryAlternate : []byte ("Try Alternate" ),
CodeBadRequest : []byte ("Bad Request" ),
CodeUnauthorized : []byte ("Unauthorized" ),
CodeUnknownAttribute : []byte ("Unknown Attribute" ),
CodeStaleNonce : []byte ("Stale Nonce" ),
CodeServerError : []byte ("Server Error" ),
CodeRoleConflict : []byte ("Role Conflict" ),
CodeForbidden : []byte ("Forbidden" ),
CodeAllocMismatch : []byte ("Allocation Mismatch" ),
CodeWrongCredentials : []byte ("Wrong Credentials" ),
CodeUnsupportedTransProto : []byte ("Unsupported Transport Protocol" ),
CodeAllocQuotaReached : []byte ("Allocation Quota Reached" ),
CodeInsufficientCapacity : []byte ("Insufficient Capacity" ),
CodeConnAlreadyExists : []byte ("Connection Already Exists" ),
CodeConnTimeoutOrFailure : []byte ("Connection Timeout or Failure" ),
CodeAddrFamilyNotSupported : []byte ("Address Family not Supported" ),
CodePeerAddrFamilyMismatch : []byte ("Peer Address Family Mismatch" ),
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .