UDP

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
2,057 Views
vines
Contributor III

Hi,

Is it possible to send UDP packets even if IP is not binded? Sorry if this sounds as a basic question. I am trying out the code below:

    sockaddr_in addrUdp;

    u32 sock,sockUdp;

    u32 error;

    u16 rlen;

    _enet_address enet_address = {0,27,134,1,0,99};

    error = RTCS_create();

    if (error!= RTCS_OK)

        _task_block();

    if ((ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE, enet_address)) != IPCFG_OK)

        _task_block();

       

    addrUdp.sin_family = AF_INET;

    addrUdp.sin_port = UDP_PORT;//defined port

    addrUdp.sin_addr.s_addr = INADDR_ANY;

    sockUdp = socket(AF_INET, SOCK_DGRAM, 0);

    if (sockUdp== RTCS_SOCKET_ERROR)

         _task_block();

    error= bind(sockUdp, &addrUdp, sizeof(addrUdp));

    if (error!= RTCS_OK)

         _task_block();

   

    while(1)

    {

        byte buffer[MAX_MESSAGE];

        sock= RTCS_selectall(-1);

        if (sock== sockUdp)

        {

          byte buffer[MAX_MESSAGE];

          u32 result;

          rlen = sizeof(addrUdp);

          result = recvfrom(sockUdp, buffer, 100, 0, &addrUdp, &rlen);

          if (result != RTCS_ERROR && result > 0 && result <= MAX_MESSAGE)

          {

              //if successfull addrUdp contains the src port and sin_addr to reply. Everything are correct

              error = sendto(sockUdp, buffer, result, 0, &addrUdp, rlen);

          }

        }

        _time_delay(100);

    }

I can receive the packets from my SW tool. However, when I try to send it out, it is having an error.When I debug the code I found out that it is having an error in function SOCK_DGRAM_sendto in file sdgram.c.

Error is 0x00001510 after returning from this line error = RTCSCMD_issue(parms, UDP_send);

When I search the error it is defined as RTCSERR_IP_UNREACH.

If I add an IP bind e.g.

ipcfg_bind_staticip(ENET_DEVICE, &ip)

Everything works. However,my device by default is factory reset to 0.0.0.0 IP, subnet and gateway. Thus, trying to bind will have an error. Configuring of IP is done via UDP.

I am using a switch that connects my PC and the device only.I am using wireshark to see if there is packet being transmitted from my device. There was none captured.

On the side note, I have a competitor product with the same setup as above (switch and my PC with competitor SW). Their device replies even if all are default values. I can see from wireshark 0.0.0.0 source. Thus, from a technical point I know it is possible. I just don't know how to do it on MQX.

Now back to my question is it possible to send UDP packets without IP binding? Do I need to set any other options in mqx?  Is there any setting in rtcs project to enable this? Or user_config.h? I played around these settings on user_config.h but to no avail

all either 1/0 does not work

RTCSCFG_DISCARD_SELF_BCASTS

RTCSCFG_ENABLE_VIRTUAL_ROUTES

RTCSCFG_IP_DISABLE_DIRECTED_BROADCAST

RTCSCFG_UDP_ENABLE_LBOUND_MULTICAST

Any help will be greatly appreciated.

Thank you very much!

0 Kudos
1 Solution
1,412 Views
Martin_
NXP Employee
NXP Employee

Seems you also need to add direct route (send this ip/network to this interface). Function IP_route_add_direct(). But it is not intended for usage directly from application, it is called internally by the bind process. 


I'm afraid the answer to your initial question is: unfortunately not. Can you or anybody else interested in this feature please describe, what would be the application use case for it ?

View solution in original post

11 Replies
1,412 Views
Martin_
NXP Employee
NXP Employee

I think you should add default gateway. (RTCS_gate_add(gateway_ip, INADDR_ANY, INADDR_ANY));

0 Kudos
1,412 Views
vines
Contributor III

Hi Martin,

I add this line but still the UDP send can't get thru. IP is set as 0.0.0.0

Since IP is still unknown to device and it is only connected to a switch (no DHCP server or gateway), DHCP won't work. Thus IP will be set as default 0.0.0.0

ip.ip = IPADDR(0, 0, 0, 0);

error = RTCS_gate_add(ip.ip,INADDR_ANY,INADDR_ANY);

//no error found after this step

The error is still the same RTCSERR_IP_UNREACH

Do you know if I need set anything on BSP level to make this work?

Thank You.

0 Kudos
1,412 Views
Martin_
NXP Employee
NXP Employee

