diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..aa9c855 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,21 @@ +version: 2.0 +jobs: + build: + docker: + - image: circleci/golang:1.11 + working_directory: /go/src/github.com/sparrc/go-ping + steps: + - checkout + - run: + name: Build and run some basic tests + command: | + go get ./... + go build -race -o ping_linux ./cmd/ping/ping.go + sudo ./ping_linux --privileged -c 2 www.google.com + sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com + sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com + GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go + - store_artifacts: + path: ./ping_linux + - store_artifacts: + path: ./ping_darwin diff --git a/README.md b/README.md index 81b557c..f6b828b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ if err != nil { panic(err) } +// listen for ctrl-C signal +c := make(chan os.Signal, 1) +signal.Notify(c, os.Interrupt) +go func() { + for _ = range c { + pinger.Stop() + } +}() + pinger.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) @@ -75,13 +84,19 @@ use setcap to allow your binary using go-ping to bind to raw sockets (or just run as super-user): ``` -setcap cap_net_raw=+ep /bin/goping-binary +setcap cap_net_raw=+ep /bin/go-ping ``` -## Note on Windows Support: - -You have to use "SetPrivileged(true)" like "pinger.SetPrivileged(true)", otherwise you will receive an error: -"Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists." This even works without admin privileges. Tested on Windows 10 - See [this blog](https://sturmflut.github.io/linux/ubuntu/2015/01/17/unprivileged-icmp-sockets-on-linux/) and [the Go icmp library](https://godoc.org/golang.org/x/net/icmp) for more details. + +## Note on Windows Support: + +You must use `pinger.SetPrivileged(true)`, otherwise you will receive an error: + +``` +Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists. +``` + +This should without admin privileges. Tested on Windows 10. + diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 432c856..0000000 --- a/circle.yml +++ /dev/null @@ -1,9 +0,0 @@ -test: - post: - - go build -race -o ping_linux ./cmd/ping/ping.go - - sudo ./ping_linux --privileged -c 2 www.google.com - - sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com - - sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com - - GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go - - mv ping_linux $CIRCLE_ARTIFACTS - - mv ping_darwin $CIRCLE_ARTIFACTS diff --git a/cmd/ping/ping.go b/cmd/ping/ping.go index 77d6c23..5a3dbf7 100644 --- a/cmd/ping/ping.go +++ b/cmd/ping/ping.go @@ -3,6 +3,8 @@ package main import ( "flag" "fmt" + "os" + "os/signal" "time" "github.com/sparrc/go-ping" @@ -53,6 +55,15 @@ func main() { return } + // listen for ctrl-C signal + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for _ = range c { + pinger.Stop() + } + }() + pinger.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)