android - How to know which device interfaces will actually work for IPv6 multicast? - Stack Overflow

I am converting my IPv4 networking library to support IPv6. I am using it to initiate UDP communication

I am converting my IPv4 networking library to support IPv6. I am using it to initiate UDP communication between devices on the same LAN. One of the devices broadcasts (IPv4) a "connect" packet (as defined in my protocol) which is read by a listening device. All works fine in IPv4.

In IPv6, I have to multicast using a destination address like ff12::1234. But, we also have to specify which interface to use in the sendto() function. I am using the getifaddrs() function to query the list of available interfaces. I filter out those not supporting IPv6 or multicast, and ensure they are up. But, that still leaves me with a list of about 6 named interfaces with distinct indexes on my iPhone test device (en0, en2, anpi0, awdl0, llw0). I try sending packets using each interface. The sendto() function claims to have sent the packets for all of the interfaces, but using Wireshark I can see that only one of them - the one named en0 - actually sends the packets around my local network.

So, my question is - how do I know which interface I should use? And, how do I know if any packet was actually multicast successfully?

Do I just have to "know" that en0 is the right interface to use (or wlan0 on Android, and Ethernet on PC), or is there another query I can make to filter out the apparently non-functioning interfaces?

Here's my code for getting the list of interfaces:

ifaddrs* pFirstIf = nullptr;
if( getifaddrs( &pFirstIf ) == 0 )
{
    for( ifaddrs* pIf = pFirstIf; pIf != nullptr; pIf = pIf->ifa_next )
    {
        const uint32 ifFlags = pIf->ifa_flags;

        // Ignore loopback
        if( ifFlags & IFF_LOOPBACK )
            continue;

        // Ensure up and supports multicast
        if( ((ifFlags & IFF_UP) == 0) || ((ifFlags & IFF_MULTICAST) == 0) )
            continue;

        // Check an IP6 family interface
        if( pIf->ifa_addr->sa_family != AF_INET6 )
            continue;

        // Get the interface index
        const int ifIndex = if_nametoindex( pIf->ifa_name );

        // The address scope id is usually the same as the interface index (or 0 if not)
        const sockaddr_in6* pAddr6 = (sockaddr_in6*)pIf->ifa_addr;

        // This appears to be a viable interface to use for multicast - or is it? How can we tell?
        LOG( "Interface: " << pIf->ifa_name << " flags " << ifFlags << " index " << ifIndex << " scope id " << pAddr6->sin6_scope_id << utl::eol );
    }

    // Deallocate the link list
    freeifaddrs( pFirstIf );
}

Here's how I am sending packets:

void sendPacket( int hSocket, const void* packetData, uint32 nPacketBytes, sockaddr_in6& destAddress, int ifIndex )
{
    // Interface index is required for multi-cast, and on iOS for sending to a link local address
    sai6.sin6_scope_id = ifIndex;

    if( sendto( hSocket, packetData, nPacketBytes, 0, (sockaddr*)destAddress, sizeof( sockaddr_in6 )) > 0 )
    {
        // Packet appears to have been sent, but for many ifIndex does not appear in Wireshark
        // ...
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744187632a4562283.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信