<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Whatever happened to Benjamin Ragheb? &#187; tcpdump</title>
	<atom:link href="http://www.benzado.com/blog/tags/tcpdump/feed" rel="self" type="application/rss+xml" />
	<link>http://www.benzado.com/blog</link>
	<description>A professional&#039;s personal blog.</description>
	<lastBuildDate>Mon, 11 Apr 2011 18:06:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Don&#8217;t printf when you can tcpdump</title>
		<link>http://www.benzado.com/blog/post/317/dont-printf-when-you-can-tcpdump</link>
		<comments>http://www.benzado.com/blog/post/317/dont-printf-when-you-can-tcpdump#comments</comments>
		<pubDate>Tue, 10 Nov 2009 23:04:20 +0000</pubDate>
		<dc:creator>Benjamin</dc:creator>
				<category><![CDATA[Nerdery]]></category>
		<category><![CDATA[tcpdump]]></category>

		<guid isPermaLink="false">http://www.benzado.com/blog/?p=317</guid>
		<description><![CDATA[Instead of using print statements to debug network communication, use tcpdump and get a complete and accurate picture of what's on the wire.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on an app that talks to a web service, and in the course of debugging it&#8217;s good to know what exactly is being sent to and from the web server. I had been doing so with the tried and true method of <a href="http://stackoverflow.com/questions/189562/what-is-the-proper-name-for-doing-debugging-by-adding-print-statements">printf() debugging</a> (this is Cocoa, so NSLog() debugging, actually), but it was getting to be a pain:</p>
<ol>
<li>If I print all network traffic all the time, it overwhelms my console, making it useless for any other kind of output. So instead, I&#8217;m constantly inserting or removing NSLog() statements as I work. Not to mention having to reproduce a request because the right logging statements weren&#8217;t in place the first time around.</li>
<li>NSURLConnection returns downloaded information as an NSData object, so simply passing it to NSLog() dumps a lot of useless hexadecimal code to the screen. That means I must first create an NSString from the data, print it, then release it. (I can&#8217;t use %s, the data isn&#8217;t null terminated.)</li>
<li>NSURLConnection does a lot of behind the scenes work, like storing cookies and setting Content-Length headers. That&#8217;s nice, but that makes it hard to know exactly what&#8217;s being sent on the wire.</li>
</ol>
<p>And that&#8217;s when it occurred to me: why not just watch what&#8217;s on the wire? tcpdump is a command line utility which monitors network traffic and prints out packets that you specify.</p>
<p>Here&#8217;s the incantation to monitor HTTP traffic to and from a specified host:</p>
<blockquote><p><code>sudo tcpdump -l -q -A "host <b>(Specified Host)</b> and tcp port 80 and (((ip[2:2] - ((ip[0]&#038;0xf)<<2)) - ((tcp[12]&#038;0xf0)>>2)) != 0)"</code></p></blockquote>
<p>To explain briefly, <code>sudo</code> runs the command as root, <code>-l</code> enables line buffering, <code>-q</code> hides some of the less interesting protocol information, and <code>-A</code> prints the content of each packet in ASCII. The filtering expression that follows selects packets to or from (Specified Host), to or from port 80, and ignoring SYN, FIN, ACK-only, and other non-data packets. I&#8217;ll confess, I don&#8217;t understand that last part completely, I copied it from the <a href="http://linux.die.net/man/8/tcpdump">tcpdump man page</a>.</p>
<p>To make the output a little easier to read, I pipe the output to a Perl script I quickly hacked together which watches for the packet header lines and outputs the <a href="http://isthe.com/chongo/tech/comp/ansi_escapes.html">ANSI escape codes</a> to render them in bold. But I&#8217;ll leave that as an exercise to you, dear reader.</p>
<p>There&#8217;s no need to download anything, tcpdump is already installed on your Mac. (I don&#8217;t know if it&#8217;s part of the standard install or the Developer Tools, but what do you care?)</p>
<p>Now, what would be <em>really</em> sweet is a graphical app to do this. I found <a href="http://www.tastycocoabytes.com/cpa/">Cocoa Packet Analyzer</a>, but it&#8217;s a little low-level for me. I don&#8217;t care about packet specifics; I&#8217;d prefer something that reconstructed the tcp streams in an easy to navigate way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benzado.com/blog/post/317/dont-printf-when-you-can-tcpdump/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: www.benzado.com @ 2012-02-04 20:16:43 by W3 Total Cache -->
