forked from resiprocate/resiprocate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlow.cxx
938 lines (843 loc) · 29.8 KB
/
Flow.cxx
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <asio.hpp>
#ifdef USE_SSL
#include <asio/ssl.hpp>
#endif
#include <boost/function.hpp>
#include <rutil/Log.hxx>
#include <rutil/Logger.hxx>
#include <rutil/Timer.hxx>
#include <rutil/Lock.hxx>
#include "FlowManagerSubsystem.hxx"
#include "ErrorCode.hxx"
#include "Flow.hxx"
#include "MediaStream.hxx"
#include "FlowDtlsSocketContext.hxx"
using namespace flowmanager;
using namespace resip;
#ifdef USE_SSL
using namespace dtls;
#endif
using namespace std;
#define MAX_RECEIVE_FIFO_DURATION 10 // seconds
#define MAX_RECEIVE_FIFO_SIZE (100 * MAX_RECEIVE_FIFO_DURATION) // 1000 = 1 message every 10 ms for 10 seconds - appropriate for RTP
#define RESIPROCATE_SUBSYSTEM FlowManagerSubsystem::FLOWMANAGER
const char* srtp_error_string(err_status_t error)
{
switch(error)
{
case err_status_ok:
return "nothing to report";
break;
case err_status_fail:
return "unspecified failure";
break;
case err_status_bad_param:
return "unsupported parameter";
break;
case err_status_alloc_fail:
return "couldn't allocate memory";
break;
case err_status_dealloc_fail:
return "couldn't deallocate properly";
break;
case err_status_init_fail:
return "couldn't initialize";
break;
case err_status_terminus:
return "can't process as much data as requested";
break;
case err_status_auth_fail:
return "authentication failure";
break;
case err_status_cipher_fail:
return "cipher failure";
break;
case err_status_replay_fail:
return "replay check failed (bad index)";
break;
case err_status_replay_old:
return "replay check failed (index too old)";
break;
case err_status_algo_fail:
return "algorithm failed test routine";
break;
case err_status_no_such_op:
return "unsupported operation";
break;
case err_status_no_ctx:
return "no appropriate context found";
break;
case err_status_cant_check:
return "unable to perform desired validation";
break;
case err_status_key_expired:
return "can't use key any more";
break;
case err_status_socket_err:
return "error in use of socket";
break;
case err_status_signal_err:
return "error in use POSIX signals";
break;
case err_status_nonce_bad:
return "nonce check failed";
break;
case err_status_read_fail:
return "couldn't read data";
break;
case err_status_write_fail:
return "couldn't write data";
break;
case err_status_parse_err:
return "error pasring data";
break;
case err_status_encode_err:
return "error encoding data";
break;
case err_status_semaphore_err:
return "error while using semaphores";
break;
case err_status_pfkey_err:
return "error while using pfkey";
break;
default:
return "unrecognized error";
}
}
Flow::Flow(asio::io_service& ioService,
#ifdef USE_SSL
asio::ssl::context& sslContext,
#endif
unsigned int componentId,
const StunTuple& localBinding,
MediaStream& mediaStream)
: mIOService(ioService),
#ifdef USE_SSL
mSslContext(sslContext),
#endif
mComponentId(componentId),
mLocalBinding(localBinding),
mMediaStream(mediaStream),
mAllocationProps(StunMessage::PropsNone),
mReservationToken(0),
mFlowState(Unconnected),
mReceivedDataFifo(MAX_RECEIVE_FIFO_DURATION,MAX_RECEIVE_FIFO_SIZE)
{
InfoLog(<< "Flow: flow created for " << mLocalBinding << " ComponentId=" << mComponentId);
switch(mLocalBinding.getTransportType())
{
case StunTuple::UDP:
mTurnSocket.reset(new TurnAsyncUdpSocket(mIOService, this, mLocalBinding.getAddress(), mLocalBinding.getPort()));
break;
case StunTuple::TCP:
mTurnSocket.reset(new TurnAsyncTcpSocket(mIOService, this, mLocalBinding.getAddress(), mLocalBinding.getPort()));
break;
#ifdef USE_SSL
case StunTuple::TLS:
mTurnSocket.reset(new TurnAsyncTlsSocket(mIOService,
mSslContext,
false, // validateServerCertificateHostname - TODO - make this configurable
this,
mLocalBinding.getAddress(),
mLocalBinding.getPort()));
#endif
break;
default:
// Bad Transport type!
assert(false);
}
if(mTurnSocket.get() &&
mMediaStream.mNatTraversalMode != MediaStream::NoNatTraversal &&
!mMediaStream.mStunUsername.empty() &&
!mMediaStream.mStunPassword.empty())
{
mTurnSocket->setUsernameAndPassword(mMediaStream.mStunUsername.c_str(), mMediaStream.mStunPassword.c_str(), false);
}
}
Flow::~Flow()
{
InfoLog(<< "Flow: flow destroyed for " << mLocalBinding << " ComponentId=" << mComponentId);
#ifdef USE_SSL
// Cleanup DtlsSockets
{
Lock lock(mMutex);
std::map<reTurn::StunTuple, dtls::DtlsSocket*>::iterator it;
for(it = mDtlsSockets.begin(); it != mDtlsSockets.end(); it++)
{
delete it->second;
}
}
#endif //USE_SSL
// Cleanup TurnSocket
if(mTurnSocket.get())
{
mTurnSocket->disableTurnAsyncHandler();
mTurnSocket->close();
}
}
void
Flow::activateFlow(UInt64 reservationToken)
{
mReservationToken = reservationToken;
activateFlow(StunMessage::PropsNone);
}
void
Flow::activateFlow(UInt8 allocationProps)
{
mAllocationProps = allocationProps;
if(mTurnSocket.get())
{
if(mMediaStream.mNatTraversalMode != MediaStream::NoNatTraversal &&
!mMediaStream.mNatTraversalServerHostname.empty())
{
changeFlowState(ConnectingServer);
mTurnSocket->connect(mMediaStream.mNatTraversalServerHostname.c_str(),
mMediaStream.mNatTraversalServerPort);
}
else
{
changeFlowState(Ready);
mMediaStream.onFlowReady(mComponentId);
}
}
}
unsigned int
Flow::getSelectSocketDescriptor()
{
return mFakeSelectSocketDescriptor.getSocketDescriptor();
}
unsigned int
Flow::getSocketDescriptor()
{
if(mTurnSocket.get() != 0)
{
return mTurnSocket->getSocketDescriptor();
}
else
{
return 0;
}
}
// Turn Send Methods
void
Flow::send(char* buffer, unsigned int size)
{
assert(mTurnSocket.get());
if(isReady())
{
if(processSendData(buffer, size, mTurnSocket->getConnectedAddress(), mTurnSocket->getConnectedPort()))
{
mTurnSocket->send(buffer, size);
}
}
else
{
onSendFailure(mTurnSocket->getSocketDescriptor(), asio::error_code(flowmanager::InvalidState, asio::error::misc_category));
}
}
void
Flow::sendTo(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int size)
{
assert(mTurnSocket.get());
if(isReady())
{
if(processSendData(buffer, size, address, port))
{
mTurnSocket->sendTo(address, port, buffer, size);
}
}
else
{
onSendFailure(mTurnSocket->getSocketDescriptor(), asio::error_code(flowmanager::InvalidState, asio::error::misc_category));
}
}
// Note: this fn is used to send raw data to the far end, without attempting to SRTP encrypt it - ie. used for sending DTLS traffic
void
Flow::rawSendTo(const asio::ip::address& address, unsigned short port, const char* buffer, unsigned int size)
{
assert(mTurnSocket.get());
mTurnSocket->sendTo(address, port, buffer, size);
}
bool
Flow::processSendData(char* buffer, unsigned int& size, const asio::ip::address& address, unsigned short port)
{
if(mMediaStream.mSRTPSessionOutCreated)
{
err_status_t status = mMediaStream.srtpProtect((void*)buffer, (int*)&size, mComponentId == RTCP_COMPONENT_ID);
if(status != err_status_ok)
{
ErrLog(<< "Unable to SRTP protect the packet, error code=" << status << "(" << srtp_error_string(status) << ") ComponentId=" << mComponentId);
onSendFailure(mTurnSocket->getSocketDescriptor(), asio::error_code(flowmanager::SRTPError, asio::error::misc_category));
return false;
}
}
#ifdef USE_SSL
else
{
Lock lock(mMutex);
DtlsSocket* dtlsSocket = getDtlsSocket(StunTuple(mLocalBinding.getTransportType(), address, port));
if(dtlsSocket)
{
if(((FlowDtlsSocketContext*)dtlsSocket->getSocketContext())->isSrtpInitialized())
{
err_status_t status = ((FlowDtlsSocketContext*)dtlsSocket->getSocketContext())->srtpProtect((void*)buffer, (int*)&size, mComponentId == RTCP_COMPONENT_ID);
if(status != err_status_ok)
{
ErrLog(<< "Unable to SRTP protect the packet, error code=" << status << "(" << srtp_error_string(status) << ") ComponentId=" << mComponentId);
onSendFailure(mTurnSocket->getSocketDescriptor(), asio::error_code(flowmanager::SRTPError, asio::error::misc_category));
return false;
}
}
else
{
//WarningLog(<< "Unable to send packet yet - handshake is not completed yet, ComponentId=" << mComponentId);
onSendFailure(mTurnSocket->getSocketDescriptor(), asio::error_code(flowmanager::InvalidState, asio::error::misc_category));
return false;
}
}
}
#endif //USE_SSL
return true;
}
// Receive Methods
asio::error_code
Flow::receiveFrom(const asio::ip::address& address, unsigned short port, char* buffer, unsigned int& size, unsigned int timeout)
{
bool done = false;
asio::error_code errorCode;
UInt64 startTime = Timer::getTimeMs();
unsigned int recvTimeout;
while(!done)
{
// We define timeout of 0 differently then TimeLimitFifo - we want 0 to mean no-block at all
if(timeout == 0 && mReceivedDataFifo.empty())
{
// timeout
return asio::error_code(flowmanager::ReceiveTimeout, asio::error::misc_category);
}
recvTimeout = timeout ? (unsigned int)(timeout - (Timer::getTimeMs() - startTime)) : 0;
if(timeout != 0 && recvTimeout <= 0)
{
// timeout
return asio::error_code(flowmanager::ReceiveTimeout, asio::error::misc_category);
}
ReceivedData* receivedData = mReceivedDataFifo.getNext(recvTimeout);
if(receivedData)
{
mFakeSelectSocketDescriptor.receive();
// discard any data not from address/port requested
if(address == receivedData->mAddress && port == receivedData->mPort)
{
errorCode = processReceivedData(buffer, size, receivedData);
done = true;
}
delete receivedData;
}
else
{
// timeout
errorCode = asio::error_code(flowmanager::ReceiveTimeout, asio::error::misc_category);
done = true;
}
}
return errorCode;
}
asio::error_code
Flow::receive(char* buffer, unsigned int& size, unsigned int timeout, asio::ip::address* sourceAddress, unsigned short* sourcePort)
{
asio::error_code errorCode;
//InfoLog(<< "Flow::receive called with buffer size=" << size << ", timeout=" << timeout);
// We define timeout of 0 differently then TimeLimitFifo - we want 0 to mean no-block at all
if(timeout == 0 && mReceivedDataFifo.empty())
{
// timeout
InfoLog(<< "Receive timeout (timeout==0 and fifo empty)!");
return asio::error_code(flowmanager::ReceiveTimeout, asio::error::misc_category);
}
if(mReceivedDataFifo.empty())
{
WarningLog(<< "Receive called when there is no data available! ComponentId=" << mComponentId);
}
ReceivedData* receivedData = mReceivedDataFifo.getNext(timeout);
if(receivedData)
{
mFakeSelectSocketDescriptor.receive();
errorCode = processReceivedData(buffer, size, receivedData, sourceAddress, sourcePort);
delete receivedData;
}
else
{
// timeout
InfoLog(<< "Receive timeout! ComponentId=" << mComponentId);
errorCode = asio::error_code(flowmanager::ReceiveTimeout, asio::error::misc_category);
}
return errorCode;
}
asio::error_code
Flow::processReceivedData(char* buffer, unsigned int& size, ReceivedData* receivedData, asio::ip::address* sourceAddress, unsigned short* sourcePort)
{
asio::error_code errorCode;
unsigned int receivedsize = receivedData->mData->size();
// SRTP Unprotect (if required)
if(mMediaStream.mSRTPSessionInCreated)
{
err_status_t status = mMediaStream.srtpUnprotect((void*)receivedData->mData->data(), (int*)&receivedsize, mComponentId == RTCP_COMPONENT_ID);
if(status != err_status_ok)
{
ErrLog(<< "Unable to SRTP unprotect the packet (componentid=" << mComponentId << "), error code=" << status << "(" << srtp_error_string(status) << ")");
//errorCode = asio::error_code(flowmanager::SRTPError, asio::error::misc_category);
}
}
#ifdef USE_SSL
else
{
Lock lock(mMutex);
DtlsSocket* dtlsSocket = getDtlsSocket(StunTuple(mLocalBinding.getTransportType(), receivedData->mAddress, receivedData->mPort));
if(dtlsSocket)
{
if(((FlowDtlsSocketContext*)dtlsSocket->getSocketContext())->isSrtpInitialized())
{
err_status_t status = ((FlowDtlsSocketContext*)dtlsSocket->getSocketContext())->srtpUnprotect((void*)receivedData->mData->data(), (int*)&receivedsize, mComponentId == RTCP_COMPONENT_ID);
if(status != err_status_ok)
{
ErrLog(<< "Unable to SRTP unprotect the packet (componentid=" << mComponentId << "), error code=" << status << "(" << srtp_error_string(status) << ")");
//errorCode = asio::error_code(flowmanager::SRTPError, asio::error::misc_category);
}
}
else
{
//WarningLog(<< "Unable to send packet yet - handshake is not completed yet, ComponentId=" << mComponentId);
errorCode = asio::error_code(flowmanager::InvalidState, asio::error::misc_category);
}
}
}
#endif //USE_SSL
if(!errorCode)
{
if(size > receivedsize)
{
size = receivedsize;
memcpy(buffer, receivedData->mData->data(), size);
//InfoLog(<< "Received a buffer of size=" << receivedData->mData.size());
}
else
{
// Receive buffer too small
InfoLog(<< "Receive buffer too small for data size=" << receivedsize << " ComponentId=" << mComponentId);
errorCode = asio::error_code(flowmanager::BufferTooSmall, asio::error::misc_category);
}
if(sourceAddress)
{
*sourceAddress = receivedData->mAddress;
}
if(sourcePort)
{
*sourcePort = receivedData->mPort;
}
}
return errorCode;
}
void
Flow::setActiveDestination(const char* address, unsigned short port)
{
if(mTurnSocket.get())
{
if(mMediaStream.mNatTraversalMode != MediaStream::TurnAllocation)
{
changeFlowState(Connecting);
mTurnSocket->connect(address, port);
}
else
{
mTurnSocket->setActiveDestination(asio::ip::address::from_string(address), port);
}
}
else
WarningLog(<<"No TURN Socket, can't send media to destination");
}
#ifdef USE_SSL
void
Flow::startDtlsClient(const char* address, unsigned short port)
{
Lock lock(mMutex);
createDtlsSocketClient(StunTuple(mLocalBinding.getTransportType(), asio::ip::address::from_string(address), port));
}
#endif
void
Flow::setRemoteSDPFingerprint(const resip::Data& fingerprint)
{
Lock lock(mMutex);
mRemoteSDPFingerprint = fingerprint;
#ifdef USE_SSL
// Check all existing DtlsSockets and tear down those that don't match
std::map<reTurn::StunTuple, dtls::DtlsSocket*>::iterator it;
for(it = mDtlsSockets.begin(); it != mDtlsSockets.end(); it++)
{
if(it->second->handshakeCompleted() &&
!it->second->checkFingerprint(fingerprint.c_str(), fingerprint.size()))
{
InfoLog(<< "Marking Dtls socket bad with non-matching fingerprint!");
((FlowDtlsSocketContext*)it->second->getSocketContext())->fingerprintMismatch();
}
}
#endif //USE_SSL
}
const resip::Data
Flow::getRemoteSDPFingerprint()
{
Lock lock(mMutex);
return mRemoteSDPFingerprint;
}
const StunTuple&
Flow::getLocalTuple()
{
return mLocalBinding;
}
StunTuple
Flow::getSessionTuple()
{
assert(mFlowState == Ready);
Lock lock(mMutex);
if(mMediaStream.mNatTraversalMode == MediaStream::TurnAllocation)
{
return mRelayTuple;
}
else if(mMediaStream.mNatTraversalMode == MediaStream::StunBindDiscovery)
{
return mReflexiveTuple;
}
return mLocalBinding;
}
StunTuple
Flow::getRelayTuple()
{
assert(mFlowState == Ready);
Lock lock(mMutex);
return mRelayTuple;
}
StunTuple
Flow::getReflexiveTuple()
{
assert(mFlowState == Ready);
Lock lock(mMutex);
return mReflexiveTuple;
}
UInt64
Flow::getReservationToken()
{
assert(mFlowState == Ready);
Lock lock(mMutex);
return mReservationToken;
}
void
Flow::onConnectSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port)
{
InfoLog(<< "Flow::onConnectSuccess: socketDesc=" << socketDesc << ", address=" << address.to_string() << ", port=" << port << ", componentId=" << mComponentId);
// Start candidate discovery
switch(mMediaStream.mNatTraversalMode)
{
case MediaStream::StunBindDiscovery:
if(mFlowState == ConnectingServer)
{
changeFlowState(Binding);
mTurnSocket->bindRequest();
}
else
{
changeFlowState(Ready);
mMediaStream.onFlowReady(mComponentId);
}
break;
case MediaStream::TurnAllocation:
changeFlowState(Allocating);
mTurnSocket->createAllocation(TurnAsyncSocket::UnspecifiedLifetime,
TurnAsyncSocket::UnspecifiedBandwidth,
mAllocationProps,
mReservationToken != 0 ? mReservationToken : TurnAsyncSocket::UnspecifiedToken,
StunTuple::UDP); // Always relay as UDP
break;
case MediaStream::NoNatTraversal:
default:
changeFlowState(Ready);
mMediaStream.onFlowReady(mComponentId);
break;
}
}
void
Flow::onConnectFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onConnectFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << ", componentId=" << mComponentId);
changeFlowState(Unconnected);
mMediaStream.onFlowError(mComponentId, e.value()); // TODO define different error code?
}
void
Flow::onSharedSecretSuccess(unsigned int socketDesc, const char* username, unsigned int usernameSize, const char* password, unsigned int passwordSize)
{
InfoLog(<< "Flow::onSharedSecretSuccess: socketDesc=" << socketDesc << ", username=" << username << ", password=" << password << ", componentId=" << mComponentId);
}
void
Flow::onSharedSecretFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onSharedSecretFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
void
Flow::onBindSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& stunServerTuple)
{
InfoLog(<< "Flow::onBindingSuccess: socketDesc=" << socketDesc << ", reflexive=" << reflexiveTuple << ", componentId=" << mComponentId);
{
Lock lock(mMutex);
mReflexiveTuple = reflexiveTuple;
}
changeFlowState(Ready);
mMediaStream.onFlowReady(mComponentId);
}
void
Flow::onBindFailure(unsigned int socketDesc, const asio::error_code& e, const StunTuple& stunServerTuple)
{
WarningLog(<< "Flow::onBindingFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
changeFlowState(Connected);
mMediaStream.onFlowError(mComponentId, e.value()); // TODO define different error code?
}
void
Flow::onAllocationSuccess(unsigned int socketDesc, const StunTuple& reflexiveTuple, const StunTuple& relayTuple, unsigned int lifetime, unsigned int bandwidth, UInt64 reservationToken)
{
InfoLog(<< "Flow::onAllocationSuccess: socketDesc=" << socketDesc <<
", reflexive=" << reflexiveTuple <<
", relay=" << relayTuple <<
", lifetime=" << lifetime <<
", bandwidth=" << bandwidth <<
", reservationToken=" << reservationToken <<
", componentId=" << mComponentId);
{
Lock lock(mMutex);
mReflexiveTuple = reflexiveTuple;
mRelayTuple = relayTuple;
mReservationToken = reservationToken;
}
changeFlowState(Ready);
mMediaStream.onFlowReady(mComponentId);
}
void
Flow::onAllocationFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onAllocationFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
changeFlowState(Connected);
mMediaStream.onFlowError(mComponentId, e.value()); // TODO define different error code?
}
void
Flow::onRefreshSuccess(unsigned int socketDesc, unsigned int lifetime)
{
InfoLog(<< "Flow::onRefreshSuccess: socketDesc=" << socketDesc << ", lifetime=" << lifetime << ", componentId=" << mComponentId);
if(lifetime == 0)
{
changeFlowState(Connected);
}
}
void
Flow::onRefreshFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onRefreshFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
void
Flow::onSetActiveDestinationSuccess(unsigned int socketDesc)
{
InfoLog(<< "Flow::onSetActiveDestinationSuccess: socketDesc=" << socketDesc << ", componentId=" << mComponentId);
}
void
Flow::onSetActiveDestinationFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onSetActiveDestinationFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
void
Flow::onClearActiveDestinationSuccess(unsigned int socketDesc)
{
InfoLog(<< "Flow::onClearActiveDestinationSuccess: socketDesc=" << socketDesc << ", componentId=" << mComponentId);
}
void
Flow::onClearActiveDestinationFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onClearActiveDestinationFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
void
Flow::onChannelBindRequestSent(unsigned int socketDesc, unsigned short channelNumber)
{
InfoLog(<< "Flow::onChannelBindRequestSent: socketDesc=" << socketDesc << ", channelNumber=" << channelNumber << ", componentId=" << mComponentId);
}
void
Flow::onChannelBindSuccess(unsigned int socketDesc, unsigned short channelNumber)
{
InfoLog(<< "Flow::onChannelBindSuccess: socketDesc=" << socketDesc << ", channelNumber=" << channelNumber << ", componentId=" << mComponentId);
}
void
Flow::onChannelBindFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onChannelBindFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
void
Flow::onSendSuccess(unsigned int socketDesc)
{
//InfoLog(<< "Flow::onSendSuccess: socketDesc=" << socketDesc);
}
void
Flow::onSendFailure(unsigned int socketDesc, const asio::error_code& e)
{
if(e.value() == InvalidState)
{
// Note: if setActiveDestination is called it can take some time to "connect" the socket to the destination
// and send requests during this time, will be discarded - this can be considered normal
InfoLog(<< "Flow::onSendFailure: socketDesc=" << socketDesc << " socket is not in correct state to send yet, componentId=" << mComponentId );
}
else
{
WarningLog(<< "Flow::onSendFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId );
}
}
void
Flow::onReceiveSuccess(unsigned int socketDesc, const asio::ip::address& address, unsigned short port, boost::shared_ptr<reTurn::DataBuffer>& data)
{
DebugLog(<< "Flow::onReceiveSuccess: socketDesc=" << socketDesc << ", fromAddress=" << address.to_string() << ", fromPort=" << port << ", size=" << data->size() << ", componentId=" << mComponentId);
#ifdef USE_SSL
// Check if packet is a dtls packet - if so then process it
// Note: Stun messaging should be picked off by the reTurn library - so we only need to tell the difference between DTLS and SRTP here
if(DtlsFactory::demuxPacket((const unsigned char*) data->data(), data->size()) == DtlsFactory::dtls)
{
Lock lock(mMutex);
StunTuple endpoint(mLocalBinding.getTransportType(), address, port);
DtlsSocket* dtlsSocket = getDtlsSocket(endpoint);
if(!dtlsSocket)
{
// If don't have a socket already for this endpoint and we are receiving data, then assume we are the server side of the DTLS connection
dtlsSocket = createDtlsSocketServer(endpoint);
}
if(dtlsSocket)
{
dtlsSocket->handlePacketMaybe((const unsigned char*) data->data(), data->size());
}
// Packet was a DTLS packet - do not queue for app
return;
}
#endif
if(!mReceivedDataFifo.add(new ReceivedData(address, port, data), ReceivedDataFifo::EnforceTimeDepth))
{
WarningLog(<< "Flow::onReceiveSuccess: TimeLimitFifo is full - discarding data! componentId=" << mComponentId);
}
else
{
mFakeSelectSocketDescriptor.send();
}
}
void
Flow::onReceiveFailure(unsigned int socketDesc, const asio::error_code& e)
{
WarningLog(<< "Flow::onReceiveFailure: socketDesc=" << socketDesc << " error=" << e.value() << "(" << e.message() << "), componentId=" << mComponentId);
// Make sure we keep receiving if we get an ICMP error on a UDP socket
if(e.value() == asio::error::connection_reset && mLocalBinding.getTransportType() == StunTuple::UDP)
{
assert(mTurnSocket.get());
mTurnSocket->turnReceive();
}
}
void
Flow::onIncomingBindRequestProcessed(unsigned int socketDesc, const StunTuple& sourceTuple)
{
InfoLog(<< "Flow::onIncomingBindRequestProcessed: socketDesc=" << socketDesc << ", sourceTuple=" << sourceTuple );
// TODO - handle
}
void
Flow::changeFlowState(FlowState newState)
{
InfoLog(<< "Flow::changeState: oldState=" << flowStateToString(mFlowState) << ", newState=" << flowStateToString(newState) << ", componentId=" << mComponentId);
mFlowState = newState;
}
const char*
Flow::flowStateToString(FlowState state)
{
switch(state)
{
case Unconnected:
return "Unconnected";
case ConnectingServer:
return "ConnectingServer";
case Connecting:
return "Connecting";
case Binding:
return "Binding";
case Allocating:
return "Allocating";
case Connected:
return "Connected";
case Ready:
return "Ready";
default:
assert(false);
return "Unknown";
}
}
#ifdef USE_SSL
DtlsSocket*
Flow::getDtlsSocket(const StunTuple& endpoint)
{
std::map<reTurn::StunTuple, dtls::DtlsSocket*>::iterator it = mDtlsSockets.find(endpoint);
if(it != mDtlsSockets.end())
{
return it->second;
}
return 0;
}
DtlsSocket*
Flow::createDtlsSocketClient(const StunTuple& endpoint)
{
DtlsSocket* dtlsSocket = getDtlsSocket(endpoint);
if(!dtlsSocket && mMediaStream.mDtlsFactory)
{
InfoLog(<< "Creating DTLS Client socket, componentId=" << mComponentId);
std::auto_ptr<DtlsSocketContext> socketContext(new FlowDtlsSocketContext(*this, endpoint.getAddress(), endpoint.getPort()));
dtlsSocket = mMediaStream.mDtlsFactory->createClient(socketContext);
dtlsSocket->startClient();
mDtlsSockets[endpoint] = dtlsSocket;
}
return dtlsSocket;
}
DtlsSocket*
Flow::createDtlsSocketServer(const StunTuple& endpoint)
{
DtlsSocket* dtlsSocket = getDtlsSocket(endpoint);
if(!dtlsSocket && mMediaStream.mDtlsFactory)
{
InfoLog(<< "Creating DTLS Server socket, componentId=" << mComponentId);
std::auto_ptr<DtlsSocketContext> socketContext(new FlowDtlsSocketContext(*this, endpoint.getAddress(), endpoint.getPort()));
dtlsSocket = mMediaStream.mDtlsFactory->createServer(socketContext);
mDtlsSockets[endpoint] = dtlsSocket;
}
return dtlsSocket;
}
#endif
/* ====================================================================
Copyright (c) 2007-2008, Plantronics, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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.
3. Neither the name of Plantronics nor the names of its contributors
may 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.
==================================================================== */