1

I am trying to write an ICMP ping program in C++ but I am getting following error with socket():

Socket error: Operation not permitted

The code snippet is:

sockfd = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
if ( sockfd == -1 )
{
  cerr << '\n' << "\nSocket error: " << strerror(errno) << endl;
  return 0;
}

I am running this on MacOS Mojave. Any help would be highly appreciated.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
bitm
  • 11
  • 3
  • 1
    Opening a raw socket requires privilege. Ordinary users cannot do it. This may be of interest to you: [How does macOS allow Standard users to ping?](https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping) – John Bollinger Apr 20 '20 at 02:56
  • Note also: it's not a *Linux* socket on macOS. It would be most conventional to just say "socket", but if you need to distinguish it from some other kind of socket then "network socket" or maybe "POSIX socket" would be more appropriate. – John Bollinger Apr 20 '20 at 02:58

1 Answers1

0

This worked on Debian. From https://superuser.com/questions/288521/problem-with-ping-open-socket-operation-not-permitted

    sudo setcap cap_net_raw=ep $(which ping)
rickfoosusa
  • 1,061
  • 19
  • 26