Imported Upstream version 0.3.2
[anytun.git] / src / encryptedPacket.h
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methodes used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #ifndef ANYTUN_encryptedPacket_h_INCLUDED
34 #define ANYTUN_encryptedPacket_h_INCLUDED
35
36 #include "datatypes.h"
37 #include "buffer.h"
38
39 class Cipher;
40 class EncryptedPacket : public Buffer
41 {
42 public:
43
44   /**
45    * Packet constructor
46    * @param the length of the payload 
47    * @param allow reallocation of buffer
48    */
49   EncryptedPacket(u_int32_t payload_length, u_int32_t auth_tag_length, bool allow_realloc = false);
50
51   /**
52    * Packet destructor
53    */
54   ~EncryptedPacket() {};
55
56   /**
57    * Get the length of the header
58    * @return the length of the header
59    */
60   static u_int32_t getHeaderLength();
61
62   /**
63    * Get the sequence number
64    * @return seqence number
65    */
66   seq_nr_t getSeqNr() const;
67
68   /**
69    * Set the seqence number
70    * @param seq_nr sequence number
71    */
72   void setSeqNr(seq_nr_t seq_nr);
73
74   /**
75    * Get the sender id
76    * @return sender id
77    */
78   sender_id_t getSenderId() const;
79
80   /**
81    * Set the sender id
82    * @param sender_id sender id
83    */
84   void setSenderId(sender_id_t sender_id);
85
86   /**
87    * Get the mulitplex id
88    * @return multiplex id
89    */
90   mux_t getMux() const;
91
92   /**
93    * Set the multiplex id
94    * @param mux multiplex id
95    */
96   void setMux(mux_t mux);
97
98   /**
99    * Set the header of a packet
100    * @param seq_nr sequence number
101    * @param sender_id sender id
102    * @param mux multiplex id
103    */
104   void setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux);
105
106   /**
107    * Get the length of the payload
108    * @return the length of the payload
109    */
110   u_int32_t getPayloadLength() const;
111
112   /**
113    * Set the length of the payload
114    * @param length length of the payload
115    */
116   void setPayloadLength(u_int32_t payload_length);
117
118   /**
119    * Get the the payload
120    * @return the Pointer to the payload
121    */
122   u_int8_t* getPayload();
123
124
125   u_int8_t* getAuthenticatedPortion();
126   u_int32_t getAuthenticatedPortionLength();
127
128   void withAuthTag(bool b);
129   void addAuthTag();
130   void removeAuthTag();
131   u_int8_t* getAuthTag();
132   u_int32_t getAuthTagLength();
133                       
134 private:
135   EncryptedPacket();
136   EncryptedPacket(const EncryptedPacket &src);
137
138   void reinit();
139
140 #ifdef _MSC_VER
141   #pragma pack(push, 1)
142 #endif  
143   struct ATTR_PACKED HeaderStruct
144   {
145     seq_nr_t seq_nr;
146     sender_id_t sender_id;
147     mux_t mux;
148   };
149 #ifdef _MSC_VER
150   #pragma pack(pop)
151 #endif
152
153   struct HeaderStruct* header_;
154         u_int8_t * payload_;
155   u_int8_t * auth_tag_;
156   u_int32_t  auth_tag_length_;
157 };
158
159 #endif