Skip to content

Commit

Permalink
Added extended reject reason string API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Małecki committed Jan 9, 2023
1 parent 124ec2b commit f14c349
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion srtcore/access_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ written by
#define SRT_REJX_GW 1502 // The server acts as a gateway and the target endpoint rejected the connection.
#define SRT_REJX_DOWN 1503 // The service has been temporarily taken over by a stub reporting this error. The real service can be down for maintenance or crashed.
// CODE NOT IN USE 504: unused: timeout not supported
#define SRT_REJX_VERSION 1505 // SRT version not supported. This might be either unsupported backward compatibility, or an upper value of a version.
#define SRT_REJX_VERSION 1505 // Version not supported - either SRT or the application itself. This might be either unsupported backward compatibility, or an upper value of a version.
// CODE NOT IN USE 506: unused: negotiation and references not supported
#define SRT_REJX_NOROOM 1507 // The data stream cannot be archived due to lacking storage space. This is in case when the request type was to send a file or the live stream to be archived.
// CODE NOT IN USE 508: unused: no redirection supported
Expand Down
6 changes: 5 additions & 1 deletion srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4441,9 +4441,12 @@ EConnectStatus srt::CUDT::processConnectResponse(const CPacket& response, CUDTEx
}

HLOGC(cnlog.Debug, log << CONID() << "processConnectResponse: HS RECEIVED: " << m_ConnRes.show());
if (m_ConnRes.m_iReqType > URQ_FAILURE_TYPES)
if (m_ConnRes.m_iReqType >= URQ_FAILURE_TYPES)
{
m_RejectReason = RejectReasonForURQ(m_ConnRes.m_iReqType);
LOGC(cnlog.Warn,
log << CONID() << "processConnectResponse: rejecting per reception of a rejection HS response: "
<< RequestTypeStr(m_ConnRes.m_iReqType));
return CONN_REJECT;
}

