switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / 80221Bundle / MIHPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief MIH protocol non-inline non-template implementation */
30
31 #include "MIHPacket.hh"
32 //#include "MIHPacket.ih"
33
34 // Custom includes
35 #include <boost/io/ios_state.hpp>
36 #include <senf/Utils/String.hh>
37 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
38
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 SENF_PACKET_REGISTRY_REGISTER( senf::EtherTypes, senf::MIHPacketType::etherType, senf::MIHPacket);
43
44
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 // MIHPacketType
47
48 prefix_ void senf::MIHPacketType::dump(packet p, std::ostream & os)
49 {
50     boost::io::ios_all_saver ias(os);
51     os << "MIH Packet:\n"
52        << "  protocol header:\n"
53        << senf::fieldName("  version")           << unsigned( p->version()) << "\n"
54        << senf::fieldName("  ack request")       << p->ackRequest() << "\n"
55        << senf::fieldName("  ack response")      << p->ackResponse() << "\n"
56        << senf::fieldName("  UIR")               << p->uir() << "\n"
57        << senf::fieldName("  more fragments")    << p->moreFragment() << "\n"
58        << senf::fieldName("  fragment number")   << p->fragmentNr() << "\n"
59        << senf::fieldName("  message ID (MID)")  << unsigned( p->messageId()) << "\n"
60        << senf::fieldName("    sid")             << unsigned( p->sid()) << "\n"
61        << senf::fieldName("    opcode")          << unsigned( p->opcode()) << "\n"
62        << senf::fieldName("    aid")             << unsigned( p->aid()) << "\n"
63        << senf::fieldName("  transaction id")    << unsigned( p->transactionId()) << "\n"
64        << senf::fieldName("  payload length")    << unsigned( p->payloadLength()) << "\n";
65     p->src_mihfId().dump( os);
66     p->dst_mihfId().dump( os);
67 }
68
69 prefix_ void senf::MIHPacketType::finalize(packet p)
70 {
71     p->src_mihfId().finalize();
72     p->dst_mihfId().finalize();
73     p->payloadLength_() << p.size() - 8;
74     p->messageId() << key(p.next(nothrow));
75 }
76
77 prefix_ senf::PacketInterpreterBase::factory_t senf::MIHPacketType::nextPacketType(packet p)
78 {
79     if (p.data().size() < initSize())
80         return no_factory();
81     PacketRegistry<MIHMessageRegistry>::Entry const * e (
82         PacketRegistry<MIHMessageRegistry>::lookup( p->messageId(), nothrow ));
83     return e ? e->factory() : MIHGenericPayloadPacket::factory();
84 }
85
86 prefix_ void senf::MIHPacketType::validate(packet p)
87 {
88     try {
89         if (p.data().size() < initSize())
90             throw InvalidMIHPacketException("truncated MIH message");
91         if (p->version() != 1)
92             throw InvalidMIHPacketException("invalid MIH version: ") << senf::str(p->version());
93         if (p->payloadLength() != p.size()-8)
94             throw InvalidMIHPacketException("wrong MIH length: ") << senf::str(p->payloadLength());
95         if (p.next(senf::nothrow))
96             MIHMessageRegistry::instance().validate( p->messageId(), p.next());
97     } catch (TruncatedPacketException & e) {
98         throw InvalidMIHPacketException("truncated MIH message");
99     }
100 }
101
102 //-/////////////////////////////////////////////////////////////////////////////////////////////////
103 // MIHGenericPayloadPacketType
104
105 prefix_ void senf::MIHGenericPayloadPacketType::dump(packet p, std::ostream & os)
106 {
107     boost::io::ios_all_saver ias(os);
108     os << "MIH Payload (service specific TLVs):\n";
109     typedef parser::tlvList_t::container tlvListContainer_t;
110     tlvListContainer_t tlvListContainer (p->tlvList());
111     for (tlvListContainer_t::const_iterator i = tlvListContainer.begin(); i != tlvListContainer.end(); ++i)
112         i->dump( os);
113 }
114
115 prefix_ void senf::MIHGenericPayloadPacketType::finalize(packet p)
116 {
117     typedef parser::tlvList_t::container tlvContainer_t;
118     tlvContainer_t tlvs (p->tlvList() );
119     for (tlvContainer_t::iterator i (tlvs.begin()); i != tlvs.end(); ++i) {
120         MIHGenericTLVParser p (*i);
121         p.finalize();
122     }
123 }
124
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 #undef prefix_
128
129 \f
130 // Local Variables:
131 // mode: c++
132 // fill-column: 100
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"
137 // comment-column: 40
138 // End: