NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
TCP_ApplicationInterface.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* 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 "List.h"
27
28//Function prototype
29void fnConnectCallback(PNETSIM_SOCKET s);
30void fnListenCallback(PNETSIM_SOCKET s, NetSim_PACKET*);
31
32PSOCKETADDRESS anySocketAddr = NULL;
33
34void init_anysockaddr()
35{
36 anySocketAddr = (PSOCKETADDRESS)calloc(1, sizeof* anySocketAddr);
37 anySocketAddr->ip = STR_TO_IP("0.0.0.0", 4);
38 anySocketAddr->port = 0;
39}
40
41PSOCKETADDRESS get_anysockaddr()
42{
43 return anySocketAddr;
44}
45
46void tcp_init(NETSIM_ID d)
47{
48 if (!get_anysockaddr())
49 init_anysockaddr();
50
51 print_tcp_log("\nDevice %d, Time 0.0: Create socket for listen mode with remote addr 0.0.0.0:0", d);
52
53 PNETSIM_SOCKET listenSocket = tcp_create_socket();
54
55 add_to_socket_list(d, listenSocket);
56
57 PSOCKETADDRESS localsocketAddr = (PSOCKETADDRESS)calloc(1, sizeof* localsocketAddr);
58 localsocketAddr->ip = DEVICE_NWADDRESS(d,1);
59 localsocketAddr->port = 0;
60
61 listenSocket->localAddr = localsocketAddr;
62
63 listenSocket->localDeviceId = d;
64
65 print_tcp_log("Binding listen socket", d);
66
67 tcp_bind(listenSocket, get_anysockaddr());
68
69 print_tcp_log("Listening...", d);
70 tcp_listen(listenSocket, fnListenCallback);
71
72 tcp_create_metrics(listenSocket);
73
74}
75
76int packet_arrive_from_application_layer()
77{
78 NetSim_PACKET* packet = GET_PACKET_FROM_APP(false);
79
80 ptrSOCKETINTERFACE sId = pstruEventDetails->szOtherDetails;
81
82 PNETSIM_SOCKET s = find_socket_at_source(packet);
83
84 if (!s)
85 {
86
87 PNETSIM_SOCKET localSocket = tcp_create_socket();
88
89 add_to_socket_list(pstruEventDetails->nDeviceId, localSocket);
90
91 PSOCKETADDRESS remotesocketAddr = (PSOCKETADDRESS)calloc(1, sizeof* remotesocketAddr);
92 remotesocketAddr->ip = IP_COPY(packet->pstruNetworkData->szDestIP);
93 remotesocketAddr->port = packet->pstruTransportData->nDestinationPort;
94
95 PSOCKETADDRESS localsocketAddr = (PSOCKETADDRESS)calloc(1, sizeof* localsocketAddr);
96 localsocketAddr->ip = IP_COPY(packet->pstruNetworkData->szSourceIP);
97 localsocketAddr->port = packet->pstruTransportData->nSourcePort;
98
99 localSocket->localDeviceId = packet->nSourceId;
100 localSocket->remoteDeviceId = get_first_dest_from_packet(packet);
101 localSocket->sId = sId;
102 localSocket->appId = pstruEventDetails->nApplicationId;
103
104 print_tcp_log("\nDevice %d, Time %0.2lf: Creating socket with local addr %s:%d, Remote addr %s:%d",
105 packet->nSourceId,
106 pstruEventDetails->dEventTime,
107 localsocketAddr->ip->str_ip,
108 localsocketAddr->port,
109 remotesocketAddr->ip->str_ip,
110 remotesocketAddr->port);
111 print_tcp_log("Connecting socket...", packet->nSourceId);
112 tcp_connect(localSocket,localsocketAddr,remotesocketAddr);
113
114 }
115 else if (s->waitFromApp)
116 {
117 print_tcp_log("\nDevice %d, Time %0.2lf:",
118 packet->nSourceId,
119 pstruEventDetails->dEventTime);
120
121 if (!s->sId)
122 {
123 s->appId = pstruEventDetails->nApplicationId;
124 s->sId = sId;
125 }
126 else if (s->sId != sId)
127 {
128 fnNetSimError("Socket id in TCP (%d) and in application (%d) is mismatched.\n",
129 s->sId, sId);
130 }
131 send_segment(s);
132 s->waitFromApp = false;
133 }
134 else
135 {
136 //do nothing. Wait for TCP to complete previous operation
137 }
138 return 0;
139}
140
141void fnListenCallback(PNETSIM_SOCKET s, NetSim_PACKET* p)
142{
143 print_tcp_log("Accepting Connection...");
144 PNETSIM_SOCKET localSocket = tcp_accept(s, p);
145}
146
147void send_to_application(PNETSIM_SOCKET s, NetSim_PACKET* p)
148{
149 NetSim_EVENTDETAILS pevent;
150
151 if (p->pstruAppData) //Send only app layer packet
152 {
153 memcpy(&pevent, pstruEventDetails, sizeof pevent);
154 pevent.dPacketSize = p->pstruAppData->dPacketSize;
155 pevent.nApplicationId = p->pstruAppData->nApplicationId;
156 pevent.nEventType = APPLICATION_IN_EVENT;
157 pevent.nPacketId = p->nPacketId;
158 pevent.nProtocolId = PROTOCOL_APPLICATION;
159 pevent.nSegmentId = p->pstruAppData->nSegmentId;
160 pevent.nSubEventType = 0;
161 pevent.pPacket = p;
162 pevent.szOtherDetails = NULL;
163 fnpAddEvent(&pevent);
164 }
165
166 NetSim_PACKET* pr;
167 while(true)
168 {
169 pr = check_for_other_segment_to_send_from_queue(s);
170 if (!pr)
171 break;
172
173 if(!pr->pstruAppData)
174 continue;
175
176 memcpy(&pevent, pstruEventDetails, sizeof pevent);
177 pevent.dPacketSize = pr->pstruAppData->dPacketSize;
178 pevent.nApplicationId = pr->pstruAppData->nApplicationId;
179 pevent.nEventType = APPLICATION_IN_EVENT;
180 pevent.nPacketId = pr->nPacketId;
181 pevent.nProtocolId = PROTOCOL_APPLICATION;
182 pevent.nSegmentId = pr->pstruAppData->nSegmentId;
183 pevent.nSubEventType = 0;
184 pevent.pPacket = pr;
185 pevent.szOtherDetails = NULL;
186 fnpAddEvent(&pevent);
187
188 }
189}