diff --git a/Makefile b/Makefile index f36c4fa..24b7d71 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,9 @@ SRCS=\ server.go \ server-ctl.go \ server-peer.go \ - server-ws.go + server-ws.go \ + system-freebsd.go \ + system-linux.go CMD_DATA=\ cmd/tls.crt \ diff --git a/server.go b/server.go index 6cd39d4..0ac47e8 100644 --- a/server.go +++ b/server.go @@ -1100,7 +1100,8 @@ func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, p return nil, fmt.Errorf("too many connections - %d", s.cts_limit) } - id = rand.Uint32() + id = uint32(monotonic_time()) + //id = rand.Uint32() for { _, ok = s.cts_map[id] if !ok { break } diff --git a/system-freebsd.go b/system-freebsd.go new file mode 100644 index 0000000..796dd65 --- /dev/null +++ b/system-freebsd.go @@ -0,0 +1,22 @@ +//go:build freebsd + +package hodu + +import "syscall" +import "unsafe" + +//#include +//import "C" + +// C.CLOCK_MONOTONIC is more accurate when compiled with CGO. +// I want to avoid using it. so assume it is 1 on linux +//const FREEBSD_CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC +const CLOCK_MONOTONIC uintptr = 4 + +func monotonic_time() uint64 { + var ts syscall.Timespec + var errno syscall.Errno + _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) + if errno != 0 { return 0 } + return uint64(ts.Nano()) +} diff --git a/system-linux.go b/system-linux.go new file mode 100644 index 0000000..efb7866 --- /dev/null +++ b/system-linux.go @@ -0,0 +1,22 @@ +//go:build linux + +package hodu + +import "syscall" +import "unsafe" + +//#include +//import "C" + +// C.CLOCK_MONOTONIC is more accurate when compiled with CGO. +// I want to avoid using it. so assume it is 1 on linux +//const CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC +const CLOCK_MONOTONIC uintptr = 1 + +func monotonic_time() uint64 { + var ts syscall.Timespec + var errno syscall.Errno + _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) + if errno != 0 { return 0 } + return uint64(ts.Nano()) +}