UDP Multicast STA and AP

rsimpsonbusa
Posts: 124
Joined: Tue May 17, 2016 8:12 pm

UDP Multicast STA and AP

Postby rsimpsonbusa » Sun Dec 02, 2018 5:53 pm

Hi Y'all

I am trying to make a Repeater for UDP, where a "Main" has a "Repeater" who has a "Station".

In the Repeater, I have a connection to the SSID of the Main and make an AP ssid named Repeater. Both MAIN and REPEATER show up as valid SSID and when connected via a PC, they give a valid Ip each within its defined service range, 192.168.4.x for MAIN and 192.168.10.x for the Repeater.

In the Repeater I want to received Multicast messages on separate tasks, one for the AP section and one for the STA section, each one creating its socket and making it multicast aware. I create and bind socket like this (for the time being manual ip setting)

Code: Select all

    // Bind the socket to any address
    saddr.sin_family = PF_INET;
    saddr.sin_port = htons(port);
//    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if(ap)
    	inet_aton("192.168.10.1", &saddr.sin_addr.s_addr);
    else
    	inet_aton("192.168.4.1", &saddr.sin_addr.s_addr);

  //  saddr.sin_addr.s_addr =	ip_info.ip.addr;
    err = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
    if (err < 0) {
        ESP_LOGE(TAG, "Failed to bind socket. Error %d %s", errno,strerror(errno));
        goto err;
    }
I set the same address for multicast 232.10.11.12 but with its distinct interface as so

Code: Select all

    	
    		struct in_addr        localInterface;
		tcpip_adapter_ip_info_t if_info;
		if(ap)
			tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &if_info);
		else
			tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &if_info);
	    ESP_LOGI(TAG,"Adapter Ip %d:%d:%d:%d",IP2STR(&if_info.ip));

		localInterface.s_addr =(in_addr_t) if_info.ip.addr;
		if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,(char *)&localInterface,sizeof(localInterface)) < 0)
		{
			ESP_LOGE(TAG,"Setting local interface %d %s\n",errno,strerror(errno));
			close(sock);
			return 1;
		  }
Regretfully I do not receive messages on the two tasks independently only in the STA receiving task and for ALL messages, that is, when analyzed which ip sent the message, its either a MAIN ip or REPEATER ip. I could use this address as the logic for repeating, but the question is why does the STA Task receive messages from them AP section?

My question is if this is possible, that is, to have a task for receiving multicast messages for AP and STA or multicast will be sent to all interfaces, no matter how you set the socket of each task receiver interface.

When setting the socket and use the ANY_ADDR I do get messages in both tasks from the MAIN, which is not the intention. The STA receiver should get a message but not the AP receiver, which does.

Any help will be appreciated.

rsimpsonbusa
Posts: 124
Joined: Tue May 17, 2016 8:12 pm

Re: UDP Multicast STA and AP

Postby rsimpsonbusa » Tue Dec 04, 2018 12:13 am

Well, I'll answer my own question I guess.

Receiving Multicast is not based on IP, but on the PORT assigned. Ex, task connected to the Controller is a STA (IP 192.168.4.1) and the task that "repeats downstream" is the gateway of the AP (IP 192.168.10.1) and both create a Multicast Socket with the same PORT, both Tasks will receive the message irrelevant of how you set the Interface in the socket setup(again for receiving).

Pretty good explanation here https://docs.oracle.com/cd/E19455-01/80 ... index.html

The interface is important ONLY when sending, where one selects the "way out", in this case the STA or AP channel.

Therefore the Repeater will always receive the Multicast Message in the AP and STA tasks since they have the same PORT (which MUST be set as REUSABLE) and the only way I found to do the switching is to create just ONE receiving task and switch/decide based on the received IP.

UP will be a message sent from a Station of the Repeater (192.168.10.x in this example to 192.168.4.1) and DOWN a message from the Controller to the Repeater Stations (192.168.4.1 to 192.168.10.x). In each case the message to send must be set with the AP and STA address

Code: Select all

tcpip_adapter_get_ip_info(ap?TCPIP_ADAPTER_IF_STA:TCPIP_ADAPTER_IF_AP, &if_info);//for repeater AP and STA should be interchangeable
I did try to create the socket with a specific IP instead for ANY_ADDR to no avail. What works with 2 different tasks is if one changes the PORT or the Multicast Address(well dah!).

From the many answer I got ;) it seems nobody cares (UDP to basic I guess) or it is that "complicated" which really means that the TCP/IP stack is designed that way (apparently).

Hope it helps.

Who is online

Users browsing this forum: No registered users and 217 guests