Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
__pycache__/
.pytest_cache/
mock-bpa-test/ccsds_bpsec_redbook_requirements_modified.yaml
/deps/
34 changes: 1 addition & 33 deletions src/BPSecLib_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,37 +173,6 @@ typedef enum
*/
char *BSL_Log_DumpAsHexString(char *dstbuf, size_t dstlen, const uint8_t *srcbuf, size_t srclen);

/** Opens the event log.
* @note This should be called once per process, not thread or library instance.
* At the end of the process there should be a call to BSL_closelog()
*
* This is a mimic to POSIX @c openlog()
*/
void BSL_openlog(void);

/** Closes the event log.
* This is a mimic to POSIX @c closelog()
* @sa BSL_openlog
*/
void BSL_closelog(void);

/** Interpret a text name as a severity level.
*
* @param[out] severity The associated severity level.
* @param[in] name The text name, which is case insensitive.
* @return Zero if successful.
*/
int BSL_LogGetSeverity(int *severity, const char *name);

/** Set the least severity enabled for logging.
* Other events will be dropped by the logging facility.
* This function is multi-thread safe.
*
* @param severity The severity from a subset of the POSIX syslog values.
* @sa BSL_log_is_enabled_for()
*/
void BSL_LogSetLeastSeverity(int severity);

/** Determine if a particular severity is being logged.
* This function is multi-thread safe.
*
Expand Down Expand Up @@ -343,9 +312,8 @@ int BSL_SeqWriter_Put(BSL_SeqWriter_t *obj, const uint8_t *buf, size_t bufsize);
/** Initialize an abstract EID.
*
* @param[out] eid The object to initialize.
* @return Zero if successful.
*/
int BSL_HostEID_Init(BSL_HostEID_t *eid);
void BSL_HostEID_Init(BSL_HostEID_t *eid);

/** De-initialize an abstract EID.
*
Expand Down
35 changes: 34 additions & 1 deletion src/BPSecLib_Public.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include <sys/time.h>

#include "BSLConfig.h"
#include "Data.h"
Expand Down Expand Up @@ -261,8 +263,11 @@
} BSL_CanonicalBlock_t;

/** Dynamic BPA descriptor.
*
* @caution All functions in this structure must be thread safe, as they
* can be called by any number of BSL instances across any threads.
*/
typedef struct

Check warning on line 270 in src/BPSecLib_Public.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this structure so it has no more than 20 fields, rather than the 21 it currently has.

See more on https://sonarcloud.io/project/issues?id=NASA-AMMOS_BSL&issues=AZ0Dii724fUXOCXgcxSh&open=AZ0Dii724fUXOCXgcxSh&pullRequest=143
{
/// User data pointer for callbacks
void *user_data;
Expand All @@ -271,7 +276,7 @@
int (*get_sec_src_eid_fn)(void *user_data, BSL_HostEID_t *result);

/// @brief Host BPA function to initialize/allocate an EID type.
int (*eid_init)(void *user_data, BSL_HostEID_t *result);
void (*eid_init)(void *user_data, BSL_HostEID_t *result);

/// @brief Host BPA function to deinit/free an EID type.
void (*eid_deinit)(void *user_data, BSL_HostEID_t *eid);
Expand Down Expand Up @@ -350,6 +355,34 @@

/// @brief Host BPA function that returns true if the given EID matched an EID pattern.
bool (*eidpat_match)(const BSL_HostEIDPattern_t *pat, const BSL_HostEID_t *eid, void *user_data);

/** Called to check if logging is enabled for at least a specific severity.
*
* @note If not provided by the host, this defaults to always-true.
*
* @param severity The severity from a subset of the POSIX syslog values.
* @return True if logging will occur for that severity level.
*/
bool (*log_is_enabled_for)(int severity);

/** Called for each log event from the BSL and its PP and SC instances.
* All input text strings must be copied by the callback if they are
* referenced outside of that callback.
*
* @note If not provided by the host, this defaults to writing
* synchronously to @c stderr.
*
* @param timestamp The timestamp of the original event.
* @param severity The severity from a subset of the POSIX syslog values.
* This value has already been filtered by #log_is_enabled_for.
* @param[in] filename The originating file name, which may include directory parts.
* @param[in] lineno The originating file line number.
* @param[in] funcname The originating function name.
* @param[in] format The log message format string.
* @param args Values for the format string.
*/
void (*log_event)(const struct timeval *timestamp, int severity, const char *filename, int lineno,
const char *funcname, const char *format, va_list args);
} BSL_HostDescriptors_t;