If the IP address of the remote peer is from the same subnet then you won't need gateway and your code should work. So I assume you're trying to send to a peer from a different subnet, thus, the stack sends the packet via gateway. So there should be an entry in the routing table for this.

0 Kudos
1,412 Views
vines
Contributor III

Hi Martin,

Sorry if the following might sound stupid:

Yes my device does not belong to the same subnet since peer PC's IP is 192.1681.1.199. Then my device will be 0.0.0.0

Question though, how would Kinetis system know it's subnet if I didn't set/bind it to any IP to begin with? I mean I didn't use any ipcfg_bind_staticip or ipcfg_bind_dhcpxx methods.

Correct me if I am wrong, but this line

error= bind(sockUdp, &addrUdp, sizeof(addrUdp));

from above code is only signifying that it binds to all interfaces right?

You mentioned I should have an entry in the routing table? I am not sure how to do this?

Thank You.

0 Kudos
1,412 Views
Martin_
NXP Employee
NXP Employee

To add/remove an entry into/from routing table, use RTCS_gate_add()/RTCS_gate_remove(). The stack has to know where to send the datagram if the target is in a different subnet. Perhaps try a "trick" to set your target ip as a gateway ip (RTCS_gate_add(192.168.1.199, 0, 0)); then after UDP send you can remove the gateway I beleive. Not sure if this can work as I've never tried, but you can and give feedback here.

There is internal function in the stack (see for example how DHCP client is implemented - DHCPDISCOVER has source IP 0.0.0.0), but the interface towards user application seems more difficult to use than just add/remove gateway.

0 Kudos
1,412 Views
vines
Contributor III

Hi Martin

Adding the gateway before sending won't work

ip.ip = IPADDR(192, 168, 1, 199);

error = RTCS_gate_add(ip.ip,0,0);

The error still RTCSERR_IP_UNREACH.

Any other suggestions? Thanks.

0 Kudos
1,413 Views
Martin_
NXP Employee
NXP Employee

Seems you also need to add direct route (send this ip/network to this interface). Function IP_route_add_direct(). But it is not intended for usage directly from application, it is called internally by the bind process. 


I'm afraid the answer to your initial question is: unfortunately not. Can you or anybody else interested in this feature please describe, what would be the application use case for it ?

1,412 Views
vines
Contributor III

I used IP_route_add_direct and IP_route_remove_direct on my code.

Now I can send UDP messages even though I am not binded. It is a bit messy because I need to recreate all struct definitions and pointers as it is being handled by RTCS which is private to my application. As you mentioned these functions were not meant to be used in the application layer.

Anyway, these will do the trick for now in my use case.

Thank you very much for the help!

1,412 Views
arnogir
Senior Contributor II

Hello

I have exactly the same problem.

Like said above, the solution should be use an internal MQX API "IP_route_add_direct"

I try to use it, but without success. I must recreate a structur to pass in parameter but this is not easy..

Somebody can help me to know how call correclty this function? (When and with what parameter)

:smileyhappy:

0 Kudos
1,412 Views
vines
Contributor III

Hi Martin,

My device is a hub consolidating data acquired from other devices connected via wired communications (serial). In 1 system it can have several hubs connected to a central console(PC software) via Ethernet.


There are 2 layers of communication fro PC Software to Device Hub. The IP configurations (set/get settings of IP, subnet, gateway) is done in UDP. Any other communications will be via TCP (data gathering, control, etc). The reason will be explain later.


Connections from  Hub(s) and PC can be via switch only (no DHCP server, gateway) or it can also be connected to their own network with or without restrictions (meaning restricted open IP alloted to devices). If it is connected in the network with DHCP server and without restriction, it is not a problem since I can get the settings (IP) via DHCP. However if it is connected via switch only or network with allotted IPs, there will be a problem. Since we don't know whether this device will be connected to a public network 192.168.xxx.xxx or a private one (10.xx), there should be a default IP. Factory default IP will be 0.0.0.0. And the device does not have any idea of IP settings on the network. Consequently, the idea is to query/set IP settings via broadcast UDP 255.255.255.255. This will be receive by all devices on the network regardless of IP settings. It will only be differentiated by a unique ID (e.g MAC address) passed in data on the application layer. Devices will reply to this query or set provided it is the device being accessed. You can now set the device to your own network parameters.


For the use case:

Let say customer purchased 100 devices and installed in the network. Upon power up, all will have default IP. Since DHCP is not available. All will be in default IP. Customer will then use the configuration SW to change IPs of each device 1 by 1 so there is no IP conflict. Once you achieved this, you can now connect in the TCP layer for data communications.

Thanks.

1,412 Views
vines
Contributor III

Hi

Anybody can pick this up? Thank You.

0 Kudos