Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the mac address in the udp handler to use it in the reply packet #378

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/EtherCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
/** This type definition defines the structure of a UDP server event handler callback function */
typedef void (*UdpServerCallback)(
uint16_t dest_port, ///< Port the packet was sent to
const uint8_t *src_mac,
uint8_t src_ip[IP_LEN], ///< IP address of the sender
uint16_t src_port, ///< Port the packet was sent from
const char *data, ///< UDP payload data
Expand Down Expand Up @@ -263,7 +264,7 @@ class EtherCard : public Ethernet {
* @param dip Pointer to 4 byte destination IP address
* @param dport Destination port
*/
static void udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport);
static void udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport, const uint8_t *dmac = nullptr);

/** @brief Transmit UDP packet
* @param len Size of payload
Expand All @@ -278,7 +279,7 @@ class EtherCard : public Ethernet {
* @param dport Destination port
*/
static void sendUdp (const char *data, uint8_t len, uint16_t sport,
const uint8_t *dip, uint16_t dport);
const uint8_t *dip, uint16_t dport, const uint8_t *dmac = nullptr);

/** @brief Resister the function to handle ping events
* @param cb Pointer to function
Expand Down
12 changes: 8 additions & 4 deletions src/tcpip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,13 @@ uint8_t EtherCard::ntpProcessAnswer (uint32_t *time,uint8_t dstport_l) {
return 1;
}

void EtherCard::udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport) {
void EtherCard::udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport, const uint8_t *dmac) {
if(is_lan(myip, dip)) { // this works because both dns mac and destinations mac are stored in same variable - destmacaddr
setMACandIPs(destmacaddr, dip); // at different times. The program could have separate variable for dns mac, then here should be
if (dmac!=nullptr){
setMACandIPs(/*destmacaddr*/dmac, dip); // at different times. The program could have separate variable for dns mac, then here should be
}else{
setMACandIPs(destmacaddr, dip);
}
} else { // checked if dip is dns ip and separately if dip is hisip and then use correct mac.
setMACandIPs(gwmacaddr, dip);
}
Expand Down Expand Up @@ -408,8 +412,8 @@ void EtherCard::udpTransmit (uint16_t datalen) {
}

void EtherCard::sendUdp (const char *data, uint8_t datalen, uint16_t sport,
const uint8_t *dip, uint16_t dport) {
udpPrepare(sport, dip, dport);
const uint8_t *dip, uint16_t dport, const uint8_t *dmac) {
udpPrepare(sport, dip, dport, dmac);
if (datalen>220)
datalen = 220;
memcpy(gPB + UDP_DATA_P, data, datalen);
Expand Down
1 change: 1 addition & 0 deletions src/udpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ bool EtherCard::udpServerHasProcessedPacket(uint16_t plen) {
uint16_t datalen = (uint16_t) (gPB[UDP_LEN_H_P] << 8) + gPB[UDP_LEN_L_P] - UDP_HEADER_LEN;
listeners[i].callback(
listeners[i].port,
(const uint8_t *) gPB + ETH_SRC_MAC,
gPB + IP_SRC_P,
(gPB[UDP_SRC_PORT_H_P] << 8) | gPB[UDP_SRC_PORT_L_P],
(const char *) (gPB + UDP_DATA_P),
Expand Down