njh / EtherCard

EtherCard is an IPv4 driver for the ENC28J60 chip, compatible with Arduino IDE
https://www.aelius.com/njh/ethercard/
GNU General Public License v2.0
1.03k stars 455 forks source link

UDP not send packet #368

Open Avenitos opened 5 years ago

Avenitos commented 5 years ago

Hello!

sendUdp() function not send broadcast packets :( in Wireshark no packets.

UIP library worked correctly!

#include <EtherCard.h>

const byte myMac[] PROGMEM = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[350];                          // Buffer must be 350 for DHCP to work

void setup() {
  Serial.begin(57600);
  Serial.println(F("\n[EtherCard UDP send]"));

  if (ether.begin(sizeof Ethernet::buffer, myMac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));

  static byte myip[] = { 172,16,1,147 };
  static byte gwip[] = { 172,16,1,1 };
  static byte dnsip[] = { 8,8,8,8 };
  static byte mask[] = { 255,255,255,0 };

  ether.staticSetup(myip, gwip, dnsip , mask);

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  char payload[] = "My UDP message";
  uint8_t nSourcePort = 6454;
  uint8_t nDestinationPort = 6454;
  uint8_t broadcast[4] = { 255,255,255,255};

//ether.client_arp_whohas_blocking(ipDestinationAddress);
//delay(10);
//ether.sendUdp_mac(payload, sizeof(payload), nSourcePort, ipDestinationAddress, ether.returned_mac, nDestinationPort);

  ether.sendUdp(payload, sizeof(payload), nSourcePort, broadcast, nDestinationPort);

  Serial.println("UDP sent.");  
}

void loop() {
  ether.packetLoop(ether.packetReceive());
}
Avenitos commented 5 years ago

I did not immediately understand why the ntp demo works, but sending to my local server does not work. It turns out that any sending within the local segment does not work, sending via the gateway to the Internet address works normally. Why is that?

njh commented 5 years ago

It is probably something to do with the code that chooses the Layer 2 MAC address to send to. When sending to the Internet, EtherCard just needs the use the routers MAC address. When sending to the local subnet, it needs to find and use the right address. Although for broadcast this should just be FF:FF:FF:FF:FF:FF.

Have you tried sending to your subnet's broadcast address ? 172.16.1.255?

See also #59

marcin-koziol-iteo commented 4 years ago
 uint8_t nSourcePort = 6454;
  uint8_t nDestinationPort = 6454;

is the same as byte so you're not able to set port number larger than 255