Packets: extended description of bad_cast exception in Packet.as()
[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 <boost/io/ios_state.hpp>
31 #include <senf/Utils/String.hh>
32 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
33
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, 0x8917, senf::MIHPacket);
38
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // MIHPacketType
42
43 prefix_ void senf::MIHPacketType::dump(packet p, std::ostream & os)
44 {
45     boost::io::ios_all_saver ias(os);
46     os << "MIH Packet:\n"
47        << "  protocol header:\n"
48        << senf::fieldName("  version")           << unsigned( p->version()) << "\n"
49        << senf::fieldName("  ack request")       << p->ackRequest() << "\n"
50        << senf::fieldName("  ack response")      << p->ackResponse() << "\n"
51        << senf::fieldName("  UIR")               << p->uir() << "\n"
52        << senf::fieldName("  more fragments")    << p->moreFragment() << "\n"
53        << senf::fieldName("  fragment number")   << p->fragmentNr() << "\n"
54        << senf::fieldName("  message ID (MID)")  << unsigned( p->messageId()) << "\n"
55        << senf::fieldName("    sid")             << unsigned( p->sid()) << "\n"
56        << senf::fieldName("    opcode")          << unsigned( p->opcode()) << "\n"
57        << senf::fieldName("    aid")             << unsigned( p->aid()) << "\n"
58        << senf::fieldName("  transaction id")    << unsigned( p->transactionId()) << "\n"
59        << senf::fieldName("  payload length")    << unsigned( p->payloadLength()) << "\n";
60     p->src_mihfId().dump( os);
61     p->dst_mihfId().dump( os);
62 }
63
64 prefix_ void senf::MIHPacketType::finalize(packet p)
65 {
66     p->src_mihfId().finalize();
67     p->dst_mihfId().finalize();
68     p->payloadLength_() << p.size() - 8;
69     p->messageId() << key(p.next(nothrow));
70 }
71
72 prefix_ senf::PacketInterpreterBase::factory_t senf::MIHPacketType::nextPacketType(packet p)
73 {
74     if (p.data().size() < initSize())
75         return no_factory();
76     PacketRegistry<MIHMessageRegistry>::Entry const * e (
77         PacketRegistry<MIHMessageRegistry>::lookup( p->messageId(), nothrow ));
78     return e ? e->factory() : MIHGenericPayloadPacket::factory();
79 }
80
81 prefix_ void senf::MIHPacketType::validate(packet p)
82 {
83     try {
84         if (p.data().size() < initSize())
85             throw InvalidMIHPacketException("truncated MIH message");
86         if (p->version() != 1)
87             throw InvalidMIHPacketException("invalid MIH version: ") << senf::str(p->version());
88         if (p->payloadLength() != p.size()-8)
89             throw InvalidMIHPacketException("wrong MIH length: ") << senf::str(p->payloadLength());
90         if (p.next(senf::nothrow))
91             MIHMessageRegistry::instance().validate( p->messageId(), p.next());
92     } catch (TruncatedPacketException & e) {
93         throw InvalidMIHPacketException("truncated MIH message");
94     }
95 }
96
97 //-/////////////////////////////////////////////////////////////////////////////////////////////////
98 // MIHGenericPayloadPacketType
99
100 prefix_ void senf::MIHGenericPayloadPacketType::dump(packet p, std::ostream & os)
101 {
102     boost::io::ios_all_saver ias(os);
103     os << "MIH Payload (service specific TLVs):\n";
104     typedef parser::tlvList_t::container tlvListContainer_t;
105     tlvListContainer_t tlvListContainer (p->tlvList());
106     for (tlvListContainer_t::const_iterator i = tlvListContainer.begin(); i != tlvListContainer.end(); ++i)
107         i->dump( os);
108 }
109
110 prefix_ void senf::MIHGenericPayloadPacketType::finalize(packet p)
111 {
112     typedef parser::tlvList_t::container tlvContainer_t;
113     tlvContainer_t tlvs (p->tlvList() );
114     for (tlvContainer_t::iterator i (tlvs.begin()); i != tlvs.end(); ++i) {
115         MIHGenericTLVParser p (*i);
116         p.finalize();
117     }
118 }
119
120
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 #undef prefix_
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // comment-column: 40
133 // End: