espconn_get_connection_info()

hdrut
Posts: 25
Joined: Fri Feb 13, 2015 11:02 am
Location: Argentina

espconn_get_connection_info()

Postby hdrut » Sat Sep 19, 2015 9:35 pm

Hi,


I am trying to use this function to get IP address of sender's but have had no success.
I use:

remot_info *pinfo = NULL;
espconn_get_connection_info(esp_conn, &pinfo , 0);


Then print:

os_printf("Remote IP address: %u\n", pinfo.remote_ip);


And get 1073675224, which do not correspond to sender´s IP (in my case, 192.168.1.65 = 1090627776).

Also tried:

int i = 0;
while(i <= 100) {
os_printf("Remote IP address: %u\n", pinfo[i].remote_ip);
i++;
};

And got numbers in the same range as above 1073675224, increasing in 12 (1073675224, 1073675248, 1073675260, etc.)

What is the meaning of link_cnt??
How can i find the sender's IP address??


Please provide some insight. Thanks in advance!!


Horacio

bojanpotocnik
Posts: 9
Joined: Wed Jan 14, 2015 6:20 pm

Re: espconn_get_connection_info()

Postby bojanpotocnik » Mon Oct 05, 2015 5:15 pm

Hello,

function espconn_get_connection_info(), called in UDP Data Received Callback always returns remote_ip as 0.0.0.0. How to get around that, how to know remote IP of the sender of UDP data?

Regards,
Bojan

Code: Select all

static void ICACHE_FLASH_ATTR
uart_server_Data_Received_Callback(void *arg, char *pdata, unsigned short len)
{
   struct espconn* conn = (struct espconn*)arg;

   dprintf(DEBUG_INFO, "Server received %d bytes\r\n", len);

   /* Parameters remote_ip and REMOTE_PORT in it are the remote IP and Port set by espconn_create.
    * If users want to obtain IP and ports of the current sender, please
    * call espconn_get_connection_info relevant to get information. */
   if(conn->type == ESPCONN_UDP) {
      remot_info *remote = NULL;
      if(espconn_get_connection_info(conn, &remote, 0) == 0) {
         dprintf(DEBUG_WARNING, "Remote IP: " IPSTR "\r\n", IP2STR(remote->remote_ip));
      }
      else {
         dprintf(DEBUG_WARNING, "Cannot get sender IP\r\n");
      }
   }
   else {
      dprintfnp(DEBUG_INFO, "from " IPSTR ":%d ", IP2STR(conn->proto.tcp->remote_ip), conn->proto.tcp->remote_port);
   }
}


Server received 9 bytes
Remote IP: 0.0.0.0

bojanpotocnik
Posts: 9
Joined: Wed Jan 14, 2015 6:20 pm

Re: espconn_get_connection_info()

Postby bojanpotocnik » Mon Oct 05, 2015 8:23 pm

Now, somehow, just started to work but it reports IP 192.168.0.114 (there is no device with such IP on my network) instead of my IP 192.168.0.101. If I send data over TCP, it correctly reports IP 192.168.0.101. How is that possible? And I see that I am not the only one with wrong IP problem :?

ESP_Faye
Posts: 1642
Joined: Mon Oct 27, 2014 11:08 am

Re: espconn_get_connection_info()

Postby ESP_Faye » Thu Oct 08, 2015 5:12 pm

Hi,

Sorry that we can not duplicate your problem.

Please provide your test code, we will have a try.

Thanks for your interest in ESP8266 !

lobaro
Posts: 7
Joined: Fri Nov 13, 2015 10:31 pm

Re: espconn_get_connection_info()

Postby lobaro » Fri Nov 13, 2015 10:36 pm

bojanpotocnik wrote:Hello,

function espconn_get_connection_info(), called in UDP Data Received Callback always returns remote_ip as 0.0.0.0. How to get around that, how to know remote IP of the sender of UDP data?

Regards,
Bojan

Code: Select all

static void ICACHE_FLASH_ATTR
uart_server_Data_Received_Callback(void *arg, char *pdata, unsigned short len)
{
   struct espconn* conn = (struct espconn*)arg;

   dprintf(DEBUG_INFO, "Server received %d bytes\r\n", len);

   /* Parameters remote_ip and REMOTE_PORT in it are the remote IP and Port set by espconn_create.
    * If users want to obtain IP and ports of the current sender, please
    * call espconn_get_connection_info relevant to get information. */
   if(conn->type == ESPCONN_UDP) {
      remot_info *remote = NULL;
      if(espconn_get_connection_info(conn, &remote, 0) == 0) {
         dprintf(DEBUG_WARNING, "Remote IP: " IPSTR "\r\n", IP2STR(remote->remote_ip));
      }
      else {
         dprintf(DEBUG_WARNING, "Cannot get sender IP\r\n");
      }
   }
   else {
      dprintfnp(DEBUG_INFO, "from " IPSTR ":%d ", IP2STR(conn->proto.tcp->remote_ip), conn->proto.tcp->remote_port);
   }
}


