forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandshake.cpp
More file actions
337 lines (280 loc) · 8.25 KB
/
handshake.cpp
File metadata and controls
337 lines (280 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
/*****************************************************************************
Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the
above copyright notice, this list of conditions
and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Illinois
nor the names of its contributors may be used to
endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "platform_sys.h"
#include <cstring>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>
#include "api.h"
#include "core.h"
#include "handshake.h"
#include "utilities.h"
using namespace std;
namespace srt
{
CHandShake::CHandShake()
: m_iVersion(0)
, m_iType(0) // Universal: UDT_UNDEFINED or no flags
, m_iISN(0)
, m_iMSS(0)
, m_iFlightFlagSize(0)
, m_iReqType(URQ_WAVEAHAND)
, m_iID(0)
, m_iCookie(0)
, m_extensionType(0)
{
for (int i = 0; i < 4; ++ i)
m_piPeerIP[i] = 0;
}
int CHandShake::store_to(char* buf, size_t& w_size)
{
if (w_size < m_iContentSize)
return -1;
int32_t* p = reinterpret_cast<int32_t*>(buf);
*p++ = m_iVersion;
*p++ = m_iType;
*p++ = m_iISN;
*p++ = m_iMSS;
*p++ = m_iFlightFlagSize;
*p++ = int32_t(m_iReqType);
*p++ = int32_t(m_iID);
*p++ = m_iCookie;
for (int i = 0; i < 4; ++ i)
*p++ = m_piPeerIP[i];
w_size = m_iContentSize;
return 0;
}
int CHandShake::load_from(const char* buf, size_t size)
{
if (size < m_iContentSize)
return -1;
const int32_t* p = reinterpret_cast<const int32_t*>(buf);
m_iVersion = *p++;
m_iType = *p++;
m_iISN = *p++;
m_iMSS = *p++;
m_iFlightFlagSize = *p++;
m_iReqType = UDTRequestType(*p++);
m_iID = SRTSOCKET(*p++);
m_iCookie = *p++;
for (int i = 0; i < 4; ++ i)
m_piPeerIP[i] = *p++;
m_extensionType = 0;
if (size > m_iContentSize + sizeof(int32_t) && m_iReqType == URQ_CONCLUSION)
{
// Extensions provided - check the first word for HSREQ/HSRSP
int cmd = HS_CMDSPEC_CMD::unwrap(*p);
if (cmd == SRT_CMD_HSREQ || cmd == SRT_CMD_HSRSP)
m_extensionType = cmd;
}
return 0;
}
#if HVU_ENABLE_LOGGING
const char* srt_rejectreason_name [] = {
"UNKNOWN",
"SYSTEM",
"PEER",
"RESOURCE",
"ROGUE",
"BACKLOG",
"IPE",
"CLOSE",
"VERSION",
"RDVCOOKIE",
"BADSECRET",
"UNSECURE",
"MESSAGEAPI",
"CONGESTION",
"FILTER",
"GROUP",
"TIMEOUT",
"CRYPTO"
};
std::string RequestTypeStr(UDTRequestType rq)
{
if (rq >= URQ_FAILURE_TYPES)
{
std::ostringstream rt;
rt << "ERROR:";
int id = RejectReasonForURQ(rq);
if (id < (int) Size(srt_rejectreason_name))
rt << srt_rejectreason_name[id];
else if (id < SRT_REJC_USERDEFINED)
{
if (id < SRT_REJC_PREDEFINED)
rt << "UNKNOWN:" << id;
else
rt << "PREDEFINED:" << (id - SRT_REJC_PREDEFINED);
}
else
rt << "USERDEFINED:" << (id - SRT_REJC_USERDEFINED);
return rt.str();
}
switch ( rq )
{
case URQ_INDUCTION: return "induction";
case URQ_WAVEAHAND: return "waveahand";
case URQ_CONCLUSION: return "conclusion";
case URQ_AGREEMENT: return "agreement";
default: return "INVALID";
}
}
string CHandShake::RdvStateStr(CHandShake::RendezvousState s)
{
switch (s)
{
case RDV_WAVING: return "waving";
case RDV_ATTENTION: return "attention";
case RDV_FINE: return "fine";
case RDV_INITIATED: return "initiated";
case RDV_CONNECTED: return "connected";
default: ;
}
return "invalid";
}
#endif
bool CHandShake::valid()
{
if (m_iVersion < CUDT::HS_VERSION_UDT4
|| m_iISN < 0 || m_iISN >= CSeqNo::m_iMaxSeqNo
|| m_iMSS < 32
|| m_iFlightFlagSize < 2)
return false;
return true;
}
string CHandShake::show()
{
using namespace hvu;
ofmtbufstream so;
so << "version=" << m_iVersion
<< " type=0x" << fmt(m_iType, hex)
<< " ISN=" << m_iISN << " MSS=" << m_iMSS << " FLW=" << m_iFlightFlagSize
<< " reqtype=" << RequestTypeStr(m_iReqType) << " srcID=" << m_iID
<< " cookie=" << fmt(m_iCookie, hex)
<< " srcIP=" << CIPAddress::show(m_piPeerIP);
// XXX HS version symbols should be probably declared inside
// CHandShake, not CUDT.
if ( m_iVersion > CUDT::HS_VERSION_UDT4 )
{
so << " FLAGS: ";
so << ExtensionFlagStr(m_iType);
}
return so.str();
}
string CHandShake::ExtensionFlagStr(int32_t fl)
{
std::ostringstream out;
const int flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(fl);
const int kl = SrtHSRequest::SRT_HSTYPE_ENCFLAGS::unwrap(fl) << 6;
if (flags == SrtHSRequest::SRT_MAGIC_CODE)
out << "MAGIC ";
else if (fl == 0)
out << "NONE"; // no flags and no advertised pbkeylen
else
{
if ( fl & HS_EXT_HSREQ )
out << "hsx ";
if ( fl & HS_EXT_KMREQ )
out << "kmx ";
if ( fl & HS_EXT_CONFIG )
out << "cfg ";
}
if (kl != 0)
{
out << "[AES-" << kl << "]";
}
else
{
out << "[nokeyad]";
}
return out.str();
}
// XXX This code isn't currently used. Left here because it can
// be used in future, should any refactoring for the "manual word placement"
// code be done.
bool SrtHSRequest::serialize(char* buf, size_t size) const
{
if (size < SRT_HS_SIZE)
return false;
int32_t* p = reinterpret_cast<int32_t*>(buf);
*p++ = m_iSrtVersion;
*p++ = m_iSrtFlags;
*p++ = m_iSrtTsbpd;
*p++ = 0; // SURPRISE! Seriously, use (something) if this "reserved" is going to be used for something.
return true;
}
bool SrtHSRequest::deserialize(const char* buf, size_t size)
{
m_iSrtVersion = 0; // just to let users recognize if it succeeded or not.
if (size < SRT_HS_SIZE)
return false;
const int32_t* p = reinterpret_cast<const int32_t*>(buf);
m_iSrtVersion = (*p++);
m_iSrtFlags = (*p++);
m_iSrtTsbpd = (*p++);
m_iSrtReserved = (*p++);
return true;
}
string SrtFlagString(int32_t flags)
{
#define LEN(arr) (sizeof (arr)/(sizeof ((arr)[0])))
std::string output;
static std::string namera[] = { "TSBPD-snd", "TSBPD-rcv", "haicrypt", "TLPktDrop", "NAKReport", "ReXmitFlag", "StreamAPI", "FilterCapable" };
size_t i = 0;
for (; i < LEN(namera); ++i)
{
if ((flags & 1) == 1)
{
output += "+" + namera[i] + " ";
}
else
{
output += "-" + namera[i] + " ";
}
flags >>= 1;
}
#undef LEN
if (flags != 0)
{
output += "+unknown";
}
return output;
}
}