Ping in C
Prerequisites : ICMP | Raw Socket | Internet Checksum | DNS Ping is a necessity for debugging of the Internet. Ping is a basic Internet tool that allows a user to verify that a particular IP address exists and can accept requests., with other facilities. Ping sends out ICMP packets by opening a RAW socket, which is separate from TCP and UDP. Since IP does not have any inbuilt mechanism for sending error and control messages. It depends on Internet Control Message Protocol (ICMP) to provide an error control. It is used for reporting errors and management queries. Example of Ubuntu Ping
ping www.google.com PING www.google.com (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=1 ttl=46 time=116 ms 64 bytes from 220.127.116.11 (18.104.22.168): icmp_seq=2 ttl=46 time=102 ms 64 bytes from 22.214.171.124 (126.96.36.199): icmp_seq=3 ttl=46 time=119 ms ^C --- www.google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 3110ms
Working Mechanism The Internet Ping program works much like a sonar echo-location, sending a small packet of information containing an ICMP ECHO_REQUEST to a specified computer, which then sends an ECHO_REPLY packet in return. The packet has a TTL (time-to-live) value determining max number of router hops. If the packet does not reach, then the sender is noted back with the error. Errors are of following types:
- TTL Expired in Transit
- Destination Host Unreachable
- Request Timed Out i.e. no reply
- Unknown Host
- Take a hostname as input
- Do a DNS lookup
DNS lookup can be done using gethostbyname(). The gethostbyname() function converts a normal human readable website and returns a structure of type hostent which contains IP address in form of binary dot notation and also address type.
- Some ping programs like the one given with ubuntu support reverse DNS lookup. Reverse DNS lookup is performed using getnameinfo(), and it converts dot notation IP address to hostname. for example, the pinging of google.com frequently gives a strange address: bom07s18-in-f14.1e100.net This is as a result of a reverse DNS lookup.
- Open a Raw socket using SOCK_RAW with protocol as IPPROTO_ICMP. Note: raw socket requires superuser rights so you have to run this code using sudo
- When crtl + C is pressed, ping gives a report. This interrupt is caught by an interrupt handler which just sets our pinging looping condition to false.
- Here comes the main ping sending loop. We have to:
- Set the ttl option to a value in the socket TTL value is set to limit the number of hops a packet can make.
- Set the timeout of the recv function If timeout is not set, recv will wait forever, halting the loop.
- Fill up the icmp packet As follows:
- Set packet header type to ICMP_ECHO.
- Set id to pid of process
- Fill msg part randomly.
- Calculate checksum and fill it in checksum field.
- Send the packet
- Wait for it to be received. The main problem here is that the packet received does not mean that the destination is working. Echo reply means destination is OK. Echo reply is sent from destination OS kernel. This is the list of all types and codes. A issue here is that the program shows type 69 and code 0 if all goes correct instead of 0 which stands for echo_reply.
- An example output: Run sudo ./ping google.com
Resolving DNS.. Trying to connect to 'google.com' IP: 188.8.131.52 Reverse Lookup domain: bom07s15-in-f14.1e100.net Socket file descriptor 3 received Socket set to TTL.. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (184.108.40.206) msg_seq=1 ttl=64 rtt = 57.320584 ms. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (220.127.116.11) msg_seq=2 ttl=64 rtt = 58.666775 ms. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (18.104.22.168) msg_seq=3 ttl=64 rtt = 58.081148 ms. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (22.214.171.124) msg_seq=4 ttl=64 rtt = 58.700630 ms. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (126.96.36.199) msg_seq=5 ttl=64 rtt = 58.281802 ms. 64 bytes from bom07s15-in-f14.1e100.net (h: google.com) (188.8.131.52) msg_seq=6 ttl=64 rtt = 58.360916 ms. ===184.108.40.206 ping statistics=== 6 packets sent, 6 packets received, 0.000000 percent packet loss. Total time: 6295.187804 ms.