ziggear
ziggear

Reputation: 944

how to get icmp on udp socket on UNIX

Getting raw sockets requires root privilege, and tcp/udp doesn't have it, so I need to know how to get a udp socket and fetch icmp data. The programming language is C and the OS is BSD-like.

(In other words I want to write a ping without root privilege)

Upvotes: 4

Views: 1597

Answers (3)

jean-loup
jean-loup

Reputation: 599

You can write an UDP ping without root privileges.

When the IP_RECVERR option is enabled, all errors are stored in the socket error queue, and can be received by recvmsg(2) with the MSG_ERRQUEUE flag set.

See the UDP manual.

I assume the forge&send routine is already implemented on a SOCK_DGRAM socket. Then, to access the source addresses of the ICMP messages:

  • Set the socket options to receive errors (IP_RECVERR)
  • Make a call to recvmsg() on the error queue (MSG_ERRQUEUE)
  • Parse the returned structures (msghdr and iovec), it contains the source addresses of ICMP issuers.

Upvotes: 3

fuz
fuz

Reputation: 92976

UDP is an OSI layer four protocoll, so is ICMP. Thereby, you can't implement ICMP on an UDP socket since its layer four protocoll is already fixed to UDP. You can consider writing an implementation that requires the privilege to open a raw socket. Then you either give the application or your account the right capability or flip the executables SUID bit to make it run as root.

Upvotes: 0

unwind
unwind

Reputation: 399793

So, uh, it it was trivial to go around the requirement, do you think it would still be there?

It's meant to provide some security, so it's not super-easy to go around.

I don't think it can be done.

Upvotes: -2

Related Questions