Recognize UDP Broadcast

tomboli
Posts: 3
Joined: Wed Oct 14, 2015 3:14 am

Recognize UDP Broadcast

Postby tomboli » Thu Oct 15, 2015 3:53 pm

Hello,
it seems that there is no possibility to detect if the device has been addressed by a broadcast.

Code: Select all

LOCAL struct espconn    udpconn;        // udp server

void user_devicefind_recv(void *arg, char *data, unsigned short data_len) {

    struct espconn          *pConnection = (struct espconn*)arg;
    remot_info              *premot = NULL;

   espconn_get_connection_info(pConnection, &premot, 0);  // get sender data (source IP)
   os_memcpy(udpconn.proto.udp->remote_ip, premot->remote_ip, 4);
   udpconn.proto.udp->remote_port = premot->remote_port;

   debug("UDP remote: " IPSTR ":%d\n",  IP2STR(udpconn.proto.udp->remote_ip), udpconn.proto.udp->remote_port);
   // shows correct source IP
   
   debug("UDP local:  " IPSTR ":%d\n",  IP2STR(pConnection->proto.udp->local_ip), pConnection->proto.udp->local_port);
   // shows always the current device IP; never a broadcast address
}


Any hints?

Thanks a lot

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

Re: Recognize UDP Broadcast

Postby ESP_Faye » Fri Oct 16, 2015 10:17 am

Hi,

Here a sample code:

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
 user_udp_recv_cb(void *arg, char *pusrdata, unsigned short length)
 {
     os_printf("recv udp data: %s\n", pusrdata);
     struct espconn *pesp_conn = arg;
     
       remot_info *premot = NULL;
       sint8 value = ESPCONN_OK;
       if (espconn_get_connection_info(pesp_conn,&premot,0) == ESPCONN_OK){
          char *pbuf = "udp_sent\n";
             pesp_conn->proto.tcp->remote_port = premot->remote_port;
             pesp_conn->proto.tcp->remote_ip[0] = premot->remote_ip[0];
             pesp_conn->proto.tcp->remote_ip[1] = premot->remote_ip[1];
             pesp_conn->proto.tcp->remote_ip[2] = premot->remote_ip[2];
             pesp_conn->proto.tcp->remote_ip[3] = premot->remote_ip[3];
             espconn_send(pesp_conn, pusrdata, os_strlen(pusrdata));
       }
 }

tomboli
Posts: 3
Joined: Wed Oct 14, 2015 3:14 am

Re: Recognize UDP Broadcast

Postby tomboli » Thu Oct 22, 2015 4:04 pm

It seems that we misunderstood...
espconn_get_connection_info() gets the sender address which is needed for example for a response as you showed in your example
We wanted to solve another issue:
The ESP device can be addressed by its IP or by a broadcast address - I didn't find any way to distinguish that!
Example:
remote device with 192.168.4.50 sends an udp packet to ESP device having the 192.168.4.1 optionally in 2 ways:
1. 192.168.4.50 sends to 192.168.4.1
2. 192.168.4.50 sends to 192.168.4.255
In both cases ESP receives the packet well and can respond to 192.168.4.50. The problem is to see if it was addressed via 192.168.4.1 or 192.168.4.255.
The requirement is to send a response only if it was addressed direct but not via broadcast! Today we solved that with a flag within the payload. This requires that all callers do this correct which may not be in our response an so far is not satisfying.

eriksl
Posts: 159
Joined: Fri May 22, 2015 6:22 pm

Re: Recognize UDP Broadcast

Postby eriksl » Mon Oct 26, 2015 2:21 am

There are two ways:

- check MAC layer broadcast address, but you need access to the MAC layer for that (WLAN frame)
- if you know the ip address AND the netmask, you can assume the first and the last addresses in the range are broadcast/net.

AFAIK you can inquire the destination address for an UDP packet.

tomboli
Posts: 3
Joined: Wed Oct 14, 2015 3:14 am

Re: Recognize UDP Broadcast

Postby tomboli » Mon Oct 26, 2015 9:16 pm

@eriksl:
1. when reading the SDK 1.4 documentation I found no hint how to access the MAC layer of an incoming UDP-packet/datagram
2. network + broadcast addresses are known for sure - but the requirement is to know if the broadcast address had been used by foreign sender

eriksl
Posts: 159
Joined: Fri May 22, 2015 6:22 pm

Re: Recognize UDP Broadcast

Postby eriksl » Sun Nov 01, 2015 5:35 pm

Then you would need the destination address. I am sure it must be available "somewhere"?

Who is online

Users browsing this forum: No registered users and 83 guests