Skip to content

Commit

Permalink
[core] Fixed connected peer address recording (#2526)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethouris authored Nov 9, 2022
1 parent b76c8b2 commit f477b51
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,8 @@ int srt::CUDTUnited::connectIn(CUDTSocket* s, const sockaddr_any& target_addr, i
*/
try
{
// record peer address
s->m_PeerAddr = target_addr;
s->core().startConnect(target_addr, forced_isn);
}
catch (const CUDTException&) // Interceptor, just to change the state.
Expand Down
1 change: 1 addition & 0 deletions test/filelist.maf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test_timer.cpp
test_unitqueue.cpp
test_utilities.cpp
test_reuseaddr.cpp
test_socketdata.cpp

# Tests for bonding only - put here!

Expand Down
67 changes: 67 additions & 0 deletions test/test_socketdata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#include <chrono>
#include <thread>
#include <iostream>

#include "gtest/gtest.h"

#include "srt.h"
#include "netinet_any.h"
#include "apputil.hpp"

using namespace std;
using namespace std::chrono;
using namespace srt;

TEST(SocketData, PeerName)
{
// Single-threaded one-app connect/accept action

int csock = srt_create_socket();
int lsock = srt_create_socket();

bool rd_nonblocking = false;

srt_setsockflag(csock, SRTO_RCVSYN, &rd_nonblocking, sizeof (rd_nonblocking));
//srt_setsockflag(lsock, SRTO_RCVSYN, &rd_nonblocking, sizeof (rd_nonblocking));

sockaddr_any addr = CreateAddr("127.0.0.1", 5000, AF_INET);

ASSERT_NE(srt_bind(lsock, addr.get(), addr.size()), -1);

ASSERT_NE(srt_listen(lsock, 5), -1);

ASSERT_NE(srt_connect(csock, addr.get(), addr.size()), -1);

sockaddr_any rev_addr;
int accepted_sock = srt_accept(lsock, rev_addr.get(), &rev_addr.len);

// Wait to make sure that the socket is connected.

int retry = 10;
while (srt_getsockstate(csock) != SRTS_CONNECTED && retry)
{
this_thread::sleep_for(milliseconds(500));
--retry;
}

ASSERT_NE(retry, 0);

// Now checkups
sockaddr_any peer_addr;
ASSERT_NE(srt_getpeername(csock, peer_addr.get(), &peer_addr.len), -1);
sockaddr_any my_addr;
ASSERT_NE(srt_getsockname(csock, my_addr.get(), &my_addr.len), -1);

cout << "Connect address: " << addr.str() << endl;
cout << "Peer address: " << peer_addr.str() << endl;
cout << "Accept address: " << rev_addr.str() << endl;
cout << "Caller address: " << my_addr.str() << endl;

EXPECT_EQ(peer_addr, addr);
EXPECT_EQ(my_addr, rev_addr);

srt_close(csock);
srt_close(accepted_sock);
srt_close(lsock);
}

0 comments on commit f477b51

Please sign in to comment.