forked from fiorix/go-diameter
/
message.go
492 lines (445 loc) · 12 KB
/
message.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// 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 (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"sync"
"time"
"github.com/fiorix/go-diameter/diam/avp"
"github.com/fiorix/go-diameter/diam/datatype"
"github.com/fiorix/go-diameter/diam/dict"
)
// MessageBufferLength is the default buffer length for Diameter messages.
var MessageBufferLength = 1 << 10
func init() {
rand.Seed(time.Now().UnixNano())
}
// Message represents a Diameter message.
type Message struct {
Header *Header
AVP []*AVP // AVPs in this message.
// dictionary parser object used to encode and decode AVPs.
dictionary *dict.Parser
}
var readerBufferPool sync.Pool
func newReaderBuffer() *bytes.Buffer {
if v := readerBufferPool.Get(); v != nil {
return v.(*bytes.Buffer)
}
return bytes.NewBuffer(make([]byte, MessageBufferLength))
}
func putReaderBuffer(b *bytes.Buffer) {
if cap(b.Bytes()) == MessageBufferLength {
b.Reset()
readerBufferPool.Put(b)
}
}
func readerBufferSlice(buf *bytes.Buffer, l int) []byte {
b := buf.Bytes()
if l <= MessageBufferLength && cap(b) >= MessageBufferLength {
return b[:l]
}
return make([]byte, l)
}
// ReadMessage reads a binary stream from the reader and uses the given
// dictionary to parse it.
func ReadMessage(reader io.Reader, dictionary *dict.Parser) (*Message, error) {
buf := newReaderBuffer()
defer putReaderBuffer(buf)
m := &Message{dictionary: dictionary}
cmd, err := m.readHeader(reader, buf)
if err != nil {
return nil, err
}
if err = m.readBody(reader, buf, cmd); err != nil {
return nil, err
}
return m, nil
}
func (m *Message) readHeader(r io.Reader, buf *bytes.Buffer) (cmd *dict.Command, err error) {
b := buf.Bytes()[:HeaderLength]
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
m.Header, err = DecodeHeader(b)
if err != nil {
return nil, err
}
cmd, err = m.Dictionary().FindCommand(
m.Header.ApplicationID,
m.Header.CommandCode,
)
if err != nil {
return nil, err
}
return cmd, nil
}
func (m *Message) readBody(r io.Reader, buf *bytes.Buffer, cmd *dict.Command) error {
b := readerBufferSlice(buf, int(m.Header.MessageLength-HeaderLength))
n, err := io.ReadFull(r, b)
if err != nil {
return fmt.Errorf("readBody Error: %v, %d bytes read", err, n)
}
n = m.maxAVPsFor(cmd)
if n == 0 {
// TODO: fail to load the dictionary instead.
return fmt.Errorf(
"Command %s (%d) has no AVPs defined in the dictionary.",
cmd.Name, cmd.Code)
}
// Pre-allocate max # of AVPs for this message.
m.AVP = make([]*AVP, 0, n)
if err = m.decodeAVPs(b); err != nil {
return err
}
return nil
}
func (m *Message) maxAVPsFor(cmd *dict.Command) int {
if m.Header.CommandFlags&RequestFlag == RequestFlag {
return len(cmd.Request.Rule)
}
return len(cmd.Answer.Rule)
}
func (m *Message) decodeAVPs(b []byte) error {
var a *AVP
var err error
for n := 0; n < len(b); {
a, err = DecodeAVP(b[n:], m.Header.ApplicationID, m.Dictionary())
if err != nil {
return fmt.Errorf("Failed to decode AVP: %s", err)
}
m.AVP = append(m.AVP, a)
n += a.Len()
}
return nil
}
// NewMessage creates and initializes a Message.
func NewMessage(cmd uint32, flags uint8, appid, hopbyhop, endtoend uint32, dictionary *dict.Parser) *Message {
if hopbyhop == 0 {
hopbyhop = rand.Uint32()
}
if endtoend == 0 {
endtoend = rand.Uint32()
}
return &Message{
Header: &Header{
Version: 1,
MessageLength: HeaderLength,
CommandFlags: flags,
CommandCode: cmd,
ApplicationID: appid,
HopByHopID: hopbyhop,
EndToEndID: endtoend,
},
dictionary: dictionary,
}
}
// NewRequest creates a new Message with the Request bit set.
func NewRequest(cmd uint32, appid uint32, dictionary *dict.Parser) *Message {
return NewMessage(cmd, RequestFlag, appid, 0, 0, dictionary)
}
// Dictionary returns the dictionary parser object associated with this
// message. This dictionary is used to encode and decode the message.
// If no dictionary is associated then it returns the default dictionary.
func (m *Message) Dictionary() *dict.Parser {
if m.dictionary == nil {
return dict.Default
}
return m.dictionary
}
// NewAVP creates and initializes a new AVP and adds it to the Message.
// It is not safe for concurrent calls.
func (m *Message) NewAVP(code interface{}, flags uint8, vendor uint32, data datatype.Type) (*AVP, error) {
var a *AVP
switch code.(type) {
case int:
a = NewAVP(uint32(code.(int)), flags, vendor, data)
case uint32:
a = NewAVP(code.(uint32), flags, vendor, data)
case string:
dictAVP, err := m.Dictionary().FindAVPWithVendor(
m.Header.ApplicationID,
code.(string),
vendor,
)
if err != nil {
return nil, err
}
a = NewAVP(dictAVP.Code, flags, vendor, data)
}
m.AVP = append(m.AVP, a)
m.Header.MessageLength += uint32(a.Len())
return a, nil
}
// AddAVP adds the AVP to the Message. It is not safe for concurrent calls.
func (m *Message) AddAVP(a *AVP) {
m.AVP = append(m.AVP, a)
m.Header.MessageLength += uint32(a.Len())
}
// InsertAVP inserts the AVP to the Message as the first AVP. It is not
// safe for concurrent calls.
func (m *Message) InsertAVP(a *AVP) {
m.AVP = append([]*AVP{a}, m.AVP...)
m.Header.MessageLength += uint32(a.Len())
}
var writerBufferPool sync.Pool
func newWriterBuffer(min int) *bytes.Buffer {
if min > MessageBufferLength {
return bytes.NewBuffer(make([]byte, min))
}
if v := writerBufferPool.Get(); v != nil {
return v.(*bytes.Buffer)
}
return bytes.NewBuffer(make([]byte, MessageBufferLength))
}
func putWriterBuffer(b *bytes.Buffer) {
b.Reset()
if cap(b.Bytes()) == MessageBufferLength {
writerBufferPool.Put(b)
}
}
// WriteTo serializes the Message and writes into the writer.
func (m *Message) WriteTo(writer io.Writer) (int64, error) {
l := m.Len()
buf := newWriterBuffer(l)
defer putWriterBuffer(buf)
b := buf.Bytes()[0:l]
if err := m.SerializeTo(b); err != nil {
return 0, err
}
n, err := writer.Write(b)
if err != nil {
return 0, err
}
return int64(n), err
}
// Serialize returns the serialized bytes of the Message.
func (m *Message) Serialize() ([]byte, error) {
b := make([]byte, m.Len())
if err := m.SerializeTo(b); err != nil {
return nil, err
}
return b, nil
}
// SerializeTo writes the serialized bytes of the Message into b.
func (m *Message) SerializeTo(b []byte) (err error) {
m.Header.SerializeTo(b[0:HeaderLength])
offset := HeaderLength
for _, avp := range m.AVP {
if err = avp.SerializeTo(b[offset:]); err != nil {
return err
}
offset += avp.Len()
}
return nil
}
// Len returns the length of the Message in bytes.
func (m *Message) Len() int {
l := HeaderLength
for _, avp := range m.AVP {
l += avp.Len()
}
return l
}
func findFromAVP(avps []*AVP, code uint32, findMultiple bool) ([]*AVP, error) {
var avpResult []*AVP
for _, a := range avps {
if a.Code == code {
avpResult = append(avpResult, a)
if !findMultiple {
return avpResult, nil
}
}
if a.Data.Type() == GroupedAVPType {
groupedAVP := a.Data
result, err := findFromAVP(groupedAVP.(*GroupedAVP).AVP, code, findMultiple)
if err == nil {
avpResult = append(avpResult, result...)
if !findMultiple {
return avpResult, nil
}
}
}
}
if len(avpResult) == 0 {
return nil, errors.New("AVP not found")
}
return avpResult, nil
}
// Strict path search (eg: in case of groups)
// Can be also used to search AVPs as findFromAVP
func avpsWithPath(avps []*AVP, path []uint32) []*AVP {
if len(path) == 0 {
return avps
}
var avsOnPath []*AVP
for _, avp := range avps {
if avp.Code != path[0] {
continue
}
if len(path) == 1 { // Reached end
avsOnPath = append(avsOnPath, avp)
continue
}
if avp.Data.Type() != GroupedAVPType {
continue
}
avpsOnSubpath := avpsWithPath(avp.Data.(*GroupedAVP).AVP, path[1:])
if len(avpsOnSubpath) != 0 {
avsOnPath = append(avsOnPath, avpsOnSubpath...)
}
}
return avsOnPath
}
// FindAVPs searches the Message for all avps that match the search criteria.
// The code can be either the AVP code (int, uint32) or name (string).
//
// Example:
//
// avps, err := m.FindAVPs(264)
// avps, err := m.FindAVPs(avp.OriginHost)
// avps, err := m.FindAVPs("Origin-Host")
//
func (m *Message) FindAVPs(code interface{}, vendorID uint32) ([]*AVP, error) {
dictAVP, err := m.Dictionary().FindAVPWithVendor(m.Header.ApplicationID, code, vendorID)
if err != nil {
return nil, err
}
return findFromAVP(m.AVP, dictAVP.Code, true)
}
// FindAVP searches the Message for a specific AVP.
// The code can be either the AVP code (int, uint32) or name (string).
//
// Example:
//
// avp, err := m.FindAVP(264)
// avp, err := m.FindAVP(avp.OriginHost)
// avp, err := m.FindAVP("Origin-Host")
//
func (m *Message) FindAVP(code interface{}, vendorID uint32) (*AVP, error) {
dictAVP, err := m.Dictionary().FindAVPWithVendor(m.Header.ApplicationID, code, vendorID)
if err != nil {
return nil, err
}
result, err := findFromAVP(m.AVP, dictAVP.Code, false)
if err == nil {
return result[0], err
}
return nil, err
}
// FindAVPsWithPath searches the Message for AVPs on specific path.
// Used for example on group hierarchies.
// The path elements can be either AVP code (int, uint32), name (string) or combination of them.
//
// Example:
//
// avp, err := m.FindAVPsWithPath([]interface{}{264})
// avp, err := m.FindAVPsWithPath([]interface{}{avp.OriginHost})
// avp, err := m.FindAVPsWithPath([]interface{}{"Origin-Host"})
//
func (m *Message) FindAVPsWithPath(path []interface{}, vendorID uint32) ([]*AVP, error) {
pathCodes := make([]uint32, len(path))
for i, pathCode := range path {
dictAVP, err := m.Dictionary().FindAVPWithVendor(m.Header.ApplicationID, pathCode, vendorID)
if err != nil {
return nil, err
}
pathCodes[i] = dictAVP.Code
}
return avpsWithPath(m.AVP, pathCodes), nil
}
// Answer creates an answer for the current Message with an embedded
// Result-Code AVP.
func (m *Message) Answer(resultCode uint32) *Message {
nm := NewMessage(
m.Header.CommandCode,
m.Header.CommandFlags&^RequestFlag, // Reset the Request bit.
m.Header.ApplicationID,
m.Header.HopByHopID,
m.Header.EndToEndID,
m.Dictionary(),
)
nm.NewAVP(avp.ResultCode, avp.Mbit, 0, datatype.Unsigned32(resultCode))
return nm
}
func (m *Message) String() string {
var b bytes.Buffer
var typ string
if m.Header.CommandFlags&RequestFlag == RequestFlag {
typ = "Request"
} else {
typ = "Answer"
}
if dictCMD, err := m.Dictionary().FindCommand(
m.Header.ApplicationID,
m.Header.CommandCode,
); err != nil {
fmt.Fprintf(&b, "Unknown-%s\n%s\n", typ, m.Header)
} else {
fmt.Fprintf(&b, "%s-%s (%s%c)\n%s\n",
dictCMD.Name,
typ,
dictCMD.Short,
typ[0],
m.Header,
)
}
for _, a := range m.AVP {
if dictAVP, err := m.Dictionary().FindAVPWithVendor(
m.Header.ApplicationID,
a.Code,
a.VendorID,
); err != nil {
fmt.Fprintf(&b, "\tUnknown %s (%s)\n", a, err)
} else if a.Data.Type() == GroupedAVPType {
fmt.Fprintf(&b, "\t%s %s\n", dictAVP.Name, printGrouped("\t", m, a, 1))
} else {
fmt.Fprintf(&b, "\t%s %s\n", dictAVP.Name, a)
}
}
return b.String()
}
func printGrouped(prefix string, m *Message, a *AVP, indent int) string {
var b bytes.Buffer
fmt.Fprintf(&b, "{Code:%d,Flags:0x%x,Length:%d,VendorId:%d,Value:Grouped{\n",
a.Code,
a.Flags,
a.Len(),
a.VendorID,
)
for _, ga := range a.Data.(*GroupedAVP).AVP {
if dictAVP, err := m.Dictionary().FindAVPWithVendor(
m.Header.ApplicationID,
ga.Code,
ga.VendorID,
); err != nil {
if dictAVP != nil {
fmt.Fprintf(&b, "%s\t%s %s (%s),\n", prefix, dictAVP.Name, ga, err)
} else {
fmt.Fprintf(&b, "%s\tUnknown %s (%s),\n", prefix, ga, err)
}
} else {
if ga.Data.Type() == GroupedAVPType {
indent++
tabs := indentTabs(indent)
fmt.Fprintf(&b, "%s%s %s\n", tabs, dictAVP.Name, printGrouped(tabs, m, ga, indent))
} else {
fmt.Fprintf(&b, "%s\t%s %s,\n", prefix, dictAVP.Name, ga)
}
}
}
fmt.Fprintf(&b, "%s}}", prefix)
return b.String()
}
func indentTabs(n int) string {
var s string
for i := 0; i < n; i++ {
s += "\t"
}
return s
}