Hi,

I try to receive multicast messages using the sockets API in a task of my
application based on FreeRTOS and lwIP stack 1.3.0 (task function attached).

Let's assume my Ethernet controller is properly configured to pass multicast
messages to the stack. (This topic I am currently discussing with the ATMEL
support team for my EVK1100 board -> any help is welcome, too).

However, joining a multicast group and setting the socket option to reuse
the port number seem to fail. I can not see any IGMP messages in Wireshark
(which I would expect). Since it is possible to enable LWIP_IGMP I thought
this feature would be supported by lwIP. Does anybody have an explanation
for the failures or can somebody even provide me a working example?

Thanks for helping!
Best regards,
Mathias
portTASK_FUNCTION(RecMCast, pvParameters) {
        // Locals
        int fd_socket = 0;
        fd_set fd;
        struct sockaddr_in addr;
        u32_t addrlen = sizeof(addr);
        struct ip_mreq mreq;
        u32_t set_true = 1;             

        // The parameters are not used
        (void)pvParameters;
        
        // Set select timeout
        struct timeval socket_to;
        socket_to.tv_sec = 0;
        socket_to.tv_usec = 500;

        // Task call time
        portTickType xLastWakeTime;
        // Initialize with current tick count
        xLastWakeTime = xTaskGetTickCount();

        // Block task until network interface is ready (DHCP)
        while (!netif_is_up(&MACB_if)) {
                vTaskDelayUntil(&xLastWakeTime, TICK_RATE);
        }

        // Create socket (same as UDP socket)
        if ((fd_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                print_dbg("Socket creation failed!\n");
        }
        
        // Allow multiple sockets to use the same port number
        if (setsockopt(fd_socket, SOL_SOCKET, SO_REUSEADDR, &set_true, 
sizeof(set_true)) < 0) {
                print_dbg("Reusing port failed!\n");
        }

        // Set up address
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
        addr.sin_port = htons(REC_PORT);

        // Bind to address
        if (bind(fd_socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
                print_dbg("Binding socket failed!\n");
        }

        // Set up group address 
        mreq.imr_multiaddr.s_addr = inet_addr("239.251.34.1");
        mreq.imr_interface.s_addr = htonl(INADDR_ANY);
        // Join multicast group
        if (setsockopt(fd_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, 
sizeof(mreq)) < 0) {
                print_dbg("Joining multicast group failed!\n");
        }

        // Task loop
        while (1) {
                FD_ZERO(&fd);
                FD_SET(fd_socket, &fd);
                // Check if data on network interface present
                if (select(FD_SETSIZE, &fd, NULL, NULL, &socket_to)) {
                        // Receive packet
                        if (recvfrom(fd_socket, pucRecBuffer, BUFFER_SIZE, 0, 
(struct sockaddr *)&addr, &addrlen) >= 0) {
                                // Process packet
                                prvProcessReply();
                                print_dbg("Message received.\n");
                        }
                }
                // Delay the task
                vTaskDelayUntil(&xLastWakeTime, TICK_RATE);
        }
        // Leave multicast group (never executed)
        setsockopt(fd_socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, 
sizeof(mreq));
        // Close socket (never executed)
        close(fd_socket);
}
_______________________________________________
lwip-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/lwip-users

Reply via email to