| 1 | -- Copyright (C) 2011-2015 Anton Burdinuk
|
|---|
| 2 | -- clark15b@gmail.com
|
|---|
| 3 | -- https://tsdemuxer.googlecode.com/svn/trunk/xupnpd
|
|---|
| 4 |
|
|---|
| 5 | ssdp_msg_alive={}
|
|---|
| 6 | ssdp_msg_byebye={}
|
|---|
| 7 |
|
|---|
| 8 | function ssdp_msg(nt,usn,nts)
|
|---|
| 9 | return string.format(
|
|---|
| 10 | 'NOTIFY * HTTP/1.1\r\n'..
|
|---|
| 11 | 'HOST: 239.255.255.250:1900\r\n'..
|
|---|
| 12 | 'CACHE-CONTROL: max-age=%s\r\n'..
|
|---|
| 13 | 'LOCATION: %s\r\n'..
|
|---|
| 14 | 'NT: %s\r\n'..
|
|---|
| 15 | 'NTS: %s\r\n'..
|
|---|
| 16 | 'Server: %s\r\n'..
|
|---|
| 17 | 'USN: %s\r\n\r\n',
|
|---|
| 18 | cfg.ssdp_max_age,ssdp_location,nt,nts,ssdp_server,usn
|
|---|
| 19 | )
|
|---|
| 20 | end
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | ssdp_service_list=
|
|---|
| 24 | {
|
|---|
| 25 | 'upnp:rootdevice',
|
|---|
| 26 | 'urn:schemas-upnp-org:device:MediaServer:1',
|
|---|
| 27 | 'urn:schemas-upnp-org:service:ContentDirectory:1',
|
|---|
| 28 | 'urn:schemas-upnp-org:service:ConnectionManager:1',
|
|---|
| 29 | 'urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1'
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | function ssdp_init(t,nts)
|
|---|
| 33 | local uuid='uuid:'..ssdp_uuid
|
|---|
| 34 |
|
|---|
| 35 | table.insert(t,ssdp_msg(ssdp_uuid2,ssdp_uuid2,nts))
|
|---|
| 36 |
|
|---|
| 37 | for i,s in ipairs(ssdp_service_list) do
|
|---|
| 38 | table.insert(t,ssdp_msg(s,ssdp_uuid2..'::'..s,nts))
|
|---|
| 39 | end
|
|---|
| 40 |
|
|---|
| 41 | end
|
|---|
| 42 |
|
|---|
| 43 | function ssdp_alive()
|
|---|
| 44 | for i,s in ipairs(ssdp_msg_alive) do
|
|---|
| 45 | ssdp.send(s)
|
|---|
| 46 | end
|
|---|
| 47 | end
|
|---|
| 48 |
|
|---|
| 49 | function ssdp_byebye()
|
|---|
| 50 | for i,s in ipairs(ssdp_msg_byebye) do
|
|---|
| 51 | ssdp.send(s)
|
|---|
| 52 | end
|
|---|
| 53 | end
|
|---|
| 54 |
|
|---|
| 55 | function ssdp_handler(what,from,msg)
|
|---|
| 56 | if msg.reqline[1]=='M-SEARCH' and msg['man']=='ssdp:discover' then
|
|---|
| 57 | local st=msg['st']
|
|---|
| 58 |
|
|---|
| 59 | local resp=false
|
|---|
| 60 |
|
|---|
| 61 | if st=='ssdp:all' or st==ssdp_uuid2 then resp=true else
|
|---|
| 62 | for i,s in ipairs(ssdp_service_list) do
|
|---|
| 63 | if st==s then resp=true break end
|
|---|
| 64 | end
|
|---|
| 65 | end
|
|---|
| 66 |
|
|---|
| 67 | if resp then
|
|---|
| 68 | ssdp.send(string.format(
|
|---|
| 69 | 'HTTP/1.1 200 OK\r\n'..
|
|---|
| 70 | 'CACHE-CONTROL: max-age=%s\r\n'..
|
|---|
| 71 | 'DATE: %s\r\n'..
|
|---|
| 72 | 'EXT:\r\n'..
|
|---|
| 73 | 'LOCATION: %s\r\n'..
|
|---|
| 74 | 'Server: %s\r\n'..
|
|---|
| 75 | 'ST: %s\r\n'..
|
|---|
| 76 | 'USN: %s::%s\r\n\r\n',
|
|---|
| 77 | cfg.ssdp_max_age,os.date('!%a, %d %b %Y %H:%M:%S GMT'),ssdp_location,ssdp_server,st,ssdp_uuid2,st),from)
|
|---|
| 78 | end
|
|---|
| 79 | end
|
|---|
| 80 | end
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | events["SSDP"]=ssdp_handler
|
|---|
| 84 | events["ssdp_timer"]=function (what,sec) ssdp_alive() core.timer(sec,what) end
|
|---|
| 85 |
|
|---|
| 86 | ssdp.init(cfg.ssdp_interface,1,cfg.ssdp_loop,cfg.debug) -- interface, ttl, allow_loop, debug (0,1 or 2)
|
|---|
| 87 |
|
|---|
| 88 | www_location='http://'..ssdp.interface()..':'..cfg.http_port
|
|---|
| 89 |
|
|---|
| 90 | ssdp_location=www_location..'/dev.xml'
|
|---|
| 91 |
|
|---|
| 92 | if not cfg.uuid or cfg.uuid=='' then ssdp_uuid=core.uuid() else ssdp_uuid=cfg.uuid end
|
|---|
| 93 |
|
|---|
| 94 | ssdp_uuid2='uuid:'..ssdp_uuid
|
|---|
| 95 | ssdp_server='eXtensible UPnP agent'
|
|---|
| 96 |
|
|---|
| 97 | ssdp_init(ssdp_msg_alive,'ssdp:alive')
|
|---|
| 98 | ssdp_init(ssdp_msg_byebye,'ssdp:byebye')
|
|---|
| 99 |
|
|---|
| 100 | ssdp_alive()
|
|---|
| 101 | core.timer(cfg.ssdp_notify_interval,"ssdp_timer")
|
|---|
| 102 |
|
|---|
| 103 | table.insert(atexit,ssdp_byebye)
|
|---|