From 9fbcf47d0ed8b053c4e3931766a7be8b0f919f0f Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Tue, 11 Feb 2014 17:14:05 +0100 Subject: [PATCH] fix ARP for outgoing UDP --- UIPEthernet.cpp | 5 ++++- UIPUdp.cpp | 43 ++++++++++++++++++++++++------------------- UIPUdp.h | 21 +++++++++++++-------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/UIPEthernet.cpp b/UIPEthernet.cpp index 30f527b..56c05e7 100644 --- a/UIPEthernet.cpp +++ b/UIPEthernet.cpp @@ -25,6 +25,8 @@ #include "HardwareSerial.h" #endif +#include "UIPUdp.h" + extern "C" { #include "utility/uip-conf.h" @@ -234,7 +236,7 @@ UIPEthernetClass::tick() // uip_len is set to a value > 0. */ if (uip_len > 0) { - network_send(); + UIPUDP::_send((uip_udp_userdata_t *)(uip_udp_conns[i].appstate)); } } #endif /* UIP_UDP */ @@ -281,6 +283,7 @@ void UIPEthernetClass::init(const uint8_t* mac) { uip_seteth_addr(mac); uip_init(); + uip_arp_init(); } void UIPEthernetClass::configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) { diff --git a/UIPUdp.cpp b/UIPUdp.cpp index d1cb7b1..d38de84 100644 --- a/UIPUdp.cpp +++ b/UIPUdp.cpp @@ -187,7 +187,7 @@ UIPUDP::endPacket() uip_udp_periodic_conn(_uip_udp_conn); if (uip_len > 0) { - UIPEthernet.network_send(); + _send(&appdata); return 1; } } @@ -352,7 +352,7 @@ uipudp_appcall(void) { void UIPUDP::uip_callback() { - if (appdata_t *data = (appdata_t *)(uip_udp_conn->appstate)) + if (uip_udp_userdata_t *data = (uip_udp_userdata_t *)(uip_udp_conn->appstate)) { if (uip_newdata()) { @@ -396,29 +396,34 @@ UIPUDP::uip_callback() { Serial.println(UIPEthernet.network.blockSize(data->packet_out)); #endif UIPEthernet.uip_packet = data->packet_out; - data->packet_out = NOBLOCK; UIPEthernet.uip_hdrlen = UIP_UDP_PHYH_LEN; - UIPEthernet.packetstate |= UIPETHERNET_SENDPACKET; uip_udp_send(data->out_pos - (UIP_UDP_PHYH_LEN)); - uip_process(UIP_UDP_SEND_CONN); //generate udp + ip headers - uip_arp_out(); //add arp - if (uip_len == UIP_ARPHDRSIZE) - { - UIPEthernet.packetstate &= ~UIPETHERNET_SENDPACKET; + } + } +} + +void +UIPUDP::_send(uip_udp_userdata_t *data) { + uip_arp_out(); //add arp + if (uip_len == UIP_ARPHDRSIZE) + { + UIPEthernet.uip_packet = NOBLOCK; + UIPEthernet.packetstate &= ~UIPETHERNET_SENDPACKET; #ifdef UIPETHERNET_DEBUG_UDP - Serial.println(F("udp, uip_poll results in ARP-packet")); + Serial.println(F("udp, uip_poll results in ARP-packet")); #endif - } - else - //arp found ethaddr for ip (otherwise packet is replaced by arp-request) - { - data->send = false; + } + else + //arp found ethaddr for ip (otherwise packet is replaced by arp-request) + { + data->send = false; + data->packet_out = NOBLOCK; + UIPEthernet.packetstate |= UIPETHERNET_SENDPACKET; #ifdef UIPETHERNET_DEBUG_UDP - Serial.print(F("udp, uip_packet to send: ")); - Serial.println(UIPEthernet.uip_packet); + Serial.print(F("udp, uip_packet to send: ")); + Serial.println(UIPEthernet.uip_packet); #endif - } - } } + UIPEthernet.network_send(); } #endif diff --git a/UIPUdp.h b/UIPUdp.h index c965ffa..aaae83b 100644 --- a/UIPUdp.h +++ b/UIPUdp.h @@ -35,20 +35,21 @@ extern "C" { #define UIP_UDP_NUMPACKETS 5 #endif +typedef struct { + memaddress out_pos; + memhandle packets_in[UIP_UDP_NUMPACKETS]; + memhandle packet_in; + memhandle packet_out; + boolean send; +} uip_udp_userdata_t; + class UIPUDP : public UDP { private: struct uip_udp_conn *_uip_udp_conn; - struct appdata_t - { - memaddress out_pos; - memhandle packets_in[UIP_UDP_NUMPACKETS]; - memhandle packet_in; - memhandle packet_out; - boolean send; - } appdata; + uip_udp_userdata_t appdata; public: UIPUDP(); // Constructor @@ -121,6 +122,10 @@ class UIPUDP : public UDP friend void uipudp_appcall(void); static void uip_callback(); + + friend class UIPEthernetClass; + static void _send(uip_udp_userdata_t *data); + }; #endif