blob: 8beab7e1c523cf9c27887bb9da5ceed70258bcd6 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lisione24852d2014-02-04 13:13:02 -05002 Copyright (C) 2006-2010 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lisione24852d2014-02-04 13:13:02 -05005 it under the terms of the GNU 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>
Alexandre Lisione24852d2014-02-04 13:13:02 -050040#include <netinet/in.h>
Alexandre Lision907ed2e2014-02-04 10:33:09 -050041
Alexandre Lision51140e12013-12-02 10:54:09 -050042#include <libzrtpcpp/zrtpPacket.h>
43#include <libzrtpcpp/ZrtpTextData.h>
44#include <libzrtpcpp/ZrtpConfigure.h>
45#include <libzrtpcpp/ZrtpCrc32.h>
46
47// #define DEBUGOUT(deb) deb
48#define DEBUGOUT(deb)
49
50/*
51 * This is the unique ZRTP ID in network order (PZ)
52 */
53const uint16_t zrtpId = 0x505a;
54
55/**
56 * This is the base class for all ZRTP packets
57 *
58 * All other ZRTP packet classes inherit from this class. It does not have
59 * an implementation of its own.
60 *
61 * The standard constructors of the subclasses usually initialize the @c allocate
62 * field with their fixed data array which is large enough to hold all message
63 * data. If an implementation needs to change this to use dynamic memory
64 * allocation only that line in the subclasses must be changed and the destructors
65 * should take care of memory management.
66 *
67 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
68 */
69
70class __EXPORT ZrtpPacketBase {
71
72 private:
73
74 protected:
75 void* allocated; ///< Pointer to ZRTP message data
76 zrtpPacketHeader_t* zrtpHeader; ///< points to the fixed ZRTP header structure
77
78 public:
79 /**
80 * Destructor is empty
81 */
82 virtual ~ZrtpPacketBase() {};
83
84 /**
85 * Get pointer to ZRTP header
86 *
87 * @return
88 * Pointer to ZRTP header structure.
89 */
90 const uint8_t* getHeaderBase() { return (const uint8_t*)zrtpHeader; };
91
92 /**
93 * Check is this is a ZRTP message
94 *
95 * @return
96 * @c true if check was ok
97 */
Alexandre Lisione24852d2014-02-04 13:13:02 -050098 bool isZrtpPacket() { return (ntohs(zrtpHeader->zrtpId) == zrtpId); };
Alexandre Lision51140e12013-12-02 10:54:09 -050099
100 /**
101 * Get the length in words of the ZRTP message
102 *
103 * @return
104 * The length in words
105 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500106 uint16_t getLength() { return ntohs(zrtpHeader->length); };
Alexandre Lision51140e12013-12-02 10:54:09 -0500107
108 /**
109 * Return pointer to fixed length message type ASCII data
110 *
111 * @return
112 * Pointer to ASCII character array
113 */
114 uint8_t* getMessageType() { return zrtpHeader->messageType; };
115
116 /**
117 * Set the lenght field in the ZRTP header
118 *
119 * @param len
120 * The length of the ZRTP message in words, host order
121 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500122 void setLength(uint16_t len) { zrtpHeader->length = htons(len); };
Alexandre Lision51140e12013-12-02 10:54:09 -0500123
124 /**
125 * Copy the message type ASCII data to ZRTP message type field
126 *
127 * @param msg
128 * Pointer to message type ASCII character array
129 */
130 void setMessageType(uint8_t *msg)
131 { memcpy(zrtpHeader->messageType, msg, sizeof(zrtpHeader->messageType)); };
132
133 /**
134 * Initializes the ZRTP Id field
135 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500136 void setZrtpId() { zrtpHeader->zrtpId = htons(zrtpId); }
Alexandre Lision51140e12013-12-02 10:54:09 -0500137};
138
139/**
140 * @}
141 */
142#endif // ZRTPPACKETBASE