From 53661cda2f8622fb9d870116980586ce681328cb Mon Sep 17 00:00:00 2001 From: Valentin D'Emmanuele Date: Mon, 25 Mar 2024 18:23:07 +0100 Subject: [PATCH] Fix Marshaling of Dialogue --- dialogue-pdu.go | 3 ++- dialogue.go | 46 ++++++++++++++++++++++++++-------------------- tcap.go | 1 + 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/dialogue-pdu.go b/dialogue-pdu.go index a080be2..446d675 100644 --- a/dialogue-pdu.go +++ b/dialogue-pdu.go @@ -702,7 +702,7 @@ func (d *DialoguePDU) ContextVersion() string { // String returns DialoguePDU in human readable string. func (d *DialoguePDU) String() string { - return fmt.Sprintf("{Type: %#x, Length: %d, ProtocolVersion: %v, ApplicationContextName: %v, Result: %v, ResultSourceDiagnostic: %v, AbortSource: %v}", + return fmt.Sprintf("{Type: %#x, Length: %d, ProtocolVersion: %v, ApplicationContextName: %v, Result: %v, ResultSourceDiagnostic: %v, AbortSource: %v, UserInformation: %v}", d.Type, d.Length, d.ProtocolVersion, @@ -710,5 +710,6 @@ func (d *DialoguePDU) String() string { d.Result, d.ResultSourceDiagnostic, d.AbortSource, + d.UserInformation, ) } diff --git a/dialogue.go b/dialogue.go index 9d99484..11c0221 100644 --- a/dialogue.go +++ b/dialogue.go @@ -24,7 +24,7 @@ type Dialogue struct { ExternalTag Tag ExternalLength uint8 ObjectIdentifier *IE - SingleAsn1Type *IE + singleAsn1Type *IE DialoguePDU *DialoguePDU Payload []byte } @@ -39,7 +39,7 @@ func NewDialogue(oid, ver uint8, pdu *DialoguePDU, payload []byte) *Dialogue { Length: 7, Value: []byte{0, 17, 134, 5, 1, oid, ver}, }, - SingleAsn1Type: &IE{ + singleAsn1Type: &IE{ Tag: NewContextSpecificConstructorTag(0), Length: uint8(pdu.MarshalLen()), }, @@ -78,14 +78,16 @@ func (d *Dialogue) MarshalTo(b []byte) error { offset += field.MarshalLen() } - if field := d.SingleAsn1Type; field != nil { - if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil { - return err + if field := d.singleAsn1Type; field != nil { + if field := d.DialoguePDU; field != nil { + d.singleAsn1Type.Value = make([]byte, d.DialoguePDU.MarshalLen()) + + if err := d.DialoguePDU.MarshalTo(d.singleAsn1Type.Value); err != nil { + return err + } } - offset += field.MarshalLen() - } - if field := d.DialoguePDU; field != nil { + d.singleAsn1Type.SetLength() if err := field.MarshalTo(b[offset : offset+field.MarshalLen()]); err != nil { return err } @@ -93,6 +95,7 @@ func (d *Dialogue) MarshalTo(b []byte) error { } copy(b[offset:], d.Payload) + return nil } @@ -125,13 +128,13 @@ func (d *Dialogue) UnmarshalBinary(b []byte) error { } offset += d.ObjectIdentifier.MarshalLen() - d.SingleAsn1Type, err = ParseIE(b[offset:]) + d.singleAsn1Type, err = ParseIE(b[offset:]) if err != nil { return err } - offset += d.SingleAsn1Type.MarshalLen() + offset += d.singleAsn1Type.MarshalLen() - d.DialoguePDU, err = ParseDialoguePDU(d.SingleAsn1Type.Value) + d.DialoguePDU, err = ParseDialoguePDU(d.singleAsn1Type.Value) if err != nil { return err } @@ -155,7 +158,7 @@ func (d *Dialogue) SetValsFrom(berParsed *IE) error { case 0x06: d.ObjectIdentifier = iex case 0xa0: - d.SingleAsn1Type = iex + d.singleAsn1Type = iex dpdu = iex.IE[0] } } @@ -190,32 +193,35 @@ func (d *Dialogue) MarshalLen() int { if field := d.ObjectIdentifier; field != nil { l += field.MarshalLen() } - if field := d.SingleAsn1Type; field != nil { - l += field.MarshalLen() - } if field := d.DialoguePDU; field != nil { - l += field.MarshalLen() + l += field.MarshalLen() + 2 // 2 = singleAsn1Type IE Header } - l += len(d.Payload) - return l + return l + len(d.Payload) } // SetLength sets the length in Length field. func (d *Dialogue) SetLength() { + if field := d.ObjectIdentifier; field != nil { + d.ObjectIdentifier.SetLength() + } + if field := d.DialoguePDU; field != 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}", + 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.singleAsn1Type, d.DialoguePDU, d.Payload, ) diff --git a/tcap.go b/tcap.go index b11b3d3..371d3d3 100644 --- a/tcap.go +++ b/tcap.go @@ -114,6 +114,7 @@ func Parse(b []byte) (*TCAP, error) { if err := t.UnmarshalBinary(b); err != nil { return nil, err } + return t, nil }