removed some useless spaces; not very important, I know :)
[senf.git] / Packets / MPEGDVBBundle / DTCPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     David Wagner <dw6@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
24 #ifndef DTCPPACKET_HH_
25 #define DTCPPACKET_HH_
26
27 #include "../../Packets/Packets.hh"
28 #include "../../Packets/DefaultBundle/IPv4Packet.hh"
29 #include "../../Packets/DefaultBundle/IPv6Packet.hh"
30
31 #define DTCP_V4_MCADDRESS "224.0.0.36"
32 #define DTCP_V6_MCADDRESS "FF02:0:0:0:0:0:1:4"
33 #define DTCP_UDP_PORT 652
34
35 namespace senf {
36     
37     //first we have to define some helpers
38     struct DTCPIPv4AddressListParser : public PacketParserBase {
39 #       include SENF_PARSER()        
40         SENF_PARSER_PRIVATE_FIELD ( num_of_fbips, UInt8Parser );
41         SENF_PARSER_PRIVATE_FIELD ( reserved ,    UInt8Parser );   //must be zero 
42         SENF_PARSER_VEC_N         ( fbiplist,     num_of_fbips, INet4AddressParser );
43
44         SENF_PARSER_FINALIZE(DTCPIPv4AddressListParser);
45     };
46         
47     struct DTCPIPv6AddressListParser : public PacketParserBase {
48 #       include SENF_PARSER()        
49         SENF_PARSER_PRIVATE_FIELD ( num_of_fbips, UInt8Parser );
50         SENF_PARSER_PRIVATE_FIELD ( reserved,     UInt8Parser );   //must be zero 
51         SENF_PARSER_VEC_N         ( fbiplist,     num_of_fbips, INet6AddressParser );
52
53         SENF_PARSER_FINALIZE(DTCPIPv6AddressListParser);
54     };
55
56     /** \brief Parse a DTCP packet
57
58         Parser implementing the DTCP packet according to RFC 3077
59         
60         \see DTCPPacketType
61      */
62     struct DTCPPacketParser : public PacketParserBase
63     {
64 #       include SENF_PARSER()
65
66         SENF_PARSER_BITFIELD         ( version_number,       4, unsigned );  // =1 according to rfc3077
67         SENF_PARSER_BITFIELD         ( command,              4, unsigned );  // 1=JOIN 2=LEAVE
68         SENF_PARSER_FIELD            ( interval,             UInt8Parser );  // 5 according to rfc3077
69         SENF_PARSER_FIELD            ( sequence_number,      UInt16Parser );
70         SENF_PARSER_PRIVATE_BITFIELD ( reserved,             3, unsigned );
71         SENF_PARSER_BITFIELD         ( receive_capable_feed, 1, bool );      // 0=send only, 1=receive_capable_feed
72         SENF_PARSER_BITFIELD_RO      ( ip_version,           4, unsigned );  // 4=IPv4, 6=IPv6
73         SENF_PARSER_FIELD    ( tunnel_protocol,      UInt8Parser ); 
74         /* Please consider the following comments on the implementation given in this class: 
75          * 1. you could think of simply using SENF_PARSER_PRIVATE_VARIANT and List / Vectorparser like this:
76          * SENF_PARSER_PRIVATE_VARIANT  ( fbiplist,             ip_version,
77          *                                                       (senf::VoidPacketParser) //ip_version=0
78          *                                                       (senf::VoidPacketParser) //1
79          *                                                       (senf::VoidPacketParser) //2
80          *                                                       (senf::VoidPacketParser) //3
81          *                                                       (senf::ListBParser< IPv4Packet, num_of_fbips>) //4 
82          *                                                       (senf::VoidPacketParser) //5
83          *                                                       (senf::ListBParser< IPv6Packet, num_of_fbips>) ); //6
84          * This can't work for two reasons: 
85          *      -SENF_PARSER_PRIVATE_VARIANT only accepts 6 templates in types but you have to start from 0.
86          *      -you NEVER can use templated Parsers in these macros since the macro-preprocessor won't recognize the <> brackets and will
87          *      interpret the ","
88          * 
89          * The first problem is solved by using (actually inventing)  SENF_PARSER_VARIANT_TRANS which has the same limitations 
90          *      concerning the number of types but isn't limited to the values used. This is achieved by a translating function 
91          *      as you can see. 
92          * The second problem is solved by introducing Helper-Parser which cover both the list and the number field. By that no 
93          *      templates have to be used. 
94          */
95
96         struct ip_version_translator {
97             static unsigned fromChooser(ip_version_t::value_type in) {
98                 switch (in) { 
99                 case 4: return 0;
100                 case 6: return 1;
101                 }
102                 return 1; //default. should rather throw an exception 
103             }
104             static ip_version_t::value_type toChooser(unsigned in) {
105                 switch (in) {
106                 case 0: return 4;
107                 case 1: return 6; 
108                 }
109                 return 6; //default. should rather throw an exception 
110             }
111         };
112     
113         SENF_PARSER_VARIANT_TRANS    ( fbiplist,             ip_version, ip_version_translator,
114                                                                  (senf::DTCPIPv4AddressListParser)        //IPv4 
115                                                                  (senf::DTCPIPv6AddressListParser) );     //IPv6
116                                                                  
117         DTCPIPv4AddressListParser getIpv4AddressList () const { return fbiplist().get<0>(); }  // this is the absolute index 
118         DTCPIPv6AddressListParser getIpv6AddressList () const { return fbiplist().get<1>(); }
119         void setIpVersion4() const { fbiplist().init<0>(); }
120         void setIpVersion6() const { fbiplist().init<1>(); }
121
122         SENF_PARSER_FINALIZE(DTCPPacketParser);
123     };
124     
125     /** \brief DTCP packet
126         
127         \par Packet type (typedef):
128             \ref DTCPPacket
129
130         \par Fields:
131             \ref DTCPPacketParser
132
133         \ingroup protocolbundle_mpegdvb
134      */
135     struct DTCPPacketType
136         : public PacketTypeBase,
137           public PacketTypeMixin<DTCPPacketType>
138     {
139         typedef PacketTypeMixin<DTCPPacketType> mixin;
140         typedef ConcretePacket<DTCPPacketType> packet;
141         typedef DTCPPacketParser parser;
142     
143         using mixin::nextPacketRange;
144         using mixin::init;
145         using mixin::initSize;
146         
147         static void dump(packet p, std::ostream & os);
148     };
149     
150     /** \brief DTCP packet typedef */
151     typedef DTCPPacketType::packet DTCPPacket;
152 }
153
154 #endif /*DTCPPACKET_HH_*/