Replace all relative includes with abolute ones
[senf.git] / senf / Packets / 80221Bundle / MIHPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief MIH protocol non-inline non-template implementation */
25
26 #include "MIHPacket.hh"
27 //#include "MIHPacket.ih"
28
29 // Custom includes
30 #include <senf/Packets/Packets.hh>
31 #include <senf/Utils/hexdump.hh>
32 #include <boost/io/ios_state.hpp>
33
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37
38
39 ///////////////////////////////////////////////////////////////////////////
40 // MIHFId_TLVParser
41
42 prefix_ void senf::MIHFId_TLVParser::setString(std::string const &id)
43 {
44     size_type str_size (id.size());
45     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
46     if (str_size > 253) 
47         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
48     safe_data_iterator si = resizeValueField( str_size);   
49     std::copy( id.begin(), id.end(), si);
50 }
51
52 prefix_ void senf::MIHFId_TLVParser::setMACAddress(senf::MACAddress const &mac)
53 {
54     safe_data_iterator si = resizeValueField(12);
55     std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
56 }
57
58 prefix_ void senf::MIHFId_TLVParser::setINet4Address(senf::INet4Address const &addr)
59 {
60     safe_data_iterator si = resizeValueField(8);
61     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
62 }
63
64 prefix_ void senf::MIHFId_TLVParser::setINet6Address(senf::INet6Address const &addr)
65 {
66     safe_data_iterator si = resizeValueField(32);
67     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
68 }
69
70 prefix_ void senf::MIHFId_TLVParser::setEUI64(senf::EUI64 const &addr)
71 {
72     safe_data_iterator si = resizeValueField(16);
73     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
74 }
75
76 prefix_ senf::MIHFId senf::MIHFId_TLVParser::valueAs(MIHFId::Type type)
77     const
78 {
79     if (length() == 0) return MIHFId();
80     switch (type) {
81     case MIHFId::Empty:
82         return MIHFId();
83     case MIHFId::MACAddress:
84         return MIHFId( asMACAddress());
85     case MIHFId::INet4Address:
86         return MIHFId( asINet4Address());
87     case MIHFId::INet6Address:
88         return MIHFId( asINet6Address());
89     case MIHFId::String:
90         return MIHFId( asINet6Address());
91     case MIHFId::EUI64:
92         return MIHFId( asINet6Address());
93     }
94     return MIHFId();
95 }
96
97
98 ///////////////////////////////////////////////////////////////////////////
99 // MIHPacketType
100
101 prefix_ void senf::MIHPacketType::dump(packet p, std::ostream &os)
102 {
103     boost::io::ios_all_saver ias(os);
104     os << "MIH Packet:\n"
105        <<     "  protocol header:\n"
106        <<     "    version               : " << unsigned( p->version()) << "\n"
107        <<     "    ack request           : " << p->ackRequest() << "\n"
108        <<     "    ack response          : " << p->ackResponse() << "\n"
109        <<     "    UIR                   : " << p->uir() << "\n"
110        <<     "    more fragments        : " << p->moreFragment() << "\n"
111        <<     "    fragment number       : " << p->fragmentNr() << "\n"
112        <<     "    message ID (MID)      : " << unsigned( p->messageId()) << "\n"
113        <<     "      sid                 : " << unsigned( p->sid()) << "\n"        
114        <<     "      opcode              : " << unsigned( p->opcode()) << "\n"
115        <<     "      aid                 : " << unsigned( p->aid()) << "\n"      
116        <<     "    transaction id        : " << unsigned( p->transactionId()) << "\n"
117        <<     "    payload length        : " << unsigned( p->payloadLength()) << "\n"
118        <<     "  source MIHF_Id TLV      :\n"
119        <<     "    type                  : " << unsigned (p->src_mihfId().type()) << "\n"
120        <<     "    length                : " << unsigned (p->src_mihfId().length()) << "\n"
121        <<     "    value                 :\n";
122     std::string src_mihfId (p->src_mihfId().asString());
123     hexdump(src_mihfId.begin(), src_mihfId.end(), os);
124     os <<     "  destination MIHF_Id TLV:\n"
125        <<     "    type                  : " << unsigned (p->dst_mihfId().type()) << "\n"
126        <<     "    length                : " << unsigned (p->dst_mihfId().length()) << "\n"
127        <<     "    value                 :\n";
128     std::string dst_mihfId (p->dst_mihfId().asString());
129     hexdump(dst_mihfId.begin(), dst_mihfId.end(), os);
130 }
131
132 prefix_ void senf::MIHPacketType::finalize(packet p)
133 {
134     p->src_mihfId().finalizeLength();
135     p->dst_mihfId().finalizeLength();
136     p->payloadLength_() << p.size() - 8;
137     p->messageId() << key(p.next(nothrow));
138 }
139
140 prefix_ senf::PacketInterpreterBase::factory_t senf::MIHPacketType::nextPacketType(packet p)
141 {
142     if (p.data().size() < initSize())
143         return no_factory();
144     PkReg_Entry const * e (PacketRegistry<MIHMessageRegistry>::lookup( p->messageId(), nothrow ));
145     return e ? e->factory() : MIHPayloadPacket::factory();
146 }
147
148 prefix_ void senf::MIHPayloadPacketType::dump(packet p, std::ostream &os)
149 {
150     boost::io::ios_all_saver ias(os);
151     os << "MIH Payload (service specific TLVs):\n"
152        << "  ToDo!\n";
153 }
154
155 ///////////////////////////////cc.e////////////////////////////////////////
156 #undef prefix_
157
158 \f
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // c-file-style: "senf"
163 // indent-tabs-mode: nil
164 // ispell-local-dictionary: "american"
165 // compile-command: "scons -u test"
166 // comment-column: 40
167 // End: