added anyway as eps - vector graphics
[anytun.git] / plainPacket.cpp
blobc1fd99ae0d0549f36bacdf479950d1b7a8556f6b
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 <net/ethernet.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 *payload_type_ = PAYLOAD_TYPE_T_HTON(payload_type);
62 u_int32_t PlainPacket::getPayloadLength() const
64 if(!payload_)
65 return 0;
67 return (length_ > sizeof(payload_type_t)) ? (length_ - sizeof(payload_type_t)) : 0;
70 void PlainPacket::setPayloadLength(u_int32_t payload_length)
72 Buffer::setLength(payload_length + sizeof(payload_type_t));
73 // depending on allow_realloc buf_ may point to another address
74 // therefore in this case reinit() gets called by Buffer::setLength()
77 void PlainPacket::reinit()
79 payload_type_ = reinterpret_cast<payload_type_t*>(buf_);
80 payload_ = buf_ + sizeof(payload_type_t);
82 if(length_ <= (sizeof(payload_type_t)))
83 payload_ = NULL;
85 if(length_ < (sizeof(payload_type_t))) {
86 payload_type_ = NULL;
87 throw std::runtime_error("packet can't be initialized, buffer is too small");
92 u_int8_t* PlainPacket::getPayload()
94 return payload_;
97 NetworkAddress PlainPacket::getSrcAddr() const
99 if(!payload_type_ || !payload_)
100 return NetworkAddress();
102 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
104 if(type == PAYLOAD_TYPE_TAP) // Ehternet
106 // TODO
107 return NetworkAddress();
109 else if(type == PAYLOAD_TYPE_TUN) // IPv4
111 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
112 return NetworkAddress();
113 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
114 return NetworkAddress(hdr->ip_src);
116 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
118 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
119 return NetworkAddress();
120 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
121 return NetworkAddress(hdr->ip6_src);
123 return NetworkAddress();
126 NetworkAddress PlainPacket::getDstAddr() const
128 if(!payload_type_ || !payload_)
129 return NetworkAddress();
131 payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
133 if(type == PAYLOAD_TYPE_TAP) // Ehternet
135 // TODO
136 return NetworkAddress();
138 else if(type == PAYLOAD_TYPE_TUN) // IPv4
140 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
141 return NetworkAddress();
142 struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
143 return NetworkAddress(hdr->ip_dst);
145 else if(type == PAYLOAD_TYPE_TUN6) // IPv6
147 if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
148 return NetworkAddress();
149 struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
150 return NetworkAddress(hdr->ip6_dst);
152 return NetworkAddress();