Skip to content

Commit

Permalink
Merge pull request #2 from nasa-itc/nos3#202
Browse files Browse the repository at this point in the history
[nasa/nos3#202] TC commanding and SSP telemetry in NOS3
  • Loading branch information
jlucas9 committed Apr 30, 2024
2 parents c42db9f + f9c841b commit 523edf7
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ aux_source_directory(fsw/src APP_SRC_FILES)
aux_source_directory(fsw/src/formats APP_SRC_FILES)
aux_source_directory(fsw/src/services APP_SRC_FILES)

include_directories(${MISSION_SOURCE_DIR}/../components/cryptolib/include)

# Create the app module
add_cfe_app(io_lib ${APP_SRC_FILES})
1 change: 1 addition & 0 deletions fsw/public_inc/io_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "cfe.h"
#include "common_types.h"
#include "network_includes.h"
#include "crypto.h"

#ifdef _VXWORKS_OS_
/* For vxworks, include ioLib for ioctl suport */
Expand Down
3 changes: 2 additions & 1 deletion fsw/public_inc/tm_sdlp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern "C" {
*******************************************************************************/
#include "io_lib.h"
#include "tmtf.h"
#include "crypto.h"


/*******************************************************************************
Expand All @@ -63,7 +64,7 @@ typedef struct
} TM_SDLP_GlobalConfig_t;


/** Following Structure is the user defined managed /configuration parameters
/** Following Structure is the user defined managed / configuration parameters
for a a specific Channel (Master or virtual) */
typedef struct
{
Expand Down
2 changes: 1 addition & 1 deletion fsw/public_inc/trans_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/* INADDR_NONE: 0xffffffff */
#define IO_TRANS_UDP_INADDR_NONE "255.255.255.255"
/* INADDR_ANY: 0x0 */
#define IO_TRANS_UDP_INADDR_ANY "0.0.0.0"
#define IO_TRANS_UDP_INADDR_ANY "0.0.0.0"
/* INADDR_LOOPBACK */
#define IO_TRANS_UDP_INADDR_LOOPBACK "127.0.0.1"
#define IO_TRANS_UDP_INPORT_ANY INPORT_ANY
Expand Down
70 changes: 66 additions & 4 deletions fsw/src/services/tm_sdlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo,
uint16 dataFieldOffset;
uint16 secHdrLength;
uint16 gvcid = 0;
uint8 sdlsSecurityHeaderLength = 0;
uint8 sdlsSecurityTrailerLength = 0;
char mutName[OS_MAX_API_NAME];

SecurityAssociation_t* sa_ptr = NULL;

if (pGlobalConfig == NULL || pChannelConfig == NULL || pFrameInfo == NULL ||
pOverflowBuffer == NULL || pTfBuffer == NULL)
{
Expand All @@ -116,7 +119,7 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo,
iStatus = TM_SDLP_INVALID_POINTER;
goto end_of_function;
}

secHdrLength = pChannelConfig->secHdrLength;

/* The secHdr Length must be between 1-63 bytes if present.
Expand All @@ -132,7 +135,7 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo,
iStatus = TM_SDLP_INVALID_LENGTH;
goto end_of_function;
}

dataFieldLength = (int32) pGlobalConfig->frameLength;
dataFieldOffset = TMTF_PRIHDR_LENGTH;

Expand All @@ -141,8 +144,45 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo,
dataFieldOffset += secHdrLength + 1;
}

// Need SA information for security parameter lengths
// Query SA DB for active SA / SDLS parameters
if (sa_if == NULL) // This should not happen, but tested here for safety
{
printf(KRED "ERROR: SA DB Not initalized! -- CRYPTO_LIB_ERR_NO_INIT, Will Exit\n" RESET);
iStatus = CRYPTO_LIB_ERR_NO_INIT;
}
else
{
// CODE REVIEW - Use of MAP_IDs seems non-correct. They exist for TC specifically, but somehow overtime
// we've morphed and have a TYPE_TC and TYPE_TM enum - realistically MAP_IDs are a set of allowable values
// this might take some figurin'
iStatus = sa_if->sa_get_operational_sa_from_gvcid(0, (uint16)pGlobalConfig->scId, (uint16)pChannelConfig->vcId, 0, &sa_ptr);

if (iStatus != CRYPTO_LIB_SUCCESS)
{
printf(KRED "Error retrieving operational SA. Error code %d. scId = %d, vcId = %d \n" RESET, iStatus, pGlobalConfig->scId, pChannelConfig->vcId);
goto end_of_function;
}
}

// IF using SDLS
// TODO Review this if_statement
if (1)
{
sdlsSecurityHeaderLength = Crypto_Get_Security_Header_Length(sa_ptr);
dataFieldOffset += sdlsSecurityHeaderLength;
}

// Reduce available field length based on cumulative offset
dataFieldLength -= dataFieldOffset;

// IF using SDLS
if (1)
{
sdlsSecurityTrailerLength = Crypto_Get_Security_Trailer_Length(sa_ptr);
dataFieldLength -= sdlsSecurityTrailerLength;
}

if (pChannelConfig->ocfFlag == true)
{
dataFieldLength -= TMTF_OCF_LENGTH;
Expand All @@ -153,6 +193,22 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo,
dataFieldLength -= TMTF_ERR_CTRL_FIELD_LENGTH;
}

#ifdef TM_DEBUG
printf("TM_SDLP Initializing channel:\n");
printf("\t Primary header length: \t%d\n", TMTF_PRIHDR_LENGTH);
printf("\t Secondary header length: \t%d\n", secHdrLength);
printf("\t\t SPI Length: 2 bytes\n");
printf("\t\t IV Length: %d bytes\n", sa_ptr->shivf_len);
printf("\t\t SNF Length Length: %d bytes\n", sa_ptr->shsnf_len);
printf("\t\t PLF Length: %d bytes\nEnable", sa_ptr->shplf_len);
printf("\t Security header length: \t%d\n", sdlsSecurityHeaderLength);
printf("\t Data field offset: \t%d\n", dataFieldOffset);
printf("\t Data field length: \t%d\n", dataFieldLength);
printf("\t Security trailer length: \t%d\n", sdlsSecurityTrailerLength);
printf("\t OCF Length: \t%d HARDCODED - to be changed\n", TMTF_OCF_LENGTH); // Todo, currently hardcoded
printf("\t FECF length: \t%d HARDCODED - to be changed\n", TMTF_ERR_CTRL_FIELD_LENGTH); //Todo, currently hardcoded
#endif

if (dataFieldLength < 0)
{
CFE_EVS_SendEvent(IO_LIB_TM_SDLP_EID, CFE_EVS_EventType_ERROR,
Expand Down Expand Up @@ -255,6 +311,12 @@ int32 TM_SDLP_FrameHasData(TM_SDLP_FrameInfo_t *pFrameInfo)
hasData = TM_SDLP_INVALID_POINTER;
goto end_of_function;
}

#ifdef TM_DEBUG
printf("*** DATA LENGTH INFO!***\n");
printf("*** Free Octets: %d\n", pFrameInfo->freeOctets);
printf("*** dataFieldLength: %d\n", pFrameInfo->dataFieldLength);
#endif

if (pFrameInfo->freeOctets < pFrameInfo->dataFieldLength)
{
Expand Down Expand Up @@ -447,7 +509,7 @@ int32 TM_SDLP_StartFrame(TM_SDLP_FrameInfo_t *pFrameInfo)
iStatus = TM_SDLP_FRAME_NOT_INIT;
goto end_of_function;
}

OS_MutSemTake(pFrameInfo->mutexId);

/* If the frame is already started, issue a warning. */
Expand Down
64 changes: 55 additions & 9 deletions fsw/src/services/trans_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

#include "trans_udp.h"

/* Start additional includes for hostname snippet */
#include<sys/socket.h>
#include<netdb.h> //hostent
#include<arpa/inet.h>
/* End additional includes for hostname snippet */


/** Initialize (create, configure and bind) a UDP Socket */
int32 IO_TransUdpInit(IO_TransUdpConfig_t * config, IO_TransUdp_t * udp)
{
Expand Down Expand Up @@ -110,6 +117,11 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp)
"IO_TransUDP Error: Bad config timeout input.");
return IO_TRANS_UDP_BAD_INPUT_ERROR;
}

/* Initialize socket address structures */
CFE_PSP_MemSet((void *) &udp->sockAddr, 0x0, sizeof(struct sockaddr_in));
CFE_PSP_MemSet((void *) &udp->srcAddr, 0x0, sizeof(struct sockaddr_in));
CFE_PSP_MemSet((void *) &udp->destAddr, 0x0, sizeof(struct sockaddr_in));

/* Get IP address from cAddr */
/* NOTE: inet_aton errors out if cAddr = "0.0.0.0", the value of
Expand All @@ -122,8 +134,27 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp)
}
else
{
//status = inet_aton(&config->cAddr[0], (struct in_addr *) &uiAddr);
status = inet_pton(AF_INET, &config->cAddr[0], (struct in_addr *) &uiAddr);
/*
Start hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c
*/
struct hostent *he;
struct in_addr **addr_list;
int i;

if ( (he = gethostbyname(config->cAddr) ) != NULL)
{
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(config->cAddr, inet_ntoa(*addr_list[i]));
break;
}
}
/*
End hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c
*/
status = inet_aton(&config->cAddr[0], (struct in_addr *) &uiAddr);
if (status == INET_ATON_ERROR)
{
CFE_EVS_SendEvent(IO_LIB_TRANS_UDP_EID, CFE_EVS_EventType_ERROR,
Expand All @@ -133,11 +164,6 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp)
}
}

