One of the problems of learning new stuff based on trial-and-error
iterations is that it is very easy to miss important details… but
that’s the price to pay when there is no decent documentation available
for a given feature. We saw
yesterday
multiple details about LOCAL_CREDS socket credentials and, as you may
deduce, I missed some.
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)andlisten(2). - Before entering the
accept(2)loop, set theLOCAL_CREDSoption 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_CREDSoption from the socket used to communicate with that specific client.
It couldn’t be easier! This is still different to all other socket
credentials methods I know of but can be easily adapted to protocols
that were not designed to support LOCAL_CREDS (i.e. that do not
implement the synchronization explained in the previous post).
(Content also available at: gemini://capsule.jmmv.dev/post/2006/08/more-on-localcreds.gmi)