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>
25 #include "responsepacket.h"
28 /* Packet format for an RR channel response:
30 4 bytes = channel ID = 1 (request/response channel)
31 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 ResponsePacket::ResponsePacket()
43 ResponsePacket::~ResponsePacket()
45 if (buffer) free(buffer);
48 bool ResponsePacket::init(ULONG requestID)
50 if (buffer) return false;
53 buffer = (UCHAR*)malloc(bufSize);
54 if (!buffer) return false;
56 *(ULONG*)&buffer[0] = htonl(1); // RR channel
57 *(ULONG*)&buffer[4] = htonl(requestID);
58 *(ULONG*)&buffer[userDataLenPos] = 0;
59 bufUsed = headerLength;
64 void ResponsePacket::finalise()
66 *(ULONG*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
67 Log::getInstance()->log("Client", Log::DEBUG, "RP finalise %lu", bufUsed - headerLength);
70 bool ResponsePacket::copyin(const UCHAR* src, ULONG len)
72 if (!checkExtend(len)) return false;
73 memcpy(buffer + bufUsed, src, len);
78 bool ResponsePacket::addString(const char* string)
80 ULONG len = strlen(string) + 1;
81 if (!checkExtend(len)) return false;
82 memcpy(buffer + bufUsed, string, len);
87 bool ResponsePacket::addULONG(ULONG ul)
89 if (!checkExtend(sizeof(ULONG))) return false;
90 *(ULONG*)&buffer[bufUsed] = htonl(ul);
91 bufUsed += sizeof(ULONG);
95 bool ResponsePacket::addUCHAR(UCHAR c)
97 if (!checkExtend(sizeof(UCHAR))) return false;
99 bufUsed += sizeof(UCHAR);
103 bool ResponsePacket::addLONG(LONG l)
105 if (!checkExtend(sizeof(LONG))) return false;
106 *(LONG*)&buffer[bufUsed] = htonl(l);
107 bufUsed += sizeof(LONG);
111 bool ResponsePacket::addULLONG(ULLONG ull)
113 if (!checkExtend(sizeof(ULLONG))) return false;
114 *(ULLONG*)&buffer[bufUsed] = htonll(ull);
115 bufUsed += sizeof(ULLONG);
119 bool ResponsePacket::checkExtend(ULONG by)
121 if ((bufUsed + by) < bufSize) return true;
122 if (512 > by) by = 512;
123 UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + by);
124 if (!newBuf) return false;
130 ULLONG ResponsePacket::htonll(ULLONG a)
132 #if BYTE_ORDER == BIG_ENDIAN
137 b = ((a << 56) & 0xFF00000000000000ULL)
138 | ((a << 40) & 0x00FF000000000000ULL)
139 | ((a << 24) & 0x0000FF0000000000ULL)
140 | ((a << 8) & 0x000000FF00000000ULL)
141 | ((a >> 8) & 0x00000000FF000000ULL)
142 | ((a >> 24) & 0x0000000000FF0000ULL)
143 | ((a >> 40) & 0x000000000000FF00ULL)
144 | ((a >> 56) & 0x00000000000000FFULL) ;