-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for SCCP Management Message encoding/decoding
- Loading branch information
Showing
2 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2024 Valentin D'Emmanuele. All rights reserved. | ||
// Use of this source code is governed by a MIT-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package sccp | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// MsgType is type of SCMG message. | ||
type SCMGType uint8 | ||
|
||
const ( | ||
_ SCMGType = iota | ||
SCMGTypeSSA | ||
SCMGTypeSSP | ||
SCMGTypeSST | ||
SCMGTypeSOR | ||
SCMGTypeSOG | ||
SCMGTypeSSC | ||
) | ||
|
||
// SCMG represents a SCCP Management message (SCMG). | ||
// ITU Q.713 | ||
type SCMG struct { | ||
Type SCMGType | ||
AffectedSSN uint8 | ||
AffectedPC uint16 | ||
SubsystemMultiplicityIndicator uint8 | ||
SCCPCongestionLevel uint8 | ||
} | ||
|
||
// NewSCMG creates a new SCMG. | ||
func NewSCMG(typ SCMGType, affectedSSN uint8, affectedPC uint16, subsystemMultiplicityIndicator uint8, sccpCongestionLevel uint8) *SCMG { | ||
s := &SCMG{ | ||
Type: typ, | ||
AffectedSSN: affectedSSN, | ||
AffectedPC: affectedPC, | ||
SubsystemMultiplicityIndicator: subsystemMultiplicityIndicator, | ||
SCCPCongestionLevel: sccpCongestionLevel, | ||
} | ||
|
||
return s | ||
} | ||
|
||
// MarshalBinary returns the byte sequence generated from a SCMG instance. | ||
func (s *SCMG) MarshalBinary() ([]byte, error) { | ||
b := make([]byte, s.MarshalLen()) | ||
if err := s.MarshalTo(b); err != nil { | ||
return nil, err | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
// MarshalTo puts the byte sequence in the byte array given as b. | ||
func (s *SCMG) MarshalTo(b []byte) error { | ||
l := len(b) | ||
|
||
if l < s.MarshalLen() { | ||
return io.ErrUnexpectedEOF | ||
} | ||
|
||
b[0] = uint8(s.Type) | ||
b[1] = s.AffectedSSN | ||
binary.LittleEndian.PutUint16(b[2:4], s.AffectedPC) | ||
b[4] = s.SubsystemMultiplicityIndicator | ||
if s.Type == SCMGTypeSSC { | ||
b[5] = s.SCCPCongestionLevel | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ParseSCMG decodes given byte sequence as a SCMG. | ||
func ParseSCMG(b []byte) (*SCMG, error) { | ||
s := &SCMG{} | ||
if err := s.UnmarshalBinary(b); err != nil { | ||
return nil, err | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
// UnmarshalBinary sets the values retrieved from byte sequence in a SCMG. | ||
func (s *SCMG) UnmarshalBinary(b []byte) error { | ||
l := len(b) | ||
if l < 5 { | ||
return io.ErrUnexpectedEOF | ||
} | ||
|
||
s.Type = SCMGType(b[0]) | ||
s.AffectedSSN = b[1] | ||
s.AffectedPC = binary.LittleEndian.Uint16(b[2:4]) | ||
s.SubsystemMultiplicityIndicator = b[4] | ||
|
||
if s.Type == SCMGTypeSSC { | ||
if l < 6 { | ||
return io.ErrUnexpectedEOF | ||
} | ||
s.SCCPCongestionLevel = b[5] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MarshalLen returns the serial length. | ||
func (s *SCMG) MarshalLen() int { | ||
// Table 24/Q.713 – SCMG messages | ||
l := 5 | ||
|
||
// Table 25/Q.713 – SSC | ||
if s.Type == SCMGTypeSSC { | ||
l += 1 | ||
} | ||
|
||
return l | ||
} | ||
|
||
// String returns the SCMG values in human readable format. | ||
func (s *SCMG) String() string { | ||
return fmt.Sprintf("{Type: %d, Affected SSN: %v, Affected PC: %v, Subsystem Multiplicity Indicator: %d, SCCP Congestion Level: %d}", | ||
s.Type, | ||
s.AffectedSSN, | ||
s.AffectedPC, | ||
s.SubsystemMultiplicityIndicator, | ||
s.SCCPCongestionLevel, | ||
) | ||
} | ||
|
||
// MessageType returns the Message Type in int. | ||
func (s *SCMG) MessageType() SCMGType { | ||
return s.Type | ||
} | ||
|
||
// MessageTypeName returns the Message Type in string. | ||
func (s *SCMG) MessageTypeName() string { | ||
return "SCMG" | ||
} |