| 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 01/27/2011 |
| 49 |
modified by |
| 50 |
Haivision Systems Inc. |
| 51 |
*****************************************************************************/ |
| 52 |
|
| 53 |
#include "platform_sys.h" |
| 54 |
#include <iostream> |
| 55 |
#include <iomanip> // Logging |
| 56 |
#include <srt_compat.h> |
| 57 |
#include <csignal> |
| 58 |
|
| 59 |
#include "channel.h" |
| 60 |
#include "core.h" // srt_logging:kmlog |
| 61 |
#include "packet.h" |
| 62 |
#include "logging.h" |
| 63 |
#include "netinet_any.h" |
| 64 |
#include "utilities.h" |
| 65 |
|
| 66 |
#ifdef _WIN32 |
| 67 |
typedef int socklen_t; |
| 68 |
#endif |
| 69 |
|
| 70 |
using namespace std; |
| 71 |
using namespace srt_logging; |
| 72 |
|
| 73 |
namespace srt |
| 74 |
{ |
| 75 |
|
| 76 |
#ifdef _WIN32 |
| 77 |
// use INVALID_SOCKET, as provided |
| 78 |
#else |
| 79 |
static const int INVALID_SOCKET = -1; |
| 80 |
#endif |
| 81 |
|
| 82 |
#if ENABLE_SOCK_CLOEXEC |
| 83 |
#ifndef _WIN32 |
| 84 |
|
| 85 |
#if defined(_AIX) || defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \ |
| 86 |
defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) |
| 87 |
|
| 88 |
// Set the CLOEXEC flag using ioctl() function |
| 89 |
static int set_cloexec(int fd, int set) |
| 90 |
{ |
| 91 |
int r; |
| 92 |
|
| 93 |
do |
| 94 |
r = ioctl(fd, set ? FIOCLEX : FIONCLEX); |
| 95 |
while (r == -1 && errno == EINTR); |
| 96 |
|
| 97 |
if (r) |
| 98 |
return errno; |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
#else |
| 103 |
// Set the CLOEXEC flag using fcntl() function |
| 104 |
static int set_cloexec(int fd, int set) |
| 105 |
{ |
| 106 |
int flags; |
| 107 |
int r; |
| 108 |
|
| 109 |
do |
| 110 |
r = fcntl(fd, F_GETFD); |
| 111 |
while (r == -1 && errno == EINTR); |
| 112 |
|
| 113 |
if (r == -1) |
| 114 |
return errno; |
| 115 |
|
| 116 |
/* Bail out now if already set/clear. */ |
| 117 |
if (!!(r & FD_CLOEXEC) == !!set) |
| 118 |
return 0; |
| 119 |
|
| 120 |
if (set) |
| 121 |
flags = r | FD_CLOEXEC; |
| 122 |
else |
| 123 |
flags = r & ~FD_CLOEXEC; |
| 124 |
|
| 125 |
do |
| 126 |
r = fcntl(fd, F_SETFD, flags); |
| 127 |
while (r == -1 && errno == EINTR); |
| 128 |
|
| 129 |
if (r) |
| 130 |
return errno; |
| 131 |
|
| 132 |
return 0; |
| 133 |
} |
| 134 |
#endif // if defined(_AIX) ... |
| 135 |
#endif // ifndef _WIN32 |
| 136 |
#endif // if ENABLE_CLOEXEC |
| 137 |
} // namespace srt |
| 138 |
|
| 139 |
srt::CChannel::CChannel() |
| 140 |
: m_iSocket(INVALID_SOCKET) |
| 141 |
#ifdef SRT_ENABLE_PKTINFO |
| 142 |
, m_bBindMasked(true) |
| 143 |
#endif |
| 144 |
{ |
| 145 |
#ifdef SRT_ENABLE_PKTINFO |
| 146 |
// Do the check for ancillary data buffer size, kinda assertion |
| 147 |
static const size_t CMSG_MAX_SPACE = sizeof (CMSGNodeIPv4) + sizeof (CMSGNodeIPv6); |
| 148 |
|
| 149 |
if (CMSG_MAX_SPACE < CMSG_SPACE(sizeof(in_pktinfo)) + CMSG_SPACE(sizeof(in6_pktinfo))) |
| 150 |
{ |
| 151 |
LOGC(kmlog.Fatal, log << "Size of CMSG_MAX_SPACE=" |
| 152 |
<< CMSG_MAX_SPACE << " too short for cmsg " |
| 153 |
<< CMSG_SPACE(sizeof(in_pktinfo)) << ", " |
| 154 |
<< CMSG_SPACE(sizeof(in6_pktinfo)) << " - PLEASE FIX"); |
| 155 |
throw CUDTException(MJ_SETUP, MN_NONE, 0); |
| 156 |
} |
| 157 |
#endif |
| 158 |
} |
| 159 |
|
| 160 |
srt::CChannel::~CChannel() {} |
| 161 |
|
| 162 |
void srt::CChannel::createSocket(int family) |
| 163 |
{ |
| 164 |
#if ENABLE_SOCK_CLOEXEC |
| 165 |
bool cloexec_flag = false; |
| 166 |
// construct an socket |
| 167 |
#if defined(SOCK_CLOEXEC) |
| 168 |
m_iSocket = ::socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); |
| 169 |
if (m_iSocket == INVALID_SOCKET) |
| 170 |
{ |
| 171 |
m_iSocket = ::socket(family, SOCK_DGRAM, IPPROTO_UDP); |
| 172 |
cloexec_flag = true; |
| 173 |
} |
| 174 |
#else |
| 175 |
m_iSocket = ::socket(family, SOCK_DGRAM, IPPROTO_UDP); |
| 176 |
cloexec_flag = true; |
| 177 |
#endif |
| 178 |
|
| 179 |
#else // ENABLE_SOCK_CLOEXEC |
| 180 |
m_iSocket = ::socket(family, SOCK_DGRAM, IPPROTO_UDP); |
| 181 |
#endif // ENABLE_SOCK_CLOEXEC |
| 182 |
|
| 183 |
if (m_iSocket == INVALID_SOCKET) |
| 184 |
throw CUDTException(MJ_SETUP, MN_NONE, NET_ERROR); |
| 185 |
|
| 186 |
#if ENABLE_SOCK_CLOEXEC |
| 187 |
|
| 188 |
if (cloexec_flag) |
| 189 |
{ |
| 190 |
#ifdef _WIN32 |
| 191 |
// XXX ::SetHandleInformation(hInputWrite, HANDLE_FLAG_INHERIT, 0) |
| 192 |
#else |
| 193 |
if (0 != set_cloexec(m_iSocket, 1)) |
| 194 |
{ |
| 195 |
throw CUDTException(MJ_SETUP, MN_NONE, NET_ERROR); |
| 196 |
} |
| 197 |
#endif //_WIN32 |
| 198 |
} |
| 199 |
#endif // ENABLE_SOCK_CLOEXEC |
| 200 |
|
| 201 |
if ((m_mcfg.iIpV6Only != -1) && (family == AF_INET6)) // (not an error if it fails) |
| 202 |
{ |
| 203 |
const int res SRT_ATR_UNUSED = |
| 204 |
::setsockopt(m_iSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&m_mcfg.iIpV6Only, sizeof m_mcfg.iIpV6Only); |
| 205 |
#if ENABLE_LOGGING |
| 206 |
if (res == -1) |
| 207 |
{ |
| 208 |
int err = errno; |
| 209 |
char msg[160]; |
| 210 |
LOGC(kmlog.Error, |
| 211 |
log << "::setsockopt: failed to set IPPROTO_IPV6/IPV6_V6ONLY = " << m_mcfg.iIpV6Only << ": " |
| 212 |
<< SysStrError(err, msg, 159)); |
| 213 |
} |
| 214 |
#endif // ENABLE_LOGGING |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void srt::CChannel::open(const sockaddr_any& addr) |
| 219 |
{ |
| 220 |
createSocket(addr.family()); |
| 221 |
socklen_t namelen = addr.size(); |
| 222 |
|
| 223 |
if (::bind(m_iSocket, &addr.sa, namelen) == -1) |
| 224 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 225 |
|
| 226 |
m_BindAddr = addr; |
| 227 |
#ifdef SRT_ENABLE_PKTINFO |
| 228 |
m_bBindMasked = m_BindAddr.isany(); |
| 229 |
#endif |
| 230 |
LOGC(kmlog.Debug, log << "CHANNEL: Bound to local address: " << m_BindAddr.str()); |
| 231 |
|
| 232 |
setUDPSockOpt(); |
| 233 |
} |
| 234 |
|
| 235 |
void srt::CChannel::open(int family) |
| 236 |
{ |
| 237 |
createSocket(family); |
| 238 |
|
| 239 |
// sendto or WSASendTo will also automatically bind the socket |
| 240 |
addrinfo hints; |
| 241 |
addrinfo* res; |
| 242 |
|
| 243 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 244 |
|
| 245 |
hints.ai_flags = AI_PASSIVE; |
| 246 |
hints.ai_family = family; |
| 247 |
hints.ai_socktype = SOCK_DGRAM; |
| 248 |
|
| 249 |
const int eai = ::getaddrinfo(NULL, "0", &hints, &res); |
| 250 |
if (eai != 0) |
| 251 |
{ |
| 252 |
// Controversial a little bit because this function occasionally |
| 253 |
// doesn't use errno (here: NET_ERROR for portability), instead |
| 254 |
// it returns 0 if succeeded or an error code. This error code |
| 255 |
// is passed here then. A controversy is around the fact that |
| 256 |
// the receiver of this error has completely no ability to know |
| 257 |
// what this error code's domain is, and it definitely isn't |
| 258 |
// the same as for errno. |
| 259 |
throw CUDTException(MJ_SETUP, MN_NORES, eai); |
| 260 |
} |
| 261 |
|
| 262 |
// On Windows ai_addrlen has type size_t (unsigned), while bind takes int. |
| 263 |
if (0 != ::bind(m_iSocket, res->ai_addr, (socklen_t)res->ai_addrlen)) |
| 264 |
{ |
| 265 |
::freeaddrinfo(res); |
| 266 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 267 |
} |
| 268 |
m_BindAddr = sockaddr_any(res->ai_addr, (sockaddr_any::len_t)res->ai_addrlen); |
| 269 |
|
| 270 |
#ifdef SRT_ENABLE_PKTINFO |
| 271 |
// We know that this is intentionally bound now to "any", |
| 272 |
// so the requester-destination address must be remembered and passed. |
| 273 |
m_bBindMasked = true; |
| 274 |
#endif |
| 275 |
|
| 276 |
::freeaddrinfo(res); |
| 277 |
|
| 278 |
HLOGC(kmlog.Debug, log << "CHANNEL: Bound to local address: " << m_BindAddr.str()); |
| 279 |
|
| 280 |
setUDPSockOpt(); |
| 281 |
} |
| 282 |
|
| 283 |
void srt::CChannel::attach(UDPSOCKET udpsock, const sockaddr_any& udpsocks_addr) |
| 284 |
{ |
| 285 |
// The getsockname() call is done before calling it and the |
| 286 |
// result is placed into udpsocks_addr. |
| 287 |
m_iSocket = udpsock; |
| 288 |
m_BindAddr = udpsocks_addr; |
| 289 |
setUDPSockOpt(); |
| 290 |
} |
| 291 |
|
| 292 |
static inline string fmt_opt(bool value, const string& label) |
| 293 |
{ |
| 294 |
string out; |
| 295 |
out.reserve(label.size() + 2); |
| 296 |
out = value ? "+" : "-"; |
| 297 |
out += label; |
| 298 |
return out; |
| 299 |
} |
| 300 |
|
| 301 |
static inline string fmt_alt(bool value, const string& label, const string& unlabel) |
| 302 |
{ |
| 303 |
return value ? label : unlabel; |
| 304 |
} |
| 305 |
|
| 306 |
void srt::CChannel::setUDPSockOpt() |
| 307 |
{ |
| 308 |
#if defined(SUNOS) |
| 309 |
{ |
| 310 |
socklen_t optSize; |
| 311 |
// Retrieve starting SND/RCV Buffer sizes. |
| 312 |
int startRCVBUF = 0; |
| 313 |
optSize = sizeof(startRCVBUF); |
| 314 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (void*)&startRCVBUF, &optSize)) |
| 315 |
{ |
| 316 |
startRCVBUF = -1; |
| 317 |
} |
| 318 |
int startSNDBUF = 0; |
| 319 |
optSize = sizeof(startSNDBUF); |
| 320 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, (void*)&startSNDBUF, &optSize)) |
| 321 |
{ |
| 322 |
startSNDBUF = -1; |
| 323 |
} |
| 324 |
|
| 325 |
// SunOS will fail setsockopt() if the requested buffer size exceeds system |
| 326 |
// maximum value. |
| 327 |
// However, do not reduce the buffer size. |
| 328 |
const int maxsize = 64000; |
| 329 |
if (-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, |
| 330 |
(const char*)&m_mcfg.iUDPRcvBufSize, sizeof m_mcfg.iUDPRcvBufSize)) |
| 331 |
{ |
| 332 |
int currentRCVBUF = 0; |
| 333 |
optSize = sizeof(currentRCVBUF); |
| 334 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (void*)¤tRCVBUF, &optSize)) |
| 335 |
{ |
| 336 |
currentRCVBUF = -1; |
| 337 |
} |
| 338 |
if (maxsize > currentRCVBUF) |
| 339 |
{ |
| 340 |
::setsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (const char*)&maxsize, sizeof maxsize); |
| 341 |
} |
| 342 |
} |
| 343 |
if (-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, |
| 344 |
(const char*)&m_mcfg.iUDPSndBufSize, sizeof m_mcfg.iUDPSndBufSize)) |
| 345 |
{ |
| 346 |
int currentSNDBUF = 0; |
| 347 |
optSize = sizeof(currentSNDBUF); |
| 348 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (void*)¤tSNDBUF, &optSize)) |
| 349 |
{ |
| 350 |
currentSNDBUF = -1; |
| 351 |
} |
| 352 |
if (maxsize > currentSNDBUF) |
| 353 |
{ |
| 354 |
::setsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, (const char*)&maxsize, sizeof maxsize); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// Retrieve ending SND/RCV Buffer sizes. |
| 359 |
int endRCVBUF = 0; |
| 360 |
optSize = sizeof(endRCVBUF); |
| 361 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (void*)&endRCVBUF, &optSize)) |
| 362 |
{ |
| 363 |
endRCVBUF = -1; |
| 364 |
} |
| 365 |
int endSNDBUF = 0; |
| 366 |
optSize = sizeof(endSNDBUF); |
| 367 |
if (-1 == ::getsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, (void*)&endSNDBUF, &optSize)) |
| 368 |
{ |
| 369 |
endSNDBUF = -1; |
| 370 |
} |
| 371 |
LOGC(kmlog.Debug, |
| 372 |
log << "SO_RCVBUF:" |
| 373 |
<< " startRCVBUF=" << startRCVBUF << " m_mcfg.iUDPRcvBufSize=" << m_mcfg.iUDPRcvBufSize |
| 374 |
<< " endRCVBUF=" << endRCVBUF); |
| 375 |
LOGC(kmlog.Debug, |
| 376 |
log << "SO_SNDBUF:" |
| 377 |
<< " startSNDBUF=" << startSNDBUF << " m_mcfg.iUDPSndBufSize=" << m_mcfg.iUDPSndBufSize |
| 378 |
<< " endSNDBUF=" << endSNDBUF); |
| 379 |
} |
| 380 |
#elif defined(BSD) || TARGET_OS_MAC |
| 381 |
// BSD system will fail setsockopt if the requested buffer size exceeds system maximum value |
| 382 |
int maxsize = 64000; |
| 383 |
if (-1 == ::setsockopt( |
| 384 |
m_iSocket, SOL_SOCKET, SO_RCVBUF, (const char*)&m_mcfg.iUDPRcvBufSize, sizeof m_mcfg.iUDPRcvBufSize)) |
| 385 |
::setsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (const char*)&maxsize, sizeof maxsize); |
| 386 |
if (-1 == ::setsockopt( |
| 387 |
m_iSocket, SOL_SOCKET, SO_SNDBUF, (const char*)&m_mcfg.iUDPSndBufSize, sizeof m_mcfg.iUDPSndBufSize)) |
| 388 |
::setsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, (const char*)&maxsize, sizeof maxsize); |
| 389 |
#else |
| 390 |
// for other systems, if requested is greater than maximum, the maximum value will be automatically used |
| 391 |
if ((-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, |
| 392 |
(const char*)&m_mcfg.iUDPRcvBufSize, sizeof m_mcfg.iUDPRcvBufSize)) |
| 393 |
|| |
| 394 |
(-1 == ::setsockopt( m_iSocket, SOL_SOCKET, SO_SNDBUF, |
| 395 |
(const char*)&m_mcfg.iUDPSndBufSize, sizeof m_mcfg.iUDPSndBufSize))) |
| 396 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 397 |
#endif |
| 398 |
|
| 399 |
bool is_set = false; |
| 400 |
bool adr_unspec = false, adr_mapped = false, adr_v6 = false; |
| 401 |
if (m_BindAddr.family() == AF_INET) |
| 402 |
{ |
| 403 |
adr_unspec = m_BindAddr.isany(); |
| 404 |
} |
| 405 |
else |
| 406 |
{ |
| 407 |
adr_unspec = IN6_IS_ADDR_UNSPECIFIED(&m_BindAddr.sin6.sin6_addr); |
| 408 |
adr_mapped = IN6_IS_ADDR_V4MAPPED(&m_BindAddr.sin6.sin6_addr); |
| 409 |
adr_v6 = true; |
| 410 |
} |
| 411 |
|
| 412 |
if (m_mcfg.iIpTTL != -1) |
| 413 |
{ |
| 414 |
if (!adr_v6) |
| 415 |
{ |
| 416 |
if (-1 == ::setsockopt(m_iSocket, IPPROTO_IP, IP_TTL, (const char*)&m_mcfg.iIpTTL, sizeof m_mcfg.iIpTTL)) |
| 417 |
{ |
| 418 |
LOGC(kmlog.Error, log << "setsockopt(IP_TTL): " << SysStrError(NET_ERROR)); |
| 419 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 420 |
} |
| 421 |
is_set = true; |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
// If IPv6 address is unspecified, set BOTH IP_TTL and IPV6_UNICAST_HOPS. |
| 426 |
|
| 427 |
// For specified IPv6 address, set IPV6_UNICAST_HOPS ONLY UNLESS it's an IPv4-mapped-IPv6 |
| 428 |
if (adr_unspec || !adr_mapped) |
| 429 |
{ |
| 430 |
if (-1 == ::setsockopt( m_iSocket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, |
| 431 |
(const char*)&m_mcfg.iIpTTL, sizeof m_mcfg.iIpTTL)) |
| 432 |
{ |
| 433 |
LOGC(kmlog.Error, log << "setsockopt(IPV6_UNICAST_HOPS): " << SysStrError(NET_ERROR)); |
| 434 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 435 |
} |
| 436 |
is_set = true; |
| 437 |
} |
| 438 |
// For specified IPv6 address, set IP_TTL ONLY WHEN it's an IPv4-mapped-IPv6 |
| 439 |
if (!is_set) // adr_mapped (because adr_unspec was handled above) |
| 440 |
{ |
| 441 |
if (-1 == ::setsockopt(m_iSocket, IPPROTO_IP, IP_TTL, (const char*)&m_mcfg.iIpTTL, sizeof m_mcfg.iIpTTL)) |
| 442 |
{ |
| 443 |
LOGC(kmlog.Error, log << "setsockopt(IP_TTL): " << SysStrError(NET_ERROR) |
| 444 |
<< fmt_alt(adr_unspec, " (v6 unspec)", " (v6 mapped v4)")); |
| 445 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 446 |
} |
| 447 |
is_set = true; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
if (!is_set) |
| 452 |
{ |
| 453 |
LOGC(kmlog.Error, log << "srt_setsockflag(SRTO_IPTTL): No suitable condition for adr=" << m_BindAddr.str() |
| 454 |
<< " : " << fmt_opt(adr_v6, "v6 ") << fmt_opt(adr_unspec, "unspec ") << fmt_opt(adr_mapped, "mapped")); |
| 455 |
throw CUDTException(MJ_SETUP, MN_INVAL, 0); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
is_set = false; |
| 460 |
if (m_mcfg.iIpToS != -1) |
| 461 |
{ |
| 462 |
if (m_BindAddr.family() == AF_INET) |
| 463 |
{ |
| 464 |
if (-1 == ::setsockopt(m_iSocket, IPPROTO_IP, IP_TOS, (const char*)&m_mcfg.iIpToS, sizeof m_mcfg.iIpToS)) |
| 465 |
{ |
| 466 |
LOGC(kmlog.Error, log << "setsockopt(IP_TOS): " << SysStrError(NET_ERROR)); |
| 467 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 468 |
} |
| 469 |
is_set = true; |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
// If IPv6 address is unspecified, set BOTH IP_TOS and IPV6_TCLASS. |
| 474 |
|
| 475 |
SRT_ATR_UNUSED bool using_tclass = false; |
| 476 |
#ifdef IPV6_TCLASS |
| 477 |
using_tclass = true; |
| 478 |
// For specified IPv6 address, set IPV6_TCLASS ONLY UNLESS it's an IPv4-mapped-IPv6 |
| 479 |
if (adr_unspec || !adr_mapped) |
| 480 |
{ |
| 481 |
if (-1 == ::setsockopt(m_iSocket, IPPROTO_IPV6, IPV6_TCLASS, |
| 482 |
(const char*)&m_mcfg.iIpToS, sizeof m_mcfg.iIpToS)) |
| 483 |
{ |
| 484 |
LOGC(kmlog.Error, log << "setsockopt(IPV6_TCLASS): " << SysStrError(NET_ERROR)); |
| 485 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 486 |
} |
| 487 |
is_set = true; |
| 488 |
} |
| 489 |
#endif |
| 490 |
|
| 491 |
// For specified IPv6 address, set IP_TOS ONLY WHEN it's an IPv4-mapped-IPv6 |
| 492 |
if (!is_set && (adr_unspec || adr_mapped)) |
| 493 |
{ |
| 494 |
if (-1 == ::setsockopt(m_iSocket, IPPROTO_IP, IP_TOS, (const char*)&m_mcfg.iIpToS, sizeof m_mcfg.iIpToS)) |
| 495 |
{ |
| 496 |
LOGC(kmlog.Error, log << "setsockopt(IP_TOS): " << SysStrError(NET_ERROR) |
| 497 |
<< (adr_unspec ? " (v6 unspecified)" : " (v6 mapped v4)") |
| 498 |
<< (using_tclass ? "(fallback to IP_TOS)" : "")); |
| 499 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 500 |
} |
| 501 |
is_set = true; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
if (!is_set) |
| 506 |
{ |
| 507 |
LOGC(kmlog.Error, log << "srt_setsockflag(SRTO_IPTOS): No suitable condition for adr=" << m_BindAddr.str() |
| 508 |
<< " : " << fmt_opt(adr_v6, "v6 ") << fmt_opt(adr_unspec, "unspec ") << fmt_opt(adr_mapped, "mapped")); |
| 509 |
throw CUDTException(MJ_SETUP, MN_INVAL, 0); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
#ifdef SRT_ENABLE_BINDTODEVICE |
| 515 |
if (!m_mcfg.sBindToDevice.empty()) |
| 516 |
{ |
| 517 |
if (m_BindAddr.family() != AF_INET) |
| 518 |
{ |
| 519 |
LOGC(kmlog.Error, log << "SRTO_BINDTODEVICE can only be set with AF_INET connections"); |
| 520 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 521 |
} |
| 522 |
|
| 523 |
if (-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_BINDTODEVICE, |
| 524 |
m_mcfg.sBindToDevice.c_str(), m_mcfg.sBindToDevice.size())) |
| 525 |
{ |
| 526 |
LOGC(kmlog.Error, log << "setsockopt(SRTO_BINDTODEVICE): " << SysStrError(NET_ERROR)); |
| 527 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 528 |
} |
| 529 |
} |
| 530 |
#endif |
| 531 |
|
| 532 |
#ifdef UNIX |
| 533 |
// Set non-blocking I/O |
| 534 |
// UNIX does not support SO_RCVTIMEO |
| 535 |
int opts = ::fcntl(m_iSocket, F_GETFL); |
| 536 |
if (-1 == ::fcntl(m_iSocket, F_SETFL, opts | O_NONBLOCK)) |
| 537 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 538 |
#elif defined(_WIN32) |
| 539 |
u_long nonBlocking = 1; |
| 540 |
if (-1 == ioctlsocket(m_iSocket, FIONBIO, &nonBlocking)) |
| 541 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 542 |
#else |
| 543 |
timeval tv; |
| 544 |
tv.tv_sec = 0; |
| 545 |
#if defined(BSD) || TARGET_OS_MAC |
| 546 |
// Known BSD bug as the day I wrote this code. |
| 547 |
// A small time out value will cause the socket to block forever. |
| 548 |
tv.tv_usec = 10000; |
| 549 |
#else |
| 550 |
tv.tv_usec = 100; |
| 551 |
#endif |
| 552 |
// Set receiving time-out value |
| 553 |
if (-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(timeval))) |
| 554 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 555 |
// Set sending time-out, too; O_NONBLOCK sets it in both directions, |
| 556 |
// so both should be also set here for consistency. |
| 557 |
if (-1 == ::setsockopt(m_iSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(timeval))) |
| 558 |
throw CUDTException(MJ_SETUP, MN_NORES, NET_ERROR); |
| 559 |
#endif |
| 560 |
|
| 561 |
#ifdef SRT_ENABLE_PKTINFO |
| 562 |
if (m_bBindMasked) |
| 563 |
{ |
| 564 |
HLOGP(kmlog.Debug, "Socket bound to ANY - setting PKTINFO for address retrieval"); |
| 565 |
const int on = 1, off SRT_ATR_UNUSED = 0; |
| 566 |
|
| 567 |
if (m_BindAddr.family() == AF_INET || m_mcfg.iIpV6Only == 0) |
| 568 |
{ |
| 569 |
::setsockopt(m_iSocket, IPPROTO_IP, IP_PKTINFO, (char*)&on, sizeof(on)); |
| 570 |
} |
| 571 |
|
| 572 |
if (m_BindAddr.family() == AF_INET6) |
| 573 |
{ |
| 574 |
::setsockopt(m_iSocket, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)); |
| 575 |
} |
| 576 |
|
| 577 |
// XXX Unknown why this has to be off. RETEST. |
| 578 |
//::setsockopt(m_iSocket, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off)); |
| 579 |
} |
| 580 |
#endif |
| 581 |
} |
| 582 |
|
| 583 |
void srt::CChannel::close() const |
| 584 |
{ |
| 585 |
#ifndef _WIN32 |
| 586 |
::close(m_iSocket); |
| 587 |
#else |
| 588 |
::closesocket(m_iSocket); |
| 589 |
#endif |
| 590 |
} |
| 591 |
|
| 592 |
int srt::CChannel::getSndBufSize() |
| 593 |
{ |
| 594 |
socklen_t size = (socklen_t)sizeof m_mcfg.iUDPSndBufSize; |
| 595 |
::getsockopt(m_iSocket, SOL_SOCKET, SO_SNDBUF, (char*)&m_mcfg.iUDPSndBufSize, &size); |
| 596 |
return m_mcfg.iUDPSndBufSize; |
| 597 |
} |
| 598 |
|
| 599 |
int srt::CChannel::getRcvBufSize() |
| 600 |
{ |
| 601 |
socklen_t size = (socklen_t)sizeof m_mcfg.iUDPRcvBufSize; |
| 602 |
::getsockopt(m_iSocket, SOL_SOCKET, SO_RCVBUF, (char*)&m_mcfg.iUDPRcvBufSize, &size); |
| 603 |
return m_mcfg.iUDPRcvBufSize; |
| 604 |
} |
| 605 |
|
| 606 |
void srt::CChannel::setConfig(const CSrtMuxerConfig& config) |
| 607 |
{ |
| 608 |
m_mcfg = config; |
| 609 |
} |
| 610 |
|
| 611 |
void srt::CChannel::getSocketOption(int level, int option, char* pw_dataptr, socklen_t& w_len, int& w_status) |
| 612 |
{ |
| 613 |
w_status = ::getsockopt(m_iSocket, level, option, (pw_dataptr), (&w_len)); |
| 614 |
} |
| 615 |
|
| 616 |
int srt::CChannel::getIpTTL() const |
| 617 |
{ |
| 618 |
if (m_iSocket == INVALID_SOCKET) |
| 619 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 620 |
|
| 621 |
socklen_t size = (socklen_t)sizeof m_mcfg.iIpTTL; |
| 622 |
if (m_BindAddr.family() == AF_INET) |
| 623 |
{ |
| 624 |
::getsockopt(m_iSocket, IPPROTO_IP, IP_TTL, (char*)&m_mcfg.iIpTTL, &size); |
| 625 |
} |
| 626 |
else if (m_BindAddr.family() == AF_INET6) |
| 627 |
{ |
| 628 |
::getsockopt(m_iSocket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (char*)&m_mcfg.iIpTTL, &size); |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
// If family is unspecified, the socket probably doesn't exist. |
| 633 |
LOGC(kmlog.Error, log << "IPE: CChannel::getIpTTL called with unset family"); |
| 634 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 635 |
} |
| 636 |
return m_mcfg.iIpTTL; |
| 637 |
} |
| 638 |
|
| 639 |
int srt::CChannel::getIpToS() const |
| 640 |
{ |
| 641 |
if (m_iSocket == INVALID_SOCKET) |
| 642 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 643 |
|
| 644 |
socklen_t size = (socklen_t)sizeof m_mcfg.iIpToS; |
| 645 |
if (m_BindAddr.family() == AF_INET) |
| 646 |
{ |
| 647 |
::getsockopt(m_iSocket, IPPROTO_IP, IP_TOS, (char*)&m_mcfg.iIpToS, &size); |
| 648 |
} |
| 649 |
else if (m_BindAddr.family() == AF_INET6) |
| 650 |
{ |
| 651 |
#ifdef IPV6_TCLASS |
| 652 |
::getsockopt(m_iSocket, IPPROTO_IPV6, IPV6_TCLASS, (char*)&m_mcfg.iIpToS, &size); |
| 653 |
#endif |
| 654 |
} |
| 655 |
else |
| 656 |
{ |
| 657 |
// If family is unspecified, the socket probably doesn't exist. |
| 658 |
LOGC(kmlog.Error, log << "IPE: CChannel::getIpToS called with unset family"); |
| 659 |
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0); |
| 660 |
} |
| 661 |
return m_mcfg.iIpToS; |
| 662 |
} |
| 663 |
|
| 664 |
#ifdef SRT_ENABLE_BINDTODEVICE |
| 665 |
bool srt::CChannel::getBind(char* dst, size_t len) |
| 666 |
{ |
| 667 |
if (m_iSocket == INVALID_SOCKET) |
| 668 |
return false; // No socket to get data from |
| 669 |
|
| 670 |
// Try to obtain it directly from the function. If not possible, |
| 671 |
// then return from internal data. |
| 672 |
socklen_t length = len; |
| 673 |
int res = ::getsockopt(m_iSocket, SOL_SOCKET, SO_BINDTODEVICE, dst, &length); |
| 674 |
if (res == -1) |
| 675 |
return false; // Happens on Linux v < 3.8 |
| 676 |
|
| 677 |
// For any case |
| 678 |
dst[length] = 0; |
| 679 |
return true; |
| 680 |
} |
| 681 |
#endif |
| 682 |
|
| 683 |
int srt::CChannel::ioctlQuery(int type SRT_ATR_UNUSED) const |
| 684 |
{ |
| 685 |
#if defined(unix) || defined(__APPLE__) |
| 686 |
int value = 0; |
| 687 |
int res = ::ioctl(m_iSocket, type, &value); |
| 688 |
if (res != -1) |
| 689 |
return value; |
| 690 |
#endif |
| 691 |
return -1; |
| 692 |
} |
| 693 |
|
| 694 |
int srt::CChannel::sockoptQuery(int level SRT_ATR_UNUSED, int option SRT_ATR_UNUSED) const |
| 695 |
{ |
| 696 |
#if defined(unix) || defined(__APPLE__) |
| 697 |
int value = 0; |
| 698 |
socklen_t len = sizeof(int); |
| 699 |
int res = ::getsockopt(m_iSocket, level, option, &value, &len); |
| 700 |
if (res != -1) |
| 701 |
return value; |
| 702 |
#endif |
| 703 |
return -1; |
| 704 |
} |
| 705 |
|
| 706 |
void srt::CChannel::getSockAddr(sockaddr_any& w_addr) const |
| 707 |
{ |
| 708 |
// The getsockname function requires only to have enough target |
| 709 |
// space to copy the socket name, it doesn't have to be correlated |
| 710 |
// with the address family. So the maximum space for any name, |
| 711 |
// regardless of the family, does the job. |
| 712 |
socklen_t namelen = (socklen_t)w_addr.storage_size(); |
| 713 |
::getsockname(m_iSocket, (w_addr.get()), (&namelen)); |
| 714 |
w_addr.len = namelen; |
| 715 |
} |
| 716 |
|
| 717 |
void srt::CChannel::getPeerAddr(sockaddr_any& w_addr) const |
| 718 |
{ |
| 719 |
socklen_t namelen = (socklen_t)w_addr.storage_size(); |
| 720 |
::getpeername(m_iSocket, (w_addr.get()), (&namelen)); |
| 721 |
w_addr.len = namelen; |
| 722 |
} |
| 723 |
|
| 724 |
int srt::CChannel::sendto(const sockaddr_any& addr, CPacket& packet, const sockaddr_any& source_addr SRT_ATR_UNUSED) const |
| 725 |
{ |
| 726 |
#if ENABLE_HEAVY_LOGGING |
| 727 |
ostringstream dsrc; |
| 728 |
#ifdef SRT_ENABLE_PKTINFO |
| 729 |
dsrc << " sourceIP=" << (m_bBindMasked && !source_addr.isany() ? source_addr.str() : "default"); |
| 730 |
#endif |
| 731 |
|
| 732 |
LOGC(kslog.Debug, |
| 733 |
log << "CChannel::sendto: SENDING NOW DST=" << addr.str() << " target=@" << packet.id() |
| 734 |
<< " size=" << packet.getLength() << " pkt.ts=" << packet.timestamp() |
| 735 |
<< dsrc.str() << " " << packet.Info()); |
| 736 |
#endif |
| 737 |
|
| 738 |
#ifdef SRT_TEST_FAKE_LOSS |
| 739 |
|
| 740 |
#define FAKELOSS_STRING_0(x) #x |
| 741 |
#define FAKELOSS_STRING(x) FAKELOSS_STRING_0(x) |
| 742 |
const char* fakeloss_text = FAKELOSS_STRING(SRT_TEST_FAKE_LOSS); |
| 743 |
#undef FAKELOSS_STRING |
| 744 |
#undef FAKELOSS_WRAP |
| 745 |
|
| 746 |
static int dcounter = 0; |
| 747 |
static int flwcounter = 0; |
| 748 |
|
| 749 |
struct FakelossConfig |
| 750 |
{ |
| 751 |
pair<int, int> config; |
| 752 |
FakelossConfig(const char* f) |
| 753 |
{ |
| 754 |
vector<string> out; |
| 755 |
Split(f, '+', back_inserter(out)); |
| 756 |
|
| 757 |
config.first = atoi(out[0].c_str()); |
| 758 |
config.second = out.size() > 1 ? atoi(out[1].c_str()) : 8; |
| 759 |
} |
| 760 |
}; |
| 761 |
static FakelossConfig fakeloss = fakeloss_text; |
| 762 |
|
| 763 |
if (!packet.isControl()) |
| 764 |
{ |
| 765 |
++dcounter; |
| 766 |
|
| 767 |
if (flwcounter) |
| 768 |
{ |
| 769 |
// This is a counter of how many packets in a row shall be lost |
| 770 |
--flwcounter; |
| 771 |
HLOGC(kslog.Debug, |
| 772 |
log << "CChannel: TEST: FAKE LOSS OF %" << packet.getSeqNo() << " (" << flwcounter |
| 773 |
<< " more to drop)"); |
| 774 |
return packet.getLength(); // fake successful sendinf |
| 775 |
} |
| 776 |
|
| 777 |
if (dcounter > 8) |
| 778 |
{ |
| 779 |
// Make a random number in the range between 8 and 24 |
| 780 |
const int rnd = srt::sync::genRandomInt(8, 24); |
| 781 |
|
| 782 |
if (dcounter > rnd) |
| 783 |
{ |
| 784 |
dcounter = 1; |
| 785 |
HLOGC(kslog.Debug, |
| 786 |
log << "CChannel: TEST: FAKE LOSS OF %" << packet.getSeqNo() << " (will drop " |
| 787 |
<< fakeloss.config.first << " more)"); |
| 788 |
flwcounter = fakeloss.config.first; |
| 789 |
return packet.getLength(); // fake successful sendinf |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
#endif |
| 795 |
|
| 796 |
// convert control information into network order |
| 797 |
packet.toNetworkByteOrder(); |
| 798 |
|
| 799 |
#ifndef _WIN32 |
| 800 |
msghdr mh; |
| 801 |
mh.msg_name = (sockaddr*)&addr; |
| 802 |
mh.msg_namelen = addr.size(); |
| 803 |
mh.msg_iov = (iovec*)packet.m_PacketVector; |
| 804 |
mh.msg_iovlen = 2; |
| 805 |
bool have_set_src = false; |
| 806 |
|
| 807 |
#ifdef SRT_ENABLE_PKTINFO |
| 808 |
|
| 809 |
// Note that even if PKTINFO is desired, the first caller's packet will be sent |
| 810 |
// without ancillary info anyway because there's no "peer" yet to know where to send it. |
| 811 |
char mh_crtl_buf[sizeof(CMSGNodeIPv4) + sizeof(CMSGNodeIPv6)]; |
| 812 |
if (m_bBindMasked && source_addr.family() != AF_UNSPEC && !source_addr.isany()) |
| 813 |
{ |
| 814 |
if (!setSourceAddress(mh, mh_crtl_buf, source_addr)) |
| 815 |
{ |
| 816 |
LOGC(kslog.Error, log << "CChannel::setSourceAddress: source address invalid family #" << source_addr.family() << ", NOT setting."); |
| 817 |
} |
| 818 |
else |
| 819 |
{ |
| 820 |
HLOGC(kslog.Debug, log << "CChannel::setSourceAddress: setting as " << source_addr.str()); |
| 821 |
have_set_src = true; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
#endif |
| 826 |
|
| 827 |
if (!have_set_src) |
| 828 |
{ |
| 829 |
mh.msg_control = NULL; |
| 830 |
mh.msg_controllen = 0; |
| 831 |
} |
| 832 |
mh.msg_flags = 0; |
| 833 |
|
| 834 |
const int res = (int)::sendmsg(m_iSocket, &mh, 0); |
| 835 |
#else |
| 836 |
class WSAEventRef |
| 837 |
{ |
| 838 |
public: |
| 839 |
WSAEventRef() |
| 840 |
: e(::WSACreateEvent()) |
| 841 |
{ |
| 842 |
} |
| 843 |
~WSAEventRef() |
| 844 |
{ |
| 845 |
::WSACloseEvent(e); |
| 846 |
e = NULL; |
| 847 |
} |
| 848 |
void reset() |
| 849 |
{ |
| 850 |
::WSAResetEvent(e); |
| 851 |
} |
| 852 |
WSAEVENT Handle() |
| 853 |
{ |
| 854 |
return e; |
| 855 |
} |
| 856 |
|
| 857 |
private: |
| 858 |
WSAEVENT e; |
| 859 |
}; |
| 860 |
#if !defined(__MINGW32__) && defined(ENABLE_CXX11) |
| 861 |
thread_local WSAEventRef lEvent; |
| 862 |
#else |
| 863 |
WSAEventRef lEvent; |
| 864 |
#endif |
| 865 |
WSAOVERLAPPED overlapped; |
| 866 |
::SecureZeroMemory(&overlapped, sizeof(overlapped)); |
| 867 |
overlapped.hEvent = lEvent.Handle(); |
| 868 |
|
| 869 |
DWORD size = (DWORD)(packet.m_PacketVector[0].size() + packet.m_PacketVector[1].size()); |
| 870 |
int addrsize = addr.size(); |
| 871 |
int res = ::WSASendTo(m_iSocket, (LPWSABUF)packet.m_PacketVector, 2, &size, 0, addr.get(), addrsize, &overlapped, NULL); |
| 872 |
|
| 873 |
if (res == SOCKET_ERROR) |
| 874 |
{ |
| 875 |
if (NET_ERROR == WSA_IO_PENDING) |
| 876 |
{ |
| 877 |
DWORD dwFlags = 0; |
| 878 |
const bool bCompleted = WSAGetOverlappedResult(m_iSocket, &overlapped, &size, TRUE, &dwFlags); |
| 879 |
if (bCompleted) |
| 880 |
res = 0; |
| 881 |
else |
| 882 |
LOGC(kslog.Warn, log << "CChannel::sendto call on ::WSAGetOverlappedResult failed with error: " << NET_ERROR); |
| 883 |
lEvent.reset(); |
| 884 |
} |
| 885 |
else |
| 886 |
{ |
| 887 |
LOGC(kmlog.Error, log << CONID() << "WSASendTo failed with error: " << NET_ERROR); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
res = (0 == res) ? size : -1; |
| 892 |
#endif |
| 893 |
|
| 894 |
packet.toHostByteOrder(); |
| 895 |
|
| 896 |
return res; |
| 897 |
} |
| 898 |
|
| 899 |
srt::EReadStatus srt::CChannel::recvfrom(sockaddr_any& w_addr, CPacket& w_packet) const |
| 900 |
{ |
| 901 |
EReadStatus status = RST_OK; |
| 902 |
int msg_flags = 0; |
| 903 |
int recv_size = -1; |
| 904 |
|
| 905 |
#if defined(UNIX) || defined(_WIN32) |
| 906 |
fd_set set; |
| 907 |
timeval tv; |
| 908 |
FD_ZERO(&set); |
| 909 |
FD_SET(m_iSocket, &set); |
| 910 |
tv.tv_sec = 0; |
| 911 |
tv.tv_usec = 10000; |
| 912 |
const int select_ret = ::select((int)m_iSocket + 1, &set, NULL, &set, &tv); |
| 913 |
#else |
| 914 |
const int select_ret = 1; // the socket is expected to be in the blocking mode itself |
| 915 |
#endif |
| 916 |
|
| 917 |
if (select_ret == 0) // timeout |
| 918 |
{ |
| 919 |
w_packet.setLength(-1); |
| 920 |
return RST_AGAIN; |
| 921 |
} |
| 922 |
|
| 923 |
#ifndef _WIN32 |
| 924 |
msghdr mh; // will not be used on failure |
| 925 |
|
| 926 |
#ifdef SRT_ENABLE_PKTINFO |
| 927 |
// This buffer is mounted inside mh so it must stay in the same scope |
| 928 |
char mh_crtl_buf[sizeof(CMSGNodeIPv4) + sizeof(CMSGNodeIPv6)]; |
| 929 |
#endif |
| 930 |
if (select_ret > 0) |
| 931 |
{ |
| 932 |
mh.msg_name = (w_addr.get()); |
| 933 |
mh.msg_namelen = w_addr.size(); |
| 934 |
mh.msg_iov = (w_packet.m_PacketVector); |
| 935 |
mh.msg_iovlen = 2; |
| 936 |
|
| 937 |
// Default |
| 938 |
mh.msg_control = NULL; |
| 939 |
mh.msg_controllen = 0; |
| 940 |
|
| 941 |
#ifdef SRT_ENABLE_PKTINFO |
| 942 |
// Without m_bBindMasked, we don't need ancillary data - the source |
| 943 |
// address will always be the bound address. |
| 944 |
if (m_bBindMasked) |
| 945 |
{ |
| 946 |
// Extract the destination IP address from the ancillary |
| 947 |
// data. This might be interesting for the connection to |
| 948 |
// know to which address the packet should be sent back during |
| 949 |
// the handshake and then addressed when sending during connection. |
| 950 |
mh.msg_control = (mh_crtl_buf); |
| 951 |
mh.msg_controllen = sizeof mh_crtl_buf; |
| 952 |
} |
| 953 |
#endif |
| 954 |
|
| 955 |
mh.msg_flags = 0; |
| 956 |
|
| 957 |
recv_size = (int)::recvmsg(m_iSocket, (&mh), 0); |
| 958 |
msg_flags = mh.msg_flags; |
| 959 |
} |
| 960 |
|
| 961 |
// Note that there are exactly four groups of possible errors |
| 962 |
// reported by recvmsg(): |
| 963 |
|
| 964 |
// 1. Temporary error, can't get the data, but you can try again. |
| 965 |
// Codes: EAGAIN/EWOULDBLOCK, EINTR, ECONNREFUSED |
| 966 |
// Return: RST_AGAIN. |
| 967 |
// |
| 968 |
// 2. Problems that should never happen due to unused configurations. |
| 969 |
// Codes: ECONNREFUSED, ENOTCONN |
| 970 |
// Return: RST_ERROR, just formally treat this as IPE. |
| 971 |
// |
| 972 |
// 3. Unexpected runtime errors: |
| 973 |
// Codes: EINVAL, EFAULT, ENOMEM, ENOTSOCK |
| 974 |
// Return: RST_ERROR. Except ENOMEM, this can only be an IPE. ENOMEM |
| 975 |
// should make the program stop as lacking memory will kill the program anyway soon. |
| 976 |
// |
| 977 |
// 4. Expected socket closed in the meantime by another thread. |
| 978 |
// Codes: EBADF |
| 979 |
// Return: RST_ERROR. This will simply make the worker thread exit, which is |
| 980 |
// expected to happen after CChannel::close() is called by another thread. |
| 981 |
|
| 982 |
// We do not handle <= SOCKET_ERROR as they are handled further by checking the recv_size |
| 983 |
if (select_ret == -1 || recv_size == -1) |
| 984 |
{ |
| 985 |
const int err = NET_ERROR; |
| 986 |
if (err == EAGAIN || err == EINTR || |
| 987 |
err == ECONNREFUSED) // For EAGAIN, this isn't an error, just a useless call. |
| 988 |
{ |
| 989 |
status = RST_AGAIN; |
| 990 |
} |
| 991 |
else |
| 992 |
{ |
| 993 |
HLOGC(krlog.Debug, log << CONID() << "(sys)recvmsg: " << SysStrError(err) << " [" << err << "]"); |
| 994 |
status = RST_ERROR; |
| 995 |
} |
| 996 |
|
| 997 |
goto Return_error; |
| 998 |
} |
| 999 |
|
| 1000 |
#ifdef SRT_ENABLE_PKTINFO |
| 1001 |
if (m_bBindMasked) |
| 1002 |
{ |
| 1003 |
// Extract the address. Set it explicitly; if this returns address that isany(), |
| 1004 |
// it will simply set this on the packet so that it behaves as if nothing was |
| 1005 |
// extracted (it will "fail the old way"). |
| 1006 |
w_packet.m_DestAddr = getTargetAddress(mh); |
| 1007 |
HLOGC(krlog.Debug, log << CONID() << "(sys)recvmsg: ANY BOUND, retrieved DEST ADDR: " << w_packet.m_DestAddr.str()); |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
|
| 1011 |
#else |
| 1012 |
// XXX REFACTORING NEEDED! |
| 1013 |
// This procedure uses the WSARecvFrom function that just reads |
| 1014 |
// into one buffer. On Windows, the equivalent for recvmsg, WSARecvMsg |
| 1015 |
// uses the equivalent of msghdr - WSAMSG, which has different field |
| 1016 |
// names and also uses the equivalet of iovec - WSABUF, which has different |
| 1017 |
// field names and layout. It is important that this code be translated |
| 1018 |
// to the "proper" solution, however this requires that CPacket::m_PacketVector |
| 1019 |
// also uses the "platform independent" (or, better, platform-suitable) type |
| 1020 |
// which can be appropriate for the appropriate system function, not just iovec |
| 1021 |
// (see a specifically provided definition for iovec for windows in packet.h). |
| 1022 |
// |
| 1023 |
// For the time being, the msg_flags variable is defined in both cases |
| 1024 |
// so that it can be checked independently, however it won't have any other |
| 1025 |
// value one Windows than 0, unless this procedure below is rewritten |
| 1026 |
// to use WSARecvMsg(). |
| 1027 |
|
| 1028 |
int recv_ret = SOCKET_ERROR; |
| 1029 |
DWORD flag = 0; |
| 1030 |
|
| 1031 |
if (select_ret > 0) // the total number of socket handles that are ready |
| 1032 |
{ |
| 1033 |
DWORD size = (DWORD)(CPacket::HDR_SIZE + w_packet.getLength()); |
| 1034 |
int addrsize = w_addr.size(); |
| 1035 |
|
| 1036 |
recv_ret = ::WSARecvFrom(m_iSocket, |
| 1037 |
((LPWSABUF)w_packet.m_PacketVector), |
| 1038 |
2, |
| 1039 |
(&size), |
| 1040 |
(&flag), |
| 1041 |
(w_addr.get()), |
| 1042 |
(&addrsize), |
| 1043 |
NULL, |
| 1044 |
NULL); |
| 1045 |
if (recv_ret == 0) |
| 1046 |
recv_size = size; |
| 1047 |
} |
| 1048 |
|
| 1049 |
// We do not handle <= SOCKET_ERROR as they are handled further by checking the recv_size |
| 1050 |
if (select_ret == SOCKET_ERROR || recv_ret == SOCKET_ERROR) // == SOCKET_ERROR |
| 1051 |
{ |
| 1052 |
recv_size = -1; |
| 1053 |
// On Windows this is a little bit more complicated, so simply treat every error |
| 1054 |
// as an "again" situation. This should still be probably fixed, but it needs more |
| 1055 |
// thorough research. For example, the problem usually reported from here is |
| 1056 |
// WSAETIMEDOUT, which isn't mentioned in the documentation of WSARecvFrom at all. |
| 1057 |
// |
| 1058 |
// These below errors are treated as "fatal", all others are treated as "again". |
| 1059 |
static const int fatals[] = {WSAEFAULT, WSAEINVAL, WSAENETDOWN, WSANOTINITIALISED, WSA_OPERATION_ABORTED}; |
| 1060 |
static const int* fatals_end = fatals + Size(fatals); |
| 1061 |
const int err = NET_ERROR; |
| 1062 |
if (std::find(fatals, fatals_end, err) != fatals_end) |
| 1063 |
{ |
| 1064 |
HLOGC(krlog.Debug, log << CONID() << "(sys)WSARecvFrom: " << SysStrError(err) << " [" << err << "]"); |
| 1065 |
status = RST_ERROR; |
| 1066 |
} |
| 1067 |
else |
| 1068 |
{ |
| 1069 |
status = RST_AGAIN; |
| 1070 |
} |
| 1071 |
|
| 1072 |
goto Return_error; |
| 1073 |
} |
| 1074 |
|
| 1075 |
// Not sure if this problem has ever occurred on Windows, just a sanity check. |
| 1076 |
if (flag & MSG_PARTIAL) |
| 1077 |
msg_flags = 1; |
| 1078 |
#endif |
| 1079 |
|
| 1080 |
// Sanity check for a case when it didn't fill in even the header |
| 1081 |
if (size_t(recv_size) < CPacket::HDR_SIZE) |
| 1082 |
{ |
| 1083 |
status = RST_AGAIN; |
| 1084 |
HLOGC(krlog.Debug, |
| 1085 |
log << CONID() << "POSSIBLE ATTACK: received too short packet with " << recv_size << " bytes"); |
| 1086 |
goto Return_error; |
| 1087 |
} |
| 1088 |
|
| 1089 |
// Fix for an issue with Linux Kernel found during tests at Tencent. |
| 1090 |
// |
| 1091 |
// There was a bug in older Linux Kernel which caused that when the internal |
| 1092 |
// buffer was depleted during reading from the network, not the whole buffer |
| 1093 |
// was copied from the packet, EVEN THOUGH THE GIVEN BUFFER WAS OF ENOUGH SIZE. |
| 1094 |
// It was still very kind of the buggy procedure, though, that at least |
| 1095 |
// they inform the caller about that this has happened by setting MSG_TRUNC |
| 1096 |
// flag. |
| 1097 |
// |
| 1098 |
// Normally this flag should be set only if there was too small buffer given |
| 1099 |
// by the caller, so as this code knows that the size is enough, it never |
| 1100 |
// predicted this to happen. Just for a case then when you run this on a buggy |
| 1101 |
// system that suffers of this problem, the fix for this case is left here. |
| 1102 |
// |
| 1103 |
// When this happens, then you have at best a fragment of the buffer and it's |
| 1104 |
// useless anyway. This is solved by dropping the packet and fake that no |
| 1105 |
// packet was received, so the packet will be then retransmitted. |
| 1106 |
if (msg_flags != 0) |
| 1107 |
{ |
| 1108 |
#if ENABLE_HEAVY_LOGGING |
| 1109 |
|
| 1110 |
std::ostringstream flg; |
| 1111 |
|
| 1112 |
#if !defined(_WIN32) |
| 1113 |
|
| 1114 |
static const pair<int, const char* const> errmsgflg [] = { |
| 1115 |
make_pair<int>(MSG_OOB, "OOB"), |
| 1116 |
make_pair<int>(MSG_EOR, "EOR"), |
| 1117 |
make_pair<int>(MSG_TRUNC, "TRUNC"), |
| 1118 |
make_pair<int>(MSG_CTRUNC, "CTRUNC") |
| 1119 |
}; |
| 1120 |
|
| 1121 |
for (size_t i = 0; i < Size(errmsgflg); ++i) |
| 1122 |
if ((msg_flags & errmsgflg[i].first) != 0) |
| 1123 |
flg << " " << errmsgflg[i].second; |
| 1124 |
|
| 1125 |
if (msg_flags & MSG_TRUNC) |
| 1126 |
{ |
| 1127 |
// Additionally show buffer information in this case |
| 1128 |
flg << " buffers: "; |
| 1129 |
for (size_t i = 0; i < CPacket::PV_SIZE; ++i) |
| 1130 |
{ |
| 1131 |
flg << "[" << w_packet.m_PacketVector[i].iov_len << "] "; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
// This doesn't work the same way on Windows, so on Windows just skip it. |
| 1135 |
#endif |
| 1136 |
|
| 1137 |
HLOGC(krlog.Debug, |
| 1138 |
log << CONID() << "NET ERROR: packet size=" << recv_size << " msg_flags=0x" << hex << msg_flags |
| 1139 |
<< ", detected flags:" << flg.str()); |
| 1140 |
#endif |
| 1141 |
status = RST_AGAIN; |
| 1142 |
goto Return_error; |
| 1143 |
} |
| 1144 |
|
| 1145 |
w_packet.setLength(recv_size - CPacket::HDR_SIZE); |
| 1146 |
w_packet.toHostByteOrder(); |
| 1147 |
|
| 1148 |
return RST_OK; |
| 1149 |
|
| 1150 |
Return_error: |
| 1151 |
w_packet.setLength(-1); |
| 1152 |
return status; |
| 1153 |
} |