Packets: Adjust SENF_PARSER_VARIANT implementation for better public/private support
[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_VECTOR        ( 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_VECTOR        ( 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 key()
90          * The second problem is solved by introducing Helper-Parser which cover both the list and the number field. By that no 
91          *      templates have to be used. 
92          */
93
94         SENF_PARSER_VARIANT( fbiplist, ip_version,
95                                  ( ids(getIpv4AddressList, na, setIpVersion4, 
96                                        key(4, senf::DTCPIPv4AddressListParser)) )    //IPv4 
97                                  ( ids(getIpv6AddressList, na, setIpVersion6,
98                                        key(6, senf::DTCPIPv6AddressListParser)) ) ); //IPv6
99                                                                  
100         SENF_PARSER_FINALIZE(DTCPPacketParser);
101     };
102     
103     /** \brief DTCP packet
104         
105         \par Packet type (typedef):
106             \ref DTCPPacket
107
108         \par Fields:
109             \ref DTCPPacketParser
110
111         \ingroup protocolbundle_mpegdvb
112      */
113     struct DTCPPacketType
114         : public PacketTypeBase,
115           public PacketTypeMixin<DTCPPacketType>
116     {
117         typedef PacketTypeMixin<DTCPPacketType> mixin;
118         typedef ConcretePacket<DTCPPacketType> packet;
119         typedef DTCPPacketParser parser;
120     
121         using mixin::nextPacketRange;
122         using mixin::init;
123         using mixin::initSize;
124         
125         static void dump(packet p, std::ostream & os);
126     };
127     
128     /** \brief DTCP packet typedef */
129     typedef DTCPPacketType::packet DTCPPacket;
130 }
131
132 #endif /*DTCPPACKET_HH_*/