eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
313 stars 179 forks source link

Multicast hop not properly setted #250

Closed agalliazzo closed 2 years ago

agalliazzo commented 2 years ago

Referring to issue #175 seem that multicast hops are not setted properly when using UDP6.

Looking at the code seems that the same socket (pollfds[0]) is used for both unicast and multicast (UDP6Port::broadcast) communication but the socket option for hops are only setted on socket in pollfds[1].

I solved doing:

int UDPPort6::broadcast(const uint8_t* buf, uint32_t length)
{
    sockaddr_in6 dest;
    memset(&dest, 0, sizeof(dest));
    dest.sin6_family = AF_INET6;
    dest.sin6_port = _grpAddr.getPortNo();
    memcpy(dest.sin6_addr.s6_addr, (const void*) &_grpAddr.getIpAddress()->sin6_addr, sizeof(in6_addr));

#ifdef  DEBUG_NW
    char addrBuf[INET6_ADDRSTRLEN];
    addr->sprint(addrBuf);
    D_NWSTACK("sendto %s\n", addrBuf);
#endif

    int status = ::sendto(_pollfds[1].fd, buf, length, 0, (const sockaddr*) &dest, sizeof(dest));

    if (status < 0)
    {
        D_NWSTACK("UDP6::broadcast - sendto: %s", strerror(errno));
        return status;
    }

    return 0;
}

in MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp

but another solution can be adding the

    if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, sizeof(hops)) < 0)
    {
        D_NWSTACK("\033[0m\033[0;31m error %s IPV6_MULTICAST_HOPS\033[0m\033[0;37m\n", strerror(errno));
        close();
        return -1;
    }

on the unicast socket.

Hope this help others