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++;
74 buffer[pos++]=(channel>>24)&0xff;
75 buffer[pos++]=(channel>>16)&0xff;
76 buffer[pos++]=(channel>>8)&0xff;
77 buffer[pos++]=channel &0xff;
78 buffer[pos++]=(serialNumber>>24)&0xff;
79 buffer[pos++]=(serialNumber>>16)&0xff;
80 buffer[pos++]=(serialNumber>>8)&0xff;
81 buffer[pos++]=serialNumber &0xff;
82 buffer[pos++]=(opcode>>24)&0xff;
83 buffer[pos++]=(opcode>>16)&0xff;
84 buffer[pos++]=(opcode>>8)&0xff;
85 buffer[pos++]=opcode &0xff;
86 buffer[pos++]=(userDataLength>>24)&0xff;
87 buffer[pos++]=(userDataLength>>16)&0xff;
88 buffer[pos++]=(userDataLength>>8)&0xff;
89 buffer[pos++]=userDataLength &0xff;
91 /**(ULONG*)&buffer[4] = htonl(serialNumber);
92 *(ULONG*)&buffer[8] = htonl(opcode);
93 *(ULONG*)&buffer[userDataLenPos] = htonl(userDataLength);*/
94 bufUsed = headerLength;
99 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
101 if (!checkExtend(len)) return false;
102 memcpy(buffer + bufUsed, src, len);
105 int pos=userDataLenPos;
106 ULONG tocopy=bufUsed - headerLength;
107 buffer[pos++]=(tocopy>>24)&0xff;
108 buffer[pos++]=(tocopy>>16)&0xff;
109 buffer[pos++]=(tocopy>>8)&0xff;
110 buffer[pos++]=tocopy &0xff;
117 bool VDR_RequestPacket::addString(const char* string)
119 ULONG len = strlen(string) + 1;
120 if (!checkExtend(len)) return false;
121 memcpy(buffer + bufUsed, string, len);
124 int pos=userDataLenPos;
125 ULONG tocopy=bufUsed - headerLength;
126 buffer[pos++]=(tocopy>>24)&0xff;
127 buffer[pos++]=(tocopy>>16)&0xff;
128 buffer[pos++]=(tocopy>>8)&0xff;
129 buffer[pos++]=tocopy &0xff;
134 bool VDR_RequestPacket::addULONG(ULONG ul)
136 if (!checkExtend(sizeof(ULONG))) return false;
137 // *(ULONG*)&buffer[bufUsed] = htonl(ul);
139 buffer[bufUsed++]=(ul>>24)&0xff;
140 buffer[bufUsed++]=(ul>>16)&0xff;
141 buffer[bufUsed++]=(ul>>8)&0xff;
142 buffer[bufUsed++]=ul &0xff;
145 int pos=userDataLenPos;
146 ULONG tocopy=bufUsed - headerLength;
147 buffer[pos++]=(tocopy>>24)&0xff;
148 buffer[pos++]=(tocopy>>16)&0xff;
149 buffer[pos++]=(tocopy>>8)&0xff;
150 buffer[pos++]=tocopy &0xff;
155 bool VDR_RequestPacket::addULLONG(ULLONG ull)
157 if (!checkExtend(sizeof(ULLONG))) return false;
158 buffer[bufUsed++]=(ull>>56)&0xff;
159 buffer[bufUsed++]=(ull>>48)&0xff;
160 buffer[bufUsed++]=(ull>>40)&0xff;
161 buffer[bufUsed++]=(ull>>32)&0xff;
162 buffer[bufUsed++]=(ull>>24)&0xff;
163 buffer[bufUsed++]=(ull>>16)&0xff;
164 buffer[bufUsed++]=(ull>>8)&0xff;
165 buffer[bufUsed++]=ull &0xff;
168 int pos=userDataLenPos;
169 ULONG tocopy=bufUsed - headerLength;
170 buffer[pos++]=(tocopy>>24)&0xff;
171 buffer[pos++]=(tocopy>>16)&0xff;
172 buffer[pos++]=(tocopy>>8)&0xff;
173 buffer[pos++]=tocopy &0xff;
178 bool VDR_RequestPacket::checkExtend(ULONG by)
180 if (lengthSet) return true;
181 if ((bufUsed + by) < bufSize) return true;
182 UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + 512);
183 if (!newBuf) return false;