Search code examples
socketsluaudpluasocket

luasocket send UDP data from port 1900


I am trying to Emulate Belkin wemo switch from my Home automation controller to send voice command from amazon Echo.The controller supports lua language.

I am following up this file to send the UDP data from port 1900 of Controller to port 50000 of Echo. right now every time i send the data the Socket take the random port send data not from port 1900. Echo only make valid connection and reply if the data comes from the port 1900. I am scratching my head from last two days to make is work but havent figured it out yet.

Bellow is my code.

strData1 =
  'HTTP/1.1 200 OK' .. 
 'HOST: 239.255.255.250:1900'..
 'CACHE-CONTROL: max-age=100'..
 'EXT:'..
 'LOCATION: http://192.168.1.152:49153/description.xml'..
 'SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/1.19.0'..
 'hue-bridgeid: B8AC6FFFFEC53475'..
 'ST: upnp:rootdevice'..
 'USN: uuid:2f402f80-da50-11e1-9b23-b8ac6fc53475::upnp:rootdevice'

 local socket = require "socket"
 local udp = socket.udp()

 udp:settimeout(0)
 udp:setsockname('*', 1900)
 udp:setpeername('192.168.1.102', 50000) -- Echo IP and Port number
 udp:sendto(strData1,'192.168.1.102',50000)

Solution

  • You were almost there: the documentation states that when using setpeername, send must be used instead of sendto.

    Also, I guess it was a typo, but you are trying to send Data instead of strData1. Here is the corrected version:

    strData1 =
      'HTTP/1.1 200 OK' .. 
     'HOST: 239.255.255.250:1900'..
     'CACHE-CONTROL: max-age=100'..
     'EXT:'..
     'LOCATION: http://192.168.1.152:49153/description.xml'..
     'SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/1.19.0'..
     'hue-bridgeid: B8AC6FFFFEC53475'..
     'ST: upnp:rootdevice'..
     'USN: uuid:2f402f80-da50-11e1-9b23-b8ac6fc53475::upnp:rootdevice'
    
     local socket = require "socket"
     local udp = socket.udp()
    
     udp:settimeout(0)
     udp:setoption('reuseaddr',true)
     udp:setsockname('*', 1900)
     udp:setpeername('192.168.1.102', 50000) -- Echo IP and Port number
     udp:send(strData1)
    

    Tcpdump's capture shows that the source port is correct:

    22:40:45.653222 IP my.super.secret.ip.1900 > 192.168.1.102.50000: UDP, length 280