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>
23 #include "vdrrequestpacket.h"
27 /* Packet format for an RR channel request:
29 4 bytes = channel ID = 1 (request/response channel)
30 4 bytes = request ID (from serialNumber)
32 4 bytes = length of the rest of the packet
33 ? bytes = rest of packet. depends on packet
36 ULONG VDR_RequestPacket::serialNumber = 1;
38 VDR_RequestPacket::VDR_RequestPacket()
46 VDR_RequestPacket::~VDR_RequestPacket()
51 bool VDR_RequestPacket::init(ULONG opcode, 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;
68 *(ULONG*)&buffer[0] = htonl(1);
69 *(ULONG*)&buffer[4] = htonl(serialNumber++);
70 *(ULONG*)&buffer[8] = htonl(opcode);
71 *(ULONG*)&buffer[12] = htonl(userDataLength);
72 bufUsed = headerLength;
77 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
79 if (!checkExtend(len)) return false;
80 memcpy(buffer + bufUsed, src, len);
82 if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
86 bool VDR_RequestPacket::addString(const char* string)
88 ULONG len = strlen(string) + 1;
89 if (!checkExtend(len)) return false;
90 memcpy(buffer + bufUsed, string, len);
92 if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
96 bool VDR_RequestPacket::addULONG(ULONG ul)
98 if (!checkExtend(sizeof(ULONG))) return false;
99 *(ULONG*)&buffer[bufUsed] = htonl(ul);
100 bufUsed += sizeof(ULONG);
101 if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
105 bool VDR_RequestPacket::addULLONG(ULLONG ull)
107 if (!checkExtend(sizeof(ULLONG))) return false;
108 *(ULLONG*)&buffer[bufUsed] = htonll(ull);
109 bufUsed += sizeof(ULLONG);
110 if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
114 bool VDR_RequestPacket::checkExtend(ULONG by)
116 if (lengthSet) return true;
117 if ((bufUsed + by) < bufSize) return true;
118 UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + 512);
119 if (!newBuf) return false;