简体   繁体   中英

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)

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM