<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>funcptr - macOS</title>
    <subtitle>An engineer&#x27;s technical notebook</subtitle>
    <link rel="self" type="application/atom+xml" href="https://funcptr.net/os/macos/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://funcptr.net/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2015-10-02T01:23:45+00:00</updated>
    <id>https://funcptr.net/os/macos/atom.xml</id>
    <entry xml:lang="en">
        <title>Mac OS X El Capitan Installer Removes Custom Group ID and Membership</title>
        <published>2015-10-02T01:23:45+00:00</published>
        <updated>2015-10-02T01:23:45+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2015/mac-os-x-el-capitan-removes-custom-group-id-and-membership/"/>
        <id>https://funcptr.net/2015/mac-os-x-el-capitan-removes-custom-group-id-and-membership/</id>
        
        <content type="html" xml:base="https://funcptr.net/2015/mac-os-x-el-capitan-removes-custom-group-id-and-membership/">&lt;p&gt;As always, after Apple releases their new operating system, my systems are
upgraded. This time the upgrade was less of a surprise in terms of what it
brings because I&#x27;d been beta testing the new release for the past couple of
weeks, however I was still caught off guard.&lt;&#x2F;p&gt;
&lt;p&gt;On OS X, by default all user accounts start at ID 501 and count up, so if you
have two accounts, you will have user ID 501 and 502 in use. For most people
they will most likely never change this, and all is well. The default group ID
for all new user accounts is &lt;code&gt;staff&lt;&#x2F;code&gt; which has a group ID of 20. So if you have
a single account named for example &lt;code&gt;janedoe&lt;&#x2F;code&gt; her user ID would be 501 and her
group ID would be 20 (&lt;code&gt;staff&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Coming from a FreeBSD world and running a lot of FreeBSD systems, user accounts
start at 1001, and count up. When you create a new user account on FreeBSD, by
default that user is also added to a group with the same name as the username,
with the same ID. So you end up with an account with ID 1001 and default group
ID 1001. Using the same example, a user named &lt;code&gt;janedoe&lt;&#x2F;code&gt; would have a user ID of
1001, and a group ID of 1001 (&lt;code&gt;janedoe&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;When I first installed OS X, and almost every single new installation since, I
have followed these steps to change my user ID and group ID to match those on
my FreeBSD systems:&lt;&#x2F;p&gt;
&lt;ol start=&quot;0&quot;&gt;
&lt;li&gt;Assumption is that you have a separate user account other than the one you
are about to modify that you can temporarily use that has administrator
privileges on the local Mac; I create an &quot;Administrator&quot; account for that
reason.&lt;&#x2F;li&gt;
&lt;li&gt;System Preferences&lt;&#x2F;li&gt;
&lt;li&gt;Users and Groups&lt;&#x2F;li&gt;
&lt;li&gt;Click the &lt;code&gt;+&lt;&#x2F;code&gt; (You may need to click the lock in the bottom left first)&lt;&#x2F;li&gt;
&lt;li&gt;Change the dropdown to &lt;code&gt;group&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Enter Full Name: &lt;code&gt;janedoe&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Create group&lt;&#x2F;li&gt;
&lt;li&gt;Right click on group (&lt;code&gt;janedoe&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Advanced Options...&lt;&#x2F;li&gt;
&lt;li&gt;Change the Group ID to 1001&lt;&#x2F;li&gt;
&lt;li&gt;Okay&lt;&#x2F;li&gt;
&lt;li&gt;Right click on user (&lt;code&gt;janedoe&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Advanced Options...&lt;&#x2F;li&gt;
&lt;li&gt;Change User ID from &lt;code&gt;501&lt;&#x2F;code&gt; to &lt;code&gt;1001&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Change Group from &lt;code&gt;staff&lt;&#x2F;code&gt; to &lt;code&gt;janedoe&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Okay&lt;&#x2F;li&gt;
&lt;li&gt;Close System Preferences&lt;&#x2F;li&gt;
&lt;li&gt;Open Terminal, become root user (sudo su)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;cd &#x2F;Users&#x2F;janedoe&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;find . -uid 501 -print0 | xargs -0 chown 1001:1001&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;This allows me to have the same user ID and group ID on both my Mac OS X and on
FreeBSD, thereby making it easier to use tools like rsync that keeps ownership
and permissions, as well as using NFS. Other ways to do something similar is
using LDAP&#x2F;Kerberos with shared directory service, but that is a little heavy
handed for a home network.&lt;&#x2F;p&gt;
&lt;p&gt;This has worked for me without issues since OS X 10.8, even upgrading from 10.8 to
10.9 and then 10.10 did not change anything. However as soon as I did the
upgrade to El Capitan (10.11) I noticed that all of my &lt;code&gt;ls -lah&lt;&#x2F;code&gt; output looked
like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drwxr-xr-x+  13 xistence  1001   442B Oct  1 16:58 Desktop&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drwx------+  28 xistence  1001   952B Aug 31 12:17 Documents&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drwx------+  89 xistence  1001   3.0K Oct  1 15:56 Downloads&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;drwx------@  72 xistence  1001   2.4K Oct  2 00:16 Library&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and &lt;code&gt;id&lt;&#x2F;code&gt; provided this valuable output:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;uid=1001(xistence) gid=20(xistence) groups=20(xistence),12(everyone),61(localaccounts),399(com.apple.access_ssh),402(com.apple.sharepoint.group.2),401(com.apple.sharepoint.group.1),100(_lpoperator)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Wait, what happened to the &lt;code&gt;staff&lt;&#x2F;code&gt; group that I am supposed to be a member of,
and why is my &lt;code&gt;xistence&lt;&#x2F;code&gt; group ID now stating it is 20 and not 1001 as I was
expecting.&lt;&#x2F;p&gt;
&lt;p&gt;I wondered if the upgrade had messed up my group somehow, and it was
confirmed when I checked with &lt;code&gt;dscl&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ dscl . -read &#x2F;Groups&#x2F;xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[...]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Password: *&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;PrimaryGroupID: 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RealName: xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordName: xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordType: dsRecTypeStandard:Groups&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do note that the group &lt;code&gt;xistence&lt;&#x2F;code&gt; does not show up in &lt;em&gt;System Preferences&lt;&#x2F;em&gt; -&amp;gt;
&lt;em&gt;Users and Groups&lt;&#x2F;em&gt;, so we&#x27;ll have to do some command line magic.&lt;&#x2F;p&gt;
&lt;p&gt;Well, that&#x27;s worrisome, why is this matching a built-in group&#x27;s ID? Specifically
let&#x27;s check the &lt;code&gt;staff&lt;&#x2F;code&gt; group and make sure it still has the appropriate group
ID.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ dscl . -read &#x2F;Groups&#x2F;staff&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[...]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GroupMembership: root&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Password: *&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;PrimaryGroupID: 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RealName: Staff&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordName: staff BUILTIN\Users&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordType: dsRecTypeStandard:Groups&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next I had to check to see what my user account was set to as the default group
ID:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ dscl . -read &#x2F;Users&#x2F;xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[...]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;NFSHomeDirectory: &#x2F;Users&#x2F;xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Password: ********&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;PrimaryGroupID: 20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RealName:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Delta Regeer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordName: xistence delta@regeer.org com.apple.idms.appleid.prd.53696d524c62372b48344a53755864634e4f374b32513d3d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;RecordType: dsRecTypeStandard:Users&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UniqueID: 1001&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;UserShell: &#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, that is not entirely what I was expecting either, at last it didn&#x27;t touch
my user ID. Time to fix things.&lt;&#x2F;p&gt;
&lt;p&gt;First let&#x27;s change the &lt;code&gt;xistence&lt;&#x2F;code&gt; group&#x27;s group ID to 1001, and then change the
Primary Group ID for the user &lt;code&gt;xistence&lt;&#x2F;code&gt; to group ID 1001.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;dscl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -change&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;Groups&#x2F;xistence PrimaryGroupID&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 20 1001&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;dscl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -change&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;Users&#x2F;xistence PrimaryGroupID&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 20 1001&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After that &lt;code&gt;id&lt;&#x2F;code&gt; looked a little bit more sane:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;uid=1001(xistence) gid=1001(xistence) groups=1001(xistence),12(everyone),61(localaccounts),399(com.apple.access_ssh),402(com.apple.sharepoint.group.2),401(com.apple.sharepoint.group.1),100(_lpoperator)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However now the group &lt;code&gt;staff&lt;&#x2F;code&gt; is missing from the list of groups that the user
&lt;code&gt;xistence&lt;&#x2F;code&gt; is a member of, which I don&#x27;t think will hurt anything, but we still
want to be able to read&#x2F;write any folders that are designated as &lt;code&gt;staff&lt;&#x2F;code&gt;
elsewhere in the OS, and any other privileges that entails. So let&#x27;s add the
user &lt;code&gt;xistence&lt;&#x2F;code&gt; to the &lt;code&gt;staff&lt;&#x2F;code&gt; group:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;dscl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; -append&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; &#x2F;Groups&#x2F;staff GroupMembership xistence&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s verify, and check &lt;code&gt;id&lt;&#x2F;code&gt; again:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;uid=1001(xistence) gid=1001(xistence) groups=1001(xistence),12(everyone),20(staff),61(localaccounts),399(com.apple.access_ssh),402(com.apple.sharepoint.group.2),401(com.apple.sharepoint.group.1),100(_lpoperator)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For this to fully take effect, log out and log back in. This will make sure
that all new files have the correct user ID&#x2F;group ID set.&lt;&#x2F;p&gt;
&lt;p&gt;After the change to the Group ID, the group still doesn&#x27;t show up in &lt;em&gt;System
Preferences&lt;&#x2F;em&gt; -&amp;gt; &lt;em&gt;Users and Groups&lt;&#x2F;em&gt;, which I find weird since it is not a
built-in group.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily everything is back to the way it was before the upgrade, and my backup
scripts and NFS shares work again without issues.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>IPv6 -- getaddrinfo() and bind() ordering with V6ONLY</title>
        <published>2012-08-07T22:39:56+00:00</published>
        <updated>2012-08-07T22:39:56+00:00</updated>
        
        <author>
          <name>
            
              Delta Regeer
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://funcptr.net/2012/ipv6-and-getaddrinfo/"/>
        <id>https://funcptr.net/2012/ipv6-and-getaddrinfo/</id>
        
        <content type="html" xml:base="https://funcptr.net/2012/ipv6-and-getaddrinfo/">&lt;p&gt;Recently I ran into an issue that took me a while to sort out, and it is
regarding inconsistent behaviour on various OS&#x27;s with regards to IPv6 sockets
(&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;basedefs&#x2F;sys_socket.h.html&quot;&gt;&lt;code&gt;AF_INET6&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-20120807afvspf-1&quot;&gt;&lt;a href=&quot;#fn-20120807afvspf&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;) and calling &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=bind&amp;amp;apropos=0&amp;amp;sektion=2&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;bind(2)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; after getting the
results back from &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=getaddrinfo&amp;amp;apropos=0&amp;amp;sektion=3&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;getaddrinfo(3)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A call to &lt;code&gt;getaddrinfo()&lt;&#x2F;code&gt; with the hints set to &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;basedefs&#x2F;sys_socket.h.html&quot;&gt;&lt;code&gt;AF_UNSPEC&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in &lt;code&gt;ai_family&lt;&#x2F;code&gt;
and &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=getaddrinfo&amp;amp;apropos=0&amp;amp;sektion=3&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;AI_PASSIVE&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in &lt;code&gt;ai_flags&lt;&#x2F;code&gt; will return to us 1 or more results that we
can &lt;code&gt;bind()&lt;&#x2F;code&gt; to. Sample code for that looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; addrinfo hints,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt;addrlist;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;memset&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(hints));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Ask for TCP&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_socktype &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; SOCK_STREAM;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Any family works for us ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_family &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; AF_UNSPEC;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Set some hints&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;hints.ai_flags &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AI_PASSIVE    &lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt; &#x2F;&#x2F; We want to use this with bind&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AI_ADDRCONFIG;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;  &#x2F;&#x2F; Only return IPv4 or IPv6 if they are configured&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; rv;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; ((rv &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; getaddrinfo&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;7020&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;addrlist&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    fprintf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(stderr,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;getaddrinfo: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; gai_strerror&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(rv))&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Use the list in *addrlist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; addrlist; addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;!=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; addr &lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; addr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;ai_next) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;    &#x2F;&#x2F; use *addr as appropriate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #637777;font-style: italic;&quot;&gt;&#x2F;&#x2F; Clean up the memory from getaddrinfo()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;freeaddrinfo&lt;&#x2F;span&gt;&lt;span&gt;(addrlist);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;On Linux there are two entries returned when the host it is run on has both
IPv4 and IPv6 enabled. An &lt;code&gt;AF_INET&lt;&#x2F;code&gt; which was followed by an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;. Now,
it is not said that you are required to use all of the results that are
returned, but if you want to listen on all address families it is off course
suggested.&lt;&#x2F;p&gt;
&lt;p&gt;Following the steps below for each of the returned results should result in
having 1 or more different sockets that are bound to a single port.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create the socket()&lt;&#x2F;li&gt;
&lt;li&gt;Set any socket options you want (&lt;code&gt;SO_REUSEADDR&lt;&#x2F;code&gt; for example)&lt;&#x2F;li&gt;
&lt;li&gt;Then bind() the socket&lt;&#x2F;li&gt;
&lt;li&gt;After that call listen() (followed off course by accept() on the socket)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Only for some unknown reason (and &lt;code&gt;errno&lt;&#x2F;code&gt; is no help) &lt;code&gt;bind()&lt;&#x2F;code&gt; fails when you
get to the &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;, which was returned second. Searching online as to why
the bind would fail doesn&#x27;t give you any good results and the thing that is
even worse is that if you run the same code on another platform such as
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&quot;&gt;FreeBSD&lt;&#x2F;a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;openindiana.org&quot;&gt;OpenIndiana&lt;&#x2F;a&gt; or &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20120808044613&#x2F;http:&#x2F;&#x2F;www.apple.com&#x2F;osx&#x2F;&quot;&gt;Mac OS X&lt;&#x2F;a&gt; no such failure exists. However
I started suspecting something was up when I started looking at the output from
&lt;code&gt;netstat -lan | grep 7020&lt;&#x2F;code&gt; on Mac OS X. Where 7020 is the port I passed into
&lt;code&gt;getaddrinfo()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tcp46      0      0  *.7020                 *.*  LISTEN     &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tcp4       0      0  *.7020                 *.*  LISTEN &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Wait a minute ... one of the sockets is on both IPv4 and on IPv6. Some more
time spent searching the internet I came across &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;tools.ietf.org&#x2F;html&#x2F;rfc3493#page-22&quot;&gt;RFC 3493 section 5.3&lt;&#x2F;a&gt;,
which is titled &quot;&lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; option for &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; Sockets&quot;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;As stated in section &amp;lt;3.7 Compatibility with IPv4 Nodes&amp;gt;, &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; sockets
may be used for both IPv4 and IPv6 communications.  Some applications may
want to restrict their use of an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket to IPv6 communications
only.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This was going down the right route, so I changed my code so that in the steps
listed above in number 2 I added the following code if the socket type is
&lt;code&gt;AF_INET6&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #D6DEEB; background-color: #011627;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;setsockopt&lt;&#x2F;span&gt;&lt;span&gt;(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D7DBE0;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; sizeof&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7FDBCA;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    close&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(sockfd)&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt;    fprintf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(stderr,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt;setsockopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color: #ECC48D;&quot;&gt; IPV6_V6ONLY&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D9F5DD;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;font-style: italic;&quot;&gt; strerror&lt;&#x2F;span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;(errno))&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;font-style: italic;&quot;&gt;    continue&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The RFC 3493 section 5.3 also states that this option should be turned off by
default, which means that all IPv6 sockets can also communicate over IPv4. Thus
technically setting the option manually in code the best way to fix the issue.
FreeBSD has had this feature turned on (as in IPv6 sockets can only communicate
with IPv6 and NOT IPv4) since 5.x.&lt;&#x2F;p&gt;
&lt;p&gt;The biggest issue is that the remaining operating systems (OS X and
OpenIndiana) don&#x27;t have the same behaviour as Linux which makes troubleshooting
this issue more difficult than it should be. The issue is that the RFC doesn&#x27;t
specify what exactly the operating should do when it encounters a request to
bind to the same port on IPv4 and IPv6. The only place where I have found this
documented is in &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.amazon.com&#x2F;Network-Programming-Jun-ichiro-itojun-Hagino&#x2F;dp&#x2F;1555583180&quot;&gt;&quot;IPv6 Network Programming&quot;&lt;&#x2F;a&gt; under &quot;Tips in IPv6
Programming&quot; chapter 4, section 4, appropriately titled &quot;bind(2) Ordering and
Conflicts&quot;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;If you get a &lt;code&gt;bind()&lt;&#x2F;code&gt; error when attempting to bind to an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket
please make sure that you set the socket option &lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; on the &lt;code&gt;AF_INET6&lt;&#x2F;code&gt;
socket. The default as required by RFC 3493 is to have that option be off. The
default is wrong, and the RFC should have been more specific regarding what the
right behaviour is when attempting to bind on an &lt;code&gt;AF_INET6&lt;&#x2F;code&gt; socket when already
bound on an &lt;code&gt;AF_INET&lt;&#x2F;code&gt; while &lt;code&gt;IPV6_V6ONLY&lt;&#x2F;code&gt; is set to false.&lt;&#x2F;p&gt;
&lt;p&gt;The full code that I used for testing, along with a little bit more information
is available as a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;3231892&quot;&gt;gist on github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-20120807afvspf&quot;&gt;
&lt;p&gt;The old BSD style &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.freebsd.org&#x2F;cgi&#x2F;man.cgi?query=socket&amp;amp;apropos=0&amp;amp;sektion=2&amp;amp;manpath=FreeBSD+9.0-RELEASE&amp;amp;arch=default&amp;amp;format=html&quot;&gt;&lt;code&gt;socket()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; called for defines
starting with &lt;code&gt;PF_&lt;&#x2F;code&gt; such as &lt;code&gt;PF_INET&lt;&#x2F;code&gt; and &lt;code&gt;PF_INET6&lt;&#x2F;code&gt; with the &lt;code&gt;PF&lt;&#x2F;code&gt; standing for
protocol family. POSIX starts them with &lt;code&gt;AF_&lt;&#x2F;code&gt;, and calls them an address
family. On almost every operating system &lt;code&gt;PF_INET&lt;&#x2F;code&gt; is the same as &lt;code&gt;AF_INET&lt;&#x2F;code&gt;. If
the define doesn&#x27;t exist you can always create it. &lt;a href=&quot;#fr-20120807afvspf-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
</feed>
