Retrieve IP Address from UDP Packet

bonchenko

Retrieve IP Address from UDP Packet

Postby bonchenko » Sat Aug 08, 2015 12:03 am

Hello, I am trying to broadcast an UDP message in a network. All corresponding devices should respond to my message "DiscoverDevice". I successfully receive UDP packets from these devices, but I need their IP address. Does ESP8266 SDK allows me to retrieve IP address from UDP packet? I attach example code:

Code: Select all

#include "discover.h"

LOCAL struct espconn Conn;
static ETSTimer WiFiLinker;

uint8 udpServerIP[] = { 255, 255, 255, 255 };

/******************************************************************************
 * FunctionName : user_devicefind_recv
 * Description  : Processing the received data from the host
 * Parameters   : arg -- Additional argument to pass to the callback function
 *                pusrdata -- The received data (or NULL when the connection has been closed!)
 *                length -- The length of received data
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
recvdata(void *arg, char *pusrdata, unsigned short length)
{
   os_printf("Recv data callback\n");
    char DeviceBuffer[40] = {0};
    char Device_mac_buffer[60] = {0};
    char hwaddr[6];

    struct ip_info ipconfig;

    if (wifi_get_opmode() != STATION_MODE) {
        wifi_get_ip_info(SOFTAP_IF, &ipconfig);
        wifi_get_macaddr(SOFTAP_IF, hwaddr);

        if (!ip_addr_netcmp((struct ip_addr *)Conn.proto.udp->remote_ip, &ipconfig.ip, &ipconfig.netmask)) {
            wifi_get_ip_info(STATION_IF, &ipconfig);
            wifi_get_macaddr(STATION_IF, hwaddr);
        }
    } else {
        wifi_get_ip_info(STATION_IF, &ipconfig);
        wifi_get_macaddr(STATION_IF, hwaddr);
    }

    if (pusrdata == NULL) {
        return;
    }
   else {
      os_printf("recv data: %s\n", pusrdata); // only contains received UDP messages, I need the IP address
   }
}

void DISCOVER_Init(void) {
   os_printf("Entering DISCOVER mode\n");

      Conn.type = ESPCONN_UDP;
       Conn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
      Conn.proto.udp->local_port = 15002;
       Conn.proto.udp->remote_port = 15002;   
   os_memcpy(Conn.proto.udp->remote_ip, udpServerIP, 4);
       espconn_regist_recvcb(&Conn, recvdata);
       espconn_create(&Conn);

   os_timer_disarm(&WiFiLinker);
   os_timer_setfn(&WiFiLinker, (os_timer_func_t *)senddata, NULL);
   os_timer_arm(&WiFiLinker, 15000, 0);
}

static void ICACHE_FLASH_ATTR senddata()
{
   unsigned short length;
   os_timer_disarm(&WiFiLinker);
   os_printf("Broadcasting UDP DeviceDiscover...\n");

   espconn_sent(&Conn, "DeviceDiscover", 14);
}



Anyone have suggestion to get the source IP from these UDP packets?

User avatar
kolban
Posts: 131
Joined: Tue Jun 16, 2015 1:09 pm
Location: Fort Worth, Texas, USA

Re: Retrieve IP Address from UDP Packet

Postby kolban » Sat Aug 08, 2015 12:25 am

Howdy guys,
This is a duplicate question and answers are starting to appear here:

http://www.esp8266.com/viewtopic.php?f=6&t=4414

Who is online

Users browsing this forum: No registered users and 28 guests