switched to endpoint @ anytun-config as well
[anytun.git] / src / plainPacket.cpp
blobb3441e0b5c11767362fd4a808b5f1da19586211c
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-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <stdexcept>
33 #include <iostream>
34 #include <arpa/inet.h>
35 #include <sys/socket.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip6.h>
40 #include <net/if.h>
41 #include <net/if_arp.h>
42 #include <netinet/if_ether.h>
44 #include "datatypes.h"
45 #include "plainPacket.h"
47 PlainPacket::PlainPacket(u_int32_t payload_length, bool allow_realloc) : Buffer(payload_length + sizeof(payload_type_t), allow_realloc)
49 payload_type_ = reinterpret_cast<payload_type_t*>(buf_);
50 payload_ = buf_ + sizeof(payload_type_t);
51 *payload_type_ = 0;
54 payload_type_t PlainPacket::getPayloadType() const
56 if(payload_type_)
57 return PAYLOAD_TYPE_T_NTOH(*payload_type_);
59 return 0;
62 void PlainPacket::setPayloadType(payload_type_t payload_type)
64 if(!payload_type_)
65 return;
67 if(payload_type == PAYLOAD_TYPE_TUN) {
68 if(!payload_) {
69 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN);
70 return;
73 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
74 if(hdr->ip_v == 4)
75 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN4);
76 else if(hdr->ip_v == 6)
77 *payload_type_ = PAYLOAD_TYPE_T_HTON(PAYLOAD_TYPE_TUN6);
79 else
80 *payload_type_ = PAYLOAD_TYPE_T_HTON(payload_type);
83 u_int32_t PlainPacket::getPayloadLength() const
85 if(!payload_)
86 return 0;
88 return (length_ > sizeof(payload_type_t)) ? (length_ - sizeof(payload_type_t)) : 0;
91 void PlainPacket::setPayloadLength(u_int32_t payload_length)
93 Buffer::setLength(payload_length + sizeof(payload_type_t));
94 // depending on allow_realloc buf_ may point to another address
95 // therefore in this case reinit() gets called by Buffer::setLength()
98 void PlainPacket::reinit()
100 payload_type_ = reinterpret_cast<payload_type_t*>(buf_);
101 payload_ = buf_ + sizeof(payload_type_t);
103 if(length_ <= (sizeof(payload_type_t)))
104 payload_ = NULL;
106 if(length_ < (sizeof(payload_type_t))) {
107 payload_type_ = NULL;
108 throw std::runtime_error("packet can't be initialized, buffer is too small");
113 u_int8_t* PlainPacket::getPayload()
115 return payload_;
118 NetworkAddress PlainPacket::getSrcAddr() const
120 if(!payload_type_ || !payload_)
121 return NetworkAddress();
123 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
125 if(type == PAYLOAD_TYPE_TAP) // Ehternet
127 // TODO
128 return NetworkAddress();
130 else if(type == PAYLOAD_TYPE_TUN4) // IPv4
132 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
133 return NetworkAddress();
134 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
135 return NetworkAddress(hdr->ip_src);
137 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
139 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
140 return NetworkAddress();
141 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
142 return NetworkAddress(hdr->ip6_src);
144 return NetworkAddress();
147 NetworkAddress PlainPacket::getDstAddr() const
149 if(!payload_type_ || !payload_)
150 return NetworkAddress();
152 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
154 if(type == PAYLOAD_TYPE_TAP) // Ehternet
156 // TODO
157 return NetworkAddress();
159 else if(type == PAYLOAD_TYPE_TUN4) // IPv4
161 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
162 return NetworkAddress();
163 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
164 return NetworkAddress(hdr->ip_dst);
166 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
168 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
169 return NetworkAddress();
170 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
171 return NetworkAddress(hdr->ip6_dst);
173 return NetworkAddress();