aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/config.go27
-rw-r--r--src/main.go9
-rw-r--r--src/peer.go9
-rw-r--r--src/receive.go2
-rw-r--r--src/send.go9
-rw-r--r--src/timers.go4
-rw-r--r--src/tun_linux.go46
-rw-r--r--src/uapi_linux.go9
8 files changed, 71 insertions, 44 deletions
diff --git a/src/config.go b/src/config.go
index 2d9ac50..c889de0 100644
--- a/src/config.go
+++ b/src/config.go
@@ -9,27 +9,19 @@ import (
"strconv"
"strings"
"sync/atomic"
- "syscall"
-)
-
-const (
- ipcErrorIO = syscall.EIO
- ipcErrorNoPeer = syscall.EPROTO
- ipcErrorNoKeyValue = syscall.EPROTO
- ipcErrorInvalidKey = syscall.EPROTO
- ipcErrorInvalidValue = syscall.EPROTO
+ "time"
)
type IPCError struct {
- Code syscall.Errno
+ Code int64
}
func (s *IPCError) Error() string {
return fmt.Sprintf("IPC error: %d", s.Code)
}
-func (s *IPCError) ErrorCode() uintptr {
- return uintptr(s.Code)
+func (s *IPCError) ErrorCode() int64 {
+ return s.Code
}
func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
@@ -58,8 +50,15 @@ func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
if peer.endpoint != nil {
send("endpoint=" + peer.endpoint.String())
}
- send(fmt.Sprintf("tx_bytes=%d", peer.txBytes))
- send(fmt.Sprintf("rx_bytes=%d", peer.rxBytes))
+
+ nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)
+ secs := nano / time.Second.Nanoseconds()
+ nano %= time.Second.Nanoseconds()
+
+ send(fmt.Sprintf("last_handshake_time_sec=%d", secs))
+ send(fmt.Sprintf("last_handshake_time_nsec=%d", nano))
+ send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes))
+ send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes))
send(fmt.Sprintf("persistent_keepalive_interval=%d",
atomic.LoadUint64(&peer.persistentKeepaliveInterval),
))
diff --git a/src/main.go b/src/main.go
index 4bece16..0857999 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,11 +1,17 @@
package main
import (
+ "fmt"
"log"
"os"
"runtime"
)
+func printUsage() {
+ fmt.Printf("usage:\n")
+ fmt.Printf("%s [-f/--foreground] INTERFACE-NAME\n", os.Args[0])
+}
+
func main() {
// parse arguments
@@ -13,6 +19,7 @@ func main() {
var foreground bool
var interfaceName string
if len(os.Args) < 2 || len(os.Args) > 3 {
+ printUsage()
return
}
@@ -21,6 +28,7 @@ func main() {
case "-f", "--foreground":
foreground = true
if len(os.Args) != 3 {
+ printUsage()
return
}
interfaceName = os.Args[2]
@@ -28,6 +36,7 @@ func main() {
default:
foreground = false
if len(os.Args) != 2 {
+ printUsage()
return
}
interfaceName = os.Args[1]
diff --git a/src/peer.go b/src/peer.go
index 408c605..8eea929 100644
--- a/src/peer.go
+++ b/src/peer.go
@@ -19,9 +19,12 @@ type Peer struct {
keyPairs KeyPairs
handshake Handshake
device *Device
- txBytes uint64
- rxBytes uint64
- time struct {
+ stats struct {
+ txBytes uint64 // bytes send to peer (endpoint)
+ rxBytes uint64 // bytes received from peer
+ lastHandshakeNano int64 // nano seconds since epoch
+ }
+ time struct {
mutex sync.RWMutex
lastSend time.Time // last send message
lastHandshake time.Time // last completed handshake
diff --git a/src/receive.go b/src/receive.go
index e063c99..d97ca41 100644
--- a/src/receive.go
+++ b/src/receive.go
@@ -540,7 +540,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
return
}
- atomic.AddUint64(&peer.rxBytes, uint64(len(elem.packet)))
+ atomic.AddUint64(&peer.stats.rxBytes, uint64(len(elem.packet)))
device.addToInboundQueue(device.queue.inbound, elem)
}()
}
diff --git a/src/send.go b/src/send.go
index fdbc676..7cdb806 100644
--- a/src/send.go
+++ b/src/send.go
@@ -313,17 +313,15 @@ func (device *Device) RoutineEncryption() {
elem.packet = append(elem.packet, 0)
}
- // encrypt content
+ // encrypt content (append to header)
binary.LittleEndian.PutUint64(nonce[4:], elem.nonce)
elem.packet = elem.keyPair.send.Seal(
- elem.packet[:0],
+ header,
nonce[:],
elem.packet,
nil,
)
- length := MessageTransportHeaderSize + len(elem.packet)
- elem.packet = elem.buffer[:length]
elem.mutex.Unlock()
// refresh key if necessary
@@ -381,7 +379,8 @@ func (peer *Peer) RoutineSequentialSender() {
if err != nil {
return
}
- atomic.AddUint64(&peer.txBytes, uint64(len(elem.packet)))
+
+ atomic.AddUint64(&peer.stats.txBytes, uint64(len(elem.packet)))
peer.TimerResetKeepalive()
}()
diff --git a/src/timers.go b/src/timers.go
index fd2bdc3..2454414 100644
--- a/src/timers.go
+++ b/src/timers.go
@@ -52,6 +52,10 @@ func (peer *Peer) KeepKeyFreshReceiving() {
func (peer *Peer) EventHandshakeComplete() {
peer.device.log.Info.Println("Negotiated new handshake for", peer.String())
peer.timer.zeroAllKeys.Reset(RejectAfterTime * 3)
+ atomic.StoreInt64(
+ &peer.stats.lastHandshakeNano,
+ time.Now().UnixNano(),
+ )
signalSend(peer.signal.handshakeCompleted)
}
diff --git a/src/tun_linux.go b/src/tun_linux.go
index d0e9761..261d142 100644
--- a/src/tun_linux.go
+++ b/src/tun_linux.go
@@ -1,17 +1,17 @@
package main
+/* Implementation of the TUN device interface for linux
+ */
+
import (
"encoding/binary"
"errors"
+ "golang.org/x/sys/unix"
"os"
"strings"
- "syscall"
"unsafe"
)
-/* Implementation of the TUN device interface for linux
- */
-
const CloneDevicePath = "/dev/net/tun"
type NativeTun struct {
@@ -27,9 +27,9 @@ func (tun *NativeTun) setMTU(n int) error {
// open datagram socket
- fd, err := syscall.Socket(
- syscall.AF_INET,
- syscall.SOCK_DGRAM,
+ fd, err := unix.Socket(
+ unix.AF_INET,
+ unix.SOCK_DGRAM,
0,
)
@@ -37,15 +37,17 @@ func (tun *NativeTun) setMTU(n int) error {
return err
}
+ defer unix.Close(fd)
+
// do ioctl call
var ifr [64]byte
copy(ifr[:], tun.name)
binary.LittleEndian.PutUint32(ifr[16:20], uint32(n))
- _, _, errno := syscall.Syscall(
- syscall.SYS_IOCTL,
+ _, _, errno := unix.Syscall(
+ unix.SYS_IOCTL,
uintptr(fd),
- uintptr(syscall.SIOCSIFMTU),
+ uintptr(unix.SIOCSIFMTU),
uintptr(unsafe.Pointer(&ifr[0])),
)
@@ -60,9 +62,9 @@ func (tun *NativeTun) MTU() (int, error) {
// open datagram socket
- fd, err := syscall.Socket(
- syscall.AF_INET,
- syscall.SOCK_DGRAM,
+ fd, err := unix.Socket(
+ unix.AF_INET,
+ unix.SOCK_DGRAM,
0,
)
@@ -70,14 +72,16 @@ func (tun *NativeTun) MTU() (int, error) {
return 0, err
}
+ defer unix.Close(fd)
+
// do ioctl call
var ifr [64]byte
copy(ifr[:], tun.name)
- _, _, errno := syscall.Syscall(
- syscall.SYS_IOCTL,
+ _, _, errno := unix.Syscall(
+ unix.SYS_IOCTL,
uintptr(fd),
- uintptr(syscall.SIOCGIFMTU),
+ uintptr(unix.SIOCGIFMTU),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
@@ -113,18 +117,18 @@ func CreateTUN(name string) (TUNDevice, error) {
// create new device
var ifr [64]byte
- var flags uint16 = syscall.IFF_TUN | syscall.IFF_NO_PI
+ var flags uint16 = unix.IFF_TUN | unix.IFF_NO_PI
nameBytes := []byte(name)
- if len(nameBytes) >= syscall.IFNAMSIZ {
+ if len(nameBytes) >= unix.IFNAMSIZ {
return nil, errors.New("Name size too long")
}
copy(ifr[:], nameBytes)
binary.LittleEndian.PutUint16(ifr[16:], flags)
- _, _, errno := syscall.Syscall(
- syscall.SYS_IOCTL,
+ _, _, errno := unix.Syscall(
+ unix.SYS_IOCTL,
uintptr(fd.Fd()),
- uintptr(syscall.TUNSETIFF),
+ uintptr(unix.TUNSETIFF),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
diff --git a/src/uapi_linux.go b/src/uapi_linux.go
index ee6ee0b..1055dca 100644
--- a/src/uapi_linux.go
+++ b/src/uapi_linux.go
@@ -2,11 +2,20 @@ package main
import (
"fmt"
+ "golang.org/x/sys/unix"
"net"
"os"
"time"
)
+const (
+ ipcErrorIO = int64(unix.EIO)
+ ipcErrorNoPeer = int64(unix.EPROTO)
+ ipcErrorNoKeyValue = int64(unix.EPROTO)
+ ipcErrorInvalidKey = int64(unix.EPROTO)
+ ipcErrorInvalidValue = int64(unix.EPROTO)
+)
+
/* TODO:
* This code can be improved by using fsnotify once:
* https://github.com/fsnotify/fsnotify/pull/205