diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-05-22 18:33:50 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-05-22 18:35:52 +0200 |
commit | b4cef2524f3b2be11f905c4114e2e747a8101160 (patch) | |
tree | 3bd95e4ce54c72edc8cbf5d0eb9cf7202048bebd | |
parent | 7038de95e119147245741c04feab25b833b615e9 (diff) |
Fix integer conversions
-rw-r--r-- | tun_darwin.go | 8 | ||||
-rw-r--r-- | tun_freebsd.go | 8 | ||||
-rw-r--r-- | tun_linux.go | 8 | ||||
-rw-r--r-- | tun_openbsd.go | 8 | ||||
-rw-r--r-- | uapi_bsd.go | 5 |
5 files changed, 8 insertions, 29 deletions
diff --git a/tun_darwin.go b/tun_darwin.go index 8e4b970..e5a01a7 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -380,11 +380,5 @@ func (tun *NativeTun) MTU() (int, error) { return 0, fmt.Errorf("failed to get MTU on %s", tun.name) } - // convert result to signed 32-bit int - - val := binary.LittleEndian.Uint32(ifr[16:20]) - if val >= (1 << 31) { - return int(val-(1<<31)) - (1 << 31), nil - } - return int(val), nil + return int(*(*int32)(unsafe.Pointer(&ifr[16]))), nil } diff --git a/tun_freebsd.go b/tun_freebsd.go index dfd5d46..5461c45 100644 --- a/tun_freebsd.go +++ b/tun_freebsd.go @@ -515,11 +515,5 @@ func (tun *NativeTun) MTU() (int, error) { return 0, fmt.Errorf("failed to get MTU on %s", tun.name) } - // convert result to signed 32-bit int - mtu := ifr.MTU - if mtu >= (1 << 31) { - return int(mtu-(1<<31)) - (1 << 31), nil - } - return int(mtu), nil - + return int(*(*int32)(unsafe.Pointer(&ifr.MTU))), nil } diff --git a/tun_linux.go b/tun_linux.go index b43ded8..db9cb51 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -263,13 +263,7 @@ func (tun *NativeTun) MTU() (int, error) { return 0, errors.New("failed to get MTU of TUN device: " + strconv.FormatInt(int64(errno), 10)) } - // convert result to signed 32-bit int - - val := binary.LittleEndian.Uint32(ifr[16:20]) - if val >= (1 << 31) { - return int(toInt32(val)), nil - } - return int(val), nil + return int(*(*int32)(unsafe.Pointer(&ifr[16]))), nil } func (tun *NativeTun) Name() (string, error) { diff --git a/tun_openbsd.go b/tun_openbsd.go index 97d8f38..932404e 100644 --- a/tun_openbsd.go +++ b/tun_openbsd.go @@ -356,11 +356,5 @@ func (tun *NativeTun) MTU() (int, error) { return 0, fmt.Errorf("failed to get MTU on %s", tun.name) } - // convert result to signed 32-bit int - mtu := ifr.MTU - if mtu >= (1 << 31) { - return int(mtu-(1<<31)) - (1 << 31), nil - } - return int(mtu), nil - + return int(*(*int32)(unsafe.Pointer(&ifr.MTU))), nil } diff --git a/uapi_bsd.go b/uapi_bsd.go index e949918..b2a7644 100644 --- a/uapi_bsd.go +++ b/uapi_bsd.go @@ -15,6 +15,7 @@ import ( "net" "os" "path" + "unsafe" ) const ( @@ -101,11 +102,13 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) { go func(l *UAPIListener) { event := unix.Kevent_t{ - Ident: uint64(uapi.keventFd), Filter: unix.EVFILT_VNODE, Flags: unix.EV_ADD | unix.EV_ENABLE | unix.EV_ONESHOT, Fflags: unix.NOTE_WRITE, } + // Allow this assignment to work with both the 32-bit and 64-bit version + // of the above struct. If you know another way, please submit a patch. + *(*uintptr)(unsafe.Pointer(&event.Ident)) = uintptr(uapi.keventFd) events := make([]unix.Kevent_t, 1) n := 1 var kerr error |