First of all I assumed that setting the LOCAL_CREDS option only affected the next received message (I didn't mention this explicitly in the post though). It turns out that this is incorrect: enabling this option makes the socket transmit credentials information on each message until the option is disabled again.
Secondly, setting the LOCAL_CREDS option on a server socket (one configured with the listen(2) call) results in all sockets created from it through accept(2) to also carry the flag enabled. In other words, it is inherited.
These features are interesting because, when using combined, avoid the need for the synchronization protocol outlined in the previous post — in some cases only. If the credentials are to be transmitted at the very beginning of the connection, the server can follow these steps:
- Create the server socket and configure it with bind(2) and listen(2).
- Before entering the accept(2) loop, set the LOCAL_CREDS option on the server socket.
- Enter the accept(2) loop and start accepting clients.
- For each new client:
- Receive its first message.
- Get the credentials from it.
- Disable the LOCAL_CREDS option from the socket used to communicate with that specific client.