How to receive broadcast packet by Wiznet 5100 shield?

I, along with my nephew, are trying to make a DHCP server from scratch to teach ourselves some networking along with arduino programming paradigm.

A DHCP server need to accept both

(1) Broadcast packet (Dest MAC: FF-FF-FF-FF-FF-FF, and Dest IP: 255:255:255:255)
(2) Unicast IP (Dest MAC: DHCP Server's HW Address, Dest IP: DHCP Server's IP Address)

I have read Wiznet 5100 datasheet, and Ethernet library code. I am not able to understand how to make the shield do both (1) and (2) above. It seems to me that I always have to one of the 4 socket structure to receive any packet from W5100. If I use server(67) and server.begin(), it will receive packets that has dest MAC= Card's MAC, dest IP = Card's IP and dest port = 67. How would I setup a socket to receive packet that has Dest MAC: FF-FF-FF-FF-FF-FF, Dest IP: 255:255:255:255 and Dest port: 67?

Also, EthernetServer::begin() uses TCP only. I did not find any overload of this function to support UDP.

I am okay to tweak the library, or write my own. I just need to know how to set up W5100 registers.

I picked up W5100 datasheet from Sparkfun.

I really appreciate any help.

~~PB

I use broadcast packets on my w5100.

Insure you are using the correct broadcast IP. On a localnet, it is the last IP in the localnet range. That means if the subnet is 192.168.0.x and the subnet mask is 255.255.255.0, then the broadcast address is 192.168.0.255.

The bad part about a DHCP server is the subnet mask must be 0.0.0.0. I haven't tried that.

Thanks SurfTim for your reply.

When a new machine joins the network, it does not even know which network he is sitting on. That's why the client first sends the DISCOVER packet to 255.255.255.255.

My W5100 card's address is 192.168.6.34 and mask is 255.255.255.0.

As I understand, Layer 1's job is to pick up packets that satisfy one of these three conditions.

1(a). If the Dest MAC address of the packet matches with its own MAC address
1(b). If the Dest MAC address is broadcast MAC address
1(c). If the Dest MAC address is in multicast MAC address groups

Layer 3 is supposed to pick up packets that satisfy one of these three conditions.

2(a). If the Dest IP address of the packet matches with its own IP address
2(b). If the Dest IP address is broadcast IP address
2(c). If the Dest IP address is in multicast IP address groups

So, a packet having broadcast MAC Address and broadcast IP address should reach up till tis point.

Finally, the packet should get delivered to application layer's socket based on dest port number. If the above two packet have same Dest port number 67, they will both get into same Socket that was for port 67.

I can configure W5100 to pickup packets that satisfy 1(a) + 2(a) by programming W5100's MAC and IP address. I was expecting the controller to pick packets that satify 2(a)+2(b) automatically, by default, without any further programming. Setting subnet to 0.0.0.0. will cause every packet to be picked up, sort of promiscuous mode, isn't it?

~~PB

m not sure is that how layer 3 is supposed to pass the packet to upper layer (TCP/UDP) when the Dest port matches

Setting the subnet mask to 0.0.0.0 will NOT pick up every packet. Just the ones addressed directly to it (192.168.6.34) and the broadcast address (255.255.255.255). I use a RPi as the "server" (broadcasts) and my ethernet shield as the "client". It receives both packet addresses. However, I use a localnet broadcast address.

Hi SurferTim,

Again, thanks for your reply.

I did a little study on Subnet mask, and it seems that subnet mask does not have any role on inbound traffic. Subnet mask it used only for outbound traffic to determine which packets are local (ARP needed), and which need to be sent to Gateway (no ARP needed, send the packet to router's MAC address).

https://arstechnica.com/civis/viewtopic.php?t=281922

If that's the case, direct and broadcast packets should have been picked up by my W5100 card, irrespective of the subnet mask.

That is incorrect. It does have an effect on inbound traffic. It determines the broadcast address the WizNET IC will listen on. The broadcast address is the last IP in the localnet.

I just tested 255.255.255.255 broadcast address with my system, and the w5100 picks up its assigned address and the broadcast address 255.255.255.255.

IPAddress ip(192, 168, 1, 4);
IPAddress gateway(192,168,1,1);
IPAddress subnet(0,0,0,0);

// then in setup
  Ethernet.begin(mac, ip,gateway,gateway,subnet);

You are correct that the shield will no longer work on anything but a localnet address for outbound.

edit: Just to make this clear, if you assign a IP of 192.168.1.2 and a netmask of 255.255.255.0, the broadcast address is 192.168.1.255. If you assign a netmask of 255.255.0.0, the broadcast address is 192.168.255.255. If you assign a netmask of 255.0.0.0, the broadcast address is 192.255.255.255. If you assign a netmask of 0.0.0.0, the broadcast address is 255.255.255.255. The Wiznet IC will not receive any packets that are not addressed to its IP or the broadcast address.