<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>getifaddrs &#8211; Wade Tregaskis</title>
	<atom:link href="https://wadetregaskis.com/tags/getifaddrs/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com</link>
	<description></description>
	<lastBuildDate>Thu, 27 Apr 2023 19:25:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wadetregaskis.com/wp-content/uploads/2016/03/Stitch-512x512-1-256x256.png</url>
	<title>getifaddrs &#8211; Wade Tregaskis</title>
	<link>https://wadetregaskis.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">226351702</site>	<item>
		<title>getifaddrs never specifies broadcast addresses</title>
		<link>https://wadetregaskis.com/getifaddrs-never-specifies-broadcast-addresses/</link>
					<comments>https://wadetregaskis.com/getifaddrs-never-specifies-broadcast-addresses/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Thu, 27 Apr 2023 19:25:36 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Bugs!]]></category>
		<category><![CDATA[getifaddrs]]></category>
		<category><![CDATA[macOS]]></category>
		<category><![CDATA[Undocumented]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=5250</guid>

					<description><![CDATA[Apple &#8220;Feedback&#8221; #12149764. According to man 3 getifaddrs: The ifa_dstaddr field references the destination address on a P2P interface, if one exists, otherwise it contains the broadcast address. In my testing the ifa_dstaddr field is never non-null. I&#8217;m not sure I have any suitably configured P2P interfaces, but I definitely have interfaces with broadcast capabilities&#8230; <a class="read-more-link" href="https://wadetregaskis.com/getifaddrs-never-specifies-broadcast-addresses/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Apple &#8220;Feedback&#8221; #12149764.</p>



<p>According to <code>man 3 getifaddrs</code>:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>The ifa_dstaddr field references the destination address on a P2P interface, if one exists, otherwise it contains the broadcast address.</p>
</blockquote>



<p>In my testing the <code>ifa_dstaddr</code> field is never non-null.  I&#8217;m not sure I have any suitably configured P2P interfaces, but I definitely have interfaces with broadcast capabilities and <code>getifaddrs</code> reports them as having no broadcast address (even though it correctly notes they support broadcasting, in <code>ifa_flags</code>).</p>



<p>This is on macOS Ventura 13.3.1.  I have no idea if this applies to other versions of macOS, or other OS&#8217;s.</p>



<p>It appears there&#8217;s a workaround, though &#8211; the broadcast address is deterministic based on the IP address &amp; netmask, <a rel="noreferrer noopener external" href="https://en.wikipedia.org/wiki/Broadcast_address#IP_networking" data-type="URL" data-id="https://en.wikipedia.org/wiki/Broadcast_address#IP_networking" target="_blank" data-wpel-link="external">according to Wikipedia</a>, so you don&#8217;t technically need <code>getifaddrs</code> to spell it out for you.  That&#8217;s for AF_INET (IPv4) networks.  IPv6 doesn&#8217;t really have the same notion of a broadcast address anyway, and for other network types I have no idea if the concept applies or if there&#8217;s workarounds for this <code>getifaddr</code> bug.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/getifaddrs-never-specifies-broadcast-addresses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5250</post-id>	</item>
		<item>
		<title>getifaddrs returns truncated sockaddr_in&#8217;s for AF_INET ifa_netmasks</title>
		<link>https://wadetregaskis.com/getifaddrs-returns-truncated-sockaddr_ins-for-af_inet-ifa_netmasks/</link>
					<comments>https://wadetregaskis.com/getifaddrs-returns-truncated-sockaddr_ins-for-af_inet-ifa_netmasks/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Thu, 27 Apr 2023 18:57:02 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Bugs!]]></category>
		<category><![CDATA[getifaddrs]]></category>
		<category><![CDATA[macOS]]></category>
		<category><![CDATA[Undocumented]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=5247</guid>

					<description><![CDATA[Apple &#8220;Feedback&#8221; #12149675. Some netmasks returned by getifaddrs have family of AF_INET yet a length less than sizeof(sockaddr_in), e.g. 5, 6, 7, or 8. On macOS Ventura 13.3.1, at least. It looks like it&#8217;s actually allocating only eight bytes for the ifa_netmask (not the 16 that is the size of sockaddr_in per MacOSX13.3.sdk/usr/include/netinet/in.h), as it&#8230; <a class="read-more-link" href="https://wadetregaskis.com/getifaddrs-returns-truncated-sockaddr_ins-for-af_inet-ifa_netmasks/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Apple &#8220;Feedback&#8221; #12149675.</p>



<p>Some netmasks returned by <code>getifaddrs</code> have family of <code>AF_INET</code> yet a length less than <code>sizeof(sockaddr_in)</code>, e.g. 5, 6, 7, or 8.  On macOS Ventura 13.3.1, at least.</p>



<p>It looks like it&#8217;s actually allocating only eight bytes for the <code>ifa_netmask</code> (not the 16 that is the size of <code>sockaddr_in</code> per <code>MacOSX13.3.sdk/usr/include/netinet/in.h</code>), as it clearly has a different <code>sockaddr_in</code> immediately after it, e.g.:</p>



<pre class="wp-block-preformatted">sockaddr
  - sa_len : 5
  - sa_family : 2
  ▿ sa_data : 14 elements
    - .0 : 0
    - .1 : 0
    - .2 : 255
    - .3 : 0
    - .4 : 0
    - .5 : 0
    - .6 : 16 // This is obviously the start of the next sockaddr_in; a correct entry with length of 16.
    - .7 : 2
    - .8 : 0
    - .9 : 0
    - .10 : 127
    - .11 : 0
    - .12 : 0
    - .13 : 1</pre>



<p>It&#8217;s thus clear that it&#8217;s both wrong about the actual length in memory and it is indeed truncating the <code>sockaddr_in</code> structure, although it looks like it&#8217;s actually including the full address (the lo0 netmask in the above example is indeed 255.0.0.0).</p>



<p>Here&#8217;s another example, where the netmask is 255.255.255.0:</p>



<pre class="wp-block-preformatted">▿ sockaddr
  - sa_len : 7
  - sa_family : 2
  ▿ sa_data : 14 elements
    - .0 : 0
    - .1 : 0
    - .2 : 255
    - .3 : 255
    - .4 : 255
    - .5 : 0
    - .6 : 16 // Again, clearly the start of the next sockaddr_in.
    - .7 : 2
    - .8 : 0
    - .9 : 0
    - .10 : 192
    - .11 : 168
    - .12 : 0
    - .13 : 24</pre>



<p>So you can see it&#8217;s:</p>



<ul class="wp-block-list">
<li>Only allocating up to the address of <code>sockaddr_in</code>, not counting the padding (so <code>sizeof(sockaddr_in)</code>, <code>MemoryLayout&lt;sockaddr_in>.size</code>, etc disagree with what <code>getifaddrs</code> is doing), and</li>



<li>Weirdly truncating the stated length (<code>sin_len</code>) to the end of the non-zero bytes in the netmask.</li>
</ul>



<p>This is undocumented (well, all these data structures are completely undocumented 😔) and is all very messy.  <code>getifaddrs</code> should just provide the full <code>sockaddr_in</code> without any of this weirdness, so that users which blindly cast to <code>sockaddr_in</code> (without checking <code>sin_len</code>) don&#8217;t risk segmentation faults or reading corrupt data.</p>



<p>I have no idea why it does this <em>only</em> for <code>ifa_netmask</code>, not <code>ifa_addr</code> or <code>ifa_dstaddr</code>.  That discrepancy just makes it even more troublesome.</p>



<p>As far as I can tell there are no similar issues with <code>AF_INET6</code> netmasks, although without knowing under what criteria it&#8217;s truncating, I can&#8217;t be sure I have all relevant examples covered on the happenstance networks of my laptop.</p>



<p>For these systems, there&#8217;s seemingly not much you can do… it seems you have to hard-code the assumption that <code>ifa_netmask</code> contains exactly &amp; only the first eight bytes of the <code>sockaddr_in</code>.  You have to completely ignore <code>sin_len</code> (at least if it&#8217;s less than eight).</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/getifaddrs-returns-truncated-sockaddr_ins-for-af_inet-ifa_netmasks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5247</post-id>	</item>
	</channel>
</rss>
