Debian Codegraph @

srtcore/core.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above
20 copyright notice, this list of conditions and the
21 following disclaimer.
22
23 * Redistributions in binary form must reproduce the
24 above copyright notice, this list of conditions
25 and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 * Neither the name of the University of Illinois
29 nor the names of its contributors may be used to
30 endorse or promote products derived from this
31 software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45
46 /*****************************************************************************
47 written by
48 Yunhong Gu, last updated 02/28/2012
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #include "platform_sys.h"
54
55 // Linux specific
56 #ifdef SRT_ENABLE_BINDTODEVICE
57 #include <linux/if.h>
58 #endif
59
60 #include <cmath>
61 #include <sstream>
62 #include <algorithm>
63 #include <iterator>
64 #include "srt.h"
65 #include "access_control.h" // Required for SRT_REJX_FALLBACK
66 #include "queue.h"
67 #include "api.h"
68 #include "core.h"
69 #include "logging.h"
70 #include "crypto.h"
71 #include "logging_api.h" // Required due to containing extern srt_logger_config
72 #include "logger_defs.h"
73
74 #if !HAVE_CXX11
75 // for pthread_once
76 #include <pthread.h>
77 #endif
78
79 // Again, just in case when some "smart guy" provided such a global macro
80 #ifdef min
81 #undef min
82 #endif
83 #ifdef max
84 #undef max
85 #endif
86
87 using namespace std;
88 using namespace srt;
89 using namespace srt::sync;
90 using namespace srt_logging;
91
92 const SRTSOCKET UDT::INVALID_SOCK = srt::CUDT::INVALID_SOCK;
93 const int UDT::ERROR = srt::CUDT::ERROR;
94
95 //#define SRT_CMD_HSREQ 1 /* SRT Handshake Request (sender) */
96 #define SRT_CMD_HSREQ_MINSZ 8 /* Minimum Compatible (1.x.x) packet size (bytes) */
97 #define SRT_CMD_HSREQ_SZ 12 /* Current version packet size */
98 #if SRT_CMD_HSREQ_SZ > SRT_CMD_MAXSZ
99 #error SRT_CMD_MAXSZ too small
100 #endif
101 /* Handshake Request (Network Order)
102 0[31..0]: SRT version SRT_DEF_VERSION
103 1[31..0]: Options 0 [ | SRT_OPT_TSBPDSND ][ | SRT_OPT_HAICRYPT ]
104 2[31..16]: TsbPD resv 0
105 2[15..0]: TsbPD delay [0..60000] msec
106 */
107
108 //#define SRT_CMD_HSRSP 2 /* SRT Handshake Response (receiver) */
109 #define SRT_CMD_HSRSP_MINSZ 8 /* Minimum Compatible (1.x.x) packet size (bytes) */
110 #define SRT_CMD_HSRSP_SZ 12 /* Current version packet size */
111 #if SRT_CMD_HSRSP_SZ > SRT_CMD_MAXSZ
112 #error SRT_CMD_MAXSZ too small
113 #endif
114 /* Handshake Response (Network Order)
115 0[31..0]: SRT version SRT_DEF_VERSION
116 1[31..0]: Options 0 [ | SRT_OPT_TSBPDRCV [| SRT_OPT_TLPKTDROP ]][ | SRT_OPT_HAICRYPT]
117 [ | SRT_OPT_NAKREPORT ] [ | SRT_OPT_REXMITFLG ]
118 2[31..16]: TsbPD resv 0
119 2[15..0]: TsbPD delay [0..60000] msec
120 */
121
122 // IMPORTANT!!! This array must be ordered by value, because std::binary_search is performed on it!
123 extern const SRT_SOCKOPT srt_post_opt_list [SRT_SOCKOPT_NPOST] = {
124 SRTO_SNDSYN,
125 SRTO_RCVSYN,
126 SRTO_LINGER,
127 SRTO_SNDTIMEO,
128 SRTO_RCVTIMEO,
129 SRTO_MAXBW,
130 SRTO_INPUTBW,
131 SRTO_OHEADBW,
132 SRTO_SNDDROPDELAY,
133 SRTO_DRIFTTRACER,
134 SRTO_MININPUTBW,
135 SRTO_LOSSMAXTTL
136 #ifdef ENABLE_MAXREXMITBW
137 ,SRTO_MAXREXMITBW
138 #endif
139 };
140
141 const int32_t
142 SRTO_R_PREBIND = BIT(0), //< cannot be modified after srt_bind()
143 SRTO_R_PRE = BIT(1), //< cannot be modified after connection is established
144 SRTO_POST_SPEC = BIT(2); //< executes some action after setting the option
145
146
147 namespace srt
148 {
149
150 struct SrtOptionAction
151 {
152 int flags[SRTO_E_SIZE];
153 std::map<SRT_SOCKOPT, std::string> private_default;
154 SrtOptionAction()
155 {
156 // Set everything to 0 to clear all flags
157 // When an option isn't present here, it means that:
158 // * it is not settable, or
159 // * the option is POST (non-restricted)
160 // * it has no post-actions
161 // The post-action may be defined independently on restrictions.
162 memset(flags, 0, sizeof flags);
163
164 flags[SRTO_MSS] = SRTO_R_PREBIND;
165 flags[SRTO_FC] = SRTO_R_PRE;
166 flags[SRTO_SNDBUF] = SRTO_R_PREBIND;
167 flags[SRTO_RCVBUF] = SRTO_R_PREBIND;
168 flags[SRTO_UDP_SNDBUF] = SRTO_R_PREBIND;
169 flags[SRTO_UDP_RCVBUF] = SRTO_R_PREBIND;
170 flags[SRTO_RENDEZVOUS] = SRTO_R_PRE;
171 flags[SRTO_REUSEADDR] = SRTO_R_PREBIND;
172 flags[SRTO_MAXBW] = SRTO_POST_SPEC;
173 flags[SRTO_SENDER] = SRTO_R_PRE;
174 flags[SRTO_TSBPDMODE] = SRTO_R_PRE;
175 flags[SRTO_LATENCY] = SRTO_R_PRE;
176 flags[SRTO_INPUTBW] = SRTO_POST_SPEC;
177 flags[SRTO_MININPUTBW] = SRTO_POST_SPEC;
178 flags[SRTO_OHEADBW] = SRTO_POST_SPEC;
179 flags[SRTO_PASSPHRASE] = SRTO_R_PRE;
180 flags[SRTO_PBKEYLEN] = SRTO_R_PRE;
181 flags[SRTO_IPTTL] = SRTO_R_PREBIND;
182 flags[SRTO_IPTOS] = SRTO_R_PREBIND;
183 flags[SRTO_TLPKTDROP] = SRTO_R_PRE;
184 flags[SRTO_SNDDROPDELAY] = SRTO_POST_SPEC;
185 flags[SRTO_NAKREPORT] = SRTO_R_PRE;
186 flags[SRTO_VERSION] = SRTO_R_PRE;
187 flags[SRTO_CONNTIMEO] = SRTO_R_PRE;
188 flags[SRTO_LOSSMAXTTL] = SRTO_POST_SPEC;
189 flags[SRTO_RCVLATENCY] = SRTO_R_PRE;
190 flags[SRTO_PEERLATENCY] = SRTO_R_PRE;
191 flags[SRTO_MINVERSION] = SRTO_R_PRE;
192 flags[SRTO_STREAMID] = SRTO_R_PRE;
193 flags[SRTO_CONGESTION] = SRTO_R_PRE;
194 flags[SRTO_MESSAGEAPI] = SRTO_R_PRE;
195 flags[SRTO_PAYLOADSIZE] = SRTO_R_PRE;
196 flags[SRTO_TRANSTYPE] = SRTO_R_PREBIND;
197 flags[SRTO_KMREFRESHRATE] = SRTO_R_PRE;
198 flags[SRTO_KMPREANNOUNCE] = SRTO_R_PRE;
199 flags[SRTO_ENFORCEDENCRYPTION] = SRTO_R_PRE;
200 flags[SRTO_IPV6ONLY] = SRTO_R_PREBIND;
201 flags[SRTO_PEERIDLETIMEO] = SRTO_R_PRE;
202 #ifdef SRT_ENABLE_BINDTODEVICE
203 flags[SRTO_BINDTODEVICE] = SRTO_R_PREBIND;
204 #endif
205 #if ENABLE_BONDING
206 flags[SRTO_GROUPCONNECT] = SRTO_R_PRE;
207 flags[SRTO_GROUPMINSTABLETIMEO]= SRTO_R_PRE;
208 #endif
209 flags[SRTO_PACKETFILTER] = SRTO_R_PRE;
210 flags[SRTO_RETRANSMITALGO] = SRTO_R_PRE;
211 #ifdef ENABLE_AEAD_API_PREVIEW
212 flags[SRTO_CRYPTOMODE] = SRTO_R_PRE;
213 #endif
214
215 // For "private" options (not derived from the listener
216 // socket by an accepted socket) provide below private_default
217 // to which these options will be reset after blindly
218 // copying the option object from the listener socket.
219 // Note that this option cannot have runtime-dependent
220 // default value, like options affected by SRTO_TRANSTYPE.
221
222 // Options may be of different types, but this value should be only
223 // used as a source of the value. For example, in case of int64_t you'd
224 // have to place here a string of 8 characters. It should be copied
225 // always in the hardware order, as this is what will be directly
226 // passed to a setting function.
227 private_default[SRTO_STREAMID] = string();
228 }
229 };
230
231 const SrtOptionAction s_sockopt_action;
232
233 } // namespace srt
234
235 #if HAVE_CXX11
236
237 CUDTUnited& srt::CUDT::uglobal()
238 {
239 static CUDTUnited instance;
240 return instance;
241 }
242
243 #else // !HAVE_CXX11
244
245 static pthread_once_t s_UDTUnitedOnce = PTHREAD_ONCE_INIT;
246
247 static CUDTUnited *getInstance()
248 {
249 static CUDTUnited instance;
250 return &instance;
251 }
252
253 CUDTUnited& srt::CUDT::uglobal()
254 {
255 // We don't want lock each time, pthread_once can be faster than mutex.
256 pthread_once(&s_UDTUnitedOnce, reinterpret_cast<void (*)()>(getInstance));
257 return *getInstance();
258 }
259
260 #endif
261
262 #ifdef ENABLE_RATE_MEASUREMENT
263 void srt::RateMeasurement::pickup(const clock_time& time)
264 {
265 // Check if the time has passed the period.
266 clock_interval diff;
267 if (!passedInterval(time, (diff)))
268 {
269 HLOGC(bslog.Debug, log << "RateMeasurement: too early, " << FormatDuration<DUNIT_MS>(diff)
270 << " with begin TS=" << FormatTime(m_beginTime) << " now=" << FormatTime(time));
271 // Still collect data for the current period.
272 return;
273 }
274
275 // Doesn't matter what `diff` really is. Enough that we have
276 // this time aligned with 10ms resolution calls 10 times.
277
278 Slice slice;
279 {
280 // Lock for the moment of taking the data
281 sync::ScopedLock lk (m_lock);
282
283 slice.packets = m_nInstaPackets;
284 slice.bytes = m_nInstaBytes;
285 slice.begin_time = m_beginTime;
286
287 m_nInstaPackets = 0;
288 m_nInstaBytes = 0;
289 m_beginTime = time;
290 }
291
292 HLOGC(bslog.Debug, log << "RateMeasurement: passed " << FormatDuration<DUNIT_MS>(diff)
293 << " extracted packets=" << slice.packets << " bytes=" << slice.bytes
294 << " now beginTS=" << FormatTime(m_beginTime));
295
296 // Ok, now stickpile the slice.
297 // If the container is empty, do not stockpile empty slices.
298 if (m_slices.empty() && slice.bytes == 0)
299 {
300 HLOGC(bslog.Debug, log << "... NOT COLLECTING packet with " << slice.bytes << " bytes to " << m_slices.size() << " total");
301 // Empty-to-empty: DO NOT stockpile.
302 // NOTE: the container is never cleared when it has
303 // any older data, so this situation is only possible:
304 // - in the beginning
305 // - in the long run if it happened that 10 slices in
306 // a row were empty.
307 // So in this case we simply wait for having the first
308 // packets with something notified. We also know that
309 // the rate was initially 0 and it remains so now.
310 return;
311 }
312
313 // We either have something and have pushed an empty slice
314 // or we have pushed filled slice onto the empty container.
315 // Still, we need at least 5 samples to start measuring.
316 m_slices.push_back(slice);
317
318 // NOTE: this is also a situation that might have happened
319 // in the beginning, or after a long period without sending.
320 // If that period happens, the sending rate is also cleared,
321 // in any case we need to wait for the minimum samples.
322 if (m_slices.size() < MIN_SLICES)
323 {
324 HLOGC(bslog.Debug, log << "... NOT MEASURING: still " << m_slices.size() << " < 5 slices");
325 return;
326 }
327
328 // Keep track of only 10 last slices
329 if (m_slices.size() > MAX_SLICES)
330 {
331 m_slices.pop_front();
332
333 // After a slice was removed, there's already a chance
334 // that all other slices are also 0. Check the minimum
335 // number of slices + 1 and drop all that are empty
336 size_t end_range = 0; // default: nothing to delete
337 for (size_t i = 0; i < m_slices.size(); ++i)
338 {
339 if (i > MIN_SLICES + 1)
340 break;
341
342 if (m_slices[i].bytes > 0)
343 break;
344
345 // This can be skipped and check the next one.
346 end_range = i;
347 }
348
349 // PARANOID
350 if (end_range >= m_slices.size())
351 end_range = m_slices.size() - 1; // at least 1 element must stay
352
353 m_slices.erase(m_slices.begin(), m_slices.begin() + end_range);
354
355 m_beginTime = m_slices[0].begin_time;
356 HLOGC(bslog.Debug, log << "... Dropping 1 oldest + " << end_range << " empty slices, TS=" << FormatTime(m_beginTime));
357 }
358
359 // Check also if the begin time is older than 2* period; if so,
360 // reset it to that value
361 if (m_slices.size() > 1) // with 1 always m_beginTime = m_slices[0].begin_time
362 {
363 clock_time earliest = m_slices[1].begin_time - milliseconds_from(SLICE_INTERVAL_MS*2);
364 if (m_beginTime < earliest)
365 {
366 HLOGC(bslog.Debug, log << "... BEGIN TIME too old TS=" << FormatTime(m_beginTime)
367 << " - fixed to " << FormatTime(earliest));
368 m_beginTime = earliest;
369 m_slices[0].begin_time = earliest;
370 }
371 }
372
373 // Ok, so we have enough samples to record the measurement.
374 // (so we have at least one)
375 Summary sum (m_slices[0].begin_time);
376
377 for (size_t i = 0; i < m_slices.size(); ++i)
378 {
379 sum.consume(m_slices[i]);
380 }
381
382 m_currentPktRate = sum.ratePackets(time);
383 m_currentByteRate = sum.rateBytes(time, m_SysHeaderSize + CPacket::HDR_SIZE);
384
385 HLOGC(bslog.Debug, log << "... CALCULATED from " << m_slices.size() << " slices: total TD=" << FormatDuration(time - m_slices[0].begin_time)
386 << " packets=" << sum.packets << " bytes=" << sum.bytes << " pkt-rate=" << m_currentPktRate << " byte-rate=" << m_currentByteRate);
387 }
388 #endif
389
390 void srt::CUDT::construct()
391 {
392 m_pSndBuffer = NULL;
393 m_pRcvBuffer = NULL;
394 m_pSndLossList = NULL;
395 m_pRcvLossList = NULL;
396 m_iReorderTolerance = 0;
397 // How many times so far the packet considered lost has been received
398 // before TTL expires.
399 m_iConsecEarlyDelivery = 0;
400 m_iConsecOrderedDelivery = 0;
401
402 m_pSndQueue = NULL;
403 m_pRcvQueue = NULL;
404 m_pSNode = NULL;
405 m_pRNode = NULL;
406
407 // Will be reset to 0 for HSv5, this value is important for HSv4.
408 m_iSndHsRetryCnt = SRT_MAX_HSRETRY + 1;
409
410 m_PeerID = 0;
411 m_bOpened = false;
412 m_bListening = false;
413 m_bConnecting = false;
414 m_bConnected = false;
415 m_bClosing = false;
416 m_bShutdown = false;
417 m_bBreaking = false;
418 m_bBroken = false;
419 m_bBreakAsUnstable = false;
420 // TODO: m_iBrokenCounter should be still set to some default.
421 m_bPeerHealth = true;
422 m_RejectReason = SRT_REJ_UNKNOWN;
423 m_tsLastReqTime.store(steady_clock::time_point());
424 m_SrtHsSide = HSD_DRAW;
425 m_uPeerSrtVersion = 0; // Not defined until connected.
426 m_iTsbPdDelay_ms = 0;
427 m_iPeerTsbPdDelay_ms = 0;
428 m_bPeerTsbPd = false;
429 m_bTsbPd = false;
430 m_bTsbPdNeedsWakeup = false;
431 m_bGroupTsbPd = false;
432 m_bPeerTLPktDrop = false;
433 m_bBufferWasFull = false;
434
435 // Will be updated on first send
436 #ifdef ENABLE_MAXREXMITBW
437 m_zSndAveragePacketSize = 0;
438 m_zSndMaxPacketSize = 0;
439 #endif
440
441 // Initialize mutex and condition variables.
442 initSynch();
443
444 // TODO: Uncomment when the callback is implemented.
445 // m_cbPacketArrival.set(this, &CUDT::defaultPacketArrival);
446 }
447
448 srt::CUDT::CUDT(CUDTSocket* parent)
449 : m_parent(parent)
450 #ifdef ENABLE_MAXREXMITBW
451 // , m_SndRexmitRate(sync::steady_clock::now())
452 , m_SndRexmitShaper(CSrtConfig::DEF_MSS)
453 #endif
454 , m_iISN(-1)
455 , m_iPeerISN(-1)
456 {
457 construct();
458
459 (void)SRT_DEF_VERSION;
460
461 // Runtime fields
462 #if ENABLE_BONDING
463 m_HSGroupType = SRT_GTYPE_UNDEFINED;
464 #endif
465 m_bTLPktDrop = true; // Too-late Packet Drop
466
467 m_pCache = NULL;
468 // This is in order to set it ANY kind of initial value, however
469 // this value should not be used when not connected and should be
470 // updated in the handshake. When this value is 0, it means that
471 // packets shall not be sent, as the other party doesn't have a
472 // room to receive and store it. Therefore this value should be
473 // overridden before any sending happens.
474 m_iFlowWindowSize = 0;
475
476 }
477
478 srt::CUDT::CUDT(CUDTSocket* parent, const CUDT& ancestor)
479 : m_parent(parent)
480 #ifdef ENABLE_MAXREXMITBW
481 // , m_SndRexmitRate(sync::steady_clock::now())
482 , m_SndRexmitShaper(CSrtConfig::DEF_MSS)
483 #endif
484 , m_iISN(-1)
485 , m_iPeerISN(-1)
486 {
487 construct();
488
489 // XXX Consider all below fields (except m_bReuseAddr) to be put
490 // into a separate class for easier copying.
491
492 m_config = ancestor.m_config;
493 // Reset values that shall not be derived to default ones.
494 // These declarations should be consistent with SRTO_R_PRIVATE flag.
495 for (size_t i = 0; i < Size(s_sockopt_action.flags); ++i)
496 {
497 const string* pdef = map_getp(s_sockopt_action.private_default, SRT_SOCKOPT(i));
498 if (pdef)
499 {
500 try
501 {
502 // Ignore errors here - this is a development-time granted
503 // value, not user-provided value.
504 m_config.set(SRT_SOCKOPT(i), pdef->data(), (int) pdef->size());
505 }
506 catch (...)
507 {
508 LOGC(gglog.Error, log << "IPE: failed to set a declared default option!");
509 }
510 }
511 }
512
513 m_SrtHsSide = ancestor.m_SrtHsSide; // actually it sets it to HSD_RESPONDER
514 m_bTLPktDrop = ancestor.m_bTLPktDrop;
515 m_iReorderTolerance = m_config.iMaxReorderTolerance; // Initialize with maximum value
516
517 // Runtime
518 m_pCache = ancestor.m_pCache;
519 }
520
521 srt::CUDT::~CUDT()
522 {
523 // release mutex/condition variables
524 destroySynch();
525
526 // destroy the data structures
527 delete m_pSndBuffer;
528 delete m_pRcvBuffer;
529 delete m_pSndLossList;
530 delete m_pRcvLossList;
531 delete m_pSNode;
532 delete m_pRNode;
533 }
534
535 void srt::CUDT::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
536 {
537 if (m_bBroken || m_bClosing)
538 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
539
540 // Match check (confirm optName as index for s_sockopt_action)
541 if (int(optName) < 0 || int(optName) >= int(SRTO_E_SIZE))
542 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
543
544 // Restriction check
545 const int oflags = s_sockopt_action.flags[optName];
546
547 ScopedLock cg (m_ConnectionLock);
548 ScopedLock sendguard (m_SendLock);
549 ScopedLock recvguard (m_RecvLock);
550
551 HLOGC(aclog.Debug,
552 log << CONID() << "OPTION: #" << optName << " value:" << FormatBinaryString((uint8_t*)optval, optlen));
553
554 if (IsSet(oflags, SRTO_R_PREBIND) && m_bOpened)
555 throw CUDTException(MJ_NOTSUP, MN_ISBOUND, 0);
556
557 if (IsSet(oflags, SRTO_R_PRE) && (m_bConnected || m_bConnecting || m_bListening))
558 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
559
560 // Option execution. If this returns -1, there's no such option.
561 const int status = m_config.set(optName, optval, optlen);
562 if (status == -1)
563 {
564 LOGC(aclog.Error, log << CONID() << "OPTION: #" << optName << " UNKNOWN");
565 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
566 }
567
568 // Post-action, if applicable
569 if (IsSet(oflags, SRTO_POST_SPEC) && m_bConnected)
570 {
571 switch (optName)
572 {
573 case SRTO_MAXBW:
574 updateCC(TEV_INIT, EventVariant(TEV_INIT_RESET));
575 break;
576
577 case SRTO_INPUTBW:
578 case SRTO_MININPUTBW:
579 updateCC(TEV_INIT, EventVariant(TEV_INIT_INPUTBW));
580 break;
581
582 case SRTO_OHEADBW:
583 updateCC(TEV_INIT, EventVariant(TEV_INIT_OHEADBW));
584 break;
585
586 case SRTO_LOSSMAXTTL:
587 m_iReorderTolerance = m_config.iMaxReorderTolerance;
588
589 default: break;
590 }
591 }
592 }
593
594 void srt::CUDT::getOpt(SRT_SOCKOPT optName, void *optval, int &optlen)
595 {
596 ScopedLock cg(m_ConnectionLock);
597
598 switch (optName)
599 {
600 case SRTO_MSS:
601 *(int *)optval = m_config.iMSS;
602 optlen = sizeof(int);
603 break;
604
605 case SRTO_SNDSYN:
606 *(bool *)optval = m_config.bSynSending;
607 optlen = sizeof(bool);
608 break;
609
610 case SRTO_RCVSYN:
611 *(bool *)optval = m_config.bSynRecving;
612 optlen = sizeof(bool);
613 break;
614
615 case SRTO_ISN:
616 *(int *)optval = m_iISN;
617 optlen = sizeof(int);
618 break;
619
620 case SRTO_FC:
621 *(int *)optval = m_config.iFlightFlagSize;
622 optlen = sizeof(int);
623 break;
624
625 // For SNDBUF/RCVBUF values take the variant that uses more memory.
626 // It is not possible to make sure what "family" is in use without
627 // checking if the socket is bound. This will also be the exact size
628 // of the memory in use.
629 case SRTO_SNDBUF:
630 *(int *)optval = m_config.iSndBufSize * m_config.bytesPerPkt();
631 optlen = sizeof(int);
632 break;
633
634 case SRTO_RCVBUF:
635 *(int *)optval = m_config.iRcvBufSize * m_config.bytesPerPkt();
636 optlen = sizeof(int);
637 break;
638
639 case SRTO_LINGER:
640 if (optlen < (int)(sizeof(linger)))
641 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
642
643 *(linger *)optval = m_config.Linger;
644 optlen = sizeof(linger);
645 break;
646
647 case SRTO_UDP_SNDBUF:
648 *(int *)optval = m_config.iUDPSndBufSize;
649 optlen = sizeof(int);
650 break;
651
652 case SRTO_UDP_RCVBUF:
653 *(int *)optval = m_config.iUDPRcvBufSize;
654 optlen = sizeof(int);
655 break;
656
657 case SRTO_RENDEZVOUS:
658 *(bool *)optval = m_config.bRendezvous;
659 optlen = sizeof(bool);
660 break;
661
662 case SRTO_SNDTIMEO:
663 *(int *)optval = m_config.iSndTimeOut;
664 optlen = sizeof(int);
665 break;
666
667 case SRTO_RCVTIMEO:
668 *(int *)optval = m_config.iRcvTimeOut;
669 optlen = sizeof(int);
670 break;
671
672 case SRTO_REUSEADDR:
673 *(bool *)optval = m_config.bReuseAddr;
674 optlen = sizeof(bool);
675 break;
676
677 case SRTO_MAXBW:
678 if (size_t(optlen) < sizeof(m_config.llMaxBW))
679 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
680 *(int64_t *)optval = m_config.llMaxBW;
681 optlen = sizeof(int64_t);
682 break;
683
684 case SRTO_INPUTBW:
685 if (size_t(optlen) < sizeof(m_config.llInputBW))
686 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
687 *(int64_t*)optval = m_config.llInputBW;
688 optlen = sizeof(int64_t);
689 break;
690
691 case SRTO_MININPUTBW:
692 if (size_t(optlen) < sizeof (m_config.llMinInputBW))
693 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
694 *(int64_t*)optval = m_config.llMinInputBW;
695 optlen = sizeof(int64_t);
696 break;
697
698 case SRTO_OHEADBW:
699 *(int32_t *)optval = m_config.iOverheadBW;
700 optlen = sizeof(int32_t);
701 break;
702
703 #ifdef ENABLE_MAXREXMITBW
704 case SRTO_MAXREXMITBW:
705 if (size_t(optlen) < sizeof(m_config.llMaxRexmitBW))
706 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
707 *(int64_t*)optval = m_config.llMaxRexmitBW;
708 optlen = sizeof(int64_t);
709 break;
710 #endif
711
712 case SRTO_STATE:
713 *(int32_t *)optval = uglobal().getStatus(m_SocketID);
714 optlen = sizeof(int32_t);
715 break;
716
717 case SRTO_EVENT:
718 {
719 int32_t event = 0;
720 if (m_bBroken)
721 event |= SRT_EPOLL_ERR;
722 else
723 {
724 enterCS(m_RecvLock);
725 if (m_pRcvBuffer && isRcvBufferReady())
726 event |= SRT_EPOLL_IN;
727 leaveCS(m_RecvLock);
728 if (m_pSndBuffer && (m_config.iSndBufSize > m_pSndBuffer->getCurrBufSize()))
729 event |= SRT_EPOLL_OUT;
730 }
731 *(int32_t *)optval = event;
732 optlen = sizeof(int32_t);
733 break;
734 }
735
736 case SRTO_SNDDATA:
737 if (m_pSndBuffer)
738 *(int32_t *)optval = m_pSndBuffer->getCurrBufSize();
739 else
740 *(int32_t *)optval = 0;
741 optlen = sizeof(int32_t);
742 break;
743
744 case SRTO_RCVDATA:
745 if (m_pRcvBuffer)
746 {
747 enterCS(m_RecvLock);
748 *(int32_t *)optval = m_pRcvBuffer->getRcvDataSize();
749 leaveCS(m_RecvLock);
750 }
751 else
752 *(int32_t *)optval = 0;
753 optlen = sizeof(int32_t);
754 break;
755
756 case SRTO_IPTTL:
757 if (m_bOpened)
758 *(int32_t *)optval = m_pSndQueue->getIpTTL();
759 else
760 *(int32_t *)optval = m_config.iIpTTL;
761 optlen = sizeof(int32_t);
762 break;
763
764 case SRTO_IPTOS:
765 if (m_bOpened)
766 *(int32_t *)optval = m_pSndQueue->getIpToS();
767 else
768 *(int32_t *)optval = m_config.iIpToS;
769 optlen = sizeof(int32_t);
770 break;
771
772 case SRTO_BINDTODEVICE:
773 #ifdef SRT_ENABLE_BINDTODEVICE
774 if (optlen < IFNAMSIZ)
775 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
776
777 if (m_bOpened && m_pSndQueue->getBind(((char*)optval), optlen))
778 {
779 optlen = strlen((char*)optval);
780 break;
781 }
782
783 // Fallback: return from internal data
784 optlen = (int)m_config.sBindToDevice.copy((char*)optval, (size_t)optlen - 1);
785 ((char*)optval)[optlen] = '\0';
786 #else
787 LOGC(smlog.Error, log << "SRTO_BINDTODEVICE is not supported on that platform");
788 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
789 #endif
790 break;
791
792 case SRTO_SENDER:
793 *(bool *)optval = m_config.bDataSender;
794 optlen = sizeof(bool);
795 break;
796
797 case SRTO_TSBPDMODE:
798 *(bool *)optval = m_config.bTSBPD;
799 optlen = sizeof(bool);
800 break;
801
802 case SRTO_LATENCY:
803 case SRTO_RCVLATENCY:
804 if (m_bConnected)
805 *(int32_t *)optval = m_iTsbPdDelay_ms;
806 else
807 *(int32_t *)optval = m_config.iRcvLatency;
808 optlen = sizeof(int32_t);
809 break;
810
811 case SRTO_PEERLATENCY:
812 if (m_bConnected)
813 *(int32_t *)optval = m_iPeerTsbPdDelay_ms;
814 else
815 *(int32_t *)optval = m_config.iPeerLatency;
816
817 optlen = sizeof(int32_t);
818 break;
819
820 case SRTO_TLPKTDROP:
821 if (m_bConnected)
822 *(bool *)optval = m_bTLPktDrop;
823 else
824 *(bool *)optval = m_config.bTLPktDrop;
825
826 optlen = sizeof(bool);
827 break;
828
829 case SRTO_SNDDROPDELAY:
830 *(int32_t *)optval = m_config.iSndDropDelay;
831 optlen = sizeof(int32_t);
832 break;
833
834 case SRTO_PBKEYLEN:
835 if (m_pCryptoControl)
836 *(int32_t *)optval = (int32_t) m_pCryptoControl->KeyLen(); // Running Key length.
837 else
838 *(int32_t *)optval = m_config.iSndCryptoKeyLen; // May be 0.
839 optlen = sizeof(int32_t);
840 break;
841
842 case SRTO_KMSTATE:
843 if (!m_pCryptoControl)
844 *(int32_t *)optval = SRT_KM_S_UNSECURED;
845 else if (m_config.bDataSender)
846 *(int32_t *)optval = m_pCryptoControl->m_SndKmState;
847 else
848 *(int32_t *)optval = m_pCryptoControl->m_RcvKmState;
849 optlen = sizeof(int32_t);
850 break;
851
852 case SRTO_SNDKMSTATE: // State imposed by Agent depending on PW and KMX
853 if (m_pCryptoControl)
854 *(int32_t *)optval = m_pCryptoControl->m_SndKmState;
855 else
856 *(int32_t *)optval = SRT_KM_S_UNSECURED;
857 optlen = sizeof(int32_t);
858 break;
859
860 case SRTO_RCVKMSTATE: // State returned by Peer as informed during KMX
861 if (m_pCryptoControl)
862 *(int32_t *)optval = m_pCryptoControl->m_RcvKmState;
863 else
864 *(int32_t *)optval = SRT_KM_S_UNSECURED;
865 optlen = sizeof(int32_t);
866 break;
867
868 case SRTO_LOSSMAXTTL:
869 *(int32_t*)optval = m_config.iMaxReorderTolerance;
870 optlen = sizeof(int32_t);
871 break;
872
873 case SRTO_NAKREPORT:
874 *(bool *)optval = m_config.bRcvNakReport;
875 optlen = sizeof(bool);
876 break;
877
878 case SRTO_VERSION:
879 *(int32_t *)optval = m_config.uSrtVersion;
880 optlen = sizeof(int32_t);
881 break;
882
883 case SRTO_PEERVERSION:
884 *(int32_t *)optval = m_uPeerSrtVersion;
885 optlen = sizeof(int32_t);
886 break;
887
888 case SRTO_CONNTIMEO:
889 *(int*)optval = (int) count_milliseconds(m_config.tdConnTimeOut);
890 optlen = sizeof(int);
891 break;
892
893 case SRTO_DRIFTTRACER:
894 *(bool*)optval = m_config.bDriftTracer;
895 optlen = sizeof(bool);
896 break;
897
898 case SRTO_MINVERSION:
899 *(uint32_t *)optval = m_config.uMinimumPeerSrtVersion;
900 optlen = sizeof(uint32_t);
901 break;
902
903 case SRTO_STREAMID:
904 if (size_t(optlen) < m_config.sStreamName.size() + 1)
905 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
906
907 optlen = (int)m_config.sStreamName.copy((char*)optval, (size_t)optlen - 1);
908 ((char*)optval)[optlen] = '\0';
909 break;
910
911 case SRTO_CONGESTION:
912 if (size_t(optlen) < m_config.sCongestion.size() + 1)
913 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
914
915 optlen = (int)m_config.sCongestion.copy((char*)optval, (size_t)optlen - 1);
916 ((char*)optval)[optlen] = '\0';
917 break;
918
919 case SRTO_MESSAGEAPI:
920 optlen = sizeof(bool);
921 *(bool *)optval = m_config.bMessageAPI;
922 break;
923
924 case SRTO_PAYLOADSIZE:
925 optlen = sizeof(int);
926 *(int *)optval = (int) payloadSize();
927 break;
928
929 case SRTO_KMREFRESHRATE:
930 optlen = sizeof(int);
931 *(int*)optval = (int)m_config.uKmRefreshRatePkt;
932 break;
933
934 case SRTO_KMPREANNOUNCE:
935 optlen = sizeof(int);
936 *(int*)optval = (int)m_config.uKmPreAnnouncePkt;
937 break;
938
939 #if ENABLE_BONDING
940 case SRTO_GROUPCONNECT:
941 optlen = sizeof (int);
942 *(int*)optval = m_config.iGroupConnect;
943 break;
944
945 case SRTO_GROUPMINSTABLETIMEO:
946 optlen = sizeof(int);
947 *(int*)optval = (int)m_config.uMinStabilityTimeout_ms;
948 break;
949
950 case SRTO_GROUPTYPE:
951 optlen = sizeof (int);
952 *(int*)optval = m_HSGroupType;
953 break;
954 #endif
955
956 case SRTO_ENFORCEDENCRYPTION:
957 optlen = sizeof(bool);
958 *(bool *)optval = m_config.bEnforcedEnc;
959 break;
960
961 case SRTO_IPV6ONLY:
962 optlen = sizeof(int);
963 *(int *)optval = m_config.iIpV6Only;
964 break;
965
966 case SRTO_PEERIDLETIMEO:
967 *(int *)optval = m_config.iPeerIdleTimeout_ms;
968 optlen = sizeof(int);
969 break;
970
971 case SRTO_PACKETFILTER:
972 if (size_t(optlen) < m_config.sPacketFilterConfig.size() + 1)
973 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
974
975 optlen = (int)m_config.sPacketFilterConfig.copy((char*)optval, (size_t)optlen - 1);
976 ((char*)optval)[optlen] = '\0';
977 break;
978
979 case SRTO_RETRANSMITALGO:
980 *(int32_t *)optval = m_config.iRetransmitAlgo;
981 optlen = sizeof(int32_t);
982 break;
983 #ifdef ENABLE_AEAD_API_PREVIEW
984 case SRTO_CRYPTOMODE:
985 if (m_pCryptoControl)
986 *(int32_t*)optval = m_pCryptoControl->getCryptoMode();
987 else
988 *(int32_t*)optval = m_config.iCryptoMode;
989 optlen = sizeof(int32_t);
990 break;
991 #endif
992
993 default:
994 throw CUDTException(MJ_NOTSUP, MN_NONE, 0);
995 }
996 }
997
998
999 #if ENABLE_BONDING
1000 SRT_ERRNO srt::CUDT::applyMemberConfigObject(const SRT_SocketOptionObject& opt)
1001 {
1002 SRT_SOCKOPT this_opt = SRTO_VERSION;
1003 for (size_t i = 0; i < opt.options.size(); ++i)
1004 {
1005 SRT_SocketOptionObject::SingleOption* o = opt.options[i];
1006 HLOGC(smlog.Debug, log << CONID() << "applyMemberConfigObject: OPTION @" << m_SocketID << " #" << o->option);
1007 this_opt = SRT_SOCKOPT(o->option);
1008 setOpt(this_opt, o->storage, o->length);
1009 }
1010 return SRT_SUCCESS;
1011 }
1012 #endif
1013
1014 bool srt::CUDT::setstreamid(SRTSOCKET u, const std::string &sid)
1015 {
1016 CUDT *that = getUDTHandle(u);
1017 if (!that)
1018 return false;
1019
1020 if (sid.size() > CSrtConfig::MAX_SID_LENGTH)
1021 return false;
1022
1023 if (that->m_bConnected)
1024 return false;
1025
1026 that->m_config.sStreamName.set(sid);
1027 return true;
1028 }
1029
1030 string srt::CUDT::getstreamid(SRTSOCKET u)
1031 {
1032 CUDT *that = getUDTHandle(u);
1033 if (!that)
1034 return "";
1035
1036 return that->m_config.sStreamName.str();
1037 }
1038
1039 // XXX REFACTOR: Make common code for CUDT constructor and clearData,
1040 // possibly using CUDT::construct.
1041 // Initial sequence number, loss, acknowledgement, etc.
1042 void srt::CUDT::clearData()
1043 {
1044 const size_t full_hdr_size = CPacket::UDP_HDR_SIZE - CPacket::HDR_SIZE;
1045 m_iMaxSRTPayloadSize = m_config.iMSS - full_hdr_size;
1046 HLOGC(cnlog.Debug, log << CONID() << "clearData: PAYLOAD SIZE: " << m_iMaxSRTPayloadSize);
1047
1048 m_SndTimeWindow.initialize(full_hdr_size, m_iMaxSRTPayloadSize);
1049 m_RcvTimeWindow.initialize(full_hdr_size, m_iMaxSRTPayloadSize);
1050
1051 m_iEXPCount = 1;
1052 m_iBandwidth = 1; // pkts/sec
1053 // XXX use some constant for this 16
1054 m_iDeliveryRate = 16;
1055 m_iByteDeliveryRate = 16 * m_iMaxSRTPayloadSize;
1056 m_iAckSeqNo = 0;
1057 m_tsLastAckTime = steady_clock::now();
1058
1059 // trace information
1060 {
1061 ScopedLock stat_lock(m_StatsLock);
1062
1063 m_stats.tsStartTime = steady_clock::now();
1064 m_stats.sndr.reset();
1065 m_stats.rcvr.reset();
1066
1067 m_stats.tsLastSampleTime = steady_clock::now();
1068 m_stats.traceReorderDistance = 0;
1069 m_stats.traceBelatedTime = 0;
1070 m_stats.sndDuration = m_stats.m_sndDurationTotal = 0;
1071 }
1072
1073 // Resetting these data because this happens when agent isn't connected.
1074 m_bPeerTsbPd = false;
1075 m_iPeerTsbPdDelay_ms = 0;
1076
1077 // TSBPD as state should be set to FALSE here.
1078 // Only when the HSREQ handshake is exchanged,
1079 // should they be set to possibly true.
1080 m_bTsbPd = false;
1081 m_bGroupTsbPd = false;
1082 m_iTsbPdDelay_ms = m_config.iRcvLatency;
1083 m_bTLPktDrop = m_config.bTLPktDrop;
1084 m_bPeerTLPktDrop = false;
1085
1086 m_bPeerNakReport = false;
1087
1088 m_bPeerRexmitFlag = false;
1089
1090 m_RdvState = CHandShake::RDV_INVALID;
1091 m_tsRcvPeerStartTime = steady_clock::time_point();
1092 }
1093
1094 void srt::CUDT::open()
1095 {
1096 ScopedLock cg(m_ConnectionLock);
1097
1098 clearData();
1099
1100 // structures for queue
1101 if (m_pSNode == NULL)
1102 m_pSNode = new CSNode;
1103 m_pSNode->m_pUDT = this;
1104 m_pSNode->m_tsTimeStamp = steady_clock::now();
1105 m_pSNode->m_iHeapLoc = -1;
1106
1107 if (m_pRNode == NULL)
1108 m_pRNode = new CRNode;
1109 m_pRNode->m_pUDT = this;
1110 m_pRNode->m_tsTimeStamp = steady_clock::now();
1111 m_pRNode->m_pPrev = m_pRNode->m_pNext = NULL;
1112 m_pRNode->m_bOnList = false;
1113
1114 // Set initial values of smoothed RTT and RTT variance.
1115 m_iSRTT = INITIAL_RTT;
1116 m_iRTTVar = INITIAL_RTTVAR;
1117 m_bIsFirstRTTReceived = false;
1118
1119 // set minimum NAK and EXP timeout to 300ms
1120 m_tdMinNakInterval = milliseconds_from(300);
1121 m_tdMinExpInterval = milliseconds_from(300);
1122
1123 m_tdACKInterval = microseconds_from(COMM_SYN_INTERVAL_US);
1124 m_tdNAKInterval = m_tdMinNakInterval;
1125
1126 const steady_clock::time_point currtime = steady_clock::now();
1127 m_tsLastRspTime.store(currtime);
1128 m_tsNextACKTime.store(currtime + m_tdACKInterval);
1129 m_tsNextNAKTime.store(currtime + m_tdNAKInterval);
1130 m_tsLastRspAckTime = currtime;
1131 m_tsLastSndTime.store(currtime);
1132
1133 #if ENABLE_BONDING
1134 m_tsUnstableSince = steady_clock::time_point();
1135 m_tsFreshActivation = steady_clock::time_point();
1136 m_tsWarySince = steady_clock::time_point();
1137 #endif
1138
1139 m_iReXmitCount = 1;
1140 memset(&m_aSuppressedMsg, 0, sizeof m_aSuppressedMsg);
1141 m_iPktCount = 0;
1142 m_iLightACKCount = 1;
1143 m_tsNextSendTime = steady_clock::time_point();
1144 m_tdSendTimeDiff = microseconds_from(0);
1145
1146 // Now UDT is opened.
1147 m_bOpened = true;
1148 }
1149
1150 void srt::CUDT::setListenState()
1151 {
1152 if (!m_bOpened)
1153 throw CUDTException(MJ_NOTSUP, MN_NONE, 0);
1154
1155 if (m_bConnecting || m_bConnected)
1156 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
1157
1158 // listen can be called more than once
1159 // If two threads call srt_listen at the same time, only
1160 // one will pass this condition; others will be rejected.
1161 // If it was called ever once, none will pass.
1162 for (;;)
1163 {
1164 if (m_bListening.compare_exchange(false, true))
1165 {
1166 // if there is already another socket listening on the same port
1167 if (!m_pRcvQueue->setListener(this))
1168 {
1169 // Failed here, so
1170 m_bListening = false;
1171 throw CUDTException(MJ_NOTSUP, MN_BUSY, 0);
1172 }
1173 }
1174 else
1175 {
1176 // Ok, this thread could have been blocked access,
1177 // but still the other thread that attempted to set
1178 // the listener could have failed. Therefore check
1179 // again if the listener was set successfully, and
1180 // if the listening point is still free, try again.
1181 CUDT* current = m_pRcvQueue->getListener();
1182 if (current == NULL)
1183 {
1184 continue;
1185 }
1186 else if (current != this)
1187 {
1188 // Some other listener already set it
1189 throw CUDTException(MJ_NOTSUP, MN_BUSY, 0);
1190 }
1191 // If it was you who set this, just return with no exception.
1192 }
1193 break;
1194 }
1195 }
1196
1197 size_t srt::CUDT::fillSrtHandshake(uint32_t *aw_srtdata, size_t srtlen, int msgtype, int hs_version)
1198 {
1199 if (srtlen < SRT_HS_E_SIZE)
1200 {
1201 LOGC(cnlog.Fatal,
1202 log << CONID() << "IPE: fillSrtHandshake: buffer too small: " << srtlen << " (expected: " << SRT_HS_E_SIZE << ")");
1203 return 0;
1204 }
1205
1206 srtlen = SRT_HS_E_SIZE; // We use only that much space.
1207
1208 memset((aw_srtdata), 0, sizeof(uint32_t) * srtlen);
1209 /* Current version (1.x.x) SRT handshake */
1210 aw_srtdata[SRT_HS_VERSION] = m_config.uSrtVersion; /* Required version */
1211 aw_srtdata[SRT_HS_FLAGS] |= SrtVersionCapabilities();
1212
1213 switch (msgtype)
1214 {
1215 case SRT_CMD_HSREQ:
1216 return fillSrtHandshake_HSREQ((aw_srtdata), srtlen, hs_version);
1217 case SRT_CMD_HSRSP:
1218 return fillSrtHandshake_HSRSP((aw_srtdata), srtlen, hs_version);
1219 default:
1220 LOGC(cnlog.Fatal, log << CONID() << "IPE: fillSrtHandshake/sendSrtMsg called with value " << msgtype);
1221 return 0;
1222 }
1223 }
1224
1225 size_t srt::CUDT::fillSrtHandshake_HSREQ(uint32_t *aw_srtdata, size_t /* srtlen - unused */, int hs_version)
1226 {
1227 // INITIATOR sends HSREQ.
1228
1229 // The TSBPD(SND|RCV) options are being set only if the TSBPD is set in the current agent.
1230 // The agent has a decisive power only in the range of RECEIVING the data, however it can
1231 // also influence the peer's latency. If agent doesn't set TSBPD mode, it doesn't send any
1232 // latency flags, although the peer might still want to do Rx with TSBPD. When agent sets
1233 // TsbPd mode, it defines latency values for Rx (itself) and Tx (peer's Rx). If peer does
1234 // not set TsbPd mode, it will simply ignore the proposed latency (PeerTsbPdDelay), although
1235 // if it has received the Rx latency as well, it must honor it and respond accordingly
1236 // (the latter is only in case of HSv5 and bidirectional connection).
1237 if (m_config.bTSBPD)
1238 {
1239 m_iTsbPdDelay_ms = m_config.iRcvLatency;
1240 m_iPeerTsbPdDelay_ms = m_config.iPeerLatency;
1241 /*
1242 * Sent data is real-time, use Time-based Packet Delivery,
1243 * set option bit and configured delay
1244 */
1245 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TSBPDSND;
1246
1247 if (hs_version < CUDT::HS_VERSION_SRT1)
1248 {
1249 // HSv4 - this uses only one value.
1250 aw_srtdata[SRT_HS_LATENCY] = SRT_HS_LATENCY_LEG::wrap(m_iPeerTsbPdDelay_ms);
1251 }
1252 else
1253 {
1254 // HSv5 - this will be understood only since this version when this exists.
1255 aw_srtdata[SRT_HS_LATENCY] = SRT_HS_LATENCY_SND::wrap(m_iPeerTsbPdDelay_ms);
1256
1257 // And in the reverse direction.
1258 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TSBPDRCV;
1259 aw_srtdata[SRT_HS_LATENCY] |= SRT_HS_LATENCY_RCV::wrap(m_iTsbPdDelay_ms);
1260
1261 // This wasn't there for HSv4, this setting is only for the receiver.
1262 // HSv5 is bidirectional, so every party is a receiver.
1263
1264 if (m_bTLPktDrop)
1265 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TLPKTDROP;
1266 }
1267 }
1268
1269 if (m_config.bRcvNakReport)
1270 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_NAKREPORT;
1271
1272 // I support SRT_OPT_REXMITFLG. Do you?
1273 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_REXMITFLG;
1274
1275 // Declare the API used. The flag is set for "stream" API because
1276 // the older versions will never set this flag, but all old SRT versions use message API.
1277 if (!m_config.bMessageAPI)
1278 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_STREAM;
1279
1280 HLOGC(cnlog.Debug,
1281 log << CONID() << "HSREQ/snd: LATENCY[SND:" << SRT_HS_LATENCY_SND::unwrap(aw_srtdata[SRT_HS_LATENCY])
1282 << " RCV:" << SRT_HS_LATENCY_RCV::unwrap(aw_srtdata[SRT_HS_LATENCY]) << "] FLAGS["
1283 << SrtFlagString(aw_srtdata[SRT_HS_FLAGS]) << "]");
1284
1285 return 3;
1286 }
1287
1288 size_t srt::CUDT::fillSrtHandshake_HSRSP(uint32_t *aw_srtdata, size_t /* srtlen - unused */, int hs_version)
1289 {
1290 // Setting m_tsRcvPeerStartTime is done in processSrtMsg_HSREQ(), so
1291 // this condition will be skipped only if this function is called without
1292 // getting first received HSREQ. Doesn't look possible in both HSv4 and HSv5.
1293 if (is_zero(m_tsRcvPeerStartTime))
1294 {
1295 LOGC(cnlog.Fatal, log << CONID() << "IPE: fillSrtHandshake_HSRSP: m_tsRcvPeerStartTime NOT SET!");
1296 return 0;
1297 }
1298
1299 // If Agent doesn't set TSBPD, it will not set the TSBPD flag back to the Peer.
1300 // The peer doesn't have be disturbed by it anyway.
1301 if (isOPT_TsbPd())
1302 {
1303 /*
1304 * We got and transposed peer start time (HandShake request timestamp),
1305 * we can support Timestamp-based Packet Delivery
1306 */
1307 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TSBPDRCV;
1308
1309 if (hs_version < HS_VERSION_SRT1)
1310 {
1311 // HSv4 - this uses only one value
1312 aw_srtdata[SRT_HS_LATENCY] = SRT_HS_LATENCY_LEG::wrap(m_iTsbPdDelay_ms);
1313 }
1314 else
1315 {
1316 // HSv5 - this puts "agent's" latency into RCV field and "peer's" -
1317 // into SND field.
1318 aw_srtdata[SRT_HS_LATENCY] = SRT_HS_LATENCY_RCV::wrap(m_iTsbPdDelay_ms);
1319 }
1320 }
1321 else
1322 {
1323 HLOGC(cnlog.Debug, log << CONID() << "HSRSP/snd: TSBPD off, NOT responding TSBPDRCV flag.");
1324 }
1325
1326 // Hsv5, only when peer has declared TSBPD mode.
1327 // The flag was already set, and the value already "maximized" in processSrtMsg_HSREQ().
1328 if (m_bPeerTsbPd && hs_version >= HS_VERSION_SRT1)
1329 {
1330 // HSv5 is bidirectional - so send the TSBPDSND flag, and place also the
1331 // peer's latency into SND field.
1332 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TSBPDSND;
1333 aw_srtdata[SRT_HS_LATENCY] |= SRT_HS_LATENCY_SND::wrap(m_iPeerTsbPdDelay_ms);
1334
1335 HLOGC(cnlog.Debug,
1336 log << CONID()
1337 << "HSRSP/snd: HSv5 peer uses TSBPD, responding TSBPDSND latency=" << m_iPeerTsbPdDelay_ms);
1338 }
1339 else
1340 {
1341 HLOGC(cnlog.Debug,
1342 log << CONID() << "HSRSP/snd: HSv" << (hs_version == CUDT::HS_VERSION_UDT4 ? 4 : 5)
1343 << " with peer TSBPD=" << (m_bPeerTsbPd ? "on" : "off") << " - NOT responding TSBPDSND");
1344 }
1345
1346 if (m_bTLPktDrop)
1347 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_TLPKTDROP;
1348
1349 if (m_config.bRcvNakReport)
1350 {
1351 // HSv5: Note that this setting is independent on the value of
1352 // m_bPeerNakReport, which represent this setting in the peer.
1353
1354 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_NAKREPORT;
1355 /*
1356 * NAK Report is so efficient at controlling bandwidth that sender TLPktDrop
1357 * is not needed. SRT 1.0.5 to 1.0.7 sender TLPktDrop combined with SRT 1.0
1358 * Timestamp-Based Packet Delivery was not well implemented and could drop
1359 * big I-Frame tail before sending once on low latency setups.
1360 * Disabling TLPktDrop in the receiver SRT Handshake Reply prevents the sender
1361 * from enabling Too-Late Packet Drop.
1362 */
1363 if (m_uPeerSrtVersion <= SrtVersion(1, 0, 7))
1364 aw_srtdata[SRT_HS_FLAGS] &= ~SRT_OPT_TLPKTDROP;
1365 }
1366
1367 if (m_config.uSrtVersion >= SrtVersion(1, 2, 0))
1368 {
1369 if (!m_bPeerRexmitFlag)
1370 {
1371 // Peer does not request to use rexmit flag, if so,
1372 // we won't use as well.
1373 HLOGC(cnlog.Debug,
1374 log << CONID() << "HSRSP/snd: AGENT understands REXMIT flag, but PEER DOES NOT. NOT setting.");
1375 }
1376 else
1377 {
1378 // Request that the rexmit bit be used as a part of msgno.
1379 aw_srtdata[SRT_HS_FLAGS] |= SRT_OPT_REXMITFLG;
1380 HLOGP(cnlog.Debug, "HSRSP/snd: AGENT UNDERSTANDS REXMIT flag and PEER reported that it does, too.");
1381 }
1382 }
1383 else
1384 {
1385 // Since this is now in the code, it can occur only in case when you change the
1386 // version specification in the build configuration.
1387 HLOGP(cnlog.Debug, "HSRSP/snd: AGENT DOES NOT UNDERSTAND REXMIT flag");
1388 }
1389
1390 HLOGC(cnlog.Debug,
1391 log << CONID() << "HSRSP/snd: LATENCY[SND:" << SRT_HS_LATENCY_SND::unwrap(aw_srtdata[SRT_HS_LATENCY])
1392 << " RCV:" << SRT_HS_LATENCY_RCV::unwrap(aw_srtdata[SRT_HS_LATENCY]) << "] FLAGS["
1393 << SrtFlagString(aw_srtdata[SRT_HS_FLAGS]) << "]");
1394
1395 return 3;
1396 }
1397
1398 size_t srt::CUDT::prepareSrtHsMsg(int cmd, uint32_t *srtdata, size_t size)
1399 {
1400 size_t srtlen = fillSrtHandshake(srtdata, size, cmd, handshakeVersion());
1401 HLOGC(cnlog.Debug, log << "CMD:" << MessageTypeStr(UMSG_EXT, cmd) << "(" << cmd << ") Len:"
1402 << int(srtlen * sizeof(int32_t))
1403 << " Version: " << SrtVersionString(srtdata[SRT_HS_VERSION])
1404 << " Flags: " << srtdata[SRT_HS_FLAGS]
1405 << " (" << SrtFlagString(srtdata[SRT_HS_FLAGS]) << ") sdelay:"
1406 << srtdata[SRT_HS_LATENCY]);
1407
1408 return srtlen;
1409 }
1410
1411 void srt::CUDT::sendSrtMsg(int cmd, uint32_t *srtdata_in, size_t srtlen_in)
1412 {
1413 CPacket srtpkt;
1414 int32_t srtcmd = (int32_t)cmd;
1415
1416 SRT_STATIC_ASSERT(SRTDATA_MAXSIZE >= SRT_HS_E_SIZE, "SRT_CMD_MAXSZ is too small to hold all the data");
1417 // This will be effectively larger than SRT_HS_E_SIZE, but it will be also used for incoming data.
1418 uint32_t srtdata[SRTDATA_MAXSIZE];
1419
1420 size_t srtlen = 0;
1421
1422 if (cmd == SRT_CMD_REJECT)
1423 {
1424 // This is a value returned by processSrtMsg underlying layer, potentially
1425 // to be reported here. Should this happen, just send a rejection message.
1426 cmd = SRT_CMD_HSRSP;
1427 srtdata[SRT_HS_VERSION] = 0;
1428 }
1429
1430 switch (cmd)
1431 {
1432 case SRT_CMD_HSREQ:
1433 case SRT_CMD_HSRSP:
1434 srtlen = prepareSrtHsMsg(cmd, srtdata, SRTDATA_MAXSIZE);
1435 break;
1436
1437 case SRT_CMD_KMREQ: // Sender
1438 case SRT_CMD_KMRSP: // Receiver
1439 srtlen = srtlen_in;
1440 /* Msg already in network order
1441 * But CChannel:sendto will swap again (assuming 32-bit fields)
1442 * Pre-swap to cancel it.
1443 */
1444 HtoNLA(srtdata, srtdata_in, srtlen);
1445 m_pCryptoControl->updateKmState(cmd, srtlen); // <-- THIS function can't be moved to CUDT
1446
1447 break;
1448
1449 default:
1450 LOGC(cnlog.Error, log << "sndSrtMsg: IPE: cmd=" << cmd << " unsupported");
1451 break;
1452 }
1453
1454 if (srtlen > 0)
1455 {
1456 /* srtpkt.pack will set message data in network order */
1457 srtpkt.pack(UMSG_EXT, &srtcmd, srtdata, srtlen * sizeof(int32_t));
1458 addressAndSend(srtpkt);
1459 }
1460 }
1461
1462 size_t srt::CUDT::fillHsExtConfigString(uint32_t* pcmdspec, int cmd, const string& str)
1463 {
1464 uint32_t* space = pcmdspec + 1;
1465 size_t wordsize = (str.size() + 3) / 4;
1466 size_t aligned_bytesize = wordsize * 4;
1467
1468 memset((space), 0, aligned_bytesize);
1469 memcpy((space), str.data(), str.size());
1470 // Preswap to little endian (in place due to possible padding zeros)
1471 HtoILA((space), space, wordsize);
1472
1473 *pcmdspec = HS_CMDSPEC_CMD::wrap(cmd) | HS_CMDSPEC_SIZE::wrap((uint32_t) wordsize);
1474
1475 return wordsize;
1476 }
1477
1478 #if ENABLE_BONDING
1479 // [[using locked(m_parent->m_ControlLock)]]
1480 // [[using locked(s_UDTUnited.m_GlobControlLock)]]
1481 size_t srt::CUDT::fillHsExtGroup(uint32_t* pcmdspec)
1482 {
1483 SRT_ASSERT(m_parent->m_GroupOf != NULL);
1484 uint32_t* space = pcmdspec + 1;
1485
1486 SRTSOCKET id = m_parent->m_GroupOf->id();
1487 SRT_GROUP_TYPE tp = m_parent->m_GroupOf->type();
1488 uint32_t flags = 0;
1489
1490 // NOTE: this code remains as is for historical reasons.
1491 // The initial implementation stated that the peer id be
1492 // extracted so that it can be reported and possibly the
1493 // start time somehow encoded and written into the group
1494 // extension, but it was later seen not necessary. Therefore
1495 // this code remains, but now it's informational only.
1496 #if ENABLE_HEAVY_LOGGING
1497 m_parent->m_GroupOf->debugMasterData(m_SocketID);
1498 #endif
1499
1500 // See CUDT::interpretGroup()
1501
1502 uint32_t dataword = 0
1503 | SrtHSRequest::HS_GROUP_TYPE::wrap(tp)
1504 | SrtHSRequest::HS_GROUP_FLAGS::wrap(flags)
1505 | SrtHSRequest::HS_GROUP_WEIGHT::wrap(m_parent->m_GroupMemberData->weight);
1506
1507 const uint32_t storedata [GRPD_E_SIZE] = { uint32_t(id), dataword };
1508 memcpy((space), storedata, sizeof storedata);
1509
1510 const size_t ra_size = Size(storedata);
1511 *pcmdspec = HS_CMDSPEC_CMD::wrap(SRT_CMD_GROUP) | HS_CMDSPEC_SIZE::wrap(ra_size);
1512
1513 return ra_size;
1514 }
1515 #endif
1516
1517 size_t srt::CUDT::fillHsExtKMREQ(uint32_t* pcmdspec, size_t ki)
1518 {
1519 uint32_t* space = pcmdspec + 1;
1520
1521 size_t msglen = m_pCryptoControl->getKmMsg_size(ki);
1522 // Make ra_size back in element unit
1523 // Add one extra word if the size isn't aligned to 32-bit.
1524 size_t ra_size = (msglen / sizeof(uint32_t)) + (msglen % sizeof(uint32_t) ? 1 : 0);
1525
1526 // Store the CMD + SIZE in the next field
1527 *pcmdspec = HS_CMDSPEC_CMD::wrap(SRT_CMD_KMREQ) | HS_CMDSPEC_SIZE::wrap((uint32_t) ra_size);
1528
1529 // Copy the key - do the endian inversion because another endian inversion
1530 // will be done for every control message before sending, and this KM message
1531 // is ALREADY in network order.
1532 const uint32_t* keydata = reinterpret_cast<const uint32_t*>(m_pCryptoControl->getKmMsg_data(ki));
1533
1534 HLOGC(cnlog.Debug,
1535 log << CONID() << "createSrtHandshake: KMREQ: adding key #" << ki << " length=" << ra_size
1536 << " words (KmMsg_size=" << msglen << ")");
1537 // XXX INSECURE ": [" << FormatBinaryString((uint8_t*)keydata, msglen) << "]";
1538
1539 // Yes, I know HtoNLA and NtoHLA do exactly the same operation, but I want
1540 // to be clear about the true intention.
1541 NtoHLA((space), keydata, ra_size);
1542
1543 return ra_size;
1544 }
1545
1546 size_t srt::CUDT::fillHsExtKMRSP(uint32_t* pcmdspec, const uint32_t* kmdata, size_t kmdata_wordsize)
1547 {
1548 uint32_t* space = pcmdspec + 1;
1549 const uint32_t failure_kmrsp[] = {SRT_KM_S_UNSECURED};
1550 const uint32_t* keydata = 0;
1551
1552 // Shift the starting point with the value of previously added block,
1553 // to start with the new one.
1554
1555 size_t ra_size;
1556
1557 if (kmdata_wordsize == 0)
1558 {
1559 LOGC(cnlog.Warn,
1560 log << CONID()
1561 << "createSrtHandshake: Agent has PW, but Peer sent no KMREQ. Sending error KMRSP response");
1562 ra_size = 1;
1563 keydata = failure_kmrsp;
1564
1565 // Update the KM state as well
1566 m_pCryptoControl->m_SndKmState = SRT_KM_S_NOSECRET; // Agent has PW, but Peer won't decrypt
1567 m_pCryptoControl->m_RcvKmState = SRT_KM_S_UNSECURED; // Peer won't encrypt as well.
1568 }
1569 else
1570 {
1571 if (!kmdata)
1572 {
1573 m_RejectReason = SRT_REJ_IPE;
1574 LOGC(cnlog.Fatal, log << CONID() << "createSrtHandshake: IPE: srtkm_cmd=SRT_CMD_KMRSP and no kmdata!");
1575 return 0;
1576 }
1577 ra_size = kmdata_wordsize;
1578 keydata = reinterpret_cast<const uint32_t *>(kmdata);
1579 }
1580
1581 *pcmdspec = HS_CMDSPEC_CMD::wrap(SRT_CMD_KMRSP) | HS_CMDSPEC_SIZE::wrap((uint32_t) ra_size);
1582 HLOGC(cnlog.Debug,
1583 log << CONID() << "createSrtHandshake: KMRSP: applying returned key length="
1584 << ra_size); // XXX INSECURE << " words: [" << FormatBinaryString((uint8_t*)kmdata,
1585 // kmdata_wordsize*sizeof(uint32_t)) << "]";
1586
1587 NtoHLA((space), keydata, ra_size);
1588 return ra_size;
1589 }
1590
1591
1592 // PREREQUISITE:
1593 // pkt must be set the buffer and configured for UMSG_HANDSHAKE.
1594 // Note that this function replaces also serialization for the HSv4.
1595 bool srt::CUDT::createSrtHandshake(
1596 int srths_cmd,
1597 int srtkm_cmd,
1598 const uint32_t* kmdata,
1599 size_t kmdata_wordsize, // IN WORDS, NOT BYTES!!!
1600 CPacket& w_pkt,
1601 CHandShake& w_hs)
1602 {
1603 // This function might be called before the opposite version was recognized.
1604 // Check if the version is exactly 4 because this means that the peer has already
1605 // sent something - asynchronously, and usually in rendezvous - and we already know
1606 // that the peer is version 4. In this case, agent must behave as HSv4, til the end.
1607 if (m_ConnRes.m_iVersion == HS_VERSION_UDT4)
1608 {
1609 w_hs.m_iVersion = HS_VERSION_UDT4;
1610 w_hs.m_iType = UDT_DGRAM;
1611 if (w_hs.m_extension)
1612 {
1613 // Should be impossible
1614 LOGC(cnlog.Error,
1615 log << CONID() << "createSrtHandshake: IPE: EXTENSION SET WHEN peer reports version 4 - fixing...");
1616 w_hs.m_extension = false;
1617 }
1618 }
1619 else
1620 {
1621 w_hs.m_iType = 0; // Prepare it for flags
1622 }
1623
1624 HLOGC(cnlog.Debug,
1625 log << CONID() << "createSrtHandshake: buf size=" << w_pkt.getLength()
1626 << " hsx=" << MessageTypeStr(UMSG_EXT, srths_cmd) << " kmx=" << MessageTypeStr(UMSG_EXT, srtkm_cmd)
1627 << " kmdata_wordsize=" << kmdata_wordsize << " version=" << w_hs.m_iVersion);
1628
1629 // Once you are certain that the version is HSv5, set the enc type flags
1630 // to advertise pbkeylen. Otherwise make sure that the old interpretation
1631 // will correctly pick up the type field. PBKEYLEN should be advertised
1632 // regardless of what URQ stage the handshake is (note that in case of rendezvous
1633 // CONCLUSION might be the FIRST MESSAGE EVER RECEIVED by a party).
1634 if (w_hs.m_iVersion > HS_VERSION_UDT4)
1635 {
1636 // Check if there was a failure to receie HSREQ before trying to craft HSRSP.
1637 // If fillSrtHandshake_HSRSP catches the condition of m_tsRcvPeerStartTime == steady_clock::zero(),
1638 // it will return size 0, which will mess up with further extension procedures;
1639 // PREVENT THIS HERE.
1640 if (w_hs.m_iReqType == URQ_CONCLUSION && srths_cmd == SRT_CMD_HSRSP && is_zero(m_tsRcvPeerStartTime))
1641 {
1642 LOGC(cnlog.Error,
1643 log << CONID()
1644 << "createSrtHandshake: IPE (non-fatal): Attempting to craft HSRSP without received HSREQ. "
1645 "BLOCKING extensions.");
1646 w_hs.m_extension = false;
1647 }
1648
1649 // The situation when this function is called without requested extensions
1650 // is URQ_CONCLUSION in rendezvous mode in some of the transitions.
1651 // In this case for version 5 just clear the m_iType field, as it has
1652 // different meaning in HSv5 and contains extension flags.
1653 //
1654 // Keep 0 in the SRT_HSTYPE_HSFLAGS field, but still advertise PBKEYLEN
1655 // in the SRT_HSTYPE_ENCFLAGS field.
1656 w_hs.m_iType = SrtHSRequest::wrapFlags(false /*no magic in HSFLAGS*/, m_config.iSndCryptoKeyLen);
1657
1658 IF_HEAVY_LOGGING(bool whether = m_config.iSndCryptoKeyLen != 0);
1659 HLOGC(cnlog.Debug,
1660 log << CONID() << "createSrtHandshake: " << (whether ? "" : "NOT ")
1661 << " Advertising PBKEYLEN - value = " << m_config.iSndCryptoKeyLen);
1662
1663 // Note: This is required only when sending a HS message without SRT extensions.
1664 // When this is to be sent with SRT extensions, then KMREQ will be attached here
1665 // and the PBKEYLEN will be extracted from it. If this is going to attach KMRSP
1666 // here, it's already too late (it should've been advertised before getting the first
1667 // handshake message with KMREQ).
1668 }
1669 else
1670 {
1671 w_hs.m_iType = UDT_DGRAM;
1672 }
1673
1674 // values > URQ_CONCLUSION include also error types
1675 // if (w_hs.m_iVersion == HS_VERSION_UDT4 || w_hs.m_iReqType > URQ_CONCLUSION) <--- This condition was checked b4 and
1676 // it's only valid for caller-listener mode
1677 if (!w_hs.m_extension)
1678 {
1679 // Serialize only the basic handshake, if this is predicted for
1680 // Hsv4 peer or this is URQ_INDUCTION or URQ_WAVEAHAND.
1681 size_t hs_size = w_pkt.getLength();
1682 w_hs.store_to((w_pkt.m_pcData), (hs_size));
1683 w_pkt.setLength(hs_size);
1684 HLOGC(cnlog.Debug,
1685 log << CONID() << "createSrtHandshake: (no ext) size=" << hs_size << " data: " << w_hs.show());
1686 return true;
1687 }
1688
1689 // Sanity check, applies to HSv5 only cases.
1690 if (srths_cmd == SRT_CMD_HSREQ && m_SrtHsSide == HSD_RESPONDER)
1691 {
1692 m_RejectReason = SRT_REJ_IPE;
1693 LOGC(cnlog.Fatal,
1694 log << CONID() << "IPE: SRT_CMD_HSREQ was requested to be sent in HSv5 by an INITIATOR side!");
1695 return false; // should cause rejection
1696 }
1697
1698 ostringstream logext;
1699 logext << "HSX";
1700
1701 // Install the SRT extensions
1702 w_hs.m_iType |= CHandShake::HS_EXT_HSREQ;
1703
1704 bool have_sid = false;
1705 if (srths_cmd == SRT_CMD_HSREQ && !m_config.sStreamName.empty())
1706 {
1707 have_sid = true;
1708 w_hs.m_iType |= CHandShake::HS_EXT_CONFIG;
1709 logext << ",SID";
1710 }
1711
1712 // If this is a response, we have also information
1713 // on the peer. If Peer is NOT filter capable, don't
1714 // put filter config, even if agent is capable.
1715 bool peer_filter_capable = true;
1716 if (srths_cmd == SRT_CMD_HSRSP)
1717 {
1718 if (m_sPeerPktFilterConfigString != "")
1719 {
1720 peer_filter_capable = true;
1721 }
1722 else if (IsSet(m_uPeerSrtFlags, SRT_OPT_FILTERCAP))
1723 {
1724 peer_filter_capable = true;
1725 }
1726 else
1727 {
1728 peer_filter_capable = false;
1729 }
1730 }
1731
1732 // Now, if this is INITIATOR, then it has its
1733 // filter config already set, if configured, otherwise
1734 // it should not attach the filter config extension.
1735
1736 // If this is a RESPONDER, then it has already received
1737 // the filter config string from the peer and therefore
1738 // possibly confronted with the contents of m_OPT_FECConfigString,
1739 // and if it decided to go with filter, it will be nonempty.
1740 bool have_filter = false;
1741 if (peer_filter_capable && !m_config.sPacketFilterConfig.empty())
1742 {
1743 have_filter = true;
1744 w_hs.m_iType |= CHandShake::HS_EXT_CONFIG;
1745 logext << ",filter";
1746 }
1747
1748 bool have_congctl = false;
1749 const string sm = m_config.sCongestion.str();
1750 if (sm != "" && sm != "live")
1751 {
1752 have_congctl = true;
1753 w_hs.m_iType |= CHandShake::HS_EXT_CONFIG;
1754 logext << ",CONGCTL";
1755 }
1756
1757 bool have_kmreq = false;
1758 // Prevent adding KMRSP only in case when BOTH:
1759 // - Agent has set no password
1760 // - no KMREQ has arrived from Peer
1761 // KMRSP must be always sent when:
1762 // - Agent set a password, Peer did not send KMREQ: Agent sets snd=NOSECRET.
1763 // - Agent set no password, but Peer sent KMREQ: Ageng sets rcv=NOSECRET.
1764 if (m_config.CryptoSecret.len > 0 || kmdata_wordsize > 0)
1765 {
1766 have_kmreq = true;
1767 w_hs.m_iType |= CHandShake::HS_EXT_KMREQ;
1768 logext << ",KMX";
1769 }
1770
1771 #if ENABLE_BONDING
1772 bool have_group = false;
1773
1774 // Note: this is done without locking because we have the following possibilities:
1775 //
1776 // 1. Most positive: the group will be the same all the time up to the moment when we use it.
1777 // 2. The group will disappear when next time we try to use it having now have_group set true.
1778 //
1779 // Not possible that a group is NULL now but would appear later: the group must be either empty
1780 // or already set as valid at this time.
1781 //
1782 // If the 2nd possibility happens, then simply it means that the group has been closed during
1783 // the operation and the socket got this information updated in the meantime. This means that
1784 // it was an abnormal interrupt during the processing so the handshake process should be aborted
1785 // anyway, and that's what will be done.
1786
1787 // LOCKING INFORMATION: accessing this field just for NULL check doesn't
1788 // hurt, even if this field could be dangling in the moment. This will be
1789 // followed by an additional check, done this time under lock, and there will
1790 // be no dangling pointers at this time.
1791 if (m_parent->m_GroupOf)
1792 {
1793 // Whatever group this socket belongs to, the information about
1794 // the group is always sent the same way with the handshake.
1795 have_group = true;
1796 w_hs.m_iType |= CHandShake::HS_EXT_CONFIG;
1797 logext << ",GROUP";
1798 }
1799 #endif
1800
1801 HLOGC(cnlog.Debug, log << CONID() << "createSrtHandshake: (ext: " << logext.str() << ") data: " << w_hs.show());
1802
1803 // NOTE: The HSREQ is practically always required, although may happen
1804 // in future that CONCLUSION can be sent multiple times for a separate
1805 // stream encryption support, and this way it won't enclose HSREQ.
1806 // Also, KMREQ may occur multiple times.
1807
1808 // So, initially store the UDT legacy handshake.
1809 size_t hs_size = w_pkt.getLength(), total_ra_size = (hs_size / sizeof(uint32_t)); // Maximum size of data
1810 w_hs.store_to((w_pkt.m_pcData), (hs_size)); // hs_size is updated
1811
1812 size_t ra_size = hs_size / sizeof(int32_t);
1813
1814 // Now attach the SRT handshake for HSREQ
1815 size_t offset = ra_size;
1816 uint32_t *p = reinterpret_cast<uint32_t *>(w_pkt.m_pcData);
1817 // NOTE: since this point, ra_size has a size in int32_t elements, NOT BYTES.
1818
1819 // The first 4-byte item is the CMD/LENGTH spec.
1820 uint32_t *pcmdspec = p + offset; // Remember the location to be filled later, when we know the length
1821 ++offset;
1822
1823 // Now use the original function to store the actual SRT_HS data
1824 // ra_size after that
1825 // NOTE: so far, ra_size is m_iMaxSRTPayloadSize expressed in number of elements.
1826 // WILL BE CHANGED HERE.
1827 ra_size = fillSrtHandshake((p + offset), total_ra_size - offset, srths_cmd, HS_VERSION_SRT1);
1828 *pcmdspec = HS_CMDSPEC_CMD::wrap(srths_cmd) | HS_CMDSPEC_SIZE::wrap((uint32_t) ra_size);
1829
1830 HLOGC(cnlog.Debug,
1831 log << CONID() << "createSrtHandshake: after HSREQ: offset=" << offset << " HSREQ size=" << ra_size
1832 << " space left: " << (total_ra_size - offset));
1833
1834 // Use only in REQ phase and only if stream name is set
1835 if (have_sid)
1836 {
1837 // Now prepare the string with 4-byte alignment. The string size is limited
1838 // to half the payload size. Just a sanity check to not pack too much into
1839 // the conclusion packet.
1840 size_t size_limit = m_iMaxSRTPayloadSize / 2;
1841
1842 if (m_config.sStreamName.size() >= size_limit)
1843 {
1844 m_RejectReason = SRT_REJ_ROGUE;
1845 LOGC(cnlog.Warn,
1846 log << CONID() << "createSrtHandshake: stream id too long, limited to " << (size_limit - 1)
1847 << " bytes");
1848 return false;
1849 }
1850
1851 offset += ra_size + 1;
1852 ra_size = fillHsExtConfigString(p + offset - 1, SRT_CMD_SID, m_config.sStreamName.str());
1853
1854 HLOGC(cnlog.Debug,
1855 log << CONID() << "createSrtHandshake: after SID [" << m_config.sStreamName.c_str()
1856 << "] length=" << m_config.sStreamName.size() << " alignedln=" << (4 * ra_size)
1857 << ": offset=" << offset << " SID size=" << ra_size << " space left: " << (total_ra_size - offset));
1858 }
1859
1860 if (have_congctl)
1861 {
1862 // Pass the congctl to the other side as informational.
1863 // The other side should reject connection if it uses a different congctl.
1864 // The other side should also respond with the congctl it uses, if its non-default (for backward compatibility).
1865
1866 offset += ra_size + 1;
1867 ra_size = fillHsExtConfigString(p + offset - 1, SRT_CMD_CONGESTION, sm);
1868
1869 HLOGC(cnlog.Debug,
1870 log << CONID() << "createSrtHandshake: after CONGCTL [" << sm << "] length=" << sm.size()
1871 << " alignedln=" << (4 * ra_size) << ": offset=" << offset << " CONGCTL size=" << ra_size
1872 << " space left: " << (total_ra_size - offset));
1873 }
1874
1875 if (have_filter)
1876 {
1877 offset += ra_size + 1;
1878 ra_size = fillHsExtConfigString(p + offset - 1, SRT_CMD_FILTER, m_config.sPacketFilterConfig.str());
1879
1880 HLOGC(cnlog.Debug,
1881 log << CONID() << "createSrtHandshake: after filter [" << m_config.sPacketFilterConfig.c_str()
1882 << "] length=" << m_config.sPacketFilterConfig.size() << " alignedln=" << (4 * ra_size) << ": offset="
1883 << offset << " filter size=" << ra_size << " space left: " << (total_ra_size - offset));
1884 }
1885
1886 #if ENABLE_BONDING
1887 // Note that this will fire in both cases:
1888 // - When the group has been set by the user on a socket (or socket was created as a part of the group),
1889 // and the handshake request is to be sent with informing the peer that this connection belongs to a group
1890 // - When the agent received a HS request with a group, has created its mirror group on its side, and
1891 // now sends the HS response to the peer, with ITS OWN group id (the mirror one).
1892 //
1893 // XXX Probably a condition should be checked here around the group type.
1894 // The time synchronization should be done only on any kind of parallel sending group.
1895 // Currently all groups are such groups (broadcast, backup, balancing), but it may
1896 // need to be changed for some other types.
1897 if (have_group)
1898 {
1899 // NOTE: See information about mutex ordering in api.h
1900 SharedLock gdrg (uglobal().m_GlobControlLock);
1901 if (!m_parent->m_GroupOf)
1902 {
1903 // This may only happen if since last check of m_GroupOf pointer the socket was removed
1904 // from the group in the meantime, which can only happen due to that the group was closed.
1905 // In such a case it simply means that the handshake process was requested to be interrupted.
1906 LOGC(cnlog.Fatal, log << CONID() << "GROUP DISAPPEARED. Socket not capable of continuing HS");
1907 return false;
1908 }
1909 else
1910 {
1911 if (m_parent->m_GroupOf->closing())
1912 {
1913 m_RejectReason = SRT_REJ_IPE;
1914 LOGC(cnlog.Error,
1915 log << CONID() << "createSrtHandshake: group is closing during the process, rejecting.");
1916 return false;
1917 }
1918 offset += ra_size + 1;
1919 ra_size = fillHsExtGroup(p + offset - 1);
1920
1921 HLOGC(cnlog.Debug,
1922 log << CONID() << "createSrtHandshake: after GROUP [" << sm << "] length=" << sm.size() << ": offset="
1923 << offset << " GROUP size=" << ra_size << " space left: " << (total_ra_size - offset));
1924 }
1925 }
1926 #endif
1927
1928 // When encryption turned on
1929 if (have_kmreq)
1930 {
1931 HLOGC(cnlog.Debug,
1932 log << CONID() << "createSrtHandshake: "
1933 << (m_config.CryptoSecret.len > 0 ? "Agent uses ENCRYPTION" : "Peer requires ENCRYPTION"));
1934
1935 if (!m_pCryptoControl && (srtkm_cmd == SRT_CMD_KMREQ || srtkm_cmd == SRT_CMD_KMRSP))
1936 {
1937 m_RejectReason = SRT_REJ_IPE;
1938 LOGC(cnlog.Error,
1939 log << CONID() << "createSrtHandshake: IPE: need to send KM, but CryptoControl does not exist."
1940 << " Socket state: connected=" << boolalpha << m_bConnected << ", connecting=" << m_bConnecting
1941 << ", broken=" << m_bBroken << ", closing=" << m_bClosing << ".");
1942 return false;
1943 }
1944
1945 if (srtkm_cmd == SRT_CMD_KMREQ)
1946 {
1947 bool have_any_keys = false;
1948 for (size_t ki = 0; ki < 2; ++ki)
1949 {
1950 // Skip those that have expired
1951 if (!m_pCryptoControl->getKmMsg_needSend(ki, false))
1952 continue;
1953
1954 m_pCryptoControl->getKmMsg_markSent(ki, false);
1955
1956 offset += ra_size + 1;
1957 ra_size = fillHsExtKMREQ(p + offset - 1, ki);
1958
1959 have_any_keys = true;
1960 }
1961
1962 if (!have_any_keys)
1963 {
1964 m_RejectReason = SRT_REJ_IPE;
1965 LOGC(cnlog.Error, log << CONID() << "createSrtHandshake: IPE: all keys have expired, no KM to send.");
1966 return false;
1967 }
1968 }
1969 else if (srtkm_cmd == SRT_CMD_KMRSP)
1970 {
1971 offset += ra_size + 1;
1972 ra_size = fillHsExtKMRSP(p + offset - 1, kmdata, kmdata_wordsize);
1973 }
1974 else
1975 {
1976 m_RejectReason = SRT_REJ_IPE;
1977 LOGC(cnlog.Fatal, log << CONID() << "createSrtHandshake: IPE: wrong value of srtkm_cmd: " << srtkm_cmd);
1978 return false;
1979 }
1980 }
1981
1982 if (ra_size == 0)
1983 {
1984 // m_RejectReason is expected to be set by fillHsExtKMRSP(..) in this case.
1985 return false;
1986 }
1987
1988 // ra_size + offset has a value in element unit.
1989 // Switch it again to byte unit.
1990 w_pkt.setLength((ra_size + offset) * sizeof(int32_t));
1991
1992 HLOGC(cnlog.Debug,
1993 log << CONID() << "createSrtHandshake: filled HSv5 handshake flags: "
1994 << CHandShake::ExtensionFlagStr(w_hs.m_iType) << " length: " << w_pkt.getLength() << " bytes");
1995
1996 return true;
1997 }
1998
1999 template <class Integer>
2000 static inline int FindExtensionBlock(Integer* begin, size_t total_length,
2001 size_t& w_out_len, Integer*& w_next_block)
2002 {
2003 // Check if there's anything to process
2004 if (total_length == 0)
2005 {
2006 w_next_block = NULL;
2007 w_out_len = 0;
2008 return SRT_CMD_NONE;
2009 }
2010
2011 // This function extracts the block command from the block and its length.
2012 // The command value is returned as a function result.
2013 // The size of that command block is stored into w_out_len.
2014 // The beginning of the prospective next block is stored in w_next_block.
2015
2016 // The caller must be aware that:
2017 // - exactly one element holds the block header (cmd+size), so the actual data are after this one.
2018 // - the returned size is the number of uint32_t elements since that first data element
2019 // - the remaining size should be manually calculated as total_length - 1 - w_out_len, or
2020 // simply, as w_next_block - begin.
2021
2022 // Note that if the total_length is too short to extract the whole block, it will return
2023 // SRT_CMD_NONE. Note that total_length includes this first CMDSPEC word.
2024 //
2025 // When SRT_CMD_NONE is returned, it means that nothing has been extracted and nothing else
2026 // can be further extracted from this block.
2027
2028 int cmd = HS_CMDSPEC_CMD::unwrap(*begin);
2029 size_t size = HS_CMDSPEC_SIZE::unwrap(*begin);
2030
2031 if (size + 1 > total_length)
2032 return SRT_CMD_NONE;
2033
2034 w_out_len = size;
2035
2036 if (total_length == size + 1)
2037 w_next_block = NULL;
2038 else
2039 w_next_block = begin + 1 + size;
2040
2041 return cmd;
2042 }
2043
2044 // NOTE: the rule of order of arguments is broken here because this order
2045 // serves better the logics and readability.
2046 template <class Integer>
2047 static inline bool NextExtensionBlock(Integer*& w_begin, Integer* next, size_t& w_length)
2048 {
2049 if (!next)
2050 return false;
2051
2052 w_length = w_length - (next - w_begin);
2053 w_begin = next;
2054 return true;
2055 }
2056
2057 void SrtExtractHandshakeExtensions(const char* bufbegin, size_t buflength,
2058 vector<SrtHandshakeExtension>& w_output)
2059 {
2060 const uint32_t *begin = reinterpret_cast<const uint32_t *>(bufbegin + CHandShake::m_iContentSize);
2061 size_t size = buflength - CHandShake::m_iContentSize; // Due to previous cond check we grant it's >0
2062 const uint32_t *next = 0;
2063 size_t length = size / sizeof(uint32_t);
2064 size_t blocklen = 0;
2065
2066 for (;;) // ONE SHOT, but continuable loop
2067 {
2068 const int cmd = FindExtensionBlock(begin, length, (blocklen), (next));
2069
2070 if (cmd == SRT_CMD_NONE)
2071 {
2072 // End of blocks
2073 break;
2074 }
2075
2076 w_output.push_back(SrtHandshakeExtension(cmd));
2077
2078 SrtHandshakeExtension& ext = w_output.back();
2079
2080 std::copy(begin+1, begin+blocklen+1, back_inserter(ext.contents));
2081
2082 // Any other kind of message extracted. Search on.
2083 if (!NextExtensionBlock((begin), next, (length)))
2084 break;
2085 }
2086 }
2087
2088 #if SRT_DEBUG_RTT
2089 class RttTracer
2090 {
2091 public:
2092 RttTracer()
2093 {
2094 }
2095
2096 ~RttTracer()
2097 {
2098 srt::sync::ScopedLock lck(m_mtx);
2099 m_fout.close();
2100 }
2101
2102 void trace(const srt::sync::steady_clock::time_point& currtime,
2103 const std::string& event, int rtt_sample, int rttvar_sample,
2104 bool is_smoothed_rtt_reset, int64_t recvTotal,
2105 int smoothed_rtt, int rttvar)
2106 {
2107 srt::sync::ScopedLock lck(m_mtx);
2108 create_file();
2109
2110 m_fout << srt::sync::FormatTimeSys(currtime) << ",";
2111 m_fout << srt::sync::FormatTime(currtime) << ",";
2112 m_fout << event << ",";
2113 m_fout << rtt_sample << ",";
2114 m_fout << rttvar_sample << ",";
2115 m_fout << is_smoothed_rtt_reset << ",";
2116 m_fout << recvTotal << ",";
2117 m_fout << smoothed_rtt << ",";
2118 m_fout << rttvar << "\n";
2119 m_fout.flush();
2120 }
2121
2122 private:
2123 void print_header()
2124 {
2125 m_fout << "Timepoint_SYST,Timepoint_STDY,Event,usRTTSample,"
2126 "usRTTVarSample,IsSmoothedRTTReset,pktsRecvTotal,"
2127 "usSmoothedRTT,usRTTVar\n";
2128 }
2129
2130 void create_file()
2131 {
2132 if (m_fout.is_open())
2133 return;
2134
2135 std::string str_tnow = srt::sync::FormatTimeSys(srt::sync::steady_clock::now());
2136 str_tnow.resize(str_tnow.size() - 7); // remove trailing ' [SYST]' part
2137 while (str_tnow.find(':') != std::string::npos) {
2138 str_tnow.replace(str_tnow.find(':'), 1, 1, '_');
2139 }
2140 const std::string fname = "rtt_trace_" + str_tnow + "_" + SRT_SYNC_CLOCK_STR + ".csv";
2141 m_fout.open(fname, std::ofstream::out);
2142 if (!m_fout)
2143 std::cerr << "IPE: Failed to open " << fname << "!!!\n";
2144
2145 print_header();
2146 }
2147
2148 private:
2149 srt::sync::Mutex m_mtx;
2150 std::ofstream m_fout;
2151 };
2152
2153 RttTracer s_rtt_trace;
2154 #endif
2155
2156
2157 bool srt::CUDT::processSrtMsg(const CPacket *ctrlpkt)
2158 {
2159 uint32_t *srtdata = (uint32_t *)ctrlpkt->m_pcData;
2160 size_t len = ctrlpkt->getLength();
2161 int etype = ctrlpkt->getExtendedType();
2162 uint32_t ts = ctrlpkt->timestamp();
2163
2164 int res = SRT_CMD_NONE;
2165
2166 HLOGC(cnlog.Debug,
2167 log << CONID() << "Dispatching message type=" << etype << " data length=" << (len / sizeof(int32_t)));
2168 switch (etype)
2169 {
2170 case SRT_CMD_HSREQ:
2171 {
2172 res = processSrtMsg_HSREQ(srtdata, len, ts, CUDT::HS_VERSION_UDT4);
2173 break;
2174 }
2175 case SRT_CMD_HSRSP:
2176 {
2177 res = processSrtMsg_HSRSP(srtdata, len, ts, CUDT::HS_VERSION_UDT4);
2178 break;
2179 }
2180 case SRT_CMD_KMREQ:
2181 // Special case when the data need to be processed here
2182 // and the appropriate message must be constructed for sending.
2183 // No further processing required
2184 {
2185 uint32_t srtdata_out[SRTDATA_MAXSIZE];
2186 size_t len_out = 0;
2187 res = m_pCryptoControl->processSrtMsg_KMREQ(srtdata, len, CUDT::HS_VERSION_UDT4, m_uPeerSrtVersion,
2188 (srtdata_out), (len_out));
2189 if (res == SRT_CMD_KMRSP)
2190 {
2191 if (len_out == 1)
2192 {
2193 if (m_config.bEnforcedEnc)
2194 {
2195 LOGC(cnlog.Warn,
2196 log << CONID() << "KMREQ FAILURE: " << KmStateStr(SRT_KM_STATE(srtdata_out[0]))
2197 << " - rejecting per enforced encryption");
2198 res = SRT_CMD_NONE;
2199 break;
2200 }
2201 HLOGC(cnlog.Debug,
2202 log << CONID()
2203 << "MKREQ -> KMRSP FAILURE state: " << KmStateStr(SRT_KM_STATE(srtdata_out[0])));
2204 }
2205 else
2206 {
2207 HLOGC(cnlog.Debug, log << CONID() << "KMREQ -> requested to send KMRSP length=" << len_out);
2208 }
2209 sendSrtMsg(SRT_CMD_KMRSP, srtdata_out, len_out);
2210 }
2211 // XXX Dead code. processSrtMsg_KMREQ now doesn't return any other value now.
2212 // Please review later.
2213 else
2214 {
2215 LOGC(cnlog.Warn, log << CONID() << "KMREQ failed to process the request - ignoring");
2216 }
2217
2218 return true; // already done what's necessary
2219 }
2220
2221 case SRT_CMD_KMRSP:
2222 {
2223 // KMRSP doesn't expect any following action
2224 m_pCryptoControl->processSrtMsg_KMRSP(srtdata, len, m_uPeerSrtVersion);
2225 return true; // nothing to do
2226 }
2227
2228 default:
2229 return false;
2230 }
2231
2232 if (res == SRT_CMD_NONE)
2233 return true;
2234
2235 // Send the message that the message handler requested.
2236 sendSrtMsg(res);
2237
2238 return true;
2239 }
2240
2241 int srt::CUDT::processSrtMsg_HSREQ(const uint32_t *srtdata, size_t bytelen, uint32_t ts, int hsv)
2242 {
2243 // Set this start time in the beginning, regardless as to whether TSBPD is being
2244 // used or not. This must be done in the Initiator as well as Responder.
2245
2246 /*
2247 * Compute peer StartTime in our time reference
2248 * This takes time zone, time drift into account.
2249 * Also includes current packet transit time (rtt/2)
2250 */
2251 m_tsRcvPeerStartTime = steady_clock::now() - microseconds_from(ts);
2252 // (in case of bonding group, this value will be OVERWRITTEN
2253 // later in CUDT::interpretGroup).
2254
2255 // Prepare the initial runtime values of latency basing on the option values.
2256 // They are going to get the value fixed HERE.
2257 m_iTsbPdDelay_ms = m_config.iRcvLatency;
2258 m_iPeerTsbPdDelay_ms = m_config.iPeerLatency;
2259
2260 if (bytelen < SRT_CMD_HSREQ_MINSZ)
2261 {
2262 m_RejectReason = SRT_REJ_ROGUE;
2263 /* Packet smaller than minimum compatible packet size */
2264 LOGC(cnlog.Error, log << "HSREQ/rcv: cmd=" << SRT_CMD_HSREQ << "(HSREQ) len=" << bytelen << " invalid");
2265 return SRT_CMD_NONE;
2266 }
2267
2268 LOGC(cnlog.Debug, log << "HSREQ/rcv: cmd=" << SRT_CMD_HSREQ << "(HSREQ) len=" << bytelen
2269 << hex << " vers=0x" << srtdata[SRT_HS_VERSION] << " opts=0x" << srtdata[SRT_HS_FLAGS]
2270 << dec << " delay=" << SRT_HS_LATENCY_RCV::unwrap(srtdata[SRT_HS_LATENCY]));
2271
2272 m_uPeerSrtVersion = srtdata[SRT_HS_VERSION];
2273 m_uPeerSrtFlags = srtdata[SRT_HS_FLAGS];
2274
2275 if (hsv == CUDT::HS_VERSION_UDT4)
2276 {
2277 if (m_uPeerSrtVersion >= SRT_VERSION_FEAT_HSv5)
2278 {
2279 m_RejectReason = SRT_REJ_ROGUE;
2280 LOGC(cnlog.Error,
2281 log << CONID() << "HSREQ/rcv: With HSv4 version >= " << SrtVersionString(SRT_VERSION_FEAT_HSv5)
2282 << " is not acceptable.");
2283 return SRT_CMD_REJECT;
2284 }
2285 }
2286 else
2287 {
2288 if (m_uPeerSrtVersion < SRT_VERSION_FEAT_HSv5)
2289 {
2290 m_RejectReason = SRT_REJ_ROGUE;
2291 LOGC(cnlog.Error,
2292 log << CONID() << "HSREQ/rcv: With HSv5 version must be >= " << SrtVersionString(SRT_VERSION_FEAT_HSv5)
2293 << " .");
2294 return SRT_CMD_REJECT;
2295 }
2296 }
2297
2298 // Check also if the version satisfies the minimum required version
2299 if (m_uPeerSrtVersion < m_config.uMinimumPeerSrtVersion)
2300 {
2301 m_RejectReason = SRT_REJ_VERSION;
2302 LOGC(cnlog.Error,
2303 log << CONID() << "HSREQ/rcv: Peer version: " << SrtVersionString(m_uPeerSrtVersion)
2304 << " is too old for requested: " << SrtVersionString(m_config.uMinimumPeerSrtVersion)
2305 << " - REJECTING");
2306 return SRT_CMD_REJECT;
2307 }
2308
2309 HLOGC(cnlog.Debug,
2310 log << CONID() << "HSREQ/rcv: PEER Version: " << SrtVersionString(m_uPeerSrtVersion)
2311 << " Flags: " << m_uPeerSrtFlags << "(" << SrtFlagString(m_uPeerSrtFlags)
2312 << ") Min req version:" << SrtVersionString(m_config.uMinimumPeerSrtVersion));
2313
2314 m_bPeerRexmitFlag = IsSet(m_uPeerSrtFlags, SRT_OPT_REXMITFLG);
2315 HLOGC(cnlog.Debug, log << CONID() << "HSREQ/rcv: peer " << (m_bPeerRexmitFlag ? "UNDERSTANDS" : "DOES NOT UNDERSTAND") << " REXMIT flag");
2316
2317 // Check if both use the same API type. Reject if not.
2318 bool peer_message_api = !IsSet(m_uPeerSrtFlags, SRT_OPT_STREAM);
2319 if (peer_message_api != m_config.bMessageAPI)
2320 {
2321 m_RejectReason = SRT_REJ_MESSAGEAPI;
2322 LOGC(cnlog.Error,
2323 log << CONID() << "HSREQ/rcv: Agent uses " << (m_config.bMessageAPI ? "MESSAGE" : "STREAM")
2324 << " API, but the Peer declares " << (peer_message_api ? "MESSAGE" : "STREAM")
2325 << " API. Not compatible transmission type, rejecting.");
2326 return SRT_CMD_REJECT;
2327 }
2328
2329 SRT_STATIC_ASSERT(SRT_HS_E_SIZE == SRT_HS_LATENCY + 1, "Assuming latency is the last field");
2330 if (bytelen < (SRT_HS_E_SIZE * sizeof(uint32_t)))
2331 {
2332 // Handshake extension message includes VERSION, FLAGS and LATENCY
2333 // (3 x 32 bits). SRT v1.2.0 and earlier might supply shorter extension message,
2334 // without LATENCY fields.
2335 // It is acceptable, as long as the latency flags are not set on our side.
2336 //
2337 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2338 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2339 // | SRT Version |
2340 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2341 // | SRT Flags |
2342 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2343 // | Receiver TSBPD Delay | Sender TSBPD Delay |
2344 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2345 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDSND) || IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDRCV))
2346 {
2347 m_RejectReason = SRT_REJ_ROGUE;
2348 LOGC(cnlog.Error,
2349 log << CONID()
2350 << "HSREQ/rcv: Peer sent only VERSION + FLAGS HSREQ, but TSBPD flags are set. Rejecting.");
2351 return SRT_CMD_REJECT;
2352 }
2353
2354 LOGC(cnlog.Warn,
2355 log << CONID() << "HSREQ/rcv: Peer sent only VERSION + FLAGS HSREQ, not getting any TSBPD settings.");
2356 // Don't process any further settings in this case. Turn off TSBPD, just for a case.
2357 m_bTsbPd = false;
2358 m_bPeerTsbPd = false;
2359 return SRT_CMD_HSRSP;
2360 }
2361
2362 const uint32_t latencystr = srtdata[SRT_HS_LATENCY];
2363
2364 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDSND))
2365 {
2366 // TimeStamp-based Packet Delivery feature enabled
2367 if (!isOPT_TsbPd())
2368 {
2369 LOGC(cnlog.Warn,
2370 log << CONID() << "HSREQ/rcv: Agent did not set rcv-TSBPD - ignoring proposed latency from peer");
2371
2372 // Note: also don't set the peer TSBPD flag HERE because
2373 // - in HSv4 it will be a sender, so it doesn't matter anyway
2374 // - in HSv5 if it's going to receive, the TSBPDRCV flag will define it.
2375 }
2376 else
2377 {
2378 int peer_decl_latency;
2379 if (hsv < CUDT::HS_VERSION_SRT1)
2380 {
2381 // In HSv4 there is only one value and this is the latency
2382 // that the sender peer proposes for the agent.
2383 peer_decl_latency = SRT_HS_LATENCY_LEG::unwrap(latencystr);
2384 }
2385 else
2386 {
2387 // In HSv5 there are latency declared for sending and receiving separately.
2388
2389 // SRT_HS_LATENCY_SND is the value that the peer proposes to be the
2390 // value used by agent when receiving data. We take this as a local latency value.
2391 peer_decl_latency = SRT_HS_LATENCY_SND::unwrap(srtdata[SRT_HS_LATENCY]);
2392 }
2393
2394 // Use the maximum latency out of latency from our settings and the latency
2395 // "proposed" by the peer.
2396 int maxdelay = std::max(m_iTsbPdDelay_ms, peer_decl_latency);
2397 HLOGC(cnlog.Debug,
2398 log << CONID() << "HSREQ/rcv: LOCAL/RCV LATENCY: Agent:" << m_iTsbPdDelay_ms
2399 << " Peer:" << peer_decl_latency << " Selecting:" << maxdelay);
2400 m_iTsbPdDelay_ms = maxdelay;
2401 m_bTsbPd = true;
2402 }
2403 }
2404 else
2405 {
2406 std::string how_about_agent = isOPT_TsbPd() ? "BUT AGENT DOES" : "and nor does Agent";
2407 HLOGC(cnlog.Debug, log << CONID() << "HSREQ/rcv: Peer DOES NOT USE latency for sending - " << how_about_agent);
2408 }
2409
2410 // This happens when the HSv5 RESPONDER receives the HSREQ message; it declares
2411 // that the peer INITIATOR will receive the data and informs about its predefined
2412 // latency. We need to maximize this with our setting of the peer's latency and
2413 // record as peer's latency, which will be then sent back with HSRSP.
2414 if (hsv > CUDT::HS_VERSION_UDT4 && IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDRCV))
2415 {
2416 // So, PEER uses TSBPD, set the flag.
2417 // NOTE: it doesn't matter, if AGENT uses TSBPD.
2418 m_bPeerTsbPd = true;
2419
2420 // SRT_HS_LATENCY_RCV is the value that the peer declares as to be
2421 // used by it when receiving data. We take this as a peer's value,
2422 // and select the maximum of this one and our proposed latency for the peer.
2423 int peer_decl_latency = SRT_HS_LATENCY_RCV::unwrap(latencystr);
2424 int maxdelay = std::max(m_iPeerTsbPdDelay_ms, peer_decl_latency);
2425 HLOGC(cnlog.Debug,
2426 log << CONID() << "HSREQ/rcv: PEER/RCV LATENCY: Agent:" << m_iPeerTsbPdDelay_ms
2427 << " Peer:" << peer_decl_latency << " Selecting:" << maxdelay);
2428 m_iPeerTsbPdDelay_ms = maxdelay;
2429 }
2430 else
2431 {
2432 std::string how_about_agent = isOPT_TsbPd() ? "BUT AGENT DOES" : "and nor does Agent";
2433 HLOGC(cnlog.Debug,
2434 log << CONID() << "HSREQ/rcv: Peer DOES NOT USE latency for receiving - " << how_about_agent);
2435 }
2436
2437 if (hsv > CUDT::HS_VERSION_UDT4)
2438 {
2439 // This is HSv5, do the same things as required for the sending party in HSv4,
2440 // as in HSv5 this can also be a sender.
2441 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TLPKTDROP))
2442 {
2443 // Too late packets dropping feature supported
2444 m_bPeerTLPktDrop = true;
2445 }
2446 if (IsSet(m_uPeerSrtFlags, SRT_OPT_NAKREPORT))
2447 {
2448 // Peer will send Periodic NAK Reports
2449 m_bPeerNakReport = true;
2450 }
2451 }
2452
2453 return SRT_CMD_HSRSP;
2454 }
2455
2456 int srt::CUDT::processSrtMsg_HSRSP(const uint32_t *srtdata, size_t bytelen, uint32_t ts, int hsv)
2457 {
2458 // XXX Check for mis-version
2459 // With HSv4 we accept only version less than 1.3.0
2460 if (hsv == CUDT::HS_VERSION_UDT4 && srtdata[SRT_HS_VERSION] >= SRT_VERSION_FEAT_HSv5)
2461 {
2462 LOGC(cnlog.Error, log << CONID() << "HSRSP/rcv: With HSv4 version >= 1.2.0 is not acceptable.");
2463 return SRT_CMD_NONE;
2464 }
2465
2466 if (bytelen < SRT_CMD_HSRSP_MINSZ)
2467 {
2468 /* Packet smaller than minimum compatible packet size */
2469 LOGC(cnlog.Error, log << CONID() << "HSRSP/rcv: cmd=" << SRT_CMD_HSRSP << "(HSRSP) len=" << bytelen << " invalid");
2470 return SRT_CMD_NONE;
2471 }
2472
2473 // Set this start time in the beginning, regardless as to whether TSBPD is being
2474 // used or not. This must be done in the Initiator as well as Responder. In case when
2475 // agent is sender only (HSv4) this value simply won't be used.
2476
2477 /*
2478 * Compute peer StartTime in our time reference
2479 * This takes time zone, time drift into account.
2480 * Also includes current packet transit time (rtt/2)
2481 */
2482
2483 if (is_zero(m_tsRcvPeerStartTime))
2484 {
2485 // Do not set this time when it's already set, which may be the case
2486 // if the agent has this value already "borrowed" from a master socket
2487 // that was in the group at the time when it was added.
2488 m_tsRcvPeerStartTime = steady_clock::now() - microseconds_from(ts);
2489 HLOGC(cnlog.Debug,
2490 log << CONID()
2491 << "HSRSP/rcv: PEER START TIME not yet defined, setting: " << FormatTime(m_tsRcvPeerStartTime));
2492 }
2493 else
2494 {
2495 HLOGC(cnlog.Debug,
2496 log << CONID()
2497 << "HSRSP/rcv: PEER START TIME already set (derived): " << FormatTime(m_tsRcvPeerStartTime));
2498 }
2499
2500 m_uPeerSrtVersion = srtdata[SRT_HS_VERSION];
2501 m_uPeerSrtFlags = srtdata[SRT_HS_FLAGS];
2502
2503 HLOGC(cnlog.Debug, log << "HSRSP/rcv: Version: " << SrtVersionString(m_uPeerSrtVersion)
2504 << " Flags: SND:" << setw(8) << setfill('0') << hex << m_uPeerSrtFlags
2505 << setw(0) << " (" << SrtFlagString(m_uPeerSrtFlags) << ")");
2506 // Basic version check
2507 if (m_uPeerSrtVersion < m_config.uMinimumPeerSrtVersion)
2508 {
2509 m_RejectReason = SRT_REJ_VERSION;
2510 LOGC(cnlog.Error,
2511 log << CONID() << "HSRSP/rcv: Peer version: " << SrtVersionString(m_uPeerSrtVersion)
2512 << " is too old for requested: " << SrtVersionString(m_config.uMinimumPeerSrtVersion)
2513 << " - REJECTING");
2514 return SRT_CMD_REJECT;
2515 }
2516
2517 if (hsv == CUDT::HS_VERSION_UDT4)
2518 {
2519 // The old HSv4 way: extract just one value and put it under peer.
2520 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDRCV))
2521 {
2522 // TsbPd feature enabled
2523 m_bPeerTsbPd = true;
2524 m_iPeerTsbPdDelay_ms = SRT_HS_LATENCY_LEG::unwrap(srtdata[SRT_HS_LATENCY]);
2525 HLOGC(cnlog.Debug,
2526 log << CONID() << "HSRSP/rcv: LATENCY: Peer/snd:" << m_iPeerTsbPdDelay_ms
2527 << " (Agent: declared:" << m_iTsbPdDelay_ms << " rcv:" << m_iTsbPdDelay_ms << ")");
2528 }
2529 // TSBPDSND isn't set in HSv4 by the RESPONDER, because HSv4 RESPONDER is always RECEIVER.
2530 }
2531 else
2532 {
2533 // HSv5 way: extract the receiver latency and sender latency, if used.
2534
2535 // PEER WILL RECEIVE TSBPD == AGENT SHALL SEND TSBPD.
2536 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDRCV))
2537 {
2538 // TsbPd feature enabled
2539 m_bPeerTsbPd = true;
2540 m_iPeerTsbPdDelay_ms = SRT_HS_LATENCY_RCV::unwrap(srtdata[SRT_HS_LATENCY]);
2541 HLOGC(cnlog.Debug, log << CONID() << "HSRSP/rcv: LATENCY: Peer/snd:" << m_iPeerTsbPdDelay_ms << "ms");
2542 }
2543 else
2544 {
2545 HLOGC(cnlog.Debug, log << CONID() << "HSRSP/rcv: Peer (responder) DOES NOT USE latency");
2546 }
2547
2548 // PEER WILL SEND TSBPD == AGENT SHALL RECEIVE TSBPD.
2549 if (IsSet(m_uPeerSrtFlags, SRT_OPT_TSBPDSND))
2550 {
2551 if (!isOPT_TsbPd())
2552 {
2553 LOGC(cnlog.Warn,
2554 log << CONID()
2555 << "HSRSP/rcv: BUG? Peer (responder) declares sending latency, but Agent turned off TSBPD.");
2556 }
2557 else
2558 {
2559 m_bTsbPd = true; // NOTE: in case of Group TSBPD receiving, this field will be SWITCHED TO m_bGroupTsbPd.
2560 // Take this value as a good deal. In case when the Peer did not "correct" the latency
2561 // because it has TSBPD turned off, just stay with the present value defined in options.
2562 m_iTsbPdDelay_ms = SRT_HS_LATENCY_SND::unwrap(srtdata[SRT_HS_LATENCY]);
2563 HLOGC(cnlog.Debug, log << CONID() << "HSRSP/rcv: LATENCY Agent/rcv: " << m_iTsbPdDelay_ms << "ms");
2564 }
2565 }
2566 }
2567
2568 if ((m_config.uSrtVersion >= SrtVersion(1, 0, 5)) && IsSet(m_uPeerSrtFlags, SRT_OPT_TLPKTDROP))
2569 {
2570 // Too late packets dropping feature supported
2571 m_bPeerTLPktDrop = true;
2572 }
2573
2574 if ((m_config.uSrtVersion >= SrtVersion(1, 1, 0)) && IsSet(m_uPeerSrtFlags, SRT_OPT_NAKREPORT))
2575 {
2576 // Peer will send Periodic NAK Reports
2577 m_bPeerNakReport = true;
2578 }
2579
2580 if (m_config.uSrtVersion >= SrtVersion(1, 2, 0))
2581 {
2582 if (IsSet(m_uPeerSrtFlags, SRT_OPT_REXMITFLG))
2583 {
2584 // Peer will use REXMIT flag in packet retransmission.
2585 m_bPeerRexmitFlag = true;
2586 HLOGP(cnlog.Debug, "HSRSP/rcv: 1.2.0+ Agent understands REXMIT flag and so does peer.");
2587 }
2588 else
2589 {
2590 HLOGP(cnlog.Debug, "HSRSP/rcv: Agent understands REXMIT flag, but PEER DOES NOT");
2591 }
2592 }
2593 else
2594 {
2595 HLOGP(cnlog.Debug, "HSRSP/rcv: <1.2.0 Agent DOESN'T understand REXMIT flag");
2596 }
2597
2598 handshakeDone();
2599
2600 return SRT_CMD_NONE;
2601 }
2602
2603 // This function is called only when the URQ_CONCLUSION handshake has been received from the peer.
2604 bool srt::CUDT::interpretSrtHandshake(const CHandShake& hs,
2605 const CPacket& hspkt,
2606 uint32_t* out_data SRT_ATR_UNUSED,
2607 size_t* pw_len)
2608 {
2609 // Initialize pw_len to 0 to handle the unencrypted case
2610 if (pw_len)
2611 *pw_len = 0;
2612
2613 // The version=0 statement as rejection is used only since HSv5.
2614 // The HSv4 sends the AGREEMENT handshake message with version=0, do not misinterpret it.
2615 if (m_ConnRes.m_iVersion > HS_VERSION_UDT4 && hs.m_iVersion == 0)
2616 {
2617 m_RejectReason = SRT_REJ_PEER;
2618 LOGC(cnlog.Error, log << CONID() << "HS VERSION = 0, meaning the handshake has been rejected.");
2619 return false;
2620 }
2621
2622 if (hs.m_iVersion < HS_VERSION_SRT1)
2623 {
2624 if (m_config.uMinimumPeerSrtVersion && m_config.uMinimumPeerSrtVersion >= SRT_VERSION_FEAT_HSv5)
2625 {
2626 m_RejectReason = SRT_REJ_VERSION;
2627 // This means that a version with minimum 1.3.0 that features HSv5 is required,
2628 // hence all HSv4 clients should be rejected.
2629 LOGP(cnlog.Error, "interpretSrtHandshake: minimum peer version 1.3.0 (HSv5 only), rejecting HSv4 client");
2630 return false;
2631 }
2632 return true; // do nothing
2633 }
2634
2635 // Anyway, check if the handshake contains any extra data.
2636 if (hspkt.getLength() <= CHandShake::m_iContentSize)
2637 {
2638 m_RejectReason = SRT_REJ_ROGUE;
2639 // This would mean that the handshake was at least HSv5, but somehow no extras were added.
2640 // Dismiss it then, however this has to be logged.
2641 LOGC(cnlog.Error, log << CONID() << "HS VERSION=" << hs.m_iVersion << " but no handshake extension found!");
2642 return false;
2643 }
2644
2645 // We still believe it should work, let's check the flags.
2646 const int ext_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(hs.m_iType);
2647 if (ext_flags == 0)
2648 {
2649 m_RejectReason = SRT_REJ_ROGUE;
2650 LOGC(cnlog.Error,
2651 log << CONID() << "HS VERSION=" << hs.m_iVersion << " but no handshake extension flags are set!");
2652 return false;
2653 }
2654
2655 HLOGC(cnlog.Debug,
2656 log << CONID() << "HS VERSION=" << hs.m_iVersion
2657 << " EXTENSIONS: " << CHandShake::ExtensionFlagStr(ext_flags));
2658
2659 // Ok, now find the beginning of an int32_t array that follows the UDT handshake.
2660 uint32_t* p = reinterpret_cast<uint32_t*>(hspkt.m_pcData + CHandShake::m_iContentSize);
2661 size_t size = hspkt.getLength() - CHandShake::m_iContentSize; // Due to previous cond check we grant it's >0
2662
2663 int hsreq_type_cmd SRT_ATR_UNUSED = SRT_CMD_NONE;
2664
2665 if (IsSet(ext_flags, CHandShake::HS_EXT_HSREQ))
2666 {
2667 HLOGC(cnlog.Debug, log << CONID() << "interpretSrtHandshake: extracting HSREQ/RSP type extension");
2668 uint32_t *begin = p;
2669 uint32_t *next = 0;
2670 size_t length = size / sizeof(uint32_t);
2671 size_t blocklen = 0;
2672
2673 for (;;) // this is ONE SHOT LOOP
2674 {
2675 int cmd = FindExtensionBlock(begin, length, (blocklen), (next));
2676
2677 size_t bytelen = blocklen * sizeof(uint32_t);
2678
2679 if (cmd == SRT_CMD_HSREQ)
2680 {
2681 hsreq_type_cmd = cmd;
2682 // Set is the size as it should, then give it for interpretation for
2683 // the proper function.
2684 if (blocklen < SRT_HS_E_SIZE)
2685 {
2686 m_RejectReason = SRT_REJ_ROGUE;
2687 LOGC(cnlog.Error,
2688 log << CONID() << "HS-ext HSREQ found but invalid size: " << bytelen
2689 << " (expected: " << SRT_HS_E_SIZE << ")");
2690 return false; // don't interpret
2691 }
2692
2693 int rescmd = processSrtMsg_HSREQ(begin + 1, bytelen, hspkt.timestamp(), HS_VERSION_SRT1);
2694 // Interpreted? Then it should be responded with SRT_CMD_HSRSP.
2695 if (rescmd != SRT_CMD_HSRSP)
2696 {
2697 // m_RejectReason already set
2698 LOGC(cnlog.Error,
2699 log << CONID() << "interpretSrtHandshake: process HSREQ returned unexpected value " << rescmd);
2700 return false;
2701 }
2702 handshakeDone();
2703 // updateAfterSrtHandshake -> moved to postConnect and processRendezvous
2704 }
2705 else if (cmd == SRT_CMD_HSRSP)
2706 {
2707 hsreq_type_cmd = cmd;
2708 // Set is the size as it should, then give it for interpretation for
2709 // the proper function.
2710 if (blocklen < SRT_HS_E_SIZE)
2711 {
2712 m_RejectReason = SRT_REJ_ROGUE;
2713 LOGC(cnlog.Error,
2714 log << CONID() << "HS-ext HSRSP found but invalid size: " << bytelen
2715 << " (expected: " << SRT_HS_E_SIZE << ")");
2716
2717 return false; // don't interpret
2718 }
2719
2720 int rescmd = processSrtMsg_HSRSP(begin + 1, bytelen, hspkt.timestamp(), HS_VERSION_SRT1);
2721 // Interpreted? Then it should be responded with SRT_CMD_NONE.
2722 // (nothing to be responded for HSRSP, unless there was some kinda problem)
2723 if (rescmd != SRT_CMD_NONE)
2724 {
2725 // Just formally; the current code doesn't seem to return anything else
2726 // (unless it's already set)
2727 if (m_RejectReason == SRT_REJ_UNKNOWN)
2728 m_RejectReason = SRT_REJ_ROGUE;
2729 LOGC(cnlog.Error,
2730 log << CONID() << "interpretSrtHandshake: process HSRSP returned unexpected value " << rescmd);
2731 return false;
2732 }
2733 handshakeDone();
2734 // updateAfterSrtHandshake -> moved to postConnect and processRendezvous
2735 }
2736 else if (cmd == SRT_CMD_NONE)
2737 {
2738 m_RejectReason = SRT_REJ_ROGUE;
2739 LOGC(cnlog.Warn,
2740 log << CONID() << "interpretSrtHandshake: no HSREQ/HSRSP block found in the handshake msg!");
2741 // This means that there can be no more processing done by FindExtensionBlock().
2742 // And we haven't found what we need - otherwise one of the above cases would pass
2743 // and lead to exit this loop immediately.
2744 return false;
2745 }
2746 else
2747 {
2748 // Any other kind of message extracted. Search on.
2749 length -= (next - begin);
2750 begin = next;
2751 if (begin)
2752 continue;
2753 }
2754
2755 break;
2756 }
2757 }
2758
2759 HLOGC(cnlog.Debug, log << CONID() << "interpretSrtHandshake: HSREQ done, checking KMREQ");
2760
2761 // Now check the encrypted
2762
2763 bool encrypted = false;
2764
2765 if (IsSet(ext_flags, CHandShake::HS_EXT_KMREQ))
2766 {
2767 HLOGC(cnlog.Debug, log << CONID() << "interpretSrtHandshake: extracting KMREQ/RSP type extension");
2768
2769 #ifdef SRT_ENABLE_ENCRYPTION
2770 if (!m_pCryptoControl->hasPassphrase())
2771 {
2772 if (m_config.bEnforcedEnc)
2773 {
2774 m_RejectReason = SRT_REJ_UNSECURE;
2775 LOGC(cnlog.Error,
2776 log << CONID()
2777 << "HS KMREQ: Peer declares encryption, but agent does not - rejecting per enforced "
2778 "encryption");
2779 return false;
2780 }
2781
2782 LOGC(cnlog.Warn,
2783 log << CONID()
2784 << "HS KMREQ: Peer declares encryption, but agent does not - still allowing connection.");
2785
2786 // Still allow for connection, and allow Agent to send unencrypted stream to the peer.
2787 // Also normally allow the key to be processed; worst case it will send the failure response.
2788 }
2789
2790 uint32_t *begin = p;
2791 uint32_t *next = 0;
2792 size_t length = size / sizeof(uint32_t);
2793 size_t blocklen = 0;
2794
2795 for (;;) // This is one shot loop, unless REPEATED by 'continue'.
2796 {
2797 int cmd = FindExtensionBlock(begin, length, (blocklen), (next));
2798
2799 HLOGC(cnlog.Debug,
2800 log << CONID() << "interpretSrtHandshake: found extension: (" << cmd << ") "
2801 << MessageTypeStr(UMSG_EXT, cmd));
2802
2803 size_t bytelen = blocklen * sizeof(uint32_t);
2804 if (cmd == SRT_CMD_KMREQ)
2805 {
2806 if (!out_data || !pw_len)
2807 {
2808 m_RejectReason = SRT_REJ_IPE;
2809 LOGC(cnlog.Fatal, log << CONID() << "IPE: HS/KMREQ extracted without passing target buffer!");
2810 return false;
2811 }
2812
2813 int res = m_pCryptoControl->processSrtMsg_KMREQ(begin + 1, bytelen, HS_VERSION_SRT1, m_uPeerSrtVersion,
2814 (out_data), (*pw_len));
2815 if (res != SRT_CMD_KMRSP)
2816 {
2817 m_RejectReason = SRT_REJ_IPE;
2818 // Something went wrong.
2819 HLOGC(cnlog.Debug,
2820 log << CONID() << "interpretSrtHandshake: IPE/EPE KMREQ processing failed - returned "
2821 << res);
2822 return false;
2823 }
2824 if (*pw_len == 1)
2825 {
2826 #ifdef ENABLE_AEAD_API_PREVIEW
2827 if (m_pCryptoControl->m_RcvKmState == SRT_KM_S_BADCRYPTOMODE)
2828 {
2829 // Cryptographic modes mismatch. Not acceptable at all.
2830 m_RejectReason = SRT_REJ_CRYPTO;
2831 LOGC(cnlog.Error,
2832 log << CONID()
2833 << "interpretSrtHandshake: KMREQ result: Bad crypto mode - rejecting");
2834 return false;
2835 }
2836 #endif
2837
2838 // This means that there was an abnormal encryption situation occurred.
2839 // This is inacceptable in case of strict encryption.
2840 if (m_config.bEnforcedEnc)
2841 {
2842 if (m_pCryptoControl->m_RcvKmState == SRT_KM_S_BADSECRET)
2843 {
2844 m_RejectReason = SRT_REJ_BADSECRET;
2845 }
2846 else
2847 {
2848 m_RejectReason = SRT_REJ_UNSECURE;
2849 }
2850 LOGC(cnlog.Error,
2851 log << CONID()
2852 << "interpretSrtHandshake: KMREQ result abnornal - rejecting per enforced encryption");
2853 return false;
2854 }
2855 }
2856 encrypted = true;
2857 }
2858 else if (cmd == SRT_CMD_KMRSP)
2859 {
2860 int res = m_pCryptoControl->processSrtMsg_KMRSP(begin + 1, bytelen, m_uPeerSrtVersion);
2861 if (m_config.bEnforcedEnc && res == -1)
2862 {
2863 if (m_pCryptoControl->m_SndKmState == SRT_KM_S_BADSECRET)
2864 m_RejectReason = SRT_REJ_BADSECRET;
2865 #ifdef ENABLE_AEAD_API_PREVIEW
2866 else if (m_pCryptoControl->m_SndKmState == SRT_KM_S_BADCRYPTOMODE)
2867 m_RejectReason = SRT_REJ_CRYPTO;
2868 #endif
2869 else
2870 m_RejectReason = SRT_REJ_UNSECURE;
2871 LOGC(cnlog.Error,
2872 log << CONID() << "KMRSP failed - rejecting connection as per enforced encryption.");
2873 return false;
2874 }
2875 encrypted = true;
2876 }
2877 else if (cmd == SRT_CMD_NONE)
2878 {
2879 m_RejectReason = SRT_REJ_ROGUE;
2880 LOGC(cnlog.Error, log << CONID() << "HS KMREQ expected - none found!");
2881 return false;
2882 }
2883 else
2884 {
2885 HLOGC(cnlog.Debug, log << CONID() << "interpretSrtHandshake: ... skipping " << MessageTypeStr(UMSG_EXT, cmd));
2886 if (NextExtensionBlock((begin), next, (length)))
2887 continue;
2888 }
2889
2890 break;
2891 }
2892 #else
2893 // When encryption is not enabled at compile time, behave as if encryption wasn't set,
2894 // so accordingly to StrictEncryption flag.
2895
2896 if (m_config.bEnforcedEnc)
2897 {
2898 m_RejectReason = SRT_REJ_UNSECURE;
2899 LOGC(cnlog.Error,
2900 log << CONID()
2901 << "HS KMREQ: Peer declares encryption, but agent didn't enable it at compile time - rejecting "
2902 "per enforced encryption");
2903 return false;
2904 }
2905
2906 LOGC(cnlog.Warn,
2907 log << CONID()
2908 << "HS KMREQ: Peer declares encryption, but agent didn't enable it at compile time - still allowing "
2909 "connection.");
2910 encrypted = true;
2911 #endif
2912 }
2913
2914 bool have_congctl = false;
2915 bool have_filter = false;
2916 string agsm = m_config.sCongestion.str();
2917 if (agsm == "")
2918 {
2919 agsm = "live";
2920 m_config.sCongestion.set("live", 4);
2921 }
2922
2923 bool have_group SRT_ATR_UNUSED = false;
2924
2925 if (IsSet(ext_flags, CHandShake::HS_EXT_CONFIG))
2926 {
2927 HLOGC(cnlog.Debug, log << CONID() << "interpretSrtHandshake: extracting various CONFIG extensions");
2928
2929 uint32_t *begin = p;
2930 uint32_t *next = 0;
2931 size_t length = size / sizeof(uint32_t);
2932 size_t blocklen = 0;
2933
2934 for (;;) // This is one shot loop, unless REPEATED by 'continue'.
2935 {
2936 int cmd = FindExtensionBlock(begin, length, (blocklen), (next));
2937
2938 HLOGC(cnlog.Debug,
2939 log << CONID() << "interpretSrtHandshake: found extension: (" << cmd << ") "
2940 << MessageTypeStr(UMSG_EXT, cmd));
2941
2942 const size_t bytelen = blocklen * sizeof(uint32_t);
2943 if (cmd == SRT_CMD_SID)
2944 {
2945 if (!bytelen || bytelen > CSrtConfig::MAX_SID_LENGTH)
2946 {
2947 LOGC(cnlog.Error,
2948 log << CONID() << "interpretSrtHandshake: STREAMID length " << bytelen << " is 0 or > "
2949 << +CSrtConfig::MAX_SID_LENGTH << " - PROTOCOL ERROR, REJECTING");
2950 return false;
2951 }
2952 // Copied through a cleared array. This is because the length is aligned to 4
2953 // where the padding is filled by zero bytes. For the case when the string is
2954 // exactly of a 4-divisible length, we make a big array with maximum allowed size
2955 // filled with zeros. Copying to this array should then copy either only the valid
2956 // characters of the string (if the length is divisible by 4), or the string with
2957 // padding zeros. In all these cases in the resulting array we should have all
2958 // subsequent characters of the string plus at least one '\0' at the end. This will
2959 // make it a perfect NUL-terminated string, to be used to initialize a string.
2960 char target[CSrtConfig::MAX_SID_LENGTH + 1];
2961 memset((target), 0, CSrtConfig::MAX_SID_LENGTH + 1);
2962 memcpy((target), begin + 1, bytelen);
2963
2964 // Un-swap on big endian machines
2965 ItoHLA((uint32_t *)target, (uint32_t *)target, blocklen);
2966
2967 // Determine the length by dropping all but one of terminal NUL characters.
2968
2969 size_t targetlen = bytelen;
2970 for (size_t pos = bytelen - 1; pos != 0; --pos)
2971 {
2972 if (target[pos])
2973 {
2974 // Found the first non-NUL character backwards
2975 // So the length is pos+1
2976 targetlen = pos + 1;
2977 break;
2978 }
2979 }
2980
2981 m_config.sStreamName.set(target, targetlen);
2982 HLOGC(cnlog.Debug,
2983 log << CONID() << "CONNECTOR'S REQUESTED SID [" << m_config.sStreamName.c_str()
2984 << "] (bytelen=" << bytelen << " blocklen=" << blocklen << ")");
2985 }
2986 else if (cmd == SRT_CMD_CONGESTION)
2987 {
2988 if (have_congctl)
2989 {
2990 m_RejectReason = SRT_REJ_ROGUE;
2991 LOGC(cnlog.Error, log << CONID() << "CONGCTL BLOCK REPEATED!");
2992 return false;
2993 }
2994
2995 if (!bytelen || bytelen > CSrtConfig::MAX_CONG_LENGTH)
2996 {
2997 LOGC(cnlog.Error,
2998 log << CONID() << "interpretSrtHandshake: CONGESTION-control type length " << bytelen
2999 << " is 0 or > " << +CSrtConfig::MAX_CONG_LENGTH << " - PROTOCOL ERROR, REJECTING");
3000 return false;
3001 }
3002 // Declare that congctl has been received
3003 have_congctl = true;
3004
3005 char target[CSrtConfig::MAX_CONG_LENGTH + 1];
3006 memset((target), 0, CSrtConfig::MAX_CONG_LENGTH + 1);
3007 memcpy((target), begin + 1, bytelen);
3008 // Un-swap on big endian machines
3009 ItoHLA((uint32_t *)target, (uint32_t *)target, blocklen);
3010
3011 string sm = target;
3012
3013 // As the congctl has been declared by the peer,
3014 // check if your congctl is compatible.
3015 // sm cannot be empty, but the agent's sm can be empty meaning live.
3016 if (sm != agsm)
3017 {
3018 m_RejectReason = SRT_REJ_CONGESTION;
3019 LOGC(cnlog.Error,
3020 log << CONID() << "PEER'S CONGCTL '" << sm << "' does not match AGENT'S CONGCTL '" << agsm
3021 << "'");
3022 return false;
3023 }
3024
3025 HLOGC(cnlog.Debug,
3026 log << CONID() << "CONNECTOR'S CONGCTL [" << sm << "] (bytelen=" << bytelen
3027 << " blocklen=" << blocklen << ")");
3028 }
3029 else if (cmd == SRT_CMD_FILTER)
3030 {
3031 if (have_filter)
3032 {
3033 m_RejectReason = SRT_REJ_FILTER;
3034 LOGC(cnlog.Error, log << CONID() << "FILTER BLOCK REPEATED!");
3035 return false;
3036 }
3037
3038 if (!bytelen || bytelen > CSrtConfig::MAX_PFILTER_LENGTH)
3039 {
3040 LOGC(cnlog.Error,
3041 log << CONID() << "interpretSrtHandshake: packet-filter type length " << bytelen
3042 << " is 0 or > " << +CSrtConfig::MAX_PFILTER_LENGTH << " - PROTOCOL ERROR, REJECTING");
3043 return false;
3044 }
3045 // Declare that filter has been received
3046 have_filter = true;
3047
3048 char target[CSrtConfig::MAX_PFILTER_LENGTH + 1];
3049 memset((target), 0, CSrtConfig::MAX_PFILTER_LENGTH + 1);
3050 memcpy((target), begin + 1, bytelen);
3051 // Un-swap on big endian machines
3052 ItoHLA((uint32_t *)target, (uint32_t *)target, blocklen);
3053
3054 string fltcfg = target;
3055
3056 HLOGC(cnlog.Debug,
3057 log << CONID() << "PEER'S FILTER CONFIG [" << fltcfg << "] (bytelen=" << bytelen
3058 << " blocklen=" << blocklen << ")");
3059
3060 if (!checkApplyFilterConfig(fltcfg))
3061 {
3062 m_RejectReason = SRT_REJ_FILTER;
3063 LOGC(cnlog.Error, log << CONID() << "PEER'S FILTER CONFIG [" << fltcfg << "] has been rejected");
3064 return false;
3065 }
3066 }
3067 #if ENABLE_BONDING
3068 else if ( cmd == SRT_CMD_GROUP )
3069 {
3070 // Note that this will fire in both cases:
3071 // - When receiving HS request from the Initiator, which belongs to a group, and agent must
3072 // create the mirror group on his side (or join the existing one, if there's already
3073 // a mirror group for that group ID).
3074 // - When receiving HS response from the Responder, with its mirror group ID, so the agent
3075 // must put the group into his peer group data
3076 int32_t groupdata[GRPD_E_SIZE] = {};
3077 if (bytelen < GRPD_MIN_SIZE * GRPD_FIELD_SIZE || bytelen % GRPD_FIELD_SIZE)
3078 {
3079 m_RejectReason = SRT_REJ_ROGUE;
3080 LOGC(cnlog.Error, log << CONID() << "PEER'S GROUP wrong size: " << (bytelen/GRPD_FIELD_SIZE));
3081 return false;
3082 }
3083 size_t used_bytelen = std::min(bytelen, sizeof(groupdata));
3084 size_t groupdata_size = used_bytelen / GRPD_FIELD_SIZE;
3085
3086 memcpy(groupdata, begin+1, used_bytelen);
3087 if (!interpretGroup(groupdata, groupdata_size, hsreq_type_cmd) )
3088 {
3089 // m_RejectReason handled inside interpretGroup().
3090 return false;
3091 }
3092
3093 have_group = true;
3094 HLOGC(cnlog.Debug,
3095 log << CONID() << "CONNECTOR'S PEER GROUP [" << groupdata[0] << "] (bytelen=" << bytelen
3096 << " blocklen=" << blocklen << ")");
3097 }
3098 #endif
3099 else if (cmd == SRT_CMD_NONE)
3100 {
3101 break;
3102 }
3103 else
3104 {
3105 // Found some block that is not interesting here. Skip this and get the next one.
3106 HLOGC(cnlog.Debug,
3107 log << CONID() << "interpretSrtHandshake: ... skipping " << MessageTypeStr(UMSG_EXT, cmd));
3108 }
3109
3110 if (!NextExtensionBlock((begin), next, (length)))
3111 break;
3112 }
3113 }
3114
3115 // Post-checks
3116 // Check if peer declared encryption
3117 if (!encrypted && m_config.CryptoSecret.len > 0)
3118 {
3119 if (m_config.bEnforcedEnc)
3120 {
3121 m_RejectReason = SRT_REJ_UNSECURE;
3122 LOGC(cnlog.Error,
3123 log << CONID()
3124 << "HS EXT: Agent declares encryption, but Peer does not - rejecting connection per "
3125 "enforced encryption.");
3126 return false;
3127 }
3128
3129 LOGC(cnlog.Warn,
3130 log << CONID()
3131 << "HS EXT: Agent declares encryption, but Peer does not (Agent can still receive unencrypted packets "
3132 "from Peer).");
3133
3134 // This is required so that the sender is still allowed to send data, when encryption is required,
3135 // just this will be for waste because the receiver won't decrypt them anyway.
3136 m_pCryptoControl->createFakeSndContext();
3137 m_pCryptoControl->m_SndKmState = SRT_KM_S_NOSECRET; // Because Peer did not send KMX, though Agent has pw
3138 m_pCryptoControl->m_RcvKmState = SRT_KM_S_UNSECURED; // Because Peer has no PW, as has sent no KMREQ.
3139 return true;
3140 }
3141
3142 // If agent has set some nondefault congctl, then congctl is expected from the peer.
3143 if (agsm != "live" && !have_congctl)
3144 {
3145 m_RejectReason = SRT_REJ_CONGESTION;
3146 LOGC(cnlog.Error,
3147 log << CONID() << "HS EXT: Agent uses '" << agsm
3148 << "' congctl, but peer DID NOT DECLARE congctl (assuming 'live').");
3149 return false;
3150 }
3151
3152 // Check if the filter was configured on agent,
3153 // but the peer responded with no filter.
3154 if (!m_config.sPacketFilterConfig.empty() && !have_filter)
3155 {
3156 if (m_SrtHsSide == HSD_INITIATOR)
3157 {
3158 // IMPORTANT:
3159 // If agent didn't set the packet filter in the configuration, the peer
3160 // could have responded with its own configuration and enforce the packet filter
3161 // setting here locally. In this case the sPacketFilterConfig will not be empty
3162 // at the moment, even if it was empty initially.
3163
3164 // The situation caught here is that the configuration for packet filter was set,
3165 // but the peer responded as if it didn't understand it, or simply didn't accept it.
3166 // In such a situation the configuration should be rejected because if the
3167 // peer doesn't support packet filter, the caller side should not have set it.
3168 m_RejectReason = SRT_REJ_FILTER;
3169 LOGC(cnlog.Error, log << CONID()
3170 << "HS EXT: Agent has configured packetfilter, but peer didn't respond.");
3171 return false;
3172 }
3173
3174 // Responder: we are the listener that has configured packetfilter,
3175 // but the caller didn't request packetfilter.
3176 m_config.sPacketFilterConfig.set("");
3177 LOGC(cnlog.Warn, log << CONID() << "HS EXT: Agent has configured packetfilter, but peer didn't request it");
3178 }
3179
3180 #if ENABLE_BONDING
3181 // m_GroupOf and locking info: NULL check won't hurt here. If the group
3182 // was deleted in the meantime, it will be found out later anyway and result with error.
3183 if (m_SrtHsSide == HSD_INITIATOR && m_parent->m_GroupOf)
3184 {
3185 // XXX Later probably needs to check if this group REQUIRES the group
3186 // response. Currently this implements the bonding-category group, and this
3187 // always requires that the listener respond with the group id, otherwise
3188 // it probably DID NOT UNDERSTAND THE GROUP, so the connection should be rejected.
3189 if (!have_group)
3190 {
3191 m_RejectReason = SRT_REJ_GROUP;
3192 LOGC(cnlog.Error,
3193 log << CONID()
3194 << "HS EXT: agent is a group member, but the listener did not respond with group ID. Rejecting.");
3195 return false;
3196 }
3197 }
3198 #endif
3199
3200 // Ok, finished, for now.
3201 return true;
3202 }
3203
3204 bool srt::CUDT::checkApplyFilterConfig(const std::string &confstr)
3205 {
3206 SrtFilterConfig cfg;
3207 if (!ParseFilterConfig(confstr, (cfg)))
3208 return false;
3209
3210 // Now extract the type, if present, and
3211 // check if you have this type of corrector available.
3212 if (!PacketFilter::correctConfig(cfg))
3213 return false;
3214
3215 string thisconf = m_config.sPacketFilterConfig.str();
3216
3217 // Now parse your own string, if you have it.
3218 if (thisconf != "")
3219 {
3220 // - for rendezvous, both must be exactly the same (it's unspecified, which will be the first one)
3221 if (m_config.bRendezvous && thisconf != confstr)
3222 {
3223 return false;
3224 }
3225
3226 SrtFilterConfig mycfg;
3227 if (!ParseFilterConfig(thisconf, (mycfg)))
3228 return false;
3229
3230 // Check only if both have set a filter of the same type.
3231 if (mycfg.type != cfg.type)
3232 return false;
3233
3234 // If so, then:
3235 // - for caller-listener configuration, accept the listener version.
3236 if (m_SrtHsSide == HSD_INITIATOR)
3237 {
3238 // This is a caller, this should apply all parameters received
3239 // from the listener, forcefully.
3240 for (map<string, string>::iterator x = cfg.parameters.begin(); x != cfg.parameters.end(); ++x)
3241 {
3242 mycfg.parameters[x->first] = x->second;
3243 }
3244 }
3245 else
3246 {
3247 if (!CheckFilterCompat((mycfg), cfg))
3248 return false;
3249 }
3250
3251 HLOGC(cnlog.Debug,
3252 log << CONID() << "checkApplyFilterConfig: param: LOCAL: " << Printable(mycfg.parameters)
3253 << " FORGN: " << Printable(cfg.parameters));
3254
3255 ostringstream myos;
3256 myos << mycfg.type;
3257 for (map<string, string>::iterator x = mycfg.parameters.begin(); x != mycfg.parameters.end(); ++x)
3258 {
3259 myos << "," << x->first << ":" << x->second;
3260 }
3261
3262 m_config.sPacketFilterConfig.set(myos.str());
3263
3264 HLOGC(cnlog.Debug, log << CONID() << "checkApplyFilterConfig: Effective config: " << thisconf);
3265 }
3266 else
3267 {
3268 // Take the foreign configuration as a good deal.
3269 HLOGC(cnlog.Debug, log << CONID() << "checkApplyFilterConfig: Good deal config: " << thisconf);
3270 m_config.sPacketFilterConfig.set(confstr);
3271 }
3272
3273 // XXX Using less maximum payload size of IPv4 and IPv6; this is only about the payload size
3274 // for live.
3275 size_t efc_max_payload_size = SRT_LIVE_MAX_PLSIZE - cfg.extra_size;
3276 if (m_config.zExpPayloadSize > efc_max_payload_size)
3277 {
3278 LOGC(cnlog.Warn,
3279 log << CONID() << "Due to filter-required extra " << cfg.extra_size << " bytes, SRTO_PAYLOADSIZE fixed to "
3280 << efc_max_payload_size << " bytes");
3281 m_config.zExpPayloadSize = efc_max_payload_size;
3282 }
3283
3284 return true;
3285 }
3286
3287 #if ENABLE_BONDING
3288 bool srt::CUDT::interpretGroup(const int32_t groupdata[], size_t data_size SRT_ATR_UNUSED, int hsreq_type_cmd SRT_ATR_UNUSED)
3289 {
3290 // `data_size` isn't checked because we believe it's checked earlier.
3291 // Also this code doesn't predict to get any other format than the official one,
3292 // so there are only data in two fields. Passing this argument is only left
3293 // for consistency and possibly changes in future.
3294
3295 // We are granted these two fields do exist
3296 SRTSOCKET grpid = groupdata[GRPD_GROUPID];
3297 uint32_t gd = groupdata[GRPD_GROUPDATA];
3298
3299 SRT_GROUP_TYPE gtp = SRT_GROUP_TYPE(SrtHSRequest::HS_GROUP_TYPE::unwrap(gd));
3300 int link_weight = SrtHSRequest::HS_GROUP_WEIGHT::unwrap(gd);
3301 uint32_t link_flags = SrtHSRequest::HS_GROUP_FLAGS::unwrap(gd);
3302
3303 if (m_config.iGroupConnect == 0)
3304 {
3305 m_RejectReason = SRT_REJ_GROUP;
3306 LOGC(cnlog.Error, log << CONID() << "HS/GROUP: this socket is not allowed for group connect.");
3307 return false;
3308 }
3309
3310 // This is called when the group type has come in the handshake is invalid.
3311 if (gtp >= SRT_GTYPE_E_END)
3312 {
3313 m_RejectReason = SRT_REJ_GROUP;
3314 LOGC(cnlog.Error,
3315 log << CONID() << "HS/GROUP: incorrect group type value " << gtp << " (max is " << SRT_GTYPE_E_END << ")");
3316 return false;
3317 }
3318
3319 if ((grpid & SRTGROUP_MASK) == 0)
3320 {
3321 m_RejectReason = SRT_REJ_ROGUE;
3322 LOGC(cnlog.Error, log << CONID() << "HS/GROUP: socket ID passed as a group ID is not a group ID");
3323 return false;
3324 }
3325
3326 // We have the group, now take appropriate action.
3327 // The redundancy group requires to make a mirror group
3328 // on this side, and the newly created socket should
3329 // be made belong to it.
3330
3331 #if ENABLE_HEAVY_LOGGING
3332 static const char* hs_side_name[] = {"draw", "initiator", "responder"};
3333 HLOGC(cnlog.Debug,
3334 log << CONID() << "interpretGroup: STATE: HsSide=" << hs_side_name[m_SrtHsSide]
3335 << " HS MSG: " << MessageTypeStr(UMSG_EXT, hsreq_type_cmd) << " $" << grpid << " type=" << gtp
3336 << " weight=" << link_weight << " flags=0x" << std::hex << link_flags);
3337 #endif
3338
3339 // XXX Here are two separate possibilities:
3340 //
3341 // 1. This is a HS request and this is a newly created socket not yet part of any group.
3342 // 2. This is a HS response and the group is the mirror group for the group to which the agent belongs; we need to pin the mirror group as peer group
3343 //
3344 // These two situations can be only distinguished by the HS side.
3345 if (m_SrtHsSide == HSD_DRAW)
3346 {
3347 m_RejectReason = SRT_REJ_IPE;
3348 LOGC(cnlog.Error,
3349 log << CONID()
3350 << "IPE: interpretGroup: The HS side should have been already decided; it's still DRAW. Grouping "
3351 "rejected.");
3352 return false;
3353 }
3354
3355 if (m_SrtHsSide == HSD_INITIATOR)
3356 {
3357 // Here we'll be only using a pre-existing group, so shared is enough.
3358 SharedLock guard_group_existence (uglobal().m_GlobControlLock);
3359
3360 // Recheck broken flags after acquisition
3361 if (m_bClosing || m_bBroken)
3362 {
3363 m_RejectReason = SRT_REJ_CLOSE;
3364 LOGC(cnlog.Error, log << CONID() << "interpretGroup: closure during handshake, interrupting");
3365 return false;
3366 }
3367
3368 // This is a connection initiator that has requested the peer to make a
3369 // mirror group and join it, then respond its mirror group id. The
3370 // `grpid` variable contains this group ID; map this as your peer
3371 // group. If your group already has a peer group set, check if this is
3372 // the same id, otherwise the connection should be rejected.
3373
3374 // So, first check the group of the current socket and see if a peer is set.
3375 CUDTGroup* pg = m_parent->m_GroupOf;
3376 if (!pg)
3377 {
3378 // This means that the responder has responded with a group membership,
3379 // but the initiator did not request any group membership presence.
3380 // Currently impossible situation.
3381 m_RejectReason = SRT_REJ_IPE;
3382 LOGC(cnlog.Error, log << CONID() << "IPE: HS/RSP: group membership responded, while not requested.");
3383 return false;
3384 }
3385
3386 {
3387 // XXX Consider moving this to another function to avoid
3388 // exposing group lock.
3389
3390 // Group existence is guarded, so we can now lock the group as well.
3391 ScopedLock gl(*pg->exp_groupLock());
3392
3393 // Now we know the group exists, but it might still be closed
3394 if (pg->closing())
3395 {
3396 LOGC(cnlog.Error, log << CONID() << "HS/RSP: group was closed in the process, can't continue connecting");
3397 m_RejectReason = SRT_REJ_IPE;
3398 return false;
3399 }
3400
3401 SRTSOCKET peer = pg->peerid();
3402 if (peer == SRT_INVALID_SOCK)
3403 {
3404 // This is the first connection within this group, so this group
3405 // has just been informed about the peer membership. Accept it.
3406 pg->set_peerid(grpid);
3407 HLOGC(cnlog.Debug,
3408 log << CONID() << "HS/RSP: group $" << pg->id() << " -> peer $" << pg->peerid()
3409 << ", copying characteristic data");
3410
3411 // The call to syncWithSocket is copying
3412 // some interesting data from the first connected
3413 // socket. This should be only done for the first successful connection.
3414 pg->syncWithSocket(*this, HSD_INITIATOR);
3415 }
3416 // Otherwise the peer id must be the same as existing, otherwise
3417 // this group is considered already bound to another peer group.
3418 // (Note that the peer group is peer-specific, and peer id numbers
3419 // may repeat among sockets connected to groups established on
3420 // different peers).
3421 else if (peer != grpid)
3422 {
3423 LOGC(cnlog.Error,
3424 log << CONID() << "IPE: HS/RSP: group membership responded for peer $" << grpid
3425 << " but the current socket's group $" << pg->id() << " has already a peer $" << peer);
3426 m_RejectReason = SRT_REJ_GROUP;
3427 return false;
3428 }
3429 else
3430 {
3431 HLOGC(cnlog.Debug,
3432 log << CONID() << "HS/RSP: group $" << pg->id() << " ALREADY MAPPED to peer mirror $"
3433 << pg->peerid());
3434 }
3435 }
3436 m_parent->m_GroupOf->debugGroup();
3437 }
3438 else
3439 {
3440 // Here we'll be potentially creating a new group, hence exclusive is needed.
3441 ExclusiveLock guard_group_existence (uglobal().m_GlobControlLock);
3442
3443 // Recheck broken flags after acquisition
3444 if (m_bClosing || m_bBroken)
3445 {
3446 m_RejectReason = SRT_REJ_CLOSE;
3447 LOGC(cnlog.Error, log << CONID() << "interpretGroup: closure during handshake, interrupting");
3448 return false;
3449 }
3450
3451 // This is a connection responder that has been requested to make a
3452 // mirror group and join it. Later on, the HS response will be sent
3453 // and its group ID will be added to the HS extensions as mirror group
3454 // ID to the peer.
3455
3456 SRTSOCKET lgid = makeMePeerOf(grpid, gtp, link_flags);
3457 if (lgid == 0)
3458 return true; // already done
3459
3460 if (lgid == SRT_INVALID_SOCK)
3461 {
3462 // NOTE: This error currently isn't reported by makeMePeerOf,
3463 // so this is left to handle a possible error introduced in future.
3464 m_RejectReason = SRT_REJ_GROUP;
3465 return false; // error occurred
3466 }
3467
3468 if (!m_parent->m_GroupOf)
3469 {
3470 // Strange, we just added it...
3471 m_RejectReason = SRT_REJ_IPE;
3472 LOGC(cnlog.Fatal, log << CONID() << "IPE: socket not in group after adding to it");
3473 return false;
3474 }
3475
3476 groups::SocketData* f = m_parent->m_GroupMemberData;
3477
3478 f->weight = link_weight;
3479 f->agent = m_parent->m_SelfAddr;
3480 f->peer = m_PeerAddr;
3481 m_parent->m_GroupOf->debugGroup();
3482 }
3483
3484 // That's all. For specific things concerning group
3485 // types, this will be later.
3486 return true;
3487 }
3488 #endif
3489
3490 #if ENABLE_BONDING
3491 // NOTE: This function is called only in one place and it's done
3492 // exclusively on the listener side (HSD_RESPONDER, HSv5+).
3493
3494 // [[using locked(s_UDTUnited.m_GlobControlLock)]]
3495 SRTSOCKET srt::CUDT::makeMePeerOf(SRTSOCKET peergroup, SRT_GROUP_TYPE gtp, uint32_t link_flags)
3496 {
3497 // Note: This function will lock pg->m_GroupLock!
3498
3499 CUDTSocket* s = m_parent;
3500
3501 // Note that the socket being worked out here is about to be returned
3502 // from `srt_accept` call, and until this moment it will be inaccessible
3503 // for any other thread. It is then assumed that no other thread is accessing
3504 // it right now so there's no need to lock s->m_ControlLock.
3505
3506 // Check if there exists a group that this one is a peer of.
3507 CUDTGroup* gp = uglobal().findPeerGroup_LOCKED(peergroup);
3508 bool was_empty = true;
3509 if (gp)
3510 {
3511 if (gp->type() != gtp)
3512 {
3513 LOGC(gmlog.Error,
3514 log << CONID() << "HS: GROUP TYPE COLLISION: peer group=$" << peergroup << " type " << gtp
3515 << " agent group=$" << gp->id() << " type" << gp->type());
3516 return -1;
3517 }
3518
3519 HLOGC(gmlog.Debug, log << CONID() << "makeMePeerOf: group for peer=$" << peergroup << " found: $" << gp->id());
3520
3521 if (!gp->groupEmpty())
3522 was_empty = false;
3523 }
3524 else
3525 {
3526 try
3527 {
3528 gp = &newGroup(gtp);
3529 }
3530 catch (...)
3531 {
3532 // Expected exceptions are only those referring to system resources
3533 return -1;
3534 }
3535
3536 if (!gp->applyFlags(link_flags, m_SrtHsSide))
3537 {
3538 // Wrong settings. Must reject. Delete group.
3539 uglobal().deleteGroup_LOCKED(gp);
3540 return -1;
3541 }
3542
3543 gp->set_peerid(peergroup);
3544 gp->deriveSettings(this);
3545
3546 // This can only happen on a listener (it's only called on a site that is
3547 // HSD_RESPONDER), so it was a response for a groupwise connection.
3548 // Therefore such a group shall always be considered opened.
3549 gp->setOpen();
3550
3551 HLOGC(gmlog.Debug,
3552 log << CONID() << "makeMePeerOf: no group has peer=$" << peergroup << " - creating new mirror group $"
3553 << gp->id());
3554 }
3555
3556 {
3557 ScopedLock glock (*gp->exp_groupLock());
3558 if (gp->closing())
3559 {
3560 HLOGC(gmlog.Debug, log << CONID() << "makeMePeerOf: group $" << gp->id() << " is being closed, can't process");
3561 }
3562
3563 if (was_empty)
3564 {
3565 gp->syncWithSocket(s->core(), HSD_RESPONDER);
3566 }
3567 }
3568
3569 // Setting non-blocking reading for group socket.
3570 s->core().m_config.bSynRecving = false;
3571 s->core().m_config.bSynSending = false;
3572
3573 // Copy of addSocketToGroup. No idea how many parts could be common, not much.
3574
3575 // Check if the socket already is in the group
3576 groups::SocketData* f;
3577 if (gp->contains(m_SocketID, (f)))
3578 {
3579 // XXX This is internal error. Report it, but continue
3580 // (A newly created socket from acceptAndRespond should not have any group membership yet)
3581 LOGC(gmlog.Error, log << CONID() << "IPE (non-fatal): the socket is in the group, but has no clue about it!");
3582 s->m_GroupOf = gp;
3583 s->m_GroupMemberData = f;
3584 return 0;
3585 }
3586
3587 s->m_GroupMemberData = gp->add(groups::prepareSocketData(s));
3588 s->m_GroupOf = gp;
3589 m_HSGroupType = gtp;
3590
3591 // Record the remote address in the group data.
3592
3593 return gp->id();
3594 }
3595
3596 void srt::CUDT::synchronizeWithGroup(CUDTGroup* gp)
3597 {
3598 ScopedLock gl (*gp->exp_groupLock());
3599
3600 // We have blocked here the process of connecting a new
3601 // socket and adding anything new to the group, so no such
3602 // thing may happen in the meantime.
3603 steady_clock::time_point start_time, peer_start_time;
3604
3605 start_time = m_stats.tsStartTime;
3606 peer_start_time = m_tsRcvPeerStartTime;
3607
3608 if (!gp->applyGroupTime((start_time), (peer_start_time)))
3609 {
3610 HLOGC(gmlog.Debug,
3611 log << CONID() << "synchronizeWithGroup: ST=" << FormatTime(m_stats.tsStartTime) << " -> "
3612 << FormatTime(start_time) << " PST=" << FormatTime(m_tsRcvPeerStartTime) << " -> "
3613 << FormatTime(peer_start_time));
3614 m_stats.tsStartTime = start_time;
3615 m_tsRcvPeerStartTime = peer_start_time;
3616 }
3617 else
3618 {
3619 // This was the first connected socket and it defined start time.
3620 HLOGC(gmlog.Debug,
3621 log << CONID() << "synchronizeWithGroup: ST=" << FormatTime(m_stats.tsStartTime)
3622 << " PST=" << FormatTime(m_tsRcvPeerStartTime));
3623 }
3624
3625 steady_clock::time_point rcv_buffer_time_base;
3626 bool rcv_buffer_wrap_period = false;
3627 steady_clock::duration rcv_buffer_udrift(0);
3628 if (m_bTsbPd && gp->getBufferTimeBase(this, (rcv_buffer_time_base), (rcv_buffer_wrap_period), (rcv_buffer_udrift)))
3629 {
3630 // We have at least one socket in the group, each socket should have
3631 // the value of the timebase set exactly THE SAME.
3632
3633 // In case when we have the following situation:
3634
3635 // - the existing link is before [LAST30] (so wrap period is off)
3636 // - the new link gets the timestamp from [LAST30] range
3637 // --> this will be recognized as entering the wrap period, next
3638 // timebase will get added a segment to this value
3639 //
3640 // The only dangerous situations could be when one link gets
3641 // timestamps from the [FOLLOWING30] and the other in [FIRST30],
3642 // but between them there's a 30s distance, considered large enough
3643 // time to not fill a network window.
3644 enterCS(m_RecvLock);
3645 m_pRcvBuffer->applyGroupTime(rcv_buffer_time_base, rcv_buffer_wrap_period, m_iTsbPdDelay_ms * 1000, rcv_buffer_udrift);
3646 m_pRcvBuffer->setPeerRexmitFlag(m_bPeerRexmitFlag);
3647 leaveCS(m_RecvLock);
3648
3649 HLOGC(gmlog.Debug, log << "AFTER HS: Set Rcv TsbPd mode: delay="
3650 << (m_iTsbPdDelay_ms/1000) << "." << (m_iTsbPdDelay_ms%1000)
3651 << "s GROUP TIME BASE: " << FormatTime(rcv_buffer_time_base)
3652 << " (" << (rcv_buffer_wrap_period ? "" : "NOT") << " WRAP PERIOD)");
3653 }
3654 else
3655 {
3656 HLOGC(gmlog.Debug,
3657 log << CONID() << "AFTER HS: (GROUP, but "
3658 << (m_bTsbPd ? "FIRST SOCKET is initialized normally)" : "no TSBPD set)"));
3659 updateSrtRcvSettings();
3660 }
3661
3662 // This function currently does nothing, just left for consistency
3663 // with updateAfterSrtHandshake().
3664 updateSrtSndSettings();
3665
3666 // These are the values that are normally set initially by setters.
3667 int32_t snd_isn = m_iSndLastAck, rcv_isn = m_iRcvLastAck;
3668 if (!gp->applyGroupSequences(m_SocketID, (snd_isn), (rcv_isn)))
3669 {
3670 HLOGC(gmlog.Debug,
3671 log << CONID() << "synchronizeWithGroup: DERIVED ISN: RCV=%" << m_iRcvLastAck << " -> %" << rcv_isn
3672 << " (shift by " << CSeqNo::seqcmp(rcv_isn, m_iRcvLastAck) << ") SND=%" << m_iSndLastAck
3673 << " -> %" << snd_isn << " (shift by " << CSeqNo::seqcmp(snd_isn, m_iSndLastAck) << ")");
3674 setInitialRcvSeq(rcv_isn);
3675 setInitialSndSeq(snd_isn);
3676 }
3677 else
3678 {
3679 HLOGC(gmlog.Debug,
3680 log << CONID() << "synchronizeWithGroup: DEFINED ISN: RCV=%" << m_iRcvLastAck << " SND=%"
3681 << m_iSndLastAck);
3682 }
3683 }
3684 #endif
3685
3686 void srt::CUDT::startConnect(const sockaddr_any& serv_addr, int32_t forced_isn)
3687 {
3688 ScopedLock cg (m_ConnectionLock);
3689
3690 HLOGC(aclog.Debug, log << CONID() << "startConnect: -> " << serv_addr.str()
3691 << (m_config.bSynRecving ? " (SYNCHRONOUS)" : " (ASYNCHRONOUS)") << "...");
3692
3693 if (!m_bOpened)
3694 throw CUDTException(MJ_NOTSUP, MN_NONE, 0);
3695
3696 if (m_bListening)
3697 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
3698
3699 if (m_bConnecting || m_bConnected)
3700 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
3701
3702 // record peer/server address
3703 m_PeerAddr = serv_addr;
3704
3705 // register this socket in the rendezvous queue
3706 // RendezevousQueue is used to temporarily store incoming handshake, non-rendezvous connections also require this
3707 // function
3708 steady_clock::duration ttl = m_config.tdConnTimeOut;
3709
3710 if (m_config.bRendezvous)
3711 ttl *= 10;
3712
3713 const steady_clock::time_point ttl_time = steady_clock::now() + ttl;
3714 m_pRcvQueue->registerConnector(m_SocketID, this, serv_addr, ttl_time);
3715
3716 // The m_iType is used in the INDUCTION for nothing. This value is only regarded
3717 // in CONCLUSION handshake, however this must be created after the handshake version
3718 // is already known. UDT_DGRAM is the value that was the only valid in the old SRT
3719 // with HSv4 (it supported only live transmission), for HSv5 it will be changed to
3720 // handle handshake extension flags.
3721 m_ConnReq.m_iType = UDT_DGRAM;
3722
3723 // Auto mode for Caller and in Rendezvous is equivalent to CIPHER_MODE_AES_CTR.
3724 if (m_config.iCryptoMode == CSrtConfig::CIPHER_MODE_AUTO)
3725 m_config.iCryptoMode = CSrtConfig::CIPHER_MODE_AES_CTR;
3726
3727 // This is my current configuration
3728 if (m_config.bRendezvous)
3729 {
3730 // For rendezvous, use version 5 in the waveahand and the cookie.
3731 // In case when you get the version 4 waveahand, simply switch to
3732 // the legacy HSv4 rendezvous and this time send version 4 CONCLUSION.
3733
3734 // The HSv4 client simply won't check the version nor the cookie and it
3735 // will be sending its waveahands with version 4. Only when the party
3736 // has sent version 5 waveahand should the agent continue with HSv5
3737 // rendezvous.
3738 m_ConnReq.m_iVersion = HS_VERSION_SRT1;
3739 // m_ConnReq.m_iVersion = HS_VERSION_UDT4; // <--- Change in order to do regression test.
3740 m_ConnReq.m_iReqType = URQ_WAVEAHAND;
3741 m_ConnReq.m_iCookie = bake(serv_addr);
3742
3743 // This will be also passed to a HSv4 rendezvous, but fortunately the old
3744 // SRT didn't read this field from URQ_WAVEAHAND message, only URQ_CONCLUSION.
3745 m_ConnReq.m_iType = SrtHSRequest::wrapFlags(false /* no MAGIC here */, m_config.iSndCryptoKeyLen);
3746 IF_HEAVY_LOGGING(const bool whether = m_config.iSndCryptoKeyLen != 0);
3747 HLOGC(aclog.Debug,
3748 log << CONID() << "startConnect (rnd): " << (whether ? "" : "NOT ")
3749 << " Advertising PBKEYLEN - value = " << m_config.iSndCryptoKeyLen);
3750 m_RdvState = CHandShake::RDV_WAVING;
3751 m_SrtHsSide = HSD_DRAW; // initially not resolved.
3752 }
3753 else
3754 {
3755 // For caller-listener configuration, set the version 4 for INDUCTION
3756 // due to a serious problem in UDT code being also in the older SRT versions:
3757 // the listener peer simply sents the EXACT COPY of the caller's induction
3758 // handshake, except the cookie, which means that when the caller sents version 5,
3759 // the listener will respond with version 5, which is a false information. Therefore
3760 // HSv5 clients MUST send HS_VERSION_UDT4 from the caller, regardless of currently
3761 // supported handshake version.
3762 //
3763 // The HSv5 listener should only respond with INDUCTION with m_iVersion == HS_VERSION_SRT1.
3764 m_ConnReq.m_iVersion = HS_VERSION_UDT4;
3765 m_ConnReq.m_iReqType = URQ_INDUCTION;
3766 m_ConnReq.m_iCookie = 0;
3767 m_RdvState = CHandShake::RDV_INVALID;
3768 }
3769
3770 m_ConnReq.m_iMSS = m_config.iMSS;
3771 // Defined as the size of the receiver buffer in packets, unless
3772 // SRTO_FC has been set to a less value.
3773 m_ConnReq.m_iFlightFlagSize = m_config.flightCapacity();
3774 m_ConnReq.m_iID = m_SocketID;
3775 CIPAddress::ntop(serv_addr, (m_ConnReq.m_piPeerIP));
3776
3777 if (forced_isn == SRT_SEQNO_NONE)
3778 {
3779 forced_isn = generateISN();
3780 HLOGC(aclog.Debug, log << CONID() << "startConnect: ISN generated = " << forced_isn);
3781 }
3782 else
3783 {
3784 HLOGC(aclog.Debug, log << CONID() << "startConnect: ISN forced = " << forced_isn);
3785 }
3786
3787 m_iISN = m_ConnReq.m_iISN = forced_isn;
3788
3789 setInitialSndSeq(m_iISN);
3790 // Inform the server my configurations.
3791 CPacket reqpkt;
3792 reqpkt.setControl(UMSG_HANDSHAKE);
3793 reqpkt.allocate(m_iMaxSRTPayloadSize);
3794 // XXX NOTE: Now the memory for the payload part is allocated automatically,
3795 // and such allocated memory is also automatically deallocated in the
3796 // destructor. If you use CPacket::allocate, remember that you must not:
3797 // - delete this memory
3798 // - assign to m_pcData.
3799 // If you use only manual assignment to m_pCData, this is then manual
3800 // allocation and so it won't be deallocated in the destructor.
3801 //
3802 // (Desired would be to disallow modification of m_pcData outside the
3803 // control of methods.)
3804
3805 // ID = 0, connection request
3806 reqpkt.set_id(0);
3807
3808 size_t hs_size = m_iMaxSRTPayloadSize;
3809 m_ConnReq.store_to((reqpkt.m_pcData), (hs_size));
3810
3811 // Note that CPacket::allocate() sets also the size
3812 // to the size of the allocated buffer, which not
3813 // necessarily is to be the size of the data.
3814 reqpkt.setLength(hs_size);
3815
3816 const steady_clock::time_point tnow = steady_clock::now();
3817 m_SndLastAck2Time = tnow;
3818 setPacketTS(reqpkt, tnow);
3819
3820 HLOGC(cnlog.Debug,
3821 log << CONID() << "CUDT::startConnect: REQ-TIME set HIGH (TimeStamp: " << reqpkt.timestamp()
3822 << "). SENDING HS: " << m_ConnReq.show());
3823
3824 /*
3825 * Race condition if non-block connect response thread scheduled before we set m_bConnecting to true?
3826 * Connect response will be ignored and connecting will wait until timeout.
3827 * Maybe m_ConnectionLock handling problem? Not used in CUDT::connect(const CPacket& response)
3828 */
3829 m_tsLastReqTime = tnow;
3830 m_bConnecting = true;
3831
3832 // At this point m_SourceAddr is probably default-any, but this function
3833 // now requires that the address be specified here because there will be
3834 // no possibility to do it at any next stage of sending.
3835 m_pSndQueue->sendto(serv_addr, reqpkt, m_SourceAddr);
3836
3837 //
3838 ///
3839 //// ---> CONTINUE TO: <PEER>.CUDT::processConnectRequest()
3840 /// (Take the part under condition: hs.m_iReqType == URQ_INDUCTION)
3841 //// <--- RETURN WHEN: m_pSndQueue->sendto() is called.
3842 //// .... SKIP UNTIL m_pRcvQueue->recvfrom() HERE....
3843 //// (the first "sendto" will not be called due to being too early)
3844 ///
3845 //
3846
3847 //////////////////////////////////////////////////////
3848 // SYNCHRO BAR
3849 //////////////////////////////////////////////////////
3850 if (!m_config.bSynRecving)
3851 {
3852 HLOGC(cnlog.Debug, log << CONID() << "startConnect: ASYNC MODE DETECTED. Deferring the process to RcvQ:worker");
3853 return;
3854 }
3855
3856 // Below this bar, rest of function maintains only and exclusively
3857 // the SYNCHRONOUS (blocking) connection process.
3858
3859 // Wait for the negotiated configurations from the peer side.
3860
3861 // This packet only prepares the storage where we will read the
3862 // next incoming packet.
3863 CPacket response;
3864 response.setControl(UMSG_HANDSHAKE);
3865 response.allocate(m_iMaxSRTPayloadSize);
3866
3867 CUDTException e;
3868 EConnectStatus cst = CONN_CONTINUE;
3869 // This is a temporary place to store the DESTINATION IP from the incoming packet.
3870 // We can't record this address yet until the cookie-confirmation is done, for safety reasons.
3871 sockaddr_any use_source_adr(serv_addr.family());
3872
3873 while (!m_bClosing && !m_bBreaking)
3874 {
3875 const steady_clock::time_point local_tnow = steady_clock::now();
3876 const steady_clock::duration tdiff = local_tnow - m_tsLastReqTime.load();
3877 // avoid sending too many requests, at most 1 request per 250ms
3878
3879 // SHORT VERSION:
3880 // The immediate first run of this loop WILL SKIP THIS PART, so
3881 // the processing really begins AFTER THIS CONDITION.
3882 //
3883 // Note that some procedures inside may set m_tsLastReqTime to 0,
3884 // which will result of this condition to trigger immediately in
3885 // the next iteration.
3886 if (count_milliseconds(tdiff) > 250)
3887 {
3888 HLOGC(cnlog.Debug,
3889 log << CONID() << "startConnect: LOOP: time to send (" << count_milliseconds(tdiff)
3890 << " > 250 ms). size=" << reqpkt.getLength());
3891
3892 if (m_config.bRendezvous)
3893 reqpkt.set_id(m_ConnRes.m_iID);
3894
3895 #if ENABLE_HEAVY_LOGGING
3896 {
3897 CHandShake debughs;
3898 debughs.load_from(reqpkt.m_pcData, reqpkt.getLength());
3899 HLOGC(cnlog.Debug,
3900 log << CONID() << "startConnect: REQ-TIME HIGH."
3901 << " cont/sending HS to peer: " << debughs.show());
3902 }
3903 #endif
3904
3905 m_tsLastReqTime = local_tnow;
3906 setPacketTS(reqpkt, local_tnow);
3907 m_pSndQueue->sendto(serv_addr, reqpkt, use_source_adr);
3908 }
3909 else
3910 {
3911 HLOGC(cnlog.Debug,
3912 log << CONID() << "startConnect: LOOP: too early to send - " << count_milliseconds(tdiff)
3913 << " < 250ms");
3914 }
3915
3916 cst = CONN_CONTINUE;
3917 response.setLength(m_iMaxSRTPayloadSize);
3918 if (m_pRcvQueue->recvfrom(m_SocketID, (response)) > 0)
3919 {
3920 use_source_adr = response.udpDestAddr();
3921
3922 HLOGC(cnlog.Debug, log << CONID() << "startConnect: got response for connect request");
3923 cst = processConnectResponse(response, &e);
3924
3925 HLOGC(cnlog.Debug, log << CONID() << "startConnect: response processing result: " << ConnectStatusStr(cst));
3926
3927 // Expected is that:
3928 // - the peer responded with URQ_INDUCTION + cookie. This above function
3929 // should check that and craft the URQ_CONCLUSION handshake, in which
3930 // case this function returns CONN_CONTINUE. As an extra action taken
3931 // for that case, we set the SECURING mode if encryption requested,
3932 // and serialize again the handshake, possibly together with HS extension
3933 // blocks, if HSv5 peer responded. The serialized handshake will be then
3934 // sent again, as the loop is repeated.
3935 // - the peer responded with URQ_CONCLUSION. This handshake was accepted
3936 // as a connection, and for >= HSv5 the HS extension blocks have been
3937 // also read and interpreted. In this case this function returns:
3938 // - CONN_ACCEPT, if everything was correct - break this loop and return normally
3939 // - CONN_REJECT in case of any problems with the delivered handshake
3940 // (incorrect data or data conflict) - throw error exception
3941 // - the peer responded with any of URQ_ERROR_*. - throw error exception
3942 //
3943 // The error exception should make the API connect() function fail, if blocking
3944 // or mark the failure for that socket in epoll, if non-blocking.
3945
3946 if (cst == CONN_RENDEZVOUS)
3947 {
3948 // When this function returned CONN_RENDEZVOUS, this requires
3949 // very special processing for the Rendezvous-v5 algorithm. This MAY
3950 // involve also preparing a new handshake form, also interpreting the
3951 // SRT handshake extension and crafting SRT handshake extension for the
3952 // peer, which should be next sent. When this function returns CONN_CONTINUE,
3953 // it means that it has done all that was required, however none of the below
3954 // things has to be done (this function will do it by itself if needed).
3955 // Otherwise the handshake rolling can be interrupted and considered complete.
3956 cst = processRendezvous(&response, serv_addr, RST_OK, (reqpkt));
3957 if (cst == CONN_CONTINUE)
3958 continue;
3959
3960 HLOGC(cnlog.Debug,
3961 log << CONID() << "startConnect: processRendezvous returned cst=" << ConnectStatusStr(cst));
3962
3963 if (cst == CONN_REJECT)
3964 {
3965 // Just in case it wasn't set, set this as a fallback
3966 if (m_RejectReason == SRT_REJ_UNKNOWN)
3967 m_RejectReason = SRT_REJ_ROGUE;
3968
3969 // rejection or erroneous code.
3970 reqpkt.setLength(m_iMaxSRTPayloadSize);
3971 reqpkt.setControl(UMSG_HANDSHAKE);
3972 sendRendezvousRejection(serv_addr, (reqpkt));
3973 }
3974 }
3975
3976 if (cst == CONN_REJECT)
3977 {
3978 HLOGC(cnlog.Debug,
3979 log << CONID() << "startConnect: REJECTED by processConnectResponse - sending SHUTDOWN");
3980 sendCtrl(UMSG_SHUTDOWN);
3981 }
3982
3983 if (cst != CONN_CONTINUE && cst != CONN_CONFUSED)
3984 break; // --> OUTSIDE-LOOP
3985
3986 // IMPORTANT
3987 // [[using assert(m_pCryptoControl != nullptr)]];
3988
3989 // new request/response should be sent out immediately on receiving a response
3990 HLOGC(cnlog.Debug,
3991 log << CONID() << "startConnect: SYNC CONNECTION STATUS:" << ConnectStatusStr(cst)
3992 << ", REQ-TIME: LOW.");
3993 m_tsLastReqTime = steady_clock::time_point();
3994
3995 // Now serialize the handshake again to the existing buffer so that it's
3996 // then sent later in this loop.
3997
3998 // First, set the size back to the original size, m_iMaxSRTPayloadSize because
3999 // this is the size of the originally allocated space. It might have been
4000 // shrunk by serializing the INDUCTION handshake (which was required before
4001 // sending this packet to the output queue) and therefore be too
4002 // small to store the CONCLUSION handshake (with HSv5 extensions).
4003 reqpkt.setLength(m_iMaxSRTPayloadSize);
4004
4005 HLOGC(cnlog.Debug,
4006 log << CONID() << "startConnect: creating HS CONCLUSION: buffer size=" << reqpkt.getLength());
4007
4008 // NOTE: BUGFIX: SERIALIZE AGAIN.
4009 // The original UDT code didn't do it, so it was theoretically
4010 // turned into conclusion, but was sending still the original
4011 // induction handshake challenge message. It was working only
4012 // thanks to that simultaneously there were being sent handshake
4013 // messages from a separate thread (CSndQueue::worker) from
4014 // RendezvousQueue, this time serialized properly, which caused
4015 // that with blocking mode there was a kinda initial "drunk
4016 // passenger with taxi driver talk" until the RendezvousQueue sends
4017 // (when "the time comes") the right CONCLUSION handshake
4018 // challenge message.
4019 //
4020 // Now that this is fixed, the handshake messages from RendezvousQueue
4021 // are sent only when there is a rendezvous mode or non-blocking mode.
4022 if (!createSrtHandshake(SRT_CMD_HSREQ, SRT_CMD_KMREQ, 0, 0, (reqpkt), (m_ConnReq)))
4023 {
4024 LOGC(cnlog.Warn, log << CONID() << "createSrtHandshake failed - REJECTING.");
4025 cst = CONN_REJECT;
4026 break;
4027 }
4028 // These last 2 parameters designate the buffer, which is in use only for SRT_CMD_KMRSP.
4029 // If m_ConnReq.m_iVersion == HS_VERSION_UDT4, this function will do nothing,
4030 // except just serializing the UDT handshake.
4031 // The trick is that the HS challenge is with version HS_VERSION_UDT4, but the
4032 // listener should respond with HS_VERSION_SRT1, if it is HSv5 capable.
4033 }
4034
4035 HLOGC(cnlog.Debug,
4036 log << CONID() << "startConnect: timeout from Q:recvfrom, looping again; cst=" << ConnectStatusStr(cst));
4037
4038 #if ENABLE_HEAVY_LOGGING
4039 // Non-fatal assertion
4040 if (cst == CONN_REJECT) // Might be returned by processRendezvous
4041 {
4042 LOGC(cnlog.Error,
4043 log << CONID()
4044 << "startConnect: IPE: cst=REJECT NOT EXPECTED HERE, the loop should've been interrupted!");
4045 break;
4046 }
4047 #endif
4048
4049 if (steady_clock::now() > ttl_time)
4050 {
4051 // timeout
4052 e = CUDTException(MJ_SETUP, MN_TIMEOUT, 0);
4053 m_RejectReason = SRT_REJ_TIMEOUT;
4054 HLOGC(cnlog.Debug,
4055 log << CONID() << "startConnect: TTL time " << FormatTime(ttl_time) << " exceeded, TIMEOUT.");
4056 break;
4057 }
4058 }
4059
4060 // <--- OUTSIDE-LOOP
4061 // Here will fall the break when not CONN_CONTINUE.
4062 // CONN_RENDEZVOUS is handled by processRendezvous.
4063 // CONN_ACCEPT will skip this and pass on.
4064 if (cst == CONN_REJECT)
4065 {
4066 e = CUDTException(MJ_SETUP, MN_REJECTED, 0);
4067 }
4068
4069 if (e.getErrorCode() == 0)
4070 {
4071 if (m_bClosing || m_bBreaking) // if the socket is closed before connection...
4072 e = CUDTException(MJ_SETUP, MN_CLOSED, 0);
4073 else if (m_ConnRes.m_iReqType > URQ_FAILURE_TYPES) // connection request rejected
4074 {
4075 m_RejectReason = RejectReasonForURQ(m_ConnRes.m_iReqType);
4076 e = CUDTException(MJ_SETUP, MN_REJECTED, 0);
4077 }
4078 else if ((!m_config.bRendezvous) && (m_ConnRes.m_iISN != m_iISN)) // security check
4079 e = CUDTException(MJ_SETUP, MN_SECURITY, 0);
4080 }
4081
4082 if (e.getErrorCode() != 0)
4083 {
4084 m_bConnecting = false;
4085 // The process is to be abnormally terminated, remove the connector
4086 // now because most likely no other processing part has done anything with it.
4087 m_pRcvQueue->removeConnector(m_SocketID);
4088 throw e;
4089 }
4090
4091 HLOGC(cnlog.Debug,
4092 log << CONID() << "startConnect: handshake exchange succeeded. sourceIP=" << m_SourceAddr.str());
4093
4094 // Parameters at the end.
4095 HLOGC(cnlog.Debug,
4096 log << CONID() << "startConnect: END. Parameters: mss=" << m_config.iMSS
4097 << " max-cwnd-size=" << m_CongCtl->cgWindowMaxSize() << " cwnd-size=" << m_CongCtl->cgWindowSize()
4098 << " rtt=" << m_iSRTT << " bw=" << m_iBandwidth);
4099 }
4100
4101 // Asynchronous connection
4102 EConnectStatus srt::CUDT::processAsyncConnectResponse(const CPacket &pkt) ATR_NOEXCEPT
4103 {
4104 EConnectStatus cst = CONN_CONTINUE;
4105 CUDTException e;
4106
4107 ScopedLock cg(m_ConnectionLock);
4108 HLOGC(cnlog.Debug, log << CONID() << "processAsyncConnectResponse: got response for connect request, processing");
4109 cst = processConnectResponse(pkt, &e);
4110
4111 HLOGC(cnlog.Debug,
4112 log << CONID() << "processAsyncConnectResponse: response processing result: " << ConnectStatusStr(cst)
4113 << "; REQ-TIME LOW to enforce immediate response");
4114 m_tsLastReqTime = steady_clock::time_point();
4115
4116 return cst;
4117 }
4118
4119 bool srt::CUDT::processAsyncConnectRequest(EReadStatus rst,
4120 EConnectStatus cst,
4121 const CPacket* pResponse /*[[nullable]]*/,
4122 const sockaddr_any& serv_addr)
4123 {
4124 // IMPORTANT!
4125
4126 // This function is called, still asynchronously, but in the order
4127 // of call just after the call to the above processAsyncConnectResponse.
4128 // This should have got the original value returned from
4129 // processConnectResponse through processAsyncConnectResponse.
4130
4131 CPacket reqpkt;
4132 reqpkt.setControl(UMSG_HANDSHAKE);
4133 reqpkt.allocate(m_iMaxSRTPayloadSize);
4134 const steady_clock::time_point now = steady_clock::now();
4135 setPacketTS(reqpkt, now);
4136
4137 HLOGC(cnlog.Debug,
4138 log << CONID() << "processAsyncConnectRequest: REQ-TIME: HIGH. Should prevent too quick responses.");
4139 m_tsLastReqTime = now;
4140 // ID = 0, connection request
4141 reqpkt.set_id(!m_config.bRendezvous ? 0 : m_ConnRes.m_iID);
4142
4143 bool status = true;
4144
4145 ScopedLock cg(m_ConnectionLock);
4146 if (!m_bOpened) // Check the socket has not been closed before already.
4147 return false;
4148
4149 if (cst == CONN_RENDEZVOUS)
4150 {
4151 HLOGC(cnlog.Debug, log << CONID() << "processAsyncConnectRequest: passing to processRendezvous");
4152 cst = processRendezvous(pResponse, serv_addr, rst, (reqpkt));
4153 if (cst == CONN_ACCEPT)
4154 {
4155 HLOGC(cnlog.Debug,
4156 log << CONID()
4157 << "processAsyncConnectRequest: processRendezvous completed the process and responded by itself. "
4158 "Done.");
4159 return true;
4160 }
4161
4162 if (cst != CONN_CONTINUE)
4163 {
4164 // processRendezvous already set the reject reason
4165 LOGC(cnlog.Warn,
4166 log << CONID()
4167 << "processAsyncConnectRequest: REJECT reported from processRendezvous, not processing further.");
4168
4169 if (m_RejectReason == SRT_REJ_UNKNOWN)
4170 m_RejectReason = SRT_REJ_ROGUE;
4171
4172 sendRendezvousRejection(serv_addr, (reqpkt));
4173 status = false;
4174 }
4175 }
4176 else if (cst == CONN_REJECT)
4177 {
4178 // m_RejectReason already set at worker_ProcessAddressedPacket.
4179 LOGC(cnlog.Warn,
4180 log << CONID() << "processAsyncConnectRequest: REJECT reported from HS processing: "
4181 << srt_rejectreason_str(m_RejectReason) << " - not processing further");
4182 // m_tsLastReqTime = steady_clock::time_point(); XXX ?
4183 return false;
4184 }
4185 else
4186 {
4187 // (this procedure will be also run for HSv4 rendezvous)
4188 HLOGC(cnlog.Debug,
4189 log << CONID() << "processAsyncConnectRequest: serializing HS: buffer size=" << reqpkt.getLength());
4190 if (!createSrtHandshake(SRT_CMD_HSREQ, SRT_CMD_KMREQ, 0, 0, (reqpkt), (m_ConnReq)))
4191 {
4192 // All 'false' returns from here are IPE-type, mostly "invalid argument" plus "all keys expired".
4193 LOGC(cnlog.Error,
4194 log << CONID() << "IPE: processAsyncConnectRequest: createSrtHandshake failed, dismissing.");
4195 status = false;
4196 }
4197 else
4198 {
4199 HLOGC(cnlog.Debug,
4200 log << CONID()
4201 << "processAsyncConnectRequest: sending HS reqtype=" << RequestTypeStr(m_ConnReq.m_iReqType)
4202 << " to socket " << reqpkt.id() << " size=" << reqpkt.getLength());
4203 }
4204 }
4205
4206 if (!status)
4207 {
4208 return false;
4209 /* XXX Shouldn't it send a single response packet for the rejection?
4210 // Set the version to 0 as "handshake rejection" status and serialize it
4211 CHandShake zhs;
4212 size_t size = reqpkt.getLength();
4213 zhs.store_to((reqpkt.m_pcData), (size));
4214 reqpkt.setLength(size);
4215 */
4216 }
4217
4218 HLOGC(cnlog.Debug,
4219 log << CONID() << "processAsyncConnectRequest: setting REQ-TIME HIGH, SENDING HS:" << m_ConnReq.show());
4220 m_tsLastReqTime = steady_clock::now();
4221 m_pSndQueue->sendto(serv_addr, reqpkt, m_SourceAddr);
4222 return status;
4223 }
4224
4225 void srt::CUDT::sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& r_rsppkt)
4226 {
4227 // We can reuse m_ConnReq because we are about to abandon the connection process.
4228 m_ConnReq.m_iReqType = URQFailure(m_RejectReason);
4229
4230 // Assumed that r_rsppkt refers to a packet object that was already prepared
4231 // to be used for storing the handshake there.
4232 size_t size = r_rsppkt.getLength();
4233 m_ConnReq.store_to((r_rsppkt.m_pcData), (size));
4234 r_rsppkt.setLength(size);
4235
4236 HLOGC(cnlog.Debug, log << CONID() << "sendRendezvousRejection: using code=" << m_ConnReq.m_iReqType
4237 << " for reject reason code " << m_RejectReason << " (" << srt_rejectreason_str(m_RejectReason) << ")");
4238
4239 setPacketTS(r_rsppkt, steady_clock::now());
4240 m_pSndQueue->sendto(serv_addr, r_rsppkt, m_SourceAddr);
4241 }
4242
4243 enum HandshakeSide srt::CUDT::compareCookies(int32_t req, int32_t res)
4244 {
4245 if (req < res)
4246 {
4247 LOGC(gglog.Note, log << hex << setfill('0') << uppercase
4248 << "REQ: " << setw(8) << req
4249 << " RES: " << setw(8) << res << " - result: RESPONDER");
4250 return HSD_RESPONDER;
4251 }
4252 else if (req > res)
4253 {
4254 LOGC(gglog.Note, log << hex << setfill('0') << uppercase
4255 << "REQ: " << setw(8) << req
4256 << " RES: " << setw(8) << res << " - result: INITIATOR");
4257 return HSD_INITIATOR;
4258 }
4259 return HSD_DRAW;
4260 }
4261
4262 enum HandshakeSide srt::CUDT::backwardCompatibleCookieContest(int32_t req, int32_t res)
4263 {
4264 const int64_t xreq = int64_t(req);
4265 const int64_t xres = int64_t(res);
4266 const int64_t contest = xreq - xres;
4267
4268 LOGC(cnlog.Debug, log << "cookieContest: agent=" << req
4269 << " peer=" << res
4270 << hex << uppercase << setfill('0') // PERSISTENT flags
4271 << " X64: "<< setw(16) << xreq
4272 << " vs. " << setw(16) << xres
4273 << " DIFF: " << setw(16) << contest);
4274
4275 if ((contest & 0xFFFFFFFF) == 0)
4276 {
4277 return HSD_DRAW;
4278 }
4279 if (contest & 0x80000000)
4280 {
4281 const int64_t revert = xres - xreq;
4282 if (revert & 0x80000000 && req > res)
4283 return HSD_INITIATOR;
4284 return HSD_RESPONDER;
4285 }
4286
4287 return HSD_INITIATOR;
4288 }
4289
4290 void srt::CUDT::cookieContest()
4291 {
4292 if (m_SrtHsSide != HSD_DRAW)
4293 return;
4294
4295 // Here m_ConnReq.m_iCookie is a local cookie value sent in connection request to the peer.
4296 // m_ConnRes.m_iCookie is a cookie value sent by the peer in its connection request.
4297 if (m_ConnReq.m_iCookie == 0 || m_ConnRes.m_iCookie == 0)
4298 {
4299 LOGC(cnlog.Debug, log << CONID() << "cookieContest: agent=" << m_ConnReq.m_iCookie << " peer=" << m_ConnRes.m_iCookie
4300 << " - ERROR: zero not allowed!");
4301
4302 return;
4303 }
4304 m_SrtHsSide = backwardCompatibleCookieContest(m_ConnReq.m_iCookie, m_ConnRes.m_iCookie);
4305
4306 }
4307
4308 // This function should complete the data for KMX needed for an out-of-band
4309 // handshake response. Possibilities are:
4310 // - There's no KMX (including first responder's handshake in rendezvous). This writes 0 to w_kmdatasize.
4311 // - The encryption status is failure. Respond with fail code and w_kmdatasize = 1.
4312 // - The last KMX was successful. Respond with the original kmdata and their size in w_kmdatasize.
4313 EConnectStatus srt::CUDT::craftKmResponse(uint32_t* aw_kmdata, size_t& w_kmdatasize)
4314 {
4315 // If the last CONCLUSION message didn't contain the KMX extension, there's
4316 // no key recorded yet, so it can't be extracted. Mark this w_kmdatasize empty though.
4317 int hs_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_ConnRes.m_iType);
4318 if (IsSet(hs_flags, CHandShake::HS_EXT_KMREQ))
4319 {
4320 // m_pCryptoControl can be NULL if the socket has been closed already. See issue #2231.
4321 if (!m_pCryptoControl)
4322 {
4323 m_RejectReason = SRT_REJ_IPE;
4324 LOGC(cnlog.Error,
4325 log << CONID() << "IPE: craftKmResponse needs to send KM, but CryptoControl does not exist."
4326 << " Socket state: connected=" << boolalpha << m_bConnected << ", connecting=" << m_bConnecting
4327 << ", broken=" << m_bBroken << ", opened " << m_bOpened << ", closing=" << m_bClosing << ".");
4328 return CONN_REJECT;
4329 }
4330 // This is a periodic handshake update, so you need to extract the KM data from the
4331 // first message, provided that it is there.
4332 size_t msgsize = m_pCryptoControl->getKmMsg_size(0);
4333 if (msgsize == 0)
4334 {
4335 switch (m_pCryptoControl->m_RcvKmState)
4336 {
4337 // If the KMX process ended up with a failure, the KMX is not recorded.
4338 // In this case as the KMRSP answer the "failure status" should be crafted.
4339 case SRT_KM_S_NOSECRET:
4340 case SRT_KM_S_BADSECRET:
4341 {
4342 HLOGC(cnlog.Debug,
4343 log << CONID() << "craftKmResponse: No KMX recorded, status = "
4344 << KmStateStr(m_pCryptoControl->m_RcvKmState) << ". Respond it.");
4345
4346 // Just do the same thing as in CCryptoControl::processSrtMsg_KMREQ for that case,
4347 // that is, copy the NOSECRET code into KMX message.
4348 memcpy((aw_kmdata), &m_pCryptoControl->m_RcvKmState, sizeof(int32_t));
4349 w_kmdatasize = 1;
4350 }
4351 break; // Treat as ACCEPT in general; might change to REJECT on enforced-encryption
4352
4353 default:
4354 // Remaining values:
4355 // UNSECURED: should not fall here at all
4356 // SECURING: should not happen in HSv5
4357 // SECURED: should have received the recorded KMX correctly (getKmMsg_size(0) > 0)
4358 {
4359 m_RejectReason = SRT_REJ_IPE;
4360 // Remaining situations:
4361 // - password only on this site: shouldn't be considered to be sent to a no-password site
4362 LOGC(cnlog.Error,
4363 log << CONID() << "craftKmResponse: IPE: PERIODIC HS: NO KMREQ RECORDED KMSTATE: RCV="
4364 << KmStateStr(m_pCryptoControl->m_RcvKmState)
4365 << " SND=" << KmStateStr(m_pCryptoControl->m_SndKmState));
4366 return CONN_REJECT;
4367 }
4368 break;
4369 }
4370 }
4371 else
4372 {
4373 w_kmdatasize = msgsize / 4;
4374 if (msgsize > w_kmdatasize * 4)
4375 {
4376 // Sanity check
4377 LOGC(cnlog.Error, log << CONID() << "IPE: KMX data not aligned to 4 bytes! size=" << msgsize);
4378 memset((aw_kmdata + (w_kmdatasize * 4)), 0, msgsize - (w_kmdatasize * 4));
4379 ++w_kmdatasize;
4380 }
4381
4382 HLOGC(cnlog.Debug,
4383 log << CONID() << "craftKmResponse: getting KM DATA from the fore-recorded KMX from KMREQ, size="
4384 << w_kmdatasize);
4385 memcpy((aw_kmdata), m_pCryptoControl->getKmMsg_data(0), msgsize);
4386 }
4387 }
4388 else
4389 {
4390 HLOGC(cnlog.Debug, log << CONID() << "craftKmResponse: no KMX flag - not extracting KM data for KMRSP");
4391 w_kmdatasize = 0;
4392 }
4393
4394 return CONN_ACCEPT;
4395 }
4396
4397 EConnectStatus srt::CUDT::processRendezvous(
4398 const CPacket* pResponse /*[[nullable]]*/, const sockaddr_any& serv_addr,
4399 EReadStatus rst, CPacket& w_reqpkt)
4400 {
4401 if (m_RdvState == CHandShake::RDV_CONNECTED)
4402 {
4403 HLOGC(cnlog.Debug, log << CONID() << "processRendezvous: already in CONNECTED state.");
4404 return CONN_ACCEPT;
4405 }
4406
4407 uint32_t kmdata[SRTDATA_MAXSIZE];
4408 size_t kmdatasize = SRTDATA_MAXSIZE;
4409
4410 cookieContest();
4411
4412 // We know that the other side was contacted and the other side has sent
4413 // the handshake message - we know then both cookies. If it's a draw, it's
4414 // a very rare case of creating identical cookies.
4415 if (m_SrtHsSide == HSD_DRAW)
4416 {
4417 m_RejectReason = SRT_REJ_RDVCOOKIE;
4418 LOGC(cnlog.Error,
4419 log << CONID() << "COOKIE CONTEST UNRESOLVED: can't assign connection roles, please wait another minute.");
4420 return CONN_REJECT;
4421 }
4422
4423 UDTRequestType rsp_type = URQ_FAILURE_TYPES; // just to track uninitialized errors
4424
4425 // We can assume that the Handshake packet received here as 'response'
4426 // is already serialized in m_ConnRes. Check extra flags that are meaningful
4427 // for further processing here.
4428
4429 int ext_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_ConnRes.m_iType);
4430 bool needs_extension = ext_flags != 0; // Initial value: received HS has extensions.
4431 bool needs_hsrsp;
4432 rendezvousSwitchState((rsp_type), (needs_extension), (needs_hsrsp));
4433 if (rsp_type > URQ_FAILURE_TYPES)
4434 {
4435 m_RejectReason = RejectReasonForURQ(rsp_type);
4436 HLOGC(cnlog.Debug,
4437 log << CONID()
4438 << "processRendezvous: rejecting due to switch-state response: " << RequestTypeStr(rsp_type));
4439 return CONN_REJECT;
4440 }
4441 checkUpdateCryptoKeyLen("processRendezvous", m_ConnRes.m_iType);
4442
4443 // We have three possibilities here as it comes to HSREQ extensions:
4444
4445 // 1. The agent is loser in attention state, it sends EMPTY conclusion (without extensions)
4446 // 2. The agent is loser in initiated state, it interprets incoming HSREQ and creates HSRSP
4447 // 3. The agent is winner in attention or fine state, it sends HSREQ extension
4448 m_ConnReq.m_iReqType = rsp_type;
4449 m_ConnReq.m_extension = needs_extension;
4450
4451 // This must be done before prepareConnectionObjects(), because it sets ISN and m_iMaxSRTPayloadSize needed to create buffers.
4452 if (!applyResponseSettings(pResponse))
4453 {
4454 LOGC(cnlog.Error, log << CONID() << "processRendezvous: peer settings rejected");
4455 return CONN_REJECT;
4456 }
4457
4458 // The CryptoControl must be created by the prepareConnectionObjects() before interpreting and creating HSv5 extensions
4459 // because the it will be used there.
4460 if (!prepareConnectionObjects(m_ConnRes, m_SrtHsSide, NULL) || !prepareBuffers(NULL))
4461 {
4462 // m_RejectReason already handled
4463 HLOGC(cnlog.Debug,
4464 log << CONID() << "processRendezvous: rejecting due to problems in prepareConnectionObjects.");
4465 return CONN_REJECT;
4466 }
4467
4468 // Case 2.
4469 if (needs_hsrsp)
4470 {
4471 // This means that we have received HSREQ extension with the handshake, so we need to interpret
4472 // it and craft the response.
4473 if (rst == RST_OK)
4474 {
4475 // We have JUST RECEIVED packet in this session (not that this is called as periodic update).
4476 // Sanity check
4477 m_tsLastReqTime = steady_clock::time_point();
4478 if (!pResponse || pResponse->getLength() == size_t(-1))
4479 {
4480 m_RejectReason = SRT_REJ_IPE;
4481 LOGC(cnlog.Fatal,
4482 log << CONID() << "IPE: rst=RST_OK, but the packet has set -1 length - REJECTING (REQ-TIME: LOW)");
4483 return CONN_REJECT;
4484 }
4485
4486 if (!interpretSrtHandshake(m_ConnRes, *pResponse, kmdata, &kmdatasize))
4487 {
4488 HLOGC(cnlog.Debug,
4489 log << CONID() << "processRendezvous: rejecting due to problems in interpretSrtHandshake REQ-TIME: LOW.");
4490 return CONN_REJECT;
4491 }
4492
4493 updateAfterSrtHandshake(HS_VERSION_SRT1);
4494
4495 // Pass on, inform about the shortened response-waiting period.
4496 HLOGC(cnlog.Debug, log << CONID() << "processRendezvous: setting REQ-TIME: LOW. Forced to respond immediately.");
4497 }
4498 else
4499 {
4500 // This is a repeated handshake, so you can't use the incoming data to
4501 // prepare data for createSrtHandshake. They have to be extracted from inside.
4502 EConnectStatus conn = craftKmResponse((kmdata), (kmdatasize));
4503 if (conn != CONN_ACCEPT)
4504 return conn;
4505 }
4506
4507 // No matter the value of needs_extension, the extension is always needed
4508 // when HSREQ was interpreted (to store HSRSP extension).
4509 m_ConnReq.m_extension = true;
4510
4511 HLOGC(cnlog.Debug,
4512 log << CONID()
4513 << "processRendezvous: HSREQ extension ok, creating HSRSP response. kmdatasize=" << kmdatasize);
4514
4515 w_reqpkt.setLength(m_iMaxSRTPayloadSize);
4516 if (!createSrtHandshake(SRT_CMD_HSRSP, SRT_CMD_KMRSP,
4517 kmdata, kmdatasize,
4518 (w_reqpkt), (m_ConnReq)))
4519 {
4520 HLOGC(cnlog.Debug,
4521 log << CONID()
4522 << "processRendezvous: rejecting due to problems in createSrtHandshake. REQ-TIME: LOW");
4523 m_tsLastReqTime = steady_clock::time_point();
4524 return CONN_REJECT;
4525 }
4526
4527 // This means that it has received URQ_CONCLUSION with HSREQ, agent is then in RDV_FINE
4528 // state, it sends here URQ_CONCLUSION with HSREQ/KMREQ extensions and it awaits URQ_AGREEMENT.
4529 return CONN_CONTINUE;
4530 }
4531
4532 // Special case: if URQ_AGREEMENT is to be sent, when this side is INITIATOR,
4533 // then it must have received HSRSP, so it must interpret it. Otherwise it would
4534 // end up with URQ_DONE, which means that it is the other side to interpret HSRSP.
4535 if (m_SrtHsSide == HSD_INITIATOR && m_ConnReq.m_iReqType == URQ_AGREEMENT)
4536 {
4537 // The same is done in CUDT::postConnect(), however this section will
4538 // not be done in case of rendezvous. The section in postConnect() is
4539 // predicted to run only in regular CALLER handling.
4540
4541 if (rst != RST_OK || !pResponse || pResponse->getLength() == size_t(-1))
4542 {
4543 // Actually the -1 length would be an IPE, but it's likely that this was reported already.
4544 HLOGC(
4545 cnlog.Debug,
4546 log << CONID()
4547 << "processRendezvous: no INCOMING packet, NOT interpreting extensions (relying on existing data)");
4548 }
4549 else
4550 {
4551 HLOGC(cnlog.Debug,
4552 log << CONID() << "processRendezvous: INITIATOR, will send AGREEMENT - interpreting HSRSP extension");
4553 if (!interpretSrtHandshake(m_ConnRes, *pResponse, 0, 0))
4554 {
4555 // m_RejectReason is already set, so set the reqtype accordingly
4556 m_ConnReq.m_iReqType = URQFailure(m_RejectReason);
4557 return CONN_REJECT;
4558 }
4559 }
4560 // This should be false, make a kinda assert here.
4561 if (needs_extension)
4562 {
4563 LOGC(cnlog.Fatal,
4564 log << CONID() << "IPE: INITIATOR responding AGREEMENT should declare no extensions to HS");
4565 m_ConnReq.m_extension = false;
4566 }
4567 updateAfterSrtHandshake(HS_VERSION_SRT1);
4568 }
4569
4570 HLOGC(cnlog.Debug,
4571 log << CONID() << "processRendezvous: COOKIES Agent/Peer: " << m_ConnReq.m_iCookie << "/"
4572 << m_ConnRes.m_iCookie << " HSD:" << (m_SrtHsSide == HSD_INITIATOR ? "initiator" : "responder")
4573 << " STATE:" << CHandShake::RdvStateStr(m_RdvState) << " ...");
4574
4575 if (rsp_type == URQ_DONE)
4576 {
4577 HLOGC(cnlog.Debug, log << CONID() << "... WON'T SEND any response, both sides considered connected");
4578 }
4579 else
4580 {
4581 HLOGC(cnlog.Debug,
4582 log << CONID() << "... WILL SEND " << RequestTypeStr(rsp_type) << " "
4583 << (m_ConnReq.m_extension ? "with" : "without") << " SRT HS extensions");
4584 }
4585
4586 // This marks the information for the serializer that
4587 // the SRT handshake extension is required.
4588 // Rest of the data will be filled together with
4589 // serialization.
4590 m_ConnReq.m_extension = needs_extension;
4591
4592 w_reqpkt.setLength(m_iMaxSRTPayloadSize);
4593 if (m_RdvState == CHandShake::RDV_CONNECTED)
4594 {
4595 int cst = postConnect(pResponse, true, 0);
4596 if (cst == CONN_REJECT)
4597 {
4598 // m_RejectReason already set
4599 HLOGC(cnlog.Debug, log << CONID() << "processRendezvous: rejecting due to problems in postConnect.");
4600 return CONN_REJECT;
4601 }
4602 }
4603
4604 // URQ_DONE or URQ_AGREEMENT can be the result if the state is RDV_CONNECTED.
4605 // If URQ_DONE, then there's nothing to be done, when URQ_AGREEMENT then return
4606 // CONN_CONTINUE to make the caller send again the contents if the packet buffer,
4607 // this time with URQ_AGREEMENT message, but still consider yourself connected.
4608 if (rsp_type == URQ_DONE)
4609 {
4610 HLOGC(cnlog.Debug, log << CONID() << "processRendezvous: rsp=DONE, reporting ACCEPT (nothing to respond)");
4611 return CONN_ACCEPT;
4612 }
4613
4614 // createSrtHandshake moved here because if the above conditions are satisfied,
4615 // no response is going to be send, so nothing needs to be "created".
4616
4617 // needs_extension here distinguishes between cases 1 and 3.
4618 // NOTE: in case when interpretSrtHandshake was run under the conditions above (to interpret HSRSP),
4619 // then createSrtHandshake below will create only empty AGREEMENT message.
4620 if (!createSrtHandshake(SRT_CMD_HSREQ, SRT_CMD_KMREQ, 0, 0,
4621 (w_reqpkt), (m_ConnReq)))
4622 {
4623 // m_RejectReason already set
4624 LOGC(cnlog.Warn, log << CONID() << "createSrtHandshake failed (IPE?), connection rejected. REQ-TIME: LOW");
4625 m_tsLastReqTime = steady_clock::time_point();
4626 return CONN_REJECT;
4627 }
4628
4629 if (rsp_type == URQ_AGREEMENT && m_RdvState == CHandShake::RDV_CONNECTED)
4630 {
4631 // We are using our own serialization method (not the one called after
4632 // processConnectResponse, this is skipped in case when this function
4633 // is called), so we can also send this immediately. Agreement must be
4634 // sent just once and the party must switch into CONNECTED state - in
4635 // contrast to CONCLUSION messages, which should be sent in loop repeatedly.
4636 //
4637 // Even though in theory the AGREEMENT message sent just once may miss
4638 // the target (as normal thing in UDP), this is little probable to happen,
4639 // and this doesn't matter much because even if the other party doesn't
4640 // get AGREEMENT, but will get payload or KEEPALIVE messages, it will
4641 // turn into connected state as well. The AGREEMENT is rather kinda
4642 // catalyzer here and may turn the entity on the right track faster. When
4643 // AGREEMENT is missed, it may have kinda initial tearing.
4644
4645 const steady_clock::time_point now = steady_clock::now();
4646 m_tsLastReqTime = now;
4647 setPacketTS(w_reqpkt, now);
4648 HLOGC(cnlog.Debug,
4649 log << CONID()
4650 << "processRendezvous: rsp=AGREEMENT, reporting ACCEPT and sending just this one, REQ-TIME HIGH.");
4651
4652 m_pSndQueue->sendto(serv_addr, w_reqpkt, m_SourceAddr);
4653
4654 return CONN_ACCEPT;
4655 }
4656
4657 if (rst == RST_OK)
4658 {
4659 // the request time must be updated so that the next handshake can be sent out immediately
4660 HLOGC(cnlog.Debug,
4661 log << "processRendezvous: rsp=" << RequestTypeStr(m_ConnReq.m_iReqType)
4662 << " REQ-TIME: LOW to send immediately, consider yourself connected");
4663 m_tsLastReqTime = steady_clock::time_point();
4664 }
4665 else
4666 {
4667 HLOGC(cnlog.Debug,
4668 log << CONID() << "processRendezvous: REQ-TIME: remains previous value, consider yourself connected");
4669 }
4670 return CONN_CONTINUE;
4671 }
4672
4673 // [[using locked(m_ConnectionLock)]];
4674 EConnectStatus srt::CUDT::processConnectResponse(const CPacket& response, CUDTException* eout) ATR_NOEXCEPT
4675 {
4676 // NOTE: ASSUMED LOCK ON: m_ConnectionLock.
4677
4678 // this is the 2nd half of a connection request. If the connection is setup successfully this returns 0.
4679 // Returned values:
4680 // - CONN_REJECT: there was some error when processing the response, connection should be rejected
4681 // - CONN_ACCEPT: the handshake is done and finished correctly
4682 // - CONN_CONTINUE: the induction handshake has been processed correctly, and expects CONCLUSION handshake
4683
4684 if (!m_bConnecting)
4685 return CONN_REJECT;
4686
4687 // This is required in HSv5 rendezvous, in which it should send the URQ_AGREEMENT message to
4688 // the peer, however switch to connected state.
4689 HLOGC(cnlog.Debug,
4690 log << CONID() << "processConnectResponse: TYPE:"
4691 << (response.isControl() ? MessageTypeStr(response.getType(), response.getExtendedType())
4692 : string("DATA")));
4693 // ConnectStatus res = CONN_REJECT; // used later for status - must be declared here due to goto POST_CONNECT.
4694
4695 // For HSv4, the data sender is INITIATOR, and the data receiver is RESPONDER,
4696 // regardless of the connecting side affiliation. This will be changed for HSv5.
4697 bool bidirectional = false;
4698 HandshakeSide hsd = m_config.bDataSender ? HSD_INITIATOR : HSD_RESPONDER;
4699 // (defined here due to 'goto' below).
4700
4701 // SRT peer may send the SRT handshake private message (type 0x7fff) before a keep-alive.
4702
4703 // This condition is checked when the current agent is trying to do connect() in rendezvous mode,
4704 // but the peer was faster to send a handshake packet earlier. This makes it continue with connecting
4705 // process if the peer is already behaving as if the connection was already established.
4706
4707 // This value will check either the initial value, which is less than SRT1, or
4708 // the value previously loaded to m_ConnReq during the previous handshake response.
4709 // For the initial form this value should not be checked.
4710 bool hsv5 = m_ConnRes.m_iVersion >= HS_VERSION_SRT1;
4711
4712 if (m_config.bRendezvous &&
4713 (m_RdvState == CHandShake::RDV_CONNECTED // somehow Rendezvous-v5 switched it to CONNECTED.
4714 || !response.isControl() // WAS A PAYLOAD PACKET.
4715 || (response.getType() == UMSG_KEEPALIVE) // OR WAS A UMSG_KEEPALIVE message.
4716 || (response.getType() == UMSG_EXT) // OR WAS a CONTROL packet of some extended type (i.e. any SRT specific)
4717 )
4718 // This may happen if this is an initial state in which the socket type was not yet set.
4719 // If this is a field that holds the response handshake record from the peer, this means that it wasn't received
4720 // yet. HSv5: added version check because in HSv5 the m_iType field has different meaning and it may be 0 in
4721 // case when the handshake does not carry SRT extensions.
4722 && (hsv5 || m_ConnRes.m_iType != UDT_UNDEFINED))
4723 {
4724 // a data packet or a keep-alive packet comes, which means the peer side is already connected
4725 // in this situation, the previously recorded response will be used
4726 // In HSv5 this situation is theoretically possible if this party has missed the URQ_AGREEMENT message.
4727 HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: already connected - pinning in");
4728 if (hsv5)
4729 {
4730 m_RdvState = CHandShake::RDV_CONNECTED;
4731 }
4732
4733 return postConnect(&response, hsv5, eout);
4734 }
4735
4736 if (!response.isControl(UMSG_HANDSHAKE))
4737 {
4738 m_RejectReason = SRT_REJ_ROGUE;
4739 if (!response.isControl())
4740 {
4741 LOGC(cnlog.Warn, log << CONID() << "processConnectResponse: received DATA while HANDSHAKE expected");
4742 }
4743 else
4744 {
4745 LOGC(cnlog.Error,
4746 log << CONID()
4747 << "processConnectResponse: CONFUSED: expected UMSG_HANDSHAKE as connection not yet established, "
4748 "got: "
4749 << MessageTypeStr(response.getType(), response.getExtendedType()));
4750
4751 if (response.getType() == UMSG_SHUTDOWN)
4752 {
4753 LOGC(cnlog.Error,
4754 log << CONID() << "processConnectResponse: UMSG_SHUTDOWN received, rejecting connection.");
4755 return CONN_REJECT;
4756 }
4757 }
4758
4759 if (m_config.bRendezvous)
4760 {
4761 // In rendezvous mode we expect that both sides are known
4762 // to the service operator (unlike a listener, which may
4763 // operate connections from unknown sources). This means that
4764 // the connection process should be terminated anyway, on
4765 // whichever side it would happen.
4766 return CONN_REJECT;
4767 }
4768
4769 return CONN_CONFUSED;
4770 }
4771
4772 if (m_config.bRendezvous)
4773 {
4774 m_SourceAddr = response.udpDestAddr();
4775 }
4776
4777 if (m_ConnRes.load_from(response.m_pcData, response.getLength()) == -1)
4778 {
4779 m_RejectReason = SRT_REJ_ROGUE;
4780 // Handshake data were too small to reach the Handshake structure. Reject.
4781 LOGC(cnlog.Error,
4782 log << CONID()
4783 << "processConnectResponse: HANDSHAKE data buffer too small - possible blueboxing. Rejecting.");
4784 return CONN_REJECT;
4785 }
4786
4787 HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: HS RECEIVED: " << m_ConnRes.show());
4788 if (m_ConnRes.m_iReqType >= URQ_FAILURE_TYPES)
4789 {
4790 m_RejectReason = RejectReasonForURQ(m_ConnRes.m_iReqType);
4791 LOGC(cnlog.Warn,
4792 log << CONID() << "processConnectResponse: rejecting per reception of a rejection HS response: "
4793 << RequestTypeStr(m_ConnRes.m_iReqType));
4794 return CONN_REJECT;
4795 }
4796
4797 if (size_t(m_ConnRes.m_iMSS) > CPacket::ETH_MAX_MTU_SIZE)
4798 {
4799 // Yes, we do abort to prevent buffer overrun. Set your MSS correctly
4800 // and you'll avoid problems.
4801 m_RejectReason = SRT_REJ_ROGUE;
4802 LOGC(cnlog.Fatal, log << CONID() << "MSS size " << m_config.iMSS << "exceeds MTU size!");
4803 return CONN_REJECT;
4804 }
4805
4806 // (see createCrypter() call below)
4807 //
4808 // The CCryptoControl attached object must be created early
4809 // because it will be required to create a conclusion handshake in HSv5
4810 //
4811 if (m_config.bRendezvous)
4812 {
4813 // SANITY CHECK: A rendezvous socket should reject any caller requests (it's not a listener)
4814 if (m_ConnRes.m_iReqType == URQ_INDUCTION)
4815 {
4816 m_RejectReason = SRT_REJ_ROGUE;
4817 LOGC(cnlog.Error,
4818 log << CONID()
4819 << "processConnectResponse: Rendezvous-point received INDUCTION handshake (expected WAVEAHAND). "
4820 "Rejecting.");
4821 return CONN_REJECT;
4822 }
4823
4824 // The procedure for version 5 is completely different and changes the states
4825 // differently, so the old code will still maintain HSv4 the old way.
4826
4827 if (m_ConnRes.m_iVersion > HS_VERSION_UDT4)
4828 {
4829 HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: Rendezvous HSv5 DETECTED.");
4830 return CONN_RENDEZVOUS; // --> will continue in CUDT::processRendezvous().
4831 }
4832
4833 HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: Rendsezvous HSv4 DETECTED.");
4834 // So, here it has either received URQ_WAVEAHAND handshake message (while it should be in URQ_WAVEAHAND itself)
4835 // or it has received URQ_CONCLUSION/URQ_AGREEMENT message while this box has already sent URQ_WAVEAHAND to the
4836 // peer, and DID NOT send the URQ_CONCLUSION yet.
4837
4838 if (m_ConnReq.m_iReqType == URQ_WAVEAHAND || m_ConnRes.m_iReqType == URQ_WAVEAHAND)
4839 {
4840 HLOGC(cnlog.Debug,
4841 log << CONID() << "processConnectResponse: REQ-TIME LOW. got HS RDV. Agent state:"
4842 << RequestTypeStr(m_ConnReq.m_iReqType) << " Peer HS:" << m_ConnRes.show());
4843
4844 // Here we could have received WAVEAHAND or CONCLUSION.
4845 // For HSv4 simply switch to CONCLUSION for the sake of further handshake rolling.
4846 // For HSv5, make the cookie contest and basing on this decide, which party
4847 // should provide the HSREQ/KMREQ attachment.
4848
4849 if (!createCrypter(hsd, false /* unidirectional */))
4850 {
4851 m_RejectReason = SRT_REJ_RESOURCE;
4852 m_ConnReq.m_iReqType = URQFailure(SRT_REJ_RESOURCE);
4853 // the request time must be updated so that the next handshake can be sent out immediately.
4854 m_tsLastReqTime = steady_clock::time_point();
4855 return CONN_REJECT;
4856 }
4857
4858 m_ConnReq.m_iReqType = URQ_CONCLUSION;
4859 // the request time must be updated so that the next handshake can be sent out immediately.
4860 m_tsLastReqTime = steady_clock::time_point();
4861 return CONN_CONTINUE;
4862 }
4863 else
4864 {
4865 HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: Rendezvous HSv4 PAST waveahand");
4866 }
4867 }
4868 else
4869 {
4870 // set cookie
4871 if (m_ConnRes.m_iReqType == URQ_INDUCTION)
4872 {
4873 HLOGC(cnlog.Debug,
4874 log << CONID() << "processConnectResponse: REQ-TIME LOW; got INDUCTION HS response (cookie:" << hex
4875 << m_ConnRes.m_iCookie << " version:" << dec << m_ConnRes.m_iVersion
4876 << "), sending CONCLUSION HS with this cookie");
4877
4878 m_ConnReq.m_iCookie = m_ConnRes.m_iCookie;
4879 m_ConnReq.m_iReqType = URQ_CONCLUSION;
4880
4881 // Here test if the LISTENER has responded with version HS_VERSION_SRT1,
4882 // it means that it is HSv5 capable. It can still accept the HSv4 handshake.
4883 if (m_ConnRes.m_iVersion > HS_VERSION_UDT4)
4884 {
4885 const int hs_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_ConnRes.m_iType);
4886
4887 if (hs_flags != SrtHSRequest::SRT_MAGIC_CODE)
4888 {
4889 LOGC(cnlog.Warn,
4890 log << CONID() << "processConnectResponse: Listener HSv5 did not set the SRT_MAGIC_CODE.");
4891 m_RejectReason = SRT_REJ_ROGUE;
4892 return CONN_REJECT;
4893 }
4894
4895 checkUpdateCryptoKeyLen("processConnectResponse", m_ConnRes.m_iType);
4896
4897 // This will catch HS_VERSION_SRT1 and any newer.
4898 // Set your highest version.
4899 m_ConnReq.m_iVersion = HS_VERSION_SRT1;
4900 // CONTROVERSIAL: use 0 as m_iType according to the meaning in HSv5.
4901 // The HSv4 client might not understand it, which means that agent
4902 // must switch itself to HSv4 rendezvous, and this time iType should
4903 // be set to UDT_DGRAM value.
4904 m_ConnReq.m_iType = 0;
4905
4906 // This marks the information for the serializer that
4907 // the SRT handshake extension is required.
4908 // Rest of the data will be filled together with
4909 // serialization.
4910 m_ConnReq.m_extension = true;
4911
4912 // For HSv5, the caller is INITIATOR and the listener is RESPONDER.
4913 // The m_config.bDataSender value should be completely ignored and the
4914 // connection is always bidirectional.
4915 bidirectional = true;
4916 hsd = HSD_INITIATOR;
4917 m_SrtHsSide = hsd;
4918 }
4919
4920 m_tsLastReqTime = steady_clock::time_point();
4921 if (!createCrypter(hsd, bidirectional))
4922 {
4923 m_RejectReason = SRT_REJ_RESOURCE;
4924 return CONN_REJECT;
4925 }
4926 // NOTE: This setup sets URQ_CONCLUSION and appropriate data in the handshake structure.
4927 // The full handshake to be sent will be filled back in the caller function -- CUDT::startConnect().
4928 return CONN_CONTINUE;
4929 }
4930 }
4931
4932 return postConnect(&response, false, eout);
4933 }
4934
4935 bool srt::CUDT::applyResponseSettings(const CPacket* pHspkt /*[[nullable]]*/) ATR_NOEXCEPT
4936 {
4937 if (!m_ConnRes.valid())
4938 {
4939 LOGC(cnlog.Error, log << CONID() << "applyResponseSettings: ROGUE HANDSHAKE - rejecting");
4940 m_RejectReason = SRT_REJ_ROGUE;
4941 return false;
4942 }
4943
4944 // Re-configure according to the negotiated values.
4945 m_config.iMSS = m_ConnRes.m_iMSS;
4946
4947 const size_t full_hdr_size = CPacket::UDP_HDR_SIZE + CPacket::HDR_SIZE;
4948 m_iMaxSRTPayloadSize = m_config.iMSS - full_hdr_size;
4949 HLOGC(cnlog.Debug, log << CONID() << "applyResponseSettings: PAYLOAD SIZE: " << m_iMaxSRTPayloadSize);
4950
4951 m_iFlowWindowSize = m_ConnRes.m_iFlightFlagSize;
4952 const int udpsize = m_config.iMSS - CPacket::UDP_HDR_SIZE;
4953 m_iMaxSRTPayloadSize = udpsize - CPacket::HDR_SIZE;
4954 m_iPeerISN = m_ConnRes.m_iISN;
4955
4956 setInitialRcvSeq(m_iPeerISN);
4957
4958 m_iRcvCurrPhySeqNo = CSeqNo::decseq(m_ConnRes.m_iISN);
4959 m_PeerID = m_ConnRes.m_iID;
4960 memcpy((m_piSelfIP), m_ConnRes.m_piPeerIP, sizeof m_piSelfIP);
4961 if (pHspkt)
4962 m_SourceAddr = pHspkt->udpDestAddr();
4963
4964 HLOGC(cnlog.Debug,
4965 log << CONID() << "applyResponseSettings: HANDSHAKE CONCLUDED. SETTING: payload-size=" << m_iMaxSRTPayloadSize
4966 << " mss=" << m_ConnRes.m_iMSS << " flw=" << m_ConnRes.m_iFlightFlagSize << " peer-ISN=" << m_ConnRes.m_iISN
4967 << " local-ISN=" << m_iISN
4968 << " peerID=" << m_ConnRes.m_iID
4969 << " sourceIP=" << m_SourceAddr.str());
4970 return true;
4971 }
4972
4973 EConnectStatus srt::CUDT::postConnect(const CPacket* pResponse, bool rendezvous, CUDTException *eout) ATR_NOEXCEPT
4974 {
4975 if (m_ConnRes.m_iVersion < HS_VERSION_SRT1)
4976 m_tsRcvPeerStartTime = steady_clock::time_point(); // will be set correctly in SRT HS.
4977
4978 // This procedure isn't being executed in rendezvous because
4979 // in rendezvous it's completed before calling this function.
4980 if (!rendezvous)
4981 {
4982 HLOGC(cnlog.Debug, log << CONID() << boolalpha << "postConnect: packet:" << bool(pResponse) << " rendezvous:" << rendezvous);
4983 // The "local storage depleted" case shouldn't happen here, but
4984 // this is a theoretical path that needs prevention.
4985 bool ok = pResponse;
4986 if (!ok)
4987 {
4988 m_RejectReason = SRT_REJ_IPE;
4989 if (eout)
4990 {
4991 *eout = CUDTException(MJ_SETUP, MN_REJECTED, 0);
4992 }
4993 return CONN_REJECT;
4994 }
4995
4996 // [[assert (pResponse != NULL)]];
4997
4998 // NOTE: THIS function must be called before calling prepareConnectionObjects.
4999 // The reason why it's not part of prepareConnectionObjects is that the activities
5000 // done there are done SIMILAR way in acceptAndRespond, which also calls this
5001 // function. In fact, prepareConnectionObjects() represents the code that was
5002 // done separately in processConnectResponse() and acceptAndRespond(), so this way
5003 // this code is now common. Now acceptAndRespond() does "manually" something similar
5004 // to applyResponseSettings(), just a little bit differently. This SHOULD be made
5005 // common as a part of refactoring job, just needs a bit more time.
5006 //
5007 // Currently just this function must be called always BEFORE prepareConnectionObjects
5008 // everywhere except acceptAndRespond().
5009 ok = applyResponseSettings(pResponse);
5010
5011 // This will actually be done also in rendezvous HSv4,
5012 // however in this case the HSREQ extension will not be attached,
5013 // so it will simply go the "old way".
5014 // (&&: skip if failed already)
5015 // Must be called before interpretSrtHandshake() to create the CryptoControl.
5016 ok = ok && prepareConnectionObjects(m_ConnRes, m_SrtHsSide, eout);
5017
5018 // May happen that 'response' contains a data packet that was sent in rendezvous mode.
5019 // In this situation the interpretation of handshake was already done earlier.
5020 ok = ok && pResponse->isControl();
5021 ok = ok && interpretSrtHandshake(m_ConnRes, *pResponse, 0, 0);
5022 ok = ok && prepareBuffers(eout);
5023
5024 if (!ok)
5025 {
5026 if (eout)
5027 {
5028 *eout = CUDTException(MJ_SETUP, MN_REJECTED, 0);
5029 }
5030 // m_RejectReason already set
5031 return CONN_REJECT;
5032 }
5033 }
5034
5035 bool have_group = false;
5036
5037 {
5038 #if ENABLE_BONDING
5039 SharedLock cl (uglobal().m_GlobControlLock);
5040 CUDTGroup* g = m_parent->m_GroupOf;
5041 if (g)
5042 {
5043 // This is the last moment when this can be done.
5044 // The updateAfterSrtHandshake call will copy the receiver
5045 // start time to the receiver buffer data, so the correct
5046 // value must be set before this happens.
5047 synchronizeWithGroup(g);
5048 have_group = true;
5049 }
5050 #endif
5051 }
5052
5053 if (!have_group)
5054 {
5055 // This function will be called internally inside
5056 // synchronizeWithGroup(). This is just more complicated.
5057 updateAfterSrtHandshake(m_ConnRes.m_iVersion);
5058 }
5059
5060 CInfoBlock ib;
5061 ib.m_iIPversion = m_PeerAddr.family();
5062 CInfoBlock::convert(m_PeerAddr, ib.m_piIP);
5063 if (m_pCache->lookup(&ib) >= 0)
5064 {
5065 m_iSRTT = ib.m_iSRTT;
5066 m_iRTTVar = ib.m_iSRTT / 2;
5067 m_iBandwidth = ib.m_iBandwidth;
5068 }
5069
5070 #if SRT_DEBUG_RTT
5071 s_rtt_trace.trace(steady_clock::now(), "Connect", -1, -1,
5072 m_bIsFirstRTTReceived, -1, m_iSRTT, m_iRTTVar);
5073 #endif
5074
5075 SRT_REJECT_REASON rr = setupCC();
5076 if (rr != SRT_REJ_UNKNOWN)
5077 {
5078 m_RejectReason = rr;
5079 return CONN_REJECT;
5080 }
5081
5082 // And, I am connected too.
5083 m_bConnecting = false;
5084
5085 // The lock on m_ConnectionLock should still be applied, but
5086 // the socket could have been started removal before this function
5087 // has started. Do a sanity check before you continue with the
5088 // connection process.
5089 CUDTSocket* s = uglobal().locateSocket(m_SocketID);
5090 if (s)
5091 {
5092 // The socket could be closed at this very moment.
5093 // Continue with removing the socket from the pending structures,
5094 // but prevent it from setting it as connected.
5095 m_bConnected = true;
5096
5097 // register this socket for receiving data packets
5098 m_pRNode->m_bOnList = true;
5099 m_pRcvQueue->setNewEntry(this);
5100 }
5101
5102 // XXX Problem around CONN_CONFUSED!
5103 // If some too-eager packets were received from a listener
5104 // that thinks it's connected, but his last handshake was missed,
5105 // they are collected by CRcvQueue::storePktClone. The removeConnector
5106 // function will want to delete them all, so it would be nice
5107 // if these packets can be re-delivered. Of course the listener
5108 // should be prepared to resend them (as every packet can be lost
5109 // on UDP), but it's kinda overkill when we have them already and
5110 // can dispatch them.
5111
5112 // Remove from rendezvous queue (in this particular case it's
5113 // actually removing the socket that undergoes asynchronous HS processing).
5114 // Removing at THIS point because since when setNewEntry is called,
5115 // the next iteration in the CRcvQueue::worker loop will be dispatching
5116 // packets normally, as within-connection, so the "connector" won't
5117 // play any role since this time.
5118 // The connector, however, must stay alive until the setNewEntry is called
5119 // because otherwise the packets that are coming for this socket before the
5120 // connection process is complete will be rejected as "attack", instead of
5121 // being enqueued for later pickup from the queue.
5122 m_pRcvQueue->removeConnector(m_SocketID);
5123
5124 // Ok, no more things to be done as per "clear connecting state"
5125 if (!s)
5126 {
5127 LOGC(cnlog.Error, log << CONID() << "Connection broken in the process - socket closed");
5128 m_RejectReason = SRT_REJ_CLOSE;
5129 if (eout)
5130 {
5131 *eout = CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
5132 }
5133 return CONN_REJECT;
5134 }
5135
5136 // copy address information of local node
5137 // the local port must be correctly assigned BEFORE CUDT::startConnect(),
5138 // otherwise if startConnect() fails, the multiplexer cannot be located
5139 // by garbage collection and will cause leak
5140 s->core().m_pSndQueue->m_pChannel->getSockAddr((s->m_SelfAddr));
5141 CIPAddress::pton((s->m_SelfAddr), s->core().m_piSelfIP, m_PeerAddr);
5142
5143 //int token = -1;
5144 #if ENABLE_BONDING
5145 {
5146 SharedLock cl (uglobal().m_GlobControlLock);
5147 CUDTGroup* g = m_parent->m_GroupOf;
5148 if (g)
5149 {
5150 ScopedLock gl (*g->exp_groupLock());
5151 // XXX this might require another check of group type.
5152 // For redundancy group, at least, update the status in the group.
5153
5154 // LEAVING as comment for historical reasons. Locking is here most
5155 // likely not necessary because the socket cannot be removed from the
5156 // group until the socket isn't removed, and this requires locking of
5157 // m_GlobControlLock. This should ensure that when m_GroupOf is
5158 // not NULL, m_GroupMemberData is also valid.
5159 // ScopedLock glock(g->m_GroupLock);
5160
5161 HLOGC(cnlog.Debug, log << "group: Socket @" << m_parent->m_SocketID << " fresh connected, setting IDLE");
5162
5163 groups::SocketData* gi = m_parent->m_GroupMemberData;
5164 gi->sndstate = SRT_GST_IDLE;
5165 gi->rcvstate = SRT_GST_IDLE;
5166 gi->laststatus = SRTS_CONNECTED;
5167 //token = gi->token;
5168 g->setGroupConnected();
5169 }
5170 }
5171 #endif
5172
5173 s->m_Status = SRTS_CONNECTED;
5174
5175 // acknowledde any waiting epolls to write
5176 // This must be done AFTER the group member status is upgraded to IDLE because
5177 // this state change will trigger the waiting function in blocking-mode groupConnect
5178 // and this may be immediately followed by exit from connect and start sending function,
5179 // which must see this very link already as IDLE, not PENDING, which will make this
5180 // link unable to be used and therefore the sending call would fail.
5181 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_CONNECT, true);
5182
5183 CGlobEvent::triggerEvent();
5184
5185 /* XXX Likely it should NOT be called here for two reasons:
5186
5187 - likely lots of mutexes are locked here so any
5188 API call from here might cause a deadlock
5189 - if called from an asynchronous connection process, it was
5190 already called from inside updateConnStatus
5191 - if called from startConnect (synchronous mode), it is even wrong.
5192
5193 if (m_cbConnectHook)
5194 {
5195 CALLBACK_CALL(m_cbConnectHook, m_SocketID, SRT_SUCCESS, m_PeerAddr.get(), token);
5196 }
5197
5198 */
5199
5200 LOGC(cnlog.Note, log << CONID() << "Connection established from ("
5201 << m_SourceAddr.str() << ") to peer @" << m_PeerID << " (" << m_PeerAddr.str() << ")");
5202
5203 return CONN_ACCEPT;
5204 }
5205
5206 void srt::CUDT::checkUpdateCryptoKeyLen(const char *loghdr SRT_ATR_UNUSED, int32_t typefield)
5207 {
5208 int enc_flags = SrtHSRequest::SRT_HSTYPE_ENCFLAGS::unwrap(typefield);
5209
5210 // potentially 0-7 values are possible.
5211 // When 0, don't change anything - it should rely on the value 0.
5212 // When 1, 5, 6, 7, this is kinda internal error - ignore.
5213 if (enc_flags >= 2 && enc_flags <= 4) // 2 = 128, 3 = 192, 4 = 256
5214 {
5215 int rcv_pbkeylen = SrtHSRequest::SRT_PBKEYLEN_BITS::wrap(enc_flags);
5216 if (m_config.iSndCryptoKeyLen == 0)
5217 {
5218 m_config.iSndCryptoKeyLen = rcv_pbkeylen;
5219 HLOGC(cnlog.Debug,
5220 log << CONID() << loghdr
5221 << ": PBKEYLEN adopted from advertised value: " << m_config.iSndCryptoKeyLen);
5222 }
5223 else if (m_config.iSndCryptoKeyLen != rcv_pbkeylen)
5224 {
5225 // Conflict. Use SRTO_SENDER flag to check if this side should accept
5226 // the enforcement, otherwise simply let it win.
5227 if (!m_config.bDataSender)
5228 {
5229 LOGC(cnlog.Warn,
5230 log << CONID() << loghdr << ": PBKEYLEN conflict - OVERRIDDEN " << m_config.iSndCryptoKeyLen
5231 << " by " << rcv_pbkeylen << " from PEER (as AGENT is not SRTO_SENDER)");
5232 m_config.iSndCryptoKeyLen = rcv_pbkeylen;
5233 }
5234 else
5235 {
5236 LOGC(cnlog.Warn,
5237 log << CONID() << loghdr << ": PBKEYLEN conflict - keep " << m_config.iSndCryptoKeyLen
5238 << "; peer-advertised PBKEYLEN " << rcv_pbkeylen << " rejected because Agent is SRTO_SENDER");
5239 }
5240 }
5241 }
5242 else if (enc_flags != 0)
5243 {
5244 LOGC(cnlog.Error, log << CONID() << loghdr << ": IPE: enc_flags outside allowed 2, 3, 4: " << enc_flags);
5245 }
5246 else
5247 {
5248 HLOGC(cnlog.Debug, log << CONID() << loghdr << ": No encryption flags found in type field: " << typefield);
5249 }
5250 }
5251
5252 // Rendezvous
5253 void srt::CUDT::rendezvousSwitchState(UDTRequestType& w_rsptype, bool& w_needs_extension, bool& w_needs_hsrsp)
5254 {
5255 UDTRequestType req = m_ConnRes.m_iReqType;
5256 int hs_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_ConnRes.m_iType);
5257 bool has_extension = !!hs_flags; // it holds flags, if no flags, there are no extensions.
5258
5259 const HandshakeSide &hsd = m_SrtHsSide;
5260 // Note important possibilities that are considered here:
5261
5262 // 1. The serial arrangement. This happens when one party has missed the
5263 // URQ_WAVEAHAND message, it sent its own URQ_WAVEAHAND message, and then the
5264 // firstmost message it received from the peer is URQ_CONCLUSION, as a response
5265 // for agent's URQ_WAVEAHAND.
5266 //
5267 // In this case, Agent switches to RDV_FINE state and Peer switches to RDV_ATTENTION state.
5268 //
5269 // 2. The parallel arrangement. This happens when the URQ_WAVEAHAND message sent
5270 // by both parties are almost in a perfect synch (a rare, but possible case). In this
5271 // case, both parties receive one another's URQ_WAVEAHAND message and both switch to
5272 // RDV_ATTENTION state.
5273 //
5274 // It's not possible to predict neither which arrangement will happen, or which
5275 // party will be RDV_FINE in case when the serial arrangement has happened. What
5276 // will actually happen will depend on random conditions.
5277 //
5278 // No matter this randomity, we have a limited number of possible conditions:
5279 //
5280 // Stating that "agent" is the party that has received the URQ_WAVEAHAND in whatever
5281 // arrangement, we are certain, that "agent" switched to RDV_ATTENTION, and peer:
5282 //
5283 // - switched to RDV_ATTENTION state (so, both are in the same state independently)
5284 // - switched to RDV_FINE state (so, the message interchange is actually more-less sequenced)
5285 //
5286 // In particular, there's no possibility of a situation that both are in RDV_FINE state
5287 // because the agent can switch to RDV_FINE state only if it received URQ_CONCLUSION from
5288 // the peer, while the peer could not send URQ_CONCLUSION without switching off RDV_WAVING
5289 // (actually to RDV_ATTENTION). There's also no exit to RDV_FINE from RDV_ATTENTION.
5290
5291 // DEFAULT STATEMENT: don't attach extensions to URQ_CONCLUSION, neither HSREQ nor HSRSP.
5292 w_needs_extension = false;
5293 w_needs_hsrsp = false;
5294
5295 string reason;
5296
5297 #if ENABLE_HEAVY_LOGGING
5298
5299 HLOGC(cnlog.Debug, log << CONID() << "rendezvousSwitchState: HS: " << m_ConnRes.show());
5300
5301 struct LogAtTheEnd
5302 {
5303 CHandShake::RendezvousState ost;
5304 UDTRequestType orq;
5305 const CHandShake::RendezvousState &nst;
5306 const UDTRequestType & nrq;
5307 bool & needext;
5308 bool & needrsp;
5309 string & reason;
5310
5311 ~LogAtTheEnd()
5312 {
5313 HLOGC(cnlog.Debug,
5314 log << "rendezvousSwitchState: STATE[" << CHandShake::RdvStateStr(ost) << "->"
5315 << CHandShake::RdvStateStr(nst) << "] REQTYPE[" << RequestTypeStr(orq) << "->"
5316 << RequestTypeStr(nrq) << "] "
5317 << "ext:" << (needext ? (needrsp ? "HSRSP" : "HSREQ") : "NONE")
5318 << (reason == "" ? string() : "reason:" + reason));
5319 }
5320 } l_logend = {m_RdvState, req, m_RdvState, w_rsptype, w_needs_extension, w_needs_hsrsp, reason};
5321
5322 #endif
5323
5324 switch (m_RdvState)
5325 {
5326 case CHandShake::RDV_INVALID:
5327 return;
5328
5329 case CHandShake::RDV_WAVING:
5330 {
5331 if (req == URQ_WAVEAHAND)
5332 {
5333 m_RdvState = CHandShake::RDV_ATTENTION;
5334
5335 // NOTE: if this->isWinner(), attach HSREQ
5336 w_rsptype = URQ_CONCLUSION;
5337 if (hsd == HSD_INITIATOR)
5338 w_needs_extension = true;
5339 return;
5340 }
5341
5342 if (req == URQ_CONCLUSION)
5343 {
5344 m_RdvState = CHandShake::RDV_FINE;
5345 w_rsptype = URQ_CONCLUSION;
5346
5347 w_needs_extension = true; // (see below - this needs to craft either HSREQ or HSRSP)
5348 // if this->isWinner(), then craft HSREQ for that response.
5349 // if this->isLoser(), then this packet should bring HSREQ, so craft HSRSP for the response.
5350 if (hsd == HSD_RESPONDER)
5351 w_needs_hsrsp = true;
5352 return;
5353 }
5354 }
5355 reason = "WAVING -> WAVEAHAND or CONCLUSION";
5356 break;
5357
5358 case CHandShake::RDV_ATTENTION:
5359 {
5360 if (req == URQ_WAVEAHAND)
5361 {
5362 // This is only possible if the URQ_CONCLUSION sent to the peer
5363 // was lost on track. The peer is then simply unaware that the
5364 // agent has switched to ATTENTION state and continues sending
5365 // waveahands. In this case, just remain in ATTENTION state and
5366 // retry with URQ_CONCLUSION, as normally.
5367 w_rsptype = URQ_CONCLUSION;
5368 if (hsd == HSD_INITIATOR)
5369 w_needs_extension = true;
5370 return;
5371 }
5372
5373 if (req == URQ_CONCLUSION)
5374 {
5375 // We have two possibilities here:
5376 //
5377 // WINNER (HSD_INITIATOR): send URQ_AGREEMENT
5378 if (hsd == HSD_INITIATOR)
5379 {
5380 // WINNER should get a response with HSRSP, otherwise this is kinda empty conclusion.
5381 // If no HSRSP attached, stay in this state.
5382 if (hs_flags == 0)
5383 {
5384 HLOGC(cnlog.Debug,
5385 log << CONID()
5386 << "rendezvousSwitchState: {INITIATOR}[ATTENTION] awaits CONCLUSION+HSRSP, got "
5387 "CONCLUSION, remain in [ATTENTION]");
5388 w_rsptype = URQ_CONCLUSION;
5389 w_needs_extension = true; // If you expect to receive HSRSP, continue sending HSREQ
5390 return;
5391 }
5392 m_RdvState = CHandShake::RDV_CONNECTED;
5393 w_rsptype = URQ_AGREEMENT;
5394 return;
5395 }
5396
5397 // LOSER (HSD_RESPONDER): send URQ_CONCLUSION and attach HSRSP extension, then expect URQ_AGREEMENT
5398 if (hsd == HSD_RESPONDER)
5399 {
5400 // If no HSREQ attached, stay in this state.
5401 // (Although this seems completely impossible).
5402 if (hs_flags == 0)
5403 {
5404 LOGC(cnlog.Warn,
5405 log << CONID()
5406 << "rendezvousSwitchState: (IPE!){RESPONDER}[ATTENTION] awaits CONCLUSION+HSREQ, got "
5407 "CONCLUSION, remain in [ATTENTION]");
5408 w_rsptype = URQ_CONCLUSION;
5409 w_needs_extension = false; // If you received WITHOUT extensions, respond WITHOUT extensions (wait
5410 // for the right message)
5411 return;
5412 }
5413 m_RdvState = CHandShake::RDV_INITIATED;
5414 w_rsptype = URQ_CONCLUSION;
5415 w_needs_extension = true;
5416 w_needs_hsrsp = true;
5417 return;
5418 }
5419
5420 LOGC(cnlog.Error, log << CONID() << "RENDEZVOUS COOKIE DRAW! Cannot resolve to a valid state.");
5421 // Fallback for cookie draw
5422 m_RdvState = CHandShake::RDV_INVALID;
5423 w_rsptype = URQFailure(SRT_REJ_RDVCOOKIE);
5424 return;
5425 }
5426
5427 if (req == URQ_AGREEMENT)
5428 {
5429 // This means that the peer has received our URQ_CONCLUSION, but
5430 // the agent missed the peer's URQ_CONCLUSION (received only initial
5431 // URQ_WAVEAHAND).
5432 if (hsd == HSD_INITIATOR)
5433 {
5434 // In this case the missed URQ_CONCLUSION was sent without extensions,
5435 // whereas the peer received our URQ_CONCLUSION with HSREQ, and therefore
5436 // it sent URQ_AGREEMENT already with HSRSP. This isn't a problem for
5437 // us, we can go on with it, especially that the peer is already switched
5438 // into CHandShake::RDV_CONNECTED state.
5439 m_RdvState = CHandShake::RDV_CONNECTED;
5440
5441 // Both sides are connected, no need to send anything anymore.
5442 w_rsptype = URQ_DONE;
5443 return;
5444 }
5445
5446 if (hsd == HSD_RESPONDER)
5447 {
5448 // In this case the missed URQ_CONCLUSION was sent with extensions, so
5449 // we have to request this once again. Send URQ_CONCLUSION in order to
5450 // inform the other party that we need the conclusion message once again.
5451 // The ATTENTION state should be maintained.
5452 w_rsptype = URQ_CONCLUSION;
5453 w_needs_extension = true;
5454 w_needs_hsrsp = true;
5455 return;
5456 }
5457 }
5458 }
5459 reason = "ATTENTION -> WAVEAHAND(conclusion), CONCLUSION(agreement/conclusion), AGREEMENT (done/conclusion)";
5460 break;
5461
5462 case CHandShake::RDV_FINE:
5463 {
5464 // In FINE state we can't receive URQ_WAVEAHAND because if the peer has already
5465 // sent URQ_CONCLUSION, it's already in CHandShake::RDV_ATTENTION, and in this state it can
5466 // only send URQ_CONCLUSION, whereas when it isn't in CHandShake::RDV_ATTENTION, it couldn't
5467 // have sent URQ_CONCLUSION, and if it didn't, the agent wouldn't be in CHandShake::RDV_FINE state.
5468
5469 if (req == URQ_CONCLUSION)
5470 {
5471 // There's only one case when it should receive CONCLUSION in FINE state:
5472 // When it's the winner. If so, it should then contain HSREQ extension.
5473 // In case of loser, it shouldn't receive CONCLUSION at all - it should
5474 // receive AGREEMENT.
5475
5476 // The winner case, received CONCLUSION + HSRSP - switch to CONNECTED and send AGREEMENT.
5477 // So, check first if HAS EXTENSION
5478
5479 bool correct_switch = false;
5480 if (hsd == HSD_INITIATOR && !has_extension)
5481 {
5482 // Received REPEATED empty conclusion that has initially switched it into FINE state.
5483 // To exit FINE state we need the CONCLUSION message with HSRSP.
5484 HLOGC(cnlog.Debug,
5485 log << CONID()
5486 << "rendezvousSwitchState: {INITIATOR}[FINE] <CONCLUSION without HSRSP. Stay in [FINE], "
5487 "await CONCLUSION+HSRSP");
5488 }
5489 else if (hsd == HSD_RESPONDER)
5490 {
5491 // In FINE state the RESPONDER expects only to be sent AGREEMENT.
5492 // It has previously received CONCLUSION in WAVING state and this has switched
5493 // it to FINE state. That CONCLUSION message should have contained extension,
5494 // so if this is a repeated CONCLUSION+HSREQ, it should be responded with
5495 // CONCLUSION+HSRSP.
5496 HLOGC(cnlog.Debug,
5497 log << CONID()
5498 << "rendezvousSwitchState: {RESPONDER}[FINE] <CONCLUSION. Stay in [FINE], await AGREEMENT");
5499 }
5500 else
5501 {
5502 correct_switch = true;
5503 }
5504
5505 if (!correct_switch)
5506 {
5507 w_rsptype = URQ_CONCLUSION;
5508 // initiator should send HSREQ, responder HSRSP,
5509 // in both cases extension is needed
5510 w_needs_extension = true;
5511 w_needs_hsrsp = hsd == HSD_RESPONDER;
5512 return;
5513 }
5514
5515 m_RdvState = CHandShake::RDV_CONNECTED;
5516 w_rsptype = URQ_AGREEMENT;
5517 return;
5518 }
5519
5520 if (req == URQ_AGREEMENT)
5521 {
5522 // The loser case, the agreement was sent in response to conclusion that
5523 // already carried over the HSRSP extension.
5524
5525 // There's a theoretical case when URQ_AGREEMENT can be received in case of
5526 // parallel arrangement, while the agent is already in CHandShake::RDV_CONNECTED state.
5527 // This will be dispatched in the main loop and discarded.
5528
5529 m_RdvState = CHandShake::RDV_CONNECTED;
5530 w_rsptype = URQ_DONE;
5531 return;
5532 }
5533 }
5534
5535 reason = "FINE -> CONCLUSION(agreement), AGREEMENT(done)";
5536 break;
5537 case CHandShake::RDV_INITIATED:
5538 {
5539 // In this state we just wait for URQ_AGREEMENT, which should cause it to
5540 // switch to CONNECTED. No response required.
5541 if (req == URQ_AGREEMENT)
5542 {
5543 // No matter in which state we'd be, just switch to connected.
5544 if (m_RdvState == CHandShake::RDV_CONNECTED)
5545 {
5546 HLOGC(cnlog.Debug, log << CONID() << "<-- AGREEMENT: already connected");
5547 }
5548 else
5549 {
5550 HLOGC(cnlog.Debug, log << CONID() << "<-- AGREEMENT: switched to connected");
5551 }
5552 m_RdvState = CHandShake::RDV_CONNECTED;
5553 w_rsptype = URQ_DONE;
5554 return;
5555 }
5556
5557 if (req == URQ_CONCLUSION)
5558 {
5559 // Receiving conclusion in this state means that the other party
5560 // didn't get our conclusion, so send it again, the same as when
5561 // exiting the ATTENTION state.
5562 w_rsptype = URQ_CONCLUSION;
5563 if (hsd == HSD_RESPONDER)
5564 {
5565 HLOGC(cnlog.Debug,
5566 log << CONID()
5567 << "rendezvousSwitchState: {RESPONDER}[INITIATED] awaits AGREEMENT, "
5568 "got CONCLUSION, sending CONCLUSION+HSRSP");
5569 w_needs_extension = true;
5570 w_needs_hsrsp = true;
5571 return;
5572 }
5573
5574 // Loser, initiated? This may only happen in parallel arrangement, where
5575 // the agent exchanges empty conclusion messages with the peer, simultaneously
5576 // exchanging HSREQ-HSRSP conclusion messages. Check if THIS message contained
5577 // HSREQ, and set responding HSRSP in that case.
5578 if (hs_flags == 0)
5579 {
5580 HLOGC(cnlog.Debug,
5581 log << CONID()
5582 << "rendezvousSwitchState: {INITIATOR}[INITIATED] awaits AGREEMENT, "
5583 "got empty CONCLUSION, STILL RESPONDING CONCLUSION+HSRSP");
5584 }
5585 else
5586 {
5587
5588 HLOGC(cnlog.Debug,
5589 log << CONID()
5590 << "rendezvousSwitchState: {INITIATOR}[INITIATED] awaits AGREEMENT, "
5591 "got CONCLUSION+HSREQ, responding CONCLUSION+HSRSP");
5592 }
5593 w_needs_extension = true;
5594 w_needs_hsrsp = true;
5595 return;
5596 }
5597 }
5598
5599 reason = "INITIATED -> AGREEMENT(done)";
5600 break;
5601
5602 case CHandShake::RDV_CONNECTED:
5603 // Do nothing. This theoretically should never happen.
5604 w_rsptype = URQ_DONE;
5605 return;
5606 }
5607
5608 HLOGC(cnlog.Debug, log << CONID() << "rendezvousSwitchState: INVALID STATE TRANSITION, result: INVALID");
5609 // All others are treated as errors
5610 m_RdvState = CHandShake::RDV_WAVING;
5611 w_rsptype = URQFailure(SRT_REJ_ROGUE);
5612 }
5613
5614 /*
5615 * Timestamp-based Packet Delivery (TsbPd) thread
5616 * This thread runs only if TsbPd mode is enabled
5617 * Hold received packets until its time to 'play' them, at PktTimeStamp + TsbPdDelay.
5618 */
5619 void * srt::CUDT::tsbpd(void* param)
5620 {
5621 CUDT* self = (CUDT*)param;
5622
5623 THREAD_STATE_INIT("SRT:TsbPd");
5624
5625 #if ENABLE_BONDING
5626 // Make the TSBPD thread a "client" of the group,
5627 // which will ensure that the group will not be physically
5628 // deleted until this thread exits.
5629 // NOTE: DO NOT LEAD TO EVER CANCEL THE THREAD!!!
5630 CUDTUnited::GroupKeeper gkeeper(self->uglobal(), self->m_parent);
5631 #endif
5632
5633 CUniqueSync recvdata_lcc (self->m_RecvLock, self->m_RecvDataCond);
5634 CSync tsbpd_cc(self->m_RcvTsbPdCond, recvdata_lcc.locker());
5635
5636 self->m_bTsbPdNeedsWakeup = true;
5637 while (!self->m_bClosing)
5638 {
5639 steady_clock::time_point tsNextDelivery; // Next packet delivery time
5640 bool rxready = false;
5641 #if ENABLE_BONDING
5642 bool shall_update_group = false;
5643 #endif
5644
5645 INCREMENT_THREAD_ITERATIONS();
5646
5647 enterCS(self->m_RcvBufferLock);
5648 const steady_clock::time_point tnow = steady_clock::now();
5649
5650 self->m_pRcvBuffer->updRcvAvgDataSize(tnow);
5651 const srt::CRcvBuffer::PacketInfo info = self->m_pRcvBuffer->getFirstValidPacketInfo();
5652
5653 const bool is_time_to_deliver = !is_zero(info.tsbpd_time) && (tnow >= info.tsbpd_time);
5654 tsNextDelivery = info.tsbpd_time;
5655
5656 #if ENABLE_HEAVY_LOGGING
5657 if (info.seqno == SRT_SEQNO_NONE)
5658 {
5659 HLOGC(tslog.Debug, log << self->CONID() << "sok/tsbpd: packet check: NO PACKETS");
5660 }
5661 else
5662 {
5663 HLOGC(tslog.Debug, log << self->CONID() << "sok/tsbpd: packet check: %"
5664 << info.seqno << " T=" << FormatTime(tsNextDelivery)
5665 << " diff-now-playtime=" << FormatDuration(tnow - tsNextDelivery)
5666 << " ready=" << is_time_to_deliver
5667 << " ondrop=" << info.seq_gap);
5668 }
5669 #endif
5670
5671 if (!self->m_bTLPktDrop)
5672 {
5673 rxready = !info.seq_gap && is_time_to_deliver;
5674 }
5675 else if (is_time_to_deliver)
5676 {
5677 rxready = true;
5678 if (info.seq_gap)
5679 {
5680 const int iDropCnt SRT_ATR_UNUSED = self->rcvDropTooLateUpTo(info.seqno);
5681 #if ENABLE_BONDING
5682 shall_update_group = true;
5683 #endif
5684
5685 #if ENABLE_LOGGING
5686 const int64_t timediff_us = count_microseconds(tnow - info.tsbpd_time);
5687 #if ENABLE_HEAVY_LOGGING
5688 HLOGC(tslog.Debug,
5689 log << self->CONID() << "tsbpd: DROPSEQ: up to seqno %" << CSeqNo::decseq(info.seqno) << " ("
5690 << iDropCnt << " packets) playable at " << FormatTime(info.tsbpd_time) << " delayed "
5691 << (timediff_us / 1000) << "." << std::setw(3) << std::setfill('0') << (timediff_us % 1000) << " ms");
5692 #endif
5693 string why;
5694 if (self->frequentLogAllowed(FREQLOGFA_RCV_DROPPED, tnow, (why)))
5695 {
5696 LOGC(brlog.Warn, log << self->CONID() << "RCV-DROPPED " << iDropCnt << " packet(s). Packet seqno %" << info.seqno
5697 << " delayed for " << (timediff_us / 1000) << "." << std::setw(3) << std::setfill('0')
5698 << (timediff_us % 1000) << " ms " << why);
5699 }
5700 #if SRT_ENABLE_FREQUENT_LOG_TRACE
5701 else
5702 {
5703 LOGC(brlog.Warn, log << "SUPPRESSED: RCV-DROPPED LOG: " << why);
5704 }
5705 #endif
5706 #endif
5707
5708 tsNextDelivery = steady_clock::time_point(); // Ready to read, nothing to wait for.
5709 }
5710 }
5711 leaveCS(self->m_RcvBufferLock);
5712
5713 if (rxready)
5714 {
5715 HLOGC(tslog.Debug,
5716 log << self->CONID() << "tsbpd: PLAYING PACKET seq=" << info.seqno << " (belated "
5717 << FormatDuration<DUNIT_MS>(steady_clock::now() - info.tsbpd_time) << ")");
5718 /*
5719 * There are packets ready to be delivered
5720 * signal a waiting "recv" call if there is any data available
5721 */
5722 if (self->m_config.bSynRecving)
5723 {
5724 recvdata_lcc.notify_one();
5725 }
5726 /*
5727 * Set EPOLL_IN to wakeup any thread waiting on epoll
5728 */
5729 self->uglobal().m_EPoll.update_events(self->m_SocketID, self->m_sPollID, SRT_EPOLL_IN, true);
5730 #if ENABLE_BONDING
5731 // If this is NULL, it means:
5732 // - the socket never was a group member
5733 // - the socket was a group member, but:
5734 // - was just removed as a part of closure
5735 // - and will never be member of the group anymore
5736
5737 // If this is not NULL, it means:
5738 // - This socket is currently member of the group
5739 // - This socket WAS a member of the group, though possibly removed from it already, BUT:
5740 // - the group that this socket IS OR WAS member of is in the GroupKeeper
5741 // - the GroupKeeper prevents the group from being deleted
5742 // - it is then completely safe to access the group here,
5743 // EVEN IF THE SOCKET THAT WAS ITS MEMBER IS BEING DELETED.
5744
5745 // It is ensured that the group object exists here because GroupKeeper
5746 // keeps it busy, even if you just closed the socket, remove it as a member
5747 // or even the group is empty and was explicitly closed.
5748 if (gkeeper.group)
5749 {
5750 // Functions called below will lock m_GroupLock, which in hierarchy
5751 // lies after m_RecvLock. Must unlock m_RecvLock to be able to lock
5752 // m_GroupLock inside the calls.
5753 InvertedLock unrecv(self->m_RecvLock);
5754 // The current "APP reader" needs to simply decide as to whether
5755 // the next CUDTGroup::recv() call should return with no blocking or not.
5756 // When the group is read-ready, it should update its pollers as it sees fit.
5757
5758 // NOTE: this call will set lock to m_IncludedGroup->m_GroupLock
5759 HLOGC(tslog.Debug, log << self->CONID() << "tsbpd: GROUP: checking if %" << info.seqno << " makes group readable");
5760 gkeeper.group->updateReadState(self->m_SocketID, info.seqno);
5761
5762 if (shall_update_group)
5763 {
5764 // A group may need to update the parallelly used idle links,
5765 // should it have any. Pass the current socket position in order
5766 // to skip it from the group loop.
5767 // NOTE: SELF LOCKING.
5768 gkeeper.group->updateLatestRcv(self->m_parent);
5769 }
5770 }
5771
5772 // After re-acquisition of the m_RecvLock, re-check the closing flag
5773 if (self->m_bClosing)
5774 {
5775 break;
5776 }
5777 #endif
5778 CGlobEvent::triggerEvent();
5779 tsNextDelivery = steady_clock::time_point(); // Ready to read, nothing to wait for.
5780 }
5781
5782 // We may just briefly unlocked the m_RecvLock, so we need to check m_bClosing again to avoid deadlock.
5783 if (self->m_bClosing)
5784 break;
5785
5786 SRT_ATR_UNUSED bool bWokeUpOnSignal = true;
5787
5788 if (!is_zero(tsNextDelivery))
5789 {
5790 IF_HEAVY_LOGGING(const steady_clock::duration timediff = tsNextDelivery - tnow);
5791 /*
5792 * Buffer at head of queue is not ready to play.
5793 * Schedule wakeup when it will be.
5794 */
5795 self->m_bTsbPdNeedsWakeup = false;
5796 HLOGC(tslog.Debug,
5797 log << self->CONID() << "tsbpd: FUTURE PACKET seq=" << info.seqno
5798 << " T=" << FormatTime(tsNextDelivery) << " - waiting " << FormatDuration<DUNIT_MS>(timediff));
5799 THREAD_PAUSED();
5800 bWokeUpOnSignal = tsbpd_cc.wait_until(tsNextDelivery);
5801 THREAD_RESUMED();
5802 HLOGC(tslog.Debug, log << self->CONID() << "tsbpd: WAKE UP on " << (bWokeUpOnSignal? "SIGNAL" : "TIMEOUIT") << "!!!");
5803 }
5804 else
5805 {
5806 /*
5807 * We have just signaled epoll; or
5808 * receive queue is empty; or
5809 * next buffer to deliver is not in receive queue (missing packet in sequence).
5810 *
5811 * Block until woken up by one of the following event:
5812 * - All ready-to-play packets have been pulled and EPOLL_IN cleared (then loop to block until next pkt time
5813 * if any)
5814 * - New buffers ACKed
5815 * - Closing the connection
5816 */
5817 HLOGC(tslog.Debug, log << self->CONID() << "tsbpd: no data, scheduling wakeup at ack");
5818 self->m_bTsbPdNeedsWakeup = true;
5819 THREAD_PAUSED();
5820 tsbpd_cc.wait();
5821 THREAD_RESUMED();
5822 }
5823
5824 HLOGC(tslog.Debug,
5825 log << self->CONID() << "tsbpd: WAKE UP [" << (bWokeUpOnSignal ? "signal" : "timeout") << "]!!! - "
5826 << "NOW=" << FormatTime(steady_clock::now()));
5827 }
5828 THREAD_EXIT();
5829 HLOGC(tslog.Debug, log << self->CONID() << "tsbpd: EXITING");
5830 return NULL;
5831 }
5832
5833 int srt::CUDT::rcvDropTooLateUpTo(int seqno, DropReason reason)
5834 {
5835 // Make sure that it would not drop over m_iRcvCurrSeqNo, which may break senders.
5836 if (CSeqNo::seqcmp(seqno, CSeqNo::incseq(m_iRcvCurrSeqNo)) > 0)
5837 seqno = CSeqNo::incseq(m_iRcvCurrSeqNo);
5838
5839 dropFromLossLists(SRT_SEQNO_NONE, CSeqNo::decseq(seqno));
5840
5841 const std::pair<int, int> iDropDiscardedPkts = m_pRcvBuffer->dropUpTo(seqno);
5842 const int iDropCnt = iDropDiscardedPkts.first;
5843 const int iDiscardedCnt = iDropDiscardedPkts.second;
5844 const int iDropCntTotal = iDropCnt + iDiscardedCnt;
5845
5846 // In case of DROP_TOO_LATE discarded packets should also be counted because they are not read from another member socket.
5847 const int iDropStatCnt = (reason == DROP_DISCARD) ? iDropCnt : iDropCntTotal;
5848 if (iDropStatCnt > 0)
5849 {
5850 enterCS(m_StatsLock);
5851 // Estimate dropped bytes from average payload size.
5852 const uint64_t avgpayloadsz = m_pRcvBuffer->getRcvAvgPayloadSize();
5853 m_stats.rcvr.dropped.count(stats::BytesPackets(iDropStatCnt * avgpayloadsz, (uint32_t)iDropStatCnt));
5854 leaveCS(m_StatsLock);
5855 }
5856 return iDropCntTotal;
5857 }
5858
5859 void srt::CUDT::setInitialRcvSeq(int32_t isn)
5860 {
5861 m_iRcvLastAck = isn;
5862 #ifdef ENABLE_LOGGING
5863 m_iDebugPrevLastAck = isn;
5864 #endif
5865 m_iRcvLastAckAck = isn;
5866 m_iRcvCurrSeqNo = CSeqNo::decseq(isn);
5867
5868 sync::ScopedLock rb(m_RcvBufferLock);
5869 if (m_pRcvBuffer)
5870 {
5871 if (!m_pRcvBuffer->empty())
5872 {
5873 LOGC(cnlog.Error, log << CONID() << "IPE: setInitialRcvSeq expected empty RCV buffer. Dropping all.");
5874 const int iDropCnt = m_pRcvBuffer->dropAll();
5875 const uint64_t avgpayloadsz = m_pRcvBuffer->getRcvAvgPayloadSize();
5876 sync::ScopedLock sl(m_StatsLock);
5877 m_stats.rcvr.dropped.count(stats::BytesPackets(iDropCnt * avgpayloadsz, (uint32_t) iDropCnt));
5878 }
5879
5880 m_pRcvBuffer->setStartSeqNo(isn);
5881 }
5882 }
5883
5884 bool srt::CUDT::prepareConnectionObjects(const CHandShake &hs, HandshakeSide hsd, CUDTException *eout)
5885 {
5886 // This will be lazily created due to being the common
5887 // code with HSv5 rendezvous, in which this will be run
5888 // in a little bit "randomly selected" moment, but must
5889 // be run once in the whole connection process.
5890 if (m_pCryptoControl)
5891 {
5892 HLOGC(rslog.Debug, log << CONID() << "prepareConnectionObjects: (lazy) already created.");
5893 return true;
5894 }
5895
5896 // HSv5 is always bidirectional
5897 const bool bidirectional = (hs.m_iVersion > HS_VERSION_UDT4);
5898
5899 // HSD_DRAW is received only if this side is listener.
5900 // If this side is caller with HSv5, HSD_INITIATOR should be passed.
5901 // If this is a rendezvous connection with HSv5, the handshake role
5902 // is taken from m_SrtHsSide field.
5903 if (hsd == HSD_DRAW)
5904 {
5905 if (bidirectional)
5906 {
5907 hsd = HSD_RESPONDER; // In HSv5, listener is always RESPONDER and caller always INITIATOR.
5908 }
5909 else
5910 {
5911 hsd = m_config.bDataSender ? HSD_INITIATOR : HSD_RESPONDER;
5912 }
5913 }
5914
5915 if (!createCrypter(hsd, bidirectional)) // Make sure CC is created (lazy)
5916 {
5917 if (eout)
5918 *eout = CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0);
5919 m_RejectReason = SRT_REJ_RESOURCE;
5920 return false;
5921 }
5922
5923 return true;
5924 }
5925
5926 int srt::CUDT::getAuthTagSize() const
5927 {
5928 if (m_pCryptoControl && m_pCryptoControl->getCryptoMode() == CSrtConfig::CIPHER_MODE_AES_GCM)
5929 return HAICRYPT_AUTHTAG_MAX;
5930
5931 return 0;
5932 }
5933
5934 bool srt::CUDT::prepareBuffers(CUDTException* eout)
5935 {
5936 if (m_pSndBuffer)
5937 {
5938 HLOGC(rslog.Debug, log << CONID() << "prepareBuffers: (lazy) already created.");
5939 return true;
5940 }
5941
5942 try
5943 {
5944 // CryptoControl has to be initialized and in case of RESPONDER the KM REQ must be processed (interpretSrtHandshake(..)) for the crypto mode to be deduced.
5945 const int authtag = getAuthTagSize();
5946
5947 SRT_ASSERT(m_iMaxSRTPayloadSize != 0);
5948
5949 HLOGC(rslog.Debug, log << CONID() << "Creating buffers: snd-plsize=" << m_iMaxSRTPayloadSize
5950 << " snd-bufsize=" << 32
5951 << " authtag=" << authtag);
5952
5953 m_pSndBuffer = new CSndBuffer(AF_INET, 32, m_iMaxSRTPayloadSize, authtag);
5954 SRT_ASSERT(m_iPeerISN != -1);
5955 m_pRcvBuffer = new srt::CRcvBuffer(m_iPeerISN, m_config.iRcvBufSize, m_pRcvQueue->m_pUnitQueue, m_config.bMessageAPI);
5956 // After introducing lite ACK, the sndlosslist may not be cleared in time, so it requires twice a space.
5957 m_pSndLossList = new CSndLossList(m_iFlowWindowSize * 2);
5958 m_pRcvLossList = new CRcvLossList(m_config.iFlightFlagSize);
5959 }
5960 catch (...)
5961 {
5962 // Simply reject.
5963 if (eout)
5964 *eout = CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0);
5965 m_RejectReason = SRT_REJ_RESOURCE;
5966 return false;
5967 }
5968 return true;
5969 }
5970
5971 void srt::CUDT::rewriteHandshakeData(const sockaddr_any& peer, CHandShake& w_hs)
5972 {
5973 // this is a response handshake
5974 w_hs.m_iReqType = URQ_CONCLUSION;
5975 w_hs.m_iMSS = m_config.iMSS;
5976 w_hs.m_iFlightFlagSize = m_config.flightCapacity();
5977 w_hs.m_iID = m_SocketID;
5978
5979 if (w_hs.m_iVersion > HS_VERSION_UDT4)
5980 {
5981 // The version is agreed; this code is executed only in case
5982 // when AGENT is listener. In this case, conclusion response
5983 // must always contain HSv5 handshake extensions.
5984 w_hs.m_extension = true;
5985 }
5986
5987 CIPAddress::ntop(peer, (w_hs.m_piPeerIP));
5988 }
5989
5990 void srt::CUDT::acceptAndRespond(const sockaddr_any& agent, const sockaddr_any& peer, const CPacket& hspkt, CHandShake& w_hs)
5991 {
5992 HLOGC(cnlog.Debug, log << CONID() << "acceptAndRespond: setting up data according to handshake");
5993
5994 ScopedLock cg(m_ConnectionLock);
5995
5996 m_tsRcvPeerStartTime = steady_clock::time_point(); // will be set correctly at SRT HS
5997
5998 // Uses the smaller MSS between the peers
5999 m_config.iMSS = std::min(m_config.iMSS, w_hs.m_iMSS);
6000
6001 const size_t full_hdr_size = CPacket::UDP_HDR_SIZE + CPacket::HDR_SIZE;
6002 m_iMaxSRTPayloadSize = m_config.iMSS - full_hdr_size;
6003
6004 HLOGC(cnlog.Debug, log << CONID() << "acceptAndRespond: PAYLOAD SIZE: " << m_iMaxSRTPayloadSize);
6005
6006 // exchange info for maximum flow window size
6007 m_iFlowWindowSize = w_hs.m_iFlightFlagSize;
6008 m_iPeerISN = w_hs.m_iISN;
6009 setInitialRcvSeq(m_iPeerISN);
6010 m_iRcvCurrPhySeqNo = CSeqNo::decseq(w_hs.m_iISN);
6011
6012 m_PeerID = w_hs.m_iID;
6013
6014 // use peer's ISN and send it back for security check
6015 m_iISN = w_hs.m_iISN;
6016
6017 setInitialSndSeq(m_iISN);
6018 m_SndLastAck2Time = steady_clock::now();
6019
6020 // get local IP address and send the peer its IP address (because UDP cannot get local IP address)
6021 memcpy((m_piSelfIP), w_hs.m_piPeerIP, sizeof m_piSelfIP);
6022 m_parent->m_SelfAddr = agent;
6023 CIPAddress::pton((m_parent->m_SelfAddr), m_piSelfIP, peer);
6024
6025 rewriteHandshakeData(peer, (w_hs));
6026
6027
6028 // Prepare all structures
6029 if (!prepareConnectionObjects(w_hs, HSD_DRAW, 0))
6030 {
6031 HLOGC(cnlog.Debug,
6032 log << CONID() << "acceptAndRespond: prepareConnectionObjects failed - responding with REJECT.");
6033 // If the SRT Handshake extension was provided and wasn't interpreted
6034 // correctly, the connection should be rejected.
6035 //
6036 // Respond with the rejection message and exit with exception
6037 // so that the caller will know that this new socket should be deleted.
6038 w_hs.m_iReqType = URQFailure(m_RejectReason);
6039 throw CUDTException(MJ_SETUP, MN_REJECTED, 0);
6040 }
6041 // Since now you can use m_pCryptoControl
6042
6043 CInfoBlock ib;
6044 ib.m_iIPversion = peer.family();
6045 CInfoBlock::convert(peer, ib.m_piIP);
6046 if (m_pCache->lookup(&ib) >= 0)
6047 {
6048 m_iSRTT = ib.m_iSRTT;
6049 m_iRTTVar = ib.m_iSRTT / 2;
6050 m_iBandwidth = ib.m_iBandwidth;
6051 }
6052
6053 #if SRT_DEBUG_RTT
6054 s_rtt_trace.trace(steady_clock::now(), "Accept", -1, -1,
6055 m_bIsFirstRTTReceived, -1, m_iSRTT, m_iRTTVar);
6056 #endif
6057
6058 m_PeerAddr = peer;
6059
6060 // This should extract the HSREQ and KMREQ portion in the handshake packet.
6061 // This could still be a HSv4 packet and contain no such parts, which will leave
6062 // this entity as "non-SRT-handshaken", and await further HSREQ and KMREQ sent
6063 // as UMSG_EXT.
6064 uint32_t kmdata[SRTDATA_MAXSIZE];
6065 size_t kmdatasize = SRTDATA_MAXSIZE;
6066 if (!interpretSrtHandshake(w_hs, hspkt, (kmdata), (&kmdatasize)))
6067 {
6068 HLOGC(cnlog.Debug,
6069 log << CONID() << "acceptAndRespond: interpretSrtHandshake failed - responding with REJECT.");
6070 // If the SRT Handshake extension was provided and wasn't interpreted
6071 // correctly, the connection should be rejected.
6072 //
6073 // Respond with the rejection message and return false from
6074 // this function so that the caller will know that this new
6075 // socket should be deleted.
6076 w_hs.m_iReqType = URQFailure(m_RejectReason);
6077 throw CUDTException(MJ_SETUP, MN_REJECTED, 0);
6078 }
6079
6080 #if ENABLE_BONDING
6081 m_ConnectionLock.unlock();
6082 // The socket and the group are only linked to each other after interpretSrtHandshake(..) has been called.
6083 // Keep the group alive for the lifetime of this function,
6084 // and do it BEFORE acquiring m_ConnectionLock to avoid
6085 // lock inversion.
6086 // This will check if a socket belongs to a group and if so
6087 // it will remember this group and keep it alive here.
6088 CUDTUnited::GroupKeeper group_keeper(uglobal(), m_parent);
6089 m_ConnectionLock.lock();
6090 #endif
6091
6092 if (!prepareBuffers(NULL))
6093 {
6094 HLOGC(cnlog.Debug,
6095 log << CONID() << "acceptAndRespond: prepareConnectionObjects failed - responding with REJECT.");
6096 // If the SRT buffers failed to be allocated,
6097 // the connection must be rejected.
6098 //
6099 // Respond with the rejection message and exit with exception
6100 // so that the caller will know that this new socket should be deleted.
6101 w_hs.m_iReqType = URQFailure(m_RejectReason);
6102 throw CUDTException(MJ_SETUP, MN_REJECTED, 0);
6103 }
6104
6105 // Synchronize the time NOW because the following function is about
6106 // to use the start time to pass it to the receiver buffer data.
6107 bool have_group = false;
6108
6109 {
6110 #if ENABLE_BONDING
6111 CUDTGroup* g = group_keeper.group;
6112 if (g)
6113 {
6114 // This is the last moment when this can be done.
6115 // The updateAfterSrtHandshake call will copy the receiver
6116 // start time to the receiver buffer data, so the correct
6117 // value must be set before this happens.
6118 synchronizeWithGroup(g);
6119 have_group = true;
6120 }
6121 #endif
6122 }
6123
6124 if (!have_group)
6125 {
6126 // This function will be called internally inside
6127 // synchronizeWithGroup(). This is just more complicated.
6128 updateAfterSrtHandshake(w_hs.m_iVersion);
6129 }
6130
6131 SRT_REJECT_REASON rr = setupCC();
6132 // UNKNOWN used as a "no error" value
6133 if (rr != SRT_REJ_UNKNOWN)
6134 {
6135 w_hs.m_iReqType = URQFailure(rr);
6136 m_RejectReason = rr;
6137 throw CUDTException(MJ_SETUP, MN_REJECTED, 0);
6138 }
6139
6140 // And of course, it is connected.
6141 m_bConnected = true;
6142
6143 // Register this socket for receiving data packets.
6144 m_pRNode->m_bOnList = true;
6145 m_pRcvQueue->setNewEntry(this);
6146
6147 // Save the handshake in m_ConnRes in case when needs repeating.
6148 m_ConnRes = w_hs;
6149
6150 // Send the response to the peer, see listen() for more discussions
6151 // about this.
6152 // TODO: Here create CONCLUSION RESPONSE with:
6153 // - just the UDT handshake, if HS_VERSION_UDT4,
6154 // - if higher, the UDT handshake, the SRT HSRSP, the SRT KMRSP.
6155 size_t size = m_iMaxSRTPayloadSize;
6156 // Allocate the maximum possible memory for an SRT payload.
6157 // This is a maximum you can send once.
6158 CPacket rsppkt;
6159 rsppkt.setControl(UMSG_HANDSHAKE);
6160 rsppkt.allocate(size);
6161
6162 // This will serialize the handshake according to its current form.
6163 HLOGC(cnlog.Debug,
6164 log << CONID()
6165 << "acceptAndRespond: creating CONCLUSION response (HSv5: with HSRSP/KMRSP) buffer size=" << size);
6166 if (!createSrtHandshake(SRT_CMD_HSRSP, SRT_CMD_KMRSP, kmdata, kmdatasize, (rsppkt), (w_hs)))
6167 {
6168 LOGC(cnlog.Error, log << CONID() << "acceptAndRespond: error creating handshake response");
6169 throw CUDTException(MJ_SETUP, MN_REJECTED, 0);
6170 }
6171
6172 // We can safely assign it here stating that this has passed the cookie test.
6173 m_SourceAddr = hspkt.udpDestAddr();
6174
6175 #if ENABLE_HEAVY_LOGGING
6176 {
6177 // To make sure what REALLY is being sent, parse back the handshake
6178 // data that have been just written into the buffer.
6179 CHandShake debughs;
6180 debughs.load_from(rsppkt.m_pcData, rsppkt.getLength());
6181 HLOGC(cnlog.Debug,
6182 log << CONID() << "acceptAndRespond: sending HS from agent @"
6183 << debughs.m_iID << " to peer @" << rsppkt.id()
6184 << "HS:" << debughs.show()
6185 << " sourceIP=" << m_SourceAddr.str());
6186 }
6187 #endif
6188
6189 // NOTE: BLOCK THIS instruction in order to cause the final
6190 // handshake to be missed and cause the problem solved in PR #417.
6191 // When missed this message, the caller should not accept packets
6192 // coming as connected, but continue repeated handshake until finally
6193 // received the listener's handshake.
6194 addressAndSend((rsppkt));
6195 }
6196
6197 bool srt::CUDT::frequentLogAllowed(size_t logid, const time_point& tnow, std::string& w_why)
6198 {
6199 #ifndef SRT_LOG_SLOWDOWN_FREQ_MS
6200 #define SRT_LOG_SLOWDOWN_FREQ_MS 1000
6201 #endif
6202
6203 bool is_suppressed = IsSet(m_LogSlowDownExpired, BIT(logid));
6204 const bool isnow = (m_tsLogSlowDown[logid].load() + milliseconds_from(SRT_LOG_SLOWDOWN_FREQ_MS)) <= tnow;
6205 if (isnow)
6206 {
6207 // Theoretically this should prevent other calls of this function to take
6208 // set their values simultaneously, but if it happened that the time is
6209 // also set, this section will not fire for the other log, if it didn't do
6210 // the check yet.
6211 m_LogSlowDownExpired.store(uint8_t(BIT(logid))); // Clear all other bits
6212
6213 // Note: it may happen that two threads could intermix one another between
6214 // the check and setting up, but this will at worst case set the slightly
6215 // later time again.
6216 m_tsLogSlowDown[logid].store(tnow);
6217
6218 is_suppressed = false;
6219
6220 const int supr = m_aSuppressedMsg[logid];
6221
6222 if (supr > 0)
6223 w_why = Sprint("++SUPPRESSED: ", supr);
6224 m_aSuppressedMsg[logid] = 0;
6225 }
6226 else
6227 {
6228 w_why = Sprint("Too early - last one was ", FormatDuration<DUNIT_MS>(tnow - m_tsLogSlowDown[logid].load()));
6229 // Set YOUR OWN bit, atomically.
6230 m_LogSlowDownExpired |= uint8_t(BIT(logid));
6231 ++m_aSuppressedMsg[logid];
6232 }
6233
6234 return !is_suppressed;
6235 }
6236
6237 // This function is required to be called when a caller receives an INDUCTION
6238 // response from the listener and would like to create a CONCLUSION that includes
6239 // the SRT handshake extension. This extension requires that the crypter object
6240 // be created, but it's still too early for it to be completely configured.
6241 // This function then precreates the object so that the handshake extension can
6242 // be created, as this happens before the completion of the connection (and
6243 // therefore configuration of the crypter object), which can only take place upon
6244 // reception of CONCLUSION response from the listener.
6245 bool srt::CUDT::createCrypter(HandshakeSide side, bool bidirectional)
6246 {
6247 // Lazy initialization
6248 if (m_pCryptoControl)
6249 return true;
6250
6251 // Write back this value, when it was just determined.
6252 m_SrtHsSide = side;
6253
6254 m_pCryptoControl.reset(new CCryptoControl(m_SocketID));
6255
6256 // XXX These below are a little bit controversial.
6257 // These data should probably be filled only upon
6258 // reception of the conclusion handshake - otherwise
6259 // they have outdated values.
6260 m_pCryptoControl->setCryptoSecret(m_config.CryptoSecret);
6261
6262 const bool useGcm153 = m_uPeerSrtVersion <= SrtVersion(1, 5, 3);
6263
6264 if (bidirectional || m_config.bDataSender)
6265 {
6266 HLOGC(rslog.Debug, log << CONID() << "createCrypter: setting RCV/SND KeyLen=" << m_config.iSndCryptoKeyLen);
6267 m_pCryptoControl->setCryptoKeylen(m_config.iSndCryptoKeyLen);
6268 }
6269
6270 return m_pCryptoControl->init(side, m_config, bidirectional, useGcm153);
6271 }
6272
6273 SRT_REJECT_REASON srt::CUDT::setupCC()
6274 {
6275 // Prepare configuration object,
6276 // Create the CCC object and configure it.
6277
6278 // UDT also sets back the congestion window: ???
6279 // m_iCongestionWindow = m_pCC->m_dCWndSize;
6280
6281 // XXX Not sure about that. May happen that AGENT wants
6282 // tsbpd mode, but PEER doesn't, even in bidirectional mode.
6283 // This way, the reception side should get precedence.
6284 // if (bidirectional || m_config.bDataSender || m_bTwoWayData)
6285 // m_bPeerTsbPd = m_bTSBPD;
6286
6287 // SrtCongestion will retrieve whatever parameters it needs
6288 // from *this.
6289
6290 bool res = m_CongCtl.select(m_config.sCongestion.str());
6291 if (!res || !m_CongCtl.configure(this))
6292 {
6293 return SRT_REJ_CONGESTION;
6294 }
6295
6296 // Configure filter module
6297 if (!m_config.sPacketFilterConfig.empty())
6298 {
6299 // This string, when nonempty, defines that the corrector shall be
6300 // configured. Otherwise it's left uninitialized.
6301
6302 // At this point we state everything is checked and the appropriate
6303 // corrector type is already selected, so now create it.
6304 HLOGC(pflog.Debug, log << CONID() << "filter: Configuring: " << m_config.sPacketFilterConfig.c_str());
6305 bool status = true;
6306 try
6307 {
6308 // The filter configurer is build the way that allows to quit immediately
6309 // exit by exception, but the exception is meant for the filter only.
6310 status = m_PacketFilter.configure(this, m_pRcvQueue->m_pUnitQueue, m_config.sPacketFilterConfig.str());
6311 }
6312 catch (CUDTException& )
6313 {
6314 status = false;
6315 }
6316
6317 if (!status)
6318 return SRT_REJ_FILTER;
6319
6320 m_PktFilterRexmitLevel = m_PacketFilter.arqLevel();
6321 }
6322 else
6323 {
6324 // When we have no filter, ARQ should work in ALWAYS mode.
6325 m_PktFilterRexmitLevel = SRT_ARQ_ALWAYS;
6326 }
6327
6328 // Override the value of minimum NAK interval, per SrtCongestion's wish.
6329 // When default 0 value is returned, the current value set by CUDT
6330 // is preserved.
6331 const steady_clock::duration min_nak = microseconds_from(m_CongCtl->minNAKInterval());
6332 if (min_nak != steady_clock::duration::zero())
6333 m_tdMinNakInterval = min_nak;
6334
6335 // Update timers
6336 const steady_clock::time_point currtime = steady_clock::now();
6337 m_tsLastRspTime.store(currtime);
6338 m_tsNextACKTime.store(currtime + m_tdACKInterval);
6339 m_tsNextNAKTime.store(currtime + m_tdNAKInterval);
6340 m_tsLastRspAckTime = currtime;
6341 m_tsLastSndTime.store(currtime);
6342
6343 #ifdef ENABLE_RATE_MEASUREMENT
6344 // XXX NOTE: use IPv4 or IPv6 as applicable!
6345 HLOGC(bslog.Debug, log << CONID() << "RATE-MEASUREMENT: initializing time TS=" << FormatTime(currtime));
6346 m_SndRegularMeasurement.init(currtime, CPacket::UDP_HDR_SIZE);
6347 m_SndRexmitMeasurement.init(currtime, CPacket::UDP_HDR_SIZE);
6348 #endif
6349
6350 HLOGC(rslog.Debug,
6351 log << CONID() << "setupCC: setting parameters: mss=" << m_config.iMSS << " maxCWNDSize/FlowWindowSize="
6352 << m_iFlowWindowSize << " rcvrate=" << m_iDeliveryRate << "p/s (" << m_iByteDeliveryRate << "B/S)"
6353 << " rtt=" << m_iSRTT << " bw=" << m_iBandwidth);
6354
6355 if (!updateCC(TEV_INIT, EventVariant(TEV_INIT_RESET)))
6356 {
6357 LOGC(rslog.Error, log << CONID() << "setupCC: IPE: resources not yet initialized!");
6358 return SRT_REJ_IPE;
6359 }
6360 return SRT_REJ_UNKNOWN;
6361 }
6362
6363 void srt::CUDT::considerLegacySrtHandshake(const steady_clock::time_point &timebase)
6364 {
6365 // Do a fast pre-check first - this simply declares that agent uses HSv5
6366 // and the legacy SRT Handshake is not to be done. Second check is whether
6367 // agent is sender (=initiator in HSv4).
6368 if (!isOPT_TsbPd() || !m_config.bDataSender)
6369 return;
6370
6371 if (m_iSndHsRetryCnt <= 0)
6372 {
6373 HLOGC(cnlog.Debug, log << CONID() << "Legacy HSREQ: not needed, expire counter=" << m_iSndHsRetryCnt);
6374 return;
6375 }
6376
6377 const steady_clock::time_point now = steady_clock::now();
6378 if (!is_zero(timebase))
6379 {
6380 // Then this should be done only if it's the right time,
6381 // the TSBPD mode is on, and when the counter is "still rolling".
6382 /*
6383 * SRT Handshake with peer:
6384 * If...
6385 * - we want TsbPd mode; and
6386 * - we have not tried more than CSRTCC_MAXRETRY times (peer may not be SRT); and
6387 * - and did not get answer back from peer
6388 * - last sent handshake req should have been replied (RTT*1.5 elapsed); and
6389 * then (re-)send handshake request.
6390 */
6391 if (timebase > now) // too early
6392 {
6393 HLOGC(cnlog.Debug,
6394 log << CONID() << "Legacy HSREQ: TOO EARLY, will still retry " << m_iSndHsRetryCnt << " times");
6395 return;
6396 }
6397 }
6398 // If 0 timebase, it means that this is the initial sending with the very first
6399 // payload packet sent. Send only if this is still set to maximum+1 value.
6400 else if (m_iSndHsRetryCnt < SRT_MAX_HSRETRY + 1)
6401 {
6402 HLOGC(cnlog.Debug,
6403 log << CONID() << "Legacy HSREQ: INITIAL, REPEATED, so not to be done. Will repeat on sending "
6404 << m_iSndHsRetryCnt << " times");
6405 return;
6406 }
6407
6408 HLOGC(cnlog.Debug,
6409 log << CONID() << "Legacy HSREQ: SENDING, will repeat " << m_iSndHsRetryCnt << " times if no response");
6410 m_iSndHsRetryCnt--;
6411 m_tsSndHsLastTime = now;
6412 sendSrtMsg(SRT_CMD_HSREQ);
6413 }
6414
6415 void srt::CUDT::checkSndTimers()
6416 {
6417 if (m_SrtHsSide == HSD_INITIATOR)
6418 {
6419 HLOGC(cnlog.Debug,
6420 log << CONID() << "checkSndTimers: HS SIDE: INITIATOR, considering legacy handshake with timebase");
6421 // Legacy method for HSREQ, only if initiator.
6422 considerLegacySrtHandshake(m_tsSndHsLastTime + microseconds_from(m_iSRTT * 3 / 2));
6423 }
6424 else
6425 {
6426 HLOGC(cnlog.Debug,
6427 log << CONID()
6428 << "checkSndTimers: HS SIDE: " << (m_SrtHsSide == HSD_RESPONDER ? "RESPONDER" : "DRAW (IPE?)")
6429 << " - not considering legacy handshake");
6430 }
6431
6432 // Retransmit KM request after a timeout if there is no response (KM RSP).
6433 // Or send KM REQ in case of the HSv4.
6434 ScopedLock lck(m_ConnectionLock);
6435 if (m_pCryptoControl)
6436 m_pCryptoControl->sendKeysToPeer(this, SRTT());
6437 }
6438
6439 void srt::CUDT::checkSndKMRefresh()
6440 {
6441 // Do not apply the regenerated key to the to the receiver context.
6442 const bool bidir = false;
6443 if (m_pCryptoControl)
6444 m_pCryptoControl->regenCryptoKm(this, bidir);
6445 }
6446
6447 void srt::CUDT::addressAndSend(CPacket& w_pkt)
6448 {
6449 w_pkt.set_id(m_PeerID);
6450 setPacketTS(w_pkt, steady_clock::now());
6451
6452 // NOTE: w_pkt isn't modified in this call,
6453 // just in CChannel::sendto it's modified in place
6454 // before sending for performance purposes,
6455 // and then modification is undone. Logically then
6456 // there's no modification here.
6457 m_pSndQueue->sendto(m_PeerAddr, w_pkt, m_SourceAddr);
6458 }
6459
6460 // [[using maybe_locked(m_GlobControlLock, if called from breakSocket_LOCKED, usually from GC)]]
6461 // [[using maybe_locked(m_parent->m_ControlLock, if called from srt_close())]]
6462 bool srt::CUDT::closeInternal() ATR_NOEXCEPT
6463 {
6464 // NOTE: this function is called from within the garbage collector thread.
6465
6466 if (!m_bOpened)
6467 {
6468 return false;
6469 }
6470
6471 // IMPORTANT:
6472 // This function may block indefinitely, if called for a socket
6473 // that has m_bBroken == false or m_bConnected == true.
6474 // If it is intended to forcefully close the socket, make sure
6475 // that it's in response to a broken connection.
6476 HLOGC(smlog.Debug, log << CONID() << "closing socket");
6477
6478 if (m_config.Linger.l_onoff != 0)
6479 {
6480 const steady_clock::time_point entertime = steady_clock::now();
6481
6482 HLOGC(smlog.Debug, log << CONID() << "... (linger)");
6483 while (!m_bBroken && m_bConnected && (m_pSndBuffer->getCurrBufSize() > 0) &&
6484 (steady_clock::now() - entertime < seconds_from(m_config.Linger.l_linger)))
6485 {
6486 // linger has been checked by previous close() call and has expired
6487 if (m_tsLingerExpiration >= entertime)
6488 break;
6489
6490 if (!m_config.bSynSending)
6491 {
6492 // if this socket enables asynchronous sending, return immediately and let GC to close it later
6493 if (is_zero(m_tsLingerExpiration))
6494 m_tsLingerExpiration = entertime + seconds_from(m_config.Linger.l_linger);
6495
6496 HLOGC(smlog.Debug,
6497 log << CONID() << "CUDT::close: linger-nonblocking, setting expire time T="
6498 << FormatTime(m_tsLingerExpiration));
6499
6500 return false;
6501 }
6502
6503 #ifndef _WIN32
6504 timespec ts;
6505 ts.tv_sec = 0;
6506 ts.tv_nsec = 1000000;
6507 nanosleep(&ts, NULL);
6508 #else
6509 Sleep(1);
6510 #endif
6511 }
6512 }
6513
6514 // remove this socket from the snd queue
6515 if (m_bConnected)
6516 m_pSndQueue->m_pSndUList->remove(this);
6517
6518 /*
6519 * update_events below useless
6520 * removing usock for EPolls right after (update_usocks) clears it (in other HAI patch).
6521 *
6522 * What is in EPoll shall be the responsibility of the application, if it want local close event,
6523 * it would remove the socket from the EPoll after close.
6524 */
6525
6526 // Make a copy under a lock because other thread might access it
6527 // at the same time.
6528 enterCS(uglobal().m_EPoll.m_EPollLock);
6529 set<int> epollid = m_sPollID;
6530 leaveCS(uglobal().m_EPoll.m_EPollLock);
6531
6532 // trigger any pending IO events.
6533 HLOGC(smlog.Debug, log << CONID() << "close: SETTING ERR readiness on E" << Printable(epollid));
6534 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_ERR, true);
6535 // then remove itself from all epoll monitoring
6536 int no_events = 0;
6537 for (set<int>::iterator i = epollid.begin(); i != epollid.end(); ++i)
6538 {
6539 HLOGC(smlog.Debug, log << CONID() << "close: CLEARING subscription on E" << (*i));
6540 try
6541 {
6542 uglobal().m_EPoll.update_usock(*i, m_SocketID, &no_events);
6543 }
6544 catch (...)
6545 {
6546 // The goal of this loop is to remove all subscriptions in
6547 // the epoll system to this socket. If it's unsubscribed already,
6548 // that's even better.
6549 }
6550 HLOGC(smlog.Debug, log << CONID() << "close: removing E" << (*i) << " from back-subscribers");
6551 }
6552
6553 // Not deleting elements from m_sPollID inside the loop because it invalidates
6554 // the control iterator of the loop. Instead, all will be removed at once.
6555
6556 // IMPORTANT: there's theoretically little time between setting ERR readiness
6557 // and unsubscribing, however if there's an application waiting on this event,
6558 // it should be informed before this below instruction locks the epoll mutex.
6559 enterCS(uglobal().m_EPoll.m_EPollLock);
6560 m_sPollID.clear();
6561 leaveCS(uglobal().m_EPoll.m_EPollLock);
6562
6563 // XXX What's this, could any of the above actions make it !m_bOpened?
6564 if (!m_bOpened)
6565 {
6566 return true;
6567 }
6568
6569 // Inform the threads handler to stop.
6570 m_bClosing = true;
6571
6572 HLOGC(smlog.Debug, log << CONID() << "CLOSING STATE (closing=true). Acquiring connection lock");
6573
6574 ScopedLock connectguard(m_ConnectionLock);
6575
6576 // Signal the sender and recver if they are waiting for data.
6577 releaseSynch();
6578
6579 HLOGC(smlog.Debug, log << CONID() << "CLOSING, removing from listener/connector");
6580
6581 if (m_bListening)
6582 {
6583 m_bListening = false;
6584 bool removed SRT_ATR_UNUSED = m_pRcvQueue->removeListener(this);
6585 if (!removed)
6586 {
6587 LOGC(smlog.Error, log << CONID() << "CLOSING: IPE: listening=true but listener removal failed!");
6588 }
6589 }
6590 else if (m_bConnecting)
6591 {
6592 m_pRcvQueue->removeConnector(m_SocketID);
6593 }
6594
6595 if (m_bConnected)
6596 {
6597 if (!m_bShutdown)
6598 {
6599 HLOGC(smlog.Debug, log << CONID() << "CLOSING - sending SHUTDOWN to the peer @" << m_PeerID);
6600 sendCtrl(UMSG_SHUTDOWN);
6601 }
6602
6603 // Store current connection information.
6604 CInfoBlock ib;
6605 ib.m_iIPversion = m_PeerAddr.family();
6606 CInfoBlock::convert(m_PeerAddr, ib.m_piIP);
6607 ib.m_iSRTT = m_iSRTT;
6608 ib.m_iBandwidth = m_iBandwidth;
6609 m_pCache->update(&ib);
6610
6611 #if SRT_DEBUG_RTT
6612 s_rtt_trace.trace(steady_clock::now(), "Cache", -1, -1,
6613 m_bIsFirstRTTReceived, -1, m_iSRTT, -1);
6614 #endif
6615
6616 m_bConnected = false;
6617 }
6618
6619 HLOGC(smlog.Debug, log << CONID() << "CLOSING, joining send/receive threads");
6620
6621 // waiting all send and recv calls to stop
6622 ScopedLock sendguard(m_SendLock);
6623 ScopedLock recvguard(m_RecvLock);
6624
6625 // Locking m_RcvBufferLock to protect calling to m_pCryptoControl->decrypt((packet))
6626 // from the processData(...) function while resetting Crypto Control.
6627 enterCS(m_RcvBufferLock);
6628 if (m_pCryptoControl)
6629 m_pCryptoControl->close();
6630
6631 m_pCryptoControl.reset();
6632 leaveCS(m_RcvBufferLock);
6633
6634 m_uPeerSrtVersion = SRT_VERSION_UNK;
6635 m_tsRcvPeerStartTime = steady_clock::time_point();
6636
6637 m_bOpened = false;
6638
6639 return true;
6640 }
6641
6642 bool srt::CUDT::closeAtFork() ATR_NOEXCEPT
6643 {
6644 m_bShutdown = true;
6645 return closeInternal();
6646 }
6647
6648 int srt::CUDT::receiveBuffer(char *data, int len)
6649 {
6650 if (!m_CongCtl->checkTransArgs(SrtCongestion::STA_BUFFER, SrtCongestion::STAD_RECV, data, len, SRT_MSGTTL_INF, false))
6651 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
6652
6653 if (isOPT_TsbPd())
6654 {
6655 LOGP(arlog.Error, "recv: This function is not intended to be used in Live mode with TSBPD.");
6656 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
6657 }
6658
6659 UniqueLock recvguard(m_RecvLock);
6660
6661 if ((m_bBroken || m_bClosing) && !isRcvBufferReady())
6662 {
6663 if (m_bShutdown)
6664 {
6665 // For stream API, return 0 as a sign of EOF for transmission.
6666 // That's a bit controversial because theoretically the
6667 // UMSG_SHUTDOWN message may be lost as every UDP packet, although
6668 // another theory states that this will never happen because this
6669 // packet has a total size of 42 bytes and such packets are
6670 // declared as never dropped - but still, this is UDP so there's no
6671 // guarantee.
6672
6673 // The most reliable way to inform the party that the transmission
6674 // has ended would be to send a single empty packet (that is,
6675 // a data packet that contains only an SRT header in the UDP
6676 // payload), which is a normal data packet that can undergo
6677 // normal sequence check and retransmission rules, so it's ensured
6678 // that this packet will be received. Receiving such a packet should
6679 // make this function return 0, potentially also without breaking
6680 // the connection and potentially also with losing no ability to
6681 // send some larger portion of data next time.
6682 HLOGC(arlog.Debug, log << CONID() << "STREAM API, SHUTDOWN: marking as EOF");
6683 return 0;
6684 }
6685 HLOGC(arlog.Debug,
6686 log << CONID() << (m_config.bMessageAPI ? "MESSAGE" : "STREAM") << " API, " << (m_bShutdown ? "" : "no")
6687 << " SHUTDOWN. Reporting as BROKEN.");
6688 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
6689 }
6690
6691 CSync rcond (m_RecvDataCond, recvguard);
6692 CSync tscond (m_RcvTsbPdCond, recvguard);
6693 if (!isRcvBufferReady())
6694 {
6695 if (!m_config.bSynRecving)
6696 {
6697 throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0);
6698 }
6699
6700 // Kick TsbPd thread to schedule the next wakeup (if running)
6701 if (m_config.iRcvTimeOut < 0)
6702 {
6703 THREAD_PAUSED();
6704 while (stillConnected() && !isRcvBufferReady())
6705 {
6706 // Do not block forever, check connection status each 1 sec.
6707 rcond.wait_for(seconds_from(1));
6708 }
6709 THREAD_RESUMED();
6710 }
6711 else
6712 {
6713 const steady_clock::time_point exptime =
6714 steady_clock::now() + milliseconds_from(m_config.iRcvTimeOut);
6715 THREAD_PAUSED();
6716 while (stillConnected() && !isRcvBufferReady())
6717 {
6718 if (!rcond.wait_until(exptime)) // NOT means "not received a signal"
6719 break; // timeout
6720 }
6721 THREAD_RESUMED();
6722 }
6723 }
6724
6725 // throw an exception if not connected
6726 if (!m_bConnected)
6727 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
6728
6729 if ((m_bBroken || m_bClosing) && !isRcvBufferReady())
6730 {
6731 // See at the beginning
6732 if (!m_config.bMessageAPI && m_bShutdown)
6733 {
6734 HLOGC(arlog.Debug, log << CONID() << "STREAM API, SHUTDOWN: marking as EOF");
6735 return 0;
6736 }
6737 HLOGC(arlog.Debug,
6738 log << CONID() << (m_config.bMessageAPI ? "MESSAGE" : "STREAM") << " API, " << (m_bShutdown ? "" : "no")
6739 << " SHUTDOWN. Reporting as BROKEN.");
6740
6741 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
6742 }
6743
6744 enterCS(m_RcvBufferLock);
6745 const int res = m_pRcvBuffer->readBuffer(data, len);
6746 leaveCS(m_RcvBufferLock);
6747
6748 /* Kick TsbPd thread to schedule next wakeup (if running) */
6749 if (m_bTsbPd)
6750 {
6751 HLOGP(tslog.Debug, "Ping TSBPD thread to schedule wakeup");
6752 tscond.notify_one_locked(recvguard);
6753 }
6754 else
6755 {
6756 HLOGP(tslog.Debug, "NOT pinging TSBPD - not set");
6757 }
6758
6759 if (!isRcvBufferReady())
6760 {
6761 // read is not available any more
6762 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
6763 }
6764
6765 if ((res <= 0) && (m_config.iRcvTimeOut >= 0))
6766 throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
6767
6768 return res;
6769 }
6770
6771 // [[using maybe_locked(CUDTGroup::m_GroupLock, m_parent->m_GroupOf != NULL)]];
6772 // [[using locked(m_SendLock)]];
6773 int srt::CUDT::sndDropTooLate()
6774 {
6775 if (!m_bPeerTLPktDrop)
6776 return 0;
6777
6778 if (!m_config.bMessageAPI)
6779 {
6780 LOGC(aslog.Error, log << CONID() << "The SRTO_TLPKTDROP flag can only be used with message API.");
6781 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
6782 }
6783
6784 const time_point tnow = steady_clock::now();
6785 const int buffdelay_ms = (int) count_milliseconds(m_pSndBuffer->getBufferingDelay(tnow));
6786
6787 // high threshold (msec) at tsbpd_delay plus sender/receiver reaction time (2 * 10ms)
6788 // Minimum value must accommodate an I-Frame (~8 x average frame size)
6789 // >>need picture rate or app to set min threshold
6790 // >>using 1 sec for worse case 1 frame using all bit budget.
6791 // picture rate would be useful in auto SRT setting for min latency
6792 // XXX Make SRT_TLPKTDROP_MINTHRESHOLD_MS option-configurable
6793 const int threshold_ms = (m_config.iSndDropDelay >= 0)
6794 ? std::max(m_iPeerTsbPdDelay_ms + m_config.iSndDropDelay, +SRT_TLPKTDROP_MINTHRESHOLD_MS)
6795 + (2 * COMM_SYN_INTERVAL_US / 1000)
6796 : 0;
6797
6798 if (threshold_ms == 0 || buffdelay_ms <= threshold_ms)
6799 return 0;
6800
6801 // protect packet retransmission
6802 ScopedLock rcvlck(m_RecvAckLock);
6803 int dbytes;
6804 int32_t first_msgno;
6805 const int dpkts = m_pSndBuffer->dropLateData((dbytes), (first_msgno), tnow - milliseconds_from(threshold_ms));
6806 if (dpkts <= 0)
6807 return 0;
6808
6809 m_iFlowWindowSize = m_iFlowWindowSize + dpkts;
6810
6811 // If some packets were dropped update stats, socket state, loss list and the parent group if any.
6812 enterCS(m_StatsLock);
6813 m_stats.sndr.dropped.count(stats::BytesPackets((uint64_t) dbytes, (uint32_t) dpkts));
6814 leaveCS(m_StatsLock);
6815
6816 IF_HEAVY_LOGGING(const int32_t realack = m_iSndLastDataAck);
6817 const int32_t fakeack = CSeqNo::incseq(m_iSndLastDataAck, dpkts);
6818
6819 m_iSndLastAck = fakeack;
6820 m_iSndLastDataAck = fakeack;
6821
6822 const int32_t minlastack = CSeqNo::decseq(m_iSndLastDataAck);
6823 m_pSndLossList->removeUpTo(minlastack);
6824 /* If we dropped packets not yet sent, advance current position */
6825 // THIS MEANS: m_iSndCurrSeqNo = MAX(m_iSndCurrSeqNo, m_iSndLastDataAck-1)
6826 if (CSeqNo::seqcmp(m_iSndCurrSeqNo, minlastack) < 0)
6827 {
6828 m_iSndCurrSeqNo = minlastack;
6829 }
6830
6831 HLOGC(qslog.Debug,
6832 log << CONID() << "SND-DROP: %(" << realack << "-" << m_iSndCurrSeqNo << ") n=" << dpkts << "pkt " << dbytes
6833 << "B, span=" << buffdelay_ms << " ms, FIRST #" << first_msgno);
6834
6835 #if ENABLE_BONDING
6836 // This is done with a presumption that the group
6837 // exists and if this is not NULL, it means that this
6838 // function was called with locked m_GroupLock, as sendmsg2
6839 // function was called from inside CUDTGroup::send, which
6840 // locks the whole function.
6841 //
6842 // XXX This is true only because all existing groups are managed
6843 // groups, that is, sockets cannot be added or removed from group
6844 // manually, nor can send/recv operation be done on a single socket
6845 // from the API call directly. This should be extra verified, if that
6846 // changes in the future.
6847 //
6848 if (m_parent->m_GroupOf)
6849 {
6850 // What's important is that the lock on GroupLock cannot be applied
6851 // here, both because it might be applied already, that is, according
6852 // to the condition defined at this function's header, it is applied
6853 // under this condition. Hence ackMessage can be defined as 100% locked.
6854 m_parent->m_GroupOf->ackMessage(first_msgno);
6855 }
6856 #endif
6857
6858 return dpkts;
6859 }
6860
6861 int srt::CUDT::sendmsg(const char *data, int len, int msttl, bool inorder, int64_t srctime)
6862 {
6863 SRT_MSGCTRL mctrl = srt_msgctrl_default;
6864 mctrl.msgttl = msttl;
6865 mctrl.inorder = inorder;
6866 mctrl.srctime = srctime;
6867 return this->sendmsg2(data, len, (mctrl));
6868 }
6869
6870 // [[using maybe_locked(CUDTGroup::m_GroupLock, m_parent->m_GroupOf != NULL)]]
6871 // GroupLock is applied when this function is called from inside CUDTGroup::send,
6872 // which is the only case when the m_parent->m_GroupOf is not NULL.
6873 int srt::CUDT::sendmsg2(const char *data, int len, SRT_MSGCTRL& w_mctrl)
6874 {
6875 // throw an exception if not connected
6876 if (m_bBroken || m_bClosing)
6877 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
6878 else if (!m_bConnected || !m_CongCtl.ready())
6879 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
6880
6881 if (len <= 0)
6882 {
6883 LOGC(aslog.Error, log << CONID() << "INVALID: Data size for sending declared with length: " << len);
6884 return 0;
6885 }
6886
6887 if (w_mctrl.msgno != -1) // most unlikely, unless you use balancing groups
6888 {
6889 if (w_mctrl.msgno < 1 || w_mctrl.msgno > MSGNO_SEQ_MAX)
6890 {
6891 LOGC(aslog.Error,
6892 log << CONID() << "INVALID forced msgno " << w_mctrl.msgno << ": can be -1 (trap) or <1..."
6893 << MSGNO_SEQ_MAX << ">");
6894 throw CUDTException(MJ_NOTSUP, MN_INVAL);
6895 }
6896 }
6897
6898 int msttl = w_mctrl.msgttl;
6899 bool inorder = w_mctrl.inorder;
6900
6901 // Sendmsg isn't restricted to the congctl type, however the congctl
6902 // may want to have something to say here.
6903 // NOTE: SrtCongestion is also allowed to throw CUDTException() by itself!
6904 {
6905 SrtCongestion::TransAPI api = SrtCongestion::STA_MESSAGE;
6906 CodeMinor mn = MN_INVALMSGAPI;
6907 if (!m_config.bMessageAPI)
6908 {
6909 api = SrtCongestion::STA_BUFFER;
6910 mn = MN_INVALBUFFERAPI;
6911 }
6912
6913 if (!m_CongCtl->checkTransArgs(api, SrtCongestion::STAD_SEND, data, len, msttl, inorder))
6914 throw CUDTException(MJ_NOTSUP, mn, 0);
6915 }
6916
6917 // NOTE: the length restrictions differ in STREAM API and in MESSAGE API:
6918
6919 // - STREAM API:
6920 // At least 1 byte free sending buffer space is needed
6921 // (in practice, one unit buffer of 1456 bytes).
6922 // This function will send as much as possible, and return
6923 // how much was actually sent.
6924
6925 // - MESSAGE API:
6926 // At least so many bytes free in the sending buffer is needed,
6927 // as the length of the data, otherwise this function will block
6928 // or return MJ_AGAIN until this condition is satisfied. The EXACTLY
6929 // such number of data will be then written out, and this function
6930 // will effectively return either -1 (error) or the value of 'len'.
6931 // This call will be also rejected from upside when trying to send
6932 // out a message of a length that exceeds the total size of the sending
6933 // buffer (configurable by SRTO_SNDBUF).
6934
6935 if (m_config.bMessageAPI && len > int(m_config.iSndBufSize * m_iMaxSRTPayloadSize))
6936 {
6937 LOGC(aslog.Error,
6938 log << CONID() << "Message length (" << len << ") exceeds the size of sending buffer: "
6939 << (m_config.iSndBufSize * m_iMaxSRTPayloadSize) << ". Use SRTO_SNDBUF if needed.");
6940 throw CUDTException(MJ_NOTSUP, MN_XSIZE, 0);
6941 }
6942
6943 /* XXX
6944 This might be worth preserving for several occasions, but it
6945 must be at least conditional because it breaks backward compat.
6946 if (!m_pCryptoControl || !m_pCryptoControl->isSndEncryptionOK())
6947 {
6948 LOGC(aslog.Error, log << "Encryption is required, but the peer did not supply correct credentials. Sending
6949 rejected."); throw CUDTException(MJ_SETUP, MN_SECURITY, 0);
6950 }
6951 */
6952
6953 UniqueLock sendguard(m_SendLock);
6954
6955 if (m_pSndBuffer->getCurrBufSize() == 0)
6956 {
6957 // delay the EXP timer to avoid mis-fired timeout
6958 ScopedLock ack_lock(m_RecvAckLock);
6959 m_tsLastRspAckTime = steady_clock::now();
6960 m_iReXmitCount = 1;
6961 }
6962
6963 // sndDropTooLate(...) may lock m_RecvAckLock
6964 // to modify m_pSndBuffer and m_pSndLossList
6965 const int iPktsTLDropped SRT_ATR_UNUSED = sndDropTooLate();
6966
6967 // For MESSAGE API the minimum outgoing buffer space required is
6968 // the size that can carry over the whole message as passed here.
6969 // Otherwise it is allowed to send less bytes.
6970 const int iNumPktsRequired = m_config.bMessageAPI ? m_pSndBuffer->countNumPacketsRequired(len) : 1;
6971
6972 if (m_bTsbPd && iNumPktsRequired > 1)
6973 {
6974 LOGC(aslog.Error,
6975 log << CONID() << "Message length (" << len << ") can't fit into a single data packet ("
6976 << m_pSndBuffer->getMaxPacketLen() << " bytes max).");
6977 throw CUDTException(MJ_NOTSUP, MN_XSIZE, 0);
6978 }
6979
6980 if (sndBuffersLeft() < iNumPktsRequired)
6981 {
6982 //>>We should not get here if SRT_ENABLE_TLPKTDROP
6983 // XXX Check if this needs to be removed, or put to an 'else' condition for m_bTLPktDrop.
6984 if (!m_config.bSynSending)
6985 throw CUDTException(MJ_AGAIN, MN_WRAVAIL, 0);
6986
6987 {
6988 // wait here during a blocking sending
6989 UniqueLock sendblock_lock (m_SendBlockLock);
6990
6991 if (m_config.iSndTimeOut < 0)
6992 {
6993 while (stillConnected() && sndBuffersLeft() < iNumPktsRequired && m_bPeerHealth)
6994 m_SendBlockCond.wait(sendblock_lock);
6995 }
6996 else
6997 {
6998 const steady_clock::time_point exptime =
6999 steady_clock::now() + milliseconds_from(m_config.iSndTimeOut);
7000 THREAD_PAUSED();
7001 while (stillConnected() && sndBuffersLeft() < iNumPktsRequired && m_bPeerHealth)
7002 {
7003 if (!m_SendBlockCond.wait_until(sendblock_lock, exptime))
7004 break;
7005 }
7006 THREAD_RESUMED();
7007 }
7008 }
7009
7010 // check the connection status
7011 if (m_bBroken || m_bClosing)
7012 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7013 else if (!m_bConnected)
7014 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7015 else if (!m_bPeerHealth)
7016 {
7017 m_bPeerHealth = true;
7018 throw CUDTException(MJ_PEERERROR);
7019 }
7020
7021 /*
7022 * The code below is to return ETIMEOUT when blocking mode could not get free buffer in time.
7023 * If no free buffer available in non-blocking mode, we already returned. If buffer available,
7024 * we test twice if this code is outside the else section.
7025 * This fix move it in the else (blocking-mode) section
7026 */
7027 if (sndBuffersLeft() < iNumPktsRequired)
7028 {
7029 if (m_config.iSndTimeOut >= 0)
7030 throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
7031
7032 // XXX This looks very weird here, however most likely
7033 // this will happen only in the following case, when
7034 // the above loop has been interrupted, which happens when:
7035 // 1. The buffers left gets enough for minlen - but this is excluded
7036 // in the first condition here.
7037 // 2. In the case of sending timeout, the above loop was interrupted
7038 // due to reaching timeout, but this is excluded by the second
7039 // condition here
7040 // 3. The 'stillConnected()' or m_bPeerHealth condition is false, of which:
7041 // - broken/closing status is checked and responded with CONNECTION/CONNLOST
7042 // - not connected status is checked and responded with CONNECTION/NOCONN
7043 // - m_bPeerHealth condition is checked and responded with PEERERROR
7044 //
7045 // ERGO: never happens?
7046 LOGC(aslog.Fatal,
7047 log << CONID()
7048 << "IPE: sendmsg: the loop exited, while not enough size, still connected, peer healthy. "
7049 "Impossible.");
7050
7051 return 0;
7052 }
7053 }
7054
7055 // If the sender's buffer is empty,
7056 // record total time used for sending
7057 if (m_pSndBuffer->getCurrBufSize() == 0)
7058 {
7059 ScopedLock lock(m_StatsLock);
7060 m_stats.sndDurationCounter = steady_clock::now();
7061 }
7062
7063 int size = len;
7064 if (!m_config.bMessageAPI)
7065 {
7066 // For STREAM API it's allowed to send less bytes than the given buffer.
7067 // Just return how many bytes were actually scheduled for writing.
7068 // XXX May be reasonable to add a flag that requires that the function
7069 // not return until the buffer is sent completely.
7070 size = min(len, sndBuffersLeft() * m_iMaxSRTPayloadSize);
7071 }
7072
7073 {
7074 ScopedLock recvAckLock(m_RecvAckLock);
7075 // insert the user buffer into the sending list
7076
7077 int32_t seqno = m_iSndNextSeqNo;
7078 IF_HEAVY_LOGGING(int32_t orig_seqno = seqno);
7079 IF_HEAVY_LOGGING(steady_clock::time_point ts_srctime =
7080 steady_clock::time_point() + microseconds_from(w_mctrl.srctime));
7081
7082 #if ENABLE_BONDING
7083 // Check if seqno has been set, in case when this is a group sender.
7084 // If the sequence is from the past towards the "next sequence",
7085 // simply return the size, pretending that it has been sent.
7086
7087 // NOTE: it's assumed that if this is a group member, then
7088 // an attempt to call srt_sendmsg2 has been rejected, and so
7089 // the pktseq field has been set by the internal group sender function.
7090 if (m_parent->m_GroupOf
7091 && w_mctrl.pktseq != SRT_SEQNO_NONE
7092 && m_iSndNextSeqNo != SRT_SEQNO_NONE)
7093 {
7094 if (CSeqNo::seqcmp(w_mctrl.pktseq, seqno) < 0)
7095 {
7096 HLOGC(aslog.Debug, log << CONID() << "sock:SENDING (NOT): group-req %" << w_mctrl.pktseq
7097 << " OLDER THAN next expected %" << seqno << " - FAKE-SENDING.");
7098 return size;
7099 }
7100 }
7101 #endif
7102
7103 // Set this predicted next sequence to the control information.
7104 // It's the sequence of the FIRST (!) packet from all packets used to send
7105 // this buffer. Values from this field will be monotonic only if you always
7106 // have one packet per buffer (as it's in live mode).
7107 w_mctrl.pktseq = seqno;
7108
7109 // Now seqno is the sequence to which it was scheduled
7110 // XXX Conversion from w_mctrl.srctime -> steady_clock::time_point need not be accurate.
7111 HLOGC(aslog.Debug, log << CONID() << "buf:SENDING (BEFORE) srctime:"
7112 << (w_mctrl.srctime ? FormatTime(ts_srctime) : "none")
7113 << " DATA SIZE: " << size << " sched-SEQUENCE: " << seqno
7114 << " STAMP: " << BufferStamp(data, size));
7115
7116 if (w_mctrl.srctime && w_mctrl.srctime < count_microseconds(m_stats.tsStartTime.time_since_epoch()))
7117 {
7118 LOGC(aslog.Error,
7119 log << CONID() << "Wrong source time was provided. Sending is rejected.");
7120 throw CUDTException(MJ_NOTSUP, MN_INVALMSGAPI);
7121 }
7122
7123 if (w_mctrl.srctime && (!m_config.bMessageAPI || !m_bTsbPd))
7124 {
7125 HLOGC(
7126 aslog.Warn,
7127 log << CONID()
7128 << "Source time can only be used with TSBPD and Message API enabled. Using default time instead.");
7129 w_mctrl.srctime = 0;
7130 }
7131
7132 // w_mctrl.seqno is INPUT-OUTPUT value:
7133 // - INPUT: the current sequence number to be placed for the next scheduled packet
7134 // - OUTPUT: value of the sequence number to be put on the first packet at the next sendmsg2 call.
7135 // We need to supply to the output the value that was STAMPED ON THE PACKET,
7136 // which is seqno. In the output we'll get the next sequence number.
7137 m_pSndBuffer->addBuffer(data, size, (w_mctrl));
7138 m_iSndNextSeqNo = w_mctrl.pktseq;
7139 w_mctrl.pktseq = seqno;
7140
7141 HLOGC(aslog.Debug, log << CONID() << "buf:SENDING srctime:" << FormatTime(ts_srctime)
7142 << " size=" << size << " #" << w_mctrl.msgno << " SCHED %" << orig_seqno
7143 << "(>> %" << seqno << ") !" << BufferStamp(data, size));
7144
7145 if (sndBuffersLeft() < 1) // XXX Not sure if it should test if any space in the buffer, or as required.
7146 {
7147 // write is not available any more
7148 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_OUT, false);
7149 }
7150 }
7151
7152 // Insert this socket to the snd list if it is not on the list already.
7153 // m_pSndUList->pop may lock CSndUList::m_ListLock and then m_RecvAckLock
7154 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE);
7155
7156 #ifdef SRT_ENABLE_ECN
7157 // IF there was a packet drop on the sender side, report congestion to the app.
7158 if (iPktsTLDropped > 0)
7159 {
7160 LOGC(aslog.Error, log << CONID() << "sendmsg2: CONGESTION; reporting error");
7161 throw CUDTException(MJ_AGAIN, MN_CONGESTION, 0);
7162 }
7163 #endif /* SRT_ENABLE_ECN */
7164
7165 HLOGC(aslog.Debug, log << CONID() << "sock:SENDING (END): success, size=" << size);
7166 return size;
7167 }
7168
7169 int srt::CUDT::recv(char* data, int len)
7170 {
7171 SRT_MSGCTRL mctrl = srt_msgctrl_default;
7172 return recvmsg2(data, len, (mctrl));
7173 }
7174
7175 int srt::CUDT::recvmsg(char* data, int len, int64_t& srctime)
7176 {
7177 SRT_MSGCTRL mctrl = srt_msgctrl_default;
7178 int res = recvmsg2(data, len, (mctrl));
7179 srctime = mctrl.srctime;
7180 return res;
7181 }
7182
7183 // [[using maybe_locked(CUDTGroup::m_GroupLock, m_parent->m_GroupOf != NULL)]]
7184 // GroupLock is applied when this function is called from inside CUDTGroup::recv,
7185 // which is the only case when the m_parent->m_GroupOf is not NULL.
7186 int srt::CUDT::recvmsg2(char* data, int len, SRT_MSGCTRL& w_mctrl)
7187 {
7188 // Check if the socket is a member of a receiver group.
7189 // If so, then reading by receiveMessage is disallowed.
7190
7191 #if ENABLE_BONDING
7192 if (m_parent->m_GroupOf && m_parent->m_GroupOf->isGroupReceiver())
7193 {
7194 LOGP(arlog.Error, "recv*: This socket is a receiver group member. Use group ID, NOT socket ID.");
7195 throw CUDTException(MJ_NOTSUP, MN_INVALMSGAPI, 0);
7196 }
7197 #endif
7198
7199 if (!m_bConnected || !m_CongCtl.ready())
7200 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7201
7202 if (len <= 0)
7203 {
7204 LOGC(arlog.Error, log << CONID() << "Length of '" << len << "' supplied to srt_recvmsg.");
7205 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
7206 }
7207
7208 if (m_config.bMessageAPI)
7209 return receiveMessage(data, len, (w_mctrl));
7210
7211 return receiveBuffer(data, len);
7212 }
7213
7214 // [[using locked(m_RcvBufferLock)]]
7215 size_t srt::CUDT::getAvailRcvBufferSizeNoLock() const
7216 {
7217 return m_pRcvBuffer->getAvailSize(m_iRcvLastAck);
7218 }
7219
7220 bool srt::CUDT::isRcvBufferReady() const
7221 {
7222 ScopedLock lck(m_RcvBufferLock);
7223 return m_pRcvBuffer->isRcvDataReady(steady_clock::now());
7224 }
7225
7226 bool srt::CUDT::isRcvBufferReadyNoLock() const
7227 {
7228 return m_pRcvBuffer->isRcvDataReady(steady_clock::now());
7229 }
7230
7231 bool srt::CUDT::isRcvBufferFull() const
7232 {
7233 ScopedLock lck(m_RcvBufferLock);
7234 return m_pRcvBuffer->full();
7235 }
7236
7237 // int by_exception: accepts values of CUDTUnited::ErrorHandling:
7238 // - 0 - by return value
7239 // - 1 - by exception
7240 // - 2 - by abort (unused)
7241 int srt::CUDT::receiveMessage(char* data, int len, SRT_MSGCTRL& w_mctrl, int by_exception)
7242 {
7243 // Recvmsg isn't restricted to the congctl type, it's the most
7244 // basic method of passing the data. You can retrieve data as
7245 // they come in, however you need to match the size of the buffer.
7246
7247 // Note: if by_exception = ERH_RETURN, this would still break it
7248 // by exception. The intention of by_exception isn't to prevent
7249 // exceptions here, but to intercept the erroneous situation should
7250 // it be handled by the caller in a less than general way. As this
7251 // is only used internally, we state that the problem that would be
7252 // handled by exception here should not happen, and in case if it does,
7253 // it's a bug to fix, so the exception is nothing wrong.
7254 if (!m_CongCtl->checkTransArgs(SrtCongestion::STA_MESSAGE, SrtCongestion::STAD_RECV, data, len, SRT_MSGTTL_INF, false))
7255 throw CUDTException(MJ_NOTSUP, MN_INVALMSGAPI, 0);
7256
7257 UniqueLock recvguard (m_RecvLock);
7258 CSync tscond (m_RcvTsbPdCond, recvguard);
7259
7260 /* XXX DEBUG STUFF - enable when required
7261 char charbool[2] = {'0', '1'};
7262 char ptrn [] = "RECVMSG/BEGIN BROKEN 1 CONN 1 CLOSING 1 SYNCR 1 NMSG ";
7263 int pos [] = {21, 28, 38, 46, 53};
7264 ptrn[pos[0]] = charbool[m_bBroken];
7265 ptrn[pos[1]] = charbool[m_bConnected];
7266 ptrn[pos[2]] = charbool[m_bClosing];
7267 ptrn[pos[3]] = charbool[m_config.m_bSynRecving];
7268 int wrtlen = sprintf(ptrn + pos[4], "%d", m_pRcvBuffer->getRcvMsgNum());
7269 strcpy(ptrn + pos[4] + wrtlen, "\n");
7270 fputs(ptrn, stderr);
7271 // */
7272
7273 if (m_bBroken || m_bClosing)
7274 {
7275 HLOGC(arlog.Debug, log << CONID() << "receiveMessage: CONNECTION BROKEN - reading from recv buffer just for formality");
7276 enterCS(m_RcvBufferLock);
7277 const int res = (m_pRcvBuffer->isRcvDataReady(steady_clock::now()))
7278 ? m_pRcvBuffer->readMessage(data, len, &w_mctrl)
7279 : 0;
7280 leaveCS(m_RcvBufferLock);
7281
7282 // Kick TsbPd thread to schedule next wakeup (if running)
7283 if (m_bTsbPd)
7284 {
7285 HLOGP(tslog.Debug, "Ping TSBPD thread to schedule wakeup");
7286 tscond.notify_one_locked(recvguard);
7287 }
7288 else
7289 {
7290 HLOGP(tslog.Debug, "NOT pinging TSBPD - not set");
7291 }
7292
7293 if (!isRcvBufferReady())
7294 {
7295 // read is not available any more
7296 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
7297 }
7298
7299 if (res == 0)
7300 {
7301 if (!m_config.bMessageAPI && m_bShutdown)
7302 return 0;
7303 // Forced to return error instead of throwing exception.
7304 if (!by_exception)
7305 return APIError(MJ_CONNECTION, MN_CONNLOST, 0);
7306 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7307 }
7308 else
7309 return res;
7310 }
7311
7312 if (!m_config.bSynRecving)
7313 {
7314 HLOGC(arlog.Debug, log << CONID() << "receiveMessage: BEGIN ASYNC MODE. Going to extract payload size=" << len);
7315 enterCS(m_RcvBufferLock);
7316 const int res = (m_pRcvBuffer->isRcvDataReady(steady_clock::now()))
7317 ? m_pRcvBuffer->readMessage(data, len, &w_mctrl)
7318 : 0;
7319 leaveCS(m_RcvBufferLock);
7320 HLOGC(arlog.Debug, log << CONID() << "AFTER readMsg: (NON-BLOCKING) result=" << res);
7321
7322 if (res == 0)
7323 {
7324 // read is not available any more
7325 // Kick TsbPd thread to schedule next wakeup (if running)
7326 if (m_bTsbPd)
7327 {
7328 HLOGP(arlog.Debug, "receiveMessage: nothing to read, kicking TSBPD, return AGAIN");
7329 tscond.notify_one_locked(recvguard);
7330 }
7331 else
7332 {
7333 HLOGP(arlog.Debug, "receiveMessage: nothing to read, return AGAIN");
7334 }
7335
7336 // Shut up EPoll if no more messages in non-blocking mode
7337 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
7338 // Forced to return 0 instead of throwing exception, in case of AGAIN/READ
7339 if (!by_exception)
7340 return 0;
7341 throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0);
7342 }
7343
7344 if (!isRcvBufferReady())
7345 {
7346 // Kick TsbPd thread to schedule next wakeup (if running)
7347 if (m_bTsbPd)
7348 {
7349 HLOGP(arlog.Debug, "receiveMessage: DATA READ, but nothing more - kicking TSBPD.");
7350 tscond.notify_one_locked(recvguard);
7351 }
7352 else
7353 {
7354 HLOGP(arlog.Debug, "receiveMessage: DATA READ, but nothing more");
7355 }
7356
7357 // Shut up EPoll if no more messages in non-blocking mode
7358 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
7359
7360 // After signaling the tsbpd for ready data, report the bandwidth.
7361 #if ENABLE_HEAVY_LOGGING
7362 double bw = Bps2Mbps(int64_t(m_iBandwidth) * m_iMaxSRTPayloadSize );
7363 HLOGC(arlog.Debug, log << CONID() << "CURRENT BANDWIDTH: " << bw << "Mbps (" << m_iBandwidth << " buffers per second)");
7364 #endif
7365 }
7366 return res;
7367 }
7368
7369 HLOGC(arlog.Debug, log << CONID() << "receiveMessage: BEGIN SYNC MODE. Going to extract payload size max=" << len);
7370
7371 int res = 0;
7372 bool timeout = false;
7373 // Do not block forever, check connection status each 1 sec.
7374 const steady_clock::duration recv_timeout = m_config.iRcvTimeOut < 0 ? seconds_from(1) : milliseconds_from(m_config.iRcvTimeOut);
7375
7376 CSync recv_cond (m_RecvDataCond, recvguard);
7377
7378 do
7379 {
7380 if (stillConnected() && !timeout && !isRcvBufferReady())
7381 {
7382 /* Kick TsbPd thread to schedule next wakeup (if running) */
7383 if (m_bTsbPd)
7384 {
7385 // XXX Experimental, so just inform:
7386 // Check if the last check of isRcvDataReady has returned any "next time for a packet".
7387 // If so, then it means that TSBPD has fallen asleep only up to this time, so waking it up
7388 // would be "spurious". If a new packet comes ahead of the packet which's time is returned
7389 // in tstime (as TSBPD sleeps up to then), the procedure that receives it is responsible
7390 // of kicking TSBPD.
7391 // bool spurious = (tstime != 0);
7392
7393 HLOGC(tslog.Debug, log << CONID() << "receiveMessage: KICK tsbpd");
7394 tscond.notify_one_locked(recvguard);
7395 }
7396
7397 THREAD_PAUSED();
7398 do
7399 {
7400 // `wait_for(recv_timeout)` wouldn't be correct here. Waiting should be
7401 // only until the time that is now + timeout since the first moment
7402 // when this started, or sliced-waiting for 1 second, if timeout is
7403 // higher than this.
7404 const steady_clock::time_point exptime = steady_clock::now() + recv_timeout;
7405
7406 HLOGC(tslog.Debug,
7407 log << CONID() << "receiveMessage: fall asleep up to TS=" << FormatTime(exptime)
7408 << " lock=" << (&m_RecvLock) << " cond=" << (&m_RecvDataCond));
7409
7410 if (!recv_cond.wait_until(exptime))
7411 {
7412 if (m_config.iRcvTimeOut >= 0) // otherwise it's "no timeout set"
7413 timeout = true;
7414 HLOGP(tslog.Debug,
7415 "receiveMessage: DATA COND: EXPIRED -- checking connection conditions and rolling again");
7416 }
7417 else
7418 {
7419 HLOGP(tslog.Debug, "receiveMessage: DATA COND: KICKED.");
7420 }
7421 } while (stillConnected() && !timeout && (!isRcvBufferReady()));
7422 THREAD_RESUMED();
7423
7424 HLOGC(tslog.Debug,
7425 log << CONID() << "receiveMessage: lock-waiting loop exited: stillConntected=" << stillConnected()
7426 << " timeout=" << timeout << " data-ready=" << isRcvBufferReady());
7427 }
7428
7429 /* XXX DEBUG STUFF - enable when required
7430 LOGC(arlog.Debug, "RECVMSG/GO-ON BROKEN " << m_bBroken << " CONN " << m_bConnected
7431 << " CLOSING " << m_bClosing << " TMOUT " << timeout
7432 << " NMSG " << m_pRcvBuffer->getRcvMsgNum());
7433 */
7434
7435 enterCS(m_RcvBufferLock);
7436 res = m_pRcvBuffer->readMessage((data), len, &w_mctrl);
7437 leaveCS(m_RcvBufferLock);
7438 HLOGC(arlog.Debug, log << CONID() << "AFTER readMsg: (BLOCKING) result=" << res);
7439
7440 if (m_bBroken || m_bClosing)
7441 {
7442 // Forced to return 0 instead of throwing exception.
7443 if (!by_exception)
7444 return APIError(MJ_CONNECTION, MN_CONNLOST, 0);
7445 if (!m_config.bMessageAPI && m_bShutdown)
7446 return 0;
7447 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7448 }
7449 else if (!m_bConnected)
7450 {
7451 // Forced to return -1 instead of throwing exception.
7452 if (!by_exception)
7453 return APIError(MJ_CONNECTION, MN_NOCONN, 0);
7454 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7455 }
7456 } while ((res == 0) && !timeout);
7457
7458 if (!isRcvBufferReady())
7459 {
7460 // Falling here means usually that res == 0 && timeout == true.
7461 // res == 0 would repeat the above loop, unless there was also a timeout.
7462 // timeout has interrupted the above loop, but with res > 0 this condition
7463 // wouldn't be satisfied.
7464
7465 // read is not available any more
7466
7467 // Kick TsbPd thread to schedule next wakeup (if running)
7468 if (m_bTsbPd)
7469 {
7470 HLOGP(tslog.Debug, "recvmsg: KICK tsbpd() (buffer empty)");
7471 tscond.notify_one_locked(recvguard);
7472 }
7473
7474 // Shut up EPoll if no more messages in non-blocking mode
7475 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
7476 }
7477
7478 // Unblock when required
7479 // LOGC(tslog.Debug, "RECVMSG/EXIT RES " << res << " RCVTIMEOUT");
7480
7481 if ((res <= 0) && (m_config.iRcvTimeOut >= 0))
7482 {
7483 // Forced to return -1 instead of throwing exception.
7484 if (!by_exception)
7485 return APIError(MJ_AGAIN, MN_XMTIMEOUT, 0);
7486 throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
7487 }
7488
7489 return res;
7490 }
7491
7492 int64_t srt::CUDT::sendfile(fstream &ifs, int64_t &offset, int64_t size, int block)
7493 {
7494 if (m_bBroken || m_bClosing)
7495 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7496 else if (!m_bConnected || !m_CongCtl.ready())
7497 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7498
7499 if (size <= 0 && size != -1)
7500 return 0;
7501
7502 if (!m_CongCtl->checkTransArgs(SrtCongestion::STA_FILE, SrtCongestion::STAD_SEND, 0, size, SRT_MSGTTL_INF, false))
7503 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
7504
7505 if (!m_pCryptoControl || !m_pCryptoControl->isSndEncryptionOK())
7506 {
7507 LOGC(aslog.Error,
7508 log << CONID()
7509 << "Encryption is required, but the peer did not supply correct credentials. Sending rejected.");
7510 throw CUDTException(MJ_SETUP, MN_SECURITY, 0);
7511 }
7512
7513 ScopedLock sendguard (m_SendLock);
7514
7515 if (m_pSndBuffer->getCurrBufSize() == 0)
7516 {
7517 // delay the EXP timer to avoid mis-fired timeout
7518 ScopedLock ack_lock(m_RecvAckLock);
7519 m_tsLastRspAckTime = steady_clock::now();
7520 m_iReXmitCount = 1;
7521 }
7522
7523 // positioning...
7524 try
7525 {
7526 if (size == -1)
7527 {
7528 ifs.seekg(0, std::ios::end);
7529 size = ifs.tellg();
7530 if (offset > size)
7531 throw 0; // let it be caught below
7532 }
7533
7534 // This will also set the position back to the beginning
7535 // in case when it was moved to the end for measuring the size.
7536 // This will also fail if the offset exceeds size, so measuring
7537 // the size can be skipped if not needed.
7538 ifs.seekg((streamoff)offset);
7539 if (!ifs.good())
7540 throw 0;
7541 }
7542 catch (...)
7543 {
7544 // XXX It would be nice to note that this is reported
7545 // by exception only if explicitly requested by setting
7546 // the exception flags in the stream. Here it's fixed so
7547 // that when this isn't set, the exception is "thrown manually".
7548 throw CUDTException(MJ_FILESYSTEM, MN_SEEKGFAIL);
7549 }
7550
7551 int64_t tosend = size;
7552 int unitsize;
7553
7554 // sending block by block
7555 while (tosend > 0)
7556 {
7557 if (ifs.fail())
7558 throw CUDTException(MJ_FILESYSTEM, MN_WRITEFAIL);
7559
7560 if (ifs.eof())
7561 break;
7562
7563 unitsize = int((tosend >= block) ? block : tosend);
7564
7565 {
7566 UniqueLock lock(m_SendBlockLock);
7567
7568 THREAD_PAUSED();
7569 while (stillConnected() && (sndBuffersLeft() <= 0) && m_bPeerHealth)
7570 m_SendBlockCond.wait(lock);
7571 THREAD_RESUMED();
7572 }
7573
7574 if (m_bBroken || m_bClosing)
7575 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7576 else if (!m_bConnected)
7577 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7578 else if (!m_bPeerHealth)
7579 {
7580 // reset peer health status, once this error returns, the app should handle the situation at the peer side
7581 m_bPeerHealth = true;
7582 throw CUDTException(MJ_PEERERROR);
7583 }
7584
7585 // record total time used for sending
7586 if (m_pSndBuffer->getCurrBufSize() == 0)
7587 {
7588 ScopedLock lock(m_StatsLock);
7589 m_stats.sndDurationCounter = steady_clock::now();
7590 }
7591
7592 {
7593 ScopedLock recvAckLock(m_RecvAckLock);
7594 const int64_t sentsize = m_pSndBuffer->addBufferFromFile(ifs, unitsize);
7595
7596 if (sentsize > 0)
7597 {
7598 tosend -= sentsize;
7599 offset += sentsize;
7600 }
7601
7602 if (sndBuffersLeft() <= 0)
7603 {
7604 // write is not available any more
7605 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_OUT, false);
7606 }
7607 }
7608
7609 // insert this socket to snd list if it is not on the list yet
7610 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE);
7611 }
7612
7613 return size - tosend;
7614 }
7615
7616 int64_t srt::CUDT::recvfile(fstream &ofs, int64_t &offset, int64_t size, int block)
7617 {
7618 if (!m_bConnected || !m_CongCtl.ready())
7619 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7620 else if ((m_bBroken || m_bClosing) && !isRcvBufferReady())
7621 {
7622 if (!m_config.bMessageAPI && m_bShutdown)
7623 return 0;
7624 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7625 }
7626
7627 if (size <= 0)
7628 return 0;
7629
7630 if (!m_CongCtl->checkTransArgs(SrtCongestion::STA_FILE, SrtCongestion::STAD_RECV, 0, size, SRT_MSGTTL_INF, false))
7631 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
7632
7633 if (isOPT_TsbPd())
7634 {
7635 LOGC(arlog.Error,
7636 log << CONID() << "Reading from file is incompatible with TSBPD mode and would cause a deadlock");
7637 throw CUDTException(MJ_NOTSUP, MN_INVALBUFFERAPI, 0);
7638 }
7639
7640 UniqueLock recvguard(m_RecvLock);
7641
7642 // Well, actually as this works over a FILE (fstream), not just a stream,
7643 // the size can be measured anyway and predicted if setting the offset might
7644 // have a chance to work or not.
7645
7646 // positioning...
7647 try
7648 {
7649 if (offset > 0)
7650 {
7651 // Don't do anything around here if the offset == 0, as this
7652 // is the default offset after opening. Whether this operation
7653 // is performed correctly, it highly depends on how the file
7654 // has been open. For example, if you want to overwrite parts
7655 // of an existing file, the file must exist, and the ios::trunc
7656 // flag must not be set. If the file is open for only ios::out,
7657 // then the file will be truncated since the offset position on
7658 // at the time when first written; if ios::in|ios::out, then
7659 // it won't be truncated, just overwritten.
7660
7661 // What is required here is that if offset is 0, don't try to
7662 // change the offset because this might be impossible with
7663 // the current flag set anyway.
7664
7665 // Also check the status and CAUSE exception manually because
7666 // you don't know, as well, whether the user has set exception
7667 // flags.
7668
7669 ofs.seekp((streamoff)offset);
7670 if (!ofs.good())
7671 throw 0; // just to get caught :)
7672 }
7673 }
7674 catch (...)
7675 {
7676 // XXX It would be nice to note that this is reported
7677 // by exception only if explicitly requested by setting
7678 // the exception flags in the stream. For a case, when it's not,
7679 // an additional explicit throwing happens when failbit is set.
7680 throw CUDTException(MJ_FILESYSTEM, MN_SEEKPFAIL);
7681 }
7682
7683 int64_t torecv = size;
7684 int unitsize = block;
7685 int recvsize;
7686
7687 // receiving... "recvfile" is always blocking
7688 while (torecv > 0)
7689 {
7690 if (ofs.fail())
7691 {
7692 // send the sender a signal so it will not be blocked forever
7693 int32_t err_code = CUDTException::EFILE;
7694 sendCtrl(UMSG_PEERERROR, &err_code);
7695
7696 throw CUDTException(MJ_FILESYSTEM, MN_WRITEFAIL);
7697 }
7698
7699 {
7700 CSync rcond (m_RecvDataCond, recvguard);
7701
7702 THREAD_PAUSED();
7703 while (stillConnected() && !isRcvBufferReady())
7704 rcond.wait();
7705 THREAD_RESUMED();
7706 }
7707
7708 if (!m_bConnected)
7709 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7710 else if ((m_bBroken || m_bClosing) && !isRcvBufferReady())
7711 {
7712 if (!m_config.bMessageAPI && m_bShutdown)
7713 return 0;
7714 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7715 }
7716
7717 unitsize = int((torecv > block) ? block : torecv);
7718 enterCS(m_RcvBufferLock);
7719 recvsize = m_pRcvBuffer->readBufferToFile(ofs, unitsize);
7720 leaveCS(m_RcvBufferLock);
7721
7722 if (recvsize > 0)
7723 {
7724 torecv -= recvsize;
7725 offset += recvsize;
7726 }
7727 }
7728
7729 if (!isRcvBufferReady())
7730 {
7731 // read is not available any more
7732 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
7733 }
7734
7735 return size - torecv;
7736 }
7737
7738 void srt::CUDT::bstats(CBytePerfMon *perf, bool clear, bool instantaneous)
7739 {
7740 if (!m_bConnected)
7741 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
7742 if (m_bBroken || m_bClosing)
7743 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
7744
7745 const int pktHdrSize = CPacket::HDR_SIZE + CPacket::UDP_HDR_SIZE;
7746 {
7747 int32_t flight_span = getFlightSpan();
7748
7749 ScopedLock statsguard(m_StatsLock);
7750
7751 const steady_clock::time_point currtime = steady_clock::now();
7752
7753 perf->msTimeStamp = count_milliseconds(currtime - m_stats.tsStartTime);
7754 perf->pktSent = m_stats.sndr.sent.trace.count();
7755 perf->pktSentUnique = m_stats.sndr.sentUnique.trace.count();
7756 perf->pktRecv = m_stats.rcvr.recvd.trace.count();
7757 perf->pktRecvUnique = m_stats.rcvr.recvdUnique.trace.count();
7758
7759 perf->pktSndLoss = m_stats.sndr.lost.trace.count();
7760 perf->pktRcvLoss = m_stats.rcvr.lost.trace.count();
7761 perf->pktRetrans = m_stats.sndr.sentRetrans.trace.count();
7762 perf->pktRcvRetrans = m_stats.rcvr.recvdRetrans.trace.count();
7763 perf->pktSentACK = m_stats.rcvr.sentAck.trace.count();
7764 perf->pktRecvACK = m_stats.sndr.recvdAck.trace.count();
7765 perf->pktSentNAK = m_stats.rcvr.sentNak.trace.count();
7766 perf->pktRecvNAK = m_stats.sndr.recvdNak.trace.count();
7767 perf->usSndDuration = m_stats.sndDuration;
7768 perf->pktReorderDistance = m_stats.traceReorderDistance;
7769 perf->pktReorderTolerance = m_iReorderTolerance;
7770 perf->pktRcvAvgBelatedTime = m_stats.traceBelatedTime;
7771 perf->pktRcvBelated = m_stats.rcvr.recvdBelated.trace.count();
7772
7773 perf->pktSndFilterExtra = m_stats.sndr.sentFilterExtra.trace.count();
7774 perf->pktRcvFilterExtra = m_stats.rcvr.recvdFilterExtra.trace.count();
7775 perf->pktRcvFilterSupply = m_stats.rcvr.suppliedByFilter.trace.count();
7776 perf->pktRcvFilterLoss = m_stats.rcvr.lossFilter.trace.count();
7777
7778 /* perf byte counters include all headers (SRT+UDP+IP) */
7779 perf->byteSent = m_stats.sndr.sent.trace.bytesWithHdr(pktHdrSize);
7780 perf->byteSentUnique = m_stats.sndr.sentUnique.trace.bytesWithHdr(pktHdrSize);
7781 perf->byteRecv = m_stats.rcvr.recvd.trace.bytesWithHdr(pktHdrSize);
7782 perf->byteRecvUnique = m_stats.rcvr.recvdUnique.trace.bytesWithHdr(pktHdrSize);
7783 perf->byteRetrans = m_stats.sndr.sentRetrans.trace.bytesWithHdr(pktHdrSize);
7784 perf->byteRcvLoss = m_stats.rcvr.lost.trace.bytesWithHdr(pktHdrSize);
7785
7786 perf->pktSndDrop = m_stats.sndr.dropped.trace.count();
7787 perf->pktRcvDrop = m_stats.rcvr.dropped.trace.count();
7788 perf->byteSndDrop = m_stats.sndr.dropped.trace.bytesWithHdr(pktHdrSize);
7789 perf->byteRcvDrop = m_stats.rcvr.dropped.trace.bytesWithHdr(pktHdrSize);
7790 perf->pktRcvUndecrypt = m_stats.rcvr.undecrypted.trace.count();
7791 perf->byteRcvUndecrypt = m_stats.rcvr.undecrypted.trace.bytes();
7792
7793 perf->pktSentTotal = m_stats.sndr.sent.total.count();
7794 perf->pktSentUniqueTotal = m_stats.sndr.sentUnique.total.count();
7795 perf->pktRecvTotal = m_stats.rcvr.recvd.total.count();
7796 perf->pktRecvUniqueTotal = m_stats.rcvr.recvdUnique.total.count();
7797 perf->pktSndLossTotal = m_stats.sndr.lost.total.count();
7798 perf->pktRcvLossTotal = m_stats.rcvr.lost.total.count();
7799 perf->pktRetransTotal = m_stats.sndr.sentRetrans.total.count();
7800 perf->pktSentACKTotal = m_stats.rcvr.sentAck.total.count();
7801 perf->pktRecvACKTotal = m_stats.sndr.recvdAck.total.count();
7802 perf->pktSentNAKTotal = m_stats.rcvr.sentNak.total.count();
7803 perf->pktRecvNAKTotal = m_stats.sndr.recvdNak.total.count();
7804 perf->usSndDurationTotal = m_stats.m_sndDurationTotal;
7805
7806 perf->byteSentTotal = m_stats.sndr.sent.total.bytesWithHdr(pktHdrSize);
7807 perf->byteSentUniqueTotal = m_stats.sndr.sentUnique.total.bytesWithHdr(pktHdrSize);
7808 perf->byteRecvTotal = m_stats.rcvr.recvd.total.bytesWithHdr(pktHdrSize);
7809 perf->byteRecvUniqueTotal = m_stats.rcvr.recvdUnique.total.bytesWithHdr(pktHdrSize);
7810 perf->byteRetransTotal = m_stats.sndr.sentRetrans.total.bytesWithHdr(pktHdrSize);
7811 perf->pktSndFilterExtraTotal = m_stats.sndr.sentFilterExtra.total.count();
7812 perf->pktRcvFilterExtraTotal = m_stats.rcvr.recvdFilterExtra.total.count();
7813 perf->pktRcvFilterSupplyTotal = m_stats.rcvr.suppliedByFilter.total.count();
7814 perf->pktRcvFilterLossTotal = m_stats.rcvr.lossFilter.total.count();
7815
7816 perf->byteRcvLossTotal = m_stats.rcvr.lost.total.bytesWithHdr(pktHdrSize);
7817 perf->pktSndDropTotal = m_stats.sndr.dropped.total.count();
7818 perf->pktRcvDropTotal = m_stats.rcvr.dropped.total.count();
7819 // TODO: The payload is dropped. Probably header sizes should not be counted?
7820 perf->byteSndDropTotal = m_stats.sndr.dropped.total.bytesWithHdr(pktHdrSize);
7821 perf->byteRcvDropTotal = m_stats.rcvr.dropped.total.bytesWithHdr(pktHdrSize);
7822 perf->pktRcvUndecryptTotal = m_stats.rcvr.undecrypted.total.count();
7823 perf->byteRcvUndecryptTotal = m_stats.rcvr.undecrypted.total.bytes();
7824
7825 // TODO: The following class members must be protected with a different mutex, not the m_StatsLock.
7826 const double interval = (double) count_microseconds(currtime - m_stats.tsLastSampleTime);
7827 perf->mbpsSendRate = double(perf->byteSent) * 8.0 / interval;
7828 perf->mbpsRecvRate = double(perf->byteRecv) * 8.0 / interval;
7829 perf->usPktSndPeriod = (double) count_microseconds(m_tdSendInterval.load());
7830 perf->pktFlowWindow = m_iFlowWindowSize.load();
7831 perf->pktCongestionWindow = m_iCongestionWindow;
7832 perf->pktFlightSize = flight_span;
7833 perf->msRTT = (double)m_iSRTT / 1000.0;
7834 perf->msSndTsbPdDelay = m_bPeerTsbPd ? m_iPeerTsbPdDelay_ms : 0;
7835 perf->msRcvTsbPdDelay = isOPT_TsbPd() ? m_iTsbPdDelay_ms : 0;
7836 perf->byteMSS = m_config.iMSS;
7837
7838 perf->mbpsMaxBW = m_config.llMaxBW > 0 ? Bps2Mbps(m_config.llMaxBW)
7839 : m_CongCtl.ready() ? Bps2Mbps(m_CongCtl->sndBandwidth())
7840 : 0;
7841
7842 if (clear)
7843 {
7844 m_stats.sndr.resetTrace();
7845 m_stats.rcvr.resetTrace();
7846
7847 m_stats.sndDuration = 0;
7848 m_stats.tsLastSampleTime = currtime;
7849 }
7850 }
7851
7852 const int64_t availbw = m_iBandwidth == 1 ? m_RcvTimeWindow.getBandwidth() : m_iBandwidth.load();
7853
7854 perf->mbpsBandwidth = Bps2Mbps(availbw * (m_iMaxSRTPayloadSize + pktHdrSize));
7855
7856 if (tryEnterCS(m_ConnectionLock))
7857 {
7858 if (m_pSndBuffer)
7859 {
7860 if (instantaneous)
7861 {
7862 /* Get instant SndBuf instead of moving average for application-based Algorithm
7863 (such as NAE) in need of fast reaction to network condition changes. */
7864 perf->pktSndBuf = m_pSndBuffer->getCurrBufSize((perf->byteSndBuf), (perf->msSndBuf));
7865 }
7866 else
7867 {
7868 perf->pktSndBuf = m_pSndBuffer->getAvgBufSize((perf->byteSndBuf), (perf->msSndBuf));
7869 }
7870 perf->byteSndBuf += (perf->pktSndBuf * pktHdrSize);
7871 perf->byteAvailSndBuf = (m_config.iSndBufSize - perf->pktSndBuf) * m_config.iMSS;
7872 }
7873 else
7874 {
7875 perf->byteAvailSndBuf = 0;
7876 perf->pktSndBuf = 0;
7877 perf->byteSndBuf = 0;
7878 perf->msSndBuf = 0;
7879 }
7880
7881 if (m_pRcvBuffer)
7882 {
7883 ScopedLock lck(m_RcvBufferLock);
7884 perf->byteAvailRcvBuf = (int) getAvailRcvBufferSizeNoLock() * m_config.iMSS;
7885 if (instantaneous) // no need for historical API for Rcv side
7886 {
7887 perf->pktRcvBuf = m_pRcvBuffer->getRcvDataSize(perf->byteRcvBuf, perf->msRcvBuf);
7888 }
7889 else
7890 {
7891 perf->pktRcvBuf = m_pRcvBuffer->getRcvAvgDataSize(perf->byteRcvBuf, perf->msRcvBuf);
7892 }
7893 }
7894 else
7895 {
7896 perf->byteAvailRcvBuf = 0;
7897 perf->pktRcvBuf = 0;
7898 perf->byteRcvBuf = 0;
7899 perf->msRcvBuf = 0;
7900 }
7901
7902 leaveCS(m_ConnectionLock);
7903 }
7904 else
7905 {
7906 perf->byteAvailSndBuf = 0;
7907 perf->byteAvailRcvBuf = 0;
7908 perf->pktSndBuf = 0;
7909 perf->byteSndBuf = 0;
7910 perf->msSndBuf = 0;
7911 perf->byteRcvBuf = 0;
7912 perf->msRcvBuf = 0;
7913 }
7914 }
7915
7916 bool srt::CUDT::updateCC(ETransmissionEvent evt, const EventVariant arg)
7917 {
7918 // Special things that must be done HERE, not in SrtCongestion,
7919 // because it involves the input buffer in CUDT. It would be
7920 // slightly dangerous to give SrtCongestion access to it.
7921
7922 // According to the rules, the congctl should be ready at the same
7923 // time when the sending buffer. For sanity check, check both first.
7924 if (!m_CongCtl.ready() || !m_pSndBuffer)
7925 {
7926 LOGC(rslog.Error,
7927 log << CONID() << "updateCC: CAN'T DO UPDATE - congctl " << (m_CongCtl.ready() ? "ready" : "NOT READY")
7928 << "; sending buffer " << (m_pSndBuffer ? "NOT CREATED" : "created"));
7929
7930 return false;
7931 }
7932
7933 HLOGC(rslog.Debug, log << CONID() << "updateCC: EVENT:" << TransmissionEventStr(evt));
7934
7935 if (evt == TEV_INIT)
7936 {
7937 // only_input uses:
7938 // 0: in the beginning and when SRTO_MAXBW was changed
7939 // 1: SRTO_INPUTBW was changed
7940 // 2: SRTO_OHEADBW was changed
7941 EInitEvent only_input = arg.get<EventVariant::INIT>();
7942 // false = TEV_INIT_RESET: in the beginning, or when MAXBW was changed.
7943
7944 if (only_input != TEV_INIT_RESET && m_config.llMaxBW)
7945 {
7946 HLOGC(rslog.Debug, log << CONID() << "updateCC/TEV_INIT: non-RESET stage and m_config.llMaxBW already set to " << m_config.llMaxBW);
7947 // Don't change
7948 }
7949 else // either m_config.llMaxBW == 0 or only_input == TEV_INIT_RESET
7950 {
7951 // Use the values:
7952 // - if SRTO_MAXBW is >0, use it.
7953 // - if SRTO_MAXBW == 0, use SRTO_INPUTBW + SRTO_OHEADBW
7954 // - if SRTO_INPUTBW == 0, pass 0 to request in-buffer sampling
7955 // Bytes/s
7956 const int64_t bw = m_config.llMaxBW != 0 ? m_config.llMaxBW : // When used SRTO_MAXBW
7957 m_config.llInputBW != 0 ? withOverhead(m_config.llInputBW) : // SRTO_INPUTBW + SRT_OHEADBW
7958 0; // When both MAXBW and INPUTBW are 0, request in-buffer sampling
7959
7960 // Note: setting bw == 0 uses BW_INFINITE value in LiveCC
7961 m_CongCtl->updateBandwidth(m_config.llMaxBW, bw);
7962
7963 if (only_input == TEV_INIT_OHEADBW)
7964 {
7965 // On updated SRTO_OHEADBW don't change input rate.
7966 // This only influences the call to withOverhead().
7967 }
7968 else
7969 {
7970 // No need to calculate input rate if the bandwidth is set
7971 const bool disable_in_rate_calc = (bw != 0);
7972 m_pSndBuffer->resetInputRateSmpPeriod(disable_in_rate_calc);
7973 }
7974
7975 HLOGC(rslog.Debug,
7976 log << CONID() << "updateCC/TEV_INIT: updating BW=" << m_config.llMaxBW
7977 << (only_input == TEV_INIT_RESET
7978 ? " (UNCHANGED)"
7979 : only_input == TEV_INIT_OHEADBW ? " (only Overhead)" : " (updated sampling rate)"));
7980 }
7981 }
7982
7983 // This part is also required only by LiveCC, however not
7984 // moved there due to that it needs access to CSndBuffer.
7985 if (evt == TEV_ACK || evt == TEV_LOSSREPORT || evt == TEV_CHECKTIMER || evt == TEV_SYNC)
7986 {
7987 // Specific part done when MaxBW is set to 0 (auto) and InputBW is 0.
7988 // This requests internal input rate sampling.
7989 if (m_config.llMaxBW == 0 && m_config.llInputBW == 0)
7990 {
7991 // Get auto-calculated input rate, Bytes per second
7992 const int64_t inputbw = m_pSndBuffer->getInputRate();
7993
7994 /*
7995 * On blocked transmitter (tx full) and until connection closes,
7996 * auto input rate falls to 0 but there may be still lot of packet to retransmit
7997 * Calling updateBandwidth with 0 sets maxBW to default BW_INFINITE (1 Gbps)
7998 * and sendrate skyrockets for retransmission.
7999 * Keep previously set maximum in that case (inputbw == 0).
8000 */
8001 if (inputbw >= 0)
8002 m_CongCtl->updateBandwidth(0, withOverhead(std::max(m_config.llMinInputBW, inputbw))); // Bytes/sec
8003 }
8004 }
8005
8006 HLOGC(rslog.Debug, log << CONID() << "updateCC: emitting signal for EVENT:" << TransmissionEventStr(evt));
8007
8008 // Now execute a congctl-defined action for that event.
8009 EmitSignal(evt, arg);
8010
8011 // This should be done with every event except ACKACK and SEND/RECEIVE
8012 // After any action was done by the congctl, update the congestion window and sending interval.
8013 if (evt != TEV_ACKACK && evt != TEV_SEND && evt != TEV_RECEIVE)
8014 {
8015 // This part comes from original UDT.
8016 // NOTE: THESE things come from CCC class:
8017 // - m_dPktSndPeriod
8018 // - m_dCWndSize
8019 m_tdSendInterval = microseconds_from((int64_t)m_CongCtl->pktSndPeriod_us());
8020 const double cgwindow = m_CongCtl->cgWindowSize();
8021 m_iCongestionWindow = (int) cgwindow;
8022 #if ENABLE_HEAVY_LOGGING
8023 HLOGC(rslog.Debug,
8024 log << CONID() << "updateCC: updated values from congctl: interval=" << FormatDuration<DUNIT_US>(m_tdSendInterval)
8025 << " (cfg:" << m_CongCtl->pktSndPeriod_us() << "us) cgwindow="
8026 << std::setprecision(3) << cgwindow);
8027 #endif
8028 }
8029
8030 HLOGC(rslog.Debug, log << CONID() << "udpateCC: finished handling for EVENT:" << TransmissionEventStr(evt));
8031
8032 return true;
8033 }
8034
8035 void srt::CUDT::initSynch()
8036 {
8037 setupMutex(m_SendBlockLock, "SendBlock");
8038 setupCond(m_SendBlockCond, "SendBlock");
8039 setupCond(m_RecvDataCond, "RecvData");
8040 setupMutex(m_SendLock, "Send");
8041 setupMutex(m_RecvLock, "Recv");
8042 setupMutex(m_RcvLossLock, "RcvLoss");
8043 setupMutex(m_RecvAckLock, "RecvAck");
8044 setupMutex(m_RcvBufferLock, "RcvBuffer");
8045 setupMutex(m_ConnectionLock, "Connection");
8046 setupMutex(m_StatsLock, "Stats");
8047 setupCond(m_RcvTsbPdCond, "RcvTsbPd");
8048 }
8049
8050 void srt::CUDT::destroySynch()
8051 {
8052 releaseMutex(m_SendBlockLock);
8053
8054 // Just in case, signal the CV, on which some
8055 // other thread is possibly waiting, because a
8056 // process hanging on a pthread_cond_wait would
8057 // cause the call to destroy a CV hang up.
8058 m_SendBlockCond.notify_all();
8059 releaseCond(m_SendBlockCond);
8060
8061 m_RecvDataCond.notify_all();
8062 releaseCond(m_RecvDataCond);
8063 releaseMutex(m_SendLock);
8064 releaseMutex(m_RecvLock);
8065 releaseMutex(m_RcvLossLock);
8066 releaseMutex(m_RecvAckLock);
8067 releaseMutex(m_RcvBufferLock);
8068 releaseMutex(m_ConnectionLock);
8069 releaseMutex(m_StatsLock);
8070
8071 m_RcvTsbPdCond.notify_all();
8072 releaseCond(m_RcvTsbPdCond);
8073 }
8074 void srt::CUDT::resetAtFork()
8075 {
8076 resetCond(m_SendBlockCond);
8077 resetCond(m_RecvDataCond);
8078 resetCond(m_RcvTsbPdCond);
8079 }
8080
8081 void srt::CUDT::releaseSynch()
8082 {
8083 SRT_ASSERT(m_bClosing);
8084 if (!m_bClosing)
8085 {
8086 LOGC(smlog.Error, log << "releaseSynch: IPE: m_bClosing not set to false, TSBPD might hangup!");
8087 m_bClosing = true;
8088 }
8089 // wake up user calls
8090 CSync::lock_notify_one(m_SendBlockCond, m_SendBlockLock);
8091
8092 enterCS(m_SendLock);
8093 leaveCS(m_SendLock);
8094
8095 // Awake tsbpd() and srt_recv*(..) threads for them to check m_bClosing.
8096 CSync::lock_notify_all(m_RecvDataCond, m_RecvLock);
8097 CSync::lock_notify_all(m_RcvTsbPdCond, m_RecvLock);
8098
8099 // Azquiring m_RcvTsbPdStartupLock protects race in starting
8100 // the tsbpd() thread in CUDT::processData().
8101 // Wait for tsbpd() thread to finish.
8102 enterCS(m_RcvTsbPdStartupLock);
8103 if (m_RcvTsbPdThread.joinable())
8104 {
8105 m_RcvTsbPdThread.join();
8106 }
8107 leaveCS(m_RcvTsbPdStartupLock);
8108
8109 // Acquiring the m_RecvLock it is assumed that both tsbpd()
8110 // and srt_recv*(..) threads will be aware about the state of m_bClosing.
8111 enterCS(m_RecvLock);
8112 leaveCS(m_RecvLock);
8113 }
8114
8115 namespace srt {
8116 #if ENABLE_HEAVY_LOGGING
8117 static void DebugAck(string hdr, int prev, int ack)
8118 {
8119 if (!prev)
8120 {
8121 HLOGC(xtlog.Debug, log << hdr << "ACK " << ack);
8122 return;
8123 }
8124
8125 int diff = CSeqNo::seqoff(prev, ack);
8126 if (diff < 0)
8127 {
8128 HLOGC(xtlog.Debug, log << hdr << "ACK ERROR: " << prev << "-" << ack << "(diff " << diff << ")");
8129 return;
8130 }
8131
8132 bool shorted = diff > 100; // sanity
8133 if (shorted)
8134 ack = CSeqNo::incseq(prev, 100);
8135
8136 ostringstream ackv;
8137 for (; prev != ack; prev = CSeqNo::incseq(prev))
8138 ackv << prev << " ";
8139 if (shorted)
8140 ackv << "...";
8141 HLOGC(xtlog.Debug, log << hdr << "ACK (" << (diff + 1) << "): " << ackv.str() << ack);
8142 }
8143 #else
8144 static inline void DebugAck(string, int, int) {}
8145 #endif
8146 }
8147
8148 void srt::CUDT::sendCtrl(UDTMessageType pkttype, const int32_t* lparam, void* rparam, int size)
8149 {
8150 CPacket ctrlpkt;
8151 setPacketTS(ctrlpkt, steady_clock::now());
8152
8153 int nbsent = 0;
8154
8155 switch (pkttype)
8156 {
8157 case UMSG_ACK: // 010 - Acknowledgement
8158 {
8159 nbsent = sendCtrlAck(ctrlpkt, size);
8160 break;
8161 }
8162
8163 case UMSG_ACKACK: // 110 - Acknowledgement of Acknowledgement
8164 ctrlpkt.pack(pkttype, lparam);
8165 ctrlpkt.set_id(m_PeerID);
8166 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8167
8168 break;
8169
8170 case UMSG_LOSSREPORT: // 011 - Loss Report
8171 {
8172 // Explicitly defined lost sequences
8173 if (rparam)
8174 {
8175 int32_t *lossdata = (int32_t *)rparam;
8176
8177 size_t bytes = sizeof(*lossdata) * size;
8178 ctrlpkt.pack(pkttype, NULL, lossdata, bytes);
8179
8180 ctrlpkt.set_id(m_PeerID);
8181 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8182
8183 enterCS(m_StatsLock);
8184 m_stats.rcvr.sentNak.count(1);
8185 leaveCS(m_StatsLock);
8186 }
8187 // Call with no arguments - get loss list from internal data.
8188 else if (m_pRcvLossList->getLossLength() > 0)
8189 {
8190 ScopedLock lock(m_RcvLossLock);
8191 // this is periodically NAK report; make sure NAK cannot be sent back too often
8192
8193 // read loss list from the local receiver loss list
8194 int32_t *data = new int32_t[m_iMaxSRTPayloadSize / 4];
8195 int losslen;
8196 m_pRcvLossList->getLossArray(data, losslen, m_iMaxSRTPayloadSize / 4);
8197
8198 if (0 < losslen)
8199 {
8200 ctrlpkt.pack(pkttype, NULL, data, losslen * 4);
8201 ctrlpkt.set_id(m_PeerID);
8202 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8203
8204 enterCS(m_StatsLock);
8205 m_stats.rcvr.sentNak.count(1);
8206 leaveCS(m_StatsLock);
8207 }
8208
8209 delete[] data;
8210 }
8211
8212 // update next NAK time, which should wait enough time for the retansmission, but not too long
8213 m_tdNAKInterval = microseconds_from(m_iSRTT + 4 * m_iRTTVar);
8214
8215 // Fix the NAKreport period according to the congctl
8216 m_tdNAKInterval =
8217 microseconds_from(m_CongCtl->updateNAKInterval(count_microseconds(m_tdNAKInterval),
8218 m_RcvTimeWindow.getPktRcvSpeed(),
8219 m_pRcvLossList->getLossLength()));
8220
8221 // This is necessary because a congctl need not wish to define
8222 // its own minimum interval, in which case the default one is used.
8223 if (m_tdNAKInterval < m_tdMinNakInterval)
8224 m_tdNAKInterval = m_tdMinNakInterval;
8225
8226 break;
8227 }
8228
8229 case UMSG_CGWARNING: // 100 - Congestion Warning
8230 ctrlpkt.pack(pkttype);
8231 ctrlpkt.set_id(m_PeerID);
8232 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8233
8234 m_tsLastWarningTime = steady_clock::now();
8235
8236 break;
8237
8238 case UMSG_KEEPALIVE: // 001 - Keep-alive
8239 ctrlpkt.pack(pkttype);
8240 ctrlpkt.set_id(m_PeerID);
8241 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8242
8243 break;
8244
8245 case UMSG_HANDSHAKE: // 000 - Handshake
8246 ctrlpkt.pack(pkttype, NULL, rparam, sizeof(CHandShake));
8247 ctrlpkt.set_id(m_PeerID);
8248 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8249
8250 break;
8251
8252 case UMSG_SHUTDOWN: // 101 - Shutdown
8253 if (m_PeerID == 0) // Don't send SHUTDOWN if we don't know peer ID.
8254 break;
8255 ctrlpkt.pack(pkttype);
8256 ctrlpkt.set_id(m_PeerID);
8257 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8258
8259 break;
8260
8261 case UMSG_DROPREQ: // 111 - Msg drop request
8262 ctrlpkt.pack(pkttype, lparam, rparam, 8);
8263 ctrlpkt.set_id(m_PeerID);
8264 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8265
8266 break;
8267
8268 case UMSG_PEERERROR: // 1000 - acknowledge the peer side a special error
8269 ctrlpkt.pack(pkttype, lparam);
8270 ctrlpkt.set_id(m_PeerID);
8271 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8272
8273 break;
8274
8275 case UMSG_EXT: // 0x7FFF - Reserved for future use
8276 break;
8277
8278 default:
8279 break;
8280 }
8281
8282 // Fix keepalive
8283 if (nbsent)
8284 m_tsLastSndTime.store(steady_clock::now());
8285 }
8286
8287 // [[using locked(m_RcvBufferLock)]]
8288 bool srt::CUDT::getFirstNoncontSequence(int32_t& w_seq, string& w_log_reason)
8289 {
8290 if (m_config.bTSBPD || !m_config.bMessageAPI)
8291 {
8292 // The getFirstNonreadSeqNo() function retuens the sequence number of the first packet
8293 // that cannot be read. In cases when a message can consist of several data packets,
8294 // an existing packet of partially available message also cannot be read.
8295 // If TSBPD mode is enabled, a message must consist of a single data packet only.
8296 w_seq = m_pRcvBuffer->getFirstNonreadSeqNo();
8297
8298 const int32_t iNextSeqNo = CSeqNo::incseq(m_iRcvCurrSeqNo);
8299 SRT_ASSERT(CSeqNo::seqcmp(w_seq, iNextSeqNo) <= 0);
8300 w_log_reason = iNextSeqNo != w_seq ? "first lost" : "expected next";
8301
8302 if (CSeqNo::seqcmp(w_seq, iNextSeqNo) > 0)
8303 {
8304 LOGC(xtlog.Error, log << "IPE: NONCONT-SEQUENCE: RCV buffer first non-read %" << w_seq << ", RCV latest seqno %" << m_iRcvCurrSeqNo);
8305 w_seq = iNextSeqNo;
8306 }
8307
8308 return true;
8309 }
8310
8311 {
8312 ScopedLock losslock (m_RcvLossLock);
8313 const int32_t seq = m_pRcvLossList->getFirstLostSeq();
8314 if (seq != SRT_SEQNO_NONE)
8315 {
8316 HLOGC(xtlog.Debug, log << "NONCONT-SEQUENCE: first loss %" << seq << " (loss len=" <<
8317 m_pRcvLossList->getLossLength() << ")");
8318 w_seq = seq;
8319 w_log_reason = "first lost";
8320 return true;
8321 }
8322 }
8323
8324 w_seq = CSeqNo::incseq(m_iRcvCurrSeqNo);
8325 HLOGC(xtlog.Debug, log << "NONCONT-SEQUENCE: past-recv %" << w_seq);
8326 w_log_reason = "expected next";
8327
8328 return true;
8329 }
8330
8331
8332 int srt::CUDT::sendCtrlAck(CPacket& ctrlpkt, int size)
8333 {
8334 int nbsent = 0;
8335 int local_prevack = 0;
8336 #if ENABLE_HEAVY_LOGGING
8337 struct SaveBack
8338 {
8339 int& target;
8340 const int& source;
8341
8342 ~SaveBack() { target = source; }
8343 } l_saveback = { m_iDebugPrevLastAck, m_iRcvLastAck };
8344 (void)l_saveback; // kill compiler warning: unused variable `l_saveback` [-Wunused-variable]
8345
8346 local_prevack = m_iDebugPrevLastAck;
8347
8348 #endif
8349 string reason; // just for "a reason" of giving particular % for ACK
8350
8351 // The TSBPD thread may change the first lost sequence record (TLPKTDROP).
8352 // To avoid it the m_RcvBufferLock has to be acquired.
8353 UniqueLock bufflock(m_RcvBufferLock);
8354 // The full ACK should be sent to indicate there is now available space in the RCV buffer
8355 // since the last full ACK. It should unblock the sender to proceed further.
8356 const bool bNeedFullAck = (m_bBufferWasFull && getAvailRcvBufferSizeNoLock() > 0);
8357 int32_t ack; // First unacknowledged packet sequence number (acknowledge up to ack).
8358
8359 if (!getFirstNoncontSequence((ack), (reason)))
8360 return nbsent;
8361
8362 if (m_iRcvLastAckAck == ack && !bNeedFullAck)
8363 {
8364 HLOGC(xtlog.Debug,
8365 log << CONID() << "sendCtrl(UMSG_ACK): last ACK %" << ack << "(" << reason << ") == last ACKACK");
8366 return nbsent;
8367 }
8368 // send out a lite ACK
8369 // to save time on buffer processing and bandwidth/AS measurement, a lite ACK only feeds back an ACK number
8370 if (size == SEND_LITE_ACK && !bNeedFullAck)
8371 {
8372 bufflock.unlock();
8373 ctrlpkt.pack(UMSG_ACK, NULL, &ack, size);
8374 ctrlpkt.set_id(m_PeerID);
8375 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8376 DebugAck(CONID() + "sendCtrl(lite): ", local_prevack, ack);
8377 return nbsent;
8378 }
8379
8380 // IF ack %> m_iRcvLastAck
8381 // There are new received packets to acknowledge, update related information.
8382 if (CSeqNo::seqcmp(ack, m_iRcvLastAck) > 0)
8383 {
8384 // Sanity check if the "selected ACK" points to a sequence
8385 // in the past for the buffer. This SHOULD NEVER HAPPEN because
8386 // on drop the loss records should have been removed, and the last received
8387 // sequence also can't be in the past towards the buffer.
8388
8389 // NOTE: This problem has been observed when the packet sequence
8390 // was incorrectly removed from the receiver loss list. This should
8391 // then stay here as a condition in order to detect this problem,
8392 // should it happen in the future.
8393 if (CSeqNo::seqcmp(ack, m_pRcvBuffer->getStartSeqNo()) < 0)
8394 {
8395 LOGC(xtlog.Error,
8396 log << CONID() << "sendCtrlAck: IPE: invalid ACK from %" << m_iRcvLastAck << " to %" << ack << " ("
8397 << CSeqNo::seqoff(m_iRcvLastAck, ack) << " packets) buffer=%" << m_pRcvBuffer->getStartSeqNo());
8398 }
8399 else
8400 {
8401 HLOGC(xtlog.Debug,
8402 log << CONID() << "sendCtrlAck: %" << m_iRcvLastAck << " -> %" << ack << " ("
8403 << CSeqNo::seqoff(m_iRcvLastAck, ack) << " packets)");
8404 }
8405
8406 m_iRcvLastAck = ack;
8407
8408 #if ENABLE_BONDING
8409 const int32_t group_read_seq = m_pRcvBuffer->getFirstReadablePacketInfo(steady_clock::now()).seqno;
8410 #endif
8411
8412 InvertedLock un_bufflock (m_RcvBufferLock);
8413
8414 #if ENABLE_BONDING
8415 // This actually should be done immediately after the ACK pointers were
8416 // updated in this socket, but it can't be done inside this function due
8417 // to being run under a lock.
8418
8419 // At this moment no locks are applied. The only lock used so far
8420 // was m_RcvBufferLock, but this was lifed above. At this moment
8421 // it is safe to apply any locks here. This function is affined
8422 // to CRcvQueue::worker thread, so it is free to apply locks as
8423 // required in the defined order. At present we only need the lock
8424 // on m_GlobControlLock to prevent the group from being deleted
8425 // in the meantime
8426 if (m_parent->m_GroupOf)
8427 {
8428 // Check is first done before locking to avoid unnecessary
8429 // mutex locking. The condition for this field is that it
8430 // can be either never set, already reset, or ever set
8431 // and possibly dangling. The re-check after lock eliminates
8432 // the dangling case.
8433 SharedLock glock (uglobal().m_GlobControlLock);
8434
8435 // Note that updateLatestRcv will lock m_GroupOf->m_GroupLock,
8436 // but this is an intended order.
8437 if (m_parent->m_GroupOf)
8438 {
8439 // A group may need to update the parallelly used idle links,
8440 // should it have any. Pass the current socket position in order
8441 // to skip it from the group loop.
8442 m_parent->m_GroupOf->updateLatestRcv(m_parent);
8443 }
8444 }
8445 #endif
8446 // If TSBPD is enabled, then INSTEAD OF signaling m_RecvDataCond,
8447 // signal m_RcvTsbPdCond. This will kick in the tsbpd thread, which
8448 // will signal m_RecvDataCond when there's time to play for particular
8449 // data packet.
8450 HLOGC(xtlog.Debug,
8451 log << CONID() << "ACK: clip %" << m_iRcvLastAck << "-%" << ack << ", REVOKED "
8452 << CSeqNo::seqoff(ack, m_iRcvLastAck) << " from RCV buffer");
8453
8454 if (m_bTsbPd)
8455 {
8456 /* Newly acknowledged data, signal TsbPD thread */
8457 CUniqueSync tslcc (m_RecvLock, m_RcvTsbPdCond);
8458 // m_bTsbPdAckWakeup is protected by m_RecvLock in the tsbpd() thread
8459 if (m_bTsbPdNeedsWakeup)
8460 tslcc.notify_one();
8461 }
8462 else
8463 {
8464 {
8465 CUniqueSync rdcc (m_RecvLock, m_RecvDataCond);
8466
8467 // Locks m_RcvBufferLock, which is unlocked above by InvertedLock un_bufflock.
8468 // Must check read-readiness under m_RecvLock to protect the epoll from concurrent changes in readBuffer()
8469 if (isRcvBufferReady())
8470 {
8471 if (m_config.bSynRecving)
8472 {
8473 // signal a waiting "recv" call if there is any data available
8474 rdcc.notify_one();
8475 }
8476 // acknowledge any waiting epolls to read
8477 // fix SRT_EPOLL_IN event loss but rcvbuffer still have data:
8478 // 1. user call receive/receivemessage(about line number:6482)
8479 // 2. after read/receive, if rcvbuffer is empty, will set SRT_EPOLL_IN event to false
8480 // 3. but if we do not do some lock work here, will cause some sync problems between threads:
8481 // (1) user thread: call receive/receivemessage
8482 // (2) user thread: read data
8483 // (3) user thread: no data in rcvbuffer, set SRT_EPOLL_IN event to false
8484 // (4) receive thread: receive data and set SRT_EPOLL_IN to true
8485 // (5) user thread: set SRT_EPOLL_IN to false
8486 // 4. so , m_RecvLock must be used here to protect epoll event
8487 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, true);
8488 }
8489 }
8490 #if ENABLE_BONDING
8491 if (group_read_seq != SRT_SEQNO_NONE && m_parent->m_GroupOf)
8492 {
8493 // See above explanation for double-checking
8494 SharedLock glock (uglobal().m_GlobControlLock);
8495
8496 if (m_parent->m_GroupOf)
8497 {
8498 // The current "APP reader" needs to simply decide as to whether
8499 // the next CUDTGroup::recv() call should return with no blocking or not.
8500 // When the group is read-ready, it should update its pollers as it sees fit.
8501 m_parent->m_GroupOf->updateReadState(m_SocketID, group_read_seq);
8502 }
8503 }
8504 #endif
8505 CGlobEvent::triggerEvent();
8506 }
8507 }
8508 else if (ack == m_iRcvLastAck && !bNeedFullAck)
8509 {
8510 // If the ACK was just sent already AND elapsed time did not exceed RTT,
8511 if ((steady_clock::now() - m_tsLastAckTime) <
8512 (microseconds_from(m_iSRTT + 4 * m_iRTTVar)))
8513 {
8514 HLOGC(xtlog.Debug,
8515 log << CONID() << "sendCtrl(UMSG_ACK): ACK %" << ack << " just sent - too early to repeat");
8516 return nbsent;
8517 }
8518 }
8519 else if (!bNeedFullAck)
8520 {
8521 // Not possible (m_iRcvCurrSeqNo+1 <% m_iRcvLastAck ?)
8522 LOGC(xtlog.Error, log << CONID() << "sendCtrl(UMSG_ACK): IPE: curr(" << reason << ") %"
8523 << ack << " <% last %" << m_iRcvLastAck);
8524 return nbsent;
8525 }
8526
8527 // [[using assert( ack >= m_iRcvLastAck && is_periodic_ack ) ]];
8528 // [[using locked(m_RcvBufferLock)]];
8529
8530 // Send out the ACK only if has not been received by the sender before
8531 if (CSeqNo::seqcmp(m_iRcvLastAck, m_iRcvLastAckAck) > 0 || bNeedFullAck)
8532 {
8533 // NOTE: The BSTATS feature turns on extra fields above size 6
8534 // also known as ACKD_TOTAL_SIZE_VER100.
8535 int32_t data[ACKD_TOTAL_SIZE];
8536
8537 // Case you care, CAckNo::incack does exactly the same thing as
8538 // CSeqNo::incseq. Logically the ACK number is a different thing
8539 // than sequence number (it's a "journal" for ACK request-response,
8540 // and starts from 0, unlike sequence, which starts from a random
8541 // number), but still the numbers are from exactly the same domain.
8542 m_iAckSeqNo = CAckNo::incack(m_iAckSeqNo);
8543 data[ACKD_RCVLASTACK] = m_iRcvLastAck;
8544 data[ACKD_RTT] = m_iSRTT;
8545 data[ACKD_RTTVAR] = m_iRTTVar;
8546 data[ACKD_BUFFERLEFT] = (int) getAvailRcvBufferSizeNoLock();
8547 m_bBufferWasFull = data[ACKD_BUFFERLEFT] == 0;
8548 if (steady_clock::now() - m_tsLastAckTime > m_tdACKInterval)
8549 {
8550 int rcvRate;
8551 int ctrlsz = ACKD_TOTAL_SIZE_UDTBASE * ACKD_FIELD_SIZE; // Minimum required size
8552
8553 data[ACKD_RCVSPEED] = m_RcvTimeWindow.getPktRcvSpeed((rcvRate));
8554 data[ACKD_BANDWIDTH] = m_RcvTimeWindow.getBandwidth();
8555
8556 //>>Patch while incompatible (1.0.2) receiver floating around
8557 if (m_uPeerSrtVersion == SrtVersion(1, 0, 2))
8558 {
8559 data[ACKD_RCVRATE] = rcvRate; // bytes/sec
8560 data[ACKD_XMRATE_VER102_ONLY] = data[ACKD_BANDWIDTH] * m_iMaxSRTPayloadSize; // bytes/sec
8561 ctrlsz = ACKD_FIELD_SIZE * ACKD_TOTAL_SIZE_VER102_ONLY;
8562 }
8563 else if (m_uPeerSrtVersion >= SrtVersion(1, 0, 3))
8564 {
8565 // Normal, currently expected version.
8566 data[ACKD_RCVRATE] = rcvRate; // bytes/sec
8567 ctrlsz = ACKD_FIELD_SIZE * ACKD_TOTAL_SIZE_VER101;
8568 }
8569 // ELSE: leave the buffer with ...UDTBASE size.
8570
8571 ctrlpkt.pack(UMSG_ACK, &m_iAckSeqNo, data, ctrlsz);
8572 m_tsLastAckTime = steady_clock::now();
8573 }
8574 else
8575 {
8576 ctrlpkt.pack(UMSG_ACK, &m_iAckSeqNo, data, ACKD_FIELD_SIZE * ACKD_TOTAL_SIZE_SMALL);
8577 }
8578 bufflock.unlock();
8579
8580 ctrlpkt.set_id(m_PeerID);
8581 setPacketTS(ctrlpkt, steady_clock::now());
8582 nbsent = m_pSndQueue->sendto(m_PeerAddr, ctrlpkt, m_SourceAddr);
8583 DebugAck(CONID() + "sendCtrl(UMSG_ACK): ", local_prevack, ack);
8584
8585 m_ACKWindow.store(m_iAckSeqNo, m_iRcvLastAck);
8586
8587 enterCS(m_StatsLock);
8588 m_stats.rcvr.sentAck.count(1);
8589 leaveCS(m_StatsLock);
8590 }
8591 else
8592 {
8593 HLOGC(xtlog.Debug, log << CONID() << "sendCtrl(UMSG_ACK): " << "ACK %" << m_iRcvLastAck
8594 << " <=% ACKACK %" << m_iRcvLastAckAck << " - NOT SENDING ACK");
8595 }
8596
8597 return nbsent;
8598 }
8599
8600 void srt::CUDT::updateSndLossListOnACK(int32_t ackdata_seqno)
8601 {
8602 #if ENABLE_BONDING
8603 // This is for the call of CSndBuffer::getMsgNoAt that returns
8604 // this value as a notfound-trap.
8605 int32_t msgno_at_last_acked_seq = SRT_MSGNO_CONTROL;
8606 bool is_group = m_parent->m_GroupOf;
8607 #endif
8608
8609 // Update sender's loss list and acknowledge packets in the sender's buffer
8610 {
8611 // m_RecvAckLock protects sender's loss list and epoll
8612 ScopedLock ack_lock(m_RecvAckLock);
8613
8614 const int offset = CSeqNo::seqoff(m_iSndLastDataAck, ackdata_seqno);
8615 // IF distance between m_iSndLastDataAck and ack is nonempty...
8616 if (offset <= 0)
8617 return;
8618
8619 // update sending variables
8620 m_iSndLastDataAck = ackdata_seqno;
8621
8622 #if ENABLE_BONDING
8623 if (is_group)
8624 {
8625 // Get offset-1 because 'offset' points actually to past-the-end
8626 // of the sender buffer. We have already checked that offset is
8627 // at least 1.
8628 msgno_at_last_acked_seq = m_pSndBuffer->getMsgNoAt(offset-1);
8629 // Just keep this value prepared; it can't be updated exactly right
8630 // now because accessing the group needs some locks to be applied
8631 // with preserved the right locking order.
8632 }
8633 #endif
8634
8635 // remove any loss that predates 'ack' (not to be considered loss anymore)
8636 m_pSndLossList->removeUpTo(CSeqNo::decseq(m_iSndLastDataAck));
8637
8638 // acknowledge the sending buffer (remove data that predate 'ack')
8639 m_pSndBuffer->ackData(offset);
8640
8641 // acknowledde any waiting epolls to write
8642 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_OUT, true);
8643 CGlobEvent::triggerEvent();
8644 }
8645
8646 #if ENABLE_BONDING
8647 if (is_group)
8648 {
8649 // m_RecvAckLock is ordered AFTER m_GlobControlLock, so this can only
8650 // be done now that m_RecvAckLock is unlocked.
8651 SharedLock glock (uglobal().m_GlobControlLock);
8652 if (m_parent->m_GroupOf)
8653 {
8654 HLOGC(inlog.Debug, log << CONID() << "ACK: acking group sender buffer for #" << msgno_at_last_acked_seq);
8655
8656 // Guard access to m_iSndAckedMsgNo field
8657 // Note: This can't be done inside CUDTGroup::ackMessage
8658 // because this function is also called from CUDT::sndDropTooLate
8659 // called from CUDT::sendmsg2 called from CUDTGroup::send, which
8660 // applies the lock on m_GroupLock already.
8661 ScopedLock glk (*m_parent->m_GroupOf->exp_groupLock());
8662
8663 // NOTE: ackMessage also accepts and ignores the trap representation
8664 // which is SRT_MSGNO_CONTROL.
8665 m_parent->m_GroupOf->ackMessage(msgno_at_last_acked_seq);
8666 }
8667 }
8668 #endif
8669
8670 // insert this socket to snd list if it is not on the list yet
8671 const steady_clock::time_point currtime = steady_clock::now();
8672 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE, currtime);
8673
8674 if (m_config.bSynSending)
8675 {
8676 CSync::lock_notify_one(m_SendBlockCond, m_SendBlockLock);
8677 }
8678
8679 // record total time used for sending
8680 enterCS(m_StatsLock);
8681 m_stats.sndDuration += count_microseconds(currtime - m_stats.sndDurationCounter);
8682 m_stats.m_sndDurationTotal += count_microseconds(currtime - m_stats.sndDurationCounter);
8683 m_stats.sndDurationCounter = currtime;
8684 leaveCS(m_StatsLock);
8685 }
8686
8687 void srt::CUDT::processCtrlAck(const CPacket &ctrlpkt, const steady_clock::time_point& currtime)
8688 {
8689 const int32_t* ackdata = (const int32_t*)ctrlpkt.m_pcData;
8690 const int32_t ackdata_seqno = ackdata[ACKD_RCVLASTACK];
8691
8692 // Check the value of ACK in case when it was some rogue peer
8693 if (ackdata_seqno < 0)
8694 {
8695 // This embraces all cases when the most significant bit is set,
8696 // as the variable is of a signed type. So, SRT_SEQNO_NONE is
8697 // included, but it also triggers for any other kind of invalid value.
8698 // This check MUST BE DONE before making any operation on this number.
8699 LOGC(inlog.Error, log << CONID() << "ACK: IPE/EPE: received invalid ACK value: " << ackdata_seqno
8700 << " " << std::hex << ackdata_seqno << " (IGNORED)");
8701 return;
8702 }
8703
8704 const bool isLiteAck = ctrlpkt.getLength() == (size_t)SEND_LITE_ACK;
8705 HLOGC(inlog.Debug,
8706 log << CONID() << "ACK covers: " << m_iSndLastDataAck << " - " << ackdata_seqno << " [ACK=" << m_iSndLastAck
8707 << "]" << (isLiteAck ? "[LITE]" : "[FULL]"));
8708
8709 updateSndLossListOnACK(ackdata_seqno);
8710
8711 // Process a lite ACK
8712 if (isLiteAck)
8713 {
8714 ScopedLock ack_lock(m_RecvAckLock);
8715 if (CSeqNo::seqcmp(ackdata_seqno, m_iSndLastAck) >= 0)
8716 {
8717 m_iFlowWindowSize = m_iFlowWindowSize - CSeqNo::seqoff(m_iSndLastAck, ackdata_seqno);
8718 m_iSndLastAck = ackdata_seqno;
8719
8720 m_tsLastRspAckTime = currtime;
8721 m_iReXmitCount = 1; // Reset re-transmit count since last ACK
8722 }
8723 return;
8724 }
8725
8726 // Decide to send ACKACK or not
8727 {
8728 // Sequence number of the ACK packet
8729 const int32_t ack_seqno = ctrlpkt.getAckSeqNo();
8730
8731 // Send ACK acknowledgement (UMSG_ACKACK).
8732 // There can be less ACKACK packets in the stream, than the number of ACK packets.
8733 // Only send ACKACK every syn interval or if ACK packet with the sequence number
8734 // already acknowledged (with ACKACK) has come again, which probably means ACKACK was lost.
8735 if ((currtime - m_SndLastAck2Time > microseconds_from(COMM_SYN_INTERVAL_US)) || (ack_seqno == m_iSndLastAck2))
8736 {
8737 sendCtrl(UMSG_ACKACK, &ack_seqno);
8738 m_iSndLastAck2 = ack_seqno;
8739 m_SndLastAck2Time = currtime;
8740 }
8741 }
8742
8743 //
8744 // Begin of the new code with TLPKTDROP.
8745 //
8746
8747 // Protect packet retransmission
8748 {
8749 UniqueLock ack_lock(m_RecvAckLock);
8750
8751 // Check the validation of the ack
8752 if (CSeqNo::seqcmp(ackdata_seqno, CSeqNo::incseq(m_iSndCurrSeqNo)) > 0)
8753 {
8754 ack_lock.unlock();
8755
8756 // this should not happen: attack or bug
8757 LOGC(gglog.Error,
8758 log << CONID() << "ATTACK/IPE: incoming ack seq " << ackdata_seqno << " exceeds current "
8759 << m_iSndCurrSeqNo << " by " << (CSeqNo::seqoff(m_iSndCurrSeqNo, ackdata_seqno) - 1) << "!");
8760 m_bBroken = true;
8761 m_iBrokenCounter = 0;
8762
8763 updateBrokenConnection();
8764 completeBrokenConnectionDependencies(SRT_ESECFAIL); // LOCKS!
8765 return;
8766 }
8767
8768 if (CSeqNo::seqcmp(ackdata_seqno, m_iSndLastAck) >= 0)
8769 {
8770 const int cwnd1 = std::min<int>(m_iFlowWindowSize, m_iCongestionWindow);
8771 const bool bWasStuck = cwnd1<= getFlightSpan();
8772 // Update Flow Window Size, must update before and together with m_iSndLastAck
8773 m_iFlowWindowSize = ackdata[ACKD_BUFFERLEFT];
8774 m_iSndLastAck = ackdata_seqno;
8775 m_tsLastRspAckTime = currtime;
8776 m_iReXmitCount = 1; // Reset re-transmit count since last ACK
8777
8778 const int cwnd = std::min<int>(m_iFlowWindowSize, m_iCongestionWindow);
8779 if (bWasStuck && cwnd > getFlightSpan())
8780 {
8781 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE);
8782 HLOGC(gglog.Debug,
8783 log << CONID() << "processCtrlAck: could reschedule SND. iFlowWindowSize " << m_iFlowWindowSize
8784 << " SPAN " << getFlightSpan() << " ackdataseqno %" << ackdata_seqno);
8785 }
8786 }
8787
8788 /*
8789 * We must not ignore full ack received by peer
8790 * if data has been artificially acked by late packet drop.
8791 * Therefore, a distinct ack state is used for received Ack (iSndLastFullAck)
8792 * and ack position in send buffer (m_iSndLastDataAck).
8793 * Otherwise, when severe congestion causing packet drops (and m_iSndLastDataAck update)
8794 * occurs, we drop received acks (as duplicates) and do not update stats like RTT,
8795 * which may go crazy and stay there, preventing proper stream recovery.
8796 */
8797
8798 if (CSeqNo::seqoff(m_iSndLastFullAck, ackdata_seqno) <= 0)
8799 {
8800 // discard it if it is a repeated ACK
8801 return;
8802 }
8803 m_iSndLastFullAck = ackdata_seqno;
8804 }
8805 //
8806 // END of the new code with TLPKTDROP
8807 //
8808 #if ENABLE_BONDING
8809 if (m_parent->m_GroupOf)
8810 {
8811 SharedLock glock (uglobal().m_GlobControlLock);
8812 if (m_parent->m_GroupOf)
8813 {
8814 // Will apply m_GroupLock, ordered after m_GlobControlLock.
8815 // m_GlobControlLock is necessary for group existence.
8816 m_parent->m_GroupOf->updateWriteState();
8817 }
8818 }
8819 #endif
8820
8821 size_t acksize = ctrlpkt.getLength(); // TEMPORARY VALUE FOR CHECKING
8822 bool wrongsize = 0 != (acksize % ACKD_FIELD_SIZE);
8823 acksize = acksize / ACKD_FIELD_SIZE; // ACTUAL VALUE
8824
8825 if (wrongsize)
8826 {
8827 // Issue a log, but don't do anything but skipping the "odd" bytes from the payload.
8828 LOGC(inlog.Warn,
8829 log << CONID() << "Received UMSG_ACK payload is not evened up to 4-byte based field size - cutting to "
8830 << acksize << " fields");
8831 }
8832
8833 // Start with checking the base size.
8834 if (acksize < ACKD_TOTAL_SIZE_SMALL)
8835 {
8836 LOGC(inlog.Warn, log << CONID() << "Invalid ACK size " << acksize << " fields - less than minimum required!");
8837 // Ack is already interpreted, just skip further parts.
8838 return;
8839 }
8840 // This check covers fields up to ACKD_BUFFERLEFT.
8841
8842 // Extract RTT estimate and RTTVar from the ACK packet.
8843 const int rtt = ackdata[ACKD_RTT];
8844 const int rttvar = ackdata[ACKD_RTTVAR];
8845
8846 // Update the values of smoothed RTT and the variation in RTT samples
8847 // on subsequent RTT estimates extracted from the ACK packets
8848 // (during transmission).
8849 if (m_bIsFirstRTTReceived)
8850 {
8851 // Suppose transmission is bidirectional if sender is also receiving
8852 // data packets.
8853 enterCS(m_StatsLock);
8854 const bool bPktsReceived = m_stats.rcvr.recvd.total.count() != 0;
8855 leaveCS(m_StatsLock);
8856
8857 if (bPktsReceived) // Transmission is bidirectional.
8858 {
8859 // RTT value extracted from the ACK packet (rtt) is already smoothed
8860 // RTT obtained at the receiver side. Apply EWMA anyway for the second
8861 // time on the sender side. Ignore initial values which might arrive
8862 // after the smoothed RTT on the sender side has been
8863 // reset to the very first RTT sample received from the receiver.
8864 // TODO: The case of bidirectional transmission requires further
8865 // improvements and testing. Double smoothing is applied here to be
8866 // consistent with the previous behavior.
8867 if (rtt != INITIAL_RTT || rttvar != INITIAL_RTTVAR)
8868 {
8869 int iSRTT = m_iSRTT.load(), iRTTVar = m_iRTTVar.load();
8870 iRTTVar = avg_iir<4>(iRTTVar, abs(rtt - iSRTT));
8871 iSRTT = avg_iir<8>(iSRTT, rtt);
8872 m_iSRTT = iSRTT;
8873 m_iRTTVar = iRTTVar;
8874 }
8875 }
8876 else // Transmission is unidirectional.
8877 {
8878 // Simply take the values of smoothed RTT and RTT variance from
8879 // the ACK packet.
8880 m_iSRTT = rtt;
8881 m_iRTTVar = rttvar;
8882 }
8883 }
8884 // Reset the value of smoothed RTT to the first real RTT estimate extracted
8885 // from an ACK after initialization (at the beginning of transmission).
8886 // In case of resumed connection over the same network, the very first RTT
8887 // value sent within an ACK will be taken from cache and equal to previous
8888 // connection's final smoothed RTT value. The reception of such a value
8889 // will also trigger the smoothed RTT reset at the sender side.
8890 else if (rtt != INITIAL_RTT && rttvar != INITIAL_RTTVAR)
8891 {
8892 m_iSRTT = rtt;
8893 m_iRTTVar = rttvar;
8894 m_bIsFirstRTTReceived = true;
8895 }
8896
8897 #if SRT_DEBUG_RTT
8898 s_rtt_trace.trace(currtime, "ACK", rtt, rttvar, m_bIsFirstRTTReceived,
8899 m_stats.recvTotal, m_iSRTT, m_iRTTVar);
8900 #endif
8901
8902 /* Version-dependent fields:
8903 * Original UDT (total size: ACKD_TOTAL_SIZE_SMALL):
8904 * ACKD_RCVLASTACK
8905 * ACKD_RTT
8906 * ACKD_RTTVAR
8907 * ACKD_BUFFERLEFT
8908 * Additional UDT fields, not always attached:
8909 * ACKD_RCVSPEED
8910 * ACKD_BANDWIDTH
8911 * SRT extension since v1.0.1:
8912 * ACKD_RCVRATE
8913 * SRT extension in v1.0.2 only:
8914 * ACKD_XMRATE_VER102_ONLY
8915 */
8916
8917 if (acksize > ACKD_TOTAL_SIZE_SMALL)
8918 {
8919 // This means that ACKD_RCVSPEED and ACKD_BANDWIDTH fields are available.
8920 int pktps = ackdata[ACKD_RCVSPEED];
8921 int bandwidth = ackdata[ACKD_BANDWIDTH];
8922 int bytesps;
8923
8924 /* SRT v1.0.2 Bytes-based stats: bandwidth (pcData[ACKD_XMRATE_VER102_ONLY]) and delivery rate (pcData[ACKD_RCVRATE]) in
8925 * bytes/sec instead of pkts/sec */
8926 /* SRT v1.0.3 Bytes-based stats: only delivery rate (pcData[ACKD_RCVRATE]) in bytes/sec instead of pkts/sec */
8927 if (acksize > ACKD_TOTAL_SIZE_UDTBASE)
8928 bytesps = ackdata[ACKD_RCVRATE];
8929 else
8930 bytesps = pktps * m_iMaxSRTPayloadSize;
8931
8932 m_iBandwidth = avg_iir<8>(m_iBandwidth.load(), bandwidth);
8933 m_iDeliveryRate = avg_iir<8>(m_iDeliveryRate.load(), pktps);
8934 m_iByteDeliveryRate = avg_iir<8>(m_iByteDeliveryRate.load(), bytesps);
8935
8936 // Update Estimated Bandwidth and packet delivery rate
8937 // m_iRcvRate = m_iDeliveryRate;
8938 // ^^ This has been removed because with the SrtCongestion class
8939 // instead of reading the m_iRcvRate local field this will read
8940 // cudt->deliveryRate() instead.
8941 }
8942
8943 updateCC(TEV_ACK, EventVariant(ackdata_seqno));
8944
8945 enterCS(m_StatsLock);
8946 m_stats.sndr.recvdAck.count(1);
8947 leaveCS(m_StatsLock);
8948 }
8949
8950 void srt::CUDT::processCtrlAckAck(const CPacket& ctrlpkt, const time_point& tsArrival)
8951 {
8952 int32_t ack = 0;
8953
8954 // Calculate RTT estimate on the receiver side based on ACK/ACKACK pair.
8955 const int rtt = m_ACKWindow.acknowledge(ctrlpkt.getAckSeqNo(), ack, tsArrival);
8956
8957 if (rtt == -1)
8958 {
8959 if (ctrlpkt.getAckSeqNo() > (m_iAckSeqNo - static_cast<int>(ACK_WND_SIZE)) && ctrlpkt.getAckSeqNo() <= m_iAckSeqNo)
8960 {
8961 string why;
8962 if (frequentLogAllowed(FREQLOGFA_ACKACK_OUTOFORDER, tsArrival, (why)))
8963 {
8964 LOGC(inlog.Note,
8965 log << CONID() << "ACKACK out of order, skipping RTT calculation "
8966 << "(ACK number: " << ctrlpkt.getAckSeqNo() << ", last ACK sent: " << m_iAckSeqNo
8967 << ", RTT (EWMA): " << m_iSRTT << ")." << why);
8968 }
8969 #if SRT_ENABLE_FREQUENT_LOG_TRACE
8970 else
8971 {
8972 LOGC(qrlog.Note, log << "SUPPRESSED: ACKACK out of order LOG: " << why);
8973 }
8974 #endif
8975
8976 return;
8977 }
8978
8979 LOGC(inlog.Error,
8980 log << CONID() << "ACK record not found, can't estimate RTT "
8981 << "(ACK number: " << ctrlpkt.getAckSeqNo() << ", last ACK sent: " << m_iAckSeqNo
8982 << ", RTT (EWMA): " << m_iSRTT << ")");
8983 return;
8984 }
8985
8986 if (rtt <= 0)
8987 {
8988 LOGC(inlog.Error,
8989 log << CONID() << "IPE: invalid RTT estimate " << rtt
8990 << ", possible time shift. Clock: " << SRT_SYNC_CLOCK_STR);
8991 return;
8992 }
8993
8994 // If increasing delay is detected.
8995 // sendCtrl(UMSG_CGWARNING);
8996
8997 // Update the values of smoothed RTT and the variation in RTT samples
8998 // on subsequent RTT samples (during transmission).
8999 if (m_bIsFirstRTTReceived)
9000 {
9001 m_iRTTVar = avg_iir<4>(m_iRTTVar.load(), abs(rtt - m_iSRTT.load()));
9002 m_iSRTT = avg_iir<8>(m_iSRTT.load(), rtt);
9003 }
9004 // Reset the value of smoothed RTT on the first RTT sample after initialization
9005 // (at the beginning of transmission).
9006 // In case of resumed connection over the same network, the initial RTT
9007 // value will be taken from cache and equal to previous connection's
9008 // final smoothed RTT value.
9009 else
9010 {
9011 m_iSRTT = rtt;
9012 m_iRTTVar = rtt / 2;
9013 m_bIsFirstRTTReceived = true;
9014 }
9015
9016 #if SRT_DEBUG_RTT
9017 s_rtt_trace.trace(tsArrival, "ACKACK", rtt, -1, m_bIsFirstRTTReceived,
9018 -1, m_iSRTT, m_iRTTVar);
9019 #endif
9020
9021 updateCC(TEV_ACKACK, EventVariant(ack));
9022
9023 // This function will put a lock on m_RecvLock by itself, as needed.
9024 // It must be done inside because this function reads the current time
9025 // and if waiting for the lock has caused a delay, the time will be
9026 // inaccurate. Additionally it won't lock if TSBPD mode is off, and
9027 // won't update anything. Note that if you set TSBPD mode and use
9028 // srt_recvfile (which doesn't make any sense), you'll have a deadlock.
9029 if (m_config.bDriftTracer)
9030 {
9031 #if ENABLE_BONDING
9032 ExclusiveLock glock(uglobal().m_GlobControlLock); // XXX not too excessive?
9033 const bool drift_updated =
9034 #endif
9035 m_pRcvBuffer->addRcvTsbPdDriftSample(ctrlpkt.getMsgTimeStamp(), tsArrival, rtt);
9036
9037 #if ENABLE_BONDING
9038 if (drift_updated && m_parent->m_GroupOf)
9039 m_parent->m_GroupOf->synchronizeDrift(this);
9040 #endif
9041 }
9042
9043 // Update last ACK that has been received by the sender
9044 if (CSeqNo::seqcmp(ack, m_iRcvLastAckAck) > 0)
9045 m_iRcvLastAckAck = ack;
9046 }
9047
9048 void srt::CUDT::processCtrlLossReport(const CPacket& ctrlpkt)
9049 {
9050 const int32_t* losslist = (int32_t*)(ctrlpkt.m_pcData);
9051 const size_t losslist_len = ctrlpkt.getLength() / 4;
9052
9053 bool secure = true;
9054
9055 // This variable is used in "normal" logs, so it may cause a warning
9056 // when logging is forcefully off.
9057 int32_t wrong_loss SRT_ATR_UNUSED = SRT_SEQNO_NONE;
9058
9059 // protect packet retransmission
9060 {
9061 ScopedLock ack_lock(m_RecvAckLock);
9062
9063 // decode loss list message and insert loss into the sender loss list
9064 for (int i = 0, n = (int)losslist_len; i < n; ++i)
9065 {
9066 // IF the loss is a range <LO, HI>
9067 if (IsSet(losslist[i], LOSSDATA_SEQNO_RANGE_FIRST))
9068 {
9069 // Then it's this is a <LO, HI> specification with HI in a consecutive cell.
9070 const int32_t losslist_lo = SEQNO_VALUE::unwrap(losslist[i]);
9071 const int32_t losslist_hi = losslist[i + 1];
9072 // <LO, HI> specification means that the consecutive cell has been already interpreted.
9073 ++i;
9074
9075 HLOGC(inlog.Debug, log << CONID() << "received UMSG_LOSSREPORT: "
9076 << losslist_lo << "-" << losslist_hi
9077 << " (" << CSeqNo::seqlen(losslist_lo, losslist_hi) << " packets)...");
9078
9079 if ((CSeqNo::seqcmp(losslist_lo, losslist_hi) > 0) ||
9080 (CSeqNo::seqcmp(losslist_hi, m_iSndCurrSeqNo) > 0))
9081 {
9082 // LO must not be greater than HI.
9083 // HI must not be greater than the most recent sent seq.
9084 LOGC(inlog.Warn, log << CONID() << "rcv LOSSREPORT rng " << losslist_lo << " - " << losslist_hi
9085 << " with last sent " << m_iSndCurrSeqNo << " - DISCARDING");
9086 secure = false;
9087 wrong_loss = losslist_hi;
9088 break;
9089 }
9090
9091 int num = 0;
9092 // IF losslist_lo %>= m_iSndLastAck
9093 if (CSeqNo::seqcmp(losslist_lo, m_iSndLastAck) >= 0)
9094 {
9095 HLOGC(inlog.Debug, log << CONID() << "LOSSREPORT: adding "
9096 << losslist_lo << " - " << losslist_hi << " to loss list");
9097 num = m_pSndLossList->insert(losslist_lo, losslist_hi);
9098 }
9099 // ELSE losslist_lo %< m_iSndLastAck
9100 else
9101 {
9102 // This should be theoretically impossible because this would mean that
9103 // the received packet loss report informs about the loss that predates
9104 // the ACK sequence.
9105 // However, this can happen in these situations:
9106 // - if the packet reordering has caused the earlier sent LOSSREPORT will be
9107 // delivered after later sent ACK. Whatever, ACK should be more important,
9108 // so simply drop the part that predates ACK.
9109 // - redundancy second link (ISN was screwed up initially, but late towards last sent)
9110 // - initial DROPREQ was lost
9111 // This just causes repeating DROPREQ, as when the receiver continues sending
9112 // LOSSREPORT, it's probably UNAWARE OF THE SITUATION.
9113 int32_t dropreq_hi = losslist_hi;
9114 IF_HEAVY_LOGGING(const char* drop_type = "completely");
9115
9116 // IF losslist_hi %>= m_iSndLastAck
9117 if (CSeqNo::seqcmp(losslist_hi, m_iSndLastAck) >= 0)
9118 {
9119 HLOGC(inlog.Debug, log << CONID() << "LOSSREPORT: adding "
9120 << m_iSndLastAck << "[ACK] - " << losslist_hi << " to loss list");
9121 num = m_pSndLossList->insert(m_iSndLastAck, losslist_hi);
9122 dropreq_hi = CSeqNo::decseq(m_iSndLastAck);
9123 IF_HEAVY_LOGGING(drop_type = "partially");
9124 }
9125
9126 // In distinction to losslist, DROPREQ has always just one range,
9127 // and the data are <LO, HI>, with no range bit.
9128 int32_t seqpair[2] = { losslist_lo, dropreq_hi };
9129 const int32_t no_msgno = 0; // We don't know.
9130
9131 // When this DROPREQ gets lost in UDP again, the receiver will do one of these:
9132 // - repeatedly send LOSSREPORT (as per NAKREPORT), so this will happen again
9133 // - finally give up rexmit request as per TLPKTDROP (DROPREQ should make
9134 // TSBPD wake up should it still wait for new packets to get ACK-ed)
9135 HLOGC(inlog.Debug,
9136 log << CONID() << "LOSSREPORT: " << drop_type << " IGNORED with SndLastAck=%" << m_iSndLastAck
9137 << ": %" << losslist_lo << "-" << dropreq_hi << " - sending DROPREQ");
9138 sendCtrl(UMSG_DROPREQ, &no_msgno, seqpair, sizeof(seqpair));
9139 }
9140
9141 enterCS(m_StatsLock);
9142 m_stats.sndr.lost.count(num);
9143 leaveCS(m_StatsLock);
9144 }
9145 // ELSE the loss is a single seq
9146 else
9147 {
9148 // IF loss_seq %>= m_iSndLastAck
9149 if (CSeqNo::seqcmp(losslist[i], m_iSndLastAck) >= 0)
9150 {
9151 if (CSeqNo::seqcmp(losslist[i], m_iSndCurrSeqNo) > 0)
9152 {
9153 LOGC(inlog.Warn, log << CONID() << "rcv LOSSREPORT pkt %" << losslist[i]
9154 << " with last sent %" << m_iSndCurrSeqNo << " - DISCARDING");
9155 // loss_seq must not be greater than the most recent sent seq
9156 secure = false;
9157 wrong_loss = losslist[i];
9158 break;
9159 }
9160
9161 HLOGC(inlog.Debug,
9162 log << CONID() << "LOSSREPORT: adding %" << losslist[i] << " (1 packet) to loss list");
9163 const int num = m_pSndLossList->insert(losslist[i], losslist[i]);
9164
9165 enterCS(m_StatsLock);
9166 m_stats.sndr.lost.count(num);
9167 leaveCS(m_StatsLock);
9168 }
9169 // ELSE loss_seq %< m_iSndLastAck
9170 else
9171 {
9172 // In distinction to losslist, DROPREQ has always just one range,
9173 // and the data are <LO, HI>, with no range bit.
9174 int32_t seqpair[2] = { losslist[i], losslist[i] };
9175 const int32_t no_msgno = 0; // We don't know.
9176 HLOGC(inlog.Debug,
9177 log << CONID() << "LOSSREPORT: IGNORED with SndLastAck=%" << m_iSndLastAck << ": %" << losslist[i]
9178 << " - sending DROPREQ");
9179 sendCtrl(UMSG_DROPREQ, &no_msgno, seqpair, sizeof(seqpair));
9180 }
9181 }
9182 }
9183 }
9184
9185 updateCC(TEV_LOSSREPORT, EventVariant(losslist, losslist_len));
9186
9187 if (!secure)
9188 {
9189 LOGC(inlog.Warn,
9190 log << CONID() << "out-of-band LOSSREPORT received; BUG or ATTACK - last sent %" << m_iSndCurrSeqNo
9191 << " vs loss %" << wrong_loss);
9192 // this should not happen: attack or bug
9193 m_bBroken = true;
9194 m_iBrokenCounter = 0;
9195
9196 updateBrokenConnection();
9197 completeBrokenConnectionDependencies(SRT_ESECFAIL); // LOCKS!
9198 return;
9199 }
9200
9201 // the lost packet (retransmission) should be sent out immediately
9202 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE);
9203
9204 enterCS(m_StatsLock);
9205 m_stats.sndr.recvdNak.count(1);
9206 leaveCS(m_StatsLock);
9207 }
9208
9209 void srt::CUDT::processCtrlHS(const CPacket& ctrlpkt)
9210 {
9211 CHandShake req;
9212 req.load_from(ctrlpkt.m_pcData, ctrlpkt.getLength());
9213
9214 HLOGC(inlog.Debug, log << CONID() << "processCtrl: got HS: " << req.show());
9215
9216 if ((req.m_iReqType > URQ_INDUCTION_TYPES) // actually it catches URQ_INDUCTION and URQ_ERROR_* symbols...???
9217 || (m_config.bRendezvous && (req.m_iReqType != URQ_AGREEMENT))) // rnd sends AGREEMENT in rsp to CONCLUSION
9218 {
9219 // The peer side has not received the handshake message, so it keeps querying
9220 // resend the handshake packet
9221
9222 // This condition embraces cases when:
9223 // - this is normal accept() and URQ_INDUCTION was received
9224 // - this is rendezvous accept() and there's coming any kind of URQ except AGREEMENT (should be RENDEZVOUS
9225 // or CONCLUSION)
9226 // - this is any of URQ_ERROR_* - well...
9227 CHandShake initdata;
9228 initdata.m_iISN = m_iISN;
9229 initdata.m_iMSS = m_config.iMSS;
9230 initdata.m_iFlightFlagSize = m_config.iFlightFlagSize;
9231
9232 // For rendezvous we do URQ_WAVEAHAND/URQ_CONCLUSION --> URQ_AGREEMENT.
9233 // For client-server we do URQ_INDUCTION --> URQ_CONCLUSION.
9234 initdata.m_iReqType = (!m_config.bRendezvous) ? URQ_CONCLUSION : URQ_AGREEMENT;
9235 initdata.m_iID = m_SocketID;
9236
9237 uint32_t kmdata[SRTDATA_MAXSIZE];
9238 size_t kmdatasize = SRTDATA_MAXSIZE;
9239 bool have_hsreq = false;
9240 if (req.m_iVersion > HS_VERSION_UDT4)
9241 {
9242 initdata.m_iVersion = HS_VERSION_SRT1; // if I remember correctly, this is induction/listener...
9243 const int hs_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_ConnRes.m_iType);
9244 if (hs_flags != 0) // has SRT extensions
9245 {
9246 HLOGC(inlog.Debug,
9247 log << CONID() << "processCtrl/HS: got HS reqtype=" << RequestTypeStr(req.m_iReqType)
9248 << " WITH SRT ext");
9249 have_hsreq = interpretSrtHandshake(req, ctrlpkt, (kmdata), (&kmdatasize));
9250 if (!have_hsreq)
9251 {
9252 initdata.m_iVersion = 0;
9253 m_RejectReason = SRT_REJ_ROGUE;
9254 initdata.m_iReqType = URQFailure(m_RejectReason);
9255 }
9256 else
9257 {
9258 // Extensions are added only in case of CONCLUSION (not AGREEMENT).
9259 // Actually what is expected here is that this may either process the
9260 // belated-repeated handshake from a caller (and then it's CONCLUSION,
9261 // and should be added with HSRSP/KMRSP), or it's a belated handshake
9262 // of Rendezvous when it has already considered itself connected.
9263 // Sanity check - according to the rules, there should be no such situation
9264 if (m_config.bRendezvous && m_SrtHsSide == HSD_RESPONDER)
9265 {
9266 LOGC(inlog.Error,
9267 log << CONID() << "processCtrl/HS: IPE???: RESPONDER should receive all its handshakes in "
9268 "handshake phase.");
9269 }
9270
9271 // The 'extension' flag will be set from this variable; set it to false
9272 // in case when the AGREEMENT response is to be sent.
9273 have_hsreq = initdata.m_iReqType == URQ_CONCLUSION;
9274 HLOGC(inlog.Debug,
9275 log << CONID() << "processCtrl/HS: processing ok, reqtype=" << RequestTypeStr(initdata.m_iReqType)
9276 << " kmdatasize=" << kmdatasize);
9277 }
9278 }
9279 else
9280 {
9281 HLOGC(inlog.Debug, log << CONID() << "processCtrl/HS: got HS reqtype=" << RequestTypeStr(req.m_iReqType));
9282 }
9283 }
9284 else
9285 {
9286 initdata.m_iVersion = HS_VERSION_UDT4;
9287 kmdatasize = 0; // HSv4 doesn't add any extensions, no KMX
9288 }
9289
9290 initdata.m_extension = have_hsreq;
9291
9292 HLOGC(inlog.Debug,
9293 log << CONID() << "processCtrl: responding HS reqtype=" << RequestTypeStr(initdata.m_iReqType)
9294 << (have_hsreq ? " WITH SRT HS response extensions" : ""));
9295
9296 CPacket rsppkt;
9297 rsppkt.setControl(UMSG_HANDSHAKE);
9298 rsppkt.allocate(m_iMaxSRTPayloadSize);
9299
9300 // If createSrtHandshake failed, don't send anything. Actually it can only fail on IPE.
9301 // There is also no possible IPE condition in case of HSv4 - for this version it will always return true.
9302 enterCS(m_ConnectionLock);
9303 bool create_ok = createSrtHandshake(SRT_CMD_HSRSP, SRT_CMD_KMRSP, kmdata, kmdatasize, (rsppkt), (initdata));
9304 leaveCS(m_ConnectionLock);
9305 if (create_ok)
9306 {
9307 rsppkt.set_id(m_PeerID);
9308 setPacketTS(rsppkt, steady_clock::now());
9309 const int nbsent = m_pSndQueue->sendto(m_PeerAddr, rsppkt, m_SourceAddr);
9310 if (nbsent)
9311 {
9312 m_tsLastSndTime.store(steady_clock::now());
9313 }
9314 }
9315 }
9316 else
9317 {
9318 HLOGC(inlog.Debug, log << CONID() << "processCtrl: ... not INDUCTION, not ERROR, not rendezvous - IGNORED.");
9319 }
9320 }
9321
9322 void srt::CUDT::processCtrlDropReq(const CPacket& ctrlpkt)
9323 {
9324 const int32_t* dropdata = (const int32_t*) ctrlpkt.m_pcData;
9325
9326 {
9327 CUniqueSync rcvtscc (m_RecvLock, m_RcvTsbPdCond);
9328 // With both TLPktDrop and TsbPd enabled, a message always consists only of one packet.
9329 // It will be dropped as too late anyway. Not dropping it from the receiver buffer
9330 // in advance reduces false drops if the packet somehow manages to arrive.
9331 // Still remove the record from the loss list to cease further retransmission requests.
9332 if (!m_bTLPktDrop || !m_bTsbPd)
9333 {
9334 const bool using_rexmit_flag = m_bPeerRexmitFlag;
9335 ScopedLock rblock(m_RcvBufferLock);
9336 const int iDropCnt = m_pRcvBuffer->dropMessage(dropdata[0], dropdata[1], ctrlpkt.getMsgSeq(using_rexmit_flag), CRcvBuffer::KEEP_EXISTING);
9337
9338 if (iDropCnt > 0)
9339 {
9340 ScopedLock lg (m_StatsLock);
9341 const steady_clock::time_point tnow = steady_clock::now();
9342 string why;
9343 if (frequentLogAllowed(FREQLOGFA_RCV_DROPPED, tnow, (why)))
9344 {
9345 LOGC(brlog.Warn, log << CONID() << "RCV-DROPPED " << iDropCnt << " packet(s), seqno range %"
9346 << dropdata[0] << "-%" << dropdata[1] << ", msgno " << ctrlpkt.getMsgSeq(using_rexmit_flag)
9347 << " (SND DROP REQUEST). " << why);
9348 }
9349 #if SRT_ENABLE_FREQUENT_LOG_TRACE
9350 else
9351 {
9352 LOGC(brlog.Warn, log << "SUPPRESSED: RCV-DROPPED LOG: " << why);
9353 }
9354 #endif
9355
9356 // Estimate dropped bytes from average payload size.
9357 const uint64_t avgpayloadsz = m_pRcvBuffer->getRcvAvgPayloadSize();
9358 m_stats.rcvr.dropped.count(stats::BytesPackets(iDropCnt * avgpayloadsz, (uint32_t) iDropCnt));
9359 }
9360 }
9361 // When the drop request was received, it means that there are
9362 // packets for which there will never be ACK sent; if the TSBPD thread
9363 // is currently in the ACK-waiting state, it may never exit.
9364 if (m_bTsbPd)
9365 {
9366 HLOGP(inlog.Debug, "DROPREQ: signal TSBPD");
9367 rcvtscc.notify_one();
9368 }
9369 }
9370
9371 dropFromLossLists(dropdata[0], dropdata[1]);
9372
9373 // If dropping ahead of the current largest sequence number,
9374 // move the recv seq number forward.
9375 if ((CSeqNo::seqcmp(dropdata[0], CSeqNo::incseq(m_iRcvCurrSeqNo)) <= 0)
9376 && (CSeqNo::seqcmp(dropdata[1], m_iRcvCurrSeqNo) > 0))
9377 {
9378 HLOGC(inlog.Debug, log << CONID() << "DROPREQ: dropping %"
9379 << dropdata[0] << "-" << dropdata[1] << " <-- set as current seq");
9380 m_iRcvCurrSeqNo = dropdata[1];
9381 }
9382 else
9383 {
9384 HLOGC(inlog.Debug, log << CONID() << "DROPREQ: dropping %"
9385 << dropdata[0] << "-" << dropdata[1] << " current %" << m_iRcvCurrSeqNo);
9386 }
9387 }
9388
9389 void srt::CUDT::processCtrlShutdown()
9390 {
9391 m_bShutdown = true;
9392 m_bClosing = true;
9393 m_bBroken = true;
9394 m_iBrokenCounter = 60;
9395
9396 // This does the same as it would happen on connection timeout,
9397 // just we know about this state prematurely thanks to this message.
9398 updateBrokenConnection();
9399 completeBrokenConnectionDependencies(SRT_ECONNLOST); // LOCKS!
9400 }
9401
9402 void srt::CUDT::processCtrlUserDefined(const CPacket& ctrlpkt)
9403 {
9404 HLOGC(inlog.Debug, log << CONID() << "CONTROL EXT MSG RECEIVED:"
9405 << MessageTypeStr(ctrlpkt.getType(), ctrlpkt.getExtendedType())
9406 << ", value=" << ctrlpkt.getExtendedType());
9407
9408 // This has currently two roles in SRT:
9409 // - HSv4 (legacy) handshake
9410 // - refreshed KMX (initial KMX is done still in the HS process in HSv5)
9411 const bool understood = processSrtMsg(&ctrlpkt);
9412 // CAREFUL HERE! This only means that this update comes from the UMSG_EXT
9413 // message received, REGARDLESS OF WHAT IT IS. This version doesn't mean
9414 // the handshake version, but the reason of calling this function.
9415 //
9416 // Fortunately, the only messages taken into account in this function
9417 // are HSREQ and HSRSP, which should *never* be interchanged when both
9418 // parties are HSv5.
9419 if (understood)
9420 {
9421 if (ctrlpkt.getExtendedType() == SRT_CMD_HSREQ || ctrlpkt.getExtendedType() == SRT_CMD_HSRSP)
9422 {
9423 updateAfterSrtHandshake(HS_VERSION_UDT4);
9424 }
9425 }
9426 else
9427 {
9428 updateCC(TEV_CUSTOM, EventVariant(&ctrlpkt));
9429 }
9430 }
9431
9432 void srt::CUDT::processCtrl(const CPacket &ctrlpkt)
9433 {
9434 // Just heard from the peer, reset the expiration count.
9435 m_iEXPCount = 1;
9436 const steady_clock::time_point currtime = steady_clock::now();
9437 m_tsLastRspTime = currtime;
9438
9439 HLOGC(inlog.Debug,
9440 log << CONID() << "incoming UMSG:" << ctrlpkt.getType() << " ("
9441 << MessageTypeStr(ctrlpkt.getType(), ctrlpkt.getExtendedType()) << ") socket=%" << ctrlpkt.id());
9442
9443 switch (ctrlpkt.getType())
9444 {
9445 case UMSG_ACK: // 010 - Acknowledgement
9446 processCtrlAck(ctrlpkt, currtime);
9447 break;
9448
9449 case UMSG_ACKACK: // 110 - Acknowledgement of Acknowledgement
9450 processCtrlAckAck(ctrlpkt, currtime);
9451 break;
9452
9453 case UMSG_LOSSREPORT: // 011 - Loss Report
9454 processCtrlLossReport(ctrlpkt);
9455 break;
9456
9457 case UMSG_CGWARNING: // 100 - Delay Warning
9458 // One way packet delay is increasing, so decrease the sending rate
9459 m_tdSendInterval = (m_tdSendInterval.load() * 1125) / 1000;
9460 // XXX Note as interesting fact: this is only prepared for handling,
9461 // but nothing in the code is sending this message. Probably predicted
9462 // for a custom congctl. There's a predicted place to call it under
9463 // UMSG_ACKACK handling, but it's commented out.
9464
9465 break;
9466
9467 case UMSG_KEEPALIVE: // 001 - Keep-alive
9468 processKeepalive(ctrlpkt, currtime);
9469 break;
9470
9471 case UMSG_HANDSHAKE: // 000 - Handshake
9472 processCtrlHS(ctrlpkt);
9473 break;
9474
9475 case UMSG_SHUTDOWN: // 101 - Shutdown
9476 processCtrlShutdown();
9477 break;
9478
9479 case UMSG_DROPREQ: // 111 - Msg drop request
9480 processCtrlDropReq(ctrlpkt);
9481 break;
9482
9483 case UMSG_PEERERROR: // 1000 - An error has happened to the peer side
9484 // int err_type = packet.getAddInfo();
9485
9486 // currently only this error is signalled from the peer side
9487 // if recvfile() fails (e.g., due to disk fail), blocked sendfile/send should return immediately
9488 // giving the app a chance to fix the issue
9489 m_bPeerHealth = false;
9490
9491 break;
9492
9493 case UMSG_EXT: // 0x7FFF - reserved and user defined messages
9494 processCtrlUserDefined(ctrlpkt);
9495 break;
9496
9497 default:
9498 break;
9499 }
9500 }
9501
9502 void srt::CUDT::updateSrtRcvSettings()
9503 {
9504 // CHANGED: we need to apply the tsbpd delay only for socket TSBPD.
9505 // For Group TSBPD the buffer will have to deliver packets always on request
9506 // by sequence number, although the buffer will have to solve all the TSBPD
9507 // things internally anyway. Extracting by sequence number means only that
9508 // the packet can be retrieved from the buffer before its time to play comes
9509 // (unlike in normal situation when reading directly from socket), however
9510 // its time to play shall be properly defined.
9511 ScopedLock lock(m_RecvLock);
9512
9513 // NOTE: remember to also update synchronizeWithGroup() if more settings are updated here.
9514 m_pRcvBuffer->setPeerRexmitFlag(m_bPeerRexmitFlag);
9515
9516 // XXX m_bGroupTsbPd is ignored with SRT_ENABLE_APP_READER
9517 if (m_bTsbPd || m_bGroupTsbPd)
9518 {
9519 m_pRcvBuffer->setTsbPdMode(m_tsRcvPeerStartTime, false, milliseconds_from(m_iTsbPdDelay_ms));
9520
9521 HLOGC(cnlog.Debug, log << "AFTER HS: Set Rcv TsbPd mode"
9522 << (m_bGroupTsbPd ? " (AS GROUP MEMBER)" : "")
9523 << ": delay=" << (m_iTsbPdDelay_ms / 1000) << "." << (m_iTsbPdDelay_ms % 1000)
9524 << "s RCV START: " << FormatTime(m_tsRcvPeerStartTime).c_str());
9525 }
9526 else
9527 {
9528 HLOGC(cnlog.Debug, log << CONID() << "AFTER HS: Rcv TsbPd mode not set");
9529 }
9530 }
9531
9532 void srt::CUDT::updateSrtSndSettings()
9533 {
9534 if (m_bPeerTsbPd)
9535 {
9536 /* We are TsbPd sender */
9537 // XXX Check what happened here.
9538 // m_iPeerTsbPdDelay_ms = m_CongCtl->getSndPeerTsbPdDelay();// + ((m_iSRTT + (4 * m_iRTTVar)) / 1000);
9539 /*
9540 * For sender to apply Too-Late Packet Drop
9541 * option (m_bTLPktDrop) must be enabled and receiving peer shall support it
9542 */
9543 HLOGC(cnlog.Debug, log << "AFTER HS: Set Snd TsbPd mode "
9544 << (m_bPeerTLPktDrop ? "with" : "without")
9545 << " TLPktDrop: delay=" << (m_iPeerTsbPdDelay_ms/1000) << "." << (m_iPeerTsbPdDelay_ms%1000)
9546 << "s START TIME: " << FormatTime(m_stats.tsStartTime).c_str());
9547 }
9548 else
9549 {
9550 HLOGC(cnlog.Debug, log << CONID() << "AFTER HS: Snd TsbPd mode not set");
9551 }
9552 }
9553
9554 void srt::CUDT::updateAfterSrtHandshake(int hsv)
9555 {
9556 HLOGC(cnlog.Debug, log << CONID() << "updateAfterSrtHandshake: HS version " << hsv);
9557 // This is blocked from being run in the "app reader" version because here
9558 // every socket does its TsbPd independently, just the sequence screwup is
9559 // done and the application reader sorts out packets by sequence numbers,
9560 // but only when they are signed off by TsbPd.
9561
9562 // The only possibility here is one of these two:
9563 // - Agent is RESPONDER and it receives HSREQ.
9564 // - Agent is INITIATOR and it receives HSRSP.
9565 //
9566 // In HSv4, INITIATOR is sender and RESPONDER is receiver.
9567 // In HSv5, both are sender AND receiver.
9568 //
9569 // This function will be called only ONCE in this
9570 // instance, through either HSREQ or HSRSP.
9571 #if ENABLE_HEAVY_LOGGING
9572 const char* hs_side[] = { "DRAW", "INITIATOR", "RESPONDER" };
9573 #if ENABLE_BONDING
9574 string grpspec;
9575
9576 if (m_parent->m_GroupOf)
9577 {
9578 SharedLock glock (uglobal().m_GlobControlLock);
9579 grpspec = m_parent->m_GroupOf
9580 ? " group=$" + Sprint(m_parent->m_GroupOf->id())
9581 : string();
9582 }
9583 #else
9584 const char* grpspec = "";
9585 #endif
9586
9587 HLOGC(cnlog.Debug,
9588 log << CONID() << "updateAfterSrtHandshake: version=" << m_ConnRes.m_iVersion
9589 << " side=" << hs_side[m_SrtHsSide] << grpspec);
9590 #endif
9591
9592 if (hsv > HS_VERSION_UDT4)
9593 {
9594 updateSrtRcvSettings();
9595 updateSrtSndSettings();
9596 }
9597 else if (m_SrtHsSide == HSD_INITIATOR)
9598 {
9599 // HSv4 INITIATOR is sender
9600 updateSrtSndSettings();
9601 }
9602 else
9603 {
9604 // HSv4 RESPONDER is receiver
9605 updateSrtRcvSettings();
9606 }
9607 }
9608
9609 // XXX During sender buffer refax turn this into either returning
9610 // a sequence number or move it to the sender buffer facility.
9611 // [[using locked (m_RecvAckLock)]]
9612 std::pair<int32_t, int> srt::CUDT::getCleanRexmitOffset()
9613 {
9614 // This function is required to look into the loss list and
9615 // drop all sequences that are already revoked from the sender
9616 // buffer, send the drop request if needed, and return the sender
9617 // buffer offset for the next packet to retransmit, or -1 if
9618 // there is no retransmission candidate at the moment.
9619
9620 for (;;)
9621 {
9622 // REPEATABLE because we might need to drop this scheduled seq.
9623
9624 // Pick up the first sequence.
9625 int32_t seq = m_pSndLossList->popLostSeq();
9626 if (seq == SRT_SEQNO_NONE)
9627 {
9628 // No loss reports, we are clear here.
9629 return std::make_pair(SRT_SEQNO_NONE, -1);
9630 }
9631
9632 int offset = CSeqNo::seqoff(m_iSndLastDataAck, seq);
9633 if (offset < 0)
9634 {
9635 // XXX Likely that this will never be executed because if the upper
9636 // sequence is not in the sender buffer, then most likely the loss
9637 // was completely ignored.
9638 LOGC(qslog.Error, log << CONID()
9639 << "IPE/EPE: packLostData: LOST packet negative offset: seqoff(seqno() "
9640 << seq << ", m_iSndLastDataAck " << m_iSndLastDataAck << ")=" << offset
9641 << ". Continue, request DROP");
9642
9643 int32_t oldest_outdated_seq = seq;
9644 // We have received the first outdated sequence.
9645 // Loop on to find out more.
9646 // Interrupt on the first non-outdated sequence
9647 // or when the loss list gets empty.
9648
9649 for (;;)
9650 {
9651 seq = m_pSndLossList->popLostSeq();
9652 if (seq == SRT_SEQNO_NONE)
9653 {
9654 offset = -1; // set it because this will be returned.
9655 break;
9656 }
9657
9658 offset = CSeqNo::seqoff(m_iSndLastDataAck, seq);
9659 if (offset >= 0)
9660 break;
9661 }
9662
9663 // Now we have 'seq' that is either non-sequence
9664 // or a valid sequence. We can safely exit with this
9665 // value from this branch. We just need to manage
9666 // the drop report.
9667
9668 // No matter whether this is right or not (maybe the attack case should be
9669 // considered, and some LOSSREPORT flood prevention), send the drop request
9670 // to the peer.
9671 int32_t seqpair[2] = {
9672 oldest_outdated_seq,
9673 CSeqNo::decseq(m_iSndLastDataAck)
9674 };
9675
9676 HLOGC(qslog.Debug,
9677 log << CONID() << "PEER reported LOSS not from the sending buffer - requesting DROP: %("
9678 << seqpair[0] << " - " << seqpair[1] << ") (" << (-offset) << " packets)");
9679
9680 // See interpretation in processCtrlDropReq(). We don't know the message number,
9681 // so we request that the drop be exclusively sequence number based.
9682 int32_t msgno = SRT_MSGNO_CONTROL;
9683 sendCtrl(UMSG_DROPREQ, &msgno, seqpair, sizeof(seqpair));
9684 }
9685
9686 // We have exit anyway with the right sequence or no sequence.
9687 if (seq == SRT_SEQNO_NONE)
9688 return std::make_pair(SRT_SEQNO_NONE, -1);
9689
9690 // For the right sequence though check also the right time.
9691 const steady_clock::time_point time_now = steady_clock::now();
9692 if (!checkRexmitRightTime(offset, time_now))
9693 {
9694 // Ignore this, even though valid, sequence, and pick up
9695 // the next from the list.
9696 continue;
9697 }
9698
9699 // Ok, we have what we needed one way or another.
9700 return std::make_pair(seq, offset);
9701 }
9702 }
9703
9704 // [[using locked (m_RecvAckLock)]]
9705 bool srt::CUDT::checkRexmitRightTime(int offset, const srt::sync::steady_clock::time_point& current_time)
9706 {
9707 // If not configured, the time is always right
9708 if (!m_bPeerNakReport || m_config.iRetransmitAlgo == 0)
9709 return true;
9710
9711 const steady_clock::time_point time_nak = current_time - microseconds_from(m_iSRTT - 4 * m_iRTTVar);
9712 const steady_clock::time_point tsLastRexmit = m_pSndBuffer->getPacketRexmitTime(offset);
9713
9714 if (tsLastRexmit >= time_nak)
9715 {
9716 #if ENABLE_HEAVY_LOGGING
9717 ostringstream last_rextime;
9718 if (is_zero(tsLastRexmit))
9719 {
9720 last_rextime << "REXMIT FIRST TIME";
9721 }
9722 else
9723 {
9724 last_rextime << "REXMITTED " << FormatDuration<DUNIT_US>(current_time - tsLastRexmit)
9725 << " ago at " << FormatTime(tsLastRexmit);
9726 }
9727 HLOGC(qslog.Debug, log << CONID() << "REXMIT: ignoring %" << CSeqNo::incseq(m_iSndLastDataAck, offset)
9728 << " " << last_rextime.str()
9729 << ", RTT: ~" << FormatValue(m_iSRTT, 1000, "ms")
9730 << " <>" << FormatValue(m_iRTTVar, 1000, "")
9731 << " now=" << FormatTime(current_time));
9732 #endif
9733 return false;
9734 }
9735
9736 return true;
9737 }
9738
9739
9740 // [[using locked (m_RecvAckLock)]]
9741 int srt::CUDT::extractCleanRexmitPacket(int32_t seqno, int offset, CPacket& w_packet,
9742 srt::sync::steady_clock::time_point& w_tsOrigin)
9743 {
9744 // REPEATABLE BLOCK (not a real loop)
9745 for (;;)
9746 {
9747 typedef CSndBuffer::DropRange DropRange;
9748 DropRange buffer_drop;
9749 w_packet.set_seqno(seqno);
9750
9751 const int payload = m_pSndBuffer->readData(offset, (w_packet), (w_tsOrigin), (buffer_drop));
9752 if (payload == CSndBuffer::READ_DROP)
9753 {
9754 SRT_ASSERT(CSeqNo::seqoff(buffer_drop.seqno[DropRange::BEGIN], buffer_drop.seqno[DropRange::END]) >= 0);
9755
9756 HLOGC(qslog.Debug,
9757 log << CONID() << "loss-reported packets expired in SndBuf - requesting DROP: #"
9758 << buffer_drop.msgno << " %(" << buffer_drop.seqno[DropRange::BEGIN] << " - "
9759 << buffer_drop.seqno[DropRange::END] << ")");
9760 sendCtrl(UMSG_DROPREQ, &buffer_drop.msgno, buffer_drop.seqno, sizeof(buffer_drop.seqno));
9761
9762 // skip all dropped packets
9763 m_pSndLossList->removeUpTo(buffer_drop.seqno[DropRange::END]);
9764 m_iSndCurrSeqNo = CSeqNo::maxseq(m_iSndCurrSeqNo, buffer_drop.seqno[DropRange::END]);
9765 continue;
9766 }
9767
9768 if (payload == CSndBuffer::READ_NONE)
9769 {
9770 LOGC(qslog.Error, log << CONID() << "loss-reported packet %" << w_packet.seqno() << " NOT FOUND in the sender buffer");
9771 return 0;
9772 }
9773
9774 return payload;
9775 }
9776
9777 }
9778
9779 int srt::CUDT::packLostData(CPacket& w_packet)
9780 {
9781 #if ENABLE_MAXREXMITBW
9782
9783 #ifdef ENABLE_RATE_MEASUREMENT
9784 #define IF_RATE_MEA(expr) expr
9785 #else
9786 #define IF_RATE_MEA(expr)
9787 #endif
9788
9789 // XXX NOTE: If you refactor the sender buffer so that the random access
9790 // is possible, you might be able to extract the exact packet size to
9791 // check for enough tokens and consume them in one step.
9792
9793 bool maxrexmitbw_enabled = m_config.llMaxRexmitBW >= 0;
9794 if (maxrexmitbw_enabled)
9795 {
9796 IF_RATE_MEA( IF_HEAVY_LOGGING(const int64_t iRexmitRateMeasured = m_SndRexmitMeasurement.rateBytes()); )
9797 size_t granted_size = m_zSndAveragePacketSize;
9798 size_t len = granted_size + CPacket::HDR_SIZE + CPacket::UDP_HDR_SIZE;
9799
9800 if (!m_SndRexmitShaper.enoughTokens(len))
9801 {
9802 HLOGC(qslog.Debug, log << "REXMIT-SH: BLOCKED pkt est/len=" << len << " exceeds " << m_SndRexmitShaper.ntokens()
9803 << " tokens, rate/Bps:used=" << m_SndRexmitShaper.usedRate_Bps() << ",avail=" << m_SndRexmitShaper.availRate_Bps()
9804 IF_RATE_MEA( << " (measured: " << FormatValue(iRexmitRateMeasured, 1024, "kBps") << ")" )
9805 );
9806 return 0;
9807 }
9808
9809 HLOGC(qslog.Debug, log << "REXMIT-SH: ALLOWED pkt est/len=" << len << " allowed, budget " << m_SndRexmitShaper.ntokens()
9810 << " tokens, rate/Bps:used=" << m_SndRexmitShaper.usedRate_Bps() << ",avail=" << m_SndRexmitShaper.availRate_Bps()
9811 IF_RATE_MEA( << " (measured: " << FormatValue(iRexmitRateMeasured, 1024, "kBps") << ")" )
9812 );
9813 }
9814
9815 #undef IF_RATE_MEA
9816 #endif
9817
9818 // Original sending time of the packet, to be stamped after filling.
9819 steady_clock::time_point tsOrigin;
9820
9821 {
9822 // protect m_iSndLastDataAck from updating by ACK processing
9823 ScopedLock ackguard(m_RecvAckLock);
9824 int32_t seqno;
9825 int offset;
9826
9827 // Get the first sequence for retransmission, bypassing and taking care of
9828 // those that are in the forgotten region, as well as required to be rejected.
9829 Tie2(seqno, offset) = getCleanRexmitOffset();
9830
9831 if (seqno == SRT_SEQNO_NONE)
9832 return 0;
9833
9834 // Extract the packet from the sender buffer that is mapped to the expected sequence
9835 // number, bypassing and taking care of those that are decided to be dropped.
9836 const int payload = extractCleanRexmitPacket(seqno, offset, (w_packet), (tsOrigin));
9837 if (payload <= 0)
9838 return 0;
9839 }
9840
9841 HLOGC(qslog.Debug, log << CONID() << "packed REXMIT packet %" << w_packet.seqno() << " size=" << w_packet.getLength()
9842 << " - still " << m_pSndLossList->getLossLength() << " LOSS ENTRIES left");
9843
9844 // POST-EXTRACTION length fixes.
9845
9846 // The packet has been ecrypted, thus the authentication tag is expected to be stored
9847 // in the SND buffer as well right after the payload.
9848 if (m_pCryptoControl && m_pCryptoControl->getCryptoMode() == CSrtConfig::CIPHER_MODE_AES_GCM)
9849 {
9850 w_packet.setLength(w_packet.getLength() + HAICRYPT_AUTHTAG_MAX);
9851 }
9852
9853 #if ENABLE_MAXREXMITBW
9854 if (maxrexmitbw_enabled)
9855 {
9856 // Token consumption will only happen when the retransmission
9857 // effectively happens.
9858 // XXX NOTE: In version 1.6.0 use the IP-version dependent value for UDP_HDR_SIZE
9859 size_t network_size = w_packet.getLength() + CPacket::HDR_SIZE + CPacket::UDP_HDR_SIZE;
9860 m_SndRexmitShaper.consumeTokens(network_size);
9861 HLOGC(qslog.Debug, log << "REXMIT-SH: consumed " << network_size << " tokens, remain " << m_SndRexmitShaper.ntokens());
9862 }
9863 #endif
9864
9865 enterCS(m_StatsLock);
9866 m_stats.sndr.sentRetrans.count(w_packet.getLength());
9867 leaveCS(m_StatsLock);
9868
9869 // Despite the contextual interpretation of packet.m_iMsgNo around
9870 // CSndBuffer::readData version 2 (version 1 doesn't return -1), in this particular
9871 // case we can be sure that this is exactly the value of PH_MSGNO as a bitset.
9872 // So, set here the rexmit flag if the peer understands it.
9873 if (m_bPeerRexmitFlag)
9874 {
9875 w_packet.set_msgflags(w_packet.msgflags() | PACKET_SND_REXMIT);
9876 }
9877 setDataPacketTS(w_packet, tsOrigin);
9878
9879 #ifdef ENABLE_MAXREXMITBW
9880 // XXX OLD rexmit measurement
9881 // m_SndRexmitRate.addSample(time_now, 1, w_packet.getLength());
9882 #endif
9883
9884 // XXX Consider calculating the total packet length once you have a possibility
9885 // to get the in-connection used IP version.
9886 #ifdef ENABLE_RATE_MEASUREMENT
9887 m_SndRexmitMeasurement.dataUpdate(1, w_packet.getLength());
9888 HLOGC(bslog.Debug, log << "RateMeasurement: REXMIT, pkt-size=" << w_packet.getLength()
9889 << " - COLLECTED pkts=" << m_SndRexmitMeasurement.m_nInstaPackets
9890 << " bytes=" << m_SndRexmitMeasurement.m_nInstaBytes);
9891 #endif
9892
9893 return w_packet.getLength();
9894 }
9895
9896 #if SRT_DEBUG_TRACE_SND
9897 class snd_logger
9898 {
9899 typedef srt::sync::steady_clock steady_clock;
9900
9901 public:
9902 snd_logger() {}
9903
9904 ~snd_logger()
9905 {
9906 ScopedLock lck(m_mtx);
9907 m_fout.close();
9908 }
9909
9910 struct
9911 {
9912 typedef srt::sync::steady_clock steady_clock;
9913 long long usElapsed;
9914 steady_clock::time_point tsNow;
9915 int usSRTT;
9916 int usRTTVar;
9917 int msSndBuffSpan;
9918 int msTimespanTh;
9919 int msNextUniqueToSend;
9920 long long usElapsedLastDrop;
9921 bool canRexmit;
9922 int iPktSeqno;
9923 bool isRetransmitted;
9924 } state;
9925
9926 void trace()
9927 {
9928 using namespace srt::sync;
9929 ScopedLock lck(m_mtx);
9930 create_file();
9931
9932 m_fout << state.usElapsed << ",";
9933 m_fout << state.usSRTT << ",";
9934 m_fout << state.usRTTVar << ",";
9935 m_fout << state.msSndBuffSpan << ",";
9936 m_fout << state.msTimespanTh << ",";
9937 m_fout << state.msNextUniqueToSend << ",";
9938 m_fout << state.usElapsedLastDrop << ",";
9939 m_fout << state.canRexmit << ",";
9940 m_fout << state.iPktSeqno << ',';
9941 m_fout << state.isRetransmitted << '\n';
9942
9943 m_fout.flush();
9944 }
9945
9946 private:
9947 void print_header()
9948 {
9949 m_fout << "usElapsed,usSRTT,usRTTVar,msSndBuffTimespan,msTimespanTh,msNextUniqueToSend,usDLastDrop,canRexmit,sndPktSeqno,isRexmit";
9950 m_fout << "\n";
9951 }
9952
9953 void create_file()
9954 {
9955 if (m_fout.is_open())
9956 return;
9957
9958 m_start_time = srt::sync::steady_clock::now();
9959 std::string str_tnow = srt::sync::FormatTimeSys(m_start_time);
9960 str_tnow.resize(str_tnow.size() - 7); // remove trailing ' [SYST]' part
9961 while (str_tnow.find(':') != std::string::npos)
9962 {
9963 str_tnow.replace(str_tnow.find(':'), 1, 1, '_');
9964 }
9965 const std::string fname = "snd_trace_" + str_tnow + ".csv";
9966 m_fout.open(fname, std::ofstream::out);
9967 if (!m_fout)
9968 std::cerr << "IPE: Failed to open " << fname << "!!!\n";
9969
9970 print_header();
9971 }
9972
9973 private:
9974 srt::sync::Mutex m_mtx;
9975 std::ofstream m_fout;
9976 srt::sync::steady_clock::time_point m_start_time;
9977 };
9978
9979 snd_logger g_snd_logger;
9980 #endif // SRT_DEBUG_TRACE_SND
9981
9982 void srt::CUDT::setPacketTS(CPacket& p, const time_point& ts)
9983 {
9984 enterCS(m_StatsLock);
9985 const time_point tsStart = m_stats.tsStartTime;
9986 leaveCS(m_StatsLock);
9987 p.set_timestamp(makeTS(ts, tsStart));
9988 }
9989
9990 void srt::CUDT::setDataPacketTS(CPacket& p, const time_point& ts)
9991 {
9992 enterCS(m_StatsLock);
9993 const time_point tsStart = m_stats.tsStartTime;
9994 leaveCS(m_StatsLock);
9995
9996 if (!m_bPeerTsbPd)
9997 {
9998 // If TSBPD is disabled, use the current time as the source (timestamp using the sending time).
9999 p.set_timestamp(makeTS(steady_clock::now(), tsStart));
10000 return;
10001 }
10002
10003 // TODO: Might be better for performance to ensure this condition is always false, and just use SRT_ASSERT here.
10004 if (ts < tsStart)
10005 {
10006 p.set_timestamp(makeTS(steady_clock::now(), tsStart));
10007 LOGC(qslog.Warn,
10008 log << CONID() << "setPacketTS: reference time=" << FormatTime(ts)
10009 << " is in the past towards start time=" << FormatTime(tsStart)
10010 << " - setting NOW as reference time for the data packet");
10011 return;
10012 }
10013
10014 // Use the provided source time for the timestamp.
10015 p.set_timestamp(makeTS(ts, tsStart));
10016 }
10017
10018 bool srt::CUDT::isRegularSendingPriority()
10019 {
10020 // In order to have regular packets take precedence over retransmitted:
10021 // - SRTO_TLPKTDROP = true
10022 // - SRTO_MESSAGEAPI = true
10023 // NOTE:
10024 // - tlpktdrop is ignored in stream mode
10025 // - messageapi without tlpktdrop is possible in non-live message mode
10026 // - Live mode without tlpktdrop is possible and in this mode also the
10027 // retransmitted packets should have priority.
10028 if (!m_bPeerTLPktDrop || !m_config.bMessageAPI)
10029 return false;
10030
10031 // XXX NOTE: the current solution is simple - the regular packet takes precedence
10032 // over a retransmitted packet, if there is at least one such packet already
10033 // scheduled.
10034 //
10035 // This probably isn't the most wanted solution, some more elaborate condition
10036 // might be better in some situations. For example, it should be acceptable
10037 // that a packet that has very little time to be recovered is sent before a
10038 // regular packet that has still STT + Latency time to deliver. The regular
10039 // packets should still be favored, but not necessarily at the expense of
10040 // dismissing a recovery chance that wouldn't endanger the delivery of a regular
10041 // packet. Criteria might be various, for example, the number of scheduled
10042 // packets and their late delivery time might be taken into account.
10043 const time_point tsNextPacket = m_pSndBuffer->peekNextOriginal();
10044
10045 if (tsNextPacket != time_point())
10046 {
10047 // Have regular packet and we decided they have a priority.
10048 HLOGC(qslog.Debug, log << "REXMIT-SH: BLOCKED because regular packets have priority");
10049 return true;
10050 }
10051 return false;
10052 }
10053
10054 namespace srt {
10055 #ifdef ENABLE_MAXREXMITBW
10056 static sync::steady_clock::duration optimisticSingleTripDuration(int rttval, int rttvar)
10057 {
10058 int lowrtt = rttval - rttvar;
10059 if (lowrtt < 0) // bullshit?
10060 lowrtt = rttval;
10061
10062 int stt = lowrtt/2;
10063
10064 // Make sure that burst period has at least this value
10065 sync::steady_clock::duration stt_td = sync::microseconds_from(stt);
10066
10067 // Still, prevent from setting enormous STT values
10068 if (stt_td < sync::seconds_from(4))
10069 {
10070 return stt_td;
10071 }
10072 return sync::steady_clock::duration();
10073 }
10074 #endif
10075 }
10076
10077 void srt::CUDT::updateSenderMeasurements(bool can_rexmit SRT_ATR_UNUSED)
10078 {
10079 const time_point tnow SRT_ATR_UNUSED = steady_clock::now();
10080
10081 #if SRT_DEBUG_TRACE_SND
10082 const int buffdelay_ms = count_milliseconds(m_pSndBuffer->getBufferingDelay(tnow));
10083 // If there is a small loss, still better to retransmit. If timespan is already big,
10084 // then consider sending original packets.
10085 const int threshold_ms_min = (2 * m_iSRTT + 4 * m_iRTTVar + COMM_SYN_INTERVAL_US) / 1000;
10086 const int msNextUniqueToSend = count_milliseconds(tnow - tsNextPacket) + m_iPeerTsbPdDelay_ms;
10087
10088 g_snd_logger.state.tsNow = tnow;
10089 g_snd_logger.state.usElapsed = count_microseconds(tnow - m_stats.tsStartTime);
10090 g_snd_logger.state.usSRTT = m_iSRTT;
10091 g_snd_logger.state.usRTTVar = m_iRTTVar;
10092 g_snd_logger.state.msSndBuffSpan = buffdelay_ms;
10093 g_snd_logger.state.msTimespanTh = threshold_ms_min;
10094 g_snd_logger.state.msNextUniqueToSend = msNextUniqueToSend;
10095 g_snd_logger.state.usElapsedLastDrop = count_microseconds(tnow - m_tsLastTLDrop);
10096 g_snd_logger.state.canRexmit = can_rexmit;
10097 #endif
10098
10099 #ifdef ENABLE_MAXREXMITBW
10100 /* OLD rate estimator; CODE for packLostData()!
10101 m_SndRexmitRate.addSample(tnow, 0, 0); // Update the estimation.
10102 const int64_t iRexmitRateBps = m_SndRexmitRate.getRate();
10103 if (iRexmitRateLimitBps >= 0 && iRexmitRateBps > iRexmitRateLimitBps)
10104 {
10105 // Too many retransmissions, so don't send anything.
10106 // TODO: When to wake up next time?
10107 return false;
10108 }
10109 */
10110 const int64_t iRexmitRateLimitBps = m_config.llMaxRexmitBW;
10111 if (iRexmitRateLimitBps >= 0)
10112 {
10113 int b4_tokens SRT_ATR_UNUSED = m_SndRexmitShaper.ntokens();
10114 m_SndRexmitShaper.setBitrate(iRexmitRateLimitBps);
10115 duration stt_td = optimisticSingleTripDuration(m_iSRTT, m_iRTTVar);
10116 if (stt_td != duration())
10117 m_SndRexmitShaper.setMinimumBurstPeriod(stt_td);
10118 m_SndRexmitShaper.tick(tnow);
10119 int a4_tokens SRT_ATR_UNUSED = m_SndRexmitShaper.ntokens();
10120
10121 HLOGC(qslog.Debug, log << "REXMIT-SW: tick tokens updated: " << b4_tokens << " -> " << a4_tokens
10122 << "/" << m_SndRexmitShaper.maxTokens() << " period="
10123 << FormatDuration<DUNIT_MS>(m_SndRexmitShaper.burstPeriod()));
10124 }
10125 #endif
10126
10127 }
10128
10129 bool srt::CUDT::packData(CPacket& w_packet, steady_clock::time_point& w_nexttime, sockaddr_any& w_src_addr)
10130 {
10131 int payload = 0;
10132 bool probe = false;
10133 bool new_packet_packed = false;
10134
10135 const steady_clock::time_point enter_time = steady_clock::now();
10136
10137 w_nexttime = enter_time;
10138
10139 if (!is_zero(m_tsNextSendTime) && enter_time > m_tsNextSendTime)
10140 {
10141 m_tdSendTimeDiff = m_tdSendTimeDiff.load() + (enter_time - m_tsNextSendTime);
10142 }
10143
10144 ScopedLock connectguard(m_ConnectionLock);
10145 // If a closing action is done simultaneously, then
10146 // m_bOpened should already be false, and it's set
10147 // just before releasing this lock.
10148 //
10149 // If this lock is caught BEFORE the closing could
10150 // start the dissolving process, this process will
10151 // not be started until this function is finished.
10152 if (!m_bOpened)
10153 return false;
10154
10155 // This function returns true if there is a regular packet candidate waiting
10156 // for being sent and sending this packet has a prioriry over retransmission candidate.
10157 if (!isRegularSendingPriority())
10158 {
10159 payload = packLostData((w_packet));
10160 #if ENABLE_MAXREXMITBW
10161 if (payload == 0)
10162 {
10163 HLOGC(qslog.Debug, log << "REXMIT-SH: no rexmit required, remain " << m_SndRexmitShaper.ntokens() << " tokens");
10164 }
10165 #endif
10166 }
10167 else
10168 {
10169 HLOGC(qslog.Debug, log << "REXMIT: retransmission SUPERSEDED, proceed with regular candidate");
10170 }
10171
10172 // Updates the data that will be next used in packLostData() in next calls.
10173 updateSenderMeasurements(payload != 0);
10174
10175 IF_HEAVY_LOGGING(const char* reason); // The source of the data packet (normal/rexmit/filter)
10176 if (payload > 0)
10177 {
10178 IF_HEAVY_LOGGING(reason = "reXmit");
10179 }
10180 else if (m_PacketFilter &&
10181 m_PacketFilter.packControlPacket(m_iSndCurrSeqNo, m_pCryptoControl->getSndCryptoFlags(), (w_packet)))
10182 {
10183 HLOGC(qslog.Debug, log << CONID() << "filter: filter/CTL packet ready - packing instead of data.");
10184 payload = (int) w_packet.getLength();
10185 IF_HEAVY_LOGGING(reason = "filter");
10186
10187 // Stats
10188 ScopedLock lg(m_StatsLock);
10189 m_stats.sndr.sentFilterExtra.count(1);
10190 }
10191 else
10192 {
10193 if (!packUniqueData(w_packet))
10194 {
10195 m_tsNextSendTime = steady_clock::time_point();
10196 m_tdSendTimeDiff = steady_clock::duration();
10197 return false;
10198 }
10199
10200 #if ENABLE_MAXREXMITBW
10201 if (m_zSndAveragePacketSize > 0)
10202 {
10203 m_zSndAveragePacketSize = avg_iir<16>(m_zSndAveragePacketSize, w_packet.getLength());
10204 }
10205 else
10206 {
10207 m_zSndAveragePacketSize = w_packet.getLength();
10208 }
10209 m_zSndMaxPacketSize = std::max(m_zSndMaxPacketSize, w_packet.getLength());
10210 #endif
10211 new_packet_packed = true;
10212
10213 // every 16 (0xF) packets, a packet pair is sent
10214 if ((w_packet.seqno() & PUMASK_SEQNO_PROBE) == 0)
10215 probe = true;
10216
10217 payload = (int) w_packet.getLength();
10218 IF_HEAVY_LOGGING(reason = "normal");
10219 }
10220
10221 w_packet.set_id(m_PeerID); // Set the destination SRT socket ID.
10222
10223 if (new_packet_packed && m_PacketFilter)
10224 {
10225 HLOGC(qslog.Debug, log << CONID() << "filter: Feeding packet for source clip");
10226 m_PacketFilter.feedSource((w_packet));
10227 }
10228
10229 #if ENABLE_HEAVY_LOGGING // Required because of referring to MessageFlagStr()
10230 HLOGC(qslog.Debug,
10231 log << CONID() << "packData: " << reason << " packet seq=" << w_packet.seqno() << " (ACK=" << m_iSndLastAck
10232 << " ACKDATA=" << m_iSndLastDataAck << " MSG/FLAGS: " << w_packet.MessageFlagStr() << ")");
10233 #endif
10234
10235 // Fix keepalive
10236 m_tsLastSndTime.store(enter_time);
10237
10238 considerLegacySrtHandshake(steady_clock::time_point());
10239
10240 // WARNING: TEV_SEND is the only event that is reported from
10241 // the CSndQueue::worker thread. All others are reported from
10242 // CRcvQueue::worker. If you connect to this signal, make sure
10243 // that you are aware of prospective simultaneous access.
10244 updateCC(TEV_SEND, EventVariant(&w_packet));
10245
10246 // XXX This was a blocked code also originally in UDT. Probably not required.
10247 // Left untouched for historical reasons.
10248 // Might be possible that it was because of that this is send from
10249 // different thread than the rest of the signals.
10250 // m_pSndTimeWindow->onPktSent(w_packet.timestamp());
10251
10252 enterCS(m_StatsLock);
10253 m_stats.sndr.sent.count(payload);
10254 if (new_packet_packed)
10255 m_stats.sndr.sentUnique.count(payload);
10256 leaveCS(m_StatsLock);
10257
10258 const duration sendint = m_tdSendInterval;
10259 if (probe)
10260 {
10261 // sends out probing packet pair
10262 m_tsNextSendTime = enter_time;
10263 // Sending earlier, need to adjust the pace later on.
10264 m_tdSendTimeDiff = m_tdSendTimeDiff.load() - sendint;
10265 probe = false;
10266 }
10267 else
10268 {
10269 #if USE_BUSY_WAITING
10270 m_tsNextSendTime = enter_time + m_tdSendInterval.load();
10271 #else
10272 const duration sendbrw = m_tdSendTimeDiff;
10273
10274 if (sendbrw >= sendint)
10275 {
10276 // Send immediately
10277 m_tsNextSendTime = enter_time;
10278
10279 // ATOMIC NOTE: this is the only thread that
10280 // modifies this field
10281 m_tdSendTimeDiff = sendbrw - sendint;
10282 }
10283 else
10284 {
10285 m_tsNextSendTime = enter_time + (sendint - sendbrw);
10286 m_tdSendTimeDiff = duration();
10287 }
10288 #endif
10289 }
10290 HLOGC(qslog.Debug, log << "packData: Setting source address: " << m_SourceAddr.str());
10291 w_src_addr = m_SourceAddr;
10292 w_nexttime = m_tsNextSendTime;
10293
10294 return payload >= 0; // XXX shouldn't be > 0 ? == 0 is only when buffer range exceeded.
10295 }
10296
10297 bool srt::CUDT::packUniqueData(CPacket& w_packet)
10298 {
10299 int current_sequence_number; // reflexing variable
10300 int kflg;
10301 time_point tsOrigin;
10302 int pld_size;
10303
10304 {
10305 ScopedLock lkrack (m_RecvAckLock);
10306 // Check the congestion/flow window limit
10307 const int cwnd = std::min<int>(m_iFlowWindowSize, m_iCongestionWindow);
10308 const int flightspan = getFlightSpan();
10309 if (cwnd <= flightspan)
10310 {
10311 HLOGC(qslog.Debug,
10312 log << CONID() << "packUniqueData: CONGESTED: cwnd=min(" << m_iFlowWindowSize << "," << m_iCongestionWindow
10313 << ")=" << cwnd << " seqlen=(" << m_iSndLastAck << "-" << m_iSndCurrSeqNo << ")=" << flightspan);
10314 return false;
10315 }
10316
10317 // XXX Here it's needed to set kflg to msgno_bitset in the block stored in the
10318 // send buffer. This should be somehow avoided, the crypto flags should be set
10319 // together with encrypting, and the packet should be sent as is, when rexmitting.
10320 // It would be nice to research as to whether CSndBuffer::Block::m_iMsgNoBitset field
10321 // isn't a useless redundant state copy. If it is, then taking the flags here can be removed.
10322 kflg = m_pCryptoControl->getSndCryptoFlags();
10323 int pktskipseqno = 0;
10324 pld_size = m_pSndBuffer->readData((w_packet), (tsOrigin), kflg, (pktskipseqno));
10325 if (pktskipseqno)
10326 {
10327 // Some packets were skipped due to TTL expiry.
10328 m_iSndCurrSeqNo = CSeqNo::incseq(m_iSndCurrSeqNo, pktskipseqno);
10329 HLOGC(qslog.Debug, log << "packUniqueData: reading skipped " << pktskipseqno << " seq up to %" << m_iSndCurrSeqNo
10330 << " due to TTL expiry");
10331 }
10332
10333 if (pld_size == 0)
10334 {
10335 HLOGC(qslog.Debug, log << "packUniqueData: nothing extracted from the buffer");
10336 return false;
10337 }
10338
10339 // A CHANGE. The sequence number is currently added to the packet
10340 // when scheduling, not when extracting. This is a inter-migration form,
10341 // only override extraction sequence with scheduling sequence in group mode.
10342 m_iSndCurrSeqNo = CSeqNo::incseq(m_iSndCurrSeqNo);
10343 current_sequence_number = m_iSndCurrSeqNo;
10344 }
10345
10346 #if ENABLE_BONDING
10347 // Fortunately the group itself isn't being accessed.
10348 if (m_parent->m_GroupOf)
10349 {
10350 const int packetspan = CSeqNo::seqoff(current_sequence_number, w_packet.seqno());
10351 if (packetspan > 0)
10352 {
10353 // After increasing by 1, but being previously set as ISN-1, this should be == ISN,
10354 // if this is the very first packet to send.
10355 if (current_sequence_number == m_iISN)
10356 {
10357 // This is the very first packet to be sent; so there's nothing in
10358 // the sending buffer yet, and therefore we are in a situation as just
10359 // after connection. No packets in the buffer, no packets are sent,
10360 // no ACK to be awaited. We can screw up all the variables that are
10361 // initialized from ISN just after connection.
10362 LOGC(qslog.Note,
10363 log << CONID() << "packUniqueData: Fixing EXTRACTION sequence " << current_sequence_number
10364 << " from SCHEDULING sequence " << w_packet.seqno() << " for the first packet: DIFF="
10365 << packetspan << " STAMP=" << BufferStamp(w_packet.m_pcData, w_packet.getLength()));
10366 }
10367 else
10368 {
10369 // There will be a serious data discrepancy between the agent and the peer.
10370 LOGC(qslog.Error,
10371 log << CONID() << "IPE: packUniqueData: Fixing EXTRACTION sequence " << current_sequence_number
10372 << " from SCHEDULING sequence " << w_packet.seqno() << " in the middle of transition: DIFF="
10373 << packetspan << " STAMP=" << BufferStamp(w_packet.m_pcData, w_packet.getLength()));
10374 }
10375
10376 // Additionally send the drop request to the peer so that it
10377 // won't stupidly request the packets to be retransmitted.
10378 // Don't do it if the difference isn't positive or exceeds the threshold.
10379 int32_t seqpair[2];
10380 seqpair[0] = current_sequence_number;
10381 seqpair[1] = CSeqNo::decseq(w_packet.seqno());
10382 const int32_t no_msgno = 0;
10383 LOGC(qslog.Debug,
10384 log << CONID() << "packUniqueData: Sending DROPREQ: SEQ: " << seqpair[0] << " - " << seqpair[1] << " ("
10385 << packetspan << " packets)");
10386 sendCtrl(UMSG_DROPREQ, &no_msgno, seqpair, sizeof(seqpair));
10387 // In case when this message is lost, the peer will still get the
10388 // UMSG_DROPREQ message when the agent realizes that the requested
10389 // packet are not present in the buffer (preadte the send buffer).
10390
10391 // Override extraction sequence with scheduling sequence.
10392 ScopedLock ackguard(m_RecvAckLock);
10393 m_iSndCurrSeqNo = w_packet.seqno();
10394 m_iSndLastAck = w_packet.seqno();
10395 m_iSndLastDataAck = w_packet.seqno();
10396 m_iSndLastFullAck = w_packet.seqno();
10397 m_iSndLastAck2 = w_packet.seqno();
10398 }
10399 else if (packetspan < 0)
10400 {
10401 LOGC(qslog.Error,
10402 log << CONID() << "IPE: packData: SCHEDULING sequence " << w_packet.seqno()
10403 << " is behind of EXTRACTION sequence " << current_sequence_number << ", dropping this packet: DIFF="
10404 << packetspan << " STAMP=" << BufferStamp(w_packet.m_pcData, w_packet.getLength()));
10405 // XXX: Probably also change the socket state to broken?
10406 return false;
10407 }
10408 }
10409 else
10410 #endif
10411 {
10412 HLOGC(qslog.Debug,
10413 log << CONID() << "packUniqueData: Applying EXTRACTION sequence " << current_sequence_number
10414 << " over SCHEDULING sequence " << w_packet.seqno() << " for socket not in group:"
10415 << " DIFF=" << CSeqNo::seqcmp(current_sequence_number, w_packet.seqno())
10416 << " STAMP=" << BufferStamp(w_packet.m_pcData, w_packet.getLength()));
10417 // Do this always when not in a group.
10418 w_packet.set_seqno(current_sequence_number);
10419 }
10420
10421 // Set missing fields before encrypting the packet, because those fields might be used for encryption.
10422 w_packet.set_id(m_PeerID); // Destination SRT Socket ID
10423 setDataPacketTS(w_packet, tsOrigin);
10424
10425 if (kflg != EK_NOENC)
10426 {
10427 // Note that the packet header must have a valid seqno set, as it is used as a counter for encryption.
10428 // Other fields of the data packet header (e.g. timestamp, destination socket ID) are not used for the counter.
10429 // Cypher may change packet length!
10430 if (m_pCryptoControl->encrypt((w_packet)) != ENCS_CLEAR)
10431 {
10432 // Encryption failed
10433 //>>Add stats for crypto failure
10434 LOGC(qslog.Warn, log << CONID() << "ENCRYPT FAILED - packet won't be sent, size=" << pld_size);
10435 return false;
10436 }
10437
10438 checkSndKMRefresh();
10439 }
10440
10441 #if SRT_DEBUG_TRACE_SND
10442 g_snd_logger.state.iPktSeqno = w_packet.seqno();
10443 g_snd_logger.state.isRetransmitted = w_packet.getRexmitFlag();
10444 g_snd_logger.trace();
10445 #endif
10446
10447 #ifdef ENABLE_RATE_MEASUREMENT
10448 HLOGC(bslog.Debug, log << "RATE-MEASUREMENT: REGULAR, pkt-size=" << w_packet.getLength());
10449 m_SndRegularMeasurement.dataUpdate(1, w_packet.getLength());
10450 #endif
10451
10452 return true;
10453 }
10454
10455 // This is a close request, but called from the
10456 void srt::CUDT::processClose()
10457 {
10458 sendCtrl(UMSG_SHUTDOWN);
10459
10460 m_bShutdown = true;
10461 m_bClosing = true;
10462 m_bBroken = true;
10463 m_iBrokenCounter = 60;
10464
10465 HLOGP(smlog.Debug, "processClose: (closing=true) sent message and set flags");
10466
10467 if (m_bTsbPd)
10468 {
10469 HLOGP(smlog.Debug, "processClose: lock-and-signal TSBPD");
10470 CSync::lock_notify_one(m_RcvTsbPdCond, m_RecvLock);
10471 }
10472
10473 // Signal the sender and recver if they are waiting for data.
10474 releaseSynch();
10475 // Unblock any call so they learn the connection_broken error
10476 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_ERR, true);
10477
10478 HLOGP(smlog.Debug, "processClose: triggering timer event to spread the bad news");
10479 CGlobEvent::triggerEvent();
10480 }
10481
10482 void srt::CUDT::sendLossReport(const std::vector<std::pair<int32_t, int32_t> > &loss_seqs)
10483 {
10484 vector<int32_t> seqbuffer;
10485 seqbuffer.reserve(2 * loss_seqs.size()); // pessimistic
10486 for (loss_seqs_t::const_iterator i = loss_seqs.begin(); i != loss_seqs.end(); ++i)
10487 {
10488 if (i->first == i->second)
10489 {
10490 seqbuffer.push_back(i->first);
10491 HLOGC(qrlog.Debug, log << "lost packet " << i->first << ": sending LOSSREPORT");
10492 }
10493 else
10494 {
10495 seqbuffer.push_back(i->first | LOSSDATA_SEQNO_RANGE_FIRST);
10496 seqbuffer.push_back(i->second);
10497 HLOGC(qrlog.Debug, log << "lost packets " << i->first << "-" << i->second
10498 << " (" << (1 + CSeqNo::seqcmp(i->second, i->first)) << " packets): sending LOSSREPORT");
10499 }
10500 }
10501
10502 if (!seqbuffer.empty())
10503 {
10504 sendCtrl(UMSG_LOSSREPORT, NULL, &seqbuffer[0], (int) seqbuffer.size());
10505 }
10506 }
10507
10508
10509 bool srt::CUDT::overrideSndSeqNo(int32_t seq)
10510 {
10511 // This function is intended to be called from the socket
10512 // group management functions to synchronize the sequence in
10513 // all sockes in the bonding group. THIS sequence given
10514 // here is the sequence TO BE STAMPED AT THE EXACTLY NEXT
10515 // sent payload. Therefore, screw up the ISN to exactly this
10516 // value, and the send sequence to the value one less - because
10517 // the m_iSndCurrSeqNo is increased by one immediately before
10518 // stamping it to the packet.
10519
10520 // This function can only be called:
10521 // - from the operation on an idle socket in the socket group
10522 // - IMMEDIATELY after connection established and BEFORE the first payload
10523 // - The corresponding socket at the peer side must be also
10524 // in this idle state!
10525
10526 ScopedLock cg (m_RecvAckLock);
10527
10528 // Both the scheduling and sending sequences should be fixed.
10529 // The new sequence normally should jump over several sequence numbers
10530 // towards what is currently in m_iSndCurrSeqNo.
10531 // Therefore it's not allowed that:
10532 // - the jump go backward: backward packets should be already there
10533 // - the jump go forward by a value larger than half the period: DISCREPANCY.
10534 const int diff = CSeqNo(seq) - CSeqNo(m_iSndCurrSeqNo);
10535 if (diff < 0 || diff > CSeqNo::m_iSeqNoTH)
10536 {
10537 LOGC(gslog.Error, log << CONID() << "IPE: Overriding with seq %" << seq << " DISCREPANCY against current %"
10538 << m_iSndCurrSeqNo << " and next sched %" << m_iSndNextSeqNo << " - diff=" << diff);
10539 return false;
10540 }
10541
10542 //
10543 // The peer will have to do the same, as a reaction on perceived
10544 // packet loss. When it recognizes that this initial screwing up
10545 // has happened, it should simply ignore the loss and go on.
10546 // ISN isn't being changed here - it doesn't make much sense now.
10547
10548 setInitialSndSeq(seq);
10549
10550 // m_iSndCurrSeqNo will be most likely lower than m_iSndNextSeqNo because
10551 // the latter is ahead with the number of packets already scheduled, but
10552 // not yet sent.
10553
10554 HLOGC(gslog.Debug,
10555 log << CONID() << "overrideSndSeqNo: sched-seq=" << m_iSndNextSeqNo << " send-seq=" << m_iSndCurrSeqNo
10556 << " (unchanged)");
10557 return true;
10558 }
10559
10560 int srt::CUDT::checkLazySpawnTsbPdThread()
10561 {
10562 const bool need_tsbpd = m_bTsbPd || m_bGroupTsbPd;
10563 if (!need_tsbpd)
10564 return 0;
10565
10566 ScopedLock lock(m_RcvTsbPdStartupLock);
10567 if (!m_RcvTsbPdThread.joinable())
10568 {
10569 if (m_bClosing) // Check m_bClosing to protect join() in CUDT::releaseSync().
10570 return -1;
10571
10572 HLOGP(qrlog.Debug, "Spawning Socket TSBPD thread");
10573 #if ENABLE_HEAVY_LOGGING
10574 std::ostringstream tns1, tns2;
10575 // Take the last 2 ciphers from the socket ID.
10576 tns1 << setfill('0') << setw(2) << m_SocketID;
10577 std::string s = tns1.str();
10578 tns2 << "SRT:TsbPd:@" << s.substr(s.size()-2, 2);
10579 const string thname = tns2.str();
10580 #else
10581 const string thname = "SRT:TsbPd";
10582 #endif
10583 if (!StartThread(m_RcvTsbPdThread, CUDT::tsbpd, this, thname))
10584 return -1;
10585 }
10586
10587 return 0;
10588 }
10589
10590 CUDT::time_point srt::CUDT::getPktTsbPdTime(void*, const CPacket& packet)
10591 {
10592 return m_pRcvBuffer->getPktTsbPdTime(packet.getMsgTimeStamp());
10593 }
10594
10595 SRT_ATR_UNUSED static const char *const s_rexmitstat_str[] = {"ORIGINAL", "REXMITTED", "RXS-UNKNOWN"};
10596
10597 // [[using locked(m_RcvBufferLock)]]
10598 int srt::CUDT::handleSocketPacketReception(const vector<CUnit*>& incoming, bool& w_new_inserted, bool& w_was_sent_in_order, CUDT::loss_seqs_t& w_srt_loss_seqs)
10599 {
10600 bool excessive SRT_ATR_UNUSED = true; // stays true unless it was successfully added
10601
10602 w_new_inserted = false;
10603 const int32_t bufseq = m_pRcvBuffer->getStartSeqNo();
10604
10605 // Loop over all incoming packets that were filtered out.
10606 // In case when there is no filter, there's just one packet in 'incoming',
10607 // the one that came in the input of this function.
10608 for (vector<CUnit *>::const_iterator unitIt = incoming.begin(); unitIt != incoming.end(); ++unitIt)
10609 {
10610 CUnit * u = *unitIt;
10611 CPacket &rpkt = u->m_Packet;
10612 const int pktrexmitflag = m_bPeerRexmitFlag ? (rpkt.getRexmitFlag() ? 1 : 0) : 2;
10613 const bool retransmitted = pktrexmitflag == 1;
10614
10615 bool adding_successful = true;
10616
10617 const int32_t bufidx = CSeqNo::seqoff(bufseq, rpkt.seqno());
10618
10619 IF_HEAVY_LOGGING(const char *exc_type = "EXPECTED");
10620
10621 // bufidx < 0: the packet is in the past for the buffer
10622 // seqno <% m_iRcvLastAck : the sequence may be within the buffer,
10623 // but if so, it is in the acknowledged-but-not-retrieved area.
10624
10625 // NOTE: if we have a situation when there are any packets in the
10626 // acknowledged area, but they aren't retrieved, this area DOES NOT
10627 // contain any losses. So a packet in this area is at best a duplicate.
10628
10629 // In case when a loss would be abandoned (TLPKTDROP), there must at
10630 // some point happen to be an empty first cell in the buffer, followed
10631 // somewhere by a valid packet. If this state is achieved at some point,
10632 // the acknowledgement sequence should be equal to the beginning of the
10633 // buffer. Then, when TSBPD decides to drop these initial empty cells,
10634 // we'll have: (m_iRcvLastAck <% buffer->getStartSeqNo()) - and in this
10635 // case (bufidx < 0) condition will be satisfied also for this case.
10636 //
10637 // The only case when bufidx > 0, but packet seq is <% m_iRcvLastAck
10638 // is when the packet sequence is within the initial contiguous area,
10639 // which never contains losses, so discarding this packet does not
10640 // discard a loss coverage, even if this were past ACK.
10641
10642 if (bufidx < 0 || CSeqNo::seqcmp(rpkt.seqno(), m_iRcvLastAck) < 0)
10643 {
10644 time_point pts = getPktTsbPdTime(NULL, rpkt);
10645
10646 enterCS(m_StatsLock);
10647 const double bltime = (double) CountIIR<uint64_t>(
10648 uint64_t(m_stats.traceBelatedTime) * 1000,
10649 count_microseconds(steady_clock::now() - pts), 0.2);
10650
10651 m_stats.traceBelatedTime = bltime / 1000.0;
10652 m_stats.rcvr.recvdBelated.count(rpkt.getLength());
10653 leaveCS(m_StatsLock);
10654 HLOGC(qrlog.Debug,
10655 log << CONID() << "RECEIVED: %" << rpkt.seqno() << " bufidx=" << bufidx << " (BELATED/"
10656 << s_rexmitstat_str[pktrexmitflag] << ") with ACK %" << m_iRcvLastAck
10657 << " FLAGS: " << rpkt.MessageFlagStr());
10658 continue;
10659 }
10660
10661 if (bufidx >= int(m_pRcvBuffer->capacity()))
10662 {
10663 // This is already a sequence discrepancy. Probably there could be found
10664 // some way to make it continue reception by overriding the sequence and
10665 // make a kinda TLKPTDROP, but there has been found no reliable way to do this.
10666 if (m_bTsbPd && m_bTLPktDrop && m_pRcvBuffer->empty())
10667 {
10668 // Only in live mode. In File mode this shall not be possible
10669 // because the sender should stop sending in this situation.
10670 // In Live mode this means that there is a gap between the
10671 // lowest sequence in the empty buffer and the incoming sequence
10672 // that exceeds the buffer size. Receiving data in this situation
10673 // is no longer possible and this is a point of no return.
10674
10675 LOGC(qrlog.Error, log << CONID() <<
10676 "SEQUENCE DISCREPANCY. BREAKING CONNECTION."
10677 " %" << rpkt.seqno()
10678 << " buffer=(%" << bufseq
10679 << ":%" << m_iRcvCurrSeqNo // -1 = size to last index
10680 << "+%" << CSeqNo::incseq(bufseq, int(m_pRcvBuffer->capacity()) - 1)
10681 << "), " << (m_pRcvBuffer->capacity() - bufidx + 1)
10682 << " past max. Reception no longer possible. REQUESTING TO CLOSE.");
10683
10684 return -2;
10685 }
10686 else
10687 {
10688 LOGC(qrlog.Warn, log << CONID() << "No room to store incoming packet seqno " << rpkt.seqno()
10689 << ", insert offset " << bufidx << ". "
10690 << m_pRcvBuffer->strFullnessState(m_iRcvLastAck, steady_clock::now())
10691 );
10692
10693 return -1;
10694 }
10695 }
10696
10697 const int buffer_add_result = m_pRcvBuffer->insert(u);
10698 if (buffer_add_result < 0)
10699 {
10700 // The insert() result is -1 if at the position evaluated from this packet's
10701 // sequence number there already is a packet.
10702 // So this packet is "redundant".
10703 IF_HEAVY_LOGGING(exc_type = "UNACKED");
10704 adding_successful = false;
10705 }
10706 else
10707 {
10708 w_new_inserted = true;
10709
10710 IF_HEAVY_LOGGING(exc_type = "ACCEPTED");
10711 excessive = false;
10712 if (u->m_Packet.getMsgCryptoFlags() != EK_NOENC)
10713 {
10714 // TODO: reset and restore the timestamp if TSBPD is disabled.
10715 // Reset retransmission flag (must be excluded from GCM auth tag).
10716 u->m_Packet.setRexmitFlag(false);
10717 const EncryptionStatus rc = m_pCryptoControl ? m_pCryptoControl->decrypt((u->m_Packet)) : ENCS_NOTSUP;
10718 u->m_Packet.setRexmitFlag(retransmitted); // Recover the flag.
10719
10720 if (rc != ENCS_CLEAR)
10721 {
10722 adding_successful = false;
10723 IF_HEAVY_LOGGING(exc_type = "UNDECRYPTED");
10724
10725 // If TSBPD is disabled, then SRT either operates in buffer mode, of in message API without a restriction
10726 // of a single message packet. In that case just dropping a packet is not enough.
10727 // In message mode the whole message has to be dropped.
10728 // However, when decryption fails the message number in the packet cannot be trusted.
10729 // The packet has to be removed from the RCV buffer based on that pkt sequence number,
10730 // and the sequence number itself must go into the RCV loss list.
10731 // See issue ##2626.
10732 SRT_ASSERT(m_bTsbPd);
10733
10734 // Drop the packet from the receiver buffer.
10735 // The packet was added to the buffer based on the sequence number, therefore sequence number should be used to drop it from the buffer.
10736 // A drawback is that it would prevent a valid packet with the same sequence number, if it happens to arrive later, to end up in the buffer.
10737 const int iDropCnt = m_pRcvBuffer->dropMessage(u->m_Packet.getSeqNo(), u->m_Packet.getSeqNo(), SRT_MSGNO_NONE, CRcvBuffer::DROP_EXISTING);
10738
10739 const steady_clock::time_point tnow = steady_clock::now();
10740 ScopedLock lg(m_StatsLock);
10741 m_stats.rcvr.dropped.count(stats::BytesPackets(iDropCnt * rpkt.getLength(), iDropCnt));
10742 m_stats.rcvr.undecrypted.count(stats::BytesPackets(rpkt.getLength(), 1));
10743 string why;
10744 if (frequentLogAllowed(FREQLOGFA_ENCRYPTION_FAILURE, tnow, (why)))
10745 {
10746 LOGC(qrlog.Warn, log << CONID() << "Decryption failed (seqno %" << u->m_Packet.getSeqNo() << "), dropped "
10747 << iDropCnt << ". pktRcvUndecryptTotal=" << m_stats.rcvr.undecrypted.total.count() << "." << why);
10748 }
10749 #if SRT_ENABLE_FREQUENT_LOG_TRACE
10750 else
10751 {
10752
10753 LOGC(qrlog.Warn, log << "SUPPRESSED: Decryption failed LOG: " << why);
10754 }
10755 #endif
10756 }
10757 }
10758 else if (m_pCryptoControl && m_pCryptoControl->m_RcvKmState == SRT_KM_S_SECURED)
10759 {
10760 // Unencrypted packets are not allowed.
10761 const int iDropCnt = m_pRcvBuffer->dropMessage(u->m_Packet.getSeqNo(), u->m_Packet.getSeqNo(), SRT_MSGNO_NONE, CRcvBuffer::DROP_EXISTING);
10762
10763 const steady_clock::time_point tnow = steady_clock::now();
10764 ScopedLock lg(m_StatsLock);
10765 m_stats.rcvr.dropped.count(stats::BytesPackets(iDropCnt* rpkt.getLength(), iDropCnt));
10766 m_stats.rcvr.undecrypted.count(stats::BytesPackets(rpkt.getLength(), 1));
10767 string why;
10768 if (frequentLogAllowed(FREQLOGFA_ENCRYPTION_FAILURE, tnow, (why)))
10769 {
10770 LOGC(qrlog.Warn, log << CONID() << "Packet not encrypted (seqno %" << u->m_Packet.getSeqNo() << "), dropped "
10771 << iDropCnt << ". pktRcvUndecryptTotal=" << m_stats.rcvr.undecrypted.total.count() << ".");
10772 }
10773 }
10774 }
10775
10776 if (adding_successful)
10777 {
10778 ScopedLock statslock(m_StatsLock);
10779 m_stats.rcvr.recvdUnique.count(u->m_Packet.getLength());
10780 }
10781
10782 #if ENABLE_HEAVY_LOGGING
10783 std::ostringstream expectspec;
10784 if (excessive)
10785 expectspec << "EXCESSIVE(" << exc_type << ")";
10786 else
10787 expectspec << "ACCEPTED";
10788
10789 std::ostringstream bufinfo;
10790
10791 if (m_pRcvBuffer)
10792 {
10793 // XXX Fix this when the end of contiguous region detection is added.
10794 const int ackidx = std::max(0, CSeqNo::seqoff(m_pRcvBuffer->getStartSeqNo(), m_iRcvLastAck));
10795
10796 bufinfo << " BUF.s=" << m_pRcvBuffer->capacity()
10797 << " avail=" << (int(m_pRcvBuffer->capacity()) - ackidx)
10798 << " buffer=(%" << bufseq
10799 << ":%" << m_iRcvCurrSeqNo // -1 = size to last index
10800 << "+%" << CSeqNo::incseq(bufseq, int(m_pRcvBuffer->capacity()) - 1)
10801 << ")";
10802 }
10803
10804 // Empty buffer info in case of groupwise receiver.
10805 // There's no way to obtain this information here.
10806
10807 LOGC(qrlog.Debug, log << CONID() << "RECEIVED: %" << rpkt.seqno()
10808 << bufinfo.str()
10809 << " RSL=" << expectspec.str()
10810 << " SN=" << s_rexmitstat_str[pktrexmitflag]
10811 << " FLAGS: "
10812 << rpkt.MessageFlagStr());
10813 #endif
10814
10815 // Decryption should have made the crypto flags EK_NOENC.
10816 // Otherwise it's an error.
10817 if (adding_successful)
10818 {
10819 HLOGC(qrlog.Debug,
10820 log << CONID()
10821 << "CONTIGUITY CHECK: sequence distance: " << CSeqNo::seqoff(m_iRcvCurrSeqNo, rpkt.seqno()));
10822
10823 if (CSeqNo::seqcmp(rpkt.seqno(), CSeqNo::incseq(m_iRcvCurrSeqNo)) > 0) // Loss detection.
10824 {
10825 int32_t seqlo = CSeqNo::incseq(m_iRcvCurrSeqNo);
10826 int32_t seqhi = CSeqNo::decseq(rpkt.seqno());
10827 w_srt_loss_seqs.push_back(make_pair(seqlo, seqhi));
10828 HLOGC(qrlog.Debug, log << "pkt/LOSS DETECTED: %" << seqlo << " - %" << seqhi);
10829 }
10830 }
10831
10832 // Update the current largest sequence number that has been received.
10833 // Or it is a retransmitted packet, remove it from receiver loss list.
10834 if (CSeqNo::seqcmp(rpkt.seqno(), m_iRcvCurrSeqNo) > 0)
10835 {
10836 m_iRcvCurrSeqNo = rpkt.seqno(); // Latest possible received
10837 }
10838 else
10839 {
10840 unlose(rpkt); // was BELATED or RETRANSMITTED
10841 w_was_sent_in_order &= 0 != pktrexmitflag;
10842 }
10843 }
10844
10845 return 0;
10846 }
10847
10848 int srt::CUDT::processData(CUnit* in_unit)
10849 {
10850 if (m_bClosing)
10851 return -1;
10852
10853 CPacket &packet = in_unit->m_Packet;
10854
10855 // Just heard from the peer, reset the expiration count.
10856 m_iEXPCount = 1;
10857 m_tsLastRspTime.store(steady_clock::now());
10858
10859
10860 // We are receiving data, start tsbpd thread if TsbPd is enabled
10861 if (-1 == checkLazySpawnTsbPdThread())
10862 {
10863 return -1;
10864 }
10865
10866 const int pktrexmitflag = m_bPeerRexmitFlag ? (packet.getRexmitFlag() ? 1 : 0) : 2;
10867 const bool retransmitted = pktrexmitflag == 1;
10868 #if ENABLE_HEAVY_LOGGING
10869 string rexmit_reason;
10870 #endif
10871
10872 if (retransmitted)
10873 {
10874 // This packet was retransmitted
10875 enterCS(m_StatsLock);
10876 m_stats.rcvr.recvdRetrans.count(packet.getLength());
10877 leaveCS(m_StatsLock);
10878
10879 #if ENABLE_HEAVY_LOGGING
10880 // Check if packet was retransmitted on request or on ack timeout
10881 // Search the sequence in the loss record.
10882 rexmit_reason = " by ";
10883 ScopedLock lock(m_RcvLossLock);
10884 if (!m_pRcvLossList->find(packet.seqno(), packet.seqno()))
10885 rexmit_reason += "BLIND";
10886 else
10887 rexmit_reason += "NAKREPORT";
10888 #endif
10889 }
10890
10891 #if ENABLE_HEAVY_LOGGING
10892 {
10893 steady_clock::duration tsbpddelay = milliseconds_from(m_iTsbPdDelay_ms); // (value passed to CRcvBuffer::setRcvTsbPdMode)
10894
10895 // It's easier to remove the latency factor from this value than to add a function
10896 // that exposes the details basing on which this value is calculated.
10897 steady_clock::time_point pts = m_pRcvBuffer->getPktTsbPdTime(packet.getMsgTimeStamp());
10898 steady_clock::time_point ets = pts - tsbpddelay;
10899
10900 HLOGC(qrlog.Debug, log << CONID() << "processData: RECEIVED DATA: size=" << packet.getLength()
10901 << " seq=" << packet.getSeqNo()
10902 // XXX FIX IT. OTS should represent the original sending time, but it's relative.
10903 //<< " OTS=" << FormatTime(packet.getMsgTimeStamp())
10904 << " ETS=" << FormatTime(ets)
10905 << " PTS=" << FormatTime(pts));
10906 }
10907 #endif
10908
10909 updateCC(TEV_RECEIVE, EventVariant(&packet));
10910 ++m_iPktCount;
10911
10912 const int pktsz = (int) packet.getLength();
10913 // Update time information
10914 // XXX Note that this adds the byte size of a packet
10915 // of which we don't yet know as to whether this has
10916 // carried out some useful data or some excessive data
10917 // that will be later discarded.
10918 // FIXME: before adding this on the rcv time window,
10919 // make sure that this packet isn't going to be
10920 // effectively discarded, as repeated retransmission,
10921 // for example, burdens the link, but doesn't better the speed.
10922 m_RcvTimeWindow.onPktArrival(pktsz);
10923
10924 // Probe the packet pair if needed.
10925 // Conditions and any extra data required for the packet
10926 // this function will extract and test as needed.
10927
10928 const bool unordered = CSeqNo::seqcmp(packet.seqno(), m_iRcvCurrSeqNo) <= 0;
10929
10930 // Retransmitted and unordered packets do not provide expected measurement.
10931 // We expect the 16th and 17th packet to be sent regularly,
10932 // otherwise measurement must be rejected.
10933 m_RcvTimeWindow.probeArrival(packet, unordered || retransmitted);
10934
10935 enterCS(m_StatsLock);
10936 m_stats.rcvr.recvd.count(pktsz);
10937 leaveCS(m_StatsLock);
10938
10939 loss_seqs_t filter_loss_seqs;
10940 loss_seqs_t srt_loss_seqs;
10941 vector<CUnit *> incoming;
10942 bool was_sent_in_order = true;
10943
10944 // If the peer doesn't understand REXMIT flag, send rexmit request
10945 // always immediately.
10946 int initial_loss_ttl = 0;
10947 if (m_bPeerRexmitFlag)
10948 initial_loss_ttl = m_iReorderTolerance;
10949
10950 // Track packet loss in statistics early, because a packet filter (e.g. FEC) might recover it later on,
10951 // supply the missing packet(s), and the loss will no longer be visible for the code that follows.
10952 if (packet.getMsgSeq(m_bPeerRexmitFlag) != SRT_MSGNO_CONTROL) // disregard filter-control packets, their seq may mean nothing
10953 {
10954 const int diff = CSeqNo::seqoff(m_iRcvCurrPhySeqNo, packet.seqno());
10955 // Difference between these two sequence numbers is expected to be:
10956 // 0 - duplicated last packet (theory only)
10957 // 1 - subsequent packet (alright)
10958 // <0 - belated or recovered packet
10959 // >1 - jump over a packet loss (loss = seqdiff-1)
10960 if (diff > 1)
10961 {
10962 const int loss = diff - 1; // loss is all that is above diff == 1
10963
10964 ScopedLock lg(m_StatsLock);
10965 const uint64_t avgpayloadsz = m_pRcvBuffer->getRcvAvgPayloadSize();
10966 m_stats.rcvr.lost.count(stats::BytesPackets(loss * avgpayloadsz, (uint32_t) loss));
10967
10968 HLOGC(qrlog.Debug,
10969 log << CONID() << "LOSS STATS: n=" << loss << " SEQ: [" << CSeqNo::incseq(m_iRcvCurrPhySeqNo) << " "
10970 << CSeqNo::decseq(packet.seqno()) << "]");
10971 }
10972
10973 if (diff > 0)
10974 {
10975 // Record if it was further than latest
10976 m_iRcvCurrPhySeqNo = packet.seqno();
10977 }
10978 }
10979
10980 // [[using locked()]]; // (NOTHING locked)
10981
10982 #if ENABLE_BONDING
10983 // Switch to RUNNING even if there was a discrepancy, unless
10984 // it was long way forward.
10985 // XXX Important: This code is in the dead function defaultPacketArrival
10986 // but normally it should be called here regardless if the packet was
10987 // accepted or rejected because if it was belated it may result in a
10988 // "runaway train" problem as the IDLE links are being updated the base
10989 // reception sequence pointer stating that this link is not receiving.
10990 if (m_parent->m_GroupOf)
10991 {
10992 ExclusiveLock protect_group_existence (uglobal().m_GlobControlLock);
10993 groups::SocketData* gi = m_parent->m_GroupMemberData;
10994
10995 // This check is needed as after getting the lock the socket
10996 // could be potentially removed. It is however granted that as long
10997 // as gi is non-NULL iterator, the group does exist and it does contain
10998 // this socket as member (that is, 'gi' cannot be a dangling pointer).
10999 if (gi != NULL)
11000 {
11001 if (gi->rcvstate < SRT_GST_RUNNING) // PENDING or IDLE, though PENDING is unlikely
11002 {
11003 HLOGC(qrlog.Debug,
11004 log << CONID() << "processData: IN-GROUP rcv state transition " << srt_log_grp_state[gi->rcvstate]
11005 << " -> RUNNING.");
11006 gi->rcvstate = SRT_GST_RUNNING;
11007 }
11008 else
11009 {
11010 HLOGC(qrlog.Debug,
11011 log << CONID() << "processData: IN-GROUP rcv state transition NOT DONE - state:"
11012 << srt_log_grp_state[gi->rcvstate]);
11013 }
11014 }
11015 }
11016 #endif
11017
11018 bool new_inserted = false;
11019
11020 if (m_PacketFilter)
11021 {
11022 // Stuff this data into the filter
11023 m_PacketFilter.receive(in_unit, (incoming), (filter_loss_seqs));
11024 HLOGC(qrlog.Debug,
11025 log << CONID() << "(FILTER) fed data, received " << incoming.size() << " pkts, " << Printable(filter_loss_seqs)
11026 << " loss to report, "
11027 << (m_PktFilterRexmitLevel == SRT_ARQ_ALWAYS ? "FIND & REPORT LOSSES YOURSELF"
11028 : "REPORT ONLY THOSE"));
11029 }
11030 else
11031 {
11032 // Stuff in just one packet that has come in.
11033 incoming.push_back(in_unit);
11034 }
11035
11036 {
11037 // Start of offset protected section
11038 // Prevent TsbPd thread from modifying Ack position while adding data
11039 // offset from RcvLastAck in RcvBuffer must remain valid between seqoff() and addData()
11040 UniqueLock recvbuf_acklock(m_RcvBufferLock);
11041 // Needed for possibly check for needsQuickACK.
11042 const bool incoming_belated = (CSeqNo::seqcmp(in_unit->m_Packet.seqno(), m_pRcvBuffer->getStartSeqNo()) < 0);
11043
11044 const int res = handleSocketPacketReception(incoming,
11045 (new_inserted),
11046 (was_sent_in_order),
11047 (srt_loss_seqs));
11048
11049 if (res == -2)
11050 {
11051 // This is a scoped lock with AckLock, but for the moment
11052 // when processClose() is called this lock must be taken out,
11053 // otherwise this will cause a deadlock. We don't need this
11054 // lock anymore, and at 'return' it will be unlocked anyway.
11055 recvbuf_acklock.unlock();
11056 processClose();
11057
11058 return -1;
11059 }
11060
11061 if (res == -1)
11062 {
11063 return -1;
11064 }
11065
11066 if (!srt_loss_seqs.empty())
11067 {
11068 ScopedLock lock(m_RcvLossLock);
11069
11070 HLOGC(qrlog.Debug,
11071 log << CONID() << "processData: RECORDING LOSS: " << Printable(srt_loss_seqs)
11072 << " tolerance=" << initial_loss_ttl);
11073
11074 for (loss_seqs_t::iterator i = srt_loss_seqs.begin(); i != srt_loss_seqs.end(); ++i)
11075 {
11076 m_pRcvLossList->insert(i->first, i->second);
11077 if (initial_loss_ttl)
11078 {
11079 // The LOSSREPORT will be sent after initial_loss_ttl.
11080 m_FreshLoss.push_back(CRcvFreshLoss(i->first, i->second, initial_loss_ttl));
11081 }
11082 }
11083 }
11084
11085 // This is moved earlier after introducing filter because it shouldn't
11086 // be executed in case when the packet was rejected by the receiver buffer.
11087 // However now the 'excessive' condition may be true also in case when
11088 // a truly non-excessive packet has been received, just it has been temporarily
11089 // stored for better times by the filter module. This way 'excessive' is also true,
11090 // although the old condition that a packet with a newer sequence number has arrived
11091 // or arrived out of order may still be satisfied.
11092 if (!incoming_belated && was_sent_in_order)
11093 {
11094 // Basing on some special case in the packet, it might be required
11095 // to enforce sending ACK immediately (earlier than normally after
11096 // a given period).
11097 if (m_CongCtl->needsQuickACK(packet))
11098 {
11099 m_tsNextACKTime.store(steady_clock::now());
11100 }
11101 }
11102
11103 if (!new_inserted)
11104 {
11105 return -1;
11106 }
11107 } // End of recvbuf_acklock
11108
11109 if (m_bClosing)
11110 {
11111 // RcvQueue worker thread can call processData while closing (or close while processData)
11112 // This race condition exists in the UDT design but the protection against TsbPd thread
11113 // (with AckLock) and decryption enlarged the probability window.
11114 // Application can crash deep in decrypt stack since crypto context is deleted in close.
11115 // RcvQueue worker thread will not necessarily be deleted with this connection as it can be
11116 // used by others (socket multiplexer).
11117 return -1;
11118 }
11119
11120 if (incoming.empty())
11121 {
11122 // Treat as excessive. This is when a filter cumulates packets
11123 // until the loss is rebuilt, or eats up a filter control packet
11124 return -1;
11125 }
11126
11127 if (!srt_loss_seqs.empty())
11128 {
11129 const bool report_recorded_loss = !m_PacketFilter || m_PktFilterRexmitLevel == SRT_ARQ_ALWAYS;
11130 if (!initial_loss_ttl && report_recorded_loss)
11131 {
11132 HLOGC(qrlog.Debug, log << CONID() << "WILL REPORT LOSSES (SRT): " << Printable(srt_loss_seqs));
11133 sendLossReport(srt_loss_seqs);
11134 }
11135
11136 if (m_bTsbPd)
11137 {
11138 HLOGC(qrlog.Debug, log << CONID() << "loss: signaling TSBPD cond");
11139 CSync::lock_notify_one(m_RcvTsbPdCond, m_RecvLock);
11140 }
11141 else
11142 {
11143 HLOGC(qrlog.Debug, log << CONID() << "loss: socket is not TSBPD, not signaling");
11144 }
11145 }
11146
11147 // Separately report loss records of those reported by a filter.
11148 // ALWAYS report whatever has been reported back by a filter. Note that
11149 // the filter never reports anything when rexmit fallback level is ALWAYS or NEVER.
11150 // With ALWAYS only those are reported that were recorded here by SRT.
11151 // With NEVER, nothing is to be reported.
11152 if (!filter_loss_seqs.empty())
11153 {
11154 HLOGC(qrlog.Debug, log << CONID() << "WILL REPORT LOSSES (filter): " << Printable(filter_loss_seqs));
11155 sendLossReport(filter_loss_seqs);
11156
11157 if (m_bTsbPd)
11158 {
11159 HLOGC(qrlog.Debug, log << CONID() << "loss: signaling TSBPD cond");
11160 CSync::lock_notify_one(m_RcvTsbPdCond, m_RecvLock);
11161 }
11162 }
11163
11164 // Now review the list of FreshLoss to see if there's any "old enough" to send UMSG_LOSSREPORT to it.
11165
11166 // PERFORMANCE CONSIDERATIONS:
11167 // This list is quite inefficient as a data type and finding the candidate to send UMSG_LOSSREPORT
11168 // is linear time. On the other hand, there are some special cases that are important for performance:
11169 // - only the first (plus some following) could have had TTL drown to 0
11170 // - the only (little likely) possibility that the next-to-first record has TTL=0 is when there was
11171 // a loss range split (due to dropFromLossLists() of one sequence)
11172 // - first found record with TTL>0 means end of "ready to LOSSREPORT" records
11173 // So:
11174 // All you have to do is:
11175 // - start with first element and continue with next elements, as long as they have TTL=0
11176 // If so, send the loss report and remove this element.
11177 // - Since the first element that has TTL>0, iterate until the end of container and decrease TTL.
11178 //
11179 // This will be efficient because the loop to increment one field (without any condition check)
11180 // can be quite well optimized.
11181
11182 vector<int32_t> lossdata;
11183 {
11184 ScopedLock lg(m_RcvLossLock);
11185
11186 // XXX There was a mysterious crash around m_FreshLoss. When the initial_loss_ttl is 0
11187 // (that is, "belated loss report" feature is off), don't even touch m_FreshLoss.
11188 if (initial_loss_ttl && !m_FreshLoss.empty())
11189 {
11190 deque<CRcvFreshLoss>::iterator i = m_FreshLoss.begin();
11191
11192 // Phase 1: take while TTL <= 0.
11193 // There can be more than one record with the same TTL, if it has happened before
11194 // that there was an 'unlost' (@c dropFromLossLists) sequence that has split one detected loss
11195 // into two records.
11196 for (; i != m_FreshLoss.end() && i->ttl <= 0; ++i)
11197 {
11198 HLOGC(qrlog.Debug, log << "Packet seq " << i->seq[0] << "-" << i->seq[1]
11199 << " (" << (CSeqNo::seqoff(i->seq[0], i->seq[1]) + 1) << " packets) considered lost - sending LOSSREPORT");
11200 addLossRecord(lossdata, i->seq[0], i->seq[1]);
11201 }
11202
11203 // Remove elements that have been processed and prepared for lossreport.
11204 if (i != m_FreshLoss.begin())
11205 {
11206 m_FreshLoss.erase(m_FreshLoss.begin(), i);
11207 i = m_FreshLoss.begin();
11208 }
11209
11210 if (m_FreshLoss.empty())
11211 {
11212 HLOGP(qrlog.Debug, "NO MORE FRESH LOSS RECORDS.");
11213 }
11214 else
11215 {
11216 HLOGC(qrlog.Debug, log << "STILL " << m_FreshLoss.size() << " FRESH LOSS RECORDS, FIRST: "
11217 << i->seq[0] << "-" << i->seq[1]
11218 << " (" << (1 + CSeqNo::seqoff(i->seq[0], i->seq[1])) << ") TTL: " << i->ttl);
11219 }
11220
11221 // Phase 2: rest of the records should have TTL decreased.
11222 for (; i != m_FreshLoss.end(); ++i)
11223 --i->ttl;
11224 }
11225 }
11226 if (!lossdata.empty())
11227 {
11228 sendCtrl(UMSG_LOSSREPORT, NULL, &lossdata[0], (int) lossdata.size());
11229 }
11230
11231 // was_sent_in_order means either of:
11232 // - packet was sent in order (first if branch above)
11233 // - packet was sent as old, but was a retransmitted packet
11234
11235 if (m_bPeerRexmitFlag && was_sent_in_order)
11236 {
11237 ++m_iConsecOrderedDelivery;
11238 if (m_iConsecOrderedDelivery >= 50)
11239 {
11240 m_iConsecOrderedDelivery = 0;
11241 if (m_iReorderTolerance > 0)
11242 {
11243 m_iReorderTolerance--;
11244 enterCS(m_StatsLock);
11245 m_stats.traceReorderDistance--;
11246 leaveCS(m_StatsLock);
11247 HLOGC(qrlog.Debug, log << "ORDERED DELIVERY of 50 packets in a row - decreasing tolerance to "
11248 << m_iReorderTolerance);
11249 }
11250 }
11251 }
11252
11253 return 0;
11254 }
11255
11256 #if ENABLE_BONDING
11257
11258 // NOTE: this is updated from the value of m_iRcvLastAck,
11259 // which might be past the buffer and potentially cause setting
11260 // the value to the last received and re-requiring retransmission.
11261 // Worst case is that there could be a few packets to tear the transmission
11262 // even more (as there will be likely no time to recover them), but
11263 // if the transmission was already torn in the previously active link
11264 // this shouldn't be a problem that these packets won't be recovered
11265 // after activating the second link, although will be retried this way.
11266 void srt::CUDT::updateIdleLinkFrom(CUDT* source)
11267 {
11268 int bufseq;
11269 {
11270 ScopedLock lg (m_RcvBufferLock);
11271 bufseq = source->m_pRcvBuffer->getStartSeqNo();
11272 }
11273 ScopedLock lg (m_RecvLock);
11274
11275 if (!m_pRcvBuffer->empty())
11276 {
11277 HLOGC(grlog.Debug, log << "grp: NOT updating rcv-seq in @" << m_SocketID << ": receiver buffer not empty");
11278 return;
11279 }
11280
11281 int32_t new_last_rcv = source->m_iRcvLastAck;
11282
11283 if (CSeqNo::seqcmp(new_last_rcv, bufseq) < 0)
11284 {
11285 // Emergency check whether the last ACK was behind the
11286 // buffer. This may happen when TSBPD dropped empty cells.
11287 // This may cause that the newly activated link may derive
11288 // these empty cells which will never be recovered.
11289 new_last_rcv = bufseq;
11290 }
11291
11292 // if (new_last_rcv <=% m_iRcvCurrSeqNo)
11293 if (CSeqNo::seqcmp(new_last_rcv, m_iRcvCurrSeqNo) <= 0)
11294 {
11295 // Reject the change because that would shift the reception pointer backwards.
11296 HLOGC(grlog.Debug, log << "grp: NOT updating rcv-seq in @" << m_SocketID
11297 << ": backward setting rejected: %" << m_iRcvCurrSeqNo
11298 << " -> %" << new_last_rcv);
11299 return;
11300 }
11301
11302 HLOGC(grlog.Debug, log << "grp: updating rcv-seq in @" << m_SocketID
11303 << " from @" << source->m_SocketID << ": %" << new_last_rcv);
11304 setInitialRcvSeq(new_last_rcv);
11305 }
11306
11307 #endif
11308
11309 /// This function is called when a packet has arrived, which was behind the current
11310 /// received sequence - that is, belated or retransmitted. Try to remove the packet
11311 /// from both loss records: the general loss record and the fresh loss record.
11312 ///
11313 /// Additionally, check - if supported by the peer - whether the "latecoming" packet
11314 /// has been sent due to retransmission or due to reordering, by checking the rexmit
11315 /// support flag and rexmit flag itself. If this packet was surely ORIGINALLY SENT
11316 /// it means that the current network connection suffers of packet reordering. This
11317 /// way try to introduce a dynamic tolerance by calculating the difference between
11318 /// the current packet reception sequence and this packet's sequence. This value
11319 /// will be set to the tolerance value, which means that later packet retransmission
11320 /// will not be required immediately, but only after receiving N next packets that
11321 /// do not include the lacking packet.
11322 /// The tolerance is not increased infinitely - it's bordered by iMaxReorderTolerance.
11323 /// This value can be set in options - SRT_LOSSMAXTTL.
11324 void srt::CUDT::unlose(const CPacket &packet)
11325 {
11326 ScopedLock lg(m_RcvLossLock);
11327 int32_t sequence = packet.seqno();
11328 m_pRcvLossList->remove(sequence);
11329
11330 // Rest of this code concerns only the "belated lossreport" feature.
11331
11332 bool has_increased_tolerance = false;
11333 bool was_reordered = false;
11334
11335 if (m_bPeerRexmitFlag)
11336 {
11337 // If the peer understands the REXMIT flag, it means that the REXMIT flag is contained
11338 // in the PH_MSGNO field.
11339
11340 // The packet is considered coming originally (just possibly out of order), if REXMIT
11341 // flag is NOT set.
11342 was_reordered = !packet.getRexmitFlag();
11343 if (was_reordered)
11344 {
11345 HLOGC(qrlog.Debug, log << "received out-of-band packet %" << sequence);
11346
11347 const int seqdiff = abs(CSeqNo::seqcmp(m_iRcvCurrSeqNo, packet.seqno()));
11348 enterCS(m_StatsLock);
11349 m_stats.traceReorderDistance = max(seqdiff, m_stats.traceReorderDistance);
11350 leaveCS(m_StatsLock);
11351 if (seqdiff > m_iReorderTolerance)
11352 {
11353 const int new_tolerance = min(seqdiff, m_config.iMaxReorderTolerance);
11354 HLOGC(qrlog.Debug, log << "Belated by " << seqdiff << " seqs - Reorder tolerance "
11355 << (new_tolerance == m_iReorderTolerance ? "REMAINS with " : "increased to ") << new_tolerance);
11356 m_iReorderTolerance = new_tolerance;
11357 has_increased_tolerance =
11358 true; // Yes, even if reorder tolerance is already at maximum - this prevents decreasing tolerance.
11359 }
11360 }
11361 else
11362 {
11363 HLOGC(qrlog.Debug, log << CONID() << "received reXmitted packet seq=" << sequence);
11364 }
11365 }
11366 else
11367 {
11368 HLOGC(qrlog.Debug, log << "received reXmitted or belated packet seq " << sequence << " (distinction not supported by peer)");
11369 }
11370
11371 // Don't do anything if "belated loss report" feature is not used.
11372 // In that case the FreshLoss list isn't being filled in at all, the
11373 // loss report is sent directly.
11374 // Note that this condition blocks two things being done in this function:
11375 // - remove given sequence from the fresh loss record
11376 // (in this case it's empty anyway)
11377 // - decrease current reorder tolerance based on whether packets come in order
11378 // (current reorder tolerance is 0 anyway)
11379 if (m_bPeerRexmitFlag == 0 || m_iReorderTolerance == 0)
11380 return;
11381
11382 int had_ttl = 0;
11383 if (CRcvFreshLoss::removeOne((m_FreshLoss), sequence, (&had_ttl)))
11384 {
11385 HLOGC(qrlog.Debug, log << "sequence " << sequence << " removed from belated lossreport record");
11386 }
11387
11388 if (was_reordered)
11389 {
11390 m_iConsecOrderedDelivery = 0;
11391 if (has_increased_tolerance)
11392 {
11393 m_iConsecEarlyDelivery = 0; // reset counter
11394 }
11395 else if (had_ttl > 2)
11396 {
11397 ++m_iConsecEarlyDelivery; // otherwise, and if it arrived quite earlier, increase counter
11398 HLOGC(qrlog.Debug, log << "... arrived at TTL " << had_ttl << " case " << m_iConsecEarlyDelivery);
11399
11400 // After 10 consecutive
11401 if (m_iConsecEarlyDelivery >= 10)
11402 {
11403 m_iConsecEarlyDelivery = 0;
11404 if (m_iReorderTolerance > 0)
11405 {
11406 m_iReorderTolerance--;
11407 enterCS(m_StatsLock);
11408 m_stats.traceReorderDistance--;
11409 leaveCS(m_StatsLock);
11410 HLOGC(qrlog.Debug, log << "... reached " << m_iConsecEarlyDelivery
11411 << " times - decreasing tolerance to " << m_iReorderTolerance);
11412 }
11413 }
11414 }
11415 // If hasn't increased tolerance, but the packet appeared at TTL less than 2, do nothing.
11416 }
11417 }
11418
11419 void srt::CUDT::dropFromLossLists(int32_t from, int32_t to)
11420 {
11421 ScopedLock lg(m_RcvLossLock);
11422
11423 IF_HEAVY_LOGGING(bool autodetected = false);
11424 int32_t begin SRT_ATR_UNUSED;
11425 if (from == SRT_SEQNO_NONE)
11426 {
11427 begin = m_pRcvLossList->removeUpTo(to);
11428 IF_HEAVY_LOGGING(autodetected = true);
11429 }
11430 else
11431 {
11432 begin = from;
11433 m_pRcvLossList->remove(from, to);
11434 }
11435
11436 #if ENABLE_HEAVY_LOGGING
11437 ostringstream range;
11438 if (begin == SRT_SEQNO_NONE)
11439 {
11440 range << "no";
11441 }
11442 else
11443 {
11444 int off = CSeqNo::seqoff(begin, to);
11445 if (off < 0)
11446 {
11447 range << "WEIRD NUMBER OF";
11448 }
11449 else
11450 {
11451 range << (off + 1);
11452 }
11453 }
11454
11455 static const char* const beginwhere[2] = {"explicit", "detected"};
11456
11457 const char* const reqtype = (from == SRT_SEQNO_NONE) ? "TLPKTDROP" : "DROPREQ";
11458
11459 HLOGC(qrlog.Debug, log << CONID() << "DROP PER " << reqtype << " %" << begin
11460 << "[" << beginwhere[1*autodetected] << "]-" << to << " ("
11461 << range.str() << " packets)");
11462 #endif
11463
11464 if (m_bPeerRexmitFlag == 0 || m_iReorderTolerance == 0)
11465 return;
11466
11467 // All code below concerns only "belated lossreport" feature.
11468
11469 // It's highly unlikely that this is waiting to send a belated UMSG_LOSSREPORT,
11470 // so treat it rather as a sanity check.
11471
11472 // It's enough to check if the first element of the list starts with a sequence older than 'to'.
11473 // If not, just do nothing.
11474
11475 size_t delete_index = 0;
11476 for (size_t i = 0; i < m_FreshLoss.size(); ++i)
11477 {
11478 CRcvFreshLoss::Emod result = m_FreshLoss[i].revoke(from, to);
11479 switch (result)
11480 {
11481 case CRcvFreshLoss::DELETE:
11482 delete_index = i + 1; // PAST THE END
11483 continue; // There may be further ranges that are included in this one, so check on.
11484
11485 case CRcvFreshLoss::NONE:
11486 case CRcvFreshLoss::STRIPPED:
11487 break; // THIS BREAKS ONLY 'switch', not 'for'!
11488
11489 case CRcvFreshLoss::SPLIT:; // This function never returns it. It's only a compiler shut-up.
11490 }
11491
11492 break; // Now this breaks also FOR.
11493 }
11494
11495 m_FreshLoss.erase(m_FreshLoss.begin(),
11496 m_FreshLoss.begin() + delete_index); // with delete_index == 0 will do nothing
11497 }
11498
11499 // This function, as the name states, should bake a new cookie.
11500 int32_t srt::CUDT::bake(const sockaddr_any& addr, int32_t current_cookie, int correction)
11501 {
11502 static unsigned int distractor = 0;
11503 unsigned int rollover = distractor + 10;
11504
11505 for (;;)
11506 {
11507 // SYN cookie
11508 char clienthost[NI_MAXHOST];
11509 char clientport[NI_MAXSERV];
11510 getnameinfo(addr.get(),
11511 addr.size(),
11512 clienthost,
11513 sizeof(clienthost),
11514 clientport,
11515 sizeof(clientport),
11516 NI_NUMERICHOST | NI_NUMERICSERV);
11517 int64_t timestamp = (count_microseconds(steady_clock::now() - m_stats.tsStartTime) / 60000000) + distractor +
11518 correction; // secret changes every one minute
11519 stringstream cookiestr;
11520 cookiestr << clienthost << ":" << clientport << ":" << timestamp;
11521 union {
11522 unsigned char cookie[16];
11523 int32_t cookie_val;
11524 };
11525 CMD5::compute(cookiestr.str().c_str(), cookie);
11526
11527 if (cookie_val != current_cookie)
11528 return cookie_val;
11529
11530 ++distractor;
11531
11532 // This is just to make the loop formally breakable,
11533 // but this is virtually impossible to happen.
11534 if (distractor == rollover)
11535 return cookie_val;
11536 }
11537 }
11538
11539 // XXX This is quite a mystery, why this function has a return value
11540 // and what the purpose for it was. There's just one call of this
11541 // function in the whole code and in that call the return value is
11542 // ignored. Actually this call happens in the CRcvQueue::worker thread,
11543 // where it makes a response for incoming UDP packet that might be
11544 // a connection request. Should any error occur in this process, there
11545 // is no way to "report error" that happened here. Basing on that
11546 // these values in original UDT code were quite like the values
11547 // for m_iReqType, they have been changed to URQ_* symbols, which
11548 // may mean that the intent for the return value was to send this
11549 // value back as a control packet back to the connector.
11550 //
11551 // This function is run when the CRcvQueue object is reading packets
11552 // from the multiplexer (@c CRcvQueue::worker_RetrieveUnit) and the
11553 // target socket ID is 0.
11554 //
11555 // XXX Make this function return EConnectStatus enum type (extend if needed),
11556 // and this will be directly passed to the caller.
11557
11558 // [[using locked(m_pRcvQueue->m_LSLock)]];
11559 int srt::CUDT::processConnectRequest(const sockaddr_any& addr, CPacket& packet)
11560 {
11561 // XXX ASSUMPTIONS:
11562 // [[using assert(packet.id() == 0)]]
11563
11564 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: received a connection request");
11565
11566 if (m_bClosing)
11567 {
11568 m_RejectReason = SRT_REJ_CLOSE;
11569 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: ... NOT. Rejecting because closing.");
11570 return m_RejectReason;
11571 }
11572
11573 /*
11574 * Closing a listening socket only set bBroken
11575 * If a connect packet is received while closing it gets through
11576 * processing and crashes later.
11577 */
11578 if (m_bBroken)
11579 {
11580 m_RejectReason = SRT_REJ_CLOSE;
11581 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: ... NOT. Rejecting because broken.");
11582 return m_RejectReason;
11583 }
11584 // When CHandShake::m_iContentSize is used in log, the file fails to link!
11585 size_t exp_len = CHandShake::m_iContentSize;
11586
11587 // NOTE!!! Old version of SRT code checks if the size of the HS packet
11588 // is EQUAL to the above CHandShake::m_iContentSize.
11589
11590 // Changed to < exp_len because we actually need that the packet
11591 // be at least of a size for handshake, although it may contain
11592 // more data, depending on what's inside.
11593 if (packet.getLength() < exp_len)
11594 {
11595 m_RejectReason = SRT_REJ_ROGUE;
11596 HLOGC(cnlog.Debug,
11597 log << CONID() << "processConnectRequest: ... NOT. Wrong size: " << packet.getLength()
11598 << " (expected: " << exp_len << ")");
11599 return m_RejectReason;
11600 }
11601
11602 // Don't know why the original UDT4 code only MUCH LATER was checking if the packet was UMSG_HANDSHAKE.
11603 // It doesn't seem to make sense to deserialize it into the handshake structure if we are not
11604 // sure that the packet contains the handshake at all!
11605 if (!packet.isControl(UMSG_HANDSHAKE))
11606 {
11607 m_RejectReason = SRT_REJ_ROGUE;
11608 LOGC(cnlog.Error,
11609 log << CONID() << "processConnectRequest: the packet received as handshake is not a handshake message");
11610 return m_RejectReason;
11611 }
11612
11613 CHandShake hs;
11614 hs.load_from(packet.m_pcData, packet.getLength());
11615
11616 // XXX MOST LIKELY this hs should be now copied into m_ConnRes field, which holds
11617 // the handshake structure sent from the peer (no matter the role or mode).
11618 // This should simplify the createSrtHandshake() function which can this time
11619 // simply write the crafted handshake structure into m_ConnReq, which needs no
11620 // participation of the local handshake and passing it as a parameter through
11621 // newConnection() -> acceptAndRespond() -> createSrtHandshake(). This is also
11622 // required as a source of the peer's information used in processing in other
11623 // structures.
11624
11625 int32_t cookie_val = bake(addr);
11626
11627 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: new cookie: " << hex << cookie_val);
11628
11629 // Remember the incoming destination address here and use it as a source
11630 // address when responding. It's not possible to record this address yet
11631 // because this happens still in the frames of the listener socket. Only
11632 // when processing switches to the newly spawned accepted socket can the
11633 // address be recorded in its m_SourceAddr field.
11634 sockaddr_any use_source_addr = packet.udpDestAddr();
11635
11636 // REQUEST:INDUCTION.
11637 // Set a cookie, a target ID, and send back the same as
11638 // RESPONSE:INDUCTION.
11639 if (hs.m_iReqType == URQ_INDUCTION)
11640 {
11641 HLOGC(cnlog.Debug,
11642 log << CONID() << "processConnectRequest: received type=induction, sending back with cookie+socket");
11643
11644 // XXX That looks weird - the calculated md5 sum out of the given host/port/timestamp
11645 // is 16 bytes long, but CHandShake::m_iCookie has 4 bytes. This then effectively copies
11646 // only the first 4 bytes. Moreover, it's dangerous on some platforms because the char
11647 // array need not be aligned to int32_t - changed to union in a hope that using int32_t
11648 // inside a union will enforce whole union to be aligned to int32_t.
11649 hs.m_iCookie = cookie_val;
11650 packet.set_id(hs.m_iID);
11651
11652 // Ok, now's the time. The listener sets here the version 5 handshake,
11653 // even though the request was 4. This is because the old client would
11654 // simply return THE SAME version, not even looking into it, giving the
11655 // listener false impression as if it supported version 5.
11656 //
11657 // If the caller was really HSv4, it will simply ignore the version 5 in INDUCTION;
11658 // it will respond with CONCLUSION, but with its own set version, which is version 4.
11659 //
11660 // If the caller was really HSv5, it will RECOGNIZE this version 5 in INDUCTION, so
11661 // it will respond with version 5 when sending CONCLUSION.
11662
11663 hs.m_iVersion = HS_VERSION_SRT1;
11664
11665 // Additionally, set this field to a MAGIC value. This field isn't used during INDUCTION
11666 // by HSv4 client, HSv5 client can use it to additionally verify that this is a HSv5 listener.
11667 // In this field we also advertise the PBKEYLEN value. When 0, it's considered not advertised.
11668 hs.m_iType = SrtHSRequest::wrapFlags(true /*put SRT_MAGIC_CODE in HSFLAGS*/, m_config.iSndCryptoKeyLen);
11669 bool whether SRT_ATR_UNUSED = m_config.iSndCryptoKeyLen != 0;
11670 HLOGC(cnlog.Debug,
11671 log << CONID() << "processConnectRequest: " << (whether ? "" : "NOT ")
11672 << " Advertising PBKEYLEN - value = " << m_config.iSndCryptoKeyLen);
11673
11674 size_t size = packet.getLength();
11675 hs.store_to((packet.m_pcData), (size));
11676 setPacketTS(packet, steady_clock::now());
11677
11678 // Display the HS before sending it to peer
11679 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: SENDING HS (i): " << hs.show());
11680
11681 m_pSndQueue->sendto(addr, packet, use_source_addr);
11682 return SRT_REJ_UNKNOWN; // EXCEPTION: this is a "no-error" code.
11683 }
11684
11685 // Otherwise this should be REQUEST:CONCLUSION.
11686 // Should then come with the correct cookie that was
11687 // set in the above INDUCTION, in the HS_VERSION_SRT1
11688 // should also contain extra data.
11689
11690 if (!hs.valid())
11691 {
11692 LOGC(cnlog.Error, log << CONID() << "processConnectRequest: ROGUE HS RECEIVED. Rejecting");
11693 m_RejectReason = SRT_REJ_ROGUE;
11694 return SRT_REJ_ROGUE;
11695 }
11696
11697 HLOGC(cnlog.Debug,
11698 log << CONID() << "processConnectRequest: received type=" << RequestTypeStr(hs.m_iReqType)
11699 << " - checking cookie...");
11700 if (hs.m_iCookie != cookie_val)
11701 {
11702 cookie_val = bake(addr, cookie_val, -1); // SHOULD generate an earlier, distracted cookie
11703
11704 if (hs.m_iCookie != cookie_val)
11705 {
11706 m_RejectReason = SRT_REJ_RDVCOOKIE;
11707 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: ...wrong cookie " << hex << cookie_val << ". Ignoring.");
11708 return m_RejectReason;
11709 }
11710
11711 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: ... correct (FIXED) cookie. Proceeding.");
11712 }
11713 else
11714 {
11715 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: ... correct (ORIGINAL) cookie. Proceeding.");
11716 }
11717
11718 SRTSOCKET id = hs.m_iID;
11719
11720 // HANDSHAKE: The old client sees the version that does not match HS_VERSION_UDT4 (5).
11721 // In this case it will respond with URQ_ERROR_REJECT. Rest of the data are the same
11722 // as in the handshake request. When this message is received, the connector side should
11723 // switch itself to the version number HS_VERSION_UDT4 and continue the old way (that is,
11724 // continue sending URQ_INDUCTION, but this time with HS_VERSION_UDT4).
11725
11726 bool accepted_hs = true;
11727
11728 if (hs.m_iVersion == HS_VERSION_SRT1)
11729 {
11730 // No further check required.
11731 // The m_iType contains handshake extension flags.
11732 }
11733 else if (hs.m_iVersion == HS_VERSION_UDT4)
11734 {
11735 // In UDT, and so in older SRT version, the hs.m_iType field should contain
11736 // the socket type, although SRT only allowed this field to be UDT_DGRAM.
11737 // Older SRT version contained that value in a field, but now that this can
11738 // only contain UDT_DGRAM the field itself has been abandoned.
11739 // For the sake of any old client that reports version 4 handshake, interpret
11740 // this hs.m_iType field as a socket type and check if it's UDT_DGRAM.
11741
11742 // Note that in HSv5 hs.m_iType contains extension flags.
11743 if (hs.m_iType != UDT_DGRAM)
11744 {
11745 m_RejectReason = SRT_REJ_ROGUE;
11746 accepted_hs = false;
11747 }
11748 }
11749 else
11750 {
11751 // Unsupported version
11752 // (NOTE: This includes "version=0" which is a rejection flag).
11753 m_RejectReason = SRT_REJ_VERSION;
11754 accepted_hs = false;
11755 }
11756
11757 if (!accepted_hs)
11758 {
11759 HLOGC(cnlog.Debug,
11760 log << CONID() << "processConnectRequest: version/type mismatch. Sending REJECT code:" << m_RejectReason
11761 << " MSG: " << srt_rejectreason_str(m_RejectReason));
11762 // mismatch, reject the request
11763 hs.m_iReqType = URQFailure(m_RejectReason);
11764 size_t size = CHandShake::m_iContentSize;
11765 hs.store_to((packet.m_pcData), (size));
11766 packet.set_id(id);
11767 setPacketTS((packet), steady_clock::now());
11768 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: SENDING HS (e): " << hs.show());
11769 m_pSndQueue->sendto(addr, packet, use_source_addr);
11770 }
11771 else
11772 {
11773 // IMPORTANT!!!
11774 // If the newConnection() detects there is already a socket connection associated with the remote peer,
11775 // it returns the socket via `acpu`, and the `result` returned is 0.
11776 // Else if a new connection is successfully created, the conclusion handshake response
11777 // is sent by the function itself (it calls the acceptAndRespond(..)), the `acpu` remains null, the `result` is 1.
11778 int error = SRT_REJ_UNKNOWN;
11779 CUDT* acpu = NULL;
11780 int result = uglobal().newConnection(m_SocketID, addr, packet, (hs), (error), (acpu));
11781
11782 // This is listener - m_RejectReason need not be set
11783 // because listener has no functionality of giving the app
11784 // insight into rejected callers.
11785
11786 // --->
11787 // (global.) CUDTUnited::updateListenerMux
11788 // (new Socket.) CUDT::acceptAndRespond
11789 if (result == -1)
11790 {
11791 hs.m_iReqType = URQFailure(error);
11792 LOGC(cnlog.Warn, log << "processConnectRequest: rsp(REJECT): " << hs.m_iReqType << " - " << srt_rejectreason_str(error));
11793 }
11794
11795 // The `acpu` not NULL means connection exists, the `result` should be 0. It is not checked here though.
11796 // The `newConnection(..)` only sends response for newly created connection.
11797 // The connection already exists (no new connection has been created, no response sent).
11798 // Send the conclusion response manually here in case the peer has missed the first one.
11799 // The value `result` here should be 0.
11800 if (acpu)
11801 {
11802 // This is an existing connection, so the handshake is only needed
11803 // because of the rule that every handshake request must be covered
11804 // by the handshake response. It wouldn't be good to call interpretSrtHandshake
11805 // here because the data from the handshake have been already interpreted
11806 // and recorded. We just need to craft a response.
11807 HLOGC(cnlog.Debug,
11808 log << CONID() << "processConnectRequest: sending REPEATED handshake response req="
11809 << RequestTypeStr(hs.m_iReqType));
11810
11811 // Rewrite already updated previously data in acceptAndRespond
11812 acpu->rewriteHandshakeData(acpu->m_PeerAddr, (hs));
11813
11814 uint32_t kmdata[SRTDATA_MAXSIZE];
11815 size_t kmdatasize = SRTDATA_MAXSIZE;
11816 EConnectStatus conn = CONN_ACCEPT;
11817
11818 if (hs.m_iVersion >= HS_VERSION_SRT1)
11819 {
11820 // Always attach extension.
11821 hs.m_extension = true;
11822 conn = acpu->craftKmResponse((kmdata), (kmdatasize));
11823 }
11824 else
11825 {
11826 kmdatasize = 0;
11827 }
11828
11829 if (conn != CONN_ACCEPT)
11830 return conn;
11831
11832 packet.setLength(m_iMaxSRTPayloadSize);
11833 if (!acpu->createSrtHandshake(SRT_CMD_HSRSP, SRT_CMD_KMRSP,
11834 kmdata, kmdatasize,
11835 (packet), (hs)))
11836 {
11837 HLOGC(cnlog.Debug,
11838 log << CONID() << "processConnectRequest: rejecting due to problems in createSrtHandshake.");
11839 result = -1; // enforce fallthrough for the below condition!
11840 hs.m_iReqType = URQFailure(m_RejectReason == SRT_REJ_UNKNOWN ? int(SRT_REJ_IPE) : m_RejectReason.load());
11841 }
11842 else
11843 {
11844 // Send the crafted handshake
11845 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: SENDING (repeated) HS (a): " << hs.show());
11846 acpu->addressAndSend((packet));
11847 }
11848 }
11849
11850 if (result == 1)
11851 {
11852 // BUG! There is no need to update write-readiness on the listener socket once new connection is accepted.
11853 // Only read-readiness has to be updated, but it is done so in the newConnection(..) function.
11854 // See PR #1831 and issue #1667.
11855 HLOGC(cnlog.Debug,
11856 log << CONID() << "processConnectRequest: accepted connection, updating epoll to write-ready");
11857
11858 // New connection has been accepted or an existing one has been found. Update epoll write-readiness.
11859 // a new connection has been created, enable epoll for write
11860 // Note: not using SRT_EPOLL_CONNECT symbol because this is a procedure
11861 // executed for the accepted socket.
11862 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_OUT, true);
11863 }
11864 else if (result == -1)
11865 {
11866 // The new connection failed
11867 // or the connection already existed, but manually sending the HS response above has failed.
11868 // HSv4: Send the SHUTDOWN message to the peer (see PR #2010) in order to disallow the peer to connect.
11869 // The HSv4 clients do not interpret the error handshake response correctly.
11870 // HSv5: Send a handshake with an error code (hs.m_iReqType set earlier) to the peer.
11871 if (hs.m_iVersion < HS_VERSION_SRT1)
11872 {
11873 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: HSv4 caller, sending SHUTDOWN after rejection with "
11874 << RequestTypeStr(hs.m_iReqType));
11875 CPacket rsp;
11876 setPacketTS((rsp), steady_clock::now());
11877 rsp.pack(UMSG_SHUTDOWN);
11878 rsp.set_id(m_PeerID);
11879 m_pSndQueue->sendto(addr, rsp, use_source_addr);
11880 }
11881 else
11882 {
11883 HLOGC(cnlog.Debug,
11884 log << CONID() << "processConnectRequest: sending ABNORMAL handshake info req="
11885 << RequestTypeStr(hs.m_iReqType));
11886 size_t size = CHandShake::m_iContentSize;
11887 hs.store_to((packet.m_pcData), (size));
11888 packet.setLength(size);
11889 packet.set_id(id);
11890 setPacketTS(packet, steady_clock::now());
11891 HLOGC(cnlog.Debug, log << CONID() << "processConnectRequest: SENDING HS (a): " << hs.show());
11892 m_pSndQueue->sendto(addr, packet, use_source_addr);
11893 }
11894 }
11895 }
11896 LOGC(cnlog.Debug, log << CONID() << "listen ret: " << hs.m_iReqType << " - " << RequestTypeStr(hs.m_iReqType));
11897
11898 return RejectReasonForURQ(hs.m_iReqType);
11899 }
11900
11901 void srt::CUDT::addLossRecord(std::vector<int32_t> &lr, int32_t lo, int32_t hi)
11902 {
11903 if (lo == hi)
11904 lr.push_back(lo);
11905 else
11906 {
11907 lr.push_back(lo | LOSSDATA_SEQNO_RANGE_FIRST);
11908 lr.push_back(hi);
11909 }
11910 }
11911
11912 int srt::CUDT::checkACKTimer(const steady_clock::time_point &currtime)
11913 {
11914 int because_decision = BECAUSE_NO_REASON;
11915 if (currtime > m_tsNextACKTime.load() // ACK time has come
11916 // OR the number of sent packets since last ACK has reached
11917 // the congctl-defined value of ACK Interval
11918 // (note that none of the builtin congctls defines ACK Interval)
11919 || (m_CongCtl->ACKMaxPackets() > 0 && m_iPktCount >= m_CongCtl->ACKMaxPackets()))
11920 {
11921 // ACK timer expired or ACK interval is reached
11922 sendCtrl(UMSG_ACK);
11923
11924 const steady_clock::duration ack_interval = m_CongCtl->ACKTimeout_us() > 0
11925 ? microseconds_from(m_CongCtl->ACKTimeout_us())
11926 : m_tdACKInterval;
11927 m_tsNextACKTime.store(currtime + ack_interval);
11928
11929 m_iPktCount = 0;
11930 m_iLightACKCount = 1;
11931 because_decision = BECAUSE_ACK;
11932 }
11933
11934 // Or the transfer rate is so high that the number of packets
11935 // have reached the value of SelfClockInterval * LightACKCount before
11936 // the time has come according to m_tsNextACKTime. In this case a "lite ACK"
11937 // is sent, which doesn't contain statistical data and nothing more
11938 // than just the ACK number. The "fat ACK" packets will be still sent
11939 // normally according to the timely rules.
11940 else if (m_iPktCount >= SELF_CLOCK_INTERVAL * m_iLightACKCount)
11941 {
11942 // send a "light" ACK
11943 sendCtrl(UMSG_ACK, NULL, NULL, SEND_LITE_ACK);
11944 ++m_iLightACKCount;
11945 because_decision = BECAUSE_LITEACK;
11946 }
11947
11948 return because_decision;
11949 }
11950
11951 int srt::CUDT::checkNAKTimer(const steady_clock::time_point& currtime)
11952 {
11953 // XXX The problem with working NAKREPORT with SRT_ARQ_ONREQ
11954 // is not that it would be inappropriate, but because it's not
11955 // implemented. The reason for it is that the structure of the
11956 // loss list container (m_pRcvLossList) is such that it is expected
11957 // that the loss records are ordered by sequence numbers (so
11958 // that two ranges sticking together are merged in place).
11959 // Unfortunately in case of SRT_ARQ_ONREQ losses must be recorded
11960 // as before, but they should not be reported, until confirmed
11961 // by the filter. By this reason they appear often out of order
11962 // and for adding them properly the loss list container wasn't
11963 // prepared. This then requires some more effort to implement.
11964 if (!m_config.bRcvNakReport || m_PktFilterRexmitLevel != SRT_ARQ_ALWAYS)
11965 return BECAUSE_NO_REASON;
11966
11967 /*
11968 * m_config.bRcvNakReport enables NAK reports for SRT.
11969 * Retransmission based on timeout is bandwidth consuming,
11970 * not knowing what to retransmit when the only NAK sent by receiver is lost,
11971 * all packets past last ACK are retransmitted (rexmitMethod() == SRM_FASTREXMIT).
11972 */
11973 enterCS(m_RcvLossLock);
11974 const int loss_len = m_pRcvLossList->getLossLength();
11975 leaveCS(m_RcvLossLock);
11976
11977 SRT_ASSERT(loss_len >= 0);
11978 int debug_decision = BECAUSE_NO_REASON;
11979
11980 if (loss_len > 0)
11981 {
11982 if (currtime <= m_tsNextNAKTime.load())
11983 return BECAUSE_NO_REASON; // wait for next NAK time
11984
11985 sendCtrl(UMSG_LOSSREPORT);
11986 debug_decision = BECAUSE_NAKREPORT;
11987 }
11988
11989 m_tsNextNAKTime.store(currtime + m_tdNAKInterval);
11990 return debug_decision;
11991 }
11992
11993 bool srt::CUDT::checkExpTimer(const steady_clock::time_point& currtime, int check_reason SRT_ATR_UNUSED)
11994 {
11995 // VERY HEAVY LOGGING
11996 #if ENABLE_HEAVY_LOGGING & 1
11997 static const char* const decisions [] = {
11998 "ACK",
11999 "LITE-ACK",
12000 "NAKREPORT"
12001 };
12002
12003 string decision = "NOTHING";
12004 if (check_reason)
12005 {
12006 ostringstream decd;
12007 decision = "";
12008 for (int i = 0; i < LAST_BECAUSE_BIT; ++i)
12009 {
12010 int flag = 1 << i;
12011 if (check_reason & flag)
12012 decd << decisions[i] << " ";
12013 }
12014 decision = decd.str();
12015 }
12016 HLOGC(xtlog.Debug, log << CONID() << "checkTimer: ACTIVITIES PERFORMED: " << decision);
12017 #endif
12018
12019 // In UDT the m_bUserDefinedRTO and m_iRTO were in CCC class.
12020 // There's nothing in the original code that alters these values.
12021
12022 steady_clock::time_point next_exp_time;
12023 if (m_CongCtl->RTO())
12024 {
12025 next_exp_time = m_tsLastRspTime.load() + microseconds_from(m_CongCtl->RTO());
12026 }
12027 else
12028 {
12029 steady_clock::duration exp_timeout =
12030 microseconds_from(m_iEXPCount * (m_iSRTT + 4 * m_iRTTVar) + COMM_SYN_INTERVAL_US);
12031 if (exp_timeout < (m_iEXPCount * m_tdMinExpInterval))
12032 exp_timeout = m_iEXPCount * m_tdMinExpInterval;
12033 next_exp_time = m_tsLastRspTime.load() + exp_timeout;
12034 }
12035
12036 if (currtime <= next_exp_time && !m_bBreakAsUnstable)
12037 return false;
12038
12039 // ms -> us
12040 const int PEER_IDLE_TMO_US = m_config.iPeerIdleTimeout_ms * 1000;
12041 // Haven't received any information from the peer, is it dead?!
12042 // timeout: at least 16 expirations and must be greater than 5 seconds
12043 time_point last_rsp_time = m_tsLastRspTime.load();
12044 if (m_bBreakAsUnstable || ((m_iEXPCount > COMM_RESPONSE_MAX_EXP) &&
12045 (currtime - last_rsp_time > microseconds_from(PEER_IDLE_TMO_US))))
12046 {
12047 //
12048 // Connection is broken.
12049 // UDT does not signal any information about this instead of to stop quietly.
12050 // Application will detect this when it calls any UDT methods next time.
12051 //
12052 HLOGC(xtlog.Debug,
12053 log << CONID() << "CONNECTION EXPIRED after " << FormatDuration<DUNIT_MS>(currtime - last_rsp_time) << " - BREAKING");
12054 m_bClosing = true;
12055 m_bBroken = true;
12056 m_iBrokenCounter = 30;
12057
12058 // update snd U list to remove this socket
12059 m_pSndQueue->m_pSndUList->update(this, CSndUList::DO_RESCHEDULE);
12060
12061 updateBrokenConnection();
12062 completeBrokenConnectionDependencies(SRT_ECONNLOST); // LOCKS!
12063
12064 return true;
12065 }
12066
12067 HLOGC(xtlog.Debug,
12068 log << CONID() << "EXP TIMER: count=" << m_iEXPCount << "/" << (+COMM_RESPONSE_MAX_EXP)
12069 << " elapsed=" << (count_microseconds(currtime - last_rsp_time)) << "/" << (+PEER_IDLE_TMO_US) << "us");
12070
12071 ++m_iEXPCount;
12072
12073 /*
12074 * (keepalive fix)
12075 * duB:
12076 * It seems there is confusion of the direction of the Response here.
12077 * lastRspTime is supposed to be when receiving (data/ctrl) from peer
12078 * as shown in processCtrl and processData,
12079 * Here we set because we sent something?
12080 *
12081 * Disabling this code that prevent quick reconnection when peer disappear
12082 */
12083 // Reset last response time since we've just sent a heart-beat.
12084 // (fixed) m_tsLastRspTime = currtime_tk;
12085
12086 return false;
12087 }
12088
12089 void srt::CUDT::checkRexmitTimer(const steady_clock::time_point& currtime)
12090 {
12091 // Check if HSv4 should be retransmitted, and if KM_REQ should be resent if the side is INITIATOR.
12092 checkSndTimers();
12093
12094 // There are two algorithms of blind packet retransmission: LATEREXMIT and FASTREXMIT.
12095 //
12096 // LATEREXMIT is only used with FileCC.
12097 // The RTO is triggered when some time has passed since the last ACK from
12098 // the receiver, while there is still some unacknowledged data in the sender's buffer,
12099 // and the loss list is empty at the moment of RTO (nothing to retransmit yet).
12100 //
12101 // FASTREXMIT is only used with LiveCC.
12102 // The RTO is triggered if the receiver is not configured to send periodic NAK reports,
12103 // when some time has passed since the last ACK from the receiver,
12104 // while there is still some unacknowledged data in the sender's buffer.
12105 //
12106 // In case the above conditions are met, the unacknowledged packets
12107 // in the sender's buffer will be added to the SND loss list and retransmitted.
12108 //
12109
12110 {
12111 ScopedLock ack_lock(m_RecvAckLock);
12112 const uint64_t rtt_syn = (m_iSRTT + 4 * m_iRTTVar + 2 * COMM_SYN_INTERVAL_US);
12113 const uint64_t exp_int_us = (m_iReXmitCount * rtt_syn + COMM_SYN_INTERVAL_US);
12114
12115 if (currtime <= (m_tsLastRspAckTime + microseconds_from(exp_int_us)))
12116 return;
12117 }
12118
12119 // If there is no unacknowledged data in the sending buffer,
12120 // then there is nothing to retransmit.
12121 if (m_pSndBuffer->getCurrBufSize() <= 0)
12122 return;
12123
12124 const bool is_laterexmit = m_CongCtl->rexmitMethod() == SrtCongestion::SRM_LATEREXMIT; // FileCC
12125 const bool is_fastrexmit = m_CongCtl->rexmitMethod() == SrtCongestion::SRM_FASTREXMIT; // LiveCC
12126
12127 // If the receiver will send periodic NAK reports, then FASTREXMIT (live) is inactive.
12128 // TODO: Probably some method of "blind rexmit" MUST BE DONE, when TLPKTDROP is off.
12129 if (is_fastrexmit && m_bPeerNakReport)
12130 return;
12131
12132 // Schedule a retransmission IF:
12133 // - there are packets in flight (getFlightSpan() > 0);
12134 // - in case of LATEREXMIT (File Mode): the sender loss list is empty
12135 // (the receiver didn't send any LOSSREPORT, or LOSSREPORT was lost on track).
12136 // - in case of FASTREXMIT (Live Mode): the RTO (rtt_syn) was triggered, therefore
12137 // schedule unacknowledged packets for retransmission regardless of the loss list emptiness.
12138 if (getFlightSpan() > 0 && (!is_laterexmit || m_pSndLossList->getLossLength() == 0))
12139 {
12140 // Sender: Insert all the packets sent after last received acknowledgement into the sender loss list.
12141 ScopedLock acklock(m_RecvAckLock); // Protect packet retransmission
12142 // Resend all unacknowledged packets on timeout, but only if there is no packet in the loss list
12143 const int32_t csn = m_iSndCurrSeqNo;
12144 const int num = m_pSndLossList->insert(m_iSndLastAck, csn);
12145 if (num > 0)
12146 {
12147 enterCS(m_StatsLock);
12148 m_stats.sndr.lost.count(num);
12149 leaveCS(m_StatsLock);
12150
12151 HLOGC(xtlog.Debug,
12152 log << CONID() << "ENFORCED " << (is_laterexmit ? "LATEREXMIT" : "FASTREXMIT")
12153 << " by ACK-TMOUT (scheduling): " << CSeqNo::incseq(m_iSndLastAck) << "-" << csn << " ("
12154 << CSeqNo::seqoff(m_iSndLastAck, csn) << " packets)");
12155 }
12156 }
12157
12158 ++m_iReXmitCount;
12159
12160 const ECheckTimerStage stage = is_fastrexmit ? TEV_CHT_FASTREXMIT : TEV_CHT_REXMIT;
12161 updateCC(TEV_CHECKTIMER, EventVariant(stage));
12162
12163 // schedule sending if not scheduled already
12164 m_pSndQueue->m_pSndUList->update(this, CSndUList::DONT_RESCHEDULE);
12165 }
12166
12167 void srt::CUDT::checkTimers()
12168 {
12169 // update CC parameters
12170 updateCC(TEV_CHECKTIMER, EventVariant(TEV_CHT_INIT));
12171
12172 const steady_clock::time_point currtime = steady_clock::now();
12173
12174 // This is a very heavy log, unblock only for temporary debugging!
12175 #if 0
12176 HLOGC(xtlog.Debug, log << CONID() << "checkTimers: nextacktime=" << FormatTime(m_tsNextACKTime)
12177 << " AckInterval=" << m_iACKInterval
12178 << " pkt-count=" << m_iPktCount << " liteack-count=" << m_iLightACKCount);
12179 #endif
12180
12181 #ifdef ENABLE_RATE_MEASUREMENT
12182 m_SndRegularMeasurement.pickup(currtime);
12183 m_SndRexmitMeasurement.pickup(currtime);
12184 #endif
12185
12186 // Check if it is time to send ACK
12187 int debug_decision = checkACKTimer(currtime);
12188
12189 // Check if it is time to send a loss report
12190 debug_decision |= checkNAKTimer(currtime);
12191
12192 // Check if the connection is expired
12193 if (checkExpTimer(currtime, debug_decision))
12194 return;
12195
12196 // Check if FAST or LATE packet retransmission is required
12197 checkRexmitTimer(currtime);
12198
12199 if (currtime > m_tsLastSndTime.load() + microseconds_from(COMM_KEEPALIVE_PERIOD_US))
12200 {
12201 sendCtrl(UMSG_KEEPALIVE);
12202 #if ENABLE_BONDING
12203 if (m_parent->m_GroupOf)
12204 {
12205 SharedLock glock (uglobal().m_GlobControlLock);
12206 if (m_parent->m_GroupOf)
12207 {
12208 // Pass socket ID because it's about changing group socket data
12209 m_parent->m_GroupOf->internalKeepalive(m_parent->m_GroupMemberData);
12210 // NOTE: GroupLock is unnecessary here because the only data read and
12211 // modified is the target of the iterator from m_GroupMemberData. The
12212 // iterator will be valid regardless of any container modifications.
12213 }
12214 }
12215 #endif
12216 HLOGP(xtlog.Debug, "KEEPALIVE");
12217 }
12218 }
12219
12220 void srt::CUDT::updateBrokenConnection()
12221 {
12222 HLOGC(smlog.Debug, log << "updateBrokenConnection: setting closing=true and taking out epoll events");
12223 m_bClosing = true;
12224 releaseSynch();
12225 // app can call any UDT API to learn the connection_broken error
12226 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR, true);
12227 CGlobEvent::triggerEvent();
12228 }
12229
12230 void srt::CUDT::completeBrokenConnectionDependencies(int errorcode)
12231 {
12232 int token = -1;
12233
12234 #if ENABLE_BONDING
12235 bool pending_broken = false;
12236 {
12237 SharedLock guard_group_existence (uglobal().m_GlobControlLock);
12238 if (m_parent->m_GroupOf)
12239 {
12240 token = m_parent->m_GroupMemberData->token;
12241 if (m_parent->m_GroupMemberData->sndstate == SRT_GST_PENDING)
12242 {
12243 HLOGC(gmlog.Debug, log << CONID() << "updateBrokenConnection: a pending link was broken - will be removed");
12244 pending_broken = true;
12245 }
12246 else
12247 {
12248 HLOGC(gmlog.Debug,
12249 log << CONID() << "updateBrokenConnection: state="
12250 << CUDTGroup::StateStr(m_parent->m_GroupMemberData->sndstate)
12251 << " a used link was broken - not closing automatically");
12252 }
12253
12254 m_parent->m_GroupMemberData->sndstate = SRT_GST_BROKEN;
12255 m_parent->m_GroupMemberData->rcvstate = SRT_GST_BROKEN;
12256 }
12257 }
12258 #endif
12259
12260 if (m_cbConnectHook)
12261 {
12262 CALLBACK_CALL(m_cbConnectHook, m_SocketID, errorcode, m_PeerAddr.get(), token);
12263 }
12264
12265 #if ENABLE_BONDING
12266 {
12267 // Lock GlobControlLock in order to make sure that
12268 // the state if the socket having the group and the
12269 // existence of the group will not be changed during
12270 // the operation. The attempt of group deletion will
12271 // have to wait until this operation completes.
12272 ExclusiveLock lock(uglobal().m_GlobControlLock);
12273 CUDTGroup* pg = m_parent->m_GroupOf;
12274 if (pg)
12275 {
12276 // Bound to one call because this requires locking
12277 pg->updateFailedLink();
12278 }
12279 // Sockets that never succeeded to connect must be deleted
12280 // explicitly, otherwise they will never be deleted. OTOH
12281 // the socket can be on the path of deletion already, so
12282 // this only makes sure that the socket will be deleted,
12283 // one way or another.
12284 if (pending_broken)
12285 {
12286 // XXX This somehow can cause a deadlock
12287 // uglobal()->close(m_parent);
12288 LOGC(smlog.Debug, log << "updateBrokenConnection...: BROKEN SOCKET @" << m_SocketID << " - CLOSING, to be removed from group.");
12289 m_parent->setBrokenClosed();
12290 }
12291 }
12292
12293 #endif
12294 }
12295
12296 void srt::CUDT::addEPoll(const int eid)
12297 {
12298 enterCS(uglobal().m_EPoll.m_EPollLock);
12299 m_sPollID.insert(eid);
12300 leaveCS(uglobal().m_EPoll.m_EPollLock);
12301
12302 if (!stillConnected())
12303 return;
12304
12305 enterCS(m_RecvLock);
12306 if (isRcvBufferReady())
12307 {
12308 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, true);
12309 }
12310 leaveCS(m_RecvLock);
12311
12312 if (m_config.iSndBufSize > m_pSndBuffer->getCurrBufSize())
12313 {
12314 uglobal().m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_OUT, true);
12315 }
12316 }
12317
12318 void srt::CUDT::removeEPollEvents(const int eid)
12319 {
12320 // clear IO events notifications;
12321 // since this happens after the epoll ID has been removed, they cannot be set again
12322 set<int> remove;
12323 remove.insert(eid);
12324 uglobal().m_EPoll.update_events(m_SocketID, remove, SRT_EPOLL_IN | SRT_EPOLL_OUT, false);
12325 }
12326
12327 void srt::CUDT::removeEPollID(const int eid)
12328 {
12329 enterCS(uglobal().m_EPoll.m_EPollLock);
12330 m_sPollID.erase(eid);
12331 leaveCS(uglobal().m_EPoll.m_EPollLock);
12332 }
12333
12334 void srt::CUDT::ConnectSignal(ETransmissionEvent evt, EventSlot sl)
12335 {
12336 if (evt >= TEV_E_SIZE)
12337 return; // sanity check
12338
12339 m_Slots[evt].push_back(sl);
12340 }
12341
12342 void srt::CUDT::DisconnectSignal(ETransmissionEvent evt)
12343 {
12344 if (evt >= TEV_E_SIZE)
12345 return; // sanity check
12346
12347 m_Slots[evt].clear();
12348 }
12349
12350 void srt::CUDT::EmitSignal(ETransmissionEvent tev, EventVariant var)
12351 {
12352 for (std::vector<EventSlot>::iterator i = m_Slots[tev].begin(); i != m_Slots[tev].end(); ++i)
12353 {
12354 i->emit(tev, var);
12355 }
12356 }
12357
12358 int srt::CUDT::getsndbuffer(SRTSOCKET u, size_t *blocks, size_t *bytes)
12359 {
12360 CUDTSocket *s = uglobal().locateSocket(u);
12361 if (!s)
12362 return -1;
12363
12364 CSndBuffer *b = s->core().m_pSndBuffer;
12365
12366 if (!b)
12367 return -1;
12368
12369 int bytecount, timespan;
12370 int count = b->getCurrBufSize((bytecount), (timespan));
12371
12372 if (blocks)
12373 *blocks = count;
12374
12375 if (bytes)
12376 *bytes = bytecount;
12377
12378 return std::abs(timespan);
12379 }
12380
12381 int srt::CUDT::rejectReason(SRTSOCKET u)
12382 {
12383 CUDTSocket* s = uglobal().locateSocket(u);
12384 if (!s)
12385 return SRT_REJ_UNKNOWN;
12386
12387 return s->core().m_RejectReason;
12388 }
12389
12390 int srt::CUDT::rejectReason(SRTSOCKET u, int value)
12391 {
12392 CUDTSocket* s = uglobal().locateSocket(u);
12393 if (!s)
12394 return APIError(MJ_NOTSUP, MN_SIDINVAL);
12395
12396 if (value < SRT_REJC_PREDEFINED)
12397 return APIError(MJ_NOTSUP, MN_INVAL);
12398
12399 s->core().m_RejectReason = value;
12400 return 0;
12401 }
12402
12403 int64_t srt::CUDT::socketStartTime(SRTSOCKET u)
12404 {
12405 CUDTSocket* s = uglobal().locateSocket(u);
12406 if (!s)
12407 return APIError(MJ_NOTSUP, MN_SIDINVAL);
12408
12409 return count_microseconds(s->core().socketStartTime().time_since_epoch());
12410 }
12411
12412 bool srt::CUDT::runAcceptHook(CUDT *acore, const sockaddr* peer, const CHandShake& hs, const CPacket& hspkt)
12413 {
12414 // Prepare the information for the hook.
12415
12416 // We need streamid.
12417 char target[CSrtConfig::MAX_SID_LENGTH + 1];
12418 memset((target), 0, CSrtConfig::MAX_SID_LENGTH + 1);
12419
12420 // Just for a case, check the length.
12421 // This wasn't done before, and we could risk memory crash.
12422 // In case of error, this will remain unset and the empty
12423 // string will be passed as streamid.
12424
12425 int ext_flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(hs.m_iType);
12426
12427 #if ENABLE_BONDING
12428 bool have_group = false;
12429 SRT_GROUP_TYPE gt = SRT_GTYPE_UNDEFINED;
12430 #endif
12431
12432 // This tests if there are any extensions.
12433 if (hspkt.getLength() > CHandShake::m_iContentSize + 4 && IsSet(ext_flags, CHandShake::HS_EXT_CONFIG))
12434 {
12435 uint32_t *begin = reinterpret_cast<uint32_t *>(hspkt.m_pcData + CHandShake::m_iContentSize);
12436 size_t size = hspkt.getLength() - CHandShake::m_iContentSize; // Due to previous cond check we grant it's >0
12437 uint32_t *next = 0;
12438 size_t length = size / sizeof(uint32_t);
12439 size_t blocklen = 0;
12440
12441 for (;;) // ONE SHOT, but continuable loop
12442 {
12443 int cmd = FindExtensionBlock(begin, length, (blocklen), (next));
12444
12445 const size_t bytelen = blocklen * sizeof(uint32_t);
12446
12447 if (cmd == SRT_CMD_SID)
12448 {
12449 if (!bytelen || bytelen > CSrtConfig::MAX_SID_LENGTH)
12450 {
12451 LOGC(cnlog.Error,
12452 log << CONID() << "interpretSrtHandshake: STREAMID length " << bytelen << " is 0 or > "
12453 << +CSrtConfig::MAX_SID_LENGTH << " - PROTOCOL ERROR, REJECTING");
12454 return false;
12455 }
12456 // See comment at CUDT::interpretSrtHandshake().
12457 memcpy((target), begin + 1, bytelen);
12458
12459 // Un-swap on big endian machines
12460 ItoHLA(((uint32_t *)target), (uint32_t *)target, blocklen);
12461 }
12462 #if ENABLE_BONDING
12463 else if (cmd == SRT_CMD_GROUP)
12464 {
12465 uint32_t* groupdata = begin + 1;
12466 have_group = true; // Even if parse error happes
12467 if (bytelen / sizeof(int32_t) >= GRPD_E_SIZE)
12468 {
12469 uint32_t gd = groupdata[GRPD_GROUPDATA];
12470 gt = SRT_GROUP_TYPE(SrtHSRequest::HS_GROUP_TYPE::unwrap(gd));
12471 }
12472 }
12473 #endif
12474 else if (cmd == SRT_CMD_NONE)
12475 {
12476 // End of blocks
12477 break;
12478 }
12479
12480 // Any other kind of message extracted. Search on.
12481 if (!NextExtensionBlock((begin), next, (length)))
12482 break;
12483 }
12484 }
12485
12486 #if ENABLE_BONDING
12487 if (have_group && acore->m_config.iGroupConnect == 0)
12488 {
12489 HLOGC(cnlog.Debug,
12490 log << CONID() << "runAcceptHook: REJECTING connection WITHOUT calling the hook - groups not allowed");
12491 return false;
12492 }
12493
12494 // Update the groupconnect flag
12495 acore->m_config.iGroupConnect = have_group ? 1 : 0;
12496 acore->m_HSGroupType = gt;
12497 #endif
12498
12499 // Set the default value
12500 acore->m_RejectReason = SRT_REJX_FALLBACK;
12501 try
12502 {
12503 int result = CALLBACK_CALL(m_cbAcceptHook, acore->m_SocketID, hs.m_iVersion, peer, target);
12504 if (result == -1)
12505 return false;
12506 }
12507 catch (...)
12508 {
12509 LOGP(cnlog.Warn, "runAcceptHook: hook interrupted by exception");
12510 return false;
12511 }
12512
12513 acore->m_RejectReason = SRT_REJ_UNKNOWN;
12514 return true;
12515 }
12516
12517 void srt::CUDT::processKeepalive(const CPacket& ctrlpkt, const time_point& tsArrival)
12518 {
12519 // Here can be handled some protocol definition
12520 // for extra data sent through keepalive.
12521
12522 #if ENABLE_BONDING
12523 if (m_parent->m_GroupOf)
12524 {
12525 // Lock GlobControlLock in order to make sure that
12526 // the state of the socket having the group and the
12527 // existence of the group will not be changed during
12528 // the operation. The attempt of group deletion will
12529 // have to wait until this operation completes.
12530 ExclusiveLock lock(uglobal().m_GlobControlLock);
12531 CUDTGroup* pg = m_parent->m_GroupOf;
12532 if (pg)
12533 {
12534 // Whether anything is to be done with this socket
12535 // about the fact that keepalive arrived, let the
12536 // group handle it
12537 pg->processKeepalive(m_parent->m_GroupMemberData);
12538 }
12539 }
12540 #endif
12541
12542 ScopedLock lck(m_RcvBufferLock);
12543 m_pRcvBuffer->updateTsbPdTimeBase(ctrlpkt.getMsgTimeStamp());
12544 if (m_config.bDriftTracer)
12545 m_pRcvBuffer->addRcvTsbPdDriftSample(ctrlpkt.getMsgTimeStamp(), tsArrival, -1);
12546 }
12547
12548 namespace srt {
12549 HandshakeSide getHandshakeSide(SRTSOCKET u)
12550 {
12551 return CUDT::handshakeSide(u);
12552 }
12553
12554 HandshakeSide CUDT::handshakeSide(SRTSOCKET u)
12555 {
12556 CUDTSocket *s = uglobal().locateSocket(u);
12557 return s ? s->core().handshakeSide() : HSD_DRAW;
12558 }
12559 }
12560
12561