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 messages for access control rejection messages #2606

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
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
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;
}

}
5 changes: 3 additions & 2 deletions testing/srt-test-live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
#include "testmedia.hpp" // requires access to SRT-dependent globals
#include "verbose.hpp"

// NOTE: This is without "haisrt/" because it uses an internal path
// NOTE: This is without "srt/" because it uses an internal path
// to the library. Application using the "installed" library should
// use <srt/srt.h>
#include <srt.h>
#include <udt.h> // This TEMPORARILY contains extra C++-only SRT API.
#include <access_control.h>
#include <logging.h>

using namespace std;
Expand Down Expand Up @@ -354,6 +354,7 @@ extern "C" int SrtUserPasswordHook(void* , SRTSOCKET acpsock, int hsv, const soc
// This hook sets the password to the just accepted socket
// depending on the user

srt_setrejectreason(acpsock, SRT_REJX_UNAUTHORIZED);
string exp_pw = passwd.at(username);

srt_setsockflag(acpsock, SRTO_PASSPHRASE, exp_pw.c_str(), exp_pw.size());
Expand Down
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