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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
#include <thread>
#include <future>
#include <sstream>
#ifndef _WIN32
#include <ifaddrs.h>
#endif
#include "gtest/gtest.h"
#include "test_env.h"
#include "common.h"
#include "srt.h"
#include "udt.h"
using srt::sockaddr_any;
template<typename Future>
struct AtReturnJoin
{
Future& which_future;
~AtReturnJoin()
{
if (which_future.valid())
which_future.wait();
}
};
#ifdef _WIN32
// On Windows there's a function for it, but it requires an extra
// iphlp library to be attached to the executable, which is kinda
// problematic. Temporarily block tests using this function on Windows.
static std::string GetLocalIP(int af = AF_UNSPEC)
{
std::cout << "!!!WARNING!!!: GetLocalIP not supported, test FORCEFULLY passed\n";
return "";
}
#else
struct IfAddr
{
ifaddrs* handle;
IfAddr()
{
getifaddrs(&handle);
}
~IfAddr()
{
freeifaddrs(handle);
}
};
static std::string GetLocalIP(int af = AF_UNSPEC)
{
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
IfAddr ifAddr;
for (ifa = ifAddr.handle; ifa != NULL; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr)
{
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET)
{
// Ignore IPv4 address if not wanted.
if (af == AF_INET6)
continue;
// is a valid IP4 Address
sockaddr_in* psin = (struct sockaddr_in *)ifa->ifa_addr;
tmpAddrPtr=&psin->sin_addr;
if (ntohl(psin->sin_addr.s_addr) == 0x7f000001) // Skip 127.0.0.1
continue;
char addressBuffer[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
return addressBuffer;
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{ // check it is IP6
// Ignore IPv6 address if not wanted.
if (af == AF_INET)
continue;
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN] = "";
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
return addressBuffer;
}
}
return "";
}
#endif
class ReuseAddr : public srt::Test
{
protected:
std::string showEpollContents(const char* label, int* array, int length)
{
std::ostringstream out;
out << label << ":[";
if (length)
{
// Now is at least 1
out << "@" << array[0];
for (int i = 1; i < length; ++i)
out << " @" << array[i];
}
out << "]";
return out.str();
}
struct UniquePollid
{
int pollid = SRT_ERROR;
UniquePollid()
{
pollid = srt_epoll_create();
}
~UniquePollid()
{
srt_epoll_release(pollid);
}
operator int() const
{
return pollid;
}
};
void clientSocket(SRTSOCKET client_sock, std::string ip, int port, bool expect_success)
{
using namespace std;
int yes = 1;
int no = 0;
int family = AF_INET;
string famname = "IPv4";
if (ip.substr(0, 2) == "6.")
{
family = AF_INET6;
ip = ip.substr(2);
famname = "IPv6";
}
cout << "[T/C] Setting up client socket\n";
ASSERT_NE(client_sock, SRT_INVALID_SOCK);
ASSERT_EQ(srt_getsockstate(client_sock), SRTS_INIT);
EXPECT_NE(srt_setsockflag(client_sock, SRTO_SNDSYN, &no, sizeof no), SRT_ERROR); // for async connect
EXPECT_NE(srt_setsockflag(client_sock, SRTO_SENDER, &yes, sizeof yes), SRT_ERROR);
EXPECT_NE(srt_setsockflag(client_sock, SRTO_TSBPDMODE, &yes, sizeof yes), SRT_ERROR);
UniquePollid client_pollid;
ASSERT_NE(int(client_pollid), SRT_ERROR);
int epoll_out = SRT_EPOLL_OUT;
srt_epoll_add_usock(client_pollid, client_sock, &epoll_out);
sockaddr_any sa = srt::CreateAddr(ip, port, family);
cout << "[T/C] Connecting to: " << sa.str() << " (" << famname << ")" << endl;
int connect_res = srt_connect(client_sock, sa.get(), sa.size());
if (connect_res == -1)
{
cout << "srt_connect: " << srt_getlasterror_str() << endl;
}
if (expect_success)
{
EXPECT_NE(connect_res, -1);
if (connect_res == -1)
return;
// Socket readiness for connection is checked by polling on WRITE allowed sockets.
if (connect_res != -1)
{
int rlen = 2;
SRTSOCKET read[2];
int wlen = 2;
SRTSOCKET write[2];
cout << "[T/C] Waiting for connection readiness...\n";
EXPECT_NE(srt_epoll_wait(client_pollid, read, &rlen,
write, &wlen,
-1, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();
EXPECT_EQ(rlen, 0) << showEpollContents("[T/C] R", read, rlen); // get exactly one write event without reads
EXPECT_EQ(wlen, 1) << showEpollContents("[T/C] W", write, wlen); // get exactly one write event without reads
EXPECT_EQ(write[0], client_sock); // for our client socket
char buffer[1316] = {1, 2, 3, 4};
EXPECT_NE(srt_sendmsg(client_sock, buffer, sizeof buffer,
-1, // infinit ttl
true // in order must be set to true
),
SRT_ERROR);
}
else
{
cout << "[T/C] (NOT TESTING TRANSMISSION - CONNECTION FAILED ALREADY)\n";
}
}
else
{
EXPECT_EQ(connect_res, -1);
}
cout << "[T/C] Client exit\n";
}
SRTSOCKET prepareServerSocket()
{
SRTSOCKET bindsock = srt_create_socket();
EXPECT_NE(bindsock, SRT_ERROR);
int yes = 1;
int no = 0;
EXPECT_NE(srt_setsockopt(bindsock, 0, SRTO_RCVSYN, &no, sizeof no), SRT_ERROR); // for async connect
EXPECT_NE(srt_setsockopt(bindsock, 0, SRTO_TSBPDMODE, &yes, sizeof yes), SRT_ERROR);
return bindsock;
}
bool bindSocket(SRTSOCKET bindsock, std::string ip, int port, bool expect_success)
{
sockaddr_any sa = srt::CreateAddr(ip, port, AF_INET);
std::string fam = (sa.family() == AF_INET) ? "IPv4" : "IPv6";
std::cout << "[T/S] Bind @" << bindsock << " to: " << sa.str() << " (" << fam << ")" << std::endl;
int bind_res = srt_bind(bindsock, sa.get(), sa.size());
std::cout << "[T/S] ... result " << bind_res << " (expected to "
<< (expect_success ? "succeed" : "fail") << ")\n";
if (!expect_success)
{
std::cout << "[T/S] Binding should fail: " << srt_getlasterror_str() << std::endl;
EXPECT_EQ(bind_res, SRT_ERROR);
return false;
}
EXPECT_NE(bind_res, SRT_ERROR);
return true;
}
bool bindListener(SRTSOCKET bindsock, std::string ip, int port, bool expect_success)
{
if (!bindSocket(bindsock, ip, port, expect_success))
return false;
EXPECT_NE(srt_listen(bindsock, SOMAXCONN), SRT_ERROR);
return true;
}
SRTSOCKET createListener(std::string ip, int port, bool expect_success)
{
std::cout << "[T/S] serverSocket: creating listener socket\n";
SRTSOCKET bindsock = prepareServerSocket();
if (!bindListener(bindsock, ip, port, expect_success))
return SRT_INVALID_SOCK;
return bindsock;
}
SRTSOCKET createBinder(std::string ip, int port, bool expect_success)
{
std::cout << "[T/S] serverSocket: creating binder socket\n";
SRTSOCKET bindsock = prepareServerSocket();
if (!bindSocket(bindsock, ip, port, expect_success))
{
srt_close(bindsock);
return SRT_INVALID_SOCK;
}
return bindsock;
}
void testAccept(SRTSOCKET bindsock, std::string ip, int port, bool expect_success)
{
MAKE_UNIQUE_SOCK(client_sock, "[T/S]connect", srt_create_socket());
auto run = [this, &client_sock, ip, port, expect_success]() { clientSocket(client_sock, ip, port, expect_success); };
auto launched = std::async(std::launch::async, run);
AtReturnJoin<decltype(launched)> atreturn_join {launched};
int server_pollid = srt_epoll_create();
int epoll_in = SRT_EPOLL_IN;
std::cout << "[T/S] Listener/binder sock @" << bindsock << " added to server_pollid\n";
srt_epoll_add_usock(server_pollid, bindsock, &epoll_in);
{ // wait for connection from client
int rlen = 2;
SRTSOCKET read[2];
int wlen = 2;
SRTSOCKET write[2];
std::cout << "[T/S] Wait 10s on E" << server_pollid << " for acceptance on @" << bindsock << " ...\n";
EXPECT_NE(srt_epoll_wait(server_pollid,
read, &rlen,
write, &wlen,
10000, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();
EXPECT_EQ(rlen, 1) << showEpollContents("[T/S] R", read, rlen); // get exactly one read event without writes
EXPECT_EQ(wlen, 0) << showEpollContents("[T/S] W", write, wlen); // get exactly one read event without writes
ASSERT_EQ(read[0], bindsock); // read event is for bind socket
}
{
sockaddr_any scl;
MAKE_UNIQUE_SOCK(accepted_sock, "[T/S]accept", srt_accept(bindsock, scl.get(), &scl.len));
if (accepted_sock == -1)
{
std::cout << "srt_accept: " << srt_getlasterror_str() << std::endl;
}
EXPECT_NE(accepted_sock.ref(), SRT_INVALID_SOCK);
sockaddr_any showacp = (sockaddr*)&scl;
std::cout << "[T/S] Accepted from: " << showacp.str() << std::endl;
epoll_in = SRT_EPOLL_IN;
srt_epoll_add_usock(server_pollid, accepted_sock, &epoll_in); // wait for input
char buffer[1316];
{ // wait for 1316 packet from client
int rlen = 2;
SRTSOCKET read[2];
int wlen = 2;
SRTSOCKET write[2];
std::cout << "[T/S] Wait for data reception...\n";
EXPECT_NE(srt_epoll_wait(server_pollid,
read, &rlen,
write, &wlen,
-1, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();
EXPECT_EQ(rlen, 1) << showEpollContents("[T/S] R", read, rlen); // get exactly one read event without writes
EXPECT_EQ(wlen, 0) << showEpollContents("[T/S] W", write, wlen); // get exactly one read event without writes
EXPECT_EQ(read[0], accepted_sock.ref()); // read event is for bind socket
}
char pattern[4] = {1, 2, 3, 4};
EXPECT_EQ(srt_recvmsg(accepted_sock, buffer, sizeof buffer),
1316);
EXPECT_EQ(memcmp(pattern, buffer, sizeof pattern), 0);
// XXX There is a possibility that a broken socket can be closed automatically,
// just the srt_close() call would simply return error in case of nonexistent
// socket. Therefore close them both at once; this problem needs to be fixed
// separately.
//
// The test only intends to send one portion of data from the client, so once
// received, the client has nothing more to do and should exit.
std::cout << "[T/S] closing client socket\n";
client_sock.close();
std::cout << "[T/S] closing sockets: ACP:@" << accepted_sock << "...\n";
}
srt_epoll_release(server_pollid);
// client_sock closed through UniqueSocket.
// cannot close client_sock after srt_sendmsg because of issue in api.c:2346
std::cout << "[T/S] joining client async \n";
launched.get();
}
static void shutdownListener(SRTSOCKET bindsock)
{
// Silently ignore. Usually it should have been checked earlier,
// and an invalid sock might be expected in particular tests.
if (bindsock == SRT_INVALID_SOCK)
return;
int yes = 1;
EXPECT_NE(srt_setsockopt(bindsock, 0, SRTO_RCVSYN, &yes, sizeof yes), SRT_ERROR); // for async connect
EXPECT_NE(srt_close(bindsock), SRT_ERROR);
std::chrono::milliseconds check_period (100);
int credit = 400; // 10 seconds
auto then = std::chrono::steady_clock::now();
std::cout << "[T/S] waiting for cleanup of @" << bindsock << " up to 10s" << std::endl;
while (srt_getsockstate(bindsock) != SRTS_NONEXIST)
{
std::this_thread::sleep_for(check_period);
--credit;
if (!credit)
break;
}
auto now = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(now - then);
// Keep as single string because this tends to be mixed from 2 threads.
std::ostringstream sout;
sout << "[T/S] @" << bindsock << " dissolved after "
<< (dur.count() / 1000.0) << "s" << std::endl;
std::cout << sout.str() << std::flush;
EXPECT_NE(credit, 0);
}
private:
void setup()
{
}
void teardown()
{
}
};
TEST_F(ReuseAddr, SameAddr1)
{
SRTSOCKET bindsock_1 = createBinder("127.0.0.1", 5000, true);
SRTSOCKET bindsock_2 = createListener("127.0.0.1", 5000, true);
testAccept(bindsock_2, "127.0.0.1", 5000, true);
std::thread s1(shutdownListener, bindsock_1);
std::thread s2(shutdownListener, bindsock_2);
s1.join();
s2.join();
}
TEST_F(ReuseAddr, SameAddr2)
{
std::string localip = GetLocalIP(AF_INET);
if (localip == "")
return; // DISABLE TEST if this doesn't work.
SRTSOCKET bindsock_1 = createBinder(localip, 5000, true);
SRTSOCKET bindsock_2 = createListener(localip, 5000, true);
testAccept(bindsock_2, localip, 5000, true);
shutdownListener(bindsock_1);
// Test simple close and reuse the multiplexer
ASSERT_NE(srt_close(bindsock_2), SRT_ERROR);
SRTSOCKET bindsock_3 = createListener(localip, 5000, true);
testAccept(bindsock_3, localip, 5000, true);
shutdownListener(bindsock_3);
}
TEST_F(ReuseAddr, SameAddrV6)
{
SRTST_REQUIRES(IPv6);
SRTSOCKET bindsock_1 = createBinder("::1", 5000, true);
SRTSOCKET bindsock_2 = createListener("::1", 5000, true);
testAccept(bindsock_2, "::1", 5000, true);
shutdownListener(bindsock_1);
// Test simple close and reuse the multiplexer
ASSERT_NE(srt_close(bindsock_2), SRT_ERROR);
SRTSOCKET bindsock_3 = createListener("::1", 5000, true);
testAccept(bindsock_3, "::1", 5000, true);
shutdownListener(bindsock_3);
}
TEST_F(ReuseAddr, DiffAddr)
{
std::string localip = GetLocalIP(AF_INET);
if (localip == "")
return; // DISABLE TEST if this doesn't work.
SRTSOCKET bindsock_1 = createBinder("127.0.0.1", 5000, true);
SRTSOCKET bindsock_2 = createListener(localip, 5000, true);
//std::thread server_1(testAccept, bindsock_1, "127.0.0.1", 5000, true);
//server_1.join();
testAccept(bindsock_2, localip, 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
}
TEST_F(ReuseAddr, Wildcard)
{
#if defined(_WIN32) || defined(CYGWIN)
std::cout << "!!!WARNING!!!: On Windows connection to localhost this way isn't possible.\n"
"Forcing test to pass, PLEASE FIX.\n";
return;
#endif
// This time exceptionally require IPv4 because we'll be
// checking it against 0.0.0.0
std::string localip = GetLocalIP(AF_INET);
if (localip == "")
return; // DISABLE TEST if this doesn't work.
SRTSOCKET bindsock_1 = createListener("0.0.0.0", 5000, true);
// Binding a certain address when wildcard is already bound should fail.
SRTSOCKET bindsock_2 = createBinder(localip, 5000, false);
testAccept(bindsock_1, "127.0.0.1", 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
}
TEST_F(ReuseAddr, Wildcard6)
{
SRTST_REQUIRES(IPv6);
#if defined(_WIN32) || defined(CYGWIN)
std::cout << "!!!WARNING!!!: On Windows connection to localhost this way isn't possible.\n"
"Forcing test to pass, PLEASE FIX.\n";
return;
#endif
// This time exceptionally require IPv6 because we'll be
// checking it against ::
std::string localip = GetLocalIP(AF_INET6);
if (localip == "")
return; // DISABLE TEST if this doesn't work.
// This "should work", but there can also be platforms
// that do not have IPv4, in which case this test can't be
// performed there.
std::string localip_v4 = GetLocalIP(AF_INET);
// This must be obligatory set before binding a socket to "::"
int strict_ipv6 = 1;
SRTSOCKET bindsock_1 = prepareServerSocket();
srt_setsockflag(bindsock_1, SRTO_IPV6ONLY, &strict_ipv6, sizeof strict_ipv6);
bindListener(bindsock_1, "::", 5000, true);
// Binding a certain address when wildcard is already bound should fail.
SRTSOCKET bindsock_2 = createBinder(localip, 5000, false);
SRTSOCKET bindsock_3 = SRT_INVALID_SOCK;
if (localip_v4 != "")
{
// V6ONLY = 1, which means that binding to IPv4 should be possible.
bindsock_3 = createBinder(localip_v4, 5000, true);
}
testAccept(bindsock_1, "::1", 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
shutdownListener(bindsock_3);
// Now the same thing, except that we bind to both IPv4 and IPv6.
strict_ipv6 = 0;
bindsock_1 = prepareServerSocket();
srt_setsockflag(bindsock_1, SRTO_IPV6ONLY, &strict_ipv6, sizeof strict_ipv6);
bindListener(bindsock_1, "::", 5000, true);
// Binding a certain address when wildcard is already bound should fail.
bindsock_2 = createBinder(localip, 5000, false);
bindsock_3 = SRT_INVALID_SOCK;
if (localip_v4 != "")
{
// V6ONLY = 0, which means that binding to IPv4 should not be possible.
bindsock_3 = createBinder(localip_v4, 5000, false);
}
testAccept(bindsock_1, "::1", 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
shutdownListener(bindsock_3);
}
TEST_F(ReuseAddr, ProtocolVersion6)
{
SRTST_REQUIRES(IPv6);
#if defined(_WIN32) || defined(CYGWIN)
std::cout << "!!!WARNING!!!: On Windows connection to localhost this way isn't possible.\n"
"Forcing test to pass, PLEASE FIX.\n";
return;
#endif
SRTSOCKET bindsock_1 = createListener("0.0.0.0", 5000, true);
// We need a small interception in this one.
// createListener = prepareServerSocket | bindListener
SRTSOCKET bindsock_2 = prepareServerSocket();
{
int yes = 1;
ASSERT_NE(srt_setsockflag(bindsock_2, SRTO_IPV6ONLY, &yes, sizeof yes), SRT_ERROR);
ASSERT_TRUE(bindListener(bindsock_2, "::", 5000, true));
}
testAccept(bindsock_1, "127.0.0.1", 5000, true);
testAccept(bindsock_2, "::1", 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
}
TEST_F(ReuseAddr, ProtocolVersionFaux6)
{
SRTST_REQUIRES(IPv6);
#if defined(_WIN32) || defined(CYGWIN)
std::cout << "!!!WARNING!!!: On Windows connection to localhost this way isn't possible.\n"
"Forcing test to pass, PLEASE FIX.\n";
return;
#endif
SRTSOCKET bindsock_1 = createListener("0.0.0.0", 5000, true);
// We need a small interception in this one.
// createListener = prepareServerSocket | bindListener
SRTSOCKET bindsock_2 = prepareServerSocket();
{
int no = 0;
ASSERT_NE(srt_setsockflag(bindsock_2, SRTO_IPV6ONLY, &no, sizeof no), SRT_ERROR);
ASSERT_FALSE(bindListener(bindsock_2, "::", 5000, false));
}
testAccept(bindsock_1, "127.0.0.1", 5000, true);
shutdownListener(bindsock_1);
shutdownListener(bindsock_2);
}
|