go-ping / ping

ICMP Ping library for Go
MIT License
1.31k stars 344 forks source link

listen ip4:icmp : socket: operation not permitted macOS Monterey #193

Closed gerkra closed 1 year ago

gerkra commented 2 years ago

Hello!

My code:

        path := fmt.Sprintf(`%v`, IPAddr)
    pinger, err := ping.NewPinger(path)

    if err != nil {
        return err
    }

    pinger.SetPrivileged(true)
    pinger.Count = environments.StatsPingCount
    pinger.Timeout = environments.TimeOutStatsPing

    err = pinger.Run()

    log.Println(err)

    if err != nil {
        return err
    }

    stats := pinger.Statistics()

    if stats.PacketsRecv == 0 {
        return errors.New("ping error")
    }

    return nil

I have next error when run code: socket: operation not permitted.

OS: macOS Monterey

CHTJonas commented 2 years ago

Have you tried running as root?

gerkra commented 2 years ago

Yes, process is freezy

CHTJonas commented 2 years ago

What do you mean by "freezy"?

Could you include complete sample code that reproduces the issue, rather than just a snippet? Thanks!

gerkra commented 2 years ago

Hello!

func (i *Object) Ping() error {
    ip, err := net.ResolveIPAddr("ip4", i.IPAddr)

    if err != nil {
        return err
    }

    pinger, err := ping.NewPinger(ip.String())

    if err != nil {
        return err
    }

    pinger.SetIPAddr(ip)
    pinger.SetPrivileged(true)
    pinger.Count = environments.StatsPingCount
    pinger.Timeout = environments.TimeOutStatsPing
    pinger.Interval = 300 * time.Millisecond

    err = pinger.Run()

    if err != nil {
        errStr := fmt.Sprintf(`Ping %v error: %v`, i.IPAddr, err.Error())
        return errors.New(errStr)
    }

    stats := pinger.Statistics()

    if stats.PacketsRecv == 0 {
        errStr := fmt.Sprintf(`Ping %v error: PacketsRecv = 0`, i.IPAddr)
        return errors.New(errStr)
    }

    return nil
}
gerkra commented 2 years ago

In OS windows I have next problem:

read ip4 0.0.0.0: wsarecvfrom: A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.

Can You help? Maybe I must change something on host?

CHTJonas commented 2 years ago

The last bit is a dupe of #168.

stanimirivanovde commented 2 years ago

On Mac OS you need to pass privileged as false: pinger.SetPrivileged(false)

CHTJonas commented 2 years ago

Really? Is this a Monterey thing? I'm on Big Sur and don't experience this:

$ uname -v
Darwin Kernel Version 20.6.0: Tue Feb 22 21:10:41 PST 2022; root:xnu-7195.141.26~1/RELEASE_X86_64
$ sudo go run cmd/ping/ping.go --privileged www.google.com
PING www.google.com (142.250.187.196):
32 bytes from 142.250.187.196: icmp_seq=0 time=7.062ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=1 time=7.007ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=2 time=8.108ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=3 time=6.64ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=4 time=7.199ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=5 time=6.979ms ttl=119
^C
--- www.google.com ping statistics ---
6 packets transmitted, 6 packets received, 0 duplicates, 0% packet loss
round-trip min/avg/max/stddev = 6.64ms/7.165834ms/8.108ms/453.908µs
$ go run cmd/ping/ping.go www.google.com
PING www.google.com (142.250.187.196):
32 bytes from 142.250.187.196: icmp_seq=0 time=7.081ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=1 time=7.198ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=2 time=7.815ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=3 time=7.115ms ttl=119
32 bytes from 142.250.187.196: icmp_seq=4 time=6.976ms ttl=119
^C
--- www.google.com ping statistics ---
5 packets transmitted, 5 packets received, 0 duplicates, 0% packet loss
round-trip min/avg/max/stddev = 6.976ms/7.237ms/7.815ms/297.639µs
stanimirivanovde commented 2 years ago

This is on macOS Monterey 12.3.1:

uname -v
Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64

With privileged flag:

go run cmd/ping/ping.go --privileged 1.1.1.1
PING 1.1.1.1 (1.1.1.1):
Failed to ping target host: listen ip4:icmp : socket: operation not permitted

This is without the privileged flag

 go run cmd/ping/ping.go 1.1.1.1
PING 1.1.1.1 (1.1.1.1):
2022/04/25 16:38:50 DEBUG: Setting the timeout to: 27h46m40s
2022/04/25 16:38:50 DEBUG: Setting the interval to: 1s
2022/04/25 16:38:50 DEBUG: Done with timeout
2022/04/25 16:38:50 DEBUG: Done with interval
32 bytes from 1.1.1.1: icmp_seq=0 time=19.303ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=1 time=17.288ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=2 time=35.344ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=3 time=15.612ms ttl=248

This is running with sudo:

sudo go run cmd/ping/ping.go --privileged 1.1.1.1
Password:
PING 1.1.1.1 (1.1.1.1):
2022/04/25 16:41:12 DEBUG: Setting the timeout to: 27h46m40s
2022/04/25 16:41:12 DEBUG: Setting the interval to: 1s
2022/04/25 16:41:12 DEBUG: Done with timeout
2022/04/25 16:41:12 DEBUG: Done with interval
32 bytes from 1.1.1.1: icmp_seq=0 time=28.419ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=1 time=74.463ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=2 time=24.24ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=3 time=34.363ms ttl=248
32 bytes from 1.1.1.1: icmp_seq=4 time=24.302ms ttl=248
^C
--- 1.1.1.1 ping statistics ---