Update sample code, README, and circle v2 (#42)

Update sample code, README, and circle v2
This commit is contained in:
Cameron Sparr 2018-11-06 12:45:58 +00:00 committed by GitHub
parent 667d0a66f2
commit 74b9e0e9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 15 deletions

21
.circleci/config.yml Normal file
View File

@ -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

View File

@ -25,6 +25,15 @@ if err != nil {
panic(err) 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) { pinger.OnRecv = func(pkt *ping.Packet) {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) 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): (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/) 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. 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.

View File

@ -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

View File

@ -3,6 +3,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"os/signal"
"time" "time"
"github.com/sparrc/go-ping" "github.com/sparrc/go-ping"
@ -53,6 +55,15 @@ func main() {
return 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) { pinger.OnRecv = func(pkt *ping.Packet) {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)