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

wip

parent 5b06a6ed
No related branches found
No related tags found
No related merge requests found
package net
import (
"fmt"
)
// IP represents an IPv4 or IPv6 address
type IP struct {
higher uint64
lower uint64
higher uint64
lower uint64
ipVersion uint8
}
// IPv4 returns a new `IP` representing an IPv4 address
func IPv4(val uint32) IP {
return IP{
lower: uint64(val),
lower: uint64(val),
ipVersion: 4,
}
}
// IPv6 returns a new `IP` representing an IPv6 address
func IPv6(higher, lower uint64) IP {
return IP{
higher: higher,
lower: lower,
higher: higher,
lower: lower,
ipVersion: 6,
}
}
......@@ -27,8 +34,8 @@ func (ip *IP) ToUint32() uint32 {
}
// Compare compares two IP addresses (returns 0 if equal, -1 if `ip` is smaller than `other`, 1 if `ip` is greater than `other`)
func (ip *IP) Compare(other *IP) int {
if ip.higher == other.higher && ip.lower == other.lower {
func (ip IP) Compare(other IP) int {
if ip == other {
return 0
}
......@@ -46,3 +53,39 @@ func (ip *IP) Compare(other *IP) int {
return -1
}
func (ip IP) String() string {
if ip.ipVersion == 6 {
return ip.stringIPv6()
}
return ip.stringIPv4()
}
func (ip IP) stringIPv6() string {
fmt.Println(ip.higher & 0xFFFF000000000000 >> 48)
return fmt.Sprintf("%X:%X:%X:%X:%X:%X:%X:%X",
ip.higher&0xFFFF000000000000>>48,
ip.higher&0x0000FFFF00000000>>32,
ip.higher&0x00000000FFFF0000>>16,
ip.higher&0x000000000000FFFF,
ip.lower&0xFFFF000000000000>>48,
ip.lower&0x0000FFFF00000000>>32,
ip.lower&0x00000000FFFF0000>>16,
ip.lower&0x000000000000FFFF)
}
func (ip IP) stringIPv4() string {
u := ip.ToUint32()
return fmt.Sprintf("%d.%d.%d.%d",
u&0xFF000000>>24,
u&0x00FF0000>>16,
u&0x0000FF00>>8,
u&0x000000FF)
}
func (ip IP) Bytes() {
}
......@@ -37,17 +37,17 @@ func TestToUint32(t *testing.T) {
func TestCompare(t *testing.T) {
tests := []struct {
name string
ip *IP
other *IP
ip IP
other IP
expected int
}{
{
name: "equal",
ip: &IP{
ip: IP{
lower: 100,
higher: 200,
},
other: &IP{
other: IP{
lower: 100,
higher: 200,
},
......@@ -55,11 +55,11 @@ func TestCompare(t *testing.T) {
},
{
name: "greater higher word",
ip: &IP{
ip: IP{
lower: 123,
higher: 200,
},
other: &IP{
other: IP{
lower: 456,
higher: 100,
},
......@@ -67,11 +67,11 @@ func TestCompare(t *testing.T) {
},
{
name: "lesser higher word",
ip: &IP{
ip: IP{
lower: 123,
higher: 100,
},
other: &IP{
other: IP{
lower: 456,
higher: 200,
},
......@@ -79,11 +79,11 @@ func TestCompare(t *testing.T) {
},
{
name: "equal higher word but lesser lower word",
ip: &IP{
ip: IP{
lower: 456,
higher: 100,
},
other: &IP{
other: IP{
lower: 123,
higher: 100,
},
......@@ -91,11 +91,11 @@ func TestCompare(t *testing.T) {
},
{
name: "equal higher word but lesser lower word",
ip: &IP{
ip: IP{
lower: 123,
higher: 100,
},
other: &IP{
other: IP{
lower: 456,
higher: 100,
},
......@@ -109,3 +109,39 @@ func TestCompare(t *testing.T) {
})
}
}
func TestIPString(t *testing.T) {
tests := []struct {
ip IP
expected string
}{
{
ip: IPv4(strAddr("192.168.0.1")),
expected: "192.168.0.1",
},
{
ip: IPv4(strAddr("0.0.0.0")),
expected: "0.0.0.0",
},
{
ip: IPv4(strAddr("255.255.255.255")),
expected: "255.255.255.255",
},
{
ip: IPv6(0, 0),
expected: "0:0:0:0:0:0:0:0",
},
{
ip: IPv6(2306131596687708724, 6230974922281175806),
expected: "2001:678:1E0:1234:5678:DEAD:BEEF:CAFE",
},
{
ip: IPv6(^uint64(0), ^uint64(0)),
expected: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
},
}
for _, test := range tests {
assert.Equal(t, test.expected, test.ip.String())
}
}
......@@ -21,7 +21,7 @@ type BGPPath struct {
MED uint32
EBGP bool
BGPIdentifier uint32
Source uint32
Source bnet.IP
Communities []uint32
LargeCommunities []packet.LargeCommunity
UnknownAttributes *packet.PathAttribute
......@@ -92,19 +92,19 @@ func (b *BGPPath) Compare(c *BGPPath) int8 {
}
// g)
if c.Source < b.Source {
if c.Source.Compare(b.Source) == -1 {
return 1
}
if c.Source > b.Source {
if c.Source.Compare(b.Source) == 1 {
return -1
}
if c.NextHop < b.NextHop {
if c.NextHop.Compare(b.NextHop) == -1 {
return 1
}
if c.NextHop > b.NextHop {
if c.NextHop.Compare(b.NextHop) == 1 {
return -1
}
......@@ -156,7 +156,7 @@ func (b *BGPPath) better(c *BGPPath) bool {
return true
}
if c.Source < b.Source {
if c.Source.Compare(b.Source) == -1 {
return true
}
......@@ -184,7 +184,7 @@ func (b *BGPPath) Print() string {
ret += fmt.Sprintf("\t\tOrigin: %s\n", origin)
ret += fmt.Sprintf("\t\tAS Path: %v\n", b.ASPath)
ret += fmt.Sprintf("\t\tBGP type: %s\n", bgpType)
nh := uint32To4Byte(b.NextHop)
nh := uint32To4Byte(b.NextHop.ToUint32())
ret += fmt.Sprintf("\t\tNEXT HOP: %d.%d.%d.%d\n", nh[0], nh[1], nh[2], nh[3])
ret += fmt.Sprintf("\t\tMED: %d\n", b.MED)
ret += fmt.Sprintf("\t\tPath ID: %d\n", b.PathIdentifier)
......
......@@ -2,16 +2,17 @@ package actions
import (
"github.com/bio-routing/bio-rd/net"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type SetNextHopAction struct {
addr uint32
ip bnet.IP
}
func NewSetNextHopAction(addr uint32) *SetNextHopAction {
func NewSetNextHopAction(ip bnet.IP) *SetNextHopAction {
return &SetNextHopAction{
addr: addr,
ip: ip,
}
}
......@@ -21,7 +22,7 @@ func (a *SetNextHopAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path
}
modified := pa.Copy()
modified.BGPPath.NextHop = a.addr
modified.BGPPath.NextHop = a.ip
return modified, false
}
package routingtable
import bnet "github.com/bio-routing/bio-rd/net"
// Neighbor represents the attributes identifying a neighbor relationsship
type Neighbor struct {
// Addres is the IPv4 address of the neighbor as integer representation
Address uint32
Address bnet.IP
// Local address is the local address of the BGP TCP connection
LocalAddress uint32
LocalAddress bnet.IP
// Type is the type / protocol used for routing inforation communitation
Type uint8
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment