Strange thing on IPv6 multicast program on Windows

Posted by zhanglistar on Stack Overflow See other posts from Stack Overflow or by zhanglistar
Published on 2011-11-28T09:06:57Z Indexed on 2011/11/28 9:51 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

I have written an ipv6 multicast program on windows xp sp3. But a problem bothers me a lot. The sendto function implies no error, but I can't capture the packet using wireshark. I am sure the filter is right.

Thanks in advance.

And the code is as follows:

#include "stdafx.h"
#include <stdio.h>      /* for printf() and fprintf() */
#include <winsock2.h>   /* for socket(), connect(), sendto(), and recvfrom() */
#include <ws2tcpip.h>   /* for ip_mreq */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <time.h>       /* for timestamps */
#include <pcap.h>
#include <Iphlpapi.h> 

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wpcap.lib")
#pragma comment(lib, "Iphlpapi.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    int sfd;
    int on, length, iResult;
    WSADATA wsaData;
    struct addrinfo Hints;
    struct addrinfo *multicastAddr, *localAddr;
    char buf[46];

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    /* Resolve destination address for multicast datagrams */
    memset(&Hints, 0, sizeof (Hints));
    Hints.ai_family = AF_INET6;
    Hints.ai_socktype = SOCK_DGRAM;
    Hints.ai_protocol = IPPROTO_UDP;
    Hints.ai_flags = AI_NUMERICHOST;

    iResult = getaddrinfo("FF02::1:2", "547", &Hints, &multicastAddr);
    if (iResult != 0) {
        /* error handling */   
        printf("socket error: %d\n", WSAGetLastError());
        return -1;
    }

    /* Get a local address with the same family (IPv4 or IPv6) as our multicast group */
    Hints.ai_family   = multicastAddr->ai_family;
    Hints.ai_socktype = SOCK_DGRAM;
    Hints.ai_flags    = AI_PASSIVE; /* Return an address we can bind to */
    if ( getaddrinfo(NULL, "546", &Hints, &localAddr) != 0 )
    {
        printf("getaddrinfo() failed: %d\n", WSAGetLastError());
        exit(-1);
    }

    // Create sending socket
    //sfd = socket (multicastAddr->ai_family, multicastAddr->ai_socktype, multicastAddr->ai_protocol);
    sfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (sfd == -1)
    {
        printf("socket error: %d\n", WSAGetLastError());
        return 0;
    }

    /* Bind to the multicast port */
    if ( bind(sfd, localAddr->ai_addr, localAddr->ai_addrlen) != 0 )
    {
        printf("bind() failed: %d\n", WSAGetLastError());
        exit(-1);
    }

    if (multicastAddr->ai_family  == AF_INET6 &&
            multicastAddr->ai_addrlen == sizeof(struct sockaddr_in6)) /* IPv6 */
    {
        on = 1;
        if (setsockopt (sfd, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&on, sizeof (on) /*(char *)&interface_addr,
                                                                                         sizeof(interface_addr)*/) == -1)
            {
                printf("setsockopt error:%d\n", WSAGetLastError());
                return -1;
            }

        if (setsockopt (sfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&on, sizeof (on) /*(char *)&interface_addr,
                                                                                           sizeof(interface_addr)*/) == -1)
            {
                printf("setsockopt error:%d\n", WSAGetLastError());
                return -1;
            }

        struct ipv6_mreq multicastRequest;  /* Multicast address join structure */

        /* Specify the multicast group */
        memcpy(&multicastRequest.ipv6mr_multiaddr,
                &((struct sockaddr_in6*)(multicastAddr->ai_addr))->sin6_addr,
                sizeof(struct in6_addr));

        /* Accept multicast from any interface */
        multicastRequest.ipv6mr_interface = 0;

        /* Join the multicast address */
        if ( setsockopt(sfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*) &multicastRequest, sizeof(multicastRequest)) != 0 )
        {
            printf("setsockopt() failed: %d\n", WSAGetLastError());
            return -1;
        }

        on = 1;
        if (setsockopt (sfd, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&on, sizeof (on)) == -1)
        {
            printf("setsockopt error:%d\n", WSAGetLastError());
            return 0;
        }
    }

    memset(buf, 0, sizeof(buf));
    strcpy(buf, "hello world");

    iResult = sendto(sfd, buf, strlen(buf), 0,
            (LPSOCKADDR) multicastAddr->ai_addr, multicastAddr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("setsockopt error:%d\n", WSAGetLastError());
        return -1;
        /* Error handling */
    }

    return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about Windows