This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
forked from julienblitte/tiny-ssdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssdp.c
324 lines (276 loc) · 7.78 KB
/
ssdp.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <syslog.h>
#include "network.h"
#include "http.h"
#include "ssdp.h"
#include "config.h"
#include "logfilter.h"
const char *ip;
int socket_descriptor = -1;
static pthread_mutex_t ssdp_mutex;
text ssdp_get_st_match_list()
{
text result;
static char st_list[1024];
static char service[128];
int i;
snprintf(st_list, sizeof(st_list), "ssdp:all\nuuid:%s\nupnp:rootdevice\nurn:schemas-upnp-org:device:%s:%d",
configuration.device_uuid, configuration.device_type_name, configuration.device_type_version);
for(i=0; i < configuration.service_count; i++)
{
if (configuration.service_name[i])
{
snprintf(service, sizeof(service), "\nurn:schemas-upnp-org:service:%s:%d",
configuration.service_name[i], configuration.service_version[i]);
strncat(st_list, service, sizeof(st_list));
}
}
result = data2text(st_list);
return result;
}
// https://embeddedinn.wordpress.com/tutorials/upnp-device-architecture/
// http://buildingskb.schneider-electric.com/view.php?AID=15197
const char *ssdp_get_notify(int type)
{
static char notify[SSDP_MAX_PACKETSIZE];
char nt[256];
char usn[256];
int service;
switch(type)
{
case 0:
snprintf(nt, sizeof(nt), "uuid:%s", configuration.device_uuid);
snprintf(usn, sizeof(usn), "uuid:%s", configuration.device_uuid);
break;
case 1:
strncpy(nt, "upnp:rootdevice", sizeof(nt));
*usn = '\0';
break;
case 2:
snprintf(nt, sizeof(nt), "urn:schemas-upnp-org:device:%s:%d",
configuration.device_type_name, configuration.device_type_version);
*usn = '\0';
break;
default:
service = type - 3;
// if valid service in list
if (service < configuration.service_count && configuration.service_name[service])
{
snprintf(nt, sizeof(nt), "urn:schemas-upnp-org:service:%s:%d",
configuration.service_name[service], configuration.service_version[service]);
*usn = '\0';
break;
}
else
{
// end of service, return NULL
return NULL;
}
}
// no USN, build it from NT
if (strcmp(usn, "") == 0)
{
snprintf(usn, sizeof(usn), "uuid:%s::%s", configuration.device_uuid, nt);
}
snprintf(notify, sizeof(notify), "NOTIFY * HTTP/1.1\r\nHost: %s:%d\r\nServer: %s\r\nCache-Control: max-age=%d\r\n"
"Location: http://%s:%d%s\r\nNT: %s\r\nNTS: %s\r\nUSN: %s\r\n\r\n",
configuration.network_multicast, configuration.network_port, configuration.device_description, configuration.ssdp_allowed_cache,
ip, configuration.base_url_port, configuration.base_url_resource, nt, SSDP_NOTIFY_NTS, usn);
return notify;
}
void ssdp_notify()
{
const char *notify;
int type;
// not initialized, try to initialize
if (socket_descriptor <= 0)
{
ssdp_init();
if (socket_descriptor <= 0)
{
writelog(LOG_ERR, "Fatal error while trying to initialize ssdp!");
return;
}
}
// lock mutext
pthread_mutex_lock(&ssdp_mutex);
type=0;
notify = ssdp_get_notify(type);
while(notify)
{
writelog(LOG_INFO, "Sending NOTIFY type %d", type);
multicast_send(socket_descriptor, configuration.network_multicast, configuration.network_port, notify, strlen(notify));
type++;
notify = ssdp_get_notify(type);
}
// unlock mutex
pthread_mutex_unlock(&ssdp_mutex);
}
const char *ssdp_get_response(const char *st)
{
static char response[SSDP_MAX_PACKETSIZE];
time_t rawtime;
struct tm * timeinfo;
char now[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(now, sizeof(now), "%a, %d %b %Y %X %Z", timeinfo);
snprintf(response, sizeof(response), "HTTP/1.1 200 OK\r\nServer: %s\r\nDate: %s\r\nLocation: http://%s:%d%s\r\n"
"ST: %s\r\nEXT: %s\r\nUSN: uuid:%s::upnp:rootdevice\r\nCache-Control: max-age=%d\r\n\r\n",
configuration.device_description, now, ip, configuration.base_url_port, configuration.base_url_resource, st,
SSDP_EXT, configuration.device_uuid, configuration.ssdp_allowed_cache);
return response;
}
void ssdp_wait_random(int s)
{
if (!s) return;
srand(time(NULL));
sleep(rand() % s);
}
void ssdp_handle_request(text request, struct sockaddr_in *client_addr, socklen_t client_size)
{
text st_match_list;
char *st, *man, *mx;
const char *reply;
struct sockaddr_in local_socket;
// lock mutex
pthread_mutex_lock(&ssdp_mutex);
writelog(LOG_DEBUG, "Recieved:");
writelog_text(LOG_DEBUG, request);
st_match_list = ssdp_get_st_match_list();
if (http_type(request) == HTTP_TYPE_SSDP_SEARCH)
{
writelog(LOG_DEBUG, "Datagram type is M-SEARCH");
st = (char *)http_search_variable(request, "ST");
man = (char *)http_search_variable(request, "MAN");
mx = (char *)http_search_variable(request, "MX");
if (st && man)
{
man = http_dup_clean_value(man);
if (!strcasecmp(man, SSDP_MAN_DISCOVER))
{
writelog(LOG_DEBUG, "ST matching filters are:");
writelog_text(LOG_DEBUG, st_match_list);
st = http_dup_clean_value(st);
if (str_find_line(st_match_list, st) >= 0)
{
ssdp_wait_random(atoi(mx));
writelog(LOG_DEBUG, "Request match: %s", st);
reply = ssdp_get_response(st);
if (sendto(socket_descriptor, reply, strlen(reply), 0, (struct sockaddr*)client_addr, client_size) > 0)
{
writelog(LOG_INFO, "Request answered");
}
else
{
writelog(LOG_WARNING, "Error while answering request!");
}
}
free(st);
}
free(man);
}
}
else if (http_type(request) == HTTP_TYPE_SSDP_NOTIFY)
{
writelog(LOG_DEBUG, "Datagram type is NOTIFY\n");
// do nothing
}
// unlock mutex
pthread_mutex_unlock(&ssdp_mutex);
}
void ssdp_init()
{
int tentatives;
// already initialized
if (socket_descriptor > 0)
return;
// lock mutex
pthread_mutex_lock(&ssdp_mutex);
writelog(LOG_NOTICE, "Loading configuration...");
config_load();
writelog(LOG_NOTICE, "Initializing network...");
ip = iptos(get_ip_address());
socket_descriptor = udp_listen(configuration.network_port);
if (!socket_descriptor)
{
writelog(LOG_ERR, "Fatal error creating or binding datagram socket!");
// unlock mutex
pthread_mutex_unlock(&ssdp_mutex);
return;
}
tentatives = 5;
while ((tentatives > 0) && !multicast_join_group(socket_descriptor, configuration.network_multicast, configuration.network_interface))
{
tentatives--;
writelog(LOG_WARNING, "Unable to join multicast group. Tries left = %d", tentatives);
sleep(2);
}
if (tentatives <= 0)
{
writelog(LOG_ERR, "Fatal error while joining multicast group!");
writelog(LOG_ERR, "Do you have multicast-enabled network connectivity?");
close(socket_descriptor);
// set not to initialized
socket_descriptor = 0;
// unlock mutex before returning
pthread_mutex_unlock(&ssdp_mutex);
return;
}
// unlock mutex
pthread_mutex_unlock(&ssdp_mutex);
}
void *ssdp_thread_server(void *arg)
{
char buffer[SSDP_MAX_PACKETSIZE];
struct sockaddr_in client_addr;
socklen_t client_size;
// not initialized, try to initialize
if (socket_descriptor <= 0)
{
ssdp_init();
if (socket_descriptor <= 0)
{
writelog(LOG_ERR, "Fatal error while trying to initialize ssdp!");
pthread_exit(NULL);
return;
}
}
/* Read from the socket. */
memset(buffer, 0, sizeof(buffer));
client_size = sizeof(client_addr);
while(recvfrom(socket_descriptor, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)&client_addr, &client_size) >= 0)
{
ssdp_handle_request(data2text(buffer), &client_addr, client_size);
memset(buffer, 0, sizeof(buffer));
client_size = sizeof(client_addr);
}
writelog(LOG_ERR, "Fatal error while reading datagram message!");
close(socket_descriptor);
socket_descriptor = 0;
pthread_exit(NULL);
}
void *ssdp_thread_notify(void *arg)
{
int period = 90;
if (arg)
{
period = *((int*)arg);
}
while(1)
{
ssdp_notify();
sleep(period);
}
}