Packets/80221Bundle/MIHPacket: register MIH EtherType in registry
[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 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39     senf::PacketRegistry<senf::EtherTypes>::RegistrationProxy<senf::MIHPacket>
40         registerMIHPacket (0x86dd);
41
42 }
43
44 ///////////////////////////////////////////////////////////////////////////
45 // MIHFId_TLVParser
46
47 prefix_ void senf::MIHFId_TLVParser::setString(std::string const &id)
48 {
49     size_type str_size (id.size());
50     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
51     if (str_size > 253) 
52         throw std::length_error("maximum length of a MIHF_ID is 253 octets");
53     safe_data_iterator si = resizeValueField( str_size);   
54     std::copy( id.begin(), id.end(), si);
55 }
56
57 prefix_ void senf::MIHFId_TLVParser::setMACAddress(senf::MACAddress const &mac)
58 {
59     safe_data_iterator si = resizeValueField(12);
60     std::copy( mac.begin(), mac.end(), getNAIEncodedOutputIterator(si));
61 }
62
63 prefix_ void senf::MIHFId_TLVParser::setINet4Address(senf::INet4Address const &addr)
64 {
65     safe_data_iterator si = resizeValueField(8);
66     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
67 }
68
69 prefix_ void senf::MIHFId_TLVParser::setINet6Address(senf::INet6Address const &addr)
70 {
71     safe_data_iterator si = resizeValueField(32);
72     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
73 }
74
75 prefix_ void senf::MIHFId_TLVParser::setEUI64(senf::EUI64 const &addr)
76 {
77     safe_data_iterator si = resizeValueField(16);
78     std::copy( addr.begin(), addr.end(), getNAIEncodedOutputIterator(si));
79 }
80
81 prefix_ senf::MIHFId senf::MIHFId_TLVParser::valueAs(MIHFId::Type type)
82     const
83 {
84     if (length() == 0) return MIHFId();
85     switch (type) {
86     case MIHFId::Empty:
87         return MIHFId();
88     case MIHFId::MACAddress:
89         return MIHFId( asMACAddress());
90     case MIHFId::INet4Address:
91         return MIHFId( asINet4Address());
92     case MIHFId::INet6Address:
93         return MIHFId( asINet6Address());
94     case MIHFId::String:
95         return MIHFId( asINet6Address());
96     case MIHFId::EUI64:
97         return MIHFId( asINet6Address());
98     }
99     return MIHFId();
100 }
101
102
103 ///////////////////////////////////////////////////////////////////////////
104 // MIHPacketType
105
106 prefix_ void senf::MIHPacketType::dump(packet p, std::ostream &os)
107 {
108     boost::io::ios_all_saver ias(os);
109     os << "MIH Packet:\n"
110        <<     "  protocol header:\n"
111        <<     "    version               : " << unsigned( p->version()) << "\n"
112        <<     "    ack request           : " << p->ackRequest() << "\n"
113        <<     "    ack response          : " << p->ackResponse() << "\n"
114        <<     "    UIR                   : " << p->uir() << "\n"
115        <<     "    more fragments        : " << p->moreFragment() << "\n"
116        <<     "    fragment number       : " << p->fragmentNr() << "\n"
117        <<     "    message ID (MID)      : " << unsigned( p->messageId()) << "\n"
118        <<     "      sid                 : " << unsigned( p->sid()) << "\n"        
119        <<     "      opcode              : " << unsigned( p->opcode()) << "\n"
120        <<     "      aid                 : " << unsigned( p->aid()) << "\n"      
121        <<     "    transaction id        : " << unsigned( p->transactionId()) << "\n"
122        <<     "    payload length        : " << unsigned( p->payloadLength()) << "\n"
123        <<     "  source MIHF_Id TLV      :\n"
124        <<     "    type                  : " << unsigned (p->src_mihfId().type()) << "\n"
125        <<     "    length                : " << unsigned (p->src_mihfId().length()) << "\n"
126        <<     "    value                 :\n";
127     std::string src_mihfId (p->src_mihfId().asString());
128     hexdump(src_mihfId.begin(), src_mihfId.end(), os);
129     os <<     "  destination MIHF_Id TLV:\n"
130        <<     "    type                  : " << unsigned (p->dst_mihfId().type()) << "\n"
131        <<     "    length                : " << unsigned (p->dst_mihfId().length()) << "\n"
132        <<     "    value                 :\n";
133     std::string dst_mihfId (p->dst_mihfId().asString());
134     hexdump(dst_mihfId.begin(), dst_mihfId.end(), os);
135 }
136
137 prefix_ void senf::MIHPacketType::finalize(packet p)
138 {
139     p->src_mihfId().finalizeLength();
140     p->dst_mihfId().finalizeLength();
141     p->payloadLength_() << p.size() - 8;
142     p->messageId() << key(p.next(nothrow));
143 }
144
145 prefix_ senf::PacketInterpreterBase::factory_t senf::MIHPacketType::nextPacketType(packet p)
146 {
147     if (p.data().size() < initSize())
148         return no_factory();
149     PkReg_Entry const * e (PacketRegistry<MIHMessageRegistry>::lookup( p->messageId(), nothrow ));
150     return e ? e->factory() : MIHPayloadPacket::factory();
151 }
152
153 prefix_ void senf::MIHPayloadPacketType::dump(packet p, std::ostream &os)
154 {
155     boost::io::ios_all_saver ias(os);
156     os << "MIH Payload (service specific TLVs):\n"
157        << "  ToDo!\n";
158 }
159
160 ///////////////////////////////cc.e////////////////////////////////////////
161 #undef prefix_
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // comment-column: 40
172 // End: