in source/defender-tools/report_builder.c [143:275]
static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
uint32_t ulBufferLength,
const uint16_t * pusOpenPortsArray,
uint32_t ulOpenPortsArrayLength,
uint32_t * pulOutCharsWritten );
/**
* @brief Write established connections array to the given buffer in the format
* expected by the AWS IoT Device Defender Service.
*
* This function write array of the following format:
* [
* {
* "local_port":44207,
* "remote_addr":"127.0.0.1:45148"
* },
* {
* "local_port":22,
* "remote_addr":"24.16.237.194:63552"
* }
* ]
*
* @param[in] pcBuffer The buffer to write the connections array.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] pxConnectionsArray The array containing the established connections.
* @param[in] ulConnectionsArrayLength Length of the pxConnectionsArray array.
* @param[out] pulOutCharsWritten Number of characters written to the buffer.
*
* @return #ReportBuilderSuccess if the array is successfully written;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
*/
static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
uint32_t ulBufferLength,
const Connection_t * pxConnectionsArray,
uint32_t ulConnectionsArrayLength,
uint32_t * pulOutCharsWritten );
/**
* @brief Write task ids array to the given buffer as a JSON array.
*
* @param[in] pcBuffer The buffer to write the connections array.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] pulTaskIdsArray The array containing the task ids.
* @param[in] pulTaskIdsArrayLength Length of the pulTaskIdsArray array.
* @param[out] pulOutCharsWritten Number of characters written to the buffer.
*
* @return #ReportBuilderSuccess if the array is successfully written;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
*/
static eReportBuilderStatus prvWriteTaskIdsArray( char * pcBuffer,
uint32_t ulBufferLength,
const uint32_t * pulTaskIdsArray,
uint32_t pulTaskIdsArrayLength,
uint32_t * pulOutCharsWritten );
/*-----------------------------------------------------------*/
static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
uint32_t ulBufferLength,
const uint16_t * pusOpenPortsArray,
uint32_t ulOpenPortsArrayLength,
uint32_t * pulOutCharsWritten )
{
char * pcCurrentWritePos = pcBuffer;
uint32_t i, ulRemainingBufferLength = ulBufferLength;
int32_t lCharactersWritten;
eReportBuilderStatus eStatus = eReportBuilderSuccess;
configASSERT( pcBuffer != NULL );
configASSERT( pusOpenPortsArray != NULL );
configASSERT( pulOutCharsWritten != NULL );
/* Write the JSON array open marker. */
if( ulRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
ulRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
}
else
{
eStatus = eReportBuilderBufferTooSmall;
}
/* Write the array elements. */
for( i = 0; ( ( i < ulOpenPortsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
reportbuilderJSON_PORT_OBJECT_FORMAT,
DEFENDER_REPORT_PORT_KEY,
pusOpenPortsArray[ i ] );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
{
eStatus = eReportBuilderBufferTooSmall;
break;
}
else
{
ulRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
if( eStatus == eReportBuilderSuccess )
{
/* Discard the last comma. */
if( ulOpenPortsArrayLength > 0 )
{
pcCurrentWritePos -= 1;
ulRemainingBufferLength += 1;
}
/* Write the JSON array close marker. */
if( ulRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
ulRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
}
else
{
eStatus = eReportBuilderBufferTooSmall;
}
}
if( eStatus == eReportBuilderSuccess )
{
*pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
}
return eStatus;
}