<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 27, 2022 at 1:51 PM Klaus Ebbe Grue <<a href="mailto:grue@di.ku.dk">grue@di.ku.dk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-6451703730670707768">
<div dir="ltr">
<div id="m_-6451703730670707768divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<div dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<div><span style="font-family:Arial,Helvetica,sans-serif">Hi systemd-devel,</span><br>
<span style="font-family:Arial,Helvetica,sans-serif"></span><br>
<span style="font-family:Arial,Helvetica,sans-serif"></span></div>
<span style="font-family:Arial,Helvetica,sans-serif">Sorry to bug you with another user question.<br>
</span></div>
<div dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<span style="font-family:Arial,Helvetica,sans-serif">I have a socket activated daemon, call it mydaemon, and I have trouble finding out who connects to it.</span>
<p><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></p>
<p><span style="font-family:Arial,Helvetica,sans-serif">mydaemon.socket contains:</span></p>
<p><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></p>
<div><span style="font-family:"Courier New",monospace"><span style="font-family:"Courier New",monospace">
</span>[Socket]</span><br>
<span style="font-family:"Courier New",monospace"> ListenStream=9999</span><br>
</div>
<div><br>
</div>
<div><span style="font-family:Arial,Helvetica,sans-serif">When I connect using IPv4 using</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:"Courier New",monospace"> nc -4 localhost 9999<br>
</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif">then mydaemon does</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:"Courier New",monospace"> sockaddr_in6 peer;</span><br>
<span style="font-family:"Courier New",monospace"> socklen_t peer_size=sizeof(peer);</span><br>
<span style="font-family:"Courier New",monospace"> accept(3,(struct sockaddr *)&peer,sizeof(peer))</span><br>
</div>
<span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></span>
<p><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif">Afterwards,
</span><span style="font-family:Arial,Helvetica,sans-serif">peer.</span></span><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif">sin6_family</span><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif">
is </span><span style="font-family:Arial,Helvetica,sans-serif">AF_INET6</span><span style="font-family:Arial,Helvetica,sans-serif"> and peer.<span>sin6_addr contains some gibberish like <span>a00:e5ae::</span></span></span></span></span></p></div></div></div></div></blockquote><div><br></div><div>If you specify nothing for the listen address, systemd will assume the IPv6 address [::] as the default, and will create an AF_INET6 socket bound to [::]:9999.<br></div><div><br></div><div>Due to Linux's default "bind both families" magic, it will actually be bound to both [::]:9999 *and* <a href="http://0.0.0.0:9999">0.0.0.0:9999</a>, so it will accept IPv4 connections – but you'll receive them in the form of AF_INET6 sockets, so the peer address of your v4 client indeed has family AF_INET6 but contains a "v6-mapped" IPv4 address such as [::ffff:10.0.229.174] aka [::ffff:a00:e5ae].<br></div><div><br></div><div>The alternative would be to specify both ListenStream=[::]:9999 and ListenStream=<a href="http://0.0.0.0:9999">0.0.0.0:9999</a> (as well as BindIPv6Only=ipv6-only), which would cause you to receive *two* socket FDs – one purely for IPv6 clients, the other for IPv4 – that you'd have to put into poll() or some other loop for accepting clients.<br></div><div><br></div><div>You can extract the IPv4 address by detecting the [::ffff:0:0/96] prefix and stripping away the first 12 bytes. (There's also a magic option for getsockopt() listed in ipv6(7) that can convert such a "v6-mapped" socket to a "real" AF_INET socket, but it's rarely needed.)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-6451703730670707768"><div dir="ltr"><div id="m_-6451703730670707768divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr"><div dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif"><span><span><br>
</span></span></span></span></span></p>
<p><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif"><span><span>If I connect more than once, the gibberish changes from connection to connection.<br></span></span></span></span></span></p></div></div></div></div></blockquote><div><br></div><div>I have a feeling it "changes" because you're trying to give the whole struct sockaddr to inet_pton() instead of giving just the .sin6_addr field, so your program is trying to interpret the *port number* (i.e. the .sin6_port which precedes .sin_addr) as part of the address...</div><div><br></div><div>But please show your entire code, otherwise this is all just guessing.</div><div><br></div><div>Here's a working example that I've just tested with `systemd-socket-activate --listen=9999`: <a href="https://gist.github.com/grawity/63369273742f23b596d764cb6d45feb7">https://gist.github.com/grawity/63369273742f23b596d764cb6d45feb7</a></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-6451703730670707768"><div dir="ltr"><div id="m_-6451703730670707768divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr"><div dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif">If mydaemon creates the listening socket, I can easily get the peer address.</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif">I suspect that when systemd creates the listening socket then accept(3,...) returns a socket which is connected to a local socket created by systemd.</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif"><br>
</span></div>
<div><span style="font-family:Arial,Helvetica,sans-serif">QUESTION: Is that suspicion correct?</span></div></div></div></div></div></blockquote><div><br></div><div>No, it isn't.<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-6451703730670707768"><div dir="ltr"><div id="m_-6451703730670707768divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr">
</div>
</div>
</div></blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">Mantas Mikulėnas</div></div></div>