summaryrefslogtreecommitdiffstats
path: root/src/common/Network/Packet/VLANHeader.h
blob: cf6b2efffe642f66e5270c0d11d43d4c2206ac2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _VLAN_HEADER_H_
#define _VLAN_HEADER_H_
    
#include "PacketHeaderBase.h"
#include "EthernetHeader.h"


/*   
                              VLAN Header Fields
                              ------------------
                                    
    0       2   3   4                                                            15
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    |Priority| CFI |                                                             |
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    |                              Type                                          |
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/**
 * This class encapsulates a VLAN header.
 * It has fields that are equivalent to the VLAN header fields.
 * The data is saved in network byte order, and therefore the class can be used to create a packet in a buffer
 * and send it over the network.
 */ 
class VLANHeader
{


////////////////////////////////////////////////////////////////////////////////////////
// Field Manipulation
////////////////////////////////////////////////////////////////////////////////////////
public:

    // Sets the complete tag field without sub fields manipulation
    void    setVlanTag          (uint16_t data);
    uint16_t  getVlanTag          ();

    void    setTagUserPriorty   (uint8_t data);
    uint8_t   getTagUserPriorty   ();
    
    bool    getTagCFI           ();
    void    setTagCFI           (bool);

    uint16_t  getTagID            ();
    void    setTagID            (uint16_t);

    void    incrementTagID(uint16_t inc_value);

    void    setFromPkt          (uint8_t* data);

    uint8_t   reconstructPkt      (uint8_t* destBuff);


////////////////////////////////////////////////////////////////////////////////////////
// Common Interface
////////////////////////////////////////////////////////////////////////////////////////

public: 
    uint8_t*  getPointer          (){return (uint8_t*)this;}
    uint32_t  getSize      (){return (uint32_t)sizeof(VLANHeader);}

    uint16_t  getNextProtocolNetOrder    ();
	uint16_t  getNextProtocolHostOrder   ();
    void    setNextProtocolFromNetOrder(uint16_t);
	void    setNextProtocolFromHostOrder(uint16_t);

    void    dump                (FILE*  fd);

	static uint16_t bytesToSkip(uint8_t* base)
	{
		return sizeof(VLANHeader);
	}

	static uint8_t reconstructFromBuffer(uint8_t* destBuffer, uint8_t* srcBuffer);

	static uint8_t fillReconstructionBuffer(uint8_t* destBuffer, uint8_t* srcBuffer);

    static uint8_t fillReconstructionBuffer(uint8_t* destBuffer, VLANHeader& vHeader);

    
    static uint32_t getMaxVlanTag()  { return (1<<12) - 1 ; }

public:
    uint16_t      myTag;
    uint16_t      myNextProtocol;
};

#include "VLANHeader.inl"

#endif //_VLAN_HEADER_H_