]> git.vomp.tv Git - vompclient.git/blob - vdrrequestpacket.cc
More compiler warning fixes
[vompclient.git] / vdrrequestpacket.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19 */
20
21 #ifndef WIN32
22 #include <arpa/inet.h>
23 #else
24
25 #endif
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "vdrrequestpacket.h"
30
31 #include "vdr.h"
32
33 ULONG VDR_RequestPacket::serialNumberCounter = 1;
34
35 VDR_RequestPacket::VDR_RequestPacket()
36 {
37   buffer = NULL;
38   bufSize = 0;
39   bufUsed = 0;
40   lengthSet = false;
41   serialNumber = 0;
42   
43   opcode = 0;
44 }
45
46 VDR_RequestPacket::~VDR_RequestPacket()
47 {
48   free(buffer);
49 }
50
51 bool VDR_RequestPacket::init(ULONG topcode, bool setUserDataLength, ULONG userDataLength)
52 {
53   if (buffer) return false;
54   
55   if (setUserDataLength)
56   {
57     bufSize = headerLength + userDataLength;
58     lengthSet = true;
59   }
60   else
61   {
62     bufSize = 512;
63     userDataLength = 0; // so the below will write a zero
64   }
65   
66   buffer = (UCHAR*)malloc(bufSize);
67   if (!buffer) return false;
68   
69   channel = VDR::CHANNEL_REQUEST_RESPONSE;
70   serialNumber = serialNumberCounter++;
71   opcode = topcode;
72   
73   int pos=0;
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;
90
91   /**(ULONG*)&buffer[4] = htonl(serialNumber);
92   *(ULONG*)&buffer[8] = htonl(opcode);
93   *(ULONG*)&buffer[userDataLenPos] = htonl(userDataLength);*/
94   bufUsed = headerLength;
95
96   return true;
97 }
98
99 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
100 {
101   if (!checkExtend(len)) return false;
102   memcpy(buffer + bufUsed, src, len);
103   bufUsed += len;
104   if (!lengthSet) {
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;
111   }
112   return true;
113 }
114
115
116
117 bool VDR_RequestPacket::addString(const char* string)
118 {
119   ULONG len = strlen(string) + 1;
120   if (!checkExtend(len)) return false;
121   memcpy(buffer + bufUsed, string, len);
122   bufUsed += len;
123   if (!lengthSet) {
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;
130   }
131   return true;
132 }
133
134 bool VDR_RequestPacket::addULONG(ULONG ul)
135 {
136   if (!checkExtend(sizeof(ULONG))) return false;
137 //  *(ULONG*)&buffer[bufUsed] = htonl(ul);
138
139   buffer[bufUsed++]=(ul>>24)&0xff;
140   buffer[bufUsed++]=(ul>>16)&0xff;
141   buffer[bufUsed++]=(ul>>8)&0xff;
142   buffer[bufUsed++]=ul &0xff;
143
144   if (!lengthSet) {
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;
151   }
152   return true;
153 }   
154
155 bool VDR_RequestPacket::addULLONG(ULLONG ull)
156 {
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;
166
167   if (!lengthSet) {
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;
174   }
175   return true;
176 }
177
178 bool VDR_RequestPacket::checkExtend(ULONG by)
179 {
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;
184   buffer = newBuf;
185   bufSize += 512;
186   return true;
187 }
188