UDP Broadcast with NativeEthernet?

bschena

Active member
Does anyone know if Paul's NativeEthernet lib has the ability to do Broadcast messaging using UDP?

I searched NativeEthernet.h....didn't find anything that looked related to Broadcast functionality.

I don't know much about Broadcast messaging, other than I think it would be a good fit for my application. I'm unicasting right now and can probably make that work fine, but Broadcast seems more elegant for having all nodes share everything with everyone else.

Thanks!
 
If by Broadcast you are actually referring to Multicast then yes there is support, both in Paul’s Ethernet library and my NativeEthernet library.
Code:
/* Start EthernetUDP socket, listening at local port PORT */
uint8_t EthernetUDP::beginMulticast(IPAddress ip, uint16_t port)
{
	if (sockindex < Ethernet.socket_num) Ethernet.socketClose(sockindex);
	sockindex = Ethernet.socketBeginMulticast(SnMR::UDP | SnMR::MULTI, ip, port);
	if (sockindex >= Ethernet.socket_num) return 0;
	_port = port;
	_remaining = 0;
	return 1;
}
 
If by Broadcast you are actually referring to Multicast then yes there is support, both in Paul’s Ethernet library and my NativeEthernet library.
Code:
/* Start EthernetUDP socket, listening at local port PORT */
uint8_t EthernetUDP::beginMulticast(IPAddress ip, uint16_t port)
{
	if (sockindex < Ethernet.socket_num) Ethernet.socketClose(sockindex);
	sockindex = Ethernet.socketBeginMulticast(SnMR::UDP | SnMR::MULTI, ip, port);
	if (sockindex >= Ethernet.socket_num) return 0;
	_port = port;
	_remaining = 0;
	return 1;
}

Thanks for the clarification. I'm still trying to sort Multicast from Broadcast myself. I believe that Multicast is more sophisticated than Broadcast. Multicast is sort of publish/subscribe whereas Broadcast is just "blast every packet to every node" - which would probably work well for me. I was searching specifically for "Broadcast" - which explains why I didn't stumble on the Multicast stuff! I'll take a look and see if I can figure out if Multicast will work for me without having to take a 6-month educational detour through networking textbooks.

Pointers to Multicast example code would be most welcome!

And yes, sorry, credit where credit is due - your NativeEthernet library is working great, thanks!

EDIT: http://www.fiber-optical-networking.com/unicast-vs-multicast-vs-broadcast-differences.html
 
multicast is more local-network-friendly than sending broadcasts to EVERY machine on your local ethernet. Here is sketch for wiznet but you can change the includes to use NativeEthernet.
https://github.com/manitou48/teensy3/blob/master/mtalk.ino
I have run multicast successfully on NativeEthernet.
you can test with python
Code:
#https://gist.github.com/quiver/4111310
import socket
import struct

multicast_addr = '224.7.8.9'
bind_addr = '0.0.0.0'
port = 7654

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
membership = socket.inet_aton(multicast_addr) + socket.inet_aton(bind_addr)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, membership)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sock.bind((bind_addr, port))

while True:
    message, address = sock.recvfrom(255)
    print message


or a quick linux test with netcat/nc echo "1234" | nc -u 224.7.8.9 7654

2017 multicast thread https://forum.pjrc.com/threads/42033-T3-6-amp-WIZ820io-Bonjour
 
Excellent, thanks Manitou. That gives me a head start.

The good news is that all of my nodes will be on an airgapped network dedicated for the application, so all of then network BW is for that traffic only.

But, multicast seems like it would work well also, and probably be more efficient. I guess it depends a lot on how many nodes I have on the network in the end.

I'm still curious if Broadcast (to MAC FF:FF:FF:FF:FF:FF, as linked in post #3) is something anticipated by NativeEthernet, though I"m not sure what that would look like exactly.
 
If you don’t need full networking capability then you could talk directly to a MAC address (such as broadcast) without the need for UDP, but NativeEthernet doesn’t have any functions to do that for you. FNET has those functions already though so you would have to look there to find it. If you don’t want to reinvent the wheel since FNET can take some time to learn I would just stick with UDP multicast. Though if you also want to maximize performance so you can do more with the Teensy you should learn FNET since it’s all non-blocking.
 
