diff --git a/common/logging.go b/common/logging.go index 074eb850..d42ad44a 100644 --- a/common/logging.go +++ b/common/logging.go @@ -52,3 +52,15 @@ func SetLogLevel(levelname string) { } Log.Level = level } + +func CheckFatal(e error) { + if e != nil { + Log.Fatal(e) + } +} + +func CheckWarn(e error) { + if e != nil { + Log.Warnln(e) + } +} diff --git a/ipam/allocator.go b/ipam/allocator.go index 05fdcda1..4c32404e 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -608,17 +608,17 @@ func encodeRange(r address.Range) []byte { } func (alloc *Allocator) sendSpaceRequest(dest router.PeerName, r address.Range) error { - msg := router.Concat([]byte{msgSpaceRequest}, encodeRange(r)) + msg := append([]byte{msgSpaceRequest}, encodeRange(r)...) return alloc.gossip.GossipUnicast(dest, msg) } func (alloc *Allocator) sendSpaceRequestDenied(dest router.PeerName, r address.Range) error { - msg := router.Concat([]byte{msgSpaceRequestDenied}, encodeRange(r)) + msg := append([]byte{msgSpaceRequestDenied}, encodeRange(r)...) return alloc.gossip.GossipUnicast(dest, msg) } func (alloc *Allocator) sendRingUpdate(dest router.PeerName) { - msg := router.Concat([]byte{msgRingUpdate}, alloc.encode()) + msg := append([]byte{msgRingUpdate}, alloc.encode()...) alloc.gossip.GossipUnicast(dest, msg) } diff --git a/router/connection.go b/router/connection.go index 6c9fb962..90f5f80f 100644 --- a/router/connection.go +++ b/router/connection.go @@ -414,7 +414,7 @@ func (conn *LocalConnection) sendSimpleProtocolMsg(tag ProtocolTag) error { } func (conn *LocalConnection) sendProtocolMsg(m ProtocolMsg) error { - return conn.tcpSender.Send(Concat([]byte{byte(m.tag)}, m.msg)) + return conn.tcpSender.Send(append([]byte{byte(m.tag)}, m.msg...)) } func (conn *LocalConnection) receiveTCP(receiver TCPReceiver) { diff --git a/router/crypto.go b/router/crypto.go index 939157af..160972fd 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -180,6 +180,10 @@ type PacketDecodingError struct { Desc string } +func (pde PacketDecodingError) Error() string { + return fmt.Sprint("Failed to decode packet: ", pde.Desc) +} + func NewNonDecryptor() *NonDecryptor { return &NonDecryptor{} } diff --git a/router/gossip_channel.go b/router/gossip_channel.go index 11e8e8aa..dd817712 100644 --- a/router/gossip_channel.go +++ b/router/gossip_channel.go @@ -239,3 +239,12 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) { func (c *GossipChannel) log(args ...interface{}) { log.Println(append(append([]interface{}{}, "[gossip "+c.name+"]:"), args...)...) } + +func GobEncode(items ...interface{}) []byte { + buf := new(bytes.Buffer) + enc := gob.NewEncoder(buf) + for _, i := range items { + checkFatal(enc.Encode(i)) + } + return buf.Bytes() +} diff --git a/router/mac_cache.go b/router/mac_cache.go index ecc9afb3..0d0e77c7 100644 --- a/router/mac_cache.go +++ b/router/mac_cache.go @@ -112,3 +112,20 @@ func (cache *MacCache) expire() { } cache.setExpiryTimer() } + +func macint(mac net.HardwareAddr) (r uint64) { + for _, b := range mac { + r <<= 8 + r |= uint64(b) + } + return +} + +func intmac(key uint64) (r net.HardwareAddr) { + r = make([]byte, 6) + for i := 5; i >= 0; i-- { + r[i] = byte(key) + key >>= 8 + } + return +} diff --git a/router/peer.go b/router/peer.go index e73d1592..a1114e4f 100644 --- a/router/peer.go +++ b/router/peer.go @@ -1,11 +1,28 @@ package router import ( + "crypto/rand" + "encoding/binary" "fmt" "sort" "strconv" ) +func randBytes(n int) []byte { + buf := make([]byte, n) + _, err := rand.Read(buf) + checkFatal(err) + return buf +} + +func randUint64() (r uint64) { + return binary.LittleEndian.Uint64(randBytes(8)) +} + +func randUint16() (r uint16) { + return binary.LittleEndian.Uint16(randBytes(2)) +} + type PeerUID uint64 func randomPeerUID() PeerUID { @@ -45,6 +62,18 @@ type Peer struct { connections map[PeerName]Connection } +type ListOfPeers []*Peer + +func (lop ListOfPeers) Len() int { + return len(lop) +} +func (lop ListOfPeers) Swap(i, j int) { + lop[i], lop[j] = lop[j], lop[i] +} +func (lop ListOfPeers) Less(i, j int) bool { + return lop[i].Name < lop[j].Name +} + type ConnectionSet map[Connection]struct{} func NewPeerFromSummary(summary PeerSummary) *Peer { diff --git a/router/peers.go b/router/peers.go index f3d238d0..6082c754 100644 --- a/router/peers.go +++ b/router/peers.go @@ -3,11 +3,14 @@ package router import ( "bytes" "encoding/gob" + "fmt" "io" "math/rand" "sync" ) +var void = struct{}{} + type Peers struct { sync.RWMutex ourself *LocalPeer @@ -33,10 +36,18 @@ type UnknownPeerError struct { Name PeerName } +func (upe UnknownPeerError) Error() string { + return fmt.Sprint("Reference to unknown peer ", upe.Name) +} + type NameCollisionError struct { Name PeerName } +func (nce NameCollisionError) Error() string { + return fmt.Sprint("Multiple peers found with same name: ", nce.Name) +} + type PeerNameSet map[PeerName]struct{} type ConnectionSummary struct { diff --git a/router/router.go b/router/router.go index 31ff80d6..9ea3623f 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,8 @@ import ( "net" "sync" "time" + + "github.com/weaveworks/weave/common" ) const ( @@ -20,6 +22,12 @@ const ( // [3] control rate at which new tokens are added to the bucket +var ( + log = common.Log + checkFatal = common.CheckFatal + checkWarn = common.CheckWarn +) + type Config struct { Port int ProtocolMinVersion byte diff --git a/router/sleeve.go b/router/sleeve.go index ffc11f7f..89dbb750 100644 --- a/router/sleeve.go +++ b/router/sleeve.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "net" + "os" "sync" "syscall" "time" @@ -1133,3 +1134,22 @@ func tickerChan(ticker *time.Ticker) <-chan time.Time { } return nil } + +func makeUDPAddr(addr *net.TCPAddr) *net.UDPAddr { + return &net.UDPAddr{IP: addr.IP, Port: addr.Port, Zone: addr.Zone} +} + +// Look inside an error produced by the net package to get to the +// syscall.Errno at the root of the problem. +func PosixError(err error) error { + if operr, ok := err.(*net.OpError); ok { + err = operr.Err + } + + // go1.5 wraps an Errno inside a SyscallError inside an OpError + if scerr, ok := err.(*os.SyscallError); ok { + err = scerr.Err + } + + return err +} diff --git a/router/utils.go b/router/utils.go deleted file mode 100644 index 3da6f747..00000000 --- a/router/utils.go +++ /dev/null @@ -1,121 +0,0 @@ -package router - -import ( - "bytes" - "crypto/rand" - "encoding/binary" - "encoding/gob" - "fmt" - "net" - "os" - - "github.com/weaveworks/weave/common" -) - -var log = common.Log - -var void = struct{}{} - -func checkFatal(e error) { - if e != nil { - log.Fatal(e) - } -} - -func checkWarn(e error) { - if e != nil { - log.Warnln(e) - } -} - -// Look inside an error produced by the net package to get to the -// syscall.Errno at the root of the problem. -func PosixError(err error) error { - if operr, ok := err.(*net.OpError); ok { - err = operr.Err - } - - // go1.5 wraps an Errno inside a SyscallError inside an OpError - if scerr, ok := err.(*os.SyscallError); ok { - err = scerr.Err - } - - return err -} - -func (upe UnknownPeerError) Error() string { - return fmt.Sprint("Reference to unknown peer ", upe.Name) -} - -func (nce NameCollisionError) Error() string { - return fmt.Sprint("Multiple peers found with same name: ", nce.Name) -} - -func (pde PacketDecodingError) Error() string { - return fmt.Sprint("Failed to decode packet: ", pde.Desc) -} - -func Concat(elems ...[]byte) []byte { - res := []byte{} - for _, e := range elems { - res = append(res, e...) - } - return res -} - -func randBytes(n int) []byte { - buf := make([]byte, n) - _, err := rand.Read(buf) - checkFatal(err) - return buf -} - -func randUint64() (r uint64) { - return binary.LittleEndian.Uint64(randBytes(8)) -} - -func randUint16() (r uint16) { - return binary.LittleEndian.Uint16(randBytes(2)) -} - -func GobEncode(items ...interface{}) []byte { - buf := new(bytes.Buffer) - enc := gob.NewEncoder(buf) - for _, i := range items { - checkFatal(enc.Encode(i)) - } - return buf.Bytes() -} - -func macint(mac net.HardwareAddr) (r uint64) { - for _, b := range mac { - r <<= 8 - r |= uint64(b) - } - return -} - -func intmac(key uint64) (r net.HardwareAddr) { - r = make([]byte, 6) - for i := 5; i >= 0; i-- { - r[i] = byte(key) - key >>= 8 - } - return -} - -func makeUDPAddr(addr *net.TCPAddr) *net.UDPAddr { - return &net.UDPAddr{IP: addr.IP, Port: addr.Port, Zone: addr.Zone} -} - -type ListOfPeers []*Peer - -func (lop ListOfPeers) Len() int { - return len(lop) -} -func (lop ListOfPeers) Swap(i, j int) { - lop[i], lop[j] = lop[j], lop[i] -} -func (lop ListOfPeers) Less(i, j int) bool { - return lop[i].Name < lop[j].Name -}