Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bio-rd
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
danet
bio-rd
Commits
b718bac8
Commit
b718bac8
authored
5 years ago
by
Marcus Weiner
Browse files
Options
Downloads
Patches
Plain Diff
Refactor metric field
parent
96896e20
No related branches found
No related tags found
1 merge request
!2
Packet/ospfv3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
protocols/ospf/packetv3/lsa.go
+36
-33
36 additions, 33 deletions
protocols/ospf/packetv3/lsa.go
with
36 additions
and
33 deletions
protocols/ospf/packetv3/lsa.go
+
36
−
33
View file @
b718bac8
...
@@ -159,21 +159,20 @@ func (u UnknownLSA) Serialize(buf *bytes.Buffer) {
...
@@ -159,21 +159,20 @@ func (u UnknownLSA) Serialize(buf *bytes.Buffer) {
buf
.
Write
(
u
)
buf
.
Write
(
u
)
}
}
// InterfaceMetric is the metric of a link
// helper for deserializing 24-bit metric values
// This is supposed to be 24-bit long
type
interfaceMetric
struct
{
type
InterfaceMetric
struct
{
High
uint8
High
uint8
Low
uint16
Low
uint16
}
}
// Value returns the numeric value of this metric field
// Value returns the numeric value of this metric field
func
(
m
I
nterfaceMetric
)
Value
()
uint32
{
func
(
m
i
nterfaceMetric
)
Value
()
uint32
{
return
uint32
(
m
.
High
<<
16
)
+
uint32
(
m
.
Low
)
return
uint32
(
m
.
High
)
<<
16
+
uint32
(
m
.
Low
)
}
}
func
(
x
InterfaceMetric
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
func
serializeMetric
(
buf
*
bytes
.
Buffer
,
val
uint32
)
{
buf
.
WriteByte
(
x
.
High
)
metricBytes
:=
convert
.
Uint32Byte
(
val
)
buf
.
Write
(
convert
.
Uint16Byte
(
x
.
Low
)
)
buf
.
Write
(
metricBytes
[
1
:
4
]
)
}
}
type
AreaLinkDescriptionType
uint8
type
AreaLinkDescriptionType
uint8
...
@@ -188,7 +187,7 @@ const (
...
@@ -188,7 +187,7 @@ const (
type
AreaLinkDescription
struct
{
type
AreaLinkDescription
struct
{
Type
AreaLinkDescriptionType
Type
AreaLinkDescriptionType
Metric
InterfaceMetric
Metric
uint32
// max: 24 bit
InterfaceID
ID
InterfaceID
ID
NeighborInterfaceID
ID
NeighborInterfaceID
ID
NeighborRouterID
ID
NeighborRouterID
ID
...
@@ -196,7 +195,7 @@ type AreaLinkDescription struct {
...
@@ -196,7 +195,7 @@ type AreaLinkDescription struct {
func
(
x
*
AreaLinkDescription
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
func
(
x
*
AreaLinkDescription
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
buf
.
WriteByte
(
uint8
(
x
.
Type
))
buf
.
WriteByte
(
uint8
(
x
.
Type
))
x
.
Metric
.
Serialize
(
buf
)
serializeMetric
(
buf
,
x
.
Metric
)
x
.
InterfaceID
.
Serialize
(
buf
)
x
.
InterfaceID
.
Serialize
(
buf
)
x
.
NeighborInterfaceID
.
Serialize
(
buf
)
x
.
NeighborInterfaceID
.
Serialize
(
buf
)
x
.
NeighborRouterID
.
Serialize
(
buf
)
x
.
NeighborRouterID
.
Serialize
(
buf
)
...
@@ -208,10 +207,11 @@ func DeserializeAreaLinkDescription(buf *bytes.Buffer) (AreaLinkDescription, int
...
@@ -208,10 +207,11 @@ func DeserializeAreaLinkDescription(buf *bytes.Buffer) (AreaLinkDescription, int
var
readBytes
int
var
readBytes
int
var
err
error
var
err
error
var
fields
[]
interface
{}
var
fields
[]
interface
{}
var
metric
interfaceMetric
fields
=
[]
interface
{}{
fields
=
[]
interface
{}{
&
pdu
.
Type
,
&
pdu
.
Type
,
&
pdu
.
M
etric
,
&
m
etric
,
&
pdu
.
InterfaceID
,
&
pdu
.
InterfaceID
,
&
pdu
.
NeighborInterfaceID
,
&
pdu
.
NeighborInterfaceID
,
&
pdu
.
NeighborRouterID
,
&
pdu
.
NeighborRouterID
,
...
@@ -223,6 +223,8 @@ func DeserializeAreaLinkDescription(buf *bytes.Buffer) (AreaLinkDescription, int
...
@@ -223,6 +223,8 @@ func DeserializeAreaLinkDescription(buf *bytes.Buffer) (AreaLinkDescription, int
}
}
readBytes
+=
16
readBytes
+=
16
pdu
.
Metric
=
metric
.
Value
()
return
pdu
,
readBytes
,
nil
return
pdu
,
readBytes
,
nil
}
}
...
@@ -342,47 +344,42 @@ func DeserializeNetworkLSA(buf *bytes.Buffer, bodyLength uint16) (*NetworkLSA, i
...
@@ -342,47 +344,42 @@ func DeserializeNetworkLSA(buf *bytes.Buffer, bodyLength uint16) (*NetworkLSA, i
}
}
type
InterAreaPrefixLSA
struct
{
type
InterAreaPrefixLSA
struct
{
Metric
InterfaceMetric
Metric
uint32
// max: 24 bit
Prefix
LSAPrefix
Prefix
LSAPrefix
}
}
func
(
x
*
InterAreaPrefixLSA
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
func
(
x
*
InterAreaPrefixLSA
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
buf
.
WriteByte
(
0
)
//
1 byte r
eserved
buf
.
WriteByte
(
0
)
//
R
eserved
x
.
Metric
.
Serialize
(
buf
)
serializeMetric
(
buf
,
x
.
Metric
)
x
.
Prefix
.
Serialize
(
buf
)
x
.
Prefix
.
Serialize
(
buf
)
}
}
func
DeserializeInterAreaPrefixLSA
(
buf
*
bytes
.
Buffer
)
(
*
InterAreaPrefixLSA
,
int
,
error
)
{
func
DeserializeInterAreaPrefixLSA
(
buf
*
bytes
.
Buffer
)
(
*
InterAreaPrefixLSA
,
int
,
error
)
{
pdu
:=
&
InterAreaPrefixLSA
{}
pdu
:=
&
InterAreaPrefixLSA
{}
var
readBytes
int
var
readBytes
int
var
err
error
var
fields
[]
interface
{}
fields
=
[]
interface
{}{
// decode metric
new
(
uint8
),
// 1 byte reserved
_
,
_
=
buf
.
ReadByte
()
// skip reserved byte
&
pdu
.
Metric
,
var
metric
interfaceMetric
if
err
:=
binary
.
Read
(
buf
,
binary
.
BigEndian
,
&
metric
);
err
!=
nil
{
return
nil
,
readBytes
,
errors
.
Wrap
(
err
,
"failed to read metric"
)
}
}
readBytes
+=
3
err
=
decode
.
Decode
(
buf
,
fields
)
pdu
.
Metric
=
metric
.
Value
()
if
err
!=
nil
{
return
nil
,
readBytes
,
fmt
.
Errorf
(
"Unable to decode fields: %v"
,
err
)
}
readBytes
+=
4
pfx
,
n
,
err
:=
DeserializeLSAPrefix
(
buf
)
pfx
,
n
,
err
:=
DeserializeLSAPrefix
(
buf
)
readBytes
+=
n
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
readBytes
,
errors
.
Wrap
(
err
,
"unable to decode prefix"
)
return
nil
,
readBytes
,
errors
.
Wrap
(
err
,
"unable to decode prefix"
)
}
}
pdu
.
Prefix
=
pfx
pdu
.
Prefix
=
pfx
readBytes
+=
n
return
pdu
,
readBytes
,
nil
return
pdu
,
readBytes
,
nil
}
}
type
InterAreaRouterLSA
struct
{
type
InterAreaRouterLSA
struct
{
Options
RouterOptions
Options
RouterOptions
Metric
InterfaceMetric
Metric
uint32
// max: 24 bit
DestinationRouterID
ID
DestinationRouterID
ID
}
}
...
@@ -390,7 +387,7 @@ func (x *InterAreaRouterLSA) Serialize(buf *bytes.Buffer) {
...
@@ -390,7 +387,7 @@ func (x *InterAreaRouterLSA) Serialize(buf *bytes.Buffer) {
buf
.
WriteByte
(
0
)
// 1 byte reserved
buf
.
WriteByte
(
0
)
// 1 byte reserved
x
.
Options
.
Serialize
(
buf
)
x
.
Options
.
Serialize
(
buf
)
buf
.
WriteByte
(
0
)
// 1 byte reserved
buf
.
WriteByte
(
0
)
// 1 byte reserved
x
.
Metric
.
Serialize
(
buf
)
serializeMetric
(
buf
,
x
.
Metric
)
x
.
DestinationRouterID
.
Serialize
(
buf
)
x
.
DestinationRouterID
.
Serialize
(
buf
)
}
}
...
@@ -399,13 +396,14 @@ func DeserializeInterAreaRouterLSA(buf *bytes.Buffer) (*InterAreaRouterLSA, int,
...
@@ -399,13 +396,14 @@ func DeserializeInterAreaRouterLSA(buf *bytes.Buffer) (*InterAreaRouterLSA, int,
var
readBytes
int
var
readBytes
int
var
err
error
var
err
error
var
metric
interfaceMetric
var
fields
[]
interface
{}
var
fields
[]
interface
{}
fields
=
[]
interface
{}{
fields
=
[]
interface
{}{
new
(
uint8
),
// 1 byte reserved
new
(
uint8
),
// 1 byte reserved
&
pdu
.
Options
,
&
pdu
.
Options
,
new
(
uint8
),
// 1 byte reserved
new
(
uint8
),
// 1 byte reserved
&
pdu
.
M
etric
,
&
m
etric
,
&
pdu
.
DestinationRouterID
,
&
pdu
.
DestinationRouterID
,
}
}
...
@@ -415,6 +413,8 @@ func DeserializeInterAreaRouterLSA(buf *bytes.Buffer) (*InterAreaRouterLSA, int,
...
@@ -415,6 +413,8 @@ func DeserializeInterAreaRouterLSA(buf *bytes.Buffer) (*InterAreaRouterLSA, int,
}
}
readBytes
+=
12
readBytes
+=
12
pdu
.
Metric
=
metric
.
Value
()
return
pdu
,
readBytes
,
nil
return
pdu
,
readBytes
,
nil
}
}
...
@@ -427,7 +427,7 @@ const (
...
@@ -427,7 +427,7 @@ const (
type
ASExternalLSA
struct
{
type
ASExternalLSA
struct
{
Flags
uint8
Flags
uint8
Metric
InterfaceMetric
Metric
uint32
// max: 24 bit
Prefix
LSAPrefix
Prefix
LSAPrefix
ForwardingAddress
net
.
IP
// optional
ForwardingAddress
net
.
IP
// optional
...
@@ -449,7 +449,7 @@ func (a *ASExternalLSA) FlagT() bool {
...
@@ -449,7 +449,7 @@ func (a *ASExternalLSA) FlagT() bool {
func
(
x
*
ASExternalLSA
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
func
(
x
*
ASExternalLSA
)
Serialize
(
buf
*
bytes
.
Buffer
)
{
buf
.
WriteByte
(
x
.
Flags
)
buf
.
WriteByte
(
x
.
Flags
)
x
.
Metric
.
Serialize
(
buf
)
serializeMetric
(
buf
,
x
.
Metric
)
x
.
Prefix
.
Serialize
(
buf
)
x
.
Prefix
.
Serialize
(
buf
)
if
x
.
FlagF
()
{
if
x
.
FlagF
()
{
serializeIPv6
(
x
.
ForwardingAddress
,
buf
)
serializeIPv6
(
x
.
ForwardingAddress
,
buf
)
...
@@ -468,10 +468,11 @@ func DeserializeASExternalLSA(buf *bytes.Buffer) (*ASExternalLSA, int, error) {
...
@@ -468,10 +468,11 @@ func DeserializeASExternalLSA(buf *bytes.Buffer) (*ASExternalLSA, int, error) {
var
readBytes
int
var
readBytes
int
var
err
error
var
err
error
var
fields
[]
interface
{}
var
fields
[]
interface
{}
var
metric
interfaceMetric
fields
=
[]
interface
{}{
fields
=
[]
interface
{}{
&
pdu
.
Flags
,
&
pdu
.
Flags
,
&
pdu
.
M
etric
,
&
m
etric
,
}
}
err
=
decode
.
Decode
(
buf
,
fields
)
err
=
decode
.
Decode
(
buf
,
fields
)
...
@@ -480,6 +481,8 @@ func DeserializeASExternalLSA(buf *bytes.Buffer) (*ASExternalLSA, int, error) {
...
@@ -480,6 +481,8 @@ func DeserializeASExternalLSA(buf *bytes.Buffer) (*ASExternalLSA, int, error) {
}
}
readBytes
+=
4
readBytes
+=
4
pdu
.
Metric
=
metric
.
Value
()
pfx
,
n
,
err
:=
DeserializeLSAPrefix
(
buf
)
pfx
,
n
,
err
:=
DeserializeLSAPrefix
(
buf
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
readBytes
,
errors
.
Wrap
(
err
,
"unable to decode prefix"
)
return
nil
,
readBytes
,
errors
.
Wrap
(
err
,
"unable to decode prefix"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment