NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
TCP_Header.h
1#pragma once
2/************************************************************************************
3* Copyright (C) 2023 *
4* TETCOS, Bangalore. India *
5* *
6* Tetcos owns the intellectual property rights in the Product and its content. *
7* The copying, redistribution, reselling or publication of any or all of the *
8* Product or its content without express prior written consent of Tetcos is *
9* prohibited. Ownership and / or any other right relating to the software and all *
10* intellectual property rights therein shall remain at all times with Tetcos. *
11* *
12* Author: Shashi Kant Suman *
13* *
14* ---------------------------------------------------------------------------------*/
15
16#ifndef _NETSIM_TCP_HEADER_H_
17#define _NETSIM_TCP_HEADER_H_
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22 //If TCP.h file is not included in .c file.
23#ifndef _NETSIM_TCP_H_
24#define PNETSIM_SOCKET void*
25#endif
26
27 typedef enum enum_tcp_packet
28 {
29 TCPPACKET_SYN = TX_PROTOCOL_TCP * 100 + 1,
30 TCPPACKET_SYNACK,
31 TCPPACKET_FIN,
32 TCPPACKET_RST,
33 TCPPACKET_ACK,
34 }TCPPACKET;
35
36 typedef enum enum_tcp_option
37 {
38 TCP_OPTION_END,
39 TCP_OPTION_NOOPERATION,
40 TCP_OPTION_MSS,
41 TCP_OPTION_WINDOW_SCALE,
42 TCP_OPTION_SACK_PERMITTED,
43 TCP_OPTION_SACK,
44 TCP_OPTION_ECHO, //obsoleted by option 8
45 TCP_OPTION_ECHOREPLY, //obsoleted by option 8
46 TCP_OPTION_TIMESTAMP,
47 }TCP_OPTION;
48
49 typedef struct stru_tcp_option
50 {
51 TCP_OPTION type;
52 void* option;
53 UINT32 size;
54 struct stru_tcp_option* next;
55 }TCPOPTION,*PTCPOPTION;
56
57/*
58 TCP Header Format
59
60
61 0 1 2 3
62 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
63 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 | Source Port | Destination Port |
65 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 | Sequence Number |
67 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 | Acknowledgment Number |
69 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 | Data | |U|A|P|R|S|F| |
71 | Offset| Reserved |R|C|S|S|Y|I| Window |
72 | | |G|K|H|T|N|N| |
73 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 | Checksum | Urgent Pointer |
75 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 | Options | Padding |
77 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 | data |
79 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80
81 TCP Header Format
82 */
83 //Looks little different than RFC as below is used in Linux or windows.
84 typedef struct stru_tcp_header
85 {
86 UINT16 SrcPort;
87 UINT16 DstPort;
88 UINT32 SeqNum;
89 UINT32 AckNum;
90 UINT32 HdrLength : 8;
91 UINT32 Fin : 1;
92 UINT32 Syn : 1;
93 UINT32 Rst : 1;
94 UINT32 Psh : 1;
95 UINT32 Ack : 1;
96 UINT32 Urg : 1;
97 UINT32 Reserved : 2;
98 UINT16 Window;
99 UINT16 Checksum;
100 UINT16 UrgPtr;
101 PTCPOPTION option;
102 }TCP_SEGMENT_HDR,*PTCP_SEGMENT_HDR;
103#define TCP_HDR_SIZE 20 //Bytes
104#define MAX_TCP_OPT_LEN 40 //Bytes
105#define MAX_TCP_HDR_SIZE (TCP_HDR_SIZE+MAX_TCP_OPT_LEN)
106
107 static PTCP_SEGMENT_HDR TCP_GET_SEGMENT_HDR(NetSim_PACKET* packet)
108 {
109 if (packet &&
110 packet->pstruTransportData &&
111 packet->pstruTransportData->nTransportProtocol == TX_PROTOCOL_TCP)
112 return packet->pstruTransportData->Packet_TransportProtocol;
113 return NULL;
114 }
115
116 typedef struct stru_mss_option
117 {
118 UINT8 type;
119 UINT8 len;
120 UINT16 MSSData;
121 }MSS_OPTION, *PMSS_OPTION;
122#define MSS_OPTION_LEN 4 //Bytes
123
125 {
126 UINT8 type;
127 UINT8 len;
128 }SACKPERMITTED_OPTION, *PSACKPERMITTED_OPTION;
129#define SACKPERMITTED_OPTION_LEN 2 //Bytes
130
131 typedef struct stru_sack_data
132 {
133 UINT32 leftEdge;
134 UINT32 rightEdge;
135 }SACKDATA, *PSACKDATA;
136#define SACKDATA_LEN 8 //Bytes
137
138 typedef struct stru_sack_option
139 {
140 UINT8 type;
141 UINT8 len;
142 PSACKDATA* sackData;
143 }SACK_OPTION, *PSACK_OPTION;
144#define SACK_OPTION_FIX_LEN 2 //Bytes
145#define SACK_OPTION_LEN(n) ((UINT8)(SACK_OPTION_FIX_LEN+(n)*SACKDATA_LEN)) //Bytes
146#define get_sack_data_count(sack) ((sack->len-SACK_OPTION_FIX_LEN)/SACKDATA_LEN)
147
149 {
150 UINT8 type;
151 }EXTRA_OPTION, *PEXTRA_OPTION;
152#define EXTRA_OPTION_LEN 1 //Bytes
153
155 {
156 UINT8 type;
157 UINT8 len;
158 UINT8 Shift_cnt;
159 }Wsopt, *PWsopt;
160#define WSOPT_LEN 3 //Bytes
161
163 {
164 UINT8 type;
165 UINT8 len;
166 UINT32 TSval;
167 UINT32 TSecr;
168 }TSopt, *PTSopt;
169#define TSOPT_LEN 10 //Bytes
170
171 //Function prototype
172 PTCP_SEGMENT_HDR copy_tcp_hdr(PTCP_SEGMENT_HDR hdr);
173 void free_tcp_hdr(PTCP_SEGMENT_HDR hdr);
174 void* get_tcp_option(PTCP_SEGMENT_HDR hdr, TCP_OPTION type);
175 void set_tcp_option(PTCP_SEGMENT_HDR hdr, void* option, TCP_OPTION type, UINT32 size);
176
177 //SACK
178 void set_sack_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr, double time);
179 void receive_sack_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr);
180
181 //Window Scaling
182 void set_window_scaling(PNETSIM_SOCKET s, PWsopt opt);
183
184 //TimeStamp option
185 void set_timestamp_value(PNETSIM_SOCKET s,
186 PTCP_SEGMENT_HDR hdr,
187 PTSopt opt);
188
189#ifdef __cplusplus
190}
191#endif
192#endif //_NETSIM_TCP_HEADER_H_