/** Set the BPA descriptor (callbacks) for this process.
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ set(BSL_DYNAMIC_C
${CMAKE_CURRENT_SOURCE_DIR}/backend/AbsSecBlock.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/HostInterface.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/PublicInterfaceImpl.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/LoggingStderr.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/PolicyProvider.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/TelemetryCounters.c
${CMAKE_CURRENT_SOURCE_DIR}/backend/SecOperation.c
Expand Down
115 changes: 111 additions & 4 deletions src/backend/HostInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* @brief Implementation of the host BPA and its callback functions.
* @ingroup backend_dyn
*/
#include <stdarg.h>
#include <pthread.h>
#include <sys/time.h>
#include <BPSecLib_Private.h>
#include "UtilDefs_SeqReadWrite.h"

Expand Down Expand Up @@ -148,11 +151,11 @@
HostDescriptorTable = (BSL_HostDescriptors_t) { 0 };
}

int BSL_HostEID_Init(BSL_HostEID_t *eid)
void BSL_HostEID_Init(BSL_HostEID_t *eid)
{
CHK_ARG_NONNULL(eid);
CHK_PRECONDITION(HostDescriptorTable.eid_init != NULL);
return HostDescriptorTable.eid_init(HostDescriptorTable.user_data, eid);
ASSERT_ARG_NONNULL(eid);
ASSERT_PRECONDITION(HostDescriptorTable.eid_init != NULL);
HostDescriptorTable.eid_init(HostDescriptorTable.user_data, eid);
}

void BSL_HostEID_Deinit(BSL_HostEID_t *eid)
Expand Down Expand Up @@ -224,3 +227,107 @@
ASSERT_PRECONDITION(HostDescriptorTable.eidpat_match);
return HostDescriptorTable.eidpat_match(pat, eid, HostDescriptorTable.user_data);
}

// NOLINTBEGIN
static const char *log_sev_names[] = {
NULL, // LOG_EMERG
NULL, // LOG_ALERT
"CRIT", // LOG_CRIT
"ERROR", // LOG_ERR
"WARNING", // LOG_WARNING
NULL, // LOG_NOTICE
"INFO", // LOG_INFO
"DEBUG", // LOG_DEBUG
};
// NOLINTEND

bool BSL_LogIsEnabledFor(int severity)
{
if ((severity < 0) || (severity >= 7))
{
// not valid
return false;
}

if (!HostDescriptorTable.log_is_enabled_for)
{
return true;
}

return HostDescriptorTable.log_is_enabled_for(severity);
}

void BSL_LogEvent(int severity, const char *filename, int lineno, const char *funcname, const char *format, ...)

Check failure on line 260 in src/backend/HostInterface.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove use of ellipsis notation.

See more on https://sonarcloud.io/project/issues?id=NASA-AMMOS_BSL&issues=AZ0Dii7k4fUXOCXgcxSf&open=AZ0Dii7k4fUXOCXgcxSf&pullRequest=143
{
if (!BSL_LogIsEnabledFor(severity))
{
return;
}

struct timeval timestamp;
gettimeofday(&timestamp, NULL);

va_list args;
va_start(args, format);

if (!HostDescriptorTable.log_event)
{
char tmbuf[32]; // NOLINT
{
time_t nowtime = timestamp.tv_sec;
struct tm nowtm;
gmtime_r(&nowtime, &nowtm);

char *curs = tmbuf;
size_t remain = sizeof(tmbuf) - 1;
size_t len = strftime(curs, remain, "%Y-%m-%dT%H:%M:%S", &nowtm);
curs += len;
remain -= len;
snprintf(curs, remain, ".%06ld", timestamp.tv_usec);
}

const char *severity_name = log_sev_names[severity];

pthread_t thread = pthread_self();
char thrbuf[2 * sizeof(pthread_t) + 1];
size_t remain = sizeof(thrbuf);
{
const uint8_t *data = (const void *)&thread;
char *out = thrbuf;
for (size_t ix = 0; ix < sizeof(pthread_t); ++ix)
{
snprintf(out, remain, "%02X", *data);
data++;
out += 2;
remain -= 2;
}
*out = '\0';
}

// simplify filename
static const char dirsep = '/';
const char *filepos = strrchr(filename, dirsep);
if (filepos)
{
filepos += 1;
}
else
{
filepos = filename;
}

fprintf(stderr, "%s T:%s <%s> [%s:%d:%s] ", tmbuf, thrbuf, severity_name, filepos, lineno, funcname);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
vfprintf(stderr, format, args);
#pragma GCC diagnostic pop
fprintf(stderr, "\n");
fflush(stderr);
}
else
{
HostDescriptorTable.log_event(&timestamp, severity, filename, lineno, funcname, format, args);
}

va_end(args);
}
17 changes: 17 additions & 0 deletions src/backend/PublicInterfaceImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
#include "SecurityActionSet.h"
#include "SecurityResultSet.h"

