{"id":2,"date":"2005-11-16T15:40:02","date_gmt":"2005-11-16T02:40:02","guid":{"rendered":"http:\/\/www.meta.net.nz\/~daniel\/blog\/?p=2"},"modified":"2006-04-07T14:05:23","modified_gmt":"2006-04-07T01:05:23","slug":"printing-ip-addresses-in-c","status":"publish","type":"post","link":"https:\/\/www.meta.net.nz\/~daniel\/blog\/2005\/11\/16\/printing-ip-addresses-in-c\/","title":{"rendered":"Printing IP addresses in C"},"content":{"rendered":"<p>Printing out IP addresses in C isn&#8217;t too hard- you can use inet_ntoa for easy results. inet_ntoa uses a static buffer however, so trying to use it more than once in the same printf statement causes problems.  You can split each inet_ntoa call into a new printf, or you can write some macros to do the job<\/p>\n<p>[code lang=&#8221;cpp&#8221;]<\/p>\n<p>#define IPFMT &#8220;%i.%i.%i.%i&#8221;<br \/>\n#define IP(a) ((ntohl(a.s_addr) >> 24) &#038; 0xff),\\<br \/>\n((ntohl(a.s_addr) >> 16) &#038; 0xff),\\<br \/>\n((ntohl(a.s_addr) >> 8) &#038; 0xff),\\<br \/>\n(ntohl(a.s_addr) &#038; 0xff) [\/code]<\/p>\n<p>You might use this like:<br \/>\n[code lang=&#8221;cpp&#8221;]<br \/>\nprintf(IPFMT &#8221; -> &#8221; IPFMT &#8220;\\n&#8221;,\tIP(ipptr->ip_src), IP(ipptr->ip_dst));<br \/>\n[\/code]<\/p>\n<p>The preprocessor turns this into something like the following:<\/p>\n<p>[code lang=&#8221;cpp&#8221;]<\/p>\n<p>printf(&#8220;%i.%i.%i.%i&#8221; &#8221; -> &#8221; &#8220;%i.%i.%i.%i&#8221; &#8220;\\n&#8221;,<br \/>\n((ntohl(ipptr->ip_src.s_addr) >> 24) &#038; 0xff),<br \/>\n((ntohl(ipptr->ip_src.s_addr) >> 16) &#038; 0xff),<br \/>\n((ntohl(ipptr->ip_src.s_addr) >> 8) &#038; 0xff),<br \/>\n(ntohl(ipptr->ip_src.s_addr) &#038; 0xff),<br \/>\n((ntohl(ipptr->ip_dst.s_addr) >> 24) &#038; 0xff),<br \/>\n((ntohl(ipptr->ip_dst.s_addr) >> 16) &#038; 0xff),<br \/>\n((ntohl(ipptr->ip_dst.s_addr) >> 8) &#038; 0xff),<br \/>\n(ntohl(ipptr->ip_dst.s_addr) &#038; 0xff));<br \/>\n[\/code]<\/p>\n<p>Looks a bit messy, and the IPFMT macro might throw a few people off at first, but anyone familiar with PRIu64 and the like should manage it ok. I&#8217;ll probably through this into libtrace at some point, as a helper function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some macros to print IP addresses in C<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/posts\/2"}],"collection":[{"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/comments?post=2"}],"version-history":[{"count":0,"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/posts\/2\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/media?parent=2"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/categories?post=2"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.meta.net.nz\/~daniel\/blog\/wp-json\/wp\/v2\/tags?post=2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}