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