1/*
2Copyright (c) 2015-2015 Cisco Systems, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef _TCP_HEADER_H_
17#define _TCP_HEADER_H_
18
19#include "PacketHeaderBase.h"
20#define TCP_HEADER_LEN 20
21
22class TCPHeader
23{
24
25public:
26    enum TCPHeader_enum_t
27    {
28        TCP_INVALID_PORT = 0
29    };
30
31    TCPHeader(){}
32
33    TCPHeader(uint16_t    argSourcePort,
34              uint16_t    argDestinationPort,
35              uint8_t     argFlags,
36              uint32_t    argSeqNum,
37              uint32_t    argAckNum);
38
39    struct Flag
40    {
41        enum Type
42        {
43            FIN =          0x01,
44            SYN =          0x02,
45            RST =          0x04,
46            PSH =          0x08,
47            ACK =          0x10,
48            URG =          0x20
49        };
50    };
51
52    void    setSourcePort   (uint16_t);
53    uint16_t  getSourcePort   ();
54
55    void    setDestPort     (uint16_t);
56    uint16_t  getDestPort     ();
57
58    void    setSeqNumber    (uint32_t);
59    uint32_t  getSeqNumber    ();
60
61    void    setAckNumber    (uint32_t);
62    uint32_t  getAckNumber    ();
63
64    //this is factor 4
65    void    setHeaderLength (uint8_t);
66    uint8_t   getHeaderLength ();
67
68    void    setFlag         (uint8_t);
69    uint8_t   getFlags        ();
70
71    void    setFinFlag      (bool);
72    bool    getFinFlag      ();
73
74    void    setSynFlag      (bool);
75    bool    getSynFlag      ();
76
77    void    setResetFlag    (bool);
78    bool    getResetFlag    ();
79
80    void    setPushFlag     (bool);
81    bool    getPushFlag     ();
82
83    void    setAckFlag      (bool);
84    bool    getAckFlag      ();
85
86    void    setUrgentFlag   (bool);
87    bool    getUrgentFlag   ();
88
89    void    setWindowSize   (uint16_t);
90    uint16_t  getWindowSize   ();
91
92    void    setChecksum     (uint16_t);
93    uint16_t  getChecksum     ();
94
95    inline void    setChecksumRaw     (uint16_t data){
96        myChecksum = data;
97    }
98
99    void    setUrgentOffset (uint16_t);
100    uint16_t  getUrgentOffset ();
101
102    uint32_t* getOptionPtr    ();
103
104
105////////////////////////////////////////////////////////////////////////////////////////
106// Common Header Interface
107////////////////////////////////////////////////////////////////////////////////////////
108
109public:
110    uint8_t*  getPointer      (){return (uint8_t*)this;}
111    uint32_t  getSize         (){return  getHeaderLength();}
112
113    uint16_t  getNextProtocol ();
114    void    setNextProtocol (uint16_t);
115
116    void    dump            (FILE*  fd);
117
118private:
119    uint16_t mySourcePort;
120    uint16_t myDestinationPort;
121    uint32_t mySeqNum;
122    uint32_t myAckNum;
123    uint8_t  myHeaderLength;
124    uint8_t  myFlags;
125    uint16_t myWindowSize;
126    uint16_t myChecksum;
127    uint16_t myUrgentPtr;
128    uint32_t myOption[1];
129};
130
131#include "TcpHeader.inl"
132
133#endif //_TCP_HEADER_H_
134