forked from fiorix/go-diameter
/
avp.go
165 lines (152 loc) · 4.26 KB
/
avp.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 diam
import (
"encoding/binary"
"errors"
"fmt"
"github.com/fiorix/go-diameter/v4/diam/avp"
"github.com/fiorix/go-diameter/v4/diam/datatype"
"github.com/fiorix/go-diameter/v4/diam/dict"
)
// AVP is a Diameter attribute-value-pair.
type AVP struct {
Code uint32 // Code of this AVP
Flags uint8 // Flags of this AVP
Length int // Length of this AVP's payload
VendorID uint32 // VendorId of this AVP
Data datatype.Type // Data of this AVP (payload)
}
// NewAVP creates and initializes a new AVP.
func NewAVP(code uint32, flags uint8, vendor uint32, data datatype.Type) *AVP {
a := &AVP{
Code: code,
Flags: flags,
VendorID: vendor,
Data: data,
}
a.Length = a.headerLen() + a.Data.Len() // no padding length
if vendor > 0 && flags&avp.Vbit != avp.Vbit {
a.Flags |= avp.Vbit
}
return a
}
// DecodeAVP decodes the bytes of a Diameter AVP.
// It uses the given application id and dictionary for decoding the bytes.
func DecodeAVP(data []byte, application uint32, dictionary *dict.Parser) (*AVP, error) {
avp := &AVP{}
if err := avp.DecodeFromBytes(data, application, dictionary); err != nil {
return avp, err
}
return avp, nil
}
// DecodeFromBytes decodes the bytes of a Diameter AVP.
// It uses the given application id and dictionary for decoding the bytes.
func (a *AVP) DecodeFromBytes(data []byte, application uint32, dictionary *dict.Parser) error {
if len(data) < 8 {
return fmt.Errorf("Not enough data to decode AVP header: %d bytes", len(data))
}
a.Code = binary.BigEndian.Uint32(data[0:4])
a.Flags = data[4]
a.Length = int(uint24to32(data[5:8]))
if len(data) < a.Length {
return fmt.Errorf("Not enough data to decode AVP: %d != %d",
len(data), a.Length)
}
data = data[:a.Length] // this cuts padded bytes off
if len(data) < 8 {
return fmt.Errorf("Not enough data to decode AVP header: %d bytes", len(data))
}
var hdrLength int
var payload []byte
// Read VendorId when required.
if a.Flags&avp.Vbit == avp.Vbit {
a.VendorID = binary.BigEndian.Uint32(data[8:12])
payload = data[12:]
hdrLength = 12
} else {
payload = data[8:]
hdrLength = 8
}
// Find this code in the dictionary.
dictAVP, err := dictionary.FindAVPWithVendor(application, a.Code, a.VendorID)
if err != nil && dictAVP == nil {
return err
}
bodyLen := a.Length - hdrLength
if n := len(payload); n < bodyLen {
return fmt.Errorf(
"Not enough data to decode AVP: %d != %d",
hdrLength, n,
)
}
a.Data, err = datatype.Decode(dictAVP.Data.Type, payload)
if err != nil {
return err
}
// Handle grouped AVPs.
if a.Data.Type() == datatype.GroupedType {
a.Data, err = DecodeGrouped(
a.Data.(datatype.Grouped),
application, dictionary,
)
if err != nil {
return err
}
}
return nil
}
// Serialize returns the byte sequence that represents this AVP.
// It requires at least the Code, Flags and Data fields set.
func (a *AVP) Serialize() ([]byte, error) {
if a.Data == nil {
return nil, errors.New("Failed to serialize AVP: Data is nil")
}
b := make([]byte, a.Len())
err := a.SerializeTo(b)
if err != nil {
return nil, err
}
return b, nil
}
// SerializeTo writes the byte sequence that represents this AVP to a byte array.
func (a *AVP) SerializeTo(b []byte) error {
if a.Data == nil {
return errors.New("Failed to serialize AVP: Data is nil")
}
binary.BigEndian.PutUint32(b[0:4], a.Code)
b[4] = a.Flags
hl := a.headerLen()
copy(b[5:8], uint32to24(uint32(hl+a.Data.Len())))
if a.Flags&avp.Vbit == avp.Vbit {
binary.BigEndian.PutUint32(b[8:12], a.VendorID)
}
payload := a.Data.Serialize()
copy(b[hl:], payload)
// reset padding bytes
b = b[hl+len(payload):]
for i := 0; i < a.Data.Padding(); i++ {
b[i] = 0
}
return nil
}
// Len returns the length of this AVP in bytes with padding.
func (a *AVP) Len() int {
return a.headerLen() + a.Data.Len() + a.Data.Padding()
}
func (a *AVP) headerLen() int {
if a.Flags&avp.Vbit == avp.Vbit {
return 12
}
return 8
}
func (a *AVP) String() string {
return fmt.Sprintf("{Code:%d,Flags:0x%x,Length:%d,VendorId:%d,Value:%s}",
a.Code,
a.Flags,
a.Len(),
a.VendorID,
a.Data,
)
}