Where is the source code for ping?

Sunday, July 26, 2020

Lately, I've been working on implementing ping on my own as a project to keep learning Rust and to deepen my knowledge of networks. I'm just going for a super basic utility here, nothing fancy, not even all the features of ping. But since the language is new to me and my lower-level network knowledge is weak, I decided that it could be helpful to compare notes, so to speak, with the real deal itself. So that's our question: where is the source code for the actual utility ping?

Let's find out! I'm running Ubuntu, so the question is where do my binary packages come from and where does the corresponding source live? Naively, I expected this to be super easy to find. It's not hard to find, for my system (Ubuntu), but it's not as easy as it would be on some others like Gentoo.

The first step I took was, naturally, to turn to Google and search "ping source code". The first search result is a GitHub gist, which links to a US Army page written by the original author of ping. Cool, so this is seems like the original source! This is really cool and a great historical artifact. Is this the same version that I'm running on my desktop, though? We need to dig deeper and see what's running on the local machine.

If we use man ping to look at the manual page for ping, we see "System Manager's Manual: iputils" at the top, which is our first hint at where ping comes from on our system: possibly the package is named iputils, and I do have a package named iputils-ping installed. From here, we can find the source package and... the links on that page to the Debian git repos are broken. Sigh.

Back we go to Google and we find the source package for iputils on Debian, figuring it's probably the same. And now we're in luck, and we can get to the iputils git repo that Ubuntu presumably draws from by way of Debian: https://salsa.debian.org/debian/iputils

And thus we find the source: https://salsa.debian.org/debian/iputils/-/blob/master/ping.c and https://salsa.debian.org/debian/iputils/-/blob/master/ping.h

It clocks in at a total of approximately 2k lines of code (well, there is also shared code), which is not much bigger than the original source. It's a marvel to me that this gem of software has stayed small, concise, and useful for decades without acquiring much bloat, without changing forms. May more software be like ping.

Now that we have the source, there's a lot more to learn. For example, if you receive pings intended for another process (because that happens with raw sockets, it turns out), you can setup a filter with Berkeley Packet Filter, and ignore any pings that aren't for you! This is really cool and something that I need to learn more about.

What other gems am I missing out there?

If you have comments or find errors, please email me!

For updates when I post new things, I have a lovely RSS feed you can use, or you can sign up for my newsletter, where I send out my latest posts and other great software engineering writing I've read.