Fixed port endianness

This commit is contained in:
Mathias Hall-Andersen 2017-11-11 15:43:55 +01:00
parent 0485c34c8e
commit 892276aa64
4 changed files with 159 additions and 140 deletions

View file

@ -34,6 +34,21 @@ func parseEndpoint(s string) (*net.UDPAddr, error) {
return addr, err return addr, err
} }
/* Must hold device and net lock
*/
func unsafeCloseUDPListener(device *Device) error {
netc := &device.net
if netc.bind != nil {
if err := netc.bind.Close(); err != nil {
return err
}
netc.bind = nil
netc.update.Broadcast()
}
return nil
}
// must inform all listeners
func UpdateUDPListener(device *Device) error { func UpdateUDPListener(device *Device) error {
device.mutex.Lock() device.mutex.Lock()
defer device.mutex.Unlock() defer device.mutex.Unlock()
@ -44,26 +59,22 @@ func UpdateUDPListener(device *Device) error {
// close existing sockets // close existing sockets
if netc.bind != nil { if err := unsafeCloseUDPListener(device); err != nil {
println("close bind")
if err := netc.bind.Close(); err != nil {
return err return err
} }
netc.bind = nil
println("closed") // wait for reader
}
// open new sockets // open new sockets
if device.tun.isUp.Get() { if device.tun.isUp.Get() {
println("creat")
// bind to new port // bind to new port
var err error var err error
netc.bind, netc.port, err = CreateUDPBind(netc.port) netc.bind, netc.port, err = CreateUDPBind(netc.port)
if err != nil { if err != nil {
netc.bind = nil
return err return err
} }
@ -74,8 +85,6 @@ func UpdateUDPListener(device *Device) error {
return err return err
} }
println("okay")
// clear cached source addresses // clear cached source addresses
for _, peer := range device.peers { for _, peer := range device.peers {
@ -83,14 +92,20 @@ func UpdateUDPListener(device *Device) error {
peer.endpoint.value.ClearSrc() peer.endpoint.value.ClearSrc()
peer.mutex.Unlock() peer.mutex.Unlock()
} }
// inform readers of updated bind
netc.update.Broadcast()
} }
return nil return nil
} }
func CloseUDPListener(device *Device) error { func CloseUDPListener(device *Device) error {
netc := &device.net device.mutex.Lock()
netc.mutex.Lock() device.net.mutex.Lock()
defer netc.mutex.Unlock() err := unsafeCloseUDPListener(device)
return netc.bind.Close() device.net.mutex.Unlock()
device.mutex.Unlock()
return err
} }

View file

@ -7,8 +7,8 @@
package main package main
import ( import (
"encoding/binary"
"errors" "errors"
"fmt"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"net" "net"
"strconv" "strconv"
@ -37,6 +37,17 @@ type NativeBind struct {
sock6 int sock6 int
} }
func htons(val uint16) uint16 {
var out [unsafe.Sizeof(val)]byte
binary.BigEndian.PutUint16(out[:], val)
return *((*uint16)(unsafe.Pointer(&out[0])))
}
func ntohs(val uint16) uint16 {
tmp := ((*[unsafe.Sizeof(val)]byte)(unsafe.Pointer(&val)))
return binary.BigEndian.Uint16((*tmp)[:])
}
func CreateUDPBind(port uint16) (UDPBind, uint16, error) { func CreateUDPBind(port uint16) (UDPBind, uint16, error) {
var err error var err error
var bind NativeBind var bind NativeBind
@ -50,8 +61,6 @@ func CreateUDPBind(port uint16) (UDPBind, uint16, error) {
if err != nil { if err != nil {
unix.Close(bind.sock6) unix.Close(bind.sock6)
} }
println(bind.sock6)
println(bind.sock4)
return bind, port, err return bind, port, err
} }
@ -297,13 +306,11 @@ func (end *Endpoint) SetDst(s string) error {
return err return err
} }
fmt.Println(addr, err)
ipv4 := addr.IP.To4() ipv4 := addr.IP.To4()
if ipv4 != nil { if ipv4 != nil {
dst := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst)) dst := (*unix.RawSockaddrInet4)(unsafe.Pointer(&end.dst))
dst.Family = unix.AF_INET dst.Family = unix.AF_INET
dst.Port = uint16(addr.Port) dst.Port = htons(uint16(addr.Port))
dst.Zero = [8]byte{} dst.Zero = [8]byte{}
copy(dst.Addr[:], ipv4) copy(dst.Addr[:], ipv4)
end.ClearSrc() end.ClearSrc()
@ -318,7 +325,7 @@ func (end *Endpoint) SetDst(s string) error {
} }
dst := &end.dst dst := &end.dst
dst.Family = unix.AF_INET6 dst.Family = unix.AF_INET6
dst.Port = uint16(addr.Port) dst.Port = htons(uint16(addr.Port))
dst.Flowinfo = 0 dst.Flowinfo = 0
dst.Scope_id = zone dst.Scope_id = zone
copy(dst.Addr[:], ipv6[:]) copy(dst.Addr[:], ipv6[:])
@ -392,9 +399,6 @@ func send6(sock int, end *Endpoint, buff []byte) error {
} }
func send4(sock int, end *Endpoint, buff []byte) error { func send4(sock int, end *Endpoint, buff []byte) error {
println("send 4")
println(end.DstToString())
println(sock)
// construct message header // construct message header
@ -425,6 +429,7 @@ func send4(sock int, end *Endpoint, buff []byte) error {
Name: (*byte)(unsafe.Pointer(&end.dst)), Name: (*byte)(unsafe.Pointer(&end.dst)),
Namelen: unix.SizeofSockaddrInet4, Namelen: unix.SizeofSockaddrInet4,
Control: (*byte)(unsafe.Pointer(&cmsg)), Control: (*byte)(unsafe.Pointer(&cmsg)),
Flags: 0,
} }
msghdr.SetControllen(int(unsafe.Sizeof(cmsg))) msghdr.SetControllen(int(unsafe.Sizeof(cmsg)))
@ -437,10 +442,6 @@ func send4(sock int, end *Endpoint, buff []byte) error {
0, 0,
) )
if errno == 0 {
return nil
}
// clear source and try again // clear source and try again
if errno == unix.EINVAL { if errno == unix.EINVAL {
@ -454,6 +455,12 @@ func send4(sock int, end *Endpoint, buff []byte) error {
) )
} }
// errno = 0 is still an error instance
if errno == 0 {
return nil
}
return errno return errno
} }

