blob: a5826c768093b3388a9c09427b626f2430f6dcdc [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2006-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20 */
21
22#ifndef _ZRTPPACKETBASE_H_
23#define _ZRTPPACKETBASE_H_
24
25/**
26 * @file ZrtpPacketBase.h
27 * @brief The ZRTP message header class
28 *
29 * This class defines the ZRTP message header and provides access and
30 * check methods.
31 *
32 * @ingroup GNU_ZRTP
33 * @{
34 */
35
36#include <stdio.h>
37#include <stdint.h>
38#include <string.h>
39#include <stdlib.h>
40
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050041#include <common/osSpecifics.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050042
43#include <libzrtpcpp/zrtpPacket.h>
44#include <libzrtpcpp/ZrtpTextData.h>
45#include <libzrtpcpp/ZrtpConfigure.h>
46#include <libzrtpcpp/ZrtpCrc32.h>
47
48// #define DEBUGOUT(deb) deb
49#define DEBUGOUT(deb)
50
51/*
52 * This is the unique ZRTP ID in network order (PZ)
53 */
54const uint16_t zrtpId = 0x505a;
55
56/**
57 * This is the base class for all ZRTP packets
58 *
59 * All other ZRTP packet classes inherit from this class. It does not have
60 * an implementation of its own.
61 *
62 * The standard constructors of the subclasses usually initialize the @c allocate
63 * field with their fixed data array which is large enough to hold all message
64 * data. If an implementation needs to change this to use dynamic memory
65 * allocation only that line in the subclasses must be changed and the destructors
66 * should take care of memory management.
67 *
68 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
69 */
70
71class __EXPORT ZrtpPacketBase {
72
73 private:
74
75 protected:
76 void* allocated; ///< Pointer to ZRTP message data
77 zrtpPacketHeader_t* zrtpHeader; ///< points to the fixed ZRTP header structure
78
79 public:
80 /**
81 * Destructor is empty
82 */
83 virtual ~ZrtpPacketBase() {};
84
85 /**
86 * Get pointer to ZRTP header
87 *
88 * @return
89 * Pointer to ZRTP header structure.
90 */
91 const uint8_t* getHeaderBase() { return (const uint8_t*)zrtpHeader; };
92
93 /**
94 * Check is this is a ZRTP message
95 *
96 * @return
97 * @c true if check was ok
98 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050099 bool isZrtpPacket() { return (zrtpNtohs(zrtpHeader->zrtpId) == zrtpId); };
Alexandre Lision51140e12013-12-02 10:54:09 -0500100
101 /**
102 * Get the length in words of the ZRTP message
103 *
104 * @return
105 * The length in words
106 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500107 uint16_t getLength() { return zrtpNtohs(zrtpHeader->length); };
Alexandre Lision51140e12013-12-02 10:54:09 -0500108
109 /**
110 * Return pointer to fixed length message type ASCII data
111 *
112 * @return
113 * Pointer to ASCII character array
114 */
115 uint8_t* getMessageType() { return zrtpHeader->messageType; };
116
117 /**
118 * Set the lenght field in the ZRTP header
119 *
120 * @param len
121 * The length of the ZRTP message in words, host order
122 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500123 void setLength(uint16_t len) { zrtpHeader->length = zrtpHtons(len); };
Alexandre Lision51140e12013-12-02 10:54:09 -0500124
125 /**
126 * Copy the message type ASCII data to ZRTP message type field
127 *
128 * @param msg
129 * Pointer to message type ASCII character array
130 */
131 void setMessageType(uint8_t *msg)
132 { memcpy(zrtpHeader->messageType, msg, sizeof(zrtpHeader->messageType)); };
133
134 /**
135 * Initializes the ZRTP Id field
136 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500137 void setZrtpId() { zrtpHeader->zrtpId = zrtpHtons(zrtpId); }
Alexandre Lision51140e12013-12-02 10:54:09 -0500138};
139
140/**
141 * @}
142 */
143#endif // ZRTPPACKETBASE