/**************************************************************************** * Application Layer Module for the Real-time BBC Codec/Modem FTP program * **************************************************************************** * William L. Bahn * * Academy Center for Information Security * * Department of Computer Science * * United States Air Force Academy * * USAFA, CO 80840 * **************************************************************************** * FILE:............ bbcftp.c * * DATE CREATED:.... 18 SEP 07 * * DATE MODIFIED:... 18 SEP 07 * **************************************************************************** * * REVISION HISTORY * **************************************************************************** * * DESCRIPTION * * This module provides the crude application layer functions for the ftp * demo. */ //------------------------------------------------------------------------ // REQUIRED INCLUDES //------------------------------------------------------------------------ #include // malloc(), free() #include // printf() #include // memmove() #include "bbcftp.h" //------------------------------------------------------------------------ // STRUCTURE DEFINITIONS //------------------------------------------------------------------------ // NOTE: Normally the structure definition would be in the *.c file to make // the structure members inaccessible to outside functions except through // public function calls. But for the real-time code it has been decided // to make the structure members directly visible to the functions that // manipulate them. //------------------------------------------------------------------------ // PRIVATE FUNCTION DEFINITIONS //------------------------------------------------------------------------ //------------------------------------------------------------------------ // PUBLIC FUNCTION DEFINITIONS //------------------------------------------------------------------------ BBCFTP *BBCFTP_Del(BBCFTP *p) { if (p) { p->config = CONFIG_Del(p->config); p->source = SOURCE_Del(p->source); p->codec = CODEC_Del(p->codec); p->buffer = BUFFER_Del(p->buffer); p->modem = MODEM_Del(p->modem); p->sink = SINK_Del(p->sink); } return NULL; } BBCFTP *BBCFTP_New(char *filename, DWORD *errcode) { BBCFTP *p; DWORD err; p = NULL; err = 0; *errcode = 0; p = (BBCFTP *) malloc(sizeof(BBCFTP)); if (!p) *errcode |= 1 << 0; if (!*errcode) { p->config = CONFIG_New(filename, &err); if (err) *errcode |= 1 << 1; } if (!*errcode) { p->source = SOURCE_New(p->config, &err); if (err) *errcode |= 1 << 2; p->codec = CODEC_New(p->config, &err); if (err) *errcode |= 1 << 3; p->buffer = BUFFER_New(p->config, &err); if (err) *errcode |= 1 << 4; p->modem = MODEM_New(p->config, &err); if (err) *errcode |= 1 << 5; p->sink = SINK_New(p->config, &err); if (err) *errcode |= 1 << 6; } return p; } void BBCFTP_ErrorCodes(DWORD err) { if ( err & ((DWORD) 1 << 0) ) printf("BBC-FTP System Constructor failed to allocate\n"); if ( err & ((DWORD) 1 << 1) ) printf("CONFIG Constructor exited with errors\n"); if ( err & ((DWORD) 1 << 2) ) printf("SOURCE Constructor exited with errors\n"); if ( err & ((DWORD) 1 << 3) ) printf("CODEC Constructor exited with errors\n"); if ( err & ((DWORD) 1 << 4) ) printf("BUFFER Constructor exited with errors\n"); if ( err & ((DWORD) 1 << 5) ) printf("MODEM Constructor exited with errors\n"); if ( err & ((DWORD) 1 << 6) ) printf("SINK Constructor exited with errors\n"); } void PrintMessage(BYTE *base) { int i; int chunk_size_bytes; DWORD checksum; WORD seqnum, loadbits, id; checksum = GetMessageChecksum(base); seqnum = GetMessageSeq(base); loadbits = GetMessageLoadBits(base); id = GetMessageID(base); printf("[%04lu] ", (unsigned long) checksum); printf("[%04lu] ", (unsigned long) seqnum); printf("[%04lu] ", (unsigned long) loadbits); printf("[%04lu] ", (unsigned long) id); chunk_size_bytes = loadbits/8; printf("["); for (i = 0; i < chunk_size_bytes; i++) { putc(*(base + BBC_FTP_OFFSET_PAYLOAD + i), stdout); } printf("]\n"); } void SetMessageChecksum(BYTE *base, DWORD v) { memmove(base+BBC_FTP_OFFSET_CHECKSUM, &v, BBC_FTP_BYTES_CHECKSUM); } void SetMessageSeq(BYTE *base, WORD v) { memmove(base+BBC_FTP_OFFSET_SEQNUM, &v, BBC_FTP_BYTES_SEQNUM); } void SetMessageLoadBits(BYTE *base, WORD v) { memmove(base+BBC_FTP_OFFSET_LOADBITS, &v, BBC_FTP_BYTES_LOADBITS); } void SetMessageID(BYTE *base, WORD v) { memmove(base+BBC_FTP_OFFSET_ID, &v, BBC_FTP_BYTES_ID); } void SetMessagePayload(BYTE *base, BYTE *source, DWORD bytes, int offset) { memmove(base+BBC_FTP_OFFSET_PAYLOAD+offset, source, bytes); } DWORD GetMessageChecksum(BYTE *base) { return *((DWORD *)(base + BBC_FTP_OFFSET_CHECKSUM)); } WORD GetMessageSeq(BYTE *base) { return *((WORD *)(base + BBC_FTP_OFFSET_SEQNUM)); } WORD GetMessageLoadBits(BYTE *base) { return *((WORD *)(base + BBC_FTP_OFFSET_LOADBITS)); } WORD GetMessageID(BYTE *base) { return *((WORD *)(base + BBC_FTP_OFFSET_ID)); } BYTE *GetMessagePayload(BYTE *base) { return (BYTE *)(base + BBC_FTP_OFFSET_PAYLOAD); } //------------------------------------------------------------------------