View file

@ -23,9 +23,10 @@ type Device struct {
} }
net struct { net struct {
mutex sync.RWMutex mutex sync.RWMutex
bind UDPBind bind UDPBind // bind interface
port uint16 port uint16 // listening port
fwmark uint32 fwmark uint32 // mark value (0 = disabled)
update *sync.Cond // the bind was updated
} }
mutex sync.RWMutex mutex sync.RWMutex
privateKey NoisePrivateKey privateKey NoisePrivateKey
@ -39,7 +40,6 @@ type Device struct {
} }
signal struct { signal struct {
stop chan struct{} stop chan struct{}
updateBind chan struct{}
} }
underLoadUntil atomic.Value underLoadUntil atomic.Value
ratelimiter Ratelimiter ratelimiter Ratelimiter
@ -163,6 +163,12 @@ func NewDevice(tun TUNDevice, logLevel int) *Device {
device.signal.stop = make(chan struct{}) device.signal.stop = make(chan struct{})
// prepare net
device.net.port = 0
device.net.bind = nil
device.net.update = sync.NewCond(&device.net.mutex)
// start workers // start workers
for i := 0; i < runtime.NumCPU(); i += 1 { for i := 0; i < runtime.NumCPU(); i += 1 {

View file

@ -99,26 +99,18 @@ func (device *Device) RoutineReceiveIncomming(IPVersion int) {
for { for {
// wait for new conn // wait for bind
logDebug.Println("Waiting for udp socket") logDebug.Println("Waiting for udp bind")
device.net.mutex.Lock()
select { device.net.update.Wait()
case <-device.signal.stop:
return
case <-device.signal.updateBind:
// fetch new socket
device.net.mutex.RLock()
bind := device.net.bind bind := device.net.bind
device.net.mutex.RUnlock() device.net.mutex.Unlock()
if bind == nil { if bind == nil {
continue continue
} }
logDebug.Println("Listening for inbound packets") logDebug.Println("LISTEN\n\n\n")
// receive datagrams until conn is closed // receive datagrams until conn is closed
@ -230,7 +222,6 @@ func (device *Device) RoutineReceiveIncomming(IPVersion int) {
} }
} }
} }
}
} }
func (device *Device) RoutineDecryption() { func (device *Device) RoutineDecryption() {