From 460a22c0635fb75e8a44a6f6d12dff28b4f4eaf3 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 27 May 2018 23:31:34 +0100 Subject: [PATCH] Clean up some exported constants --- src/yggdrasil/debug.go | 25 +++++++++++++++++++++++++ src/yggdrasil/icmpv6.go | 12 ++++++------ src/yggdrasil/tun.go | 12 ++++++------ src/yggdrasil/tun_darwin.go | 6 +++--- src/yggdrasil/util.go | 24 ------------------------ 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/yggdrasil/debug.go b/src/yggdrasil/debug.go index bbdd0f6..292ca57 100644 --- a/src/yggdrasil/debug.go +++ b/src/yggdrasil/debug.go @@ -456,3 +456,28 @@ func DEBUG_simLinkPeers(p, q *peer) { func (c *Core) DEBUG_simFixMTU() { c.tun.mtu = 65535 } + +//////////////////////////////////////////////////////////////////////////////// + +func Util_testAddrIDMask() { + for idx := 0; idx < 16; idx++ { + var orig NodeID + orig[8] = 42 + for bidx := 0; bidx < idx; bidx++ { + orig[bidx/8] |= (0x80 >> uint8(bidx%8)) + } + addr := address_addrForNodeID(&orig) + nid, mask := addr.getNodeIDandMask() + for b := 0; b < len(mask); b++ { + nid[b] &= mask[b] + orig[b] &= mask[b] + } + if *nid != orig { + fmt.Println(orig) + fmt.Println(*addr) + fmt.Println(*nid) + fmt.Println(*mask) + panic(idx) + } + } +} diff --git a/src/yggdrasil/icmpv6.go b/src/yggdrasil/icmpv6.go index 96c5218..fa14b2a 100644 --- a/src/yggdrasil/icmpv6.go +++ b/src/yggdrasil/icmpv6.go @@ -12,7 +12,7 @@ import "errors" type macAddress [6]byte -const ETHER = 14 +const len_ETHER = 14 type icmpv6 struct { tun *tunDevice @@ -79,13 +79,13 @@ func (i *icmpv6) parse_packet_tap(datain []byte) ([]byte, error) { } // Hand over to parse_packet_tun to interpret the IPv6 packet - ipv6packet, err := i.parse_packet_tun(datain[ETHER:]) + ipv6packet, err := i.parse_packet_tun(datain[len_ETHER:]) if err != nil { return nil, err } // Create the response buffer - dataout := make([]byte, ETHER+ipv6.HeaderLen+32) + dataout := make([]byte, len_ETHER+ipv6.HeaderLen+32) // Populate the response ethernet headers copy(dataout[:6], datain[6:12]) @@ -93,7 +93,7 @@ func (i *icmpv6) parse_packet_tap(datain []byte) ([]byte, error) { binary.BigEndian.PutUint16(dataout[12:14], uint16(0x86DD)) // Copy the returned packet to our response ethernet frame - copy(dataout[ETHER:], ipv6packet) + copy(dataout[len_ETHER:], ipv6packet) return dataout, nil } @@ -157,7 +157,7 @@ func (i *icmpv6) create_icmpv6_tap(dstmac macAddress, dst net.IP, src net.IP, mt } // Create the response buffer - dataout := make([]byte, ETHER+len(ipv6packet)) + dataout := make([]byte, len_ETHER+len(ipv6packet)) // Populate the response ethernet headers copy(dataout[:6], dstmac[:6]) @@ -165,7 +165,7 @@ func (i *icmpv6) create_icmpv6_tap(dstmac macAddress, dst net.IP, src net.IP, mt binary.BigEndian.PutUint16(dataout[12:14], uint16(0x86DD)) // Copy the returned packet to our response ethernet frame - copy(dataout[ETHER:], ipv6packet) + copy(dataout[len_ETHER:], ipv6packet) return dataout, nil } diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index f6a56e4..1ecc53b 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -5,8 +5,8 @@ package yggdrasil import "github.com/songgao/packets/ethernet" import "github.com/yggdrasil-network/water" -const IPv6_HEADER_LENGTH = 40 -const ETHER_HEADER_LENGTH = 14 +const tun_IPv6_HEADER_LENGTH = 40 +const tun_ETHER_HEADER_LENGTH = 14 type tunDevice struct { core *Core @@ -59,7 +59,7 @@ func (tun *tunDevice) write() error { ethernet.NotTagged, // VLAN tagging ethernet.IPv6, // Ethertype len(data)) // Payload length - copy(frame[ETHER_HEADER_LENGTH:], data[:]) + copy(frame[tun_ETHER_HEADER_LENGTH:], data[:]) if _, err := tun.iface.Write(frame); err != nil { panic(err) } @@ -75,7 +75,7 @@ func (tun *tunDevice) write() error { func (tun *tunDevice) read() error { mtu := tun.mtu if tun.iface.IsTAP() { - mtu += ETHER_HEADER_LENGTH + mtu += tun_ETHER_HEADER_LENGTH } buf := make([]byte, mtu) for { @@ -86,10 +86,10 @@ func (tun *tunDevice) read() error { } o := 0 if tun.iface.IsTAP() { - o = ETHER_HEADER_LENGTH + o = tun_ETHER_HEADER_LENGTH } if buf[o]&0xf0 != 0x60 || - n != 256*int(buf[o+4])+int(buf[o+5])+IPv6_HEADER_LENGTH+o { + n != 256*int(buf[o+4])+int(buf[o+5])+tun_IPv6_HEADER_LENGTH+o { // Either not an IPv6 packet or not the complete packet for some reason //panic("Should not happen in testing") continue diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 72b9cf0..6096d6a 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -33,7 +33,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) return tun.setupAddress(addr) } -const SIOCAIFADDR_IN6 = 2155899162 +const darwin_SIOCAIFADDR_IN6 = 2155899162 type in6_addrlifetime struct { ia6t_expire float64 @@ -103,9 +103,9 @@ func (tun *tunDevice) setupAddress(addr string) error { tun.core.log.Printf("Interface IPv6: %s", addr) tun.core.log.Printf("Interface MTU: %d", ir.ifru_mtu) - if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { + if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 { err = errno - tun.core.log.Printf("Error in SIOCAIFADDR_IN6: %v", errno) + tun.core.log.Printf("Error in darwin_SIOCAIFADDR_IN6: %v", errno) return err } diff --git a/src/yggdrasil/util.go b/src/yggdrasil/util.go index f882649..908b1ae 100644 --- a/src/yggdrasil/util.go +++ b/src/yggdrasil/util.go @@ -2,34 +2,10 @@ package yggdrasil // These are misc. utility functions that didn't really fit anywhere else -import "fmt" import "runtime" //import "sync" -func Util_testAddrIDMask() { - for idx := 0; idx < 16; idx++ { - var orig NodeID - orig[8] = 42 - for bidx := 0; bidx < idx; bidx++ { - orig[bidx/8] |= (0x80 >> uint8(bidx%8)) - } - addr := address_addrForNodeID(&orig) - nid, mask := addr.getNodeIDandMask() - for b := 0; b < len(mask); b++ { - nid[b] &= mask[b] - orig[b] &= mask[b] - } - if *nid != orig { - fmt.Println(orig) - fmt.Println(*addr) - fmt.Println(*nid) - fmt.Println(*mask) - panic(idx) - } - } -} - func util_yield() { runtime.Gosched() }