-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdialogue.go
255 lines (220 loc) · 5.66 KB
/
dialogue.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
// Copyright 2019-2024 go-tcap authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package tcap
import (
"fmt"
"io"
)
// Dialogue OID: Dialogue-As-ID and Unidialogue-As-Id.
const (
DialogueAsID uint8 = iota + 1
UnidialogueAsID
)
// Dialogue represents a Dialogue Portion of TCAP.
type Dialogue struct {
Tag Tag
Length uint8
ExternalTag Tag
ExternalLength uint8
ObjectIdentifier *IE
SingleAsn1Type *IE
DialoguePDU *DialoguePDU
Payload []byte
}
// NewDialogue creates a new Dialogue with the DialoguePDU given.
func NewDialogue(oid, ver uint8, pdu *DialoguePDU, payload []byte) *Dialogue {
d := &Dialogue{
Tag: NewApplicationWideConstructorTag(11),
ExternalTag: NewUniversalConstructorTag(8),
ObjectIdentifier: &IE{
Tag: NewUniversalPrimitiveTag(6),
Length: 7,
Value: []byte{0, 17, 134, 5, 1, oid, ver},
},
SingleAsn1Type: &IE{
Tag: NewContextSpecificConstructorTag(0),
Length: uint8(pdu.MarshalLen()),
},
DialoguePDU: pdu,
Payload: payload,
}
d.SetLength()
return d
}
// MarshalBinary returns the byte sequence generated from a Dialogue.
func (d *Dialogue) MarshalBinary() ([]byte, error) {
b := make([]byte, d.MarshalLen())
if err := d.MarshalTo(b); err != nil {
return nil, fmt.Errorf("failed to marshal Dialogue: %w", err)
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (d *Dialogue) MarshalTo(b []byte) error {
if len(b) < 4 {
return io.ErrUnexpectedEOF
}
b[0] = uint8(d.Tag)
b[1] = d.Length
b[2] = uint8(d.ExternalTag)
b[3] = d.ExternalLength
var offset = 4
if field := d.ObjectIdentifier; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if d.SingleAsn1Type == nil {
copy(b[offset:], d.Payload)
return nil
}
if field := d.DialoguePDU; field != nil {
d.SingleAsn1Type.Value = make([]byte, field.MarshalLen())
if err := field.MarshalTo(d.SingleAsn1Type.Value); err != nil {
return err
}
}
d.SingleAsn1Type.SetLength()
if err := d.SingleAsn1Type.MarshalTo(b[offset : offset+d.SingleAsn1Type.MarshalLen()]); err != nil {
return err
}
offset += d.SingleAsn1Type.MarshalLen()
copy(b[offset:], d.Payload)
return nil
}
// ParseDialogue parses given byte sequence as an Dialogue.
func ParseDialogue(b []byte) (*Dialogue, error) {
d := &Dialogue{}
if err := d.UnmarshalBinary(b); err != nil {
return nil, err
}
return d, nil
}
// UnmarshalBinary sets the values retrieved from byte sequence in an Dialogue.
func (d *Dialogue) UnmarshalBinary(b []byte) error {
l := len(b)
if l < 5 {
return io.ErrUnexpectedEOF
}
d.Tag = Tag(b[0])
d.Length = b[1]
d.ExternalTag = Tag(b[2])
d.ExternalLength = b[3]
var err error
var offset = 4
d.ObjectIdentifier, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += d.ObjectIdentifier.MarshalLen()
d.SingleAsn1Type, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += d.SingleAsn1Type.MarshalLen()
d.DialoguePDU, err = ParseDialoguePDU(d.SingleAsn1Type.Value)
if err != nil {
return err
}
d.Payload = b[offset:]
return nil
}
// SetValsFrom sets the values from IE parsed by ParseBER.
func (d *Dialogue) SetValsFrom(berParsed *IE) error {
d.Tag = berParsed.Tag
d.Length = berParsed.Length
for _, ie := range berParsed.IE {
var dpdu *IE
if ie.Tag == 0x28 {
d.ExternalTag = ie.Tag
d.ExternalLength = ie.Length
for _, iex := range ie.IE {
switch iex.Tag {
case 0x06:
d.ObjectIdentifier = iex
case 0xa0:
d.SingleAsn1Type = iex
dpdu = iex.IE[0]
}
}
}
switch dpdu.Tag.Code() {
case AARQ, AARE, ABRT:
d.DialoguePDU = &DialoguePDU{
Type: dpdu.Tag,
Length: dpdu.Length,
}
}
for _, iex := range dpdu.IE {
switch iex.Tag {
case 0x80:
d.DialoguePDU.ProtocolVersion = iex
case 0xa1:
d.DialoguePDU.ApplicationContextName = iex
case 0xa2:
d.DialoguePDU.Result = iex
case 0xa3:
d.DialoguePDU.ResultSourceDiagnostic = iex
}
}
}
return nil
}
// MarshalLen returns the serial length of Dialogue.
func (d *Dialogue) MarshalLen() int {
l := 4
if field := d.ObjectIdentifier; field != nil {
l += field.MarshalLen()
}
if field := d.DialoguePDU; field != nil {
l += field.MarshalLen() + 2 // 2 = singleAsn1Type IE Header
}
return l + len(d.Payload)
}
// SetLength sets the length in Length field.
func (d *Dialogue) SetLength() {
if d.ObjectIdentifier != nil {
d.ObjectIdentifier.SetLength()
}
if d.DialoguePDU != nil {
d.DialoguePDU.SetLength()
}
d.Length = uint8(d.MarshalLen() - 2)
d.ExternalLength = uint8(d.MarshalLen() - 4)
}
// String returns the SCCP common header values in human readable format.
func (d *Dialogue) String() string {
return fmt.Sprintf("{Tag: %#x, Length: %d, ExternalTag: %x, ExternalLength: %d, ObjectIdentifier: %v, SingleAsn1Type: %v, DialoguePDU: %v, Payload: %x}",
d.Tag,
d.Length,
d.ExternalTag,
d.ExternalLength,
d.ObjectIdentifier,
d.SingleAsn1Type,
d.DialoguePDU,
d.Payload,
)
}
// Version returns Protocol Version in string.
func (d *Dialogue) Version() string {
if d.DialoguePDU == nil {
return ""
}
return d.DialoguePDU.Version()
}
// Context returns the Context part of ApplicationContextName in string.
func (d *Dialogue) Context() string {
if d.DialoguePDU == nil {
return ""
}
return d.DialoguePDU.Context()
}
// ContextVersion returns the Version part of ApplicationContextName in string.
func (d *Dialogue) ContextVersion() string {
if d.DialoguePDU == nil {
return ""
}
return d.DialoguePDU.ContextVersion()
}