This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
50 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,47 @@ | ||
package ie | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
type InformationElement interface { | ||
Serialize() []byte | ||
} | ||
|
||
func ParseInformationElements(b []byte) ([]InformationElement, error) { | ||
var ies []InformationElement | ||
|
||
index := 0 | ||
|
||
for index < len(b) { | ||
if len(b[index:]) < 4 { | ||
return nil, fmt.Errorf("not enough bytes for IE header") | ||
} | ||
|
||
ieType := int(binary.BigEndian.Uint16(b[index : index+2])) | ||
ieLength := int(binary.BigEndian.Uint16(b[index+2 : index+4])) | ||
index += 4 // Move past the header | ||
|
||
if len(b[index:]) < ieLength { | ||
return nil, fmt.Errorf("not enough bytes for IE data, expected %d, got %d", ieLength, len(b[index:])) | ||
} | ||
|
||
ieValue := b[index : index+ieLength] | ||
var ie InformationElement | ||
switch ieType { | ||
case 96: | ||
ie = DeserializeRecoveryTimeStamp(ieType, ieLength, ieValue) | ||
default: | ||
return nil, fmt.Errorf("unknown IE type %d", ieType) | ||
} | ||
|
||
if ie != nil { | ||
ies = append(ies, ie) | ||
} | ||
|
||
index += ieLength | ||
} | ||
|
||
return ies, nil | ||
} |
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,41 @@ | ||
package ie | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
type NodeID struct { | ||
Type uint16 | ||
Length uint16 | ||
NodeIDType int | ||
NodeIDValue []byte | ||
} | ||
|
||
func NewNodeID(nodeIDType int, nodeIDValue []byte) NodeID { | ||
return NodeID{ | ||
Type: 60, | ||
Length: uint16(len(nodeIDValue) + 1), | ||
NodeIDType: nodeIDType, | ||
NodeIDValue: nodeIDValue, | ||
} | ||
} | ||
|
||
func (n NodeID) Serialize() []byte { | ||
buf := new(bytes.Buffer) | ||
|
||
// Octets 1 to 2: Type (60) | ||
binary.Write(buf, binary.BigEndian, uint16(n.Type)) | ||
|
||
// Octets 3 to 4: Length | ||
binary.Write(buf, binary.BigEndian, uint16(n.Length)) | ||
|
||
// Octet 5: Spare (4 bits) + Node ID Type (4 bits) | ||
spareAndType := byte(n.NodeIDType & 0x0F) // Ensure NodeIDType is only 4 bits | ||
buf.WriteByte(spareAndType) | ||
|
||
// Octets 6 to n+5: Node ID Value | ||
buf.Write(n.NodeIDValue) | ||
|
||
return buf.Bytes() | ||
} |
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
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