<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Enumerating Network Interfaces on Linux</title>
	<atom:link href="http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html</link>
	<description>Maker of Finest Quality Digital Things</description>
	<lastBuildDate>Sun, 28 Feb 2010 19:54:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14389</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Sun, 17 Jan 2010 22:05:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14389</guid>
		<description>Thanks Eric, it just so happens I was thinking about broadcast addresses today. I&#039;ve added your code to the example in the article.</description>
		<content:encoded><![CDATA[<p>Thanks Eric, it just so happens I was thinking about broadcast addresses today. I&#8217;ve added your code to the example in the article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14388</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Fri, 15 Jan 2010 16:56:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14388</guid>
		<description>Hi,
Great sample, thanks! Thought some of you might be interested in how to get broadcast address:
    if (ioctl(sock, SIOCGIFBRDADDR, &amp;ifr[nInterface]) &gt;= 0)   // rectify with &#039;item&#039; as above, as this chunk of code comes from my application
    	strcpy(szBroadcast, inet_ntoa(((struct sockaddr_in *)&amp;ifr[nInterface].ifr_broadaddr)-&gt;sin_addr));
So, to reply the  previous question: to get the subnet mask, I guess you&#039;d use the same ioctl call, passing SIOCGIFNETMASK as 2nd argument.
One note: some data returned by ioctl in the buffer (3rd argument) are part of a union, so one ioctl call might overwrite data of a previous call. So, make sure you retrieve the data you need, straight after each ioctl call
Cheers,
Eric</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Great sample, thanks! Thought some of you might be interested in how to get broadcast address:<br />
    if (ioctl(sock, SIOCGIFBRDADDR, &amp;ifr[nInterface]) &gt;= 0)   // rectify with &#8216;item&#8217; as above, as this chunk of code comes from my application<br />
    	strcpy(szBroadcast, inet_ntoa(((struct sockaddr_in *)&amp;ifr[nInterface].ifr_broadaddr)-&gt;sin_addr));<br />
So, to reply the  previous question: to get the subnet mask, I guess you&#8217;d use the same ioctl call, passing SIOCGIFNETMASK as 2nd argument.<br />
One note: some data returned by ioctl in the buffer (3rd argument) are part of a union, so one ioctl call might overwrite data of a previous call. So, make sure you retrieve the data you need, straight after each ioctl call<br />
Cheers,<br />
Eric</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lieven</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14313</link>
		<dc:creator>Lieven</dc:creator>
		<pubDate>Mon, 11 May 2009 19:09:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14313</guid>
		<description>Hi,

Is there a quick way, with these or similar methods to obtain the subnet mask of these interfaces ?

Thanks.
Lieven</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Is there a quick way, with these or similar methods to obtain the subnet mask of these interfaces ?</p>
<p>Thanks.<br />
Lieven</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Risi</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14256</link>
		<dc:creator>Adam Risi</dc:creator>
		<pubDate>Tue, 17 Feb 2009 06:05:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14256</guid>
		<description>Hi all,
I have written a fix for this code to allow it to use IPv6 interfaces, and to eliminate the segfault bug some users were experiencing. The new source can be found at &lt;a href=&quot;http://www.adamrisi.com/?p=84&quot; rel=&quot;nofollow&quot;&gt;my website&lt;/a&gt;.

HTH,
Adam Risi</description>
		<content:encoded><![CDATA[<p>Hi all,<br />
I have written a fix for this code to allow it to use IPv6 interfaces, and to eliminate the segfault bug some users were experiencing. The new source can be found at <a href="http://www.adamrisi.com/?p=84" rel="nofollow">my website</a>.</p>
<p>HTH,<br />
Adam Risi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prameeth Sreesha</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14161</link>
		<dc:creator>Prameeth Sreesha</dc:creator>
		<pubDate>Wed, 05 Nov 2008 06:42:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14161</guid>
		<description>In response to:
===========================================
Comment by Yiannis Tsiouris — 27 November 2007 @ 1:15 am

I have Arch Linux and the compiler i use is gcc 4.2.2. And this is the output:

[yiannis@main Programs]$ gcc -o interface interface.c
[yiannis@main Programs]$ ./interface
Segmentation fault
============================================

I came across the same problem too.. In my case, it was the query for the MAC Address field for a loopback device, that was the culprit. In the code, the lines below:

printf(&quot;%s: IP %s\n&quot;,
                       item-&gt;ifr_name,
                       inet_ntoa(((struct sockaddr_in *)&amp;item-&gt;ifr_addr)-&gt;sin_addr));


printf(&quot;, MAC %s\\n&quot;, ether_ntoa((struct ether_addr *)item-&gt;ifr_hwaddr.sa_data));

were the culprits in causing the segfaults. Since loopback device does not have a MAC value associated, it might have led to ifr_hwaddr being NULL. So the access to the sa_data might have led to the Segfault. I still need to go through the 2 structures though.

Commenting the above 2 printf&#039;s and just printing item-&gt;ifr_name works fine.

Regards,
Prameeth</description>
		<content:encoded><![CDATA[<p>In response to:<br />
===========================================<br />
Comment by Yiannis Tsiouris — 27 November 2007 @ 1:15 am</p>
<p>I have Arch Linux and the compiler i use is gcc 4.2.2. And this is the output:</p>
<p>[yiannis@main Programs]$ gcc -o interface interface.c<br />
[yiannis@main Programs]$ ./interface<br />
Segmentation fault<br />
============================================</p>
<p>I came across the same problem too.. In my case, it was the query for the MAC Address field for a loopback device, that was the culprit. In the code, the lines below:</p>
<p>printf(&#8220;%s: IP %s\n&#8221;,<br />
                       item-&gt;ifr_name,<br />
                       inet_ntoa(((struct sockaddr_in *)&amp;item-&gt;ifr_addr)-&gt;sin_addr));</p>
<p>printf(&#8220;, MAC %s\\n&#8221;, ether_ntoa((struct ether_addr *)item-&gt;ifr_hwaddr.sa_data));</p>
<p>were the culprits in causing the segfaults. Since loopback device does not have a MAC value associated, it might have led to ifr_hwaddr being NULL. So the access to the sa_data might have led to the Segfault. I still need to go through the 2 structures though.</p>
<p>Commenting the above 2 printf&#8217;s and just printing item-&gt;ifr_name works fine.</p>
<p>Regards,<br />
Prameeth</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erb</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14094</link>
		<dc:creator>Erb</dc:creator>
		<pubDate>Wed, 27 Aug 2008 16:55:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14094</guid>
		<description>Hmm that comment got messed up.  Should be
#include &lt;arpa/inet.h&gt;</description>
		<content:encoded><![CDATA[<p>Hmm that comment got messed up.  Should be<br />
#include &lt;arpa/inet.h&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erb</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14093</link>
		<dc:creator>Erb</dc:creator>
		<pubDate>Wed, 27 Aug 2008 16:54:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14093</guid>
		<description>to fix the seg fault, try #include 

also, that first printf should have a \n</description>
		<content:encoded><![CDATA[<p>to fix the seg fault, try #include </p>
<p>also, that first printf should have a \n</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14072</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Sat, 26 Jul 2008 12:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14072</guid>
		<description>Try the code mentioned in comment #11 above.</description>
		<content:encoded><![CDATA[<p>Try the code mentioned in comment #11 above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elessedil</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14071</link>
		<dc:creator>Elessedil</dc:creator>
		<pubDate>Fri, 25 Jul 2008 15:56:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14071</guid>
		<description>just figured that out. if no ip is assigned the interfaces are not found.
is there a possibility to get the list of interfaces even if they are not connected?</description>
		<content:encoded><![CDATA[<p>just figured that out. if no ip is assigned the interfaces are not found.<br />
is there a possibility to get the list of interfaces even if they are not connected?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elessedil</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14070</link>
		<dc:creator>Elessedil</dc:creator>
		<pubDate>Fri, 25 Jul 2008 15:49:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14070</guid>
		<description>Hello Adam,

i tried your code but unfortunately only the loopback interface is found, though the other two are up.
If you got an idea what could be the problem, please let me know.

thank you</description>
		<content:encoded><![CDATA[<p>Hello Adam,</p>
<p>i tried your code but unfortunately only the loopback interface is found, though the other two are up.<br />
If you got an idea what could be the problem, please let me know.</p>
<p>thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14046</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Sun, 29 Jun 2008 11:43:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14046</guid>
		<description>Thanks for spotting that ccspro!</description>
		<content:encoded><![CDATA[<p>Thanks for spotting that ccspro!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ccspro</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-14045</link>
		<dc:creator>ccspro</dc:creator>
		<pubDate>Sun, 29 Jun 2008 05:44:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-14045</guid>
		<description>the above example has a typo:
		colon = strchr (ame, &#039;:&#039;);
should be:
		colon = strchr (name, &#039;:&#039;);

It will compile.</description>
		<content:encoded><![CDATA[<p>the above example has a typo:<br />
		colon = strchr (ame, &#8216;:&#8217;);<br />
should be:<br />
		colon = strchr (name, &#8216;:&#8217;);</p>
<p>It will compile.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13984</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Mon, 31 Mar 2008 11:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13984</guid>
		<description>Yes it does only list the interfaces that are up.

Unfortunately, I don&#039;t think you can find every interface via the ioctl method so /proc/net/dev is probably your best bet for what you want.

/proc/net/dev is not so hard to parse. Here&#039;s some code which can do it - at least on Debian. I don&#039;t know if the format of the file is different on other distros:

&lt;pre&gt;/* listif.c - List all known interfaces on a Linux System */

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

main()
{
	char line[512];
	char *colon;
	char *name;
	FILE *fp;

	if(0 == (fp = fopen(&quot;/proc/net/dev&quot;, &quot;r&quot;)))
		return 1;
	
	while(0 != (name = fgets(line, 512, fp)))
	{
		while(isspace(name[0])) /* Trim leading whitespace */
			name++;

		colon = strchr (ame, &#039;:&#039;);
		if(colon)
		{
			*colon = 0;
			printf(&quot;%s:\n&quot;, name);
		}
	}

	fclose(fp);
	return 0;
}
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Yes it does only list the interfaces that are up.</p>
<p>Unfortunately, I don&#8217;t think you can find every interface via the ioctl method so /proc/net/dev is probably your best bet for what you want.</p>
<p>/proc/net/dev is not so hard to parse. Here&#8217;s some code which can do it &#8211; at least on Debian. I don&#8217;t know if the format of the file is different on other distros:</p>
<pre>/* listif.c - List all known interfaces on a Linux System */

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

main()
{
	char line[512];
	char *colon;
	char *name;
	FILE *fp;

	if(0 == (fp = fopen("/proc/net/dev", "r")))
		return 1;

	while(0 != (name = fgets(line, 512, fp)))
	{
		while(isspace(name[0])) /* Trim leading whitespace */
			name++;

		colon = strchr (ame, ':');
		if(colon)
		{
			*colon = 0;
			printf("%s:\n", name);
		}
	}

	fclose(fp);
	return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel Onoff</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13982</link>
		<dc:creator>Michel Onoff</dc:creator>
		<pubDate>Mon, 31 Mar 2008 08:39:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13982</guid>
		<description>This code shows only the *running* interfaces.

To get infos about *all* interfaces, I know of no other way than to read the file /proc/net/dev which, unfortunately, is not quite parse-friendly.</description>
		<content:encoded><![CDATA[<p>This code shows only the *running* interfaces.</p>
<p>To get infos about *all* interfaces, I know of no other way than to read the file /proc/net/dev which, unfortunately, is not quite parse-friendly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: marc</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13958</link>
		<dc:creator>marc</dc:creator>
		<pubDate>Fri, 29 Feb 2008 23:58:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13958</guid>
		<description>Thanks Adam! Worked right out of the box, unlike some of the other i tried.
Be proud that your code is a part of our firmware. :)</description>
		<content:encoded><![CDATA[<p>Thanks Adam! Worked right out of the box, unlike some of the other i tried.<br />
Be proud that your code is a part of our firmware. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13930</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Tue, 05 Feb 2008 09:43:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13930</guid>
		<description>Sorry Gautam, that&#039;s not really my area of expertise. Try doing a little research into routing information protocols such as RIP and OSPF.</description>
		<content:encoded><![CDATA[<p>Sorry Gautam, that&#8217;s not really my area of expertise. Try doing a little research into routing information protocols such as RIP and OSPF.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gautam Raut</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13929</link>
		<dc:creator>Gautam Raut</dc:creator>
		<pubDate>Tue, 05 Feb 2008 05:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13929</guid>
		<description>Can we fetch routing table from a router?
If yes then how to do it using C. How would u detect router in a subnet?
I have a code that detects router if that router has any virtual interface or a proxy ARP.
My email id  is gautam.raut.here@gmail.com</description>
		<content:encoded><![CDATA[<p>Can we fetch routing table from a router?<br />
If yes then how to do it using C. How would u detect router in a subnet?<br />
I have a code that detects router if that router has any virtual interface or a proxy ARP.<br />
My email id  is <a href="mailto:gautam.raut.here@gmail.com">gautam.raut.here@gmail.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gautam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13928</link>
		<dc:creator>Gautam</dc:creator>
		<pubDate>Tue, 05 Feb 2008 05:32:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13928</guid>
		<description>Thanks !!! it is very useful.</description>
		<content:encoded><![CDATA[<p>Thanks !!! it is very useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adam</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13900</link>
		<dc:creator>adam</dc:creator>
		<pubDate>Mon, 26 Nov 2007 22:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13900</guid>
		<description>Can&#039;t tell much from a segmentation fault. At least now I know it is failing when you run the utility. I kind of assumed from your last post that it was segfaulting during compilation. So here are some suggestions:

1. Segfaults are usually caused by a bad pointer or a buffer overflow. There is only one buffer in this code, so try changing buf[1024] to a much larger number (eg. buf[16384]) and see if that helps.

2. Compile the code with the debug option (gcc -g -o ifenum ifenum.c) and then step through it with a debugger such as kdbg. Then you can see what line is causing the problem.</description>
		<content:encoded><![CDATA[<p>Can&#8217;t tell much from a segmentation fault. At least now I know it is failing when you run the utility. I kind of assumed from your last post that it was segfaulting during compilation. So here are some suggestions:</p>
<p>1. Segfaults are usually caused by a bad pointer or a buffer overflow. There is only one buffer in this code, so try changing buf[1024] to a much larger number (eg. buf[16384]) and see if that helps.</p>
<p>2. Compile the code with the debug option (gcc -g -o ifenum ifenum.c) and then step through it with a debugger such as kdbg. Then you can see what line is causing the problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yiannis Tsiouris</title>
		<link>http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html/comment-page-1#comment-13899</link>
		<dc:creator>Yiannis Tsiouris</dc:creator>
		<pubDate>Mon, 26 Nov 2007 15:15:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.doctort.org/adam/nerd-notes/enumerating-network-interfaces-on-linux.html#comment-13899</guid>
		<description>I have Arch Linux and the compiler i use is gcc 4.2.2. And this is the output:

[yiannis@main Programs]$ gcc -o interface interface.c
[yiannis@main Programs]$ ./interface
Segmentation fault

A straight segmentation fault! (btw, a very ignoring error don&#039;t u think? :P)
Thank u for the quick answer! You have done an excellent work here! ;)</description>
		<content:encoded><![CDATA[<p>I have Arch Linux and the compiler i use is gcc 4.2.2. And this is the output:</p>
<p>[yiannis@main Programs]$ gcc -o interface interface.c<br />
[yiannis@main Programs]$ ./interface<br />
Segmentation fault</p>
<p>A straight segmentation fault! (btw, a very ignoring error don&#8217;t u think? :P)<br />
Thank u for the quick answer! You have done an excellent work here! ;)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
