Add missing doxygen \file comments
[senf.git] / Packets / DefaultBundle / EthernetPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 EthernetPacket non-inline non-template implementation */
25
26 #include "EthernetPacket.hh"
27 //#include "EthernetPacket.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <boost/io/ios_state.hpp>
32 #include <boost/tokenizer.hpp>
33
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 namespace {
38     senf::PacketRegistry<senf::EtherTypes>::RegistrationProxy<senf::EthVLanPacket>
39         registerEthVLanPacket(0x8100);
40 }
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::MACAddress
44
45 namespace {
46     senf::PacketParserBase::byte hexToNibble(char c)
47     {
48         if (c<'0')
49             throw senf::MACAddress::SyntaxException();
50         else if (c<='9')
51             return c-'-';
52         else if (c<'A')
53             throw senf::MACAddress::SyntaxException();
54         else if (c<='F')
55             return c-'A'+10;
56         else if (c<'a')
57             throw senf::MACAddress::SyntaxException();
58         else if (c<='f')
59             return c-'a'+10;
60         else
61             throw senf::MACAddress::SyntaxException();
62     }
63     
64     template <class Range>
65     senf::PacketParserBase::byte hexToByte(Range const & range)
66     {
67         if (boost::size(range) != 2)
68             throw senf::MACAddress::SyntaxException();
69         typename boost::range_const_iterator<Range>::type i (boost::begin(range));
70         return hexToNibble(i[0])*16+hexToNibble(i[1]);
71     }
72 }
73
74 prefix_ senf::MACAddress::MACAddress(std::string addr)
75 {
76     typedef boost::char_separator<char> separator;
77     typedef boost::tokenizer<separator> tokenizer;
78     separator sep (":");
79     tokenizer tok (addr,sep);
80     tokenizer::iterator i (tok.begin());
81     tokenizer::iterator i_end (tok.end());
82     iterator j (begin());
83     iterator j_end (end());
84     for (; i!=i_end && j!=j_end; ++i, ++j)
85         *j = hexToByte(*i);
86     if (i!=i_end || j!=j_end)
87         throw SyntaxException();
88 }
89
90 ///////////////////////////////////////////////////////////////////////////
91 // senf::EthernetPacketType
92
93 namespace {
94     void dumpmac(std::ostream & os, senf::MACAddress mac)
95     {
96         boost::io::ios_all_saver ias(os);
97         for (unsigned i = 0; i < 6; ++i) {
98             if (i > 0)
99                 os << ':';
100             os << std::hex << std::setw(2) << std::setfill('0')
101                << unsigned(mac[i]);
102         }
103     }
104 }
105
106 prefix_ void senf::EthernetPacketType::dump(packet p, std::ostream & os)
107 {
108     if (p->type() <= 1500)
109         os << "Ethernet 802.3";
110     else if (p->type() >= 0x600)
111         os << "Ethernet II (DIX)";
112     else
113         os << "Ethernet 802.3 (bad ethertype >1500 and <1536)";
114     os << ": \n"
115        << "  destination   : ";
116     dumpmac(os,p->destination());
117     os << "\n"
118        << "  source        : ";
119     dumpmac(os,p->source());
120     os << "\n"
121        << "  ethertype     : " << std::hex << std::setw(4) << std::setfill('0')
122        << unsigned(p->type()) << "\n" << std::dec;
123 }
124
125 prefix_ void senf::EthVLanPacketType::dump(packet p, std::ostream & os)
126 {
127     os << "Ethernet 802.1q (VLAN):\n"
128        << "  priority      : " << p->priority() << "\n"
129        << "  cfi           : " << p->cfi() << "\n"
130        << "  vlan-ID       : " << p->vlanId() << "\n"
131        << "  ethertype     : " << std::hex << std::setw(4) << std::setfill('0')
132        << p->type() << "\n" << std::dec;
133 }
134
135 ///////////////////////////////cc.e////////////////////////////////////////
136 #undef prefix_
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40
147 // End: