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.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <arpa/inet.h>
34 #include <cstdio> // for std::memcpy
36 #include "encryptedPacket.h"
37 #include "datatypes.h"
41 // TODO: fix auth_tag stuff
42 EncryptedPacket::EncryptedPacket(u_int32_t payload_length
, bool allow_realloc
)
43 : Buffer(payload_length
+ sizeof(struct HeaderStruct
), allow_realloc
)
45 header_
= reinterpret_cast<struct HeaderStruct
*>(buf_
);
46 payload_
= buf_
+ sizeof(struct HeaderStruct
); // TODO: fix auth_tag stuff
47 auth_tag_
= NULL
; // TODO: fix auth_tag stuff
51 header_
->sender_id
= 0;
56 seq_nr_t
EncryptedPacket::getSeqNr() const
59 return SEQ_NR_T_NTOH(header_
->seq_nr
);
64 sender_id_t
EncryptedPacket::getSenderId() const
67 return SENDER_ID_T_NTOH(header_
->sender_id
);
72 mux_t
EncryptedPacket::getMux() const
75 return MUX_T_NTOH(header_
->mux
);
80 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr
)
83 header_
->seq_nr
= SEQ_NR_T_HTON(seq_nr
);
86 void EncryptedPacket::setSenderId(sender_id_t sender_id
)
89 header_
->sender_id
= SENDER_ID_T_HTON(sender_id
);
92 void EncryptedPacket::setMux(mux_t mux
)
95 header_
->mux
= MUX_T_HTON(mux
);
98 void EncryptedPacket::setHeader(seq_nr_t seq_nr
, sender_id_t sender_id
, mux_t mux
)
103 header_
->seq_nr
= SEQ_NR_T_HTON(seq_nr
);
104 header_
->sender_id
= SENDER_ID_T_HTON(sender_id
);
105 header_
->mux
= MUX_T_HTON(mux
);
108 u_int32_t
EncryptedPacket::getPayloadLength() const
110 return (length_
> sizeof(struct HeaderStruct
)) ? (length_
- sizeof(struct HeaderStruct
)) : 0; // TODO: fix auth_tag stuff
113 void EncryptedPacket::setPayloadLength(u_int32_t payload_length
)
115 Buffer::setLength(payload_length
+ sizeof(struct HeaderStruct
));
116 // depending on allow_realloc buf_ may point to another address
117 // therefore in this case reinit() gets called by Buffer::setLength()
120 void EncryptedPacket::reinit()
123 header_
= reinterpret_cast<struct HeaderStruct
*>(buf_
);
124 payload_
= buf_
+ sizeof(struct HeaderStruct
); // TODO: fix auth_tag stuff
125 auth_tag_
= NULL
; // TODO: fix auth_tag stuff
128 u_int8_t
* EncryptedPacket::getPayload()
138 // TODO: fix auth_tag stuff
140 bool EncryptedPacket::hasAuthTag() const
142 // if( auth_tag_ == NULL )
147 void EncryptedPacket::withAuthTag(bool b
)
149 // if( b && (auth_tag_ != NULL) )
150 // throw std::runtime_error("packet already has auth tag function enabled");
151 // //TODO: return instead?
152 // if( ! b && (auth_tag_ == NULL) )
153 // throw std::runtime_error("packet already has auth tag function disabled");
154 // //TODO: return instead?
157 // auth_tag_ = reinterpret_cast<AuthTag*>( buf_ + sizeof(struct HeaderStruct) );
158 // payload_ = payload_ + AUTHTAG_SIZE;
159 // length_ -= AUTHTAG_SIZE;
160 // max_length_ -= AUTHTAG_SIZE;
162 // payload_ = reinterpret_cast<u_int8_t*>( auth_tag_ );
163 // length_ += AUTHTAG_SIZE;
164 // max_length_ += AUTHTAG_SIZE;
169 void EncryptedPacket::setAuthTag(AuthTag
& tag
)
171 // if( auth_tag_ == NULL )
172 // throw std::runtime_error("auth tag not enabled");
174 // if( tag == AuthTag(0) )
177 // if( tag.getLength() != AUTHTAG_SIZE )
178 // throw std::length_error("authtag length mismatch with AUTHTAG_SIZE");
180 // std::memcpy( auth_tag_, tag.getBuf(), AUTHTAG_SIZE );
183 AuthTag
EncryptedPacket::getAuthTag() const
185 // if( auth_tag_ == NULL )
186 // throw std::runtime_error("auth tag not enabled");
188 AuthTag
at(AUTHTAG_SIZE
);
189 // std::memcpy(at, auth_tag_, AUTHTAG_SIZE );