diff options
author | Thomas H. Ptacek <thomas@sockpuppet.org> | 2022-01-31 16:55:36 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2022-02-01 20:16:42 +0100 |
commit | a702597e228006460b1b9f01fb9d8cc12327132f (patch) | |
tree | 1fb3df08ceb5b5f1388b69fc38cea896b7557de1 /tun/netstack/examples/ping_client.go | |
parent | version: bump snapshot (diff) | |
download | wireguard-go-a702597e228006460b1b9f01fb9d8cc12327132f.tar.xz wireguard-go-a702597e228006460b1b9f01fb9d8cc12327132f.zip |
tun/netstack: implement ICMP ping
Provide a PacketConn interface for netstack's ICMP endpoint; netstack
currently only provides EchoRequest/EchoResponse ICMP support, so this
code exposes only an interface for doing ping.
Currently is missing:
- Write deadlines
- Context support
Signed-off-by: Thomas Ptacek <thomas@sockpuppet.org>
[Jason: rework structure, match std go interfaces, add example code]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tun/netstack/examples/ping_client.go')
-rw-r--r-- | tun/netstack/examples/ping_client.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tun/netstack/examples/ping_client.go b/tun/netstack/examples/ping_client.go new file mode 100644 index 0000000..843a3ee --- /dev/null +++ b/tun/netstack/examples/ping_client.go @@ -0,0 +1,57 @@ +//go:build ignore +// +build ignore + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved. + */ + +package main + +import ( + "log" + "time" + + "golang.zx2c4.com/go118/netip" + "golang.zx2c4.com/wireguard/conn" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/tun/netstack" +) + +func main() { + tun, tnet, err := netstack.CreateNetTUN( + []netip.Addr{netip.MustParseAddr("192.168.4.29")}, + []netip.Addr{netip.MustParseAddr("8.8.8.8")}, + 1420) + if err != nil { + log.Panic(err) + } + dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(device.LogLevelVerbose, "")) + dev.IpcSet(`private_key=a8dac1d8a70a751f0f699fb14ba1cff7b79cf4fbd8f09f44c6e6a90d0369604f +public_key=25123c5dcd3328ff645e4f2a3fce0d754400d3887a0cb7c56f0267e20fbf3c5b +endpoint=163.172.161.0:12912 +allowed_ip=0.0.0.0/0 +`) + err = dev.Up() + if err != nil { + log.Panic(err) + } + + socket, err := tnet.Dial("ping4", "zx2c4.com") + if err != nil { + log.Panic(err) + } + const payload = "gopher burrow" + socket.SetReadDeadline(time.Now().Add(time.Second * 10)) + start := time.Now() + _, err = socket.Write([]byte(payload)) + if err != nil { + log.Panic(err) + } + var reply [len(payload)]byte + n, err := socket.Read(reply[:]) + if err != nil || string(reply[:n]) != payload { + log.Panic(err) + } + log.Printf("Ping latency: %v", time.Since(start)) +} |