/* Initialize socket address structures */
CFE_PSP_MemSet((void *) &udp->sockAddr, 0x0, sizeof(struct sockaddr_in));
CFE_PSP_MemSet((void *) &udp->srcAddr, 0x0, sizeof(struct sockaddr_in));
CFE_PSP_MemSet((void *) &udp->destAddr, 0x0, sizeof(struct sockaddr_in));

/* Save UDP Socket Addr structure */
udp->sockAddr.sin_family = AF_INET;
udp->sockAddr.sin_addr.s_addr = uiAddr;
Expand Down Expand Up @@ -245,8 +271,28 @@ int32 IO_TransUdpSetDestAddr(IO_TransUdp_t *udp, char * destAddr,
}

/* Get IP address from cAddr */
//status = inet_aton(destAddr, (struct in_addr *) &uiAddr);
status = inet_pton(AF_INET, destAddr, (struct in_addr *) &uiAddr);
/*
Start hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c
*/
struct hostent *he;
struct in_addr **addr_list;
int i;

if ( (he = gethostbyname(destAddr) ) != NULL)
{
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
// Return the first one;
strcpy(destAddr , inet_ntoa(*addr_list[i]));
break;
}
}
/*
End hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c
*/

status = inet_aton(destAddr, (struct in_addr *) &uiAddr);
if (status == INET_ATON_ERROR)
{
CFE_EVS_SendEvent(IO_LIB_TRANS_UDP_EID, CFE_EVS_EventType_ERROR,
Expand Down

0 comments on commit 523edf7

Please sign in to comment.