Author Topic: Setting UDP destination IP  (Read 3816 times)

mmartini

  • Newbie
  • *
  • Posts: 2
    • View Profile
Setting UDP destination IP
« on: June 08, 2011, 07:09:10 AM »
Hi,

I am using UDP to broadcast data packets.  I know the destination machine IP.

I'm modifying websrvr65_mc_hw211 to do what I need.
If I understand the code correctly, the destination UDP IP is set based on the first incoming UDP packet received, and this could be anything.  I'd like to set that UDP destination IP to something specific in projdefs.h just like one can set MY_DEFAULT_DNS etc. but am having trouble figuring out exactly how.

Here's where the UDP destination IP is set in event.c
Code: [Select]
/**
 * Initializes "UDP Command Port" and "UDP Event Port". These
 * ports are used for sending and receiving and commands via the UDP port.
 *
 */
void evtInit(void) {
    NODE_INFO udpServerNode;
    activeEventPorts = 0;
   
    //Initialize to idle state
    smEvt = SM_EVT_INIT;

    //Initialize remote IP and MAC address of udpServerNode with 0, seeing that we don't know them for the node
    //that will send us an UDP message. The first time a message is received addressed to this port, the
    //remote IP and MAC addresses are automatically updated with the addresses of the remote node.
    memclr(&udpServerNode, sizeof(udpServerNode));

    //Configure for local port 54124 and remote port INVALID_UDP_PORT. This opens the socket to
    //listen on the given port.
    udpSocketEvt = UDPOpen(EVENT_UDPPORT, &udpServerNode, INVALID_UDP_PORT);
   
    //An error occurred during the UDPOpen() function
    if (udpSocketEvt == INVALID_UDP_SOCKET) {
        #if (DEBUG_CMD >= LOG_ERROR)
        debugPutMsg(17);     //@mxd:17:Could not open UDP event port
        #endif
    }
}


For instance, I know I can set the ports easily when I open the stream:

Code: [Select]
    //udpSocketUser = UDPOpen(54127, &udpServerNode, 54128);
    udpSocketUser = UDPOpen(DEFAULT_BUS_UDP1PORT, &udpServerNode, DEFAULT_MOXA_UDPPORT);

But how to set the destination IP info in NODE_INFO udpServerNode?

I'm thinking this is obvious, but I'm a bit of a TCPIP newbie and I haven't found it and would appreciate someone getting me unstuck.


Thanks,
Marinna

ruku

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: Setting UDP destination IP
« Reply #1 on: August 19, 2011, 11:45:57 AM »
If you need to change destination IP you can do next...

NODE_INFO prueba_node;
.
.
.
prueba_node.IPAddr.v[0] = 192;    //If destination IP is 192.168.0.10
prueba_node.IPAddr.v[1] = 168;
prueba_node.IPAddr.v[2] = 0;
prueba_node.IPAddr.v[3] = 10;

//now modify info of transmit socket
UDPSocketInfo[udpsocketTX].remoteNode=prueba_node; //then udpflush send packet to 192.168.0.10
UDPFlush();


sorry for my very bad english, but this is that I do to send UDP packets

mmartini

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Setting UDP destination IP
« Reply #2 on: August 22, 2011, 11:33:20 AM »
Hi,

How do you know what the udpsocketTX is?
I tried using the result of opening the UDP socket, but that basically wiped my system's network settings

static UDP_SOCKET udpSocketUser0 = INVALID_UDP_SOCKET;
.
.
void main(void)
{
   NODE_INFO udpServerNode0;
.
.
.
    udpServerNode0.IPAddr.v[0] = MOXA_DEST_ADDR0; // set in projdefs.h
    udpServerNode0.IPAddr.v[1] = MOXA_DEST_ADDR1;
    udpServerNode0.IPAddr.v[2] = MOXA_DEST_ADDR2;
    udpServerNode0.IPAddr.v[3] = MOXA_DEST_ADDR3;

   udpSocketUser0 = UDPOpen(DEFAULT_BUS_UDP1PORT, &udpServerNode0, DEFAULT_MOXA_UDPPORT);

   UDPSocketInfo[udpSocketUser0].remoteNode= udpServerNode0;
   UDPFlush();
.
.
.
}

I would prefer to set the destination IP this way explicitly, because we know the IP and MAC addresses of the machine we want to send UDP to, the only thing we have found that didn't cause malformed packets was to do an ARP check.  But if the ARP check fails, back to the malformed packet problem.  The machine on the receive end is LINUX, and won't deal with the malformed packets.  Seems crazy if we know the destination info that we can't just set it.

Clearly I'm missing something in my network understanding.

Marinna

Marinna