From 022b716508ba0a2aac14e071b983a3f09139ff9c Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 21 Nov 2007 23:51:48 +0000 Subject: [PATCH] New protocol --- Makefile | 2 +- responsepacket.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++ responsepacket.h | 58 ++++++++++++++++++ 3 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 responsepacket.c create mode 100644 responsepacket.h diff --git a/Makefile b/Makefile index 9aa3d51..659d4e9 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ DEFINES += -D_GNU_SOURCE -DPLUGIN_NAME_I18N='"$(PLUGIN)"' OBJS = $(PLUGIN).o dsock.o mvpserver.o udpreplier.o bootpd.o tftpd.o mvpclient.o tcp.o \ ringbuffer.o mvprelay.o \ recplayer.o config.o log.o thread.o mvpreceiver.o tftpclient.o \ - media.o + media.o responsepacket.o ### Implicit rules: diff --git a/responsepacket.c b/responsepacket.c new file mode 100644 index 0000000..0130235 --- /dev/null +++ b/responsepacket.c @@ -0,0 +1,149 @@ +/* + Copyright 2007 Chris Tallon + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include + +#include "responsepacket.h" +#include "log.h" + +/* Packet format for an RR channel response: + +4 bytes = channel ID = 1 (request/response channel) +4 bytes = request ID (from serialNumber) +4 bytes = length of the rest of the packet +? bytes = rest of packet. depends on packet +*/ + +ResponsePacket::ResponsePacket() +{ + buffer = NULL; + bufSize = 0; + bufUsed = 0; +} + +ResponsePacket::~ResponsePacket() +{ + if (buffer) free(buffer); +} + +bool ResponsePacket::init(ULONG requestID) +{ + if (buffer) return false; + + bufSize = 512; + buffer = (UCHAR*)malloc(bufSize); + if (!buffer) return false; + + *(ULONG*)&buffer[0] = htonl(1); // RR channel + *(ULONG*)&buffer[4] = htonl(requestID); + *(ULONG*)&buffer[userDataLenPos] = 0; + bufUsed = headerLength; + + return true; +} + +void ResponsePacket::finalise() +{ + *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength); + Log::getInstance()->log("Client", Log::DEBUG, "RP finalise %lu", bufUsed - headerLength); +} + +bool ResponsePacket::copyin(const UCHAR* src, ULONG len) +{ + if (!checkExtend(len)) return false; + memcpy(buffer + bufUsed, src, len); + bufUsed += len; + return true; +} + +bool ResponsePacket::addString(const char* string) +{ + ULONG len = strlen(string) + 1; + if (!checkExtend(len)) return false; + memcpy(buffer + bufUsed, string, len); + bufUsed += len; + return true; +} + +bool ResponsePacket::addULONG(ULONG ul) +{ + if (!checkExtend(sizeof(ULONG))) return false; + *(ULONG*)&buffer[bufUsed] = htonl(ul); + bufUsed += sizeof(ULONG); + return true; +} + +bool ResponsePacket::addUCHAR(UCHAR c) +{ + if (!checkExtend(sizeof(UCHAR))) return false; + buffer[bufUsed] = c; + bufUsed += sizeof(UCHAR); + return true; +} + +bool ResponsePacket::addLONG(LONG l) +{ + if (!checkExtend(sizeof(LONG))) return false; + *(LONG*)&buffer[bufUsed] = htonl(l); + bufUsed += sizeof(LONG); + return true; +} + +bool ResponsePacket::addULLONG(ULLONG ull) +{ + if (!checkExtend(sizeof(ULLONG))) return false; + *(ULLONG*)&buffer[bufUsed] = htonll(ull); + bufUsed += sizeof(ULLONG); + return true; +} + +bool ResponsePacket::checkExtend(ULONG by) +{ + if ((bufUsed + by) < bufSize) return true; + if (512 > by) by = 512; + UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + by); + if (!newBuf) return false; + buffer = newBuf; + bufSize += by; + return true; +} + +ULLONG ResponsePacket::htonll(ULLONG a) +{ + #if BYTE_ORDER == BIG_ENDIAN + return a; + #else + ULLONG b = 0; + + b = ((a << 56) & 0xFF00000000000000ULL) + | ((a << 40) & 0x00FF000000000000ULL) + | ((a << 24) & 0x0000FF0000000000ULL) + | ((a << 8) & 0x000000FF00000000ULL) + | ((a >> 8) & 0x00000000FF000000ULL) + | ((a >> 24) & 0x0000000000FF0000ULL) + | ((a >> 40) & 0x000000000000FF00ULL) + | ((a >> 56) & 0x00000000000000FFULL) ; + + return b; + #endif +} + diff --git a/responsepacket.h b/responsepacket.h new file mode 100644 index 0000000..e091701 --- /dev/null +++ b/responsepacket.h @@ -0,0 +1,58 @@ +/* + Copyright 2007 Chris Tallon + + This file is part of VOMP. + + VOMP is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + VOMP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef RESPONSEPACKET_H +#define RESPONSEPACKET_H + +#include "defines.h" + +class ResponsePacket +{ + public: + ResponsePacket(); + ~ResponsePacket(); + + bool init(ULONG requestID); + void finalise(); + bool copyin(const UCHAR* src, ULONG len); + bool addString(const char* string); + bool addULONG(ULONG ul); + bool addLONG(LONG l); + bool addUCHAR(UCHAR c); + bool addULLONG(ULLONG ull); + + UCHAR* getPtr() { return buffer; } + ULONG getLen() { return bufUsed; } + + private: + + UCHAR* buffer; + ULONG bufSize; + ULONG bufUsed; + + bool checkExtend(ULONG by); + ULLONG htonll(ULLONG a); + + const static ULONG headerLength = 12; + const static ULONG userDataLenPos = 8; +}; + +#endif + -- 2.39.5