-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssdp-fake.py
executable file
·296 lines (259 loc) · 11 KB
/
ssdp-fake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# Copyright (C) 2014 Graham R. Cobb
# Released under GPL V2 -- see LICENSE
# Python multicast code taken from Stack Overflow (https://stackoverflow.com/questions/603852/multicast-in-python/1794373#1794373) by tolomea (https://stackoverflow.com/users/10471/tolomea) under CC BY-SA 3.0
# Other example code taken from Stack Overflow by Toddeman (under CC BY-SA 3.0), however it does not seem to be available any longer
import socket
import struct
import time
import select
import re
from optparse import OptionParser
VERSION='0.3'
DLNA_GRP = '239.255.255.250'
DLNA_PORT = 1900
MCAST_IF = '127.0.0.1'
CRLF = "\015\012"
#SERVER='192.168.0.238'
SERVER=''
UUID=''
URL=''
INTERVAL = 180
parser = OptionParser(usage="usage: %prog [options] server\n %prog --listen-only",
epilog="Server can be specified as hostname or IP address and should be omitted if --listen-only is used",
version="%prog "+VERSION)
parser.add_option("-a", "--all",
action="store_true", dest="allif", default=False,
help="send announcements to all interfaces, not just the loopback interface")
parser.add_option("-i", "--interval", type="int", dest="interval", default=INTERVAL,
help="seconds between notification updates (default %default)")
parser.add_option("-l", "--listen-only",
action="store_true", dest="listen", default=False,
help="just listen and display messages seen, do not contact a server or send announcements")
(options, args) = parser.parse_args()
LISTEN=options.listen
if len(args) == 0 and not LISTEN:
parser.error("server must be specified (hostname or IP address)")
if len(args) > 1:
parser.error("incorrect number of arguments")
if not LISTEN:
SERVER=args[0]
INTERVAL=options.interval
osock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
osock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
osock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 4)
osock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
if not options.allif:
mreq = struct.pack("4sl", socket.inet_aton(MCAST_IF), socket.INADDR_ANY)
osock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, mreq)
imsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
imsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
imsock.bind(('', DLNA_PORT))
mreq = struct.pack("4sl", socket.inet_aton(DLNA_GRP), socket.INADDR_ANY)
imsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
def notify(addr, port):
if (URL != '' and UUID != '' and not LISTEN):
# Note: responses should have ST:, notifies should have NT:
# We include both
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: urn:schemas-upnp-org:device:MediaServer:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:device:MediaServer:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: upnp:rootdevice' + CRLF \
+ 'USN: uuid:' + UUID + '::upnp:rootdevice' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: uuid:' + UUID + CRLF \
+ 'USN: uuid:' + UUID + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: urn:schemas-upnp-org:service:ContentDirectory:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:ContentDirectory:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: urn:schemas-upnp-org:service:ConnectionManager:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:ConnectionManager:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'NOTIFY * HTTP/1.1' + CRLF \
+ 'NT: urn:schemas-upnp-org:service:X_MS_MediaReceiverRegistrar:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:X_MS_MediaReceiverRegistrar:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
else:
print "Skipping notification"
def respond(addr, port):
if (URL != '' and UUID != '' and not LISTEN):
# Note: responses should have ST:, notifies should have NT:
# We include both
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: urn:schemas-upnp-org:device:MediaServer:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:device:MediaServer:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: upnp:rootdevice' + CRLF \
+ 'USN: uuid:' + UUID + '::upnp:rootdevice' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: uuid:' + UUID + CRLF \
+ 'USN: uuid:' + UUID + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: urn:schemas-upnp-org:service:ContentDirectory:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:ContentDirectory:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: urn:schemas-upnp-org:service:ConnectionManager:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:ConnectionManager:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
msg = 'HTTP/1.1 200 OK' + CRLF \
+ 'ST: urn:schemas-upnp-org:service:X_MS_MediaReceiverRegistrar:1' + CRLF \
+ 'USN: uuid:' + UUID + '::urn:schemas-upnp-org:service:X_MS_MediaReceiverRegistrar:1' + CRLF \
+ 'NTS: ssdp:alive' + CRLF \
+ 'LOCATION: ' + URL + CRLF \
+ 'HOST: 239.255.255.250:1900' + CRLF \
+ 'SERVER: ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ 'CACHE-CONTROL: max-age=' + str(INTERVAL * 10) + CRLF \
+ CRLF
print "Sending ("+addr+":"+str(port)+"): \n" + msg
osock.sendto(msg, (addr, port))
else:
print "Skipping response"
def server():
if not LISTEN:
msg = ('M-SEARCH * HTTP/1.1' + CRLF \
+ 'Host: %s:%d' + CRLF \
+ 'Man: "ssdp:discover"' + CRLF \
+ 'ST: upnp:rootdevice' + CRLF \
+ 'MX: 3' + CRLF \
+ 'User-Agent:ssdp-fake/0 DLNADOC/1.50 UPnP/1.0 ssdp-fake/0' + CRLF \
+ CRLF) % (SERVER, DLNA_PORT)
print "Sending to server: \n" + msg
osock.sendto(msg, (SERVER, DLNA_PORT))
def parse_msg(msg):
global URL, UUID, last_update, next_notification
if (re.match('^HTTP/1.1\s*200\s*OK', msg, re.IGNORECASE)):
# Response to our M-SEARCH
match = re.search(r'^LOCATION:\s*(.*)\r$', msg, re.IGNORECASE | re.MULTILINE)
if match:
URL = match.group(1)
match = re.search(r'^USN:\s*uuid:([^:]+):', msg, re.IGNORECASE | re.MULTILINE)
if match:
UUID = match.group(1)
print 'URL=%s, UUID=%s.' % (URL, UUID)
last_update = time.time()
# Bring the notifcation forward
next_notification = time.time() + 1
def is_search(msg):
return re.match('^M-SEARCH', msg, re.IGNORECASE)
# Get info from server
last_update = 0
server()
next_notification = time.time() + INTERVAL
# Note: the port is not set up until at least one send has happened
(notused, oport) = osock.getsockname()
isock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
isock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
isock.bind(('', oport))
while True:
(readyin, notused, notused) = select.select([isock, imsock], [], [], max(next_notification - time.time(),0))
if (isock in readyin):
(msg, (addr, port)) = isock.recvfrom(4096)
print "Received unicast from %s:%d\n%s" % (addr, port, msg)
if (is_search(msg)):
respond(addr, port)
else:
parse_msg(msg)
if (imsock in readyin):
(msg, (addr, port)) = imsock.recvfrom(4096)
if (port == oport):
print "Ignored multicast from ourselves (%s:%d)" % (addr, port)
else:
print "Received multicast from %s:%d\n%s" % (addr, port, msg)
if (is_search(msg)):
respond(addr, port)
if (time.time() >= next_notification):
next_notification = time.time() + INTERVAL
# Has the server info been updated recently?
if (time.time() - last_update <= INTERVAL):
# Yes, just do the notification
notify(DLNA_GRP, DLNA_PORT)
else:
# Get new info from the server
server()