// Return the IP address of the host who sent the current incoming packet
IPAddress
UIPUDP::remoteIP()
{
  return _uip_udp_conn ? ip_addr_uip(_uip_udp_conn->ripaddr) : IPAddress();
}
예제 #2
0
파일: UIPUdp.cpp 프로젝트: 12019/libraries
void UIPUDP::uip_callback(uip_udp_appstate_t *s) {
  if (appdata_t *data = (appdata_t *)s->user)
    {
      if (uip_newdata())
        {
          data->rport = ntohs(UDPBUF->srcport);
          data->ripaddr = ip_addr_uip(UDPBUF->srcipaddr);
          memhandle *packet = &data->packets_in[0];
          uint8_t i = 0;
          do
            {
              if (*packet == NOBLOCK)
                {
                  *packet = UIPEthernet.network.allocBlock(ntohs(UDPBUF->udplen)-UIP_UDPH_LEN);
                  //if we are unable to allocate memory the packet is dropped. udp doesn't guarantee packet delivery
                  if (*packet != NOBLOCK)
                    {
                      //discard Linklevel and IP and udp-header and any trailing bytes:
                      UIPEthernet.network.copyPacket(*packet,0,UIPEthernet.in_packet,UIP_UDP_PHYH_LEN,UIPEthernet.network.blockSize(*packet));
    #ifdef UIPETHERNET_DEBUG_UDP
                      Serial.print("udp, uip_newdata received packet: ");
                      Serial.print(*packet);
                      Serial.print(", slot: ");
                      Serial.print(i);
                      Serial.print(", size: ");
                      Serial.println(UIPEthernet.network.blockSize(*packet));
    #endif
                      break;
                    }
                }
              packet++;
              i++;
            }
          while (i < UIP_UDP_NUMPACKETS);
        }
      if (uip_poll() && data->send)
        {
          //set uip_slen (uip private) by calling uip_udp_send
#ifdef UIPETHERNET_DEBUG_UDP
          Serial.print("udp, uip_poll preparing packet to send: ");
          Serial.print(data->packet_out);
          Serial.print(", size: ");
          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_OUTPACKETOFFSET + 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;
#ifdef UIPETHERNET_DEBUG_UDP
              Serial.println("udp, uip_poll results in ARP-packet");
#endif
            }
          else
          //arp found ethaddr for ip (otherwise packet is replaced by arp-request)
            {
              data->send = false;
#ifdef UIPETHERNET_DEBUG_UDP
              Serial.print("udp, uip_packet to send: ");
              Serial.println(UIPEthernet.uip_packet);
#endif
            }
        }
    }
}