Skip to content

Change to udpPrepare to handle empty return MAC address #181

@luizrrocha

Description

@luizrrocha

Hi, I am using sendUdp to reply to a packet sent by a Cacti temperature logger. It is not working and using WireShark I discovered that the return package has 00:00:00:00:00:00 as the destination MAC address, which makes the packet to be ignored at the destination.

Analyzing your library, in udpPrepare() it is assumed that we already have the MAC in destmacaddr, which is not the case in a program the just uses udpServerListenOnPort() to receive and sendUdp() to reply. I have seen solutions such as setting setGwIp() to the sender's IP to force the ARP search for the sender's IP and load destmacaddr but I wanted a more default behavior from sendUdp().

I found that the following modification to udpPrepare() fixes the issue for me and would like to see if you can validate it and perhaps use it as a basis to fix the issue in the official code:

void EtherCard::udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport) {
    if(is_lan(myip, dip)) {    // this works because both dns mac and destinations mac are stored in same variable - destmacaddr
        if (!has_dest_mac)   // we don't have the destination mac, 
        setMACandIPs(gPB + ETH_SRC_MAC, dip);  // assume it is the same as the source UDP packet's mac
      else
        setMACandIPs(destmacaddr, dip);        // at different times. The program could have separate variable for dns mac, then here should be  
    } else {   // checked if dip is dns ip and separately if dip is hisip and then use correct mac.
        setMACandIPs(gwmacaddr, dip);
    }

Best regards,

Luiz Roberto Rocha
Rio de Janeiro - Brazil

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions