]> git.vomp.tv Git - vompclient.git/blob - vdrrequestpacket.cc
Upgrade to protocol
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <arpa/inet.h>
22
23 #include "vdrrequestpacket.h"
24
25 #include "vdr.h"
26
27 /* Packet format for an RR channel request:
28
29 4 bytes = channel ID = 1 (request/response channel)
30 4 bytes = request ID (from serialNumber)
31 4 bytes = opcode
32 4 bytes = length of the rest of the packet
33 ? bytes = rest of packet. depends on packet
34 */
35
36 ULONG VDR_RequestPacket::serialNumber = 1;
37
38 VDR_RequestPacket::VDR_RequestPacket()
39 {
40   buffer = NULL;
41   bufSize = 0;
42   bufUsed = 0;
43   lengthSet = false;
44 }
45
46 VDR_RequestPacket::~VDR_RequestPacket()
47 {
48   free(buffer);
49 }
50
51 bool VDR_RequestPacket::init(ULONG opcode, 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   *(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;
73
74   return true;
75 }
76
77 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
78 {
79   if (!checkExtend(len)) return false;
80   memcpy(buffer + bufUsed, src, len);
81   bufUsed += len;
82   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
83   return true;
84 }
85
86 bool VDR_RequestPacket::addString(const char* string)
87 {
88   ULONG len = strlen(string) + 1;
89   if (!checkExtend(len)) return false;
90   memcpy(buffer + bufUsed, string, len);
91   bufUsed += len;
92   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
93   return true;
94 }
95
96 bool VDR_RequestPacket::addULONG(ULONG ul)
97 {
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);
102   return true;
103 }   
104
105 bool VDR_RequestPacket::addULLONG(ULLONG ull)
106 {
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);
111   return true;
112 }
113
114 bool VDR_RequestPacket::checkExtend(ULONG by)
115 {
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;
120   buffer = newBuf;
121   bufSize += 512;
122   return true;
123 }
124