How to bind to a broadcast address in Micrium

203 Views Asked by At

I'm trying to find a way to bind an IPv4 UDP socket to a broadcast address in Micrium (µC/OS-Ⅲ).

Attempt to bind a socket to the actual broadcast address just returns NET_SOCK_ERR_INVALID_ADDR. The broadcast is not explicitly mentioned on the list of things to bind on the NetSock_Bind call documentation, there is no analogue of SO_BROADCAST either. Is it impossible at all?

Can I, as a last resort, add a broadcast address to an existing interface as its own address (this shouldn't mess up ARP, since nobody is going to ask for it, and I'm not going to send anything through it)?

1

There are 1 best solutions below

4
On

look at micrium documentation. Follow the steps and you'll get no error.

BTW, if you won't use NetApp_SetSockAddrand try to bind using NetSock_Bind, you'll receive the NET_SOCK_ERR_INVALID_ADDR which happened to you.

Edit:

look at the next code, which works for me. Note that the IP Address I manually initialized the host's address and used the defined address NET_SOCK_ADDR_IP_V4_WILDCARD.

NET_SOCK_ADDR_IPv4 host;
NET_ERR perr;

Mem_Clr(&host, sizeof(host));
host.AddrFamily = NET_SOCK_ADDR_FAMILY_IP_V4;
host.Addr = NET_UTIL_HOST_TO_NET_32(NET_SOCK_ADDR_IP_V4_WILDCARD);
host.Port = NET_UTIL_HOST_TO_NET_16(socketPort);

netSockId = NetSock_Open( NET_SOCK_PROTOCOL_FAMILY_IP_V4, 
                          NET_SOCK_TYPE_DATAGRAM,
                          NET_SOCK_PROTOCOL_UDP,
                         &perr);

if (perr != NET_SOCK_ERR_NONE)
{
    // log the error.
    return;
}

NetSock_Bind(                 netSockId,
             (NET_SOCK_ADDR*)&host,
                              NET_SOCK_ADDR_SIZE,
                             &perr);
if (perr != NET_SOCK_ERR_NONE)
{
    // log the error.
    return;
}

What I said about the NetApp_SetSockAddr() function, is that if I call it instead of manually initializing the host, it returned the error you received.