-
Notifications
You must be signed in to change notification settings - Fork 1
/
upnpsoap.c
1996 lines (1877 loc) · 59.6 KB
/
upnpsoap.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MiniDLNA project
*
* http://sourceforge.net/projects/minidlna/
*
* MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*
* Portions of the code from the MiniUPnP project:
*
* Copyright (c) 2006-2007, Thomas Bernard
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <ctype.h>
#include "upnpglobalvars.h"
#include "utils.h"
#include "upnphttp.h"
#include "upnpsoap.h"
#include "containers.h"
#include "upnpreplyparse.h"
#include "getifaddr.h"
#include "scanner.h"
#include "sql.h"
#include "log.h"
#ifdef __sparc__ /* Sorting takes too long on slow processors with very large containers */
# define __SORT_LIMIT if( totalMatches < 10000 )
#else
# define __SORT_LIMIT
#endif
/* Standard Errors:
*
* errorCode errorDescription Description
* -------- ---------------- -----------
* 401 Invalid Action No action by that name at this service.
* 402 Invalid Args Could be any of the following: not enough in args,
* too many in args, no in arg by that name,
* one or more in args are of the wrong data type.
* 403 Out of Sync Out of synchronization.
* 501 Action Failed May be returned in current state of service
* prevents invoking that action.
* 600-699 TBD Common action errors. Defined by UPnP Forum
* Technical Committee.
* 700-799 TBD Action-specific errors for standard actions.
* Defined by UPnP Forum working committee.
* 800-899 TBD Action-specific errors for non-standard actions.
* Defined by UPnP vendor.
*/
static void
SoapError(struct upnphttp * h, int errCode, const char * errDesc)
{
static const char resp[] =
"<s:Envelope "
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body>"
"<s:Fault>"
"<faultcode>s:Client</faultcode>"
"<faultstring>UPnPError</faultstring>"
"<detail>"
"<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">"
"<errorCode>%d</errorCode>"
"<errorDescription>%s</errorDescription>"
"</UPnPError>"
"</detail>"
"</s:Fault>"
"</s:Body>"
"</s:Envelope>";
char body[2048];
int bodylen;
DPRINTF(E_WARN, L_HTTP, "Returning UPnPError %d: %s\n", errCode, errDesc);
bodylen = snprintf(body, sizeof(body), resp, errCode, errDesc);
BuildResp2_upnphttp(h, 500, "Internal Server Error", body, bodylen);
SendResp_upnphttp(h);
CloseSocket_upnphttp(h);
}
static void
BuildSendAndCloseSoapResp(struct upnphttp * h,
const char * body, int bodylen)
{
static const char beforebody[] =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body>";
static const char afterbody[] =
"</s:Body>"
"</s:Envelope>\r\n";
if (!body || bodylen < 0)
{
Send500(h);
return;
}
BuildHeader_upnphttp(h, 200, "OK", sizeof(beforebody) - 1
+ sizeof(afterbody) - 1 + bodylen );
memcpy(h->res_buf + h->res_buflen, beforebody, sizeof(beforebody) - 1);
h->res_buflen += sizeof(beforebody) - 1;
memcpy(h->res_buf + h->res_buflen, body, bodylen);
h->res_buflen += bodylen;
memcpy(h->res_buf + h->res_buflen, afterbody, sizeof(afterbody) - 1);
h->res_buflen += sizeof(afterbody) - 1;
SendResp_upnphttp(h);
CloseSocket_upnphttp(h);
}
static void
GetSystemUpdateID(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<Id>%d</Id>"
"</u:%sResponse>";
char body[512];
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:schemas-upnp-org:service:ContentDirectory:1",
updateID, action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
static void
IsAuthorizedValidated(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<Result>%d</Result>"
"</u:%sResponse>";
char body[512];
struct NameValueParserData data;
const char * id;
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data, XML_STORE_EMPTY_FL);
id = GetValueFromNameValueList(&data, "DeviceID");
if(id)
{
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
1, action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
else
SoapError(h, 402, "Invalid Args");
ClearNameValueList(&data);
}
static void
RegisterDevice(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<RegistrationRespMsg>%s</RegistrationRespMsg>"
"</u:%sResponse>";
char body[512];
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
uuidvalue, action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
static void
GetProtocolInfo(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<Source>"
RESOURCE_PROTOCOL_INFO_VALUES
"</Source>"
"<Sink></Sink>"
"</u:%sResponse>";
char * body;
int bodylen;
bodylen = asprintf(&body, resp,
action, "urn:schemas-upnp-org:service:ConnectionManager:1",
action);
BuildSendAndCloseSoapResp(h, body, bodylen);
free(body);
}
static void
GetSortCapabilities(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<SortCaps>"
"dc:title,"
"dc:date,"
"upnp:class,"
"upnp:album,"
"upnp:originalTrackNumber"
"</SortCaps>"
"</u:%sResponse>";
char body[512];
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:schemas-upnp-org:service:ContentDirectory:1",
action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
static void
GetSearchCapabilities(struct upnphttp * h, const char * action)
{
static const char resp[] =
"<u:%sResponse xmlns:u=\"%s\">"
"<SearchCaps>"
"dc:creator,"
"dc:date,"
"dc:title,"
"upnp:album,"
"upnp:actor,"
"upnp:artist,"
"upnp:class,"
"upnp:genre,"
"@id,"
"@parentID,"
"@refID"
"</SearchCaps>"
"</u:%sResponse>";
char body[512];
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:schemas-upnp-org:service:ContentDirectory:1",
action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
static void
GetCurrentConnectionIDs(struct upnphttp * h, const char * action)
{
/* TODO: Use real data. - JM */
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<ConnectionIDs>0</ConnectionIDs>"
"</u:%sResponse>";
char body[512];
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:schemas-upnp-org:service:ConnectionManager:1",
action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
static void
GetCurrentConnectionInfo(struct upnphttp * h, const char * action)
{
/* TODO: Use real data. - JM */
static const char resp[] =
"<u:%sResponse "
"xmlns:u=\"%s\">"
"<RcsID>-1</RcsID>"
"<AVTransportID>-1</AVTransportID>"
"<ProtocolInfo></ProtocolInfo>"
"<PeerConnectionManager></PeerConnectionManager>"
"<PeerConnectionID>-1</PeerConnectionID>"
"<Direction>Output</Direction>"
"<Status>Unknown</Status>"
"</u:%sResponse>";
char body[sizeof(resp)+128];
struct NameValueParserData data;
const char *id_str;
int id;
char *endptr = NULL;
ParseNameValue(h->req_buf + h->req_contentoff, h->req_contentlen, &data, XML_STORE_EMPTY_FL);
id_str = GetValueFromNameValueList(&data, "ConnectionID");
DPRINTF(E_INFO, L_HTTP, "GetCurrentConnectionInfo(%s)\n", id_str);
if(id_str)
id = strtol(id_str, &endptr, 10);
if (!id_str || endptr == id_str)
{
SoapError(h, 402, "Invalid Args");
}
else if(id != 0)
{
SoapError(h, 701, "No such object error");
}
else
{
int bodylen;
bodylen = snprintf(body, sizeof(body), resp,
action, "urn:schemas-upnp-org:service:ConnectionManager:1",
action);
BuildSendAndCloseSoapResp(h, body, bodylen);
}
ClearNameValueList(&data);
}
/* Standard DLNA/UPnP filter flags */
#define FILTER_CHILDCOUNT 0x00000001
#define FILTER_DC_CREATOR 0x00000002
#define FILTER_DC_DATE 0x00000004
#define FILTER_DC_DESCRIPTION 0x00000008
#define FILTER_DLNA_NAMESPACE 0x00000010
#define FILTER_REFID 0x00000020
#define FILTER_RES 0x00000040
#define FILTER_RES_BITRATE 0x00000080
#define FILTER_RES_DURATION 0x00000100
#define FILTER_RES_NRAUDIOCHANNELS 0x00000200
#define FILTER_RES_RESOLUTION 0x00000400
#define FILTER_RES_SAMPLEFREQUENCY 0x00000800
#define FILTER_RES_SIZE 0x00001000
#define FILTER_SEARCHABLE 0x00002000
#define FILTER_UPNP_ACTOR 0x00004000
#define FILTER_UPNP_ALBUM 0x00008000
#define FILTER_UPNP_ALBUMARTURI 0x00010000
#define FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID 0x00020000
#define FILTER_UPNP_ARTIST 0x00040000
#define FILTER_UPNP_GENRE 0x00080000
#define FILTER_UPNP_ORIGINALTRACKNUMBER 0x00100000
#define FILTER_UPNP_SEARCHCLASS 0x00200000
#define FILTER_UPNP_STORAGEUSED 0x00400000
/* Vendor-specific filter flags */
#define FILTER_SEC_CAPTION_INFO_EX 0x01000000
#define FILTER_SEC_DCM_INFO 0x02000000
#define FILTER_PV_SUBTITLE_FILE_TYPE 0x04000000
#define FILTER_PV_SUBTITLE_FILE_URI 0x08000000
#define FILTER_PV_SUBTITLE 0x0C000000
#define FILTER_AV_MEDIA_CLASS 0x10000000
static uint32_t
set_filter_flags(char *filter, struct upnphttp *h)
{
char *item, *saveptr = NULL;
uint32_t flags = 0;
int samsung = h->req_client && (h->req_client->type->flags & FLAG_SAMSUNG);
if( !filter || (strlen(filter) <= 1) ) {
/* Not the full 32 bits. Skip vendor-specific stuff by default. */
flags = 0xFFFFFF;
if (samsung)
flags |= FILTER_SEC_CAPTION_INFO_EX | FILTER_SEC_DCM_INFO;
}
if (flags)
return flags;
if( samsung )
flags |= FILTER_DLNA_NAMESPACE;
item = strtok_r(filter, ",", &saveptr);
while( item != NULL )
{
if( saveptr )
*(item-1) = ',';
while( isspace(*item) )
item++;
if( strcmp(item, "@childCount") == 0 )
{
flags |= FILTER_CHILDCOUNT;
}
else if( strcmp(item, "@searchable") == 0 )
{
flags |= FILTER_SEARCHABLE;
}
else if( strcmp(item, "dc:creator") == 0 )
{
flags |= FILTER_DC_CREATOR;
}
else if( strcmp(item, "dc:date") == 0 )
{
flags |= FILTER_DC_DATE;
}
else if( strcmp(item, "dc:description") == 0 )
{
flags |= FILTER_DC_DESCRIPTION;
}
else if( strcmp(item, "dlna") == 0 )
{
flags |= FILTER_DLNA_NAMESPACE;
}
else if( strcmp(item, "@refID") == 0 )
{
flags |= FILTER_REFID;
}
else if( strcmp(item, "upnp:album") == 0 )
{
flags |= FILTER_UPNP_ALBUM;
}
else if( strcmp(item, "upnp:albumArtURI") == 0 )
{
flags |= FILTER_UPNP_ALBUMARTURI;
if( samsung )
flags |= FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID;
}
else if( strcmp(item, "upnp:albumArtURI@dlna:profileID") == 0 )
{
flags |= FILTER_UPNP_ALBUMARTURI;
flags |= FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID;
}
else if( strcmp(item, "upnp:artist") == 0 )
{
flags |= FILTER_UPNP_ARTIST;
}
else if( strcmp(item, "upnp:actor") == 0 )
{
flags |= FILTER_UPNP_ACTOR;
}
else if( strcmp(item, "upnp:genre") == 0 )
{
flags |= FILTER_UPNP_GENRE;
}
else if( strcmp(item, "upnp:originalTrackNumber") == 0 )
{
flags |= FILTER_UPNP_ORIGINALTRACKNUMBER;
}
else if( strcmp(item, "upnp:searchClass") == 0 )
{
flags |= FILTER_UPNP_SEARCHCLASS;
}
else if( strcmp(item, "upnp:storageUsed") == 0 )
{
flags |= FILTER_UPNP_STORAGEUSED;
}
else if( strcmp(item, "res") == 0 )
{
flags |= FILTER_RES;
}
else if( (strcmp(item, "res@bitrate") == 0) ||
(strcmp(item, "@bitrate") == 0) ||
((strcmp(item, "bitrate") == 0) && (flags & FILTER_RES)) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_BITRATE;
}
else if( (strcmp(item, "res@duration") == 0) ||
(strcmp(item, "@duration") == 0) ||
((strcmp(item, "duration") == 0) && (flags & FILTER_RES)) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_DURATION;
}
else if( (strcmp(item, "res@nrAudioChannels") == 0) ||
(strcmp(item, "@nrAudioChannels") == 0) ||
((strcmp(item, "nrAudioChannels") == 0) && (flags & FILTER_RES)) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_NRAUDIOCHANNELS;
}
else if( (strcmp(item, "res@resolution") == 0) ||
(strcmp(item, "@resolution") == 0) ||
((strcmp(item, "resolution") == 0) && (flags & FILTER_RES)) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_RESOLUTION;
}
else if( (strcmp(item, "res@sampleFrequency") == 0) ||
(strcmp(item, "@sampleFrequency") == 0) ||
((strcmp(item, "sampleFrequency") == 0) && (flags & FILTER_RES)) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_SAMPLEFREQUENCY;
}
else if( (strcmp(item, "res@size") == 0) ||
(strcmp(item, "@size") == 0) ||
(strcmp(item, "size") == 0) )
{
flags |= FILTER_RES;
flags |= FILTER_RES_SIZE;
}
else if( strcmp(item, "sec:CaptionInfoEx") == 0 )
{
flags |= FILTER_SEC_CAPTION_INFO_EX;
}
else if( strcmp(item, "sec:dcmInfo") == 0 )
{
flags |= FILTER_SEC_DCM_INFO;
}
else if( strcmp(item, "res@pv:subtitleFileType") == 0 )
{
flags |= FILTER_PV_SUBTITLE_FILE_TYPE;
}
else if( strcmp(item, "res@pv:subtitleFileUri") == 0 )
{
flags |= FILTER_PV_SUBTITLE_FILE_URI;
}
else if( strcmp(item, "av:mediaClass") == 0 )
{
flags |= FILTER_AV_MEDIA_CLASS;
}
item = strtok_r(NULL, ",", &saveptr);
}
return flags;
}
static char *
parse_sort_criteria(char *sortCriteria, int *error)
{
char *order = NULL;
char *item, *saveptr;
int i, ret, reverse, title_sorted = 0;
struct string_s str;
*error = 0;
if( force_sort_criteria )
sortCriteria = strdup(force_sort_criteria);
if( !sortCriteria )
return NULL;
if( (item = strtok_r(sortCriteria, ",", &saveptr)) )
{
order = malloc(4096);
str.data = order;
str.size = 4096;
str.off = 0;
strcatf(&str, "order by ");
}
for( i = 0; item != NULL; i++ )
{
reverse=0;
if( i )
strcatf(&str, ", ");
if( *item == '+' )
{
item++;
}
else if( *item == '-' )
{
reverse = 1;
item++;
}
else
{
DPRINTF(E_ERROR, L_HTTP, "No order specified [%s]\n", item);
goto bad_direction;
}
if( strcasecmp(item, "upnp:class") == 0 )
{
strcatf(&str, "o.CLASS");
}
else if( strcasecmp(item, "dc:title") == 0 )
{
strcatf(&str, "d.TITLE");
title_sorted = 1;
}
else if( strcasecmp(item, "dc:date") == 0 )
{
strcatf(&str, "d.DATE");
}
else if( strcasecmp(item, "upnp:originalTrackNumber") == 0 )
{
strcatf(&str, "d.DISC, d.TRACK");
}
else if( strcasecmp(item, "upnp:album") == 0 )
{
strcatf(&str, "d.ALBUM");
}
else
{
DPRINTF(E_ERROR, L_HTTP, "Unhandled SortCriteria [%s]\n", item);
bad_direction:
*error = -1;
if( i )
{
ret = strlen(order);
order[ret-2] = '\0';
}
i--;
goto unhandled_order;
}
if( reverse )
strcatf(&str, " DESC");
unhandled_order:
item = strtok_r(NULL, ",", &saveptr);
}
if( i <= 0 )
{
free(order);
if( force_sort_criteria )
free(sortCriteria);
return NULL;
}
/* Add a "tiebreaker" sort order */
if( !title_sorted )
strcatf(&str, ", TITLE ASC");
if( force_sort_criteria )
free(sortCriteria);
return order;
}
inline static void
add_resized_res(int srcw, int srch, int reqw, int reqh, char *dlna_pn,
char *detailID, struct Response *args)
{
int dstw = reqw;
int dsth = reqh;
if( (args->flags & FLAG_NO_RESIZE) && reqw > 160 && reqh > 160 )
return;
strcatf(args->str, "<res ");
if( args->filter & FILTER_RES_RESOLUTION )
{
dstw = reqw;
dsth = ((((reqw<<10)/srcw)*srch)>>10);
if( dsth > reqh ) {
dsth = reqh;
dstw = (((reqh<<10)/srch) * srcw>>10);
}
strcatf(args->str, "resolution=\"%dx%d\" ", dstw, dsth);
}
strcatf(args->str, "protocolInfo=\"http-get:*:image/jpeg:"
"DLNA.ORG_PN=%s;DLNA.ORG_CI=1;DLNA.ORG_FLAGS=%08X%024X\">"
"http://%s:%d/Resized/%s.jpg?width=%d,height=%d"
"</res>",
dlna_pn, DLNA_FLAG_DLNA_V1_5|DLNA_FLAG_HTTP_STALLING|DLNA_FLAG_TM_B|DLNA_FLAG_TM_I, 0,
lan_addr[args->iface].str, runtime_vars.port,
detailID, dstw, dsth);
}
inline static void
add_res(char *size, char *duration, char *bitrate, char *sampleFrequency,
char *nrAudioChannels, char *resolution, char *dlna_pn, char *mime,
char *detailID, const char *ext, struct Response *args)
{
strcatf(args->str, "<res ");
if( size && (args->filter & FILTER_RES_SIZE) ) {
strcatf(args->str, "size=\"%s\" ", size);
}
if( duration && (args->filter & FILTER_RES_DURATION) ) {
strcatf(args->str, "duration=\"%s\" ", duration);
}
if( bitrate && (args->filter & FILTER_RES_BITRATE) ) {
int br = atoi(bitrate);
if(args->flags & FLAG_MS_PFS)
br /= 8;
strcatf(args->str, "bitrate=\"%d\" ", br);
}
if( sampleFrequency && (args->filter & FILTER_RES_SAMPLEFREQUENCY) ) {
strcatf(args->str, "sampleFrequency=\"%s\" ", sampleFrequency);
}
if( nrAudioChannels && (args->filter & FILTER_RES_NRAUDIOCHANNELS) ) {
strcatf(args->str, "nrAudioChannels=\"%s\" ", nrAudioChannels);
}
if( resolution && (args->filter & FILTER_RES_RESOLUTION) ) {
strcatf(args->str, "resolution=\"%s\" ", resolution);
}
if( args->filter & FILTER_PV_SUBTITLE )
{
if( args->flags & FLAG_HAS_CAPTIONS )
{
if( args->filter & FILTER_PV_SUBTITLE_FILE_TYPE )
strcatf(args->str, "pv:subtitleFileType=\"SRT\" ");
if( args->filter & FILTER_PV_SUBTITLE_FILE_URI )
strcatf(args->str, "pv:subtitleFileUri=\"http://%s:%d/Captions/%s.srt\" ",
lan_addr[args->iface].str, runtime_vars.port, detailID);
}
}
strcatf(args->str, "protocolInfo=\"http-get:*:%s:%s\">"
"http://%s:%d/MediaItems/%s.%s"
"</res>",
mime, dlna_pn, lan_addr[args->iface].str,
runtime_vars.port, detailID, ext);
}
static int
get_child_count(const char *object, struct magic_container_s *magic)
{
int ret;
if (magic && magic->child_count)
ret = sql_get_int_field(db, "SELECT count(*) from %s", magic->child_count);
else if (magic && magic->objectid && *(magic->objectid))
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", *(magic->objectid));
else
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", object);
return (ret > 0) ? ret : 0;
}
static int
object_exists(const char *object)
{
int ret;
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'",
strcmp(object, "*") == 0 ? "0" : object);
return (ret > 0);
}
#define COLUMNS "o.DETAIL_ID, o.CLASS," \
" d.SIZE, d.TITLE, d.DURATION, d.BITRATE, d.SAMPLERATE, d.ARTIST," \
" d.ALBUM, d.GENRE, d.COMMENT, d.CHANNELS, d.TRACK, d.DATE, d.RESOLUTION," \
" d.THUMBNAIL, d.CREATOR, d.DLNA_PN, d.MIME, d.ALBUM_ART, d.ROTATION, d.DISC "
#define SELECT_COLUMNS "SELECT o.OBJECT_ID, o.PARENT_ID, o.REF_ID, " COLUMNS
#define NON_ZERO(x) (x && atoi(x))
#define IS_ZERO(x) (!x || !atoi(x))
static int
callback(void *args, int argc, char **argv, char **azColName)
{
struct Response *passed_args = (struct Response *)args;
char *id = argv[0], *parent = argv[1], *refID = argv[2], *detailID = argv[3], *class = argv[4], *size = argv[5], *title = argv[6],
*duration = argv[7], *bitrate = argv[8], *sampleFrequency = argv[9], *artist = argv[10], *album = argv[11],
*genre = argv[12], *comment = argv[13], *nrAudioChannels = argv[14], *track = argv[15], *date = argv[16], *resolution = argv[17],
*tn = argv[18], *creator = argv[19], *dlna_pn = argv[20], *mime = argv[21], *album_art = argv[22], *rotate = argv[23];
char dlna_buf[128];
const char *ext;
struct string_s *str = passed_args->str;
int ret = 0;
/* Make sure we have at least 8KB left of allocated memory to finish the response. */
if( str->off > (str->size - 8192) )
{
#if MAX_RESPONSE_SIZE > 0
if( (str->size+DEFAULT_RESP_SIZE) <= MAX_RESPONSE_SIZE )
{
#endif
str->data = realloc(str->data, (str->size+DEFAULT_RESP_SIZE));
if( str->data )
{
str->size += DEFAULT_RESP_SIZE;
DPRINTF(E_DEBUG, L_HTTP, "UPnP SOAP response enlarged to %lu. [%d results so far]\n",
(unsigned long)str->size, passed_args->returned);
}
else
{
DPRINTF(E_ERROR, L_HTTP, "UPnP SOAP response was too big, and realloc failed!\n");
return -1;
}
#if MAX_RESPONSE_SIZE > 0
}
else
{
DPRINTF(E_ERROR, L_HTTP, "UPnP SOAP response cut short, to not exceed the max response size [%lld]!\n", (long long int)MAX_RESPONSE_SIZE);
return -1;
}
#endif
}
passed_args->returned++;
if( strncmp(class, "item", 4) == 0 )
{
uint32_t dlna_flags = DLNA_FLAG_DLNA_V1_5|DLNA_FLAG_HTTP_STALLING|DLNA_FLAG_TM_B;
char *alt_title = NULL;
/* We may need special handling for certain MIME types */
if( *mime == 'v' )
{
dlna_flags |= DLNA_FLAG_TM_S;
if( passed_args->flags & FLAG_MIME_AVI_DIVX )
{
if( strcmp(mime, "video/x-msvideo") == 0 )
{
if( creator )
strcpy(mime+6, "divx");
else
strcpy(mime+6, "avi");
}
}
else if( passed_args->flags & FLAG_MIME_AVI_AVI )
{
if( strcmp(mime, "video/x-msvideo") == 0 )
{
strcpy(mime+6, "avi");
}
}
else if( passed_args->client == EFreeBox && dlna_pn )
{
if( strncmp(dlna_pn, "AVC_TS", 6) == 0 ||
strncmp(dlna_pn, "MPEG_TS", 7) == 0 )
{
strcpy(mime+6, "mp2t");
}
}
if( !(passed_args->flags & FLAG_DLNA) )
{
if( strcmp(mime+6, "vnd.dlna.mpeg-tts") == 0 )
{
strcpy(mime+6, "mpeg");
}
}
if( (passed_args->flags & FLAG_CAPTION_RES) ||
(passed_args->filter & (FILTER_SEC_CAPTION_INFO_EX|FILTER_PV_SUBTITLE)) )
{
if( sql_get_int_field(db, "SELECT ID from CAPTIONS where ID = '%s'", detailID) > 0 )
passed_args->flags |= FLAG_HAS_CAPTIONS;
}
/* From what I read, Samsung TV's expect a [wrong] MIME type of x-mkv. */
if( passed_args->flags & FLAG_SAMSUNG )
{
if( strcmp(mime+6, "x-matroska") == 0 )
{
strcpy(mime+8, "mkv");
}
}
/* LG hack: subtitles won't get used unless dc:title contains a dot. */
else if( passed_args->client == ELGDevice && (passed_args->flags & FLAG_HAS_CAPTIONS) )
{
ret = asprintf(&alt_title, "%s.", title);
if( ret > 0 )
title = alt_title;
else
alt_title = NULL;
}
/* Asus OPlay reboots with titles longer than 23 characters with some file types. */
else if( passed_args->client == EAsusOPlay && (passed_args->flags & FLAG_HAS_CAPTIONS) )
{
if( strlen(title) > 23 )
title[23] = '\0';
}
}
else if( *mime == 'a' )
{
dlna_flags |= DLNA_FLAG_TM_S;
if( strcmp(mime+6, "x-flac") == 0 )
{
if( passed_args->flags & FLAG_MIME_FLAC_FLAC )
{
strcpy(mime+6, "flac");
}
}
else if( strcmp(mime+6, "x-wav") == 0 )
{
if( passed_args->flags & FLAG_MIME_WAV_WAV )
{
strcpy(mime+6, "wav");
}
}
}
else
dlna_flags |= DLNA_FLAG_TM_I;
if( dlna_pn )
snprintf(dlna_buf, sizeof(dlna_buf), "DLNA.ORG_PN=%s;"
"DLNA.ORG_OP=01;"
"DLNA.ORG_CI=0;"
"DLNA.ORG_FLAGS=%08X%024X",
dlna_pn, dlna_flags, 0);
else if( passed_args->flags & FLAG_DLNA )
snprintf(dlna_buf, sizeof(dlna_buf), "DLNA.ORG_OP=01;"
"DLNA.ORG_CI=0;"
"DLNA.ORG_FLAGS=%08X%024X",
dlna_flags, 0);
else
strcpy(dlna_buf, "*");
ret = strcatf(str, "<item id=\"%s\" parentID=\"%s\" restricted=\"1\"", id, parent);
if( refID && (passed_args->filter & FILTER_REFID) ) {
ret = strcatf(str, " refID=\"%s\"", refID);
}
ret = strcatf(str, ">"
"<dc:title>%s</dc:title>"
"<upnp:class>object.%s</upnp:class>",
title, class);
if( comment && (passed_args->filter & FILTER_DC_DESCRIPTION) ) {
ret = strcatf(str, "<dc:description>%.384s</dc:description>", comment);
}
if( creator && (passed_args->filter & FILTER_DC_CREATOR) ) {
ret = strcatf(str, "<dc:creator>%s</dc:creator>", creator);
}
if( date && (passed_args->filter & FILTER_DC_DATE) ) {
ret = strcatf(str, "<dc:date>%s</dc:date>", date);
}
if( passed_args->filter & FILTER_SEC_DCM_INFO ) {
/* Get bookmark */
ret = strcatf(str, "<sec:dcmInfo>CREATIONDATE=0,FOLDER=%s,BM=%d</sec:dcmInfo>",
title, sql_get_int_field(db, "SELECT SEC from BOOKMARKS where ID = '%s'", detailID));
}
if( artist ) {
if( (*mime == 'v') && (passed_args->filter & FILTER_UPNP_ACTOR) ) {
ret = strcatf(str, "<upnp:actor>%s</upnp:actor>", artist);
}
if( passed_args->filter & FILTER_UPNP_ARTIST ) {
ret = strcatf(str, "<upnp:artist>%s</upnp:artist>", artist);
}
}
if( album && (passed_args->filter & FILTER_UPNP_ALBUM) ) {
ret = strcatf(str, "<upnp:album>%s</upnp:album>", album);
}
if( genre && (passed_args->filter & FILTER_UPNP_GENRE) ) {
ret = strcatf(str, "<upnp:genre>%s</upnp:genre>", genre);
}
if( strncmp(id, MUSIC_PLIST_ID, strlen(MUSIC_PLIST_ID)) == 0 ) {
track = strrchr(id, '$')+1;
}
if( NON_ZERO(track) && (passed_args->filter & FILTER_UPNP_ORIGINALTRACKNUMBER) ) {
ret = strcatf(str, "<upnp:originalTrackNumber>%s</upnp:originalTrackNumber>", track);
}
if( passed_args->filter & FILTER_RES ) {
ext = mime_to_ext(mime);
add_res(size, duration, bitrate, sampleFrequency, nrAudioChannels,
resolution, dlna_buf, mime, detailID, ext, passed_args);
if( *mime == 'i' ) {
int srcw, srch;
if( resolution && (sscanf(resolution, "%6dx%6d", &srcw, &srch) == 2) )
{
if( srcw > 4096 || srch > 4096 )
add_resized_res(srcw, srch, 4096, 4096, "JPEG_LRG", detailID, passed_args);
if( srcw > 1024 || srch > 768 )
add_resized_res(srcw, srch, 1024, 768, "JPEG_MED", detailID, passed_args);
if( srcw > 640 || srch > 480 )
add_resized_res(srcw, srch, 640, 480, "JPEG_SM", detailID, passed_args);
}
if( !(passed_args->flags & FLAG_RESIZE_THUMBS) && NON_ZERO(tn) && IS_ZERO(rotate) ) {
ret = strcatf(str, "<res protocolInfo=\"http-get:*:%s:%s\">"
"http://%s:%d/Thumbnails/%s.jpg"
"</res>",
mime, "DLNA.ORG_PN=JPEG_TN;DLNA.ORG_CI=1", lan_addr[passed_args->iface].str,
runtime_vars.port, detailID);
}
else
add_resized_res(srcw, srch, 160, 160, "JPEG_TN", detailID, passed_args);
}
else if( *mime == 'v' ) {
switch( passed_args->client ) {
case EToshibaTV:
if( dlna_pn &&
(strncmp(dlna_pn, "MPEG_TS_HD_NA", 13) == 0 ||
strncmp(dlna_pn, "MPEG_TS_SD_NA", 13) == 0 ||
strncmp(dlna_pn, "AVC_TS_MP_HD_AC3", 16) == 0 ||
strncmp(dlna_pn, "AVC_TS_HP_HD_AC3", 16) == 0))
{
sprintf(dlna_buf, "DLNA.ORG_PN=%s;DLNA.ORG_OP=01;DLNA.ORG_CI=1", "MPEG_PS_NTSC");
add_res(size, duration, bitrate, sampleFrequency, nrAudioChannels,
resolution, dlna_buf, mime, detailID, ext, passed_args);
}