svn cleanup
[anytun.git] / plainPacket.cpp
blobc4717c361916ab41da396d6339c92f3cb3991131
1 /*
2 * anytun
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
31 #include <stdexcept>
32 #include <iostream>
33 #include <arpa/inet.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/ip.h>
36 #include <netinet/ip6.h>
38 #include "datatypes.h"
39 #include "plainPacket.h"
41 PlainPacket::PlainPacket(u_int32_t payload_length, bool allow_realloc) : Buffer(payload_length + sizeof(payload_type_t), allow_realloc)
43 payload_type_ = reinterpret_cast<payload_type_t*>(buf_);
44 payload_ = buf_ + sizeof(payload_type_t);
45 *payload_type_ = 0;
48 payload_type_t PlainPacket::getPayloadType() const
50 if(payload_type_)
51 return PAYLOAD_TYPE_T_NTOH(*payload_type_);
53 return 0;
56 void PlainPacket::setPayloadType(payload_type_t payload_type)
58 if(!payload_type_)
59 return;
61 if(payload_type == PAYLOAD_TYPE_TUN) {
62 if(!payload_) {
63 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN);
64 return;
67 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
68 if(hdr->ip_v == 4)
69 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN4);
70 else if(hdr->ip_v == 6)
71 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN6);
73 else
74 *payload_type_ = PAYLOAD_TYPE_T_HTON(payload_type);
77 u_int32_t PlainPacket::getPayloadLength() const
79 if(!payload_)
80 return 0;
82 return (length_ > sizeof(payload_type_t)) ? (length_ - sizeof(payload_type_t)) : 0;
85 void PlainPacket::setPayloadLength(u_int32_t payload_length)
87 Buffer::setLength(payload_length + sizeof(payload_type_t));
88 // depending on allow_realloc buf_ may point to another address
89 // therefore in this case reinit() gets called by Buffer::setLength()
92 void PlainPacket::reinit()
94 payload_type_ = reinterpret_cast<payload_type_t*>(buf_);
95 payload_ = buf_ + sizeof(payload_type_t);
97 if(length_ <= (sizeof(payload_type_t)))
98 payload_ = NULL;
100 if(length_ < (sizeof(payload_type_t))) {
101 payload_type_ = NULL;
102 throw std::runtime_error("packet can't be initialized, buffer is too small");
107 u_int8_t* PlainPacket::getPayload()
109 return payload_;
112 NetworkAddress PlainPacket::getSrcAddr() const
114 if(!payload_type_ || !payload_)
115 return NetworkAddress();
117 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
119 if(type == PAYLOAD_TYPE_TAP) // Ehternet
121 // TODO
122 return NetworkAddress();
124 else if(type == PAYLOAD_TYPE_TUN4) // IPv4
126 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
127 return NetworkAddress();
128 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
129 return NetworkAddress(hdr->ip_src);
131 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
133 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
134 return NetworkAddress();
135 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
136 return NetworkAddress(hdr->ip6_src);
138 return NetworkAddress();
141 NetworkAddress PlainPacket::getDstAddr() const
143 if(!payload_type_ || !payload_)
144 return NetworkAddress();
146 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
148 if(type == PAYLOAD_TYPE_TAP) // Ehternet
150 // TODO
151 return NetworkAddress();
153 else if(type == PAYLOAD_TYPE_TUN4) // IPv4
155 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
156 return NetworkAddress();
157 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
158 return NetworkAddress(hdr->ip_dst);
160 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
162 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
163 return NetworkAddress();
164 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
165 return NetworkAddress(hdr->ip6_dst);
167 return NetworkAddress();