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