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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <arpa/inet.h>
29 #include "vdrrequestpacket.h"
33 ULONG VDR_RequestPacket::serialNumberCounter = 1;
35 VDR_RequestPacket::VDR_RequestPacket()
46 VDR_RequestPacket::~VDR_RequestPacket()
51 bool VDR_RequestPacket::init(ULONG topcode, bool setUserDataLength, ULONG userDataLength)
53 if (buffer) return false;
55 if (setUserDataLength)
57 bufSize = headerLength + userDataLength;
63 userDataLength = 0; // so the below will write a zero
66 buffer = (UCHAR*)malloc(bufSize);
67 if (!buffer) return false;
69 channel = VDR::CHANNEL_REQUEST_RESPONSE;
70 serialNumber = serialNumberCounter++;
73 *(ULONG*)&buffer[0] = htonl(channel);
74 *(ULONG*)&buffer[4] = htonl(serialNumber);
75 *(ULONG*)&buffer[8] = htonl(opcode);
76 *(ULONG*)&buffer[userDataLenPos] = htonl(userDataLength);
77 bufUsed = headerLength;
82 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
84 if (!checkExtend(len)) return false;
85 memcpy(buffer + bufUsed, src, len);
87 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
91 bool VDR_RequestPacket::addString(const char* string)
93 ULONG len = strlen(string) + 1;
94 if (!checkExtend(len)) return false;
95 memcpy(buffer + bufUsed, string, len);
97 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
101 bool VDR_RequestPacket::addULONG(ULONG ul)
103 if (!checkExtend(sizeof(ULONG))) return false;
104 *(ULONG*)&buffer[bufUsed] = htonl(ul);
105 bufUsed += sizeof(ULONG);
106 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
110 bool VDR_RequestPacket::addULLONG(ULLONG ull)
112 if (!checkExtend(sizeof(ULLONG))) return false;
113 *(ULLONG*)&buffer[bufUsed] = htonll(ull);
114 bufUsed += sizeof(ULLONG);
115 if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
119 bool VDR_RequestPacket::checkExtend(ULONG by)
121 if (lengthSet) return true;
122 if ((bufUsed + by) < bufSize) return true;
123 UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + 512);
124 if (!newBuf) return false;