2 Copyright 2007 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <arpa/inet.h>
25 #include "vdrrequestpacket.h"
29 ULONG VDR_RequestPacket::serialNumberCounter = 1;
31 VDR_RequestPacket::VDR_RequestPacket()
42 VDR_RequestPacket::~VDR_RequestPacket()
47 bool VDR_RequestPacket::init(ULONG topcode, bool setUserDataLength, ULONG userDataLength)
49 if (buffer) return false;
51 if (setUserDataLength)
53 bufSize = headerLength + userDataLength;
59 userDataLength = 0; // so the below will write a zero
62 buffer = (UCHAR*)malloc(bufSize);
63 if (!buffer) return false;
65 channel = VDR::CHANNEL_REQUEST_RESPONSE;
66 serialNumber = serialNumberCounter++;
69 *(ULONG*)&buffer[0] = htonl(channel);
70 *(ULONG*)&buffer[4] = htonl(serialNumber);
71 *(ULONG*)&buffer[8] = htonl(opcode);
72 *(ULONG*)&buffer[userDataLenPos] = htonl(userDataLength);
73 bufUsed = headerLength;
78 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
80 if (!checkExtend(len)) return false;
81 memcpy(buffer + bufUsed, src, len);
83 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
87 bool VDR_RequestPacket::addString(const char* string)
89 ULONG len = strlen(string) + 1;
90 if (!checkExtend(len)) return false;
91 memcpy(buffer + bufUsed, string, len);
93 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
97 bool VDR_RequestPacket::addULONG(ULONG ul)
99 if (!checkExtend(sizeof(ULONG))) return false;
100 *(ULONG*)&buffer[bufUsed] = htonl(ul);
101 bufUsed += sizeof(ULONG);
102 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
106 bool VDR_RequestPacket::addULLONG(ULLONG ull)
108 if (!checkExtend(sizeof(ULLONG))) return false;
109 *(ULLONG*)&buffer[bufUsed] = htonll(ull);
110 bufUsed += sizeof(ULLONG);
111 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
115 bool VDR_RequestPacket::checkExtend(ULONG by)
117 if (lengthSet) return true;
118 if ((bufUsed + by) < bufSize) return true;
119 UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + 512);
120 if (!newBuf) return false;