-
Notifications
You must be signed in to change notification settings - Fork 24
/
component.go
707 lines (637 loc) · 15.3 KB
/
component.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// 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"
)
// Component Type definitions.
const (
Invoke int = iota + 1
ReturnResultLast
ReturnError
Reject
_
_
ReturnResultNotLast
)
// Problem Type definitions.
const (
GeneralProblem int = iota
InvokeProblem
ReturnResultProblem
ReturnErrorProblem
)
// General Problem Code definitions.
const (
UnrecognizedComponent uint8 = iota
MistypedComponent
BadlyStructuredComponent
)
// Invoke Problem Code definitions.
const (
InvokeProblemDuplicateInvokeID uint8 = iota
InvokeProblemUnrecognizedOperation
InvokeProblemMistypedParameter
InvokeProblemResourceLimitation
InvokeProblemInitiatingRelease
InvokeProblemUnrecognizedLinkedID
InvokeProblemLinkedResponseUnexpected
InvokeProblemUnexpectedLinkedOperation
)
// ReturnResult Problem Code definitions.
const (
ResultProblemUnrecognizedInvokeID uint8 = iota
ResultProblemReturnResultUnexpected
ResultProblemMistypedParameter
)
// ReturnError Problem Code definitions.
const (
ErrorProblemUnrecognizedInvokeID uint8 = iota
ErrorProblemReturnErrorUnexpected
ErrorProblemUnrecognizedError
ErrorProblemUnexpectedError
ErrorProblemMistypedParameter
)
// Components represents a TCAP Components(Header).
//
// This is a TCAP Components' Header part. Contents are in Component field.
type Components struct {
Tag Tag
Length uint8
Component []*Component
}
// Component represents a TCAP Component.
type Component struct {
Type Tag
Length uint8
InvokeID *IE
LinkedID *IE
ResultRetres *IE
SequenceTag *IE
OperationCode *IE
ErrorCode *IE
ProblemCode *IE
Parameter *IE
}
// NewComponents creates a new Components.
func NewComponents(comps ...*Component) *Components {
c := &Components{
Tag: NewApplicationWideConstructorTag(12),
Component: comps,
}
c.SetLength()
return c
}
// NewInvoke returns a new single Invoke Component.
func NewInvoke(invID, lkID, opCode int, isLocal bool, param []byte) *Component {
c := &Component{
Type: NewContextSpecificConstructorTag(Invoke),
InvokeID: &IE{
Tag: NewUniversalPrimitiveTag(2),
Length: 1,
Value: []byte{uint8(invID)},
},
OperationCode: NewOperationCode(opCode, isLocal),
}
if lkID > 0 {
c.LinkedID = &IE{
Tag: NewContextSpecificPrimitiveTag(0),
Length: 1,
Value: []byte{uint8(lkID)},
}
}
if param != nil {
if err := c.setParameterFromBytes(param); err != nil {
logf("failed to build Parameter: %v", err)
}
}
c.SetLength()
return c
}
// NewReturnResult returns a new single ReturnResultLast or ReturnResultNotLast Component.
func NewReturnResult(invID, opCode int, isLocal, isLast bool, param []byte) *Component {
tag := ReturnResultNotLast
if isLast {
tag = ReturnResultLast
}
c := &Component{
Type: NewContextSpecificConstructorTag(tag),
ResultRetres: &IE{
Tag: NewUniversalConstructorTag(0x10),
},
InvokeID: &IE{
Tag: NewUniversalPrimitiveTag(2),
Length: 1,
Value: []byte{uint8(invID)},
},
OperationCode: NewOperationCode(opCode, isLocal),
}
if param != nil {
if err := c.setParameterFromBytes(param); err != nil {
logf("failed to build Parameter: %v", err)
}
}
c.SetLength()
return c
}
// NewReturnError returns a new single ReturnError Component.
func NewReturnError(invID, errCode int, isLocal bool, param []byte) *Component {
c := &Component{
Type: NewContextSpecificConstructorTag(ReturnError),
InvokeID: &IE{
Tag: NewUniversalPrimitiveTag(2),
Length: 1,
Value: []byte{uint8(invID)},
},
ErrorCode: NewErrorCode(errCode, isLocal),
}
if param != nil {
if err := c.setParameterFromBytes(param); err != nil {
logf("failed to build Parameter: %v", err)
}
}
c.SetLength()
return c
}
// NewReject returns a new single Reject Component.
func NewReject(invID, problemType int, problemCode uint8, param []byte) *Component {
c := &Component{
Type: NewContextSpecificConstructorTag(Invoke),
InvokeID: &IE{
Tag: NewUniversalPrimitiveTag(2),
Length: 1,
Value: []byte{uint8(invID)},
},
ProblemCode: &IE{
Tag: NewContextSpecificPrimitiveTag(problemType),
Length: 1,
Value: []byte{problemCode},
},
}
if param != nil {
if err := c.setParameterFromBytes(param); err != nil {
logf("failed to build Parameter: %v", err)
}
}
c.SetLength()
return c
}
// NewOperationCode returns a Operation Code.
func NewOperationCode(code int, isLocal bool) *IE {
var tag = 6
if isLocal {
tag = 2
}
return &IE{
Tag: NewUniversalPrimitiveTag(tag),
Length: 1,
Value: []byte{uint8(code)},
}
}
// NewErrorCode returns a Error Code.
func NewErrorCode(code int, isLocal bool) *IE {
return NewOperationCode(code, isLocal)
}
// MarshalBinary returns the byte sequence generated from a Components instance.
func (c *Components) MarshalBinary() ([]byte, error) {
b := make([]byte, c.MarshalLen())
if err := c.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (c *Components) MarshalTo(b []byte) error {
b[0] = uint8(c.Tag)
b[1] = c.Length
cursor := 2
for _, comp := range c.Component {
compLen := comp.MarshalLen()
if err := comp.MarshalTo(b[cursor : cursor+compLen]); err != nil {
return err
}
cursor += compLen
}
return nil
}
// MarshalBinary returns the byte sequence generated from a Components instance.
func (c *Component) MarshalBinary() ([]byte, error) {
b := make([]byte, c.MarshalLen())
if err := c.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (c *Component) MarshalTo(b []byte) error {
b[0] = uint8(c.Type)
b[1] = c.Length
var offset = 2
if field := c.InvokeID; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
switch c.Type.Code() {
case Invoke:
if field := c.LinkedID; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if field := c.OperationCode; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if field := c.Parameter; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
}
case ReturnResultLast, ReturnResultNotLast:
if field := c.ResultRetres; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if field := c.OperationCode; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if field := c.Parameter; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
}
case ReturnError:
if field := c.ErrorCode; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
offset += field.MarshalLen()
}
if field := c.Parameter; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
}
case Reject:
if field := c.ProblemCode; field != nil {
if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil {
return err
}
}
}
return nil
}
// ParseComponents parses given byte sequence as an Components.
func ParseComponents(b []byte) (*Components, error) {
c := &Components{}
if err := c.UnmarshalBinary(b); err != nil {
return nil, err
}
return c, nil
}
// UnmarshalBinary sets the values retrieved from byte sequence in an Components.
func (c *Components) UnmarshalBinary(b []byte) error {
if len(b) < 2 {
return io.ErrUnexpectedEOF
}
c.Tag = Tag(b[0])
c.Length = b[1]
var offset = 2
for {
if len(b) < 2 {
break
}
comp, err := ParseComponent(b[offset:])
if err != nil {
return err
}
c.Component = append(c.Component, comp)
if len(b[offset:]) == int(comp.Length)+2 {
break
}
b = b[offset+comp.MarshalLen()-2:]
}
return nil
}
// ParseComponent parses given byte sequence as an Component.
func ParseComponent(b []byte) (*Component, error) {
c := &Component{}
if err := c.UnmarshalBinary(b); err != nil {
return nil, err
}
return c, nil
}
// UnmarshalBinary sets the values retrieved from byte sequence in an Component.
func (c *Component) UnmarshalBinary(b []byte) error {
if len(b) < 2 {
return io.ErrUnexpectedEOF
}
c.Type = Tag(b[0])
c.Length = b[1]
var err error
var offset = 2
c.InvokeID, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += c.InvokeID.MarshalLen()
switch c.Type.Code() {
case Invoke:
/* TODO: Implement LinkedID Parser.
c.LinkedID, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += c.LinkedID.MarshalLen()
*/
c.OperationCode, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += c.OperationCode.MarshalLen()
if offset >= len(b) {
return nil
}
c.Parameter, err = ParseIERecursive(b[offset:])
if err != nil {
return err
}
case ReturnResultLast, ReturnResultNotLast:
c.ResultRetres, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset = 0
b = c.ResultRetres.Value[offset:]
c.OperationCode, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += c.OperationCode.MarshalLen()
if offset >= len(b) {
return nil
}
c.Parameter, err = ParseIERecursive(b[offset:])
if err != nil {
return err
}
case ReturnError:
c.ErrorCode, err = ParseIE(b[offset:])
if err != nil {
return err
}
offset += c.ErrorCode.MarshalLen()
if offset >= len(b) {
return nil
}
c.Parameter, err = ParseIERecursive(b[offset:])
if err != nil {
return err
}
case Reject:
c.ProblemCode, err = ParseIE(b[offset:])
if err != nil {
return err
}
}
return nil
}
// setParameterFromBytes sets the Parameter field from given bytes.
//
// It sets the value as it is if the given bytes cannot be parsed as (a set of) IE.
func (c *Component) setParameterFromBytes(b []byte) error {
if b == nil {
return io.ErrUnexpectedEOF
}
ies, err := ParseMultiIEs(b)
if err != nil {
logf("failed to parse given bytes, building it anyway: %v", err)
c.Parameter = &IE{
// TODO: tag should not be determined here.
Tag: NewUniversalConstructorTag(0x10),
Value: b,
}
return nil
}
c.Parameter = &IE{
// TODO: tag should not be determined here.
Tag: NewUniversalConstructorTag(0x10),
Value: b,
IE: ies,
}
return nil
}
// SetValsFrom sets the values from IE parsed by ParseBER.
func (c *Components) SetValsFrom(berParsed *IE) error {
c.Tag = berParsed.Tag
c.Length = berParsed.Length
for _, ie := range berParsed.IE {
comp := &Component{
Type: ie.Tag,
Length: ie.Length,
}
switch ie.Tag {
case 0xa1: // Invoke
for i, iex := range ie.IE {
switch iex.Tag {
case 0x02:
if i == 0 {
comp.InvokeID = iex
} else {
comp.OperationCode = iex
}
case 0x30:
comp.Parameter = iex
}
}
case 0xa2, 0xa7: // ReturnResult(Not)Last
for i, iex := range ie.IE {
switch iex.Tag {
case 0x02:
if i == 0 {
comp.InvokeID = iex
}
case 0x30:
comp.ResultRetres = iex
for _, riex := range iex.IE {
switch riex.Tag {
case 0x02:
comp.OperationCode = riex
case 0x30:
comp.Parameter = riex
}
}
}
}
case 0xa3: // ReturnError
for i, iex := range ie.IE {
switch iex.Tag {
case 0x02:
if i == 0 {
comp.InvokeID = iex
} else {
comp.ErrorCode = iex
}
case 0x30:
comp.Parameter = iex
}
}
}
c.Component = append(c.Component, comp)
}
return nil
}
// MarshalLen returns the serial length of Components.
func (c *Components) MarshalLen() int {
var l = 2
for _, comp := range c.Component {
l += comp.MarshalLen()
}
return l
}
// MarshalLen returns the serial length of Component.
func (c *Component) MarshalLen() int {
var l = 2 + c.InvokeID.MarshalLen()
switch c.Type.Code() {
case Invoke:
if field := c.LinkedID; field != nil {
l += field.MarshalLen()
}
if field := c.OperationCode; field != nil {
l += field.MarshalLen()
}
if field := c.Parameter; field != nil {
l += field.MarshalLen()
}
case ReturnResultLast, ReturnResultNotLast:
if field := c.ResultRetres; field != nil {
l += field.MarshalLen()
}
if field := c.OperationCode; field != nil {
l += field.MarshalLen()
}
if field := c.Parameter; field != nil {
l += field.MarshalLen()
}
case ReturnError:
if field := c.ErrorCode; field != nil {
l += field.MarshalLen()
}
if field := c.Parameter; field != nil {
l += field.MarshalLen()
}
case Reject:
if field := c.ProblemCode; field != nil {
l += field.MarshalLen()
}
}
return l
}
// SetLength sets the length in Length field.
func (c *Components) SetLength() {
c.Length = 0
for _, comp := range c.Component {
comp.SetLength()
c.Length += uint8(comp.MarshalLen())
}
}
// SetLength sets the length in Length field.
func (c *Component) SetLength() {
l := 0
if field := c.InvokeID; field != nil {
field.SetLength()
}
if field := c.LinkedID; field != nil {
field.SetLength()
}
if field := c.OperationCode; field != nil {
field.SetLength()
l += c.OperationCode.MarshalLen()
}
if field := c.ErrorCode; field != nil {
field.SetLength()
l += c.ErrorCode.MarshalLen()
}
if field := c.Parameter; field != nil {
field.SetLength()
l += c.Parameter.MarshalLen()
}
if field := c.ProblemCode; field != nil {
field.SetLength()
l += c.ProblemCode.MarshalLen()
}
if field := c.SequenceTag; field != nil {
field.SetLength()
l += c.SequenceTag.MarshalLen()
}
if field := c.ResultRetres; field != nil {
field.Length = uint8(l)
}
c.Length = uint8(c.MarshalLen() - 2)
}
// ComponentTypeString returns the Component Type in string.
func (c *Component) ComponentTypeString() string {
switch c.Type.Code() {
case Invoke:
return "invoke"
case ReturnResultLast:
return "returnResultLast"
case ReturnError:
return "returnError"
case Reject:
return "reject"
case ReturnResultNotLast:
return "returnResultNotLast"
}
return ""
}
// InvID returns the InvID in string.
func (c *Component) InvID() uint8 {
if c.InvokeID != nil {
return c.InvokeID.Value[0]
}
return 0
}
// OpCode returns the OpCode in string.
func (c *Component) OpCode() uint8 {
if c.Type.Code() == ReturnError {
return c.ErrorCode.Value[0]
} else if c.Type.Code() != Reject {
return c.OperationCode.Value[0]
}
return 0
}
// String returns Components in human readable string.
func (c *Components) String() string {
return fmt.Sprintf("{Tag: %#x, Length: %d, Component: %v}",
c.Tag,
c.Length,
c.Component,
)
}
// String returns Component in human readable string.
func (c *Component) String() string {
return fmt.Sprintf("{Type: %#x, Length: %d, ResultRetres: %v, InvokeID: %v, LinkedID: %v, OperationCode: %v, ErrorCode: %v, ProblemCode: %v, Parameter: %v}",
c.Type,
c.Length,
c.ResultRetres,
c.InvokeID,
c.LinkedID,
c.OperationCode,
c.ErrorCode,
c.ProblemCode,
c.Parameter,
)
}