-
Notifications
You must be signed in to change notification settings - Fork 5
/
rtmp.c
4587 lines (4023 loc) · 120 KB
/
rtmp.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
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
* Copyright (C) 2008-2009 Andrej Stepanchuk
* Copyright (C) 2009-2010 Howard Chu
*
* This file is part of librtmp.
*
* libPILI_RTMP is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1,
* or (at your option) any later version.
*
* libPILI_RTMP 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 Lesser General Public License
* along with libPILI_RTMP see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/lgpl.html
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "rtmp_sys.h"
#include "log.h"
#ifdef CRYPTO
#ifdef USE_POLARSSL
#include <polarssl/havege.h>
#elif defined(USE_GNUTLS)
#include <gnutls/gnutls.h>
#else /* USE_OPENSSL */
#include <openssl/ssl.h>
#include <openssl/rc4.h>
#endif
TLS_CTX RTMP_TLS_ctx;
#endif
#define RTMP_SIG_SIZE 1536
#define RTMP_LARGE_HEADER_SIZE 12
static const int packetSize[] = { 12, 8, 4, 1 };
int PILI_RTMP_ctrlC;
const char PILI_RTMPProtocolStrings[][7] = {
"RTMP",
"RTMPT",
"RTMPE",
"RTMPTE",
"RTMPS",
"RTMPTS",
"",
"",
"RTMFP"
};
const char PILI_RTMPProtocolStringsLower[][7] = {
"rtmp",
"rtmpt",
"rtmpe",
"rtmpte",
"rtmps",
"rtmpts",
"",
"",
"rtmfp"
};
static const char *RTMPT_cmds[] = {
"open",
"send",
"idle",
"close"
};
typedef enum {
RTMPT_OPEN=0, RTMPT_SEND, RTMPT_IDLE, RTMPT_CLOSE
} RTMPTCmd;
static int DumpMetaData(AMFObject *obj);
static int HandShake(PILI_RTMP *r, int FP9HandShake, RTMPError *error);
static int SocksNegotiate(PILI_RTMP *r, RTMPError *error);
static int SendConnectPacket(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error);
static int SendCheckBW(PILI_RTMP *r, RTMPError *error);
static int SendCheckBWResult(PILI_RTMP *r, double txn, RTMPError *error);
static int SendDeleteStream(PILI_RTMP *r, double dStreamId, RTMPError *error);
static int SendFCSubscribe(PILI_RTMP *r, AVal *subscribepath, RTMPError *error);
static int SendPlay(PILI_RTMP *r, RTMPError *error);
static int SendBytesReceived(PILI_RTMP *r, RTMPError *error);
#if 0 /* unused */
static int SendBGHasStream(PILI_RTMP *r, double dId, AVal *playpath);
#endif
static int HandleInvoke(PILI_RTMP *r, const char *body, unsigned int nBodySize);
static int HandleMetadata(PILI_RTMP *r, char *body, unsigned int len);
static void HandleChangeChunkSize(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static void HandleAudio(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static void HandleVideo(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static void HandleCtrl(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static void HandleServerBW(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static void HandleClientBW(PILI_RTMP *r, const PILI_RTMPPacket *packet);
static int ReadN(PILI_RTMP *r, char *buffer, int n);
static int WriteN(PILI_RTMP *r, const char *buffer, int n, RTMPError *error);
static void DecodeTEA(AVal *key, AVal *text);
static int HTTP_Post(PILI_RTMP *r, RTMPTCmd cmd, const char *buf, int len);
static int HTTP_read(PILI_RTMP *r, int fill);
#ifndef _WIN32
static int clk_tck;
#endif
#ifdef CRYPTO
#include "handshake.h"
#endif
uint32_t
PILI_RTMP_GetTime()
{
#ifdef _DEBUG
return 0;
#elif defined(_WIN32)
return timeGetTime();
#else
struct tms t;
if (!clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
return times(&t) * 1000 / clk_tck;
#endif
}
void
PILI_RTMP_UserInterrupt()
{
PILI_RTMP_ctrlC = TRUE;
}
void
PILI_RTMPPacket_Reset(PILI_RTMPPacket *p)
{
p->m_headerType = 0;
p->m_packetType = 0;
p->m_nChannel = 0;
p->m_nTimeStamp = 0;
p->m_nInfoField2 = 0;
p->m_hasAbsTimestamp = FALSE;
p->m_nBodySize = 0;
p->m_nBytesRead = 0;
}
int
PILI_RTMPPacket_Alloc(PILI_RTMPPacket *p, int nSize)
{
char *ptr = calloc(1, nSize + RTMP_MAX_HEADER_SIZE);
if (!ptr)
return FALSE;
p->m_body = ptr + RTMP_MAX_HEADER_SIZE;
p->m_nBytesRead = 0;
return TRUE;
}
void
PILI_RTMPPacket_Free(PILI_RTMPPacket *p)
{
if (p->m_body)
{
free(p->m_body - RTMP_MAX_HEADER_SIZE);
p->m_body = NULL;
}
}
void
PILI_RTMPPacket_Dump(PILI_RTMPPacket *p)
{
RTMP_Log(RTMP_LOGDEBUG,
"PILI_RTMP PACKET: packet type: 0x%02x. channel: 0x%02x. info 1: %d info 2: %d. Body size: %lu. body: 0x%02x",
p->m_packetType, p->m_nChannel, p->m_nTimeStamp, p->m_nInfoField2,
p->m_nBodySize, p->m_body ? (unsigned char)p->m_body[0] : 0);
}
int
PILI_RTMP_LibVersion()
{
return RTMP_LIB_VERSION;
}
void
PILI_RTMP_TLS_Init()
{
#ifdef CRYPTO
#ifdef USE_POLARSSL
/* Do this regardless of NO_SSL, we use havege for rtmpe too */
RTMP_TLS_ctx = calloc(1,sizeof(struct tls_ctx));
havege_init(&RTMP_TLS_ctx->hs);
#elif defined(USE_GNUTLS) && !defined(NO_SSL)
/* Technically we need to initialize libgcrypt ourselves if
* we're not going to call gnutls_global_init(). Ignoring this
* for now.
*/
gnutls_global_init();
RTMP_TLS_ctx = malloc(sizeof(struct tls_ctx));
gnutls_certificate_allocate_credentials(&RTMP_TLS_ctx->cred);
gnutls_priority_init(&RTMP_TLS_ctx->prios, "NORMAL", NULL);
gnutls_certificate_set_x509_trust_file(RTMP_TLS_ctx->cred,
"ca.pem", GNUTLS_X509_FMT_PEM);
#elif !defined(NO_SSL) /* USE_OPENSSL */
/* libcrypto doesn't need anything special */
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_digests();
RTMP_TLS_ctx = SSL_CTX_new(SSLv23_method());
SSL_CTX_set_options(RTMP_TLS_ctx, SSL_OP_ALL);
SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx);
#endif
#endif
}
PILI_RTMP *
PILI_RTMP_Alloc()
{
return calloc(1, sizeof(PILI_RTMP));
}
void
PILI_RTMP_Free(PILI_RTMP *r)
{
r->m_errorCallback = NULL;
r->m_userData = NULL;
RTMPError_Free(r->m_error);
r->m_error = NULL;
free(r);
}
void
PILI_RTMP_Init(PILI_RTMP *r)
{
#ifdef CRYPTO
if (!RTMP_TLS_ctx)
RTMP_TLS_Init();
#endif
memset(r, 0, sizeof(PILI_RTMP));
r->m_sb.sb_socket = -1;
r->m_inChunkSize = RTMP_DEFAULT_CHUNKSIZE;
r->m_outChunkSize = RTMP_DEFAULT_CHUNKSIZE;
r->m_nBufferMS = 30000;
r->m_nClientBW = 2500000;
r->m_nClientBW2 = 2;
r->m_nServerBW = 2500000;
r->m_fAudioCodecs = 3191.0;
r->m_fVideoCodecs = 252.0;
r->Link.timeout = 10;
r->Link.send_timeout = 10;
r->Link.swfAge = 30;
r->m_errorCallback = NULL;
r->m_error = NULL;
r->m_userData = NULL;
r->m_is_closing = 0;
r->m_tcp_nodelay = 1;
r->m_connCallback = NULL;
r->ip = 0;
}
void
PILI_RTMP_EnableWrite(PILI_RTMP *r)
{
r->Link.protocol |= RTMP_FEATURE_WRITE;
}
double
PILI_RTMP_GetDuration(PILI_RTMP *r)
{
return r->m_fDuration;
}
int
PILI_RTMP_IsConnected(PILI_RTMP *r)
{
return r->m_sb.sb_socket != -1;
}
int
PILI_RTMP_Socket(PILI_RTMP *r)
{
return r->m_sb.sb_socket;
}
int
PILI_RTMP_IsTimedout(PILI_RTMP *r)
{
return r->m_sb.sb_timedout;
}
void
PILI_RTMP_SetBufferMS(PILI_RTMP *r, int size)
{
r->m_nBufferMS = size;
}
void
PILI_RTMP_UpdateBufferMS(PILI_RTMP *r, RTMPError *error)
{
PILI_RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS, error);
}
#undef OSS
#ifdef _WIN32
#define OSS "WIN"
#elif defined(__sun__)
#define OSS "SOL"
#elif defined(__APPLE__)
#define OSS "MAC"
#elif defined(__linux__)
#define OSS "LNX"
#else
#define OSS "GNU"
#endif
#define DEF_VERSTR OSS " 10,0,32,18"
static const char DEFAULT_FLASH_VER[] = DEF_VERSTR;
const AVal RTMP_DefaultFlashVer =
{ (char *)DEFAULT_FLASH_VER, sizeof(DEFAULT_FLASH_VER) - 1 };
void
PILI_RTMP_SetupStream(PILI_RTMP *r,
int protocol,
AVal *host,
unsigned int port,
AVal *sockshost,
AVal *playpath,
AVal *tcUrl,
AVal *swfUrl,
AVal *pageUrl,
AVal *app,
AVal *auth,
AVal *swfSHA256Hash,
uint32_t swfSize,
AVal *flashVer,
AVal *subscribepath,
int dStart,
int dStop, int bLiveStream, long int timeout)
{
RTMP_Log(RTMP_LOGDEBUG, "Protocol : %s", PILI_RTMPProtocolStrings[protocol&7]);
RTMP_Log(RTMP_LOGDEBUG, "Hostname : %.*s", host->av_len, host->av_val);
RTMP_Log(RTMP_LOGDEBUG, "Port : %d", port);
RTMP_Log(RTMP_LOGDEBUG, "Playpath : %s", playpath->av_val);
if (tcUrl && tcUrl->av_val)
RTMP_Log(RTMP_LOGDEBUG, "tcUrl : %s", tcUrl->av_val);
if (swfUrl && swfUrl->av_val)
RTMP_Log(RTMP_LOGDEBUG, "swfUrl : %s", swfUrl->av_val);
if (pageUrl && pageUrl->av_val)
RTMP_Log(RTMP_LOGDEBUG, "pageUrl : %s", pageUrl->av_val);
if (app && app->av_val)
RTMP_Log(RTMP_LOGDEBUG, "app : %.*s", app->av_len, app->av_val);
if (auth && auth->av_val)
RTMP_Log(RTMP_LOGDEBUG, "auth : %s", auth->av_val);
if (subscribepath && subscribepath->av_val)
RTMP_Log(RTMP_LOGDEBUG, "subscribepath : %s", subscribepath->av_val);
if (flashVer && flashVer->av_val)
RTMP_Log(RTMP_LOGDEBUG, "flashVer : %s", flashVer->av_val);
if (dStart > 0)
RTMP_Log(RTMP_LOGDEBUG, "StartTime : %d msec", dStart);
if (dStop > 0)
RTMP_Log(RTMP_LOGDEBUG, "StopTime : %d msec", dStop);
RTMP_Log(RTMP_LOGDEBUG, "live : %s", bLiveStream ? "yes" : "no");
RTMP_Log(RTMP_LOGDEBUG, "timeout : %d sec", timeout);
#ifdef CRYPTO
if (swfSHA256Hash != NULL && swfSize > 0)
{
memcpy(r->Link.SWFHash, swfSHA256Hash->av_val, sizeof(r->Link.SWFHash));
r->Link.SWFSize = swfSize;
RTMP_Log(RTMP_LOGDEBUG, "SWFSHA256:");
RTMP_LogHex(RTMP_LOGDEBUG, r->Link.SWFHash, sizeof(r->Link.SWFHash));
RTMP_Log(RTMP_LOGDEBUG, "SWFSize : %lu", r->Link.SWFSize);
}
else
{
r->Link.SWFSize = 0;
}
#endif
if (sockshost->av_len)
{
const char *socksport = strchr(sockshost->av_val, ':');
char *hostname = strdup(sockshost->av_val);
if (socksport)
hostname[socksport - sockshost->av_val] = '\0';
r->Link.sockshost.av_val = hostname;
r->Link.sockshost.av_len = strlen(hostname);
r->Link.socksport = socksport ? atoi(socksport + 1) : 1080;
RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost.av_val,
r->Link.socksport);
}
else
{
r->Link.sockshost.av_val = NULL;
r->Link.sockshost.av_len = 0;
r->Link.socksport = 0;
}
if (tcUrl && tcUrl->av_len)
r->Link.tcUrl = *tcUrl;
if (swfUrl && swfUrl->av_len)
r->Link.swfUrl = *swfUrl;
if (pageUrl && pageUrl->av_len)
r->Link.pageUrl = *pageUrl;
if (app && app->av_len)
r->Link.app = *app;
if (auth && auth->av_len)
{
r->Link.auth = *auth;
r->Link.lFlags |= RTMP_LF_AUTH;
}
if (flashVer && flashVer->av_len)
r->Link.flashVer = *flashVer;
else
r->Link.flashVer = RTMP_DefaultFlashVer;
if (subscribepath && subscribepath->av_len)
r->Link.subscribepath = *subscribepath;
r->Link.seekTime = dStart;
r->Link.stopTime = dStop;
if (bLiveStream)
r->Link.lFlags |= RTMP_LF_LIVE;
r->Link.timeout = timeout;
r->Link.protocol = protocol;
r->Link.hostname = *host;
r->Link.port = port;
r->Link.playpath = *playpath;
if (r->Link.port == 0)
{
if (protocol & RTMP_FEATURE_SSL)
r->Link.port = 443;
else if (protocol & RTMP_FEATURE_HTTP)
r->Link.port = 80;
else
r->Link.port = 1935;
}
}
enum { OPT_STR=0, OPT_INT, OPT_BOOL, OPT_CONN };
static const char *optinfo[] = {
"string", "integer", "boolean", "AMF" };
#define OFF(x) offsetof(struct PILI_RTMP,x)
static struct urlopt {
AVal name;
off_t off;
int otype;
int omisc;
char *use;
} options[] = {
{ AVC("socks"), OFF(Link.sockshost), OPT_STR, 0,
"Use the specified SOCKS proxy" },
{ AVC("app"), OFF(Link.app), OPT_STR, 0,
"Name of target app on server" },
{ AVC("tcUrl"), OFF(Link.tcUrl), OPT_STR, 0,
"URL to played stream" },
{ AVC("pageUrl"), OFF(Link.pageUrl), OPT_STR, 0,
"URL of played media's web page" },
{ AVC("swfUrl"), OFF(Link.swfUrl), OPT_STR, 0,
"URL to player SWF file" },
{ AVC("flashver"), OFF(Link.flashVer), OPT_STR, 0,
"Flash version string (default " DEF_VERSTR ")" },
{ AVC("conn"), OFF(Link.extras), OPT_CONN, 0,
"Append arbitrary AMF data to Connect message" },
{ AVC("playpath"), OFF(Link.playpath), OPT_STR, 0,
"Path to target media on server" },
{ AVC("playlist"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_PLST,
"Set playlist before play command" },
{ AVC("live"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_LIVE,
"Stream is live, no seeking possible" },
{ AVC("subscribe"), OFF(Link.subscribepath), OPT_STR, 0,
"Stream to subscribe to" },
{ AVC("token"), OFF(Link.token), OPT_STR, 0,
"Key for SecureToken response" },
{ AVC("swfVfy"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_SWFV,
"Perform SWF Verification" },
{ AVC("swfAge"), OFF(Link.swfAge), OPT_INT, 0,
"Number of days to use cached SWF hash" },
{ AVC("start"), OFF(Link.seekTime), OPT_INT, 0,
"Stream start position in milliseconds" },
{ AVC("stop"), OFF(Link.stopTime), OPT_INT, 0,
"Stream stop position in milliseconds" },
{ AVC("buffer"), OFF(m_nBufferMS), OPT_INT, 0,
"Buffer time in milliseconds" },
{ AVC("timeout"), OFF(Link.timeout), OPT_INT, 0,
"Session timeout in seconds" },
{ {NULL,0}, 0, 0}
};
static const AVal truth[] = {
AVC("1"),
AVC("on"),
AVC("yes"),
AVC("true"),
{0,0}
};
static void RTMP_OptUsage()
{
int i;
RTMP_Log(RTMP_LOGERROR, "Valid PILI_RTMP options are:\n");
for (i=0; options[i].name.av_len; i++) {
RTMP_Log(RTMP_LOGERROR, "%10s %-7s %s\n", options[i].name.av_val,
optinfo[options[i].otype], options[i].use);
}
}
static int
parseAMF(AMFObject *obj, AVal *av, int *depth)
{
AMFObjectProperty prop = {{0,0}};
int i;
char *p, *arg = av->av_val;
if (arg[1] == ':')
{
p = (char *)arg+2;
switch(arg[0])
{
case 'B':
prop.p_type = AMF_BOOLEAN;
prop.p_vu.p_number = atoi(p);
break;
case 'S':
prop.p_type = AMF_STRING;
prop.p_vu.p_aval.av_val = p;
prop.p_vu.p_aval.av_len = av->av_len - (p-arg);
break;
case 'N':
prop.p_type = AMF_NUMBER;
prop.p_vu.p_number = strtod(p, NULL);
break;
case 'Z':
prop.p_type = AMF_NULL;
break;
case 'O':
i = atoi(p);
if (i)
{
prop.p_type = AMF_OBJECT;
}
else
{
(*depth)--;
return 0;
}
break;
default:
return -1;
}
}
else if (arg[2] == ':' && arg[0] == 'N')
{
p = strchr(arg+3, ':');
if (!p || !*depth)
return -1;
prop.p_name.av_val = (char *)arg+3;
prop.p_name.av_len = p - (arg+3);
p++;
switch(arg[1])
{
case 'B':
prop.p_type = AMF_BOOLEAN;
prop.p_vu.p_number = atoi(p);
break;
case 'S':
prop.p_type = AMF_STRING;
prop.p_vu.p_aval.av_val = p;
prop.p_vu.p_aval.av_len = av->av_len - (p-arg);
break;
case 'N':
prop.p_type = AMF_NUMBER;
prop.p_vu.p_number = strtod(p, NULL);
break;
case 'O':
prop.p_type = AMF_OBJECT;
break;
default:
return -1;
}
}
else
return -1;
if (*depth)
{
AMFObject *o2;
for (i=0; i<*depth; i++)
{
o2 = &obj->o_props[obj->o_num-1].p_vu.p_object;
obj = o2;
}
}
AMF_AddProp(obj, &prop);
if (prop.p_type == AMF_OBJECT)
(*depth)++;
return 0;
}
int RTMP_SetOpt(PILI_RTMP *r, const AVal *opt, AVal *arg, RTMPError *error)
{
int i;
void *v;
for (i=0; options[i].name.av_len; i++) {
if (opt->av_len != options[i].name.av_len) continue;
if (strcasecmp(opt->av_val, options[i].name.av_val)) continue;
v = (char *)r + options[i].off;
switch(options[i].otype) {
case OPT_STR: {
AVal *aptr = v;
*aptr = *arg; }
break;
case OPT_INT: {
long l = strtol(arg->av_val, NULL, 0);
*(int *)v = l; }
break;
case OPT_BOOL: {
int j, fl;
fl = *(int *)v;
for (j=0; truth[j].av_len; j++) {
if (arg->av_len != truth[j].av_len) continue;
if (strcasecmp(arg->av_val, truth[j].av_val)) continue;
fl |= options[i].omisc; break; }
*(int *)v = fl;
}
break;
case OPT_CONN:
if (parseAMF(&r->Link.extras, arg, &r->Link.edepth))
return FALSE;
break;
}
break;
}
if (!options[i].name.av_len) {
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "Unknown option ");
strcat(msg, opt->av_val);
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorUnknowOption;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "Unknown option %s", opt->av_val);
RTMP_OptUsage();
return FALSE;
}
return TRUE;
}
int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error)
{
AVal opt, arg;
char *p1, *p2, *ptr = strchr(url, ' ');
int ret, len;
unsigned int port = 0;
if (ptr)
*ptr = '\0';
len = (int)strlen(url);
ret = PILI_RTMP_ParseURL(url, &r->Link.protocol, &r->Link.hostname,
&port, &r->Link.playpath0, &r->Link.app);
if (!ret)
return ret;
r->Link.port = port;
r->Link.playpath = r->Link.playpath0;
while (ptr) {
*ptr++ = '\0';
p1 = ptr;
p2 = strchr(p1, '=');
if (!p2)
break;
opt.av_val = p1;
opt.av_len = p2 - p1;
*p2++ = '\0';
arg.av_val = p2;
ptr = strchr(p2, ' ');
if (ptr) {
*ptr = '\0';
arg.av_len = ptr - p2;
/* skip repeated spaces */
while(ptr[1] == ' ')
*ptr++ = '\0';
} else {
arg.av_len = strlen(p2);
}
/* unescape */
port = arg.av_len;
for (p1=p2; port >0;) {
if (*p1 == '\\') {
unsigned int c;
if (port < 3)
return FALSE;
sscanf(p1+1, "%02x", &c);
*p2++ = c;
port -= 3;
p1 += 3;
} else {
*p2++ = *p1++;
port--;
}
}
arg.av_len = p2 - arg.av_val;
ret = RTMP_SetOpt(r, &opt, &arg, error);
if (!ret)
return ret;
}
if (!r->Link.tcUrl.av_len)
{
r->Link.tcUrl.av_val = url;
if (r->Link.app.av_len)
{
if (r->Link.app.av_val < url + len)
{
/* if app is part of original url, just use it */
r->Link.tcUrl.av_len = r->Link.app.av_len + (r->Link.app.av_val - url);
}
else
{
len = r->Link.hostname.av_len + r->Link.app.av_len +
sizeof("rtmpte://:65535/");
r->Link.tcUrl.av_val = malloc(len);
r->Link.tcUrl.av_len = snprintf(r->Link.tcUrl.av_val, len,
"%s://%.*s:%d/%.*s",
PILI_RTMPProtocolStringsLower[r->Link.protocol],
r->Link.hostname.av_len, r->Link.hostname.av_val,
r->Link.port,
r->Link.app.av_len, r->Link.app.av_val);
r->Link.lFlags |= RTMP_LF_FTCU;
}
}
else
{
r->Link.tcUrl.av_len = strlen(url);
}
}
#ifdef CRYPTO
if ((r->Link.lFlags & RTMP_LF_SWFV) && r->Link.swfUrl.av_len)
RTMP_HashSWF(r->Link.swfUrl.av_val, &r->Link.SWFSize,
(unsigned char *)r->Link.SWFHash, r->Link.swfAge);
#endif
if (r->Link.port == 0)
{
if (r->Link.protocol & RTMP_FEATURE_SSL)
r->Link.port = 443;
else if (r->Link.protocol & RTMP_FEATURE_HTTP)
r->Link.port = 80;
else
r->Link.port = 1935;
}
return TRUE;
}
static int
add_addr_info(PILI_RTMP *r, struct sockaddr_in *service, AVal *host, int port, RTMPError *error)
{
char *hostname;
int ret = TRUE;
if (host->av_val[host->av_len])
{
hostname = malloc(host->av_len+1);
memcpy(hostname, host->av_val, host->av_len);
hostname[host->av_len] = '\0';
}
else
{
hostname = host->av_val;
}
service->sin_addr.s_addr = inet_addr(hostname);
if (service->sin_addr.s_addr == INADDR_NONE)
{
struct hostent *host = gethostbyname(hostname);
if (host == NULL || host->h_addr == NULL)
{
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "Problem accessing the DNS. addr: ");
strcat(msg, hostname);
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorAccessDNSFailed;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname);
ret = FALSE;
goto finish;
}
service->sin_addr = *(struct in_addr *)host->h_addr;
}
service->sin_port = htons(port);
finish:
if (hostname != host->av_val)
free(hostname);
return ret;
}
int
PILI_RTMP_Connect0(PILI_RTMP *r, struct sockaddr * service, RTMPError *error)
{
r->m_sb.sb_timedout = FALSE;
r->m_pausing = 0;
r->m_fDuration = 0.0;
r->m_sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (r->m_sb.sb_socket != -1)
{
if (connect(r->m_sb.sb_socket, service, sizeof(struct sockaddr)) < 0)
{
int err = GetSockError();
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "Failed to connect socket. ");
strcat(msg, strerror(err));
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorFailedToConnectSocket;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "%s, failed to connect socket. %d (%s)",
__FUNCTION__, err, strerror(err));
PILI_RTMP_Close(r, NULL);
return FALSE;
}
if (r->Link.socksport)
{
RTMP_Log(RTMP_LOGDEBUG, "%s ... SOCKS negotiation", __FUNCTION__);
if (!SocksNegotiate(r, error))
{
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "Socks negotiation failed.");
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorSocksNegotiationFailed;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "%s, SOCKS negotiation failed.", __FUNCTION__);
PILI_RTMP_Close(r, NULL);
return FALSE;
}
}
}
else
{
int err = GetSockError();
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "Failed to create socket. ");
strcat(msg, strerror(err));
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorFailedToCreateSocket;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "%s, failed to create socket. Error: %d (%s)", __FUNCTION__, err, strerror(err));
return FALSE;
}
/* set receive timeout */
{
SET_RCVTIMEO(tv, r->Link.timeout);
if (setsockopt
(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)))
{
RTMP_Log(RTMP_LOGERROR, "%s, Setting socket recieve timeout to %ds failed!",
__FUNCTION__, r->Link.timeout);
}
}
/* set send timeout*/
{
struct timeval timeout;
timeout.tv_sec = r->Link.send_timeout;
timeout.tv_usec = 0;
if (setsockopt
(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)))
{
RTMP_Log(RTMP_LOGERROR, "%s, Setting socket send timeout to %ds failed!",
__FUNCTION__, r->Link.timeout);
}
}
/* ignore sigpipe */
int kOne = 1;
#ifdef __linux
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, MSG_NOSIGNAL, &kOne, sizeof(kOne));
#else
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_NOSIGPIPE, &kOne, sizeof(kOne));
#endif
if (r->m_tcp_nodelay)
{
int on = 1;
setsockopt(r->m_sb.sb_socket, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on));
}
return TRUE;
}
int
PILI_RTMP_Connect1(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error)
{
if (r->Link.protocol & RTMP_FEATURE_SSL)
{
#if defined(CRYPTO) && !defined(NO_SSL)
TLS_client(RTMP_TLS_ctx, r->m_sb.sb_ssl);
TLS_setfd(r->m_sb.sb_ssl, r->m_sb.sb_socket);
if (TLS_connect(r->m_sb.sb_ssl) < 0)
{
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "TLS_Connect failed.");
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorTLSConnectFailed;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__);
RTMP_Close(r, NULL);
return FALSE;
}
#else
if (error) {
char msg[100];
memset(msg, 0, 100);
strcat(msg, "No SSL/TLS support.");
RTMPError_Alloc(error, strlen(msg));
error->code = RTMPErrorNoSSLOrTLSSupport;
strcpy(error->message, msg);
}
RTMP_Log(RTMP_LOGERROR, "%s, no SSL/TLS support", __FUNCTION__);
PILI_RTMP_Close(r, NULL);
return FALSE;
#endif
}
if (r->Link.protocol & RTMP_FEATURE_HTTP)
{
r->m_msgCounter = 1;
r->m_clientID.av_val = NULL;
r->m_clientID.av_len = 0;
HTTP_Post(r, RTMPT_OPEN, "", 1);
HTTP_read(r, 1);
r->m_msgCounter = 0;
}
RTMP_Log(RTMP_LOGDEBUG, "%s, ... connected, handshaking", __FUNCTION__);
if (!HandShake(r, TRUE, error))
{
if (error) {
char msg[100];
memset(msg, 0, 100);