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

John Hernandez jph at jph.net
Mon Apr 2 13:32:58 MDT 2012


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



More information about the LUG mailing list