]> git.vomp.tv Git - vompclient.git/blob - vdrrequestpacket.cc
FSF address change
[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 #include <arpa/inet.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "vdrrequestpacket.h"
26
27 #include "vdr.h"
28
29 ULONG VDR_RequestPacket::serialNumberCounter = 1;
30
31 VDR_RequestPacket::VDR_RequestPacket()
32 {
33   buffer = NULL;
34   bufSize = 0;
35   bufUsed = 0;
36   lengthSet = false;
37   serialNumber = 0;
38   
39   opcode = 0;
40 }
41
42 VDR_RequestPacket::~VDR_RequestPacket()
43 {
44   free(buffer);
45 }
46
47 bool VDR_RequestPacket::init(ULONG topcode, bool setUserDataLength, ULONG userDataLength)
48 {
49   if (buffer) return false;
50   
51   if (setUserDataLength)
52   {
53     bufSize = headerLength + userDataLength;
54     lengthSet = true;
55   }
56   else
57   {
58     bufSize = 512;
59     userDataLength = 0; // so the below will write a zero
60   }
61   
62   buffer = (UCHAR*)malloc(bufSize);
63   if (!buffer) return false;
64   
65   channel = VDR::CHANNEL_REQUEST_RESPONSE;
66   serialNumber = serialNumberCounter++;
67   opcode = topcode;
68   
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;
74
75   return true;
76 }
77
78 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
79 {
80   if (!checkExtend(len)) return false;
81   memcpy(buffer + bufUsed, src, len);
82   bufUsed += len;
83   if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
84   return true;
85 }
86
87 bool VDR_RequestPacket::addString(const char* string)
88 {
89   ULONG len = strlen(string) + 1;
90   if (!checkExtend(len)) return false;
91   memcpy(buffer + bufUsed, string, len);
92   bufUsed += len;
93   if (!lengthSet) *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
94   return true;
95 }
96
97 bool VDR_RequestPacket::addULONG(ULONG ul)
98 {
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);
103   return true;
104 }   
105
106 bool VDR_RequestPacket::addULLONG(ULLONG ull)
107 {
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);
112   return true;
113 }
114
115 bool VDR_RequestPacket::checkExtend(ULONG by)
116 {
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;
121   buffer = newBuf;
122   bufSize += 512;
123   return true;
124 }
125