Debian Codegraph @

srtcore/handshake.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 #include "platform_sys.h"
47
48 #include <cstring>
49 #include <string>
50 #include <sstream>
51 #include <iterator>
52 #include <algorithm>
53
54 #include "udt.h"
55 #include "api.h"
56 #include "core.h"
57 #include "handshake.h"
58 #include "utilities.h"
59
60 using namespace std;
61 using namespace srt;
62
63
64 srt::CHandShake::CHandShake()
65 : m_iVersion(0)
66 , m_iType(0) // Universal: UDT_UNDEFINED or no flags
67 , m_iISN(0)
68 , m_iMSS(0)
69 , m_iFlightFlagSize(0)
70 , m_iReqType(URQ_WAVEAHAND)
71 , m_iID(0)
72 , m_iCookie(0)
73 , m_extension(false)
74 {
75 for (int i = 0; i < 4; ++ i)
76 m_piPeerIP[i] = 0;
77 }
78
79 int srt::CHandShake::store_to(char* buf, size_t& w_size)
80 {
81 if (w_size < m_iContentSize)
82 return -1;
83
84 int32_t* p = reinterpret_cast<int32_t*>(buf);
85 *p++ = m_iVersion;
86 *p++ = m_iType;
87 *p++ = m_iISN;
88 *p++ = m_iMSS;
89 *p++ = m_iFlightFlagSize;
90 *p++ = int32_t(m_iReqType);
91 *p++ = m_iID;
92 *p++ = m_iCookie;
93 for (int i = 0; i < 4; ++ i)
94 *p++ = m_piPeerIP[i];
95
96 w_size = m_iContentSize;
97
98 return 0;
99 }
100
101 int srt::CHandShake::load_from(const char* buf, size_t size)
102 {
103 if (size < m_iContentSize)
104 return -1;
105
106 const int32_t* p = reinterpret_cast<const int32_t*>(buf);
107
108 m_iVersion = *p++;
109 m_iType = *p++;
110 m_iISN = *p++;
111 m_iMSS = *p++;
112 m_iFlightFlagSize = *p++;
113 m_iReqType = UDTRequestType(*p++);
114 m_iID = *p++;
115 m_iCookie = *p++;
116 for (int i = 0; i < 4; ++ i)
117 m_piPeerIP[i] = *p++;
118
119 return 0;
120 }
121
122 #ifdef ENABLE_LOGGING
123
124 namespace srt
125 {
126 const char* srt_rejectreason_name [] = {
127 "UNKNOWN",
128 "SYSTEM",
129 "PEER",
130 "RESOURCE",
131 "ROGUE",
132 "BACKLOG",
133 "IPE",
134 "CLOSE",
135 "VERSION",
136 "RDVCOOKIE",
137 "BADSECRET",
138 "UNSECURE",
139 "MESSAGEAPI",
140 "CONGESTION",
141 "FILTER",
142 "GROUP",
143 "TIMEOUT",
144 "CRYPTO"
145 };
146 }
147
148 std::string srt::RequestTypeStr(UDTRequestType rq)
149 {
150 if (rq >= URQ_FAILURE_TYPES)
151 {
152 std::ostringstream rt;
153 rt << "ERROR:";
154 int id = RejectReasonForURQ(rq);
155 if (id < (int) Size(srt_rejectreason_name))
156 rt << srt_rejectreason_name[id];
157 else if (id < SRT_REJC_USERDEFINED)
158 {
159 if (id < SRT_REJC_PREDEFINED)
160 rt << "UNKNOWN:" << id;
161 else
162 rt << "PREDEFINED:" << (id - SRT_REJC_PREDEFINED);
163 }
164 else
165 rt << "USERDEFINED:" << (id - SRT_REJC_USERDEFINED);
166
167 return rt.str();
168 }
169
170 switch ( rq )
171 {
172 case URQ_INDUCTION: return "induction";
173 case URQ_WAVEAHAND: return "waveahand";
174 case URQ_CONCLUSION: return "conclusion";
175 case URQ_AGREEMENT: return "agreement";
176
177 default: return "INVALID";
178 }
179 }
180
181 string srt::CHandShake::RdvStateStr(CHandShake::RendezvousState s)
182 {
183 switch (s)
184 {
185 case RDV_WAVING: return "waving";
186 case RDV_ATTENTION: return "attention";
187 case RDV_FINE: return "fine";
188 case RDV_INITIATED: return "initiated";
189 case RDV_CONNECTED: return "connected";
190 default: ;
191 }
192
193 return "invalid";
194 }
195 #endif
196
197 bool srt::CHandShake::valid()
198 {
199 if (m_iVersion < CUDT::HS_VERSION_UDT4
200 || m_iISN < 0 || m_iISN >= CSeqNo::m_iMaxSeqNo
201 || m_iMSS < 32
202 || m_iFlightFlagSize < 2)
203 return false;
204
205 return true;
206 }
207
208 string srt::CHandShake::show()
209 {
210 ostringstream so;
211
212 so << "version=" << m_iVersion << " type=0x" << hex << m_iType << dec
213 << " ISN=" << m_iISN << " MSS=" << m_iMSS << " FLW=" << m_iFlightFlagSize
214 << " reqtype=" << RequestTypeStr(m_iReqType) << " srcID=" << m_iID
215 << " cookie=" << hex << m_iCookie << dec
216 << " srcIP=";
217
218 const unsigned char* p = (const unsigned char*)m_piPeerIP;
219 const unsigned char* pe = p + 4 * (sizeof(uint32_t));
220
221 copy(p, pe, ostream_iterator<unsigned>(so, "."));
222
223 // XXX HS version symbols should be probably declared inside
224 // CHandShake, not CUDT.
225 if ( m_iVersion > CUDT::HS_VERSION_UDT4 )
226 {
227 const int flags = SrtHSRequest::SRT_HSTYPE_HSFLAGS::unwrap(m_iType);
228 so << "FLAGS: ";
229 if (flags == SrtHSRequest::SRT_MAGIC_CODE)
230 so << "MAGIC";
231 else if (m_iType == 0)
232 so << "NONE"; // no flags and no advertised pbkeylen
233 else
234 so << ExtensionFlagStr(m_iType);
235 }
236
237 return so.str();
238 }
239
240 string srt::CHandShake::ExtensionFlagStr(int32_t fl)
241 {
242 std::ostringstream out;
243 if ( fl & HS_EXT_HSREQ )
244 out << " hsx";
245 if ( fl & HS_EXT_KMREQ )
246 out << " kmx";
247 if ( fl & HS_EXT_CONFIG )
248 out << " config";
249
250 const int kl = SrtHSRequest::SRT_HSTYPE_ENCFLAGS::unwrap(fl) << 6;
251 if (kl != 0)
252 {
253 out << " AES-" << kl;
254 }
255 else
256 {
257 out << " no-pbklen";
258 }
259
260 return out.str();
261 }
262
263
264 // XXX This code isn't currently used. Left here because it can
265 // be used in future, should any refactoring for the "manual word placement"
266 // code be done.
267 bool srt::SrtHSRequest::serialize(char* buf, size_t size) const
268 {
269 if (size < SRT_HS_SIZE)
270 return false;
271
272 int32_t* p = reinterpret_cast<int32_t*>(buf);
273
274 *p++ = m_iSrtVersion;
275 *p++ = m_iSrtFlags;
276 *p++ = m_iSrtTsbpd;
277 *p++ = 0; // SURPRISE! Seriously, use (something) if this "reserved" is going to be used for something.
278 return true;
279 }
280
281
282 bool srt::SrtHSRequest::deserialize(const char* buf, size_t size)
283 {
284 m_iSrtVersion = 0; // just to let users recognize if it succeeded or not.
285
286 if (size < SRT_HS_SIZE)
287 return false;
288
289 const int32_t* p = reinterpret_cast<const int32_t*>(buf);
290
291 m_iSrtVersion = (*p++);
292 m_iSrtFlags = (*p++);
293 m_iSrtTsbpd = (*p++);
294 m_iSrtReserved = (*p++);
295 return true;
296 }
297
298 std::string srt::SrtFlagString(int32_t flags)
299 {
300 #define LEN(arr) (sizeof (arr)/(sizeof ((arr)[0])))
301
302 std::string output;
303 static std::string namera[] = { "TSBPD-snd", "TSBPD-rcv", "haicrypt", "TLPktDrop", "NAKReport", "ReXmitFlag", "StreamAPI", "FilterCapable" };
304
305 size_t i = 0;
306 for (; i < LEN(namera); ++i)
307 {
308 if ((flags & 1) == 1)
309 {
310 output += "+" + namera[i] + " ";
311 }
312 else
313 {
314 output += "-" + namera[i] + " ";
315 }
316
317 flags >>= 1;
318 }
319
320 #undef LEN
321
322 if (flags != 0)
323 {
324 output += "+unknown";
325 }
326
327 return output;
328 }