-
Notifications
You must be signed in to change notification settings - Fork 142
/
cea.go
80 lines (71 loc) · 2.55 KB
/
cea.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
// Copyright 2013-2015 go-diameter authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package smparser
import (
"fmt"
"github.com/fiorix/go-diameter/v4/diam"
"github.com/fiorix/go-diameter/v4/diam/datatype"
)
// CEA is a Capabilities-Exchange-Answer message.
// See RFC 6733 section 5.3.2 for details.
type CEA struct {
ResultCode uint32 `avp:"Result-Code"`
OriginHost datatype.DiameterIdentity `avp:"Origin-Host"`
OriginRealm datatype.DiameterIdentity `avp:"Origin-Realm"`
OriginStateID uint32 `avp:"Origin-State-Id"`
AcctApplicationID []*diam.AVP `avp:"Acct-Application-Id"`
AuthApplicationID []*diam.AVP `avp:"Auth-Application-Id"`
VendorSpecificApplicationID []*diam.AVP `avp:"Vendor-Specific-Application-Id"`
FailedAVP []*diam.AVP `avp:"Failed-AVP"`
ErrorMessage string `avp:"Error-Message"`
appID []uint32 // List of supported application IDs.
}
// ErrFailedResultCode is returned by Dial or DialTLS when the handshake
// answer (CEA) contains a Result-Code AVP that is not success (2001).
type ErrFailedResultCode struct {
*CEA
}
// Error implements the error interface.
func (e ErrFailedResultCode) Error() string {
return fmt.Sprintf("failed Result-Code AVP: %d", e.CEA.ResultCode)
}
// Parse parses and validates the given message.
func (cea *CEA) Parse(m *diam.Message, localRole Role) (err error) {
if err = m.Unmarshal(cea); err != nil {
return err
}
if err = cea.sanityCheck(); err != nil {
return err
}
if cea.ResultCode != diam.Success {
return &ErrFailedResultCode{CEA: cea}
}
app := &Application{
AcctApplicationID: cea.AcctApplicationID,
AuthApplicationID: cea.AuthApplicationID,
VendorSpecificApplicationID: cea.VendorSpecificApplicationID,
}
if _, err := app.Parse(m.Dictionary(), localRole); err != nil {
return err
}
cea.appID = app.ID()
return nil
}
// sanityCheck ensures mandatory AVPs are present.
func (cea *CEA) sanityCheck() error {
if cea.ResultCode == 0 {
return ErrMissingResultCode
}
if len(cea.OriginHost) == 0 {
return ErrMissingOriginHost
}
if len(cea.OriginRealm) == 0 {
return ErrMissingOriginRealm
}
return nil
}
// Applications return a list of supported Application IDs.
func (cea *CEA) Applications() []uint32 {
return cea.appID
}