<?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: C Program to Get IP Address from Interface Name on Linux	</title>
	<atom:link href="https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/</link>
	<description>News, Technology, Entertainment and more</description>
	<lastBuildDate>Sun, 18 Dec 2022 09:31:42 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
	<item>
		<title>
		By: melson		</title>
		<link>https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/comment-page-1/#comment-348306</link>

		<dc:creator><![CDATA[melson]]></dc:creator>
		<pubDate>Sun, 18 Dec 2022 09:31:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=1462#comment-348306</guid>

					<description><![CDATA[is that possible to print interface if i give IP address with subnet mask . Ex 192.168.0.1/24 should print interface]]></description>
			<content:encoded><![CDATA[<p>is that possible to print interface if i give IP address with subnet mask . Ex 192.168.0.1/24 should print interface</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dmitry Saychenko		</title>
		<link>https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/comment-page-1/#comment-306582</link>

		<dc:creator><![CDATA[Dmitry Saychenko]]></dc:creator>
		<pubDate>Thu, 07 Jan 2021 13:36:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=1462#comment-306582</guid>

					<description><![CDATA[In Ubuntu 18.04 i get strange results:
dmitry@dmitry-lenovo:~/test/c$ uname -a
Linux dmitry-lenovo 5.4.0-58-generic #64~18.04.1-Ubuntu SMP Wed Dec 9 17:11:11 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Random results:
dmitry@dmitry-lenovo:~/test/c$ gcc getipaddr.c 
dmitry@dmitry-lenovo:~/test/c$ ./a.out
180.85.0.0
dmitry@dmitry-lenovo:~/test/c$ ./a.out
3.86.0.0
dmitry@dmitry-lenovo:~/test/c$ ./a.out
205.85.0.0
If fill if_req structure with zeroes:

 fd = socket(AF_INET, SOCK_DGRAM, 0);

 memset(&#038;ifr, 0, sizeof(struct ifreq));

 /* I want to get an IPv4 IP address */
 ifr.ifr_addr.sa_family = AF_INET;

I get:
dmitry@dmitry-lenovo:~/test/c$ ./a.out
0.0.0.0]]></description>
			<content:encoded><![CDATA[<p>In Ubuntu 18.04 i get strange results:<br />
dmitry@dmitry-lenovo:~/test/c$ uname -a<br />
Linux dmitry-lenovo 5.4.0-58-generic #64~18.04.1-Ubuntu SMP Wed Dec 9 17:11:11 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux<br />
Random results:<br />
dmitry@dmitry-lenovo:~/test/c$ gcc getipaddr.c<br />
dmitry@dmitry-lenovo:~/test/c$ ./a.out<br />
180.85.0.0<br />
dmitry@dmitry-lenovo:~/test/c$ ./a.out<br />
3.86.0.0<br />
dmitry@dmitry-lenovo:~/test/c$ ./a.out<br />
205.85.0.0<br />
If fill if_req structure with zeroes:</p>
<p> fd = socket(AF_INET, SOCK_DGRAM, 0);</p>
<p> memset(&amp;ifr, 0, sizeof(struct ifreq));</p>
<p> /* I want to get an IPv4 IP address */<br />
 ifr.ifr_addr.sa_family = AF_INET;</p>
<p>I get:<br />
dmitry@dmitry-lenovo:~/test/c$ ./a.out<br />
0.0.0.0</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Allen Edwards		</title>
		<link>https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/comment-page-1/#comment-75742</link>

		<dc:creator><![CDATA[Allen Edwards]]></dc:creator>
		<pubDate>Sun, 01 May 2016 04:30:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=1462#comment-75742</guid>

					<description><![CDATA[Finally someone posted some code that actually works.  Thank you so much.  This is such a straightforward question that receives such complicated gobbledegook on Stackoverflow.  I have been looking for this for a long time.  Great job.

I don&#039;t understand the comment above about strcpy().  Hope it isn&#039;t a problem.]]></description>
			<content:encoded><![CDATA[<p>Finally someone posted some code that actually works.  Thank you so much.  This is such a straightforward question that receives such complicated gobbledegook on Stackoverflow.  I have been looking for this for a long time.  Great job.</p>
<p>I don&#8217;t understand the comment above about strcpy().  Hope it isn&#8217;t a problem.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: cordeg		</title>
		<link>https://www.binarytides.com/c-program-to-get-ip-address-from-interface-name-on-linux/comment-page-1/#comment-66025</link>

		<dc:creator><![CDATA[cordeg]]></dc:creator>
		<pubDate>Tue, 18 Mar 2014 00:49:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=1462#comment-66025</guid>

					<description><![CDATA[This code makes a common mistake in assuming that strncpy() guarantees that the resulting string is null-terminated.  In fact, it only does so if the string is shorter than the length specified (i.e., IFNAMSIZ-1); otherwise, it simply copies up to the last character position that fits (i.e., ifr.ifr_name[IFNAMSIZ-2]) and stops -- no &#039;&#039; is inserted.  (This may seem stupid to the younger readers, but the purpose of the strncpy() function was to support an erstwhile popular use of fixed-length &quot;string buffer&quot; storage where null terminators were considered a waste of a perfectly good space in a message, for example.  In any case, the C standard never required the guarantee of a null-terminator on truncated strings as a result, though some compiler vendors implemented it that way by choice, leading some to believe the behavior was part of the function definition.)  Of course, in this example, the string is a fixed &quot;eth0&quot;, so the code works just fine, but obviously the code uses strncpy() rather than strcpy() to cover the general case where the ifname string length is unknown and could exceed the IFNAMSIZ limit.  As long as this is theoretically possible, the code should ensure null-termination by following up the strncpy() with a manual termination (e.g., &quot;ifr.ifr_name[IFNAMSIZ-1] = &#039;&#039;;).]]></description>
			<content:encoded><![CDATA[<p>This code makes a common mistake in assuming that strncpy() guarantees that the resulting string is null-terminated.  In fact, it only does so if the string is shorter than the length specified (i.e., IFNAMSIZ-1); otherwise, it simply copies up to the last character position that fits (i.e., ifr.ifr_name[IFNAMSIZ-2]) and stops &#8212; no &#8221; is inserted.  (This may seem stupid to the younger readers, but the purpose of the strncpy() function was to support an erstwhile popular use of fixed-length &#8220;string buffer&#8221; storage where null terminators were considered a waste of a perfectly good space in a message, for example.  In any case, the C standard never required the guarantee of a null-terminator on truncated strings as a result, though some compiler vendors implemented it that way by choice, leading some to believe the behavior was part of the function definition.)  Of course, in this example, the string is a fixed &#8220;eth0&#8221;, so the code works just fine, but obviously the code uses strncpy() rather than strcpy() to cover the general case where the ifname string length is unknown and could exceed the IFNAMSIZ limit.  As long as this is theoretically possible, the code should ensure null-termination by following up the strncpy() with a manual termination (e.g., &#8220;ifr.ifr_name[IFNAMSIZ-1] = &#8221;;).</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
