[lug] sendto(2) problem with ipv4/ipv6 addresses...

Alan Robertson alanr at unix.sh
Mon Apr 2 22:17:14 MDT 2012


My bug was that I had done a bind to an address on one network, but 
tried to send a packet to a different network interface with the same 
socket.  Cure:  Bind to the ANY address.

On 04/02/2012 01:45 PM, Alan Robertson wrote:
> I wrote a little test program that just did the sendto with a variety of
> ipv4 and ipv6 addresses.  It works fine :-D.
>
> Now I have something working to compare my "real" code to.  [Should have
> done this earlier]
>
> It uses IF_INET6 - which makes sense since it _is_ and IPv6-format
> address (that is, 16 bytes and not 4).
>
> So the good news is I'm doing it _almost_ right.
>
>
> On 04/02/2012 01:32 PM, John Hernandez wrote:
>> Alan,
>>
>> I think in the case of the IPv4-sourced packet, you want
>>
>> sin6_family = AF_INET
>>
>> (not AF_INET6)
>>
>> -John
>>
>> On Mon, Apr 2, 2012 at 12:38 PM, Alan Robertson<alanr at unix.sh>   wrote:
>>> As part of the Assimilation Monitoring project (assimmon.org), I've been
>>> writing some UDP network code to send packets.  It seems to work fine when
>>> sending to the ipv6 loopback address (::1), but not when sending to ipv4
>>> addresses.  I'm sending to ipv4 addresses by using the ipv4 part of the ipv6
>>> address space ("::ffff:aa.bb.cc.dd").  Tcpdump is showing that nothing is
>>> being sent - so the kernel is blocking this call before it goes out.
>>>
>>> The code being invoked is exactly the same (except for the address) for the
>>> working and failing cases - so it must be something in the data...
>>>
>>> The failing line of code is simple:
>>>       rc = sendto(self->getfd(self),  packet, (size_t)length, flags, (const
>>> struct sockaddr*)&v6addr, sizeof(v6addr));
>>>       g_return_if_fail(rc == length);
>>>
>>> When the v6addr structure refers to "::1" it works fine.  But when it is set
>>> to "::ffff:10.10.10.4" (my local address) it fails with ENETUNREACH
>>>
>>>
>>> When I watch it with strace you see this system call when it succeeds:
>>> sendto(4,
>>> "\0\1\0*\0\0\0\1\0\"\1\2\337?a\230\4\251/\333 at W\31-\304=\327H\352w\212\334"...,
>>> 48, 0, {sa_family=AF_INET6, sin6_port=htons(1984), inet_pton(AF_INET6,
>>> "::1",&sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 48
>>>
>>> When I watch it with strace you see this system call when it fails:
>>> sendto(4,
>>> "\0\1\0*\0\0\0\1\0\"\1\2\337?a\230\4\251/\333 at W\31-\304=\327H\352w\212\334"...,
>>> 48, 0, {sa_family=AF_INET6, sin6_port=htons(1984), inet_pton(AF_INET6,
>>> "::ffff:10.10.10.5",&sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) =
>>> -1 ENETUNREACH (Network is unreachable)
>>>
>>> When I look at the struct sockaddr_in6 in gdb in the failing case, I see
>>> this:
>>> (gdb) print v6addr
>>> $1 = {sin6_family = 10, sin6_port = 49159, sin6_flowinfo = 0, sin6_addr =
>>> {__in6_u = {
>>>         __u6_addr8 =
>>> "\000\000\000\000\000\000\000\000\000\000\377\377\n\n\n\005", __u6_addr16 =
>>> {0, 0, 0, 0, 0, 65535, 2570, 1290},
>>>         __u6_addr32 = {0, 0, 4294901760, 84544010}}}, sin6_scope_id = 0}
>>>
>>>
>>> Any suggestions?
>>>
>>> --
>>>       Alan Robertson<alanr at unix.sh>   - @OSSAlanR
>>>
>>> "Openness is the foundation and preservative of friendship...  Let me claim
>>> from you at all times your undisguised opinions." - William Wilberforce
>>>
>>>
>>> _______________________________________________
>>> Web Page:  http://lug.boulder.co.us
>>> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>>> Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety
>> _______________________________________________
>> Web Page:  http://lug.boulder.co.us
>> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety
>>
>


-- 
     Alan Robertson<alanr at unix.sh>  - @OSSAlanR

"Openness is the foundation and preservative of friendship...  Let me claim from you at all times your undisguised opinions." - William Wilberforce



More information about the LUG mailing list