char *BSL_Log_DumpAsHexString(char *dstbuf, size_t dstlen, const uint8_t *srcbuf, size_t srclen)
{
ASSERT_ARG_NONNULL(dstbuf);
ASSERT_ARG_NONNULL(srcbuf);
ASSERT_ARG_EXPR(dstlen > 0);
ASSERT_ARG_EXPR(srclen > 0);

memset(dstbuf, 0, dstlen);
const char hex_digits[] = "0123456789ABCDEF";
for (size_t i = 0; i < srclen && (((i * 2) + 1) < dstlen - 1); i++)
{
dstbuf[(i * 2)] = hex_digits[(srcbuf[i] >> 4) & 0x0F];

Check warning on line 48 in src/backend/PublicInterfaceImpl.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove these redundant parentheses.

See more on https://sonarcloud.io/project/issues?id=NASA-AMMOS_BSL&issues=AZ0Dii5U4fUXOCXgcxSe&open=AZ0Dii5U4fUXOCXgcxSe&pullRequest=143
dstbuf[(i * 2) + 1] = hex_digits[srcbuf[i] & 0x0F];
}
return dstbuf;
}

size_t BSL_LibCtx_Sizeof(void)
{
return sizeof(BSL_LibCtx_t);
Expand Down
2 changes: 2 additions & 0 deletions src/mock_bpa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ target_compile_options(bsl_mock_bpa PRIVATE -Wshadow -Wpointer-arith -Wstrict-pr

target_sources(
bsl_mock_bpa PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/log.h
${CMAKE_CURRENT_SOURCE_DIR}/agent.h
${CMAKE_CURRENT_SOURCE_DIR}/bundle.h
${CMAKE_CURRENT_SOURCE_DIR}/crc.h
Expand All @@ -41,6 +42,7 @@ target_sources(
)
target_sources(
bsl_mock_bpa PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/log.c
${CMAKE_CURRENT_SOURCE_DIR}/agent.c
${CMAKE_CURRENT_SOURCE_DIR}/bundle.c
${CMAKE_CURRENT_SOURCE_DIR}/crc.c
Expand Down
1 change: 1 addition & 0 deletions src/mock_bpa/MockBPA.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "encode.h"
#include "decode.h"
#include "eidpat.h"
#include "log.h"
#include "agent.h"

#endif //_BSL_MockBPA_MockBPA_H_
4 changes: 4 additions & 0 deletions src/mock_bpa/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <errno.h>
#include <poll.h>
#include "agent.h"
#include "log.h"
#include "eid.h"
#include "eidpat.h"
#include "encode.h"
Expand Down Expand Up @@ -406,6 +407,9 @@ BSL_HostDescriptors_t MockBPA_Agent_Descriptors(MockBPA_Agent_t *agent)
.eidpat_deinit = mock_bpa_eidpat_deinit,
.eidpat_from_text = mock_bpa_eidpat_from_text,
.eidpat_match = mock_bpa_eidpat_match,

.log_is_enabled_for = mock_bpa_LogIsEnabledFor,
.log_event = mock_bpa_LogEvent,
};
return bpa;
}
Expand Down
10 changes: 4 additions & 6 deletions src/mock_bpa/eid.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ void bsl_mock_eid_deinit(bsl_mock_eid_t *eid)
memset(eid, 0, sizeof(bsl_mock_eid_t));
}

int MockBPA_EID_Init(void *user_data _U_, BSL_HostEID_t *eid)
void MockBPA_EID_Init(void *user_data _U_, BSL_HostEID_t *eid)
{
BSL_CHKERR1(eid);
BSL_CHKVOID(eid);
memset(eid, 0, sizeof(BSL_HostEID_t));
eid->handle = BSL_MALLOC(sizeof(bsl_mock_eid_t));
if (!(eid->handle))
if (eid->handle)
{
return -2;
bsl_mock_eid_init(eid->handle);
}
bsl_mock_eid_init(eid->handle);
return 0;
}

void MockBPA_EID_Deinit(void *user_data _U_, BSL_HostEID_t *eid)
Expand Down
2 changes: 1 addition & 1 deletion src/mock_bpa/eid.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void bsl_mock_eid_init(bsl_mock_eid_t *eid);
void bsl_mock_eid_deinit(bsl_mock_eid_t *eid);

/// Interface for BSL_HostDescriptors_t::eid_init
int MockBPA_EID_Init(void *user_data, BSL_HostEID_t *eid);
void MockBPA_EID_Init(void *user_data, BSL_HostEID_t *eid);

/// Interface for BSL_HostDescriptors_t::eid_deinit
void MockBPA_EID_Deinit(void *user_data, BSL_HostEID_t *eid);
Expand Down
Loading
Loading