1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
/*
* 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 - 2016, 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.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu, last updated 07/25/2010
modified by
Haivision Systems Inc.
*****************************************************************************/
#define SRT_IMPORT_TIME 1
#include "platform_sys.h"
#include <string>
#include <sstream>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <vector>
#include "udt.h"
#include "md5.h"
#include "common.h"
#include "netinet_any.h"
#include "logging.h"
#include "packet.h"
#include "threadname.h"
using namespace std;
using namespace srt::sync;
using namespace srt_logging;
namespace srt_logging
{
extern Logger inlog;
}
namespace srt
{
const char* strerror_get_message(size_t major, size_t minor);
} // namespace srt
srt::CUDTException::CUDTException(CodeMajor major, CodeMinor minor, int err):
m_iMajor(major),
m_iMinor(minor)
{
if (err == -1)
m_iErrno = NET_ERROR;
else
m_iErrno = err;
}
const char* srt::CUDTException::getErrorMessage() const ATR_NOTHROW
{
return strerror_get_message(m_iMajor, m_iMinor);
}
std::string srt::CUDTException::getErrorString() const
{
return getErrorMessage();
}
#define UDT_XCODE(mj, mn) (int(mj)*1000)+int(mn)
int srt::CUDTException::getErrorCode() const
{
return UDT_XCODE(m_iMajor, m_iMinor);
}
int srt::CUDTException::getErrno() const
{
return m_iErrno;
}
void srt::CUDTException::clear()
{
m_iMajor = MJ_SUCCESS;
m_iMinor = MN_NONE;
m_iErrno = 0;
}
#undef UDT_XCODE
//
bool srt::CIPAddress::ipcmp(const sockaddr* addr1, const sockaddr* addr2, int ver)
{
if (AF_INET == ver)
{
sockaddr_in* a1 = (sockaddr_in*)addr1;
sockaddr_in* a2 = (sockaddr_in*)addr2;
if ((a1->sin_port == a2->sin_port) && (a1->sin_addr.s_addr == a2->sin_addr.s_addr))
return true;
}
else
{
sockaddr_in6* a1 = (sockaddr_in6*)addr1;
sockaddr_in6* a2 = (sockaddr_in6*)addr2;
if (a1->sin6_port == a2->sin6_port)
{
for (int i = 0; i < 16; ++ i)
if (*((char*)&(a1->sin6_addr) + i) != *((char*)&(a2->sin6_addr) + i))
return false;
return true;
}
}
return false;
}
void srt::CIPAddress::ntop(const sockaddr_any& addr, uint32_t ip[4])
{
if (addr.family() == AF_INET)
{
// SRT internal format of IPv4 address.
// The IPv4 address is in the first field. The rest is 0.
ip[0] = addr.sin.sin_addr.s_addr;
ip[1] = ip[2] = ip[3] = 0;
}
else
{
std::memcpy(ip, addr.sin6.sin6_addr.s6_addr, 16);
}
}
namespace srt {
bool checkMappedIPv4(const uint16_t* addr)
{
static const uint16_t ipv4on6_model [8] =
{
0, 0, 0, 0, 0, 0xFFFF, 0, 0
};
// Compare only first 6 words. Remaining 2 words
// comprise the IPv4 address, if these first 6 match.
const uint16_t* mbegin = ipv4on6_model;
const uint16_t* mend = ipv4on6_model + 6;
return std::equal(mbegin, mend, addr);
}
}
// XXX This has void return and the first argument is passed by reference.
// Consider simply returning sockaddr_any by value.
void srt::CIPAddress::pton(sockaddr_any& w_addr, const uint32_t ip[4], const sockaddr_any& peer)
{
//using ::srt_logging::inlog;
uint32_t* target_ipv4_addr = NULL;
if (peer.family() == AF_INET)
{
sockaddr_in* a = (&w_addr.sin);
target_ipv4_addr = (uint32_t*) &a->sin_addr.s_addr;
}
else // AF_INET6
{
// Check if the peer address is a model of IPv4-mapped-on-IPv6.
// If so, it means that the `ip` array should be interpreted as IPv4.
const bool is_mapped_ipv4 = checkMappedIPv4(peer.sin6);
sockaddr_in6* a = (&w_addr.sin6);
// This whole above procedure was only in order to EXCLUDE the
// possibility of IPv4-mapped-on-IPv6. This below may only happen
// if BOTH peers are IPv6. Otherwise we have a situation of cross-IP
// version connection in which case the address in question is always
// IPv4 in various mapping formats.
if (!is_mapped_ipv4)
{
// Here both agent and peer use IPv6, in which case
// `ip` contains the full IPv6 address, so just copy
// it as is.
std::memcpy(a->sin6_addr.s6_addr, ip, 16);
return; // The address is written, nothing left to do.
}
//
// IPv4 mapped on IPv6
// Here agent uses IPv6 with IPPROTO_IPV6/IPV6_V6ONLY == 0
// In this case, the address in `ip` is always an IPv4,
// although we are not certain as to whether it's using the
// IPv6 encoding (0::FFFF:IPv4) or SRT encoding (IPv4::0);
// this must be extra determined.
//
// Unfortunately, sockaddr_in6 doesn't give any straightforward
// method for it, although the official size of a single element
// of the IPv6 address is 16-bit.
memset((a->sin6_addr.s6_addr), 0, sizeof a->sin6_addr.s6_addr);
// The sin6_addr.s6_addr32 is non that portable to use here.
uint32_t* paddr32 = (uint32_t*)a->sin6_addr.s6_addr;
uint16_t* paddr16 = (uint16_t*)a->sin6_addr.s6_addr;
// layout: of IPv4 address 192.168.128.2
// 16-bit:
// [0000: 0000: 0000: 0000: 0000: FFFF: 192.168:128.2]
// 8-bit
// [00/00/00/00/00/00/00/00/00/00/FF/FF/192/168/128/2]
// 32-bit
// [00000000 && 00000000 && 0000FFFF && 192.168.128.2]
// Spreading every 16-bit word separately to avoid endian dilemmas
paddr16[2 * 2 + 1] = 0xFFFF;
target_ipv4_addr = &paddr32[3];
}
// Now we have two possible formats of encoding the IPv4 address:
// 1. If peer is IPv4, it's IPv4::0
// 2. If peer is IPv6, it's 0::FFFF:IPv4.
//
// Has any other possibility happen here, copy an empty address,
// which will be the only sign of an error.
const uint16_t* peeraddr16 = (uint16_t*)ip;
const bool is_mapped_ipv4 = checkMappedIPv4(peeraddr16);
if (is_mapped_ipv4)
{
*target_ipv4_addr = ip[3];
HLOGC(inlog.Debug, log << "pton: Handshake address: " << w_addr.str() << " provided in IPv6 mapping format");
}
// Check SRT IPv4 format.
else if ((ip[1] | ip[2] | ip[3]) == 0)
{
*target_ipv4_addr = ip[0];
HLOGC(inlog.Debug, log << "pton: Handshake address: " << w_addr.str() << " provided in SRT IPv4 format");
}
else
{
LOGC(inlog.Error, log << "pton: IPE or net error: can't determine IPv4 carryover format: " << std::hex
<< peeraddr16[0] << ":"
<< peeraddr16[1] << ":"
<< peeraddr16[2] << ":"
<< peeraddr16[3] << ":"
<< peeraddr16[4] << ":"
<< peeraddr16[5] << ":"
<< peeraddr16[6] << ":"
<< peeraddr16[7] << std::dec);
*target_ipv4_addr = 0;
if (peer.family() != AF_INET)
{
// Additionally overwrite the 0xFFFF that has been
// just written 50 lines above.
w_addr.sin6.sin6_addr.s6_addr[10] = 0;
w_addr.sin6.sin6_addr.s6_addr[11] = 0;
}
}
}
namespace srt {
static string ShowIP4(const sockaddr_in* sin)
{
ostringstream os;
union
{
in_addr sinaddr;
unsigned char ip[4];
};
sinaddr = sin->sin_addr;
os << int(ip[0]);
os << ".";
os << int(ip[1]);
os << ".";
os << int(ip[2]);
os << ".";
os << int(ip[3]);
return os.str();
}
static string ShowIP6(const sockaddr_in6* sin)
{
ostringstream os;
os.setf(ios::uppercase);
bool sep = false;
for (size_t i = 0; i < 16; ++i)
{
int v = sin->sin6_addr.s6_addr[i];
if ( v )
{
if ( sep )
os << ":";
os << hex << v;
sep = true;
}
}
return os.str();
}
string CIPAddress::show(const sockaddr* adr)
{
if ( adr->sa_family == AF_INET )
return ShowIP4((const sockaddr_in*)adr);
else if ( adr->sa_family == AF_INET6 )
return ShowIP6((const sockaddr_in6*)adr);
else
return "(unsupported sockaddr type)";
}
} // namespace srt
//
void srt::CMD5::compute(const char* input, unsigned char result[16])
{
md5_state_t state;
md5_init(&state);
md5_append(&state, (const md5_byte_t *)input, (int) strlen(input));
md5_finish(&state, result);
}
namespace srt {
std::string MessageTypeStr(UDTMessageType mt, uint32_t extt)
{
using std::string;
static const char* const udt_types [] = {
"handshake",
"keepalive",
"ack",
"lossreport",
"cgwarning", //4
"shutdown",
"ackack",
"dropreq",
"peererror", //8
};
static const char* const srt_types [] = {
"EXT:none",
"EXT:hsreq",
"EXT:hsrsp",
"EXT:kmreq",
"EXT:kmrsp",
"EXT:sid",
"EXT:congctl",
"EXT:filter",
"EXT:group"
};
if ( mt == UMSG_EXT )
{
if ( extt >= Size(srt_types) )
return "EXT:unknown";
return srt_types[extt];
}
if ( size_t(mt) > Size(udt_types) )
return "unknown";
return udt_types[mt];
}
std::string ConnectStatusStr(EConnectStatus cst)
{
return
cst == CONN_CONTINUE ? "INDUCED/CONCLUDING"
: cst == CONN_RUNNING ? "RUNNING"
: cst == CONN_ACCEPT ? "ACCEPTED"
: cst == CONN_RENDEZVOUS ? "RENDEZVOUS (HSv5)"
: cst == CONN_AGAIN ? "AGAIN"
: cst == CONN_CONFUSED ? "MISSING HANDSHAKE"
: "REJECTED";
}
std::string TransmissionEventStr(ETransmissionEvent ev)
{
static const char* const vals [] =
{
"init",
"ack",
"ackack",
"lossreport",
"checktimer",
"send",
"receive",
"custom",
"sync"
};
size_t vals_size = Size(vals);
if (size_t(ev) >= vals_size)
return "UNKNOWN";
return vals[ev];
}
bool SrtParseConfig(const string& s, SrtConfig& w_config)
{
using namespace std;
vector<string> parts;
Split(s, ',', back_inserter(parts));
if (parts.empty())
return false;
w_config.type = parts[0];
for (vector<string>::iterator i = parts.begin()+1; i != parts.end(); ++i)
{
vector<string> keyval;
Split(*i, ':', back_inserter(keyval));
if (keyval.size() != 2)
return false;
if (keyval[1] != "")
w_config.parameters[keyval[0]] = keyval[1];
}
return true;
}
std::string FormatLossArray(const std::vector< std::pair<int32_t, int32_t> >& lra)
{
std::ostringstream os;
os << "[ ";
for (std::vector< std::pair<int32_t, int32_t> >::const_iterator i = lra.begin(); i != lra.end(); ++i)
{
int len = CSeqNo::seqoff(i->first, i->second);
os << "%" << i->first;
if (len > 1)
os << "+" << len;
os << " ";
}
os << "]";
return os.str();
}
ostream& PrintEpollEvent(ostream& os, int events, int et_events)
{
static pair<int, const char*> const namemap [] = {
make_pair(SRT_EPOLL_IN, "R"),
make_pair(SRT_EPOLL_OUT, "W"),
make_pair(SRT_EPOLL_ERR, "E"),
make_pair(SRT_EPOLL_UPDATE, "U")
};
bool any = false;
const int N = (int)Size(namemap);
for (int i = 0; i < N; ++i)
{
if (events & namemap[i].first)
{
os << "[";
if (et_events & namemap[i].first)
os << "^";
os << namemap[i].second << "]";
any = true;
}
}
if (!any)
os << "[]";
return os;
}
} // namespace srt
namespace srt_logging
{
// Value display utilities
// (also useful for applications)
std::string SockStatusStr(SRT_SOCKSTATUS s)
{
if (int(s) < int(SRTS_INIT) || int(s) > int(SRTS_NONEXIST))
return "???";
static struct AutoMap
{
// Values start from 1, so do -1 to avoid empty cell
std::string names[int(SRTS_NONEXIST)-1+1];
AutoMap()
{
#define SINI(statename) names[SRTS_##statename-1] = #statename
SINI(INIT);
SINI(OPENED);
SINI(LISTENING);
SINI(CONNECTING);
SINI(CONNECTED);
SINI(BROKEN);
SINI(CLOSING);
SINI(CLOSED);
SINI(NONEXIST);
#undef SINI
}
} names;
return names.names[int(s)-1];
}
#if ENABLE_BONDING
std::string MemberStatusStr(SRT_MEMBERSTATUS s)
{
if (int(s) < int(SRT_GST_PENDING) || int(s) > int(SRT_GST_BROKEN))
return "???";
static struct AutoMap
{
std::string names[int(SRT_GST_BROKEN)+1];
AutoMap()
{
#define SINI(statename) names[SRT_GST_##statename] = #statename
SINI(PENDING);
SINI(IDLE);
SINI(RUNNING);
SINI(BROKEN);
#undef SINI
}
} names;
return names.names[int(s)];
}
#endif
} // (end namespace srt_logging)
|