From c2db7ee7c4aa50c2bc07a57fe11d5ccb30a0171d Mon Sep 17 00:00:00 2001 From: skarg Date: Mon, 9 Apr 2012 00:43:27 +0000 Subject: [PATCH] Added m option to mstpcrc demo to pass a line of bytes which get encoded into Wireshark pcap file for viewing in Wireshark. Useful when someone sends you a string of bytes from an MS/TP capture and you want to quickly decoding them using Wireshark. --- bacnet-stack/demo/Makefile | 2 +- bacnet-stack/demo/mstpcrc/Makefile | 10 +-- bacnet-stack/demo/mstpcrc/main.c | 97 ++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/bacnet-stack/demo/Makefile b/bacnet-stack/demo/Makefile index ced45d3d..502a5b89 100644 --- a/bacnet-stack/demo/Makefile +++ b/bacnet-stack/demo/Makefile @@ -47,7 +47,7 @@ LFLAGS = -Wl,$(LIBRARIES) SUBDIRS = readprop writeprop readfile writefile reinit server dcc \ whohas whois ucov scov timesync epics readpropm \ - mstpcap uptransfer \ + mstpcap mstpcrc uptransfer \ whoisrouter iamrouter initrouter ifeq (${BACNET_PORT},win32) SUBDIRS += ptransfer diff --git a/bacnet-stack/demo/mstpcrc/Makefile b/bacnet-stack/demo/mstpcrc/Makefile index 601a1666..05e00114 100644 --- a/bacnet-stack/demo/mstpcrc/Makefile +++ b/bacnet-stack/demo/mstpcrc/Makefile @@ -10,7 +10,7 @@ TARGET = mstpcrc TARGET_BIN = ${TARGET}$(TARGET_EXT) # This demo seems to be a little unique -BACNET_INCLUDE_DIR = ../../include +DEFINES = $(BACNET_DEFINES) BACNET_SOURCE_DIR = ../../src #libraries used @@ -27,14 +27,8 @@ endif # search order for included libraries INCLUDES = -I$(BACNET_INCLUDE_DIR) -# any special defines -DEFINES = - -# put all the flags together -CFLAGS = -Wall $(DEBUGGING) $(OPTIMIZATION) $(INCLUDES) $(DEFINES) -LFLAGS = -Wl,$(LIBRARIES) - SRCS = main.c \ + ${BACNET_PORT_DIR}/timer.c \ ${BACNET_SOURCE_DIR}/crc.c OBJS = ${SRCS:.c=.o} diff --git a/bacnet-stack/demo/mstpcrc/main.c b/bacnet-stack/demo/mstpcrc/main.c index c27eb204..b0e11e9a 100644 --- a/bacnet-stack/demo/mstpcrc/main.c +++ b/bacnet-stack/demo/mstpcrc/main.c @@ -39,8 +39,13 @@ #include #include #include +#include #include +/* OS specific include*/ +#include "net.h" +#include "timer.h" /* local includes */ +#include "bytes.h" #include "crc.h" #include "version.h" @@ -55,6 +60,10 @@ static unsigned CRC_Buffer_Len = 0; /* flags needed for options */ static bool ASCII_Decimal = false; static unsigned CRC_Size = 8; +/* save to capture file for viewing in Wireshark */ +static bool MSTP_Cap = false; +static char Capture_Filename[32] = "mstp_20090123091200.cap"; +static FILE *pFile = NULL; /* stream pointer */ /****************************************************************** * DESCRIPTION: Takes one of the arguments passed by the main function @@ -99,6 +108,10 @@ static void Parse_Arguments(int argc, char *argv[]) case 'D': ASCII_Decimal = true; break; + case 'm': + case 'M': + MSTP_Cap = true; + break; default: break; } @@ -115,6 +128,85 @@ static void Parse_Arguments(int argc, char *argv[]) } } +static void filename_create( + char *filename) +{ + time_t my_time; + struct tm *today; + + if (filename) { + my_time = time(NULL); + today = localtime(&my_time); + sprintf(filename, "mstp_%04d%02d%02d%02d%02d%02d.cap", + 1900 + today->tm_year, 1 + today->tm_mon, today->tm_mday, + today->tm_hour, today->tm_min, today->tm_sec); + } +} + +/* write packet to file in libpcap format */ +static void write_global_header( + const char *filename) +{ + uint32_t magic_number = 0xa1b2c3d4; /* magic number */ + uint16_t version_major = 2; /* major version number */ + uint16_t version_minor = 4; /* minor version number */ + int32_t thiszone = 0; /* GMT to local correction */ + uint32_t sigfigs = 0; /* accuracy of timestamps */ + uint32_t snaplen = 65535; /* max length of captured packets, in octets */ + uint32_t network = 165; /* data link type - BACNET_MS_TP */ + + /* create a new file. */ + pFile = fopen(filename, "wb"); + if (pFile) { + (void) fwrite(&magic_number, sizeof(magic_number), 1, pFile); + (void) fwrite(&version_major, sizeof(version_major), 1, pFile); + (void) fwrite(&version_minor, sizeof(version_minor), 1, pFile); + (void) fwrite(&thiszone, sizeof(thiszone), 1, pFile); + (void) fwrite(&sigfigs, sizeof(sigfigs), 1, pFile); + (void) fwrite(&snaplen, sizeof(snaplen), 1, pFile); + (void) fwrite(&network, sizeof(network), 1, pFile); + fflush(pFile); + fprintf(stdout, "mstpcap: saving capture to %s\n", filename); + } else { + fprintf(stderr, "mstpcap[header]: failed to open %s: %s\n", filename, + strerror(errno)); + } +} + +static void write_received_packet(uint8_t *buffer, unsigned length) +{ + uint32_t ts_sec; /* timestamp seconds */ + uint32_t ts_usec; /* timestamp microseconds */ + uint32_t incl_len; /* number of octets of packet saved in file */ + uint32_t orig_len; /* actual length of packet */ + struct timeval tv; + + if (pFile) { + gettimeofday(&tv, NULL); + ts_sec = tv.tv_sec; + ts_usec = tv.tv_usec; + (void) fwrite(&ts_sec, sizeof(ts_sec), 1, pFile); + (void) fwrite(&ts_usec, sizeof(ts_usec), 1, pFile); + orig_len = incl_len = length; + (void) fwrite(&incl_len, sizeof(incl_len), 1, pFile); + (void) fwrite(&orig_len, sizeof(orig_len), 1, pFile); + (void) fwrite(buffer, length, 1, pFile); + } else { + fprintf(stderr, "mstpcrc[packet]: failed to open %s: %s\n", + Capture_Filename, strerror(errno)); + } +} + +static void Write_Pcap(uint8_t *buffer, unsigned length) +{ + filename_create(&Capture_Filename[0]); + write_global_header(&Capture_Filename[0]); + write_received_packet(buffer, length); + if (pFile) { + fclose(pFile); + } +} + /* simple program to CRC the data and print the CRC */ int main( int argc, @@ -132,6 +224,7 @@ int main( "options:\r\n" "[-x] interprete the arguments as ascii hex (default)\r\n" "[-d] interprete the argument as ascii decimal\r\n" + "[-m] Write the bytes to Wireshark capture file\r\n" "[-8] calculate the MS/TP 8-bit Header CRC (default)\r\n" "[-16] calculate the MS/TP 16-bit Data CRC\r\n" "[-32] calculate the MS/TP 32-bit Extended Frame CRC\r\n"); @@ -147,6 +240,9 @@ int main( } Parse_Arguments(argc, argv); if (CRC_Buffer_Len) { + if (MSTP_Cap) { + Write_Pcap(CRC_Buffer, CRC_Buffer_Len); + } else { for (i = 0; i < CRC_Buffer_Len; i++) { if (CRC_Size == 8) { crc8 = CRC_Calc_Header(CRC_Buffer[i], crc8); @@ -176,6 +272,7 @@ int main( printf("0x%02X Data CRC\r\n", (crc16 >> 8)); } } + } } return 1;