NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
TCP_NetworkInterface.c
1/************************************************************************************
2* Copyright (C) 2023 *
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* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "TCP.h"
16#include "List.h"
17#include "TCP_Header.h"
18
19void send_to_network(NetSim_PACKET* packet, PNETSIM_SOCKET s)
20{
21 PTCP_SEGMENT_HDR hdr = TCP_GET_SEGMENT_HDR(packet);
22 print_tcp_log("Sending segment from outgoing tcp, local addr:%s:%d, Remote addr %s:%d, SEQ=%d, ACK=%d,SYN=%d,ACK=%d,RST=%d,URG=%d,FIN=%d,LEN=%d",
23 s->localAddr == NULL ? "0.0.0.0" : s->localAddr->ip->str_ip,
24 s->localAddr == NULL ? 0 : s->localAddr->port,
25 s->remoteAddr == NULL ? "0.0.0.0" : s->remoteAddr->ip->str_ip,
26 s->remoteAddr == NULL ? 0 : s->remoteAddr->port,
27 hdr->SeqNum,
28 hdr->AckNum,
29 hdr->Syn,
30 hdr->Ack,
31 hdr->Rst,
32 hdr->Urg,
33 hdr->Fin,
34 get_seg_len(packet));
35
36 packet->pstruTransportData->dStartTime = pstruEventDetails->dEventTime;
37 packet->pstruTransportData->dEndTime = pstruEventDetails->dEventTime;
38
39 NetSim_EVENTDETAILS pevent;
40 memcpy(&pevent, pstruEventDetails, sizeof pevent);
41 pevent.dPacketSize = packet->pstruTransportData->dPacketSize;
42 if (packet->pstruAppData)
43 {
44 pevent.nApplicationId = packet->pstruAppData->nApplicationId;
45 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
46 }
47 else
48 {
49 pevent.nSegmentId = 0;
50 }
51
52 pevent.nEventType = NETWORK_OUT_EVENT;
53 pevent.nPacketId = packet->nPacketId;
54 pevent.nProtocolId = fn_NetSim_Stack_GetNWProtocol(pevent.nDeviceId);
55 pevent.nSubEventType = 0;
56 pevent.pPacket = packet;
57 pevent.szOtherDetails = NULL;
58 fnpAddEvent(&pevent);
59}
60
61void packet_arrive_from_network_layer()
62{
63#ifdef _TEST_TCP_
64 if (!pass_test())
65 {
66 //Packet is dropped by test suite
67 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
68 return;
69 }
70#endif
71 NetSim_PACKET* packet = pstruEventDetails->pPacket;
72 PNETSIM_SOCKET s = find_socket_at_dest(packet);
73 PTCP_SEGMENT_HDR hdr = TCP_GET_SEGMENT_HDR(packet);
74 if (s && s->tcb)
75 {
76 print_tcp_log("\nDevice %d, Time %0.2lf: segment arrives at incoming tcp, local addr:%s:%d, Remote addr %s:%d, SEQ=%d, ACK=%d,SYN=%d,ACK=%d,RST=%d,URG=%d,FIN=%d,LEN=%d",
77 pstruEventDetails->nDeviceId,
78 pstruEventDetails->dEventTime,
79 s->localAddr == NULL ? "0.0.0.0" : s->localAddr->ip->str_ip,
80 s->localAddr == NULL ? 0 : s->localAddr->port,
81 s->remoteAddr == NULL ? "0.0.0.0" : s->remoteAddr->ip->str_ip,
82 s->remoteAddr == NULL ? 0 : s->remoteAddr->port,
83 hdr->SeqNum,
84 hdr->AckNum,
85 hdr->Syn,
86 hdr->Ack,
87 hdr->Rst,
88 hdr->Urg,
89 hdr->Fin,
90 get_seg_len(packet));
91
92 update_seq_num_on_receiving(s, packet);
93
94 if (s->tcb->tcp_state == TCPCONNECTION_CLOSED)
95 packet_arrive_at_closed_state(s, packet);
96
97 else if (s->tcb->tcp_state == TCPCONNECTION_LISTEN)
98 packet_arrives_at_listen_state(s, packet);
99
100 else if (s->tcb->tcp_state == TCPCONNECTION_SYN_SENT)
101 packet_arrives_at_synsent_state(s, packet);
102
103 else
104 packet_arrives_at_incoming_tcp(s, packet);
105 }
106 else
107 {
108 print_tcp_log("Packet arrive to TCP for device %d for which there is no connection. Discarding..",
109 pstruEventDetails->nDeviceId);
110 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
111 pstruEventDetails->pPacket = NULL;
112 }
113}