Expand Down Expand Up @@ -4626,6 +4629,7 @@ EConnectStatus srt::CUDT::postConnect(const CPacket* pResponse, bool rendezvous,
// in rendezvous it's completed before calling this function.
if (!rendezvous)
{
HLOGC(cnlog.Debug, log << CONID() << boolalpha << "postConnect: packet:" << bool(pResponse) << " rendezvous:" << rendezvous);
// The "local storage depleted" case shouldn't happen here, but
// this is a theoretical path that needs prevention.
bool ok = pResponse;
Expand Down
1 change: 1 addition & 0 deletions srtcore/srt.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ SRT_API int srt_setrejectreason(SRTSOCKET sock, int value);
// Planned removal: v1.6.0.
SRT_API SRT_ATR_DEPRECATED extern const char* const srt_rejectreason_msg [];
SRT_API const char* srt_rejectreason_str(int id);
SRT_API const char* srt_rejectreasonx_str(int id);

SRT_API uint32_t srt_getversion(void);

Expand Down
62 changes: 62 additions & 0 deletions srtcore/srt_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ written by

#include <iterator>
#include <fstream>
#include <utility>
#include <algorithm>
#include "srt.h"
#include "access_control.h"
#include "common.h"
#include "packet.h"
#include "core.h"
Expand Down Expand Up @@ -470,6 +473,11 @@ extern const char* const srt_rejectreason_msg[] = {

const char* srt_rejectreason_str(int id)
{
if (id == SRT_REJX_FALLBACK)
{
return "Application fallback (default) rejection reason";
}

if (id >= SRT_REJC_PREDEFINED)
{
return "Application-defined rejection reason";
Expand All @@ -481,4 +489,58 @@ const char* srt_rejectreason_str(int id)
return srt_rejection_reason_msg[id];
}

// NOTE: values in the first field must be sorted by numbers.
pair<int, const char* const> srt_rejectionx_reason_msg [] = {

// Internal
make_pair(SRT_REJX_FALLBACK, "Default fallback reason"),
make_pair(SRT_REJX_KEY_NOTSUP, "Unsupported streamid key"),
make_pair(SRT_REJX_FILEPATH, "Incorrect resource path"),
make_pair(SRT_REJX_HOSTNOTFOUND, "Unrecognized host under h key"),

// HTTP adopted codes
make_pair(SRT_REJX_BAD_REQUEST, "Bad request"),
make_pair(SRT_REJX_UNAUTHORIZED, "Unauthorized"),
make_pair(SRT_REJX_OVERLOAD, "Server overloaded or underpaid"),
make_pair(SRT_REJX_FORBIDDEN, "Resource access forbidden"),
make_pair(SRT_REJX_BAD_MODE, "Bad mode specified with m key"),
make_pair(SRT_REJX_UNACCEPTABLE, "Unacceptable parameters for specified resource"),
make_pair(SRT_REJX_CONFLICT, "Access conflict for a locked resource"),
make_pair(SRT_REJX_NOTSUP_MEDIA, "Unsupported media type specified with t key"),
make_pair(SRT_REJX_LOCKED, "Resource locked for any access"),
make_pair(SRT_REJX_FAILED_DEPEND, "Dependent session id expired"),
make_pair(SRT_REJX_ISE, "Internal server error"),
make_pair(SRT_REJX_GW, "Gateway target rejected connection"),
make_pair(SRT_REJX_DOWN, "Service is down for maintenance"),
make_pair(SRT_REJX_VERSION, "Unsupported version for the request"),
make_pair(SRT_REJX_NOROOM, "Storage capacity exceeded"),
};

struct FCompareItems
{
bool operator()(const pair<int, const char* const>& a, int b)
{
return a.first < b;
}
};

const char* srt_rejectreasonx_str(int id)
{
if (id < SRT_REJX_FALLBACK)
{
return "System-defined rejection reason (not extended)";
}

pair<int, const char* const>* begin = srt_rejectionx_reason_msg;
pair<int, const char* const>* end = begin + Size(srt_rejectionx_reason_msg);
pair<int, const char* const>* found = lower_bound(begin, end, id, FCompareItems());

if (found == end || found->first != id)
{
return "Undefined extended rejection code";
}

return found->second;
}

}
21 changes: 17 additions & 4 deletions testing/testmedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ string DirectionName(SRT_EPOLL_T direction)
return dir_name;
}

static string RejectReasonStr(int id)
{
if (id < SRT_REJC_PREDEFINED)
return srt_rejectreason_str(id);

if (id < SRT_REJC_USERDEFINED)
return srt_rejectreasonx_str(id);

ostringstream sout;
sout << "User-defined reason code " << id;
return sout.str();
}

template<class FileBase> inline
bytevector FileRead(FileBase& ifile, size_t chunk, const string& filename)
{
Expand Down Expand Up @@ -1054,7 +1067,7 @@ void SrtCommon::OpenGroupClient()
out << "[" << c.token << "] " << c.host << ":" << c.port;
if (!c.source.empty())
out << "[[" << c.source.str() << "]]";
out << ": " << srt_strerror(c.error, 0) << ": " << srt_rejectreason_str(c.reason) << endl;
out << ": " << srt_strerror(c.error, 0) << ": " << RejectReasonStr(c.reason) << endl;
}
reasons.insert(c.reason);
}
Expand Down Expand Up @@ -1178,7 +1191,7 @@ void SrtCommon::OpenGroupClient()
out << "[" << c.token << "] " << c.host << ":" << c.port;
if (!c.source.empty())
out << "[[" << c.source.str() << "]]";
out << ": " << srt_strerror(c.error, 0) << ": " << srt_rejectreason_str(c.reason) << endl;
out << ": " << srt_strerror(c.error, 0) << ": " << RejectReasonStr(c.reason) << endl;
}
reasons.insert(c.reason);
}
Expand Down Expand Up @@ -1387,11 +1400,11 @@ void SrtCommon::Error(string src, int reason, int force_result)
if ( Verbose::on )
Verb() << "FAILURE\n" << src << ": [" << result << "] "
<< "Connection rejected: [" << int(reason) << "]: "
<< srt_rejectreason_str(reason);
<< RejectReasonStr(reason);
else
cerr << "\nERROR #" << result
<< ": Connection rejected: [" << int(reason) << "]: "
<< srt_rejectreason_str(reason);
<< RejectReasonStr(reason);
}
else
{
Expand Down

0 comments on commit f14c349

Please sign in to comment.