Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added length type wrapper in sockaddr_any for convenience. #1492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions apps/srt-tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,11 @@ unique_ptr<Medium> SrtMedium::Accept()
unique_ptr<Medium> TcpMedium::Accept()
{
sockaddr_any sa;
socklen_t salen = sizeof sa;
int s = ::accept(m_socket, (sa.get()), (&salen));
int s = ::accept(m_socket, (sa.get()), (&sa.syslen()));
if (s == -1)
{
Error(errno, "accept");
}
sa.len = salen;

// Configure 1s timeout
timeval timeout_1s { 1, 0 };
Expand Down
23 changes: 23 additions & 0 deletions srtcore/netinet_any.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ struct sockaddr_any
// back here from the value of `syslen_t`.
len_t len;

struct SysLenWrapper
{
syslen_t syslen;
len_t& backwriter;
syslen_t* operator&() { return &syslen; }

SysLenWrapper(len_t& source): syslen(source), backwriter(source)
{
}

~SysLenWrapper()
{
backwriter = syslen;
}
};

// Usage:
// ::accept(lsn_sock, sa.get(), &sa.syslen());
SysLenWrapper syslen()
{
return SysLenWrapper(len);
}

static size_t storage_size()
{
typedef union
Expand Down
4 changes: 1 addition & 3 deletions testing/testmedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,16 +2517,14 @@ class UdpSource: public virtual Source, public virtual UdpCommon
{
bytevector data(chunk);
sockaddr_any sa(sadr.family());
socklen_t si = sa.size();
int64_t srctime = 0;
int stat = recvfrom(m_sock, data.data(), (int) chunk, 0, sa.get(), &si);
int stat = recvfrom(m_sock, data.data(), (int) chunk, 0, sa.get(), &sa.syslen());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dereferencing a temporal object SysLenWrapper::syslen.... 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowed, as long as you don't access it after it is deleted. We know it is not deleted until the current instruction finishes execution, so it won't happen before returning from the function. It actually does the same as the "temporary variable" solution used previously, just the trick is hidden inside.

if (transmit_use_sourcetime)
{
srctime = srt_time_now();
}
if (stat == -1)
Error(SysError(), "UDP Read/recvfrom");
sa.len = si;

if (stat < 1)
{
Expand Down