summaryrefslogtreecommitdiff
path: root/src/libzrtpcpp/ZrtpPacketBase.h
blob: f0d294416bc00fd4a83a83ce8a1df7751d0e5ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
  Copyright (C) 2006-2010 Werner Dittmann

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
 */

#ifndef _ZRTPPACKETBASE_H_
#define _ZRTPPACKETBASE_H_

/**
 * @file ZrtpPacketBase.h
 * @brief The ZRTP message header class
 *
 * This class defines the ZRTP message header and provides access and
 * check methods.
 *
 * @ingroup GNU_ZRTP
 * @{
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

#include <libzrtpcpp/zrtpPacket.h>
#include <libzrtpcpp/ZrtpTextData.h>
#include <libzrtpcpp/ZrtpConfigure.h>
#include <libzrtpcpp/ZrtpCrc32.h>

// #define DEBUGOUT(deb)   deb
#define DEBUGOUT(deb)

/*
 * This is the unique ZRTP ID in network order (PZ)
 */
const uint16_t zrtpId = 0x505a;

/**
 * This is the base class for all ZRTP packets
 *
 * All other ZRTP packet classes inherit from this class. It does not have
 * an implementation of its own.
 *
 * The standard constructors of the subclasses usually initialize the @c allocate
 * field with their fixed data array which is large enough to hold all message
 * data. If an implementation needs to change this to use dynamic memory
 * allocation only that line in the subclasses must be changed and the destructors
 * should take care of memory management.
 *
 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
 */

class __EXPORT ZrtpPacketBase {

  private:

  protected:
      void* allocated;                  ///< Pointer to ZRTP message data
      zrtpPacketHeader_t* zrtpHeader;   ///< points to the fixed ZRTP header structure

  public:
    /**
     * Destructor is empty
     */
    virtual ~ZrtpPacketBase() {};

    /**
     * Get pointer to ZRTP header
     *
     * @return
     *     Pointer to ZRTP header structure.
     */
    const uint8_t* getHeaderBase() { return (const uint8_t*)zrtpHeader; };

    /**
     * Check is this is a ZRTP message
     *
     * @return
     *     @c true if check was ok
     */
    bool isZrtpPacket()            { return (ntohs(zrtpHeader->zrtpId) == zrtpId); };

    /**
     * Get the length in words of the ZRTP message
     *
     * @return
     *     The length in words
     */
    uint16_t getLength()           { return ntohs(zrtpHeader->length); };

    /**
     * Return pointer to fixed length message type ASCII data
     *
     * @return
     *     Pointer to ASCII character array
     */
    uint8_t* getMessageType()      { return zrtpHeader->messageType; };

    /**
     * Set the lenght field in the ZRTP header
     *
     * @param len
     *     The length of the ZRTP message in words, host order
     */
    void setLength(uint16_t len)  { zrtpHeader->length = htons(len); };

    /**
     * Copy the message type ASCII data to ZRTP message type field
     *
     * @param msg
     *     Pointer to message type ASCII character array
     */
    void setMessageType(uint8_t *msg)
        { memcpy(zrtpHeader->messageType, msg, sizeof(zrtpHeader->messageType)); };

    /**
     * Initializes the ZRTP Id field
     */
    void setZrtpId()              { zrtpHeader->zrtpId = htons(zrtpId); }
};

/**
 * @}
 */
#endif // ZRTPPACKETBASE