Server received 9 bytes
Remote IP: 0.0.0.0


Did you solve this by now? I have exactly the same problem.

Using Non-OS SDK 1.4 and calling espconn_get_connection_info in receive callback of an udp socket...
The remote ip&port are zero, but pData and len are finde/correct.

lobaro
Posts: 7
Joined: Fri Nov 13, 2015 10:31 pm

Re: espconn_get_connection_info()

Postby lobaro » Fri Nov 13, 2015 11:30 pm

Hi,
had the same issue! But found a "solution"...

In the receive callback of an udp socket we called "espconn_get_connection_info()" and got only 0.0.0.0:0 for ip and port information of the remote sender. The actual data (pData) und length (len) were ok and valid! After doing some trial and error tests we figured out that opening an (different, 2nd) udp socket in "user_init()" makes the difference between valid remote sender information in the callback (of the udp socket created not in user_init) and the 0.0.0.0:0 error behavior:

in user_init():
[...]

Code: Select all

   Conn.type = ESPCONN_UDP;
   Conn.state = ESPCONN_NONE;
   Conn.proto.udp = &UdpConn;
   uint32_t ip =  ipaddr_addr("192.168.1.77");
   Conn.proto.udp->local_port = espconn_port();
   Conn.proto.udp->remote_port = UDP_SERVER_PORT;
   os_memcpy(Conn.proto.udp->remote_ip, &ip, 4);

   sint8 statusCreate = espconn_create(&Conn); //<---- comment this out and no error!


@Espressif
Do you have an idea why this error occurs when calling espconn_create in user_init()?
Thanks!

Tobi

ESP_Faye
Posts: 1642
Joined: Mon Oct 27, 2014 11:08 am

Re: espconn_get_connection_info()

Postby ESP_Faye » Wed Nov 18, 2015 1:42 pm

Hi lobaro,

Here is my test code, please have a try.
Attachments
UDP_test_bin.zip
(154.35 KiB) Downloaded 224 times

Simon
Posts: 1
Joined: Mon Nov 23, 2015 12:44 am

Re: espconn_get_connection_info()

Postby Simon » Mon Nov 23, 2015 1:20 am

Hello All,

we are developing uPanel, a new firmware for ESP8266 that permits to control any micro-controller (such that of Arduino) through Smart-Phones by means of customisable graphical interfaces (but without the need to write the phone App).

Testing the ESP8266 SDK we found the same problem with the function espconn_get_connection_info(), so we decided to dig a little...

The problem arises from the fact that this function actually returns (a pointer to) an array of "remot_info", with one element for each created UDP socket. This means that if you have only one UDP socket, the returned parameters are correct, but if you have more than one, the returned parameters contained into the first array position are related to the first created socket (temporally first). Therefore, you should examine all the array: the parameter (espconn)->link_cnt will tell you how many elements are present into the returned array remot_info.

Unfortunately, there is no way to find out which is the array element related to the current UDP (we didn't find one...).

Digging into the old sdk code (0.9.4), the espconn_get_connection_info function contains the following code:

case ESPCONN_UDP:
while(plist != NULL){
if (plist->pespconn->type == ESPCONN_UDP){
premot[pespconn->link_cnt].state = plist->pespconn->state;
premot[pespconn->link_cnt].remote_port = plist->pcommon.remote_port;
os_memcpy(premot[pespconn->link_cnt].remote_ip, plist->pcommon.remote_ip, 4);
pespconn->link_cnt ++;
}
plist = plist->pnext;
}

Dear Espressif team, we suggest the following change to the if condition:

if ((plist->pespconn->type == ESPCONN_UDP) && (plist->pespconn == pespconn))

This would return the info only of the wanted UDP socket.

In our tests, we solved this issue creating our function espconn_get_connection_info using the suggested change.

If someone is interested in the source code, let us know by email.

Project website: http://www.miuPanel.com

Regards,
miuPanel Team

lobaro
Posts: 7
Joined: Fri Nov 13, 2015 10:31 pm

Re: espconn_get_connection_info()

Postby lobaro » Tue Nov 24, 2015 4:21 pm

My problem has been solved using V1.41 patch of the sdk: viewtopic.php?f=46&t=1268

Anki
Posts: 25
Joined: Thu Apr 09, 2015 3:00 am

Re: espconn_get_connection_info()

Postby Anki » Thu Jan 07, 2016 6:36 am

This problem seems to have reappeared in the SDK 1.5.0

You now have to use the undocumented espconn_get_connection_info function to find out the remote IP address and port of received data. E.g.

Code: Select all

static void socketRecvCB(void *arg, char *usrdata, unsigned short len)
{
  struct espconn* src = (struct espconn*)arg;
 
  remot_info *remote = NULL;
  sint8 err;
  err = espconn_get_connection_info(src,&remote,0);


It's not clear how this would work with multiple connections either.

Espressif, this is a major bug which breaks any code implementing a UDP server. Please fix the API and / or document the espconn_get_connection_info function properly ASAP.

Who is online

Users browsing this forum: No registered users and 7 guests