Release tarball for barry-0.9
[barry.git] / src / packet.cc
blob6806e98859329d06af84acc7a2bb7fdebf5fb60b
1 ///
2 /// \file packet.cc
3 /// Low level protocol packet builder class.
4 /// Has knowledge of specific protocol commands in order
5 /// to hide protocol details behind an API.
6 ///
8 /*
9 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 See the GNU General Public License in the COPYING file at the
21 root directory of this project for more details.
24 #include "packet.h"
25 #include "controller.h"
26 #include "protocol.h"
27 #include "protostructs.h"
28 #include "data.h"
29 #include "endian.h"
30 #include "parser.h"
31 #include "builder.h"
32 #include "error.h"
34 #define __DEBUG_MODE__
35 #include "debug.h"
38 namespace Barry {
40 //////////////////////////////////////////////////////////////////////////////
41 // Packet base class
44 // Command
46 /// Returns the command value of the receive packet. If receive isn't
47 /// large enough, throws Error.
48 ///
49 unsigned int Packet::Command() const
51 Protocol::CheckSize(m_receive);
52 MAKE_PACKET(rpack, m_receive);
53 return rpack->command;
57 //////////////////////////////////////////////////////////////////////////////
58 // ZeroPacket class
60 ZeroPacket::ZeroPacket(Data &send, Data &receive)
61 : Packet(send, receive)
65 ZeroPacket::~ZeroPacket()
70 // GetAttribute
72 /// Builds a command packet for the initial socket-0 handshakes
73 /// that fetch certain (some unknown) attributes. The attributes
74 /// appear to exist in an object/attribute sequence, so that's
75 /// how we address them here.
76 ///
77 void ZeroPacket::GetAttribute(unsigned int object, unsigned int attribute)
79 size_t size = SB_SOCKET_PACKET_HEADER_SIZE + ATTRIBUTE_FETCH_COMMAND_SIZE;
80 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(size));
81 Protocol::Packet &packet = *cpack;
83 packet.socket = 0;
84 packet.size = htobs(size);
85 packet.command = SB_COMMAND_FETCH_ATTRIBUTE;
86 packet.u.socket.socket = htobs(0x00ff); // default non-socket request
87 packet.u.socket.sequence = 0; // filled in by Socket class
88 packet.u.socket.u.fetch.object = htobs(object);
89 packet.u.socket.u.fetch.attribute = htobs(attribute);
91 m_send.ReleaseBuffer(size);
94 unsigned int ZeroPacket::ObjectID() const
96 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE);
97 MAKE_PACKET(rpack, m_receive);
98 return btohs(rpack->u.socket.u.fetch.object);
101 unsigned int ZeroPacket::AttributeID() const
103 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE);
104 MAKE_PACKET(rpack, m_receive);
105 return btohs(rpack->u.socket.u.fetch.attribute);
108 uint32_t ZeroPacket::ChallengeSeed() const
110 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE +
111 PASSWORD_CHALLENGE_SEED_SIZE);
112 MAKE_PACKET(rpack, m_receive);
113 return btohl(rpack->u.socket.u.password.u.seed);
116 unsigned int ZeroPacket::RemainingTries() const
118 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE +
119 PASSWORD_CHALLENGE_HEADER_SIZE);
120 MAKE_PACKET(rpack, m_receive);
121 // this is a byte, so no byte swapping needed
122 return rpack->u.socket.u.password.remaining_tries;
125 unsigned int ZeroPacket::SocketResponse() const
127 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE);
128 MAKE_PACKET(rpack, m_receive);
129 return btohs(rpack->u.socket.socket);
132 unsigned char ZeroPacket::SocketSequence() const
134 Protocol::CheckSize(m_receive, SB_SOCKET_PACKET_HEADER_SIZE);
135 MAKE_PACKET(rpack, m_receive);
136 return rpack->u.socket.sequence; // sequence is a byte
141 //////////////////////////////////////////////////////////////////////////////
142 // DBPacket class
144 DBPacket::DBPacket(Controller &con, Data &send, Data &receive)
145 : Packet(send, receive)
146 , m_con(con)
147 , m_last_dbop(0)
151 DBPacket::~DBPacket()
156 // ClearDatabase
158 /// Builds a command packet for the CLEAR_DATABASE command code, placing
159 /// the data in the send buffer.
161 void DBPacket::ClearDatabase(unsigned int dbId)
163 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(9));
164 Protocol::Packet &packet = *cpack;
166 packet.socket = htobs(m_con.m_socket.GetSocket());
167 packet.size = htobs(9);
168 packet.command = SB_COMMAND_DB_DATA;
169 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
170 packet.u.db.u.command.operation = SB_DBOP_CLEAR_DATABASE;
171 packet.u.db.u.command.databaseId = htobs(dbId);
173 m_send.ReleaseBuffer(9);
175 m_last_dbop = SB_DBOP_CLEAR_DATABASE;
179 // GetDBDB
181 /// Builds a command packet for the GET_DBDB command code, placing the
182 /// data in m_send.
184 void DBPacket::GetDBDB()
186 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(7));
187 Protocol::Packet &packet = *cpack;
189 packet.socket = htobs(m_con.m_socket.GetSocket());
190 packet.size = htobs(7);
191 packet.command = SB_COMMAND_DB_DATA;
192 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
193 // packet.u.db.u.command.operation = SB_DBOP_GET_DBDB;
194 packet.u.db.u.command.operation = SB_DBOP_OLD_GET_DBDB;
196 m_send.ReleaseBuffer(7);
198 m_last_dbop = SB_DBOP_OLD_GET_DBDB;
202 // GetRecordStateTable
204 /// Builds a command packet in the send buffer for the
205 /// GET_RECORD_STATE_TABLE command.
207 void DBPacket::GetRecordStateTable(unsigned int dbId)
209 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(9));
210 Protocol::Packet &packet = *cpack;
212 packet.socket = htobs(m_con.m_socket.GetSocket());
213 packet.size = htobs(9);
214 packet.command = SB_COMMAND_DB_DATA;
215 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
216 packet.u.db.u.command.operation = SB_DBOP_GET_RECORD_STATE_TABLE;
217 packet.u.db.u.command.databaseId = htobs(dbId);
219 m_send.ReleaseBuffer(9);
221 m_last_dbop = SB_DBOP_GET_RECORD_STATE_TABLE;
225 // SetRecordFlags
227 /// Builds a command packet in the send buffer for the SET_RECORD_FLAGS
228 /// command code.
230 /// FIXME - this API call is incomplete, since there are unknown flags
231 /// in the SetRecordFlags protocol packet. Currently it is only
232 /// used to set all flags to zero.
234 void DBPacket::SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex,
235 uint8_t flag1)
237 size_t size = SB_PACKET_COMMAND_HEADER_SIZE + DBC_RECORD_FLAGS_SIZE;
238 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(size));
239 Protocol::Packet &packet = *cpack;
241 packet.socket = htobs(m_con.m_socket.GetSocket());
242 packet.size = htobs(size);
243 packet.command = SB_COMMAND_DB_DATA;
244 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
245 packet.u.db.u.command.operation = SB_DBOP_SET_RECORD_FLAGS;
246 packet.u.db.u.command.databaseId = htobs(dbId);
247 packet.u.db.u.command.u.flags.unknown = flag1;
248 packet.u.db.u.command.u.flags.index = htobs(stateTableIndex);
249 memset(packet.u.db.u.command.u.flags.unknown2, 0, sizeof(packet.u.db.u.command.u.flags.unknown2));
251 m_send.ReleaseBuffer(size);
253 m_last_dbop = SB_DBOP_SET_RECORD_FLAGS;
257 // DeleteRecordByIndex
259 /// Builds a command packet in the send buffer for the DELETE_RECORD_BY_INDEX
260 /// command code.
262 void DBPacket::DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex)
264 size_t size = SB_PACKET_COMMAND_HEADER_SIZE + DBC_RECORD_HEADER_SIZE;
265 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(size));
266 Protocol::Packet &packet = *cpack;
268 packet.socket = htobs(m_con.m_socket.GetSocket());
269 packet.size = htobs(size);
270 packet.command = SB_COMMAND_DB_DATA;
271 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
272 packet.u.db.u.command.operation = SB_DBOP_DELETE_RECORD_BY_INDEX;
273 packet.u.db.u.command.databaseId = htobs(dbId);
274 packet.u.db.u.command.u.record.recordIndex = htobs(stateTableIndex);
276 m_send.ReleaseBuffer(size);
278 m_last_dbop = SB_DBOP_DELETE_RECORD_BY_INDEX;
282 // GetRecordByIndex
284 /// Builds a command packet in the send buffer for the GET_RECORD_BY_INDEX
285 /// command code.
287 void DBPacket::GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex)
289 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(11));
290 Protocol::Packet &packet = *cpack;
292 packet.socket = htobs(m_con.m_socket.GetSocket());
293 packet.size = htobs(11);
294 packet.command = SB_COMMAND_DB_DATA;
295 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
296 packet.u.db.u.command.operation = SB_DBOP_GET_RECORD_BY_INDEX;
297 packet.u.db.u.command.databaseId = htobs(dbId);
298 packet.u.db.u.command.u.record.recordIndex = htobs(stateTableIndex);
300 m_send.ReleaseBuffer(11);
302 m_last_dbop = SB_DBOP_GET_RECORD_BY_INDEX;
306 // SetRecordByIndex
308 /// Builds a command packet in the m_send buffer for the SET_RECORD_BY_INDEX
309 /// command code.
311 /// \return bool
312 /// - true means success
313 /// - false means no data available from Builder object
315 bool DBPacket::SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex,
316 Builder &build)
318 // get new data if available
319 if( !build.Retrieve(dbId) )
320 return false;
322 // build packet data
323 size_t header_size = SB_PACKET_COMMAND_HEADER_SIZE + DBC_INDEXED_UPLOAD_HEADER_SIZE;
324 build.BuildFields(m_send, header_size);
325 size_t total_size = m_send.GetSize();
327 // fill in the header values
328 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(total_size));
329 Protocol::Packet &packet = *cpack;
331 packet.socket = htobs(m_con.m_socket.GetSocket());
332 packet.size = htobs(total_size);
333 packet.command = SB_COMMAND_DB_DATA;
334 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
335 packet.u.db.u.command.operation = SB_DBOP_SET_RECORD_BY_INDEX;
336 packet.u.db.u.command.databaseId = htobs(dbId);
337 packet.u.db.u.command.u.index_upload.unknown = 0;
338 packet.u.db.u.command.u.index_upload.index = htobs(stateTableIndex);
340 m_send.ReleaseBuffer(total_size);
342 m_last_dbop = SB_DBOP_SET_RECORD_BY_INDEX;
343 return true;
347 // GetRecords
349 /// Builds a command packet in the send buffer for the GET_RECORDS
350 /// command code.
352 void DBPacket::GetRecords(unsigned int dbId)
354 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(9));
355 Protocol::Packet &packet = *cpack;
357 packet.socket = htobs(m_con.m_socket.GetSocket());
358 packet.size = htobs(9);
359 packet.command = SB_COMMAND_DB_DATA;
360 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
361 packet.u.db.u.command.operation = SB_DBOP_OLD_GET_RECORDS;
362 packet.u.db.u.command.databaseId = htobs(dbId);
364 m_send.ReleaseBuffer(9);
366 m_last_dbop = SB_DBOP_OLD_GET_RECORDS;
370 // SetRecord
372 /// Builds a command packet in the m_send buffer for the SET_RECORD command
373 /// code.
375 /// \return bool
376 /// - true means success
377 /// - false means no data available from Builder object
379 bool DBPacket::SetRecord(unsigned int dbId, Builder &build)
381 // get new data if available
382 if( !build.Retrieve(dbId) )
383 return false;
385 // build packet data
386 size_t header_size = SB_PACKET_COMMAND_HEADER_SIZE + DBC_TAGGED_UPLOAD_HEADER_SIZE;
387 build.BuildHeader(m_send, header_size);
388 build.BuildFields(m_send, header_size);
389 size_t total_size = m_send.GetSize();
391 // fill in the header values
392 MAKE_PACKETPTR_BUF(cpack, m_send.GetBuffer(total_size));
393 Protocol::Packet &packet = *cpack;
395 packet.socket = htobs(m_con.m_socket.GetSocket());
396 packet.size = htobs(total_size);
397 packet.command = SB_COMMAND_DB_DATA;
398 packet.u.db.tableCmd = m_con.GetCommand(Controller::DatabaseAccess);
399 packet.u.db.u.command.operation = SB_DBOP_SET_RECORD;
400 packet.u.db.u.command.databaseId = htobs(dbId);
401 packet.u.db.u.command.u.tag_upload.rectype = build.GetRecType();
402 packet.u.db.u.command.u.tag_upload.uniqueId = htobl(build.GetUniqueId());
403 packet.u.db.u.command.u.tag_upload.unknown2 = 1; // unknown observed value
405 m_send.ReleaseBuffer(total_size);
407 m_last_dbop = SB_DBOP_SET_RECORD;
408 return true;
412 // throws FIXME if packet doesn't support it
413 unsigned int DBPacket::ReturnCode() const
415 if( Command() == SB_COMMAND_DB_DONE ) {
416 Protocol::CheckSize(m_receive, SB_PACKET_DBACCESS_HEADER_SIZE + SB_DBACCESS_RETURN_CODE_SIZE);
417 MAKE_PACKET(rpack, m_receive);
418 return rpack->u.db.u.return_code;
420 else {
421 throw Error("Attempting to extract a return code from the wrong response packet type");
426 // DBOperation
428 /// Returns the database operation code from the receive packet, assuming
429 /// that receive contains a response packet. If receive isn't large
430 /// enough, throws Error.
432 unsigned int DBPacket::DBOperation() const
434 Protocol::CheckSize(m_receive, SB_PACKET_RESPONSE_HEADER_SIZE);
435 MAKE_PACKET(rpack, m_receive);
436 return rpack->u.db.u.response.operation;
440 // Parse
442 /// Parses the data in the receive buffer, and attempts to be smart about it,
443 /// using the last send command as guidance for what to expect in the
444 /// response.
446 /// \returns bool true - packet was recognized and parse was attempted
447 /// false - packet was not recognized
449 bool DBPacket::Parse(Parser &parser)
451 size_t offset = 0;
452 MAKE_PACKET(rpack, m_receive);
454 switch( m_last_dbop )
456 case SB_DBOP_OLD_GET_RECORDS:
457 case SB_DBOP_GET_RECORD_BY_INDEX:
458 parser.Clear();
460 offset = SB_PACKET_RESPONSE_HEADER_SIZE + DBR_OLD_TAGGED_RECORD_HEADER_SIZE;
461 Protocol::CheckSize(m_receive, offset);
462 // FIXME - this may need adjustment for email records... they
463 // don't seem to have uniqueID's
464 parser.SetIds(rpack->u.db.u.response.u.tagged.rectype,
465 btohl(rpack->u.db.u.response.u.tagged.uniqueId));
467 parser.ParseHeader(m_receive, offset);
468 parser.ParseFields(m_receive, offset);
469 parser.Store();
470 return true;
472 default: // unknown command
473 return false;
477 } // namespace Barry