NetSim Source Code Help
Loading...
Searching...
No Matches
TCP_Packet.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2022 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#include "main.h"
25#include "TCP.h"
26#include "TCP_Header.h"
27
29{
31 if (hdr->Syn &&
32 !hdr->Ack)
33 return true;
34 else
35 return false;
36}
37
39{
41 return hdr->Syn ? true : false;
42}
43
44
45void set_tcp_option(PTCP_SEGMENT_HDR hdr, void* option, TCP_OPTION type, UINT32 size)
46{
47 PTCPOPTION opt = (PTCPOPTION)calloc(1, sizeof* opt);
48 opt->type = type;
49 opt->option = option;
50 opt->size = size;
51
52 hdr->HdrLength += size;
53
54 if (hdr->option)
55 {
56 PTCPOPTION o = hdr->option;
57 while (o->next)
58 o = o->next;
59 o->next = opt;
60 }
61 else
62 hdr->option = opt;
63}
64
66{
67 PTCPOPTION o = hdr->option;
68 while (o)
69 {
70 if (o->type == type)
71 return o->option;
72 o = o->next;
73 }
74 return NULL;
75}
76
78{
79 UINT32 i;
80 for (i = 0; i < 4-l; i++)
81 {
82 if (i == 4 - l - 1)
83 {
84 PEXTRA_OPTION end = calloc(1, sizeof* end);
85 end->type = TCP_OPTION_END;
87 }
88 else
89 {
90 PEXTRA_OPTION nop = calloc(1, sizeof* nop);
93 }
94 }
95}
96
98{
100 if (hdr->HdrLength % 4)
101 {
102 add_tcp_extra_option(hdr, hdr->HdrLength % 4);
103 }
107}
108
111{
112 PMSS_OPTION opt = (PMSS_OPTION)calloc(1, sizeof* opt);
113 opt->type = TCP_OPTION_MSS;
114 opt->len = MSS_OPTION_LEN;
115 opt->MSSData = s->tcb->get_MSS(s);
117}
118
121{
126}
127
130{
131 PWsopt opt = (PWsopt)calloc(1, sizeof* opt);
133 opt->len = WSOPT_LEN;
134 opt->Shift_cnt = s->tcb->Snd.Wind_Shift;
136
137 PEXTRA_OPTION ex = (PEXTRA_OPTION)calloc(1, sizeof* ex);
140}
141
144{
145 PTSopt opt = (PTSopt)calloc(1, sizeof* opt);
147 opt->len = TSOPT_LEN;
148
150
151 if (!hdr->Ack)
152 opt->TSecr = 0;
153 else
154 opt->TSecr = s->tcb->TSVal;
155
157
158 PEXTRA_OPTION ex = (PEXTRA_OPTION)calloc(1, sizeof* ex);
161}
162
164 TCPPACKET type,
165 double time)
166{
167 PTCP_SEGMENT_HDR hdr = (PTCP_SEGMENT_HDR)calloc(1, sizeof* hdr);
168 hdr->HdrLength = TCP_HDR_SIZE;
169 hdr->SrcPort = s->localAddr->port;
170 hdr->DstPort = s->remoteAddr->port;
171
173 p->nControlDataType = type;
176 p->nSourceId = s->localDeviceId;
185
190
191 return p;
192}
193
195 double time)
196{
199 time);
200
202
203 hdr->Syn = 1;
204 hdr->SeqNum = s->tcb->ISS;
205 hdr->Window = s->tcb->get_WND(s);
206
207 add_tcp_mss_option(s, hdr);
208
209 if (s->tcb->isSackPermitted)
211
212 if (s->tcb->isTSopt)
214
215 if (s->tcb->isWindowScaling)
217
219
220 return p;
221}
222
224 double time)
225{
228 time);
229
231
232 hdr->Syn = 1;
233 hdr->Ack = 1;
234 hdr->SeqNum = s->tcb->ISS;
235 hdr->AckNum = s->tcb->RCV.NXT;
236 hdr->Window = s->tcb->get_WND(s);
237
238 add_tcp_mss_option(s, hdr);
239
240 if (s->tcb->isWindowScaling)
242
243 if (s->tcb->isSackPermitted)
245
246 if (s->tcb->isTSopt)
248
250
251 return p;
252}
253
255 double time,
256 UINT c)
257{
258 NETSIM_SOCKET temp;
259 SOCKETADDRESS l,r;
260
263 temp.localAddr = &l;
264
267 temp.remoteAddr = &r;
268
270 temp.remoteDeviceId = p->nSourceId;
271
274 time);
275
278
279 if (seg->Ack || c == 1)
280 {
281 hdr->SeqNum = seg->AckNum;
282 hdr->Rst = 1;
283 }
284 else
285 {
286 hdr->SeqNum = 0;
287 hdr->AckNum = seg->SeqNum + seg->HdrLength;
288 hdr->Rst = 1;
289 hdr->Ack = 1;
290 }
291
293
294 return packet;
295}
296
298 double time,
299 UINT32 seqno,
300 UINT32 ackno)
301{
304 time);
305
307
308 hdr->Ack = 1;
309 hdr->SeqNum = seqno;
310 hdr->AckNum = ackno;
311 hdr->Window = s->tcb->get_WND(s);
312
313 if (s->tcb->isTSopt)
315
316 set_sack_option(s, hdr, time);
318
319 return p;
320}
321
323 double time)
324{
327 time);
328
330
331 hdr->Fin = 1;
332 hdr->SeqNum = s->tcb->ISS + 1 == s->tcb->SND.NXT ?
333 s->tcb->SND.NXT + 1 : s->tcb->SND.NXT;
335
337
338 return p;
339}
340
342{
343 PTCP_SEGMENT_HDR hdr = (PTCP_SEGMENT_HDR)calloc(1, sizeof* hdr);
344 hdr->DstPort = s->remoteAddr->port;
345 hdr->HdrLength = TCP_HDR_SIZE;
346 hdr->SeqNum = s->tcb->SND.NXT;
347 hdr->SrcPort = s->localAddr->port;
349
350 if(s->tcb->isTSopt)
352
360
362
364}
365
367{
368 PTCPOPTION o = (PTCPOPTION)calloc(1, sizeof* o);
369 memcpy(o, opt, sizeof* o);
370 o->next = NULL;
371 PMSS_OPTION mss = opt->option;
372 PMSS_OPTION m = (PMSS_OPTION)calloc(1, sizeof* m);
373 memcpy(m, mss, sizeof* m);
374 o->option = m;
375 return o;
376}
377
379{
380 PTCPOPTION o = (PTCPOPTION)calloc(1, sizeof* o);
381 memcpy(o, opt, sizeof* o);
382 o->next = NULL;
383 PSACKPERMITTED_OPTION sackPermitted = opt->option;
385 memcpy(sp, sackPermitted, sizeof* sp);
386 o->option = sp;
387 return o;
388}
389
391{
392 PTCPOPTION o = (PTCPOPTION)calloc(1, sizeof* o);
393 memcpy(o, opt, sizeof* o);
394 o->next = NULL;
395 PEXTRA_OPTION ex = opt->option;
396 PEXTRA_OPTION dex = (PEXTRA_OPTION)calloc(1, sizeof* dex);
397 memcpy(dex, ex, sizeof* dex);
398 o->option = dex;
399 return o;
400}
401
403{
404 PSACK_OPTION sack = t->option;
405 PSACK_OPTION s = (PSACK_OPTION)calloc(1, sizeof* s);
406 memcpy(s, sack, sizeof* s);
407 s->sackData = NULL;
408 UINT c = get_sack_data_count(sack);
409 s->sackData = (PSACKDATA*)calloc(c, sizeof* s->sackData);
410 for (UINT i = 0; i < c; i++)
411 {
412 s->sackData[i] = (PSACKDATA)calloc(1, sizeof* s->sackData[i]);
413 memcpy(s->sackData[i], sack->sackData[i], sizeof* s->sackData[i]);
414 }
415 PTCPOPTION r = (PTCPOPTION)calloc(1, sizeof* r);
416 r->option = s;
417 r->size = t->size;
418 r->type = t->type;
419 return r;
420}
421
423{
424 PTCPOPTION o = (PTCPOPTION)calloc(1, sizeof* o);
425 memcpy(o, opt, sizeof* o);
426 o->next = NULL;
427 PWsopt ex = opt->option;
428 PWsopt dex = (PWsopt)calloc(1, sizeof* dex);
429 memcpy(dex, ex, sizeof* dex);
430 o->option = dex;
431 return o;
432}
433
435{
436 PTCPOPTION o = (PTCPOPTION)calloc(1, sizeof* o);
437 memcpy(o, opt, sizeof* o);
438 o->next = NULL;
439 PTSopt ex = opt->option;
440 PTSopt dex = (PTSopt)calloc(1, sizeof* dex);
441 memcpy(dex, ex, sizeof* dex);
442 o->option = dex;
443 return o;
444}
445
447{
448 PTCPOPTION opt = hdr->option;
449 dhdr->option = NULL;
450 while (opt)
451 {
452 PTCPOPTION dopt = NULL;
453 switch (opt->type)
454 {
455 case TCP_OPTION_END:
457 dopt = copy_tcp_extra_option(opt);
458 break;
459 case TCP_OPTION_MSS:
460 dopt = copy_tcp_mss_option(opt);
461 break;
464 break;
465 case TCP_OPTION_SACK:
466 dopt = copy_sack_option(opt);
467 break;
469 dopt = copy_window_scale_option(opt);
470 break;
472 dopt = copy_timestamp_option(opt);
473 break;
474 default:
475 fnNetSimError("Unknown TCP option %d\n", opt->type);
476 break;
477 }
478 if (dhdr->option)
479 {
480 PTCPOPTION o = dhdr->option;
481 while (o->next)
482 o = o->next;
483 o->next = dopt;
484 }
485 else
486 {
487 dhdr->option = dopt;
488 }
489 opt = opt->next;
490 }
491}
492
494{
495 if (hdr)
496 {
497 PTCP_SEGMENT_HDR dhdr = (PTCP_SEGMENT_HDR)calloc(1, sizeof * dhdr);
498 memcpy(dhdr, hdr, sizeof * dhdr);
499 copy_tcp_option(dhdr, hdr);
500 return dhdr;
501 }
502 return NULL;
503}
504
506{
507 free(mss);
508}
509
511{
512 free(sackPermitted);
513}
514
516{
517 free(ex);
518}
519
521{
522 UINT c = get_sack_data_count(sack);
523 for (UINT i = 0; i < c; i++)
524 {
525 free(sack->sackData[i]);
526 }
527 free(sack->sackData);
528 free(sack);
529}
530
532{
533 free(opt);
534}
535
537{
538 free(opt);
539}
540
542{
543 while (opt)
544 {
545 PTCPOPTION o = opt->next;
546 switch (opt->type)
547 {
548 case TCP_OPTION_END:
551 break;
552 case TCP_OPTION_MSS:
554 break;
557 break;
558 case TCP_OPTION_SACK:
560 break;
563 break;
566 break;
567 default:
568 fnNetSimError("Unknown TCP option %d\n", opt->type);
569 break;
570 }
571 free(opt);
572 opt = o;
573 }
574}
575
577{
579 free(hdr);
580}
#define MAX_TTL
Definition: Application.h:34
#define c
#define UINT
Definition: Linux.h:38
#define UINT32
Definition: Linux.h:35
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
@ PacketType_Control
Definition: Packet.h:66
NETSIM_ID get_first_dest_from_packet(NetSim_PACKET *packet)
@ IPPROTOCOL_TCP
Definition: Packet.h:129
void add_dest_to_packet(NetSim_PACKET *packet, NETSIM_ID dest)
void set_sack_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr, double time)
Definition: SACK.c:68
@ TX_PROTOCOL_TCP
Definition: Stack.h:180
@ TRANSPORT_LAYER
Definition: Stack.h:97
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
UINT16 window_scale_get_wnd(PNETSIM_SOCKET s)
Definition: WindowScale.c:63
#define WSOPT_LEN
Definition: TCP_Header.h:160
enum enum_tcp_packet TCPPACKET
#define MSS_OPTION_LEN
Definition: TCP_Header.h:122
struct stru_tcp_extra_option * PEXTRA_OPTION
@ TCPPACKET_SYNACK
Definition: TCP_Header.h:30
@ TCPPACKET_ACK
Definition: TCP_Header.h:33
@ TCPPACKET_SYN
Definition: TCP_Header.h:29
@ TCPPACKET_FIN
Definition: TCP_Header.h:31
@ TCPPACKET_RST
Definition: TCP_Header.h:32
struct stru_sack_data * PSACKDATA
struct stru_mss_option * PMSS_OPTION
#define get_sack_data_count(sack)
Definition: TCP_Header.h:146
#define TCP_HDR_SIZE
Definition: TCP_Header.h:103
#define SACKPERMITTED_OPTION_LEN
Definition: TCP_Header.h:129
#define TSOPT_LEN
Definition: TCP_Header.h:169
struct stru_tcp_timestamp_option * PTSopt
struct stru_sack_permitted_option * PSACKPERMITTED_OPTION
struct stru_tcp_option * PTCPOPTION
struct stru_sack_option * PSACK_OPTION
struct stru_tcp_window_scale_option * PWsopt
enum enum_tcp_option TCP_OPTION
struct stru_tcp_header * PTCP_SEGMENT_HDR
@ TCP_OPTION_WINDOW_SCALE
Definition: TCP_Header.h:41
@ TCP_OPTION_TIMESTAMP
Definition: TCP_Header.h:46
@ TCP_OPTION_SACK
Definition: TCP_Header.h:43
@ TCP_OPTION_MSS
Definition: TCP_Header.h:40
@ TCP_OPTION_SACK_PERMITTED
Definition: TCP_Header.h:42
@ TCP_OPTION_NOOPERATION
Definition: TCP_Header.h:39
@ TCP_OPTION_END
Definition: TCP_Header.h:38
static PTCP_SEGMENT_HDR TCP_GET_SEGMENT_HDR(NetSim_PACKET *packet)
Definition: TCP_Header.h:107
#define EXTRA_OPTION_LEN
Definition: TCP_Header.h:152
NetSim_PACKET * create_ack(PNETSIM_SOCKET s, double time, UINT32 seqno, UINT32 ackno)
Definition: TCP_Packet.c:297
static PTCPOPTION copy_tcp_mss_option(PTCPOPTION opt)
Definition: TCP_Packet.c:366
static void copy_tcp_option(PTCP_SEGMENT_HDR dhdr, PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:446
static void free_tcp_timestamp_option(PTSopt opt)
Definition: TCP_Packet.c:536
static void add_tcp_extra_option(PTCP_SEGMENT_HDR hdr, UINT32 l)
Definition: TCP_Packet.c:77
static void free_tcp_window_scale_option(PWsopt opt)
Definition: TCP_Packet.c:531
NetSim_PACKET * create_synAck(PNETSIM_SOCKET s, double time)
Definition: TCP_Packet.c:223
NetSim_PACKET * create_tcp_ctrl_packet(PNETSIM_SOCKET s, TCPPACKET type, double time)
Definition: TCP_Packet.c:163
static PTCPOPTION copy_sack_option(PTCPOPTION t)
Definition: TCP_Packet.c:402
void free_tcp_hdr(PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:576
NetSim_PACKET * create_rst(NetSim_PACKET *p, double time, UINT c)
Definition: TCP_Packet.c:254
static void set_tcp_packet_size(NetSim_PACKET *p)
Definition: TCP_Packet.c:97
static void add_tcp_mss_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:109
static PTCPOPTION copy_tcp_sack_permitted_option(PTCPOPTION opt)
Definition: TCP_Packet.c:378
bool isSynbitSet(NetSim_PACKET *packet)
Definition: TCP_Packet.c:38
PTCP_SEGMENT_HDR copy_tcp_hdr(PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:493
void set_tcp_option(PTCP_SEGMENT_HDR hdr, void *option, TCP_OPTION type, UINT32 size)
Definition: TCP_Packet.c:45
static void free_tcp_sack_option(PSACK_OPTION sack)
Definition: TCP_Packet.c:520
void add_tcp_hdr(NetSim_PACKET *p, PNETSIM_SOCKET s)
Definition: TCP_Packet.c:341
bool isSynPacket(NetSim_PACKET *packet)
Definition: TCP_Packet.c:28
static void add_tcp_window_scale_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:128
NetSim_PACKET * create_syn(PNETSIM_SOCKET s, double time)
Definition: TCP_Packet.c:194
static void add_tcp_timestamp_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:142
static void free_tcp_option(PTCPOPTION opt)
Definition: TCP_Packet.c:541
static PTCPOPTION copy_window_scale_option(PTCPOPTION opt)
Definition: TCP_Packet.c:422
static void free_tcp_extra_option(PEXTRA_OPTION ex)
Definition: TCP_Packet.c:515
static PTCPOPTION copy_tcp_extra_option(PTCPOPTION opt)
Definition: TCP_Packet.c:390
static PTCPOPTION copy_timestamp_option(PTCPOPTION opt)
Definition: TCP_Packet.c:434
static void add_tcp_sack_permitted_option(PNETSIM_SOCKET s, PTCP_SEGMENT_HDR hdr)
Definition: TCP_Packet.c:119
void * get_tcp_option(PTCP_SEGMENT_HDR hdr, TCP_OPTION type)
Definition: TCP_Packet.c:65
static void free_tcp_sack_permitted_option(PSACKPERMITTED_OPTION sackPermitted)
Definition: TCP_Packet.c:510
NetSim_PACKET * create_fin(PNETSIM_SOCKET s, double time)
Definition: TCP_Packet.c:322
static void free_tcp_mss_option(PMSS_OPTION mss)
Definition: TCP_Packet.c:505
#define fn_NetSim_Packet_CreatePacket(layer)
Definition: main.h:186
IP_PROTOCOL_NUMBER IPProtocol
Definition: Packet.h:203
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
TRANSPORT_LAYER_PROTOCOL nTransportProtocol
Definition: Packet.h:186
unsigned short int nSourcePort
Definition: Packet.h:184
unsigned short int nDestinationPort
Definition: Packet.h:185
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
unsigned int nControlDataType
Definition: Packet.h:258
PACKET_TYPE nPacketType
Definition: Packet.h:257
struct stru_NetSim_Packet_TransportLayer * pstruTransportData
Definition: Packet.h:274
NETSIM_ID nSourceId
Definition: Packet.h:263
PTCB tcb
Definition: TCP.h:132
PSOCKETADDRESS localAddr
Definition: TCP.h:121
NETSIM_ID remoteDeviceId
Definition: TCP.h:126
NETSIM_ID localDeviceId
Definition: TCP.h:125
PSOCKETADDRESS remoteAddr
Definition: TCP.h:122
PSACKDATA * sackData
Definition: TCP_Header.h:142
NETSIM_IPAddress ip
Definition: TCP.h:114
UINT16 port
Definition: TCP.h:115
struct stru_tcp_Transmission_Control_Block::stru_tcb_send_seq_var SND
UINT16(* get_MSS)(PNETSIM_SOCKET)
Definition: TCB.h:134
struct stru_tcp_Transmission_Control_Block::stru_tcp_snd Snd
struct stru_tcp_Transmission_Control_Block::stru_tcb_recv_seq_var RCV
UINT16(* get_WND)(PNETSIM_SOCKET)
Definition: TCB.h:135
PTCPOPTION option
Definition: TCP_Header.h:101
UINT32 HdrLength
Definition: TCP_Header.h:90
UINT16 DstPort
Definition: TCP_Header.h:87
UINT16 SrcPort
Definition: TCP_Header.h:86
TCP_OPTION type
Definition: TCP_Header.h:51
struct stru_tcp_option * next
Definition: TCP_Header.h:54