Skip to content
Snippets Groups Projects
Commit d26b4573 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

fixed typo, removed boilerplate code

parent 29a9e629
Branches
Tags
No related merge requests found
...@@ -180,41 +180,15 @@ func (pa *PathAttribute) decodeASPath(buf *bytes.Buffer) error { ...@@ -180,41 +180,15 @@ func (pa *PathAttribute) decodeASPath(buf *bytes.Buffer) error {
} }
func (pa *PathAttribute) decodeNextHop(buf *bytes.Buffer) error { func (pa *PathAttribute) decodeNextHop(buf *bytes.Buffer) error {
addr := [4]byte{} return pa.decodeUint32(buf, "next hop")
p := uint16(0)
n, err := buf.Read(addr[:])
if err != nil {
return err
}
if n != 4 {
return fmt.Errorf("Unable to read next hop: buf.Read read %d bytes", n)
}
pa.Value = fourBytesToUint32(addr)
p += 4
return dumpNBytes(buf, pa.Length-p)
} }
func (pa *PathAttribute) decodeMED(buf *bytes.Buffer) error { func (pa *PathAttribute) decodeMED(buf *bytes.Buffer) error {
med, err := pa.decodeUint32(buf) return pa.decodeUint32(buf, "MED")
if err != nil {
return fmt.Errorf("Unable to decode MED: %v", err)
}
pa.Value = uint32(med)
return nil
} }
func (pa *PathAttribute) decodeLocalPref(buf *bytes.Buffer) error { func (pa *PathAttribute) decodeLocalPref(buf *bytes.Buffer) error {
lpref, err := pa.decodeUint32(buf) return pa.decodeUint32(buf, "local pref")
if err != nil {
return fmt.Errorf("Unable to decode local pref: %v", err)
}
pa.Value = uint32(lpref)
return nil
} }
func (pa *PathAttribute) decodeAggregator(buf *bytes.Buffer) error { func (pa *PathAttribute) decodeAggregator(buf *bytes.Buffer) error {
...@@ -273,19 +247,19 @@ func (pa *PathAttribute) decodeLargeCommunities(buf *bytes.Buffer) error { ...@@ -273,19 +247,19 @@ func (pa *PathAttribute) decodeLargeCommunities(buf *bytes.Buffer) error {
for i := uint16(0); i < count; i++ { for i := uint16(0); i < count; i++ {
com := LargeCommunity{} com := LargeCommunity{}
v, err := read4BytesAsUin32(buf) v, err := read4BytesAsUint32(buf)
if err != nil { if err != nil {
return err return err
} }
com.GlobalAdministrator = v com.GlobalAdministrator = v
v, err = read4BytesAsUin32(buf) v, err = read4BytesAsUint32(buf)
if err != nil { if err != nil {
return err return err
} }
com.DataPart1 = v com.DataPart1 = v
v, err = read4BytesAsUin32(buf) v, err = read4BytesAsUint32(buf)
if err != nil { if err != nil {
return err return err
} }
...@@ -301,22 +275,27 @@ func (pa *PathAttribute) decodeLargeCommunities(buf *bytes.Buffer) error { ...@@ -301,22 +275,27 @@ func (pa *PathAttribute) decodeLargeCommunities(buf *bytes.Buffer) error {
} }
func (pa *PathAttribute) decodeAS4Path(buf *bytes.Buffer) error { func (pa *PathAttribute) decodeAS4Path(buf *bytes.Buffer) error {
as4Path, err := pa.decodeUint32(buf) return pa.decodeUint32(buf, "AS4Path")
}
func (pa *PathAttribute) decodeAS4Aggregator(buf *bytes.Buffer) error {
return pa.decodeUint32(buf, "AS4Aggregator")
}
func (pa *PathAttribute) decodeUint32(buf *bytes.Buffer, attrName string) error {
v, err := read4BytesAsUint32(buf)
if err != nil { if err != nil {
return fmt.Errorf("Unable to decode AS4Path: %v", err) return fmt.Errorf("Unable to decode %s: %v", attrName, err)
} }
pa.Value = as4Path pa.Value = v
return nil
}
func (pa *PathAttribute) decodeAS4Aggregator(buf *bytes.Buffer) error { p := uint16(4)
as4Aggregator, err := pa.decodeUint32(buf) err = dumpNBytes(buf, pa.Length-p)
if err != nil { if err != nil {
return fmt.Errorf("Unable to decode AS4Aggregator: %v", err) return fmt.Errorf("dumpNBytes failed: %v", err)
} }
pa.Value = as4Aggregator
return nil return nil
} }
...@@ -340,24 +319,6 @@ func (pa *PathAttribute) setLength(buf *bytes.Buffer) (int, error) { ...@@ -340,24 +319,6 @@ func (pa *PathAttribute) setLength(buf *bytes.Buffer) (int, error) {
return bytesRead, nil return bytesRead, nil
} }
func (pa *PathAttribute) decodeUint32(buf *bytes.Buffer) (uint32, error) {
var v uint32
p := uint16(0)
err := decode(buf, []interface{}{&v})
if err != nil {
return 0, err
}
p += 4
err = dumpNBytes(buf, pa.Length-p)
if err != nil {
return 0, fmt.Errorf("dumpNBytes failed: %v", err)
}
return v, nil
}
func (pa *PathAttribute) ASPathString() (ret string) { func (pa *PathAttribute) ASPathString() (ret string) {
for _, p := range pa.Value.(ASPath) { for _, p := range pa.Value.(ASPath) {
if p.Type == ASSet { if p.Type == ASSet {
...@@ -670,14 +631,14 @@ func fourBytesToUint32(address [4]byte) uint32 { ...@@ -670,14 +631,14 @@ func fourBytesToUint32(address [4]byte) uint32 {
return uint32(address[0])<<24 + uint32(address[1])<<16 + uint32(address[2])<<8 + uint32(address[3]) return uint32(address[0])<<24 + uint32(address[1])<<16 + uint32(address[2])<<8 + uint32(address[3])
} }
func read4BytesAsUin32(buf *bytes.Buffer) (uint32, error) { func read4BytesAsUint32(buf *bytes.Buffer) (uint32, error) {
b := [4]byte{} b := [4]byte{}
n, err := buf.Read(b[:]) n, err := buf.Read(b[:])
if err != nil { if err != nil {
return 0, err return 0, err
} }
if n != 4 { if n != 4 {
return 0, fmt.Errorf("Unable to read as uint32: buf.Read read %d bytes", n) return 0, fmt.Errorf("Unable to read as uint32. Expected 4 bytes but got only %d", n)
} }
return fourBytesToUint32(b), nil return fourBytesToUint32(b), nil
......
...@@ -724,7 +724,7 @@ func TestSetLength(t *testing.T) { ...@@ -724,7 +724,7 @@ func TestSetLength(t *testing.T) {
} }
} }
func TestDecodeUint32(t *testing.T) { func TestRead4BytesAsUint32(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input []byte input []byte
...@@ -763,7 +763,7 @@ func TestDecodeUint32(t *testing.T) { ...@@ -763,7 +763,7 @@ func TestDecodeUint32(t *testing.T) {
pa := &PathAttribute{ pa := &PathAttribute{
Length: l, Length: l,
} }
res, err := pa.decodeUint32(bytes.NewBuffer(test.input)) err := pa.decodeUint32(bytes.NewBuffer(test.input), "test")
if test.wantFail { if test.wantFail {
if err != nil { if err != nil {
...@@ -778,7 +778,7 @@ func TestDecodeUint32(t *testing.T) { ...@@ -778,7 +778,7 @@ func TestDecodeUint32(t *testing.T) {
continue continue
} }
assert.Equal(t, test.expected, res) assert.Equal(t, test.expected, pa.Value)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment