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