Excellent, thanks Manitou. That gives me a head start.

The good news is that all of my nodes will be on an airgapped network dedicated for the application, so all of then network BW is for that traffic only.

But, multicast seems like it would work well also, and probably be more efficient. I guess it depends a lot on how many nodes I have on the network in the end.

I'm still curious if Broadcast (to MAC FF:FF:FF:FF:FF:FF, as linked in post #3) is something anticipated by NativeEthernet, though I"m not sure what that would look like exactly.

Usually, for UDP broadcast you use the the broadcast IP address of your local subnet. For my home network destination UDP broadcast IP would be 192.168.1.255 (the 255 is the broadcast address for that class C network). I know the lwIP lib recognizes that as a broadcast address, but i haven't tried it with NativeEthernet lib. For unix BSD networks, i think you have to set a socket option to enable UDP broadcast, setsockopt(fd, SOL_SOCKET, SO_BROADCAST,(char *)&on, sizeof(on));
 
If you don’t need full networking capability then you could talk directly to a MAC address (such as broadcast) without the need for UDP, but NativeEthernet doesn’t have any functions to do that for you. FNET has those functions already though so you would have to look there to find it. If you don’t want to reinvent the wheel since FNET can take some time to learn I would just stick with UDP multicast. Though if you also want to maximize performance so you can do more with the Teensy you should learn FNET since it’s all non-blocking.

oh, that's interesting. I've looked briefly at FNET - it did seem more complex than your nice NativeEthernet package, but you are right, getting closer to the iron might make sense for me. Being able to talk directly to various MACs might actually simplify a few things for me, if I can figure it all out. I didn't really realize that Native is blocking. Seems to still rip pretty good, but I'm not doing a ton of other compute at each node at the moment, so that might change.

How hard can it be? (I've lost track of how many times that statement has bitten me in the ass..).
 
The hard part is really the lack of documentation like you get from the standard Ethernet library. I’ve talked directly to MACs already using FNET because of a protocol I had to reverse engineer. Though I did have to add the Ethertype that the protocol uses to the FNET source files so that it knew where to route the messages.
 
OK, I confirmed NativeEthernet lib supports UDP BROADCAST by using local networks broadcast IP adddress, e.g. 192.168.1.255. target MAC address on packet is 0xffffffffffff

Code:
IPAddress MyServer(192, 168, 1, 255);
  Udp.beginPacket(MyServer, dstport);   //uechosrv
  Udp.write(packetBuffer, nbytes);
  Udp.endPacket();


04:e9:e5:01:86:c4 > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 60: 192.168.1.126.8888 > 192.168.1.255.7654: UDP, length 8
 
Last edited:
OK, I confirmed NativeEthernet lib supports UDP BROADCAST by using local networks broadcast IP adddress, e.g. 192.168.1.255. target MAC address on packet is 0xffffffffffff

Code:
IPAddress MyServer(192, 168, 1, 255);
  Udp.beginPacket(MyServer, dstport);   //uechosrv
  Udp.write(packetBuffer, nbytes);
  Udp.endPacket();


04:e9:e5:01:86:c4 > ff:ff:ff:ff:ff:ff, ethertype IPv4 (0x0800), length 60: 192.168.1.126.8888 > 192.168.1.255.7654: UDP, length 8

OH! that's exciting as hell! I'm going to have to look at how to use MAC addressing - I've just been doing the usual IP:port thing.

Very cool! Thanks for taking the time to look into that!
 
In the QNEthernet library, should someone find this thread:

1. There's full multicast support. See: https://github.com/ssilverman/QNEthernet#how-to-use-multicast
2. In addition, you can find the local network's broadcast address using Ethernet.broadcastIP(). You could send packets there without having to figure out the address yourself.
3. There's also an EthernetFrame object/API for sending and receiving raw Ethernet frames, should you need that.
See: https://github.com/ssilverman/QNEthernet#raw-ethernet-frames
4. There's lots of useful functions beyond the Arduino API. For example, udp.send(host, port, buf, length) instead of udp.beginPacket()/write()/endPacket().
 
Last edited:
Back
Top