NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
VPN.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 "List.h"
16#include "IP.h"
17#include "VPN.h"
18_declspec(dllexport) int fn_NetSim_IP_VPN_GetIPFromServer(NETSIM_IPAddress server,NETSIM_IPAddress* ip,NETSIM_IPAddress* mask,NETSIM_ID nCurrentDeviceId);
19NETSIM_IPAddress fn_NetSim_IP_FindWANInterfaceIP(NETSIM_ID ndeviceId);
20NETSIM_IPAddress vpn_getVirtualIp(NETSIM_ID ndeviceId);
21int vpn_addtable(ptrIP_WRAPPER wrapper, NETSIM_ID serverId, NETSIM_IPAddress virtualIp, NETSIM_ID virtualInterface);
22/**
23 This function is to initialize the VPN(Virtual Private Network).
24*/
25_declspec(dllexport) int fn_NetSim_IP_VPN_Init()
26{
27 NETSIM_ID i;
28 for(i=0;i<NETWORK->nDeviceCount;i++)
29 {
30 if(NETWORK->ppstruDeviceList[i]->pstruNetworkLayer)
31 {
32 IP_DEVVAR* devVar=DEVICE_NWLAYER(i+1)->ipVar;
33 if(devVar->nVPNStatus==VPN_SERVER)
34 {
35 VPN* vpn=devVar->vpn;
36 struct stru_NetSim_Interface* newInterface=calloc(1,sizeof* newInterface);
37 NETSIM_ID nInteraceId=NETWORK->ppstruDeviceList[i]->nNumOfInterface;
38 //Add new virtual interface
39 NETWORK->ppstruDeviceList[i]->nNumOfInterface++;
40 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList = realloc(NETWORK->ppstruDeviceList[i]->ppstruInterfaceList,
41 (NETWORK->ppstruDeviceList[i]->nNumOfInterface*sizeof(struct stru_NetSim_Interface*)));
42 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId]=newInterface;
43 newInterface->nInterfaceType=INTERFACE_VIRTUAL;
44 newInterface->nInterfaceId=nInteraceId+1;
45 newInterface->nProtocolId=NW_PROTOCOL_IPV4;
46 newInterface->szAddress=devVar->ipPoolStart;
47 newInterface->szSubnetMask=devVar->ipPoolMask;
48 newInterface->nLocalNetworkProtocol=PROTOCOL_VPN;
49 newInterface->pstruPrevInterface=NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1];
50 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1]->pstruNextInterface=newInterface;
51 NETSIM_ID in = nInteraceId + 1;
52 iptable_add(IP_WRAPPER_GET(i + 1),
53 IP_NETWORK_ADDRESS_IPV4(newInterface->szAddress, newInterface->szSubnetMask),
54 newInterface->szSubnetMask,
55 0,
56 NULL,
57 1,
58 &newInterface->szAddress,
59 &in,
60 VPN_METRIC,
61 "VPN");
62 if(!vpn)
63 {
64 vpn=calloc(1,sizeof* vpn);
65 devVar->vpn=vpn;
66 vpn->LocalIP=calloc(1,sizeof* vpn->LocalIP);
67 vpn->LocalIP[0]=IP_COPY(devVar->ipPoolStart);//Server ip
68 vpn->InternetIP=calloc(1,sizeof* vpn->InternetIP);
69 vpn->InternetIP[0]=fn_NetSim_IP_FindWANInterfaceIP(i+1);
70 }
71
72 }
73 else if(devVar->nVPNStatus==VPN_CLIENT)
74 {
75 NETSIM_ID in;
76 struct stru_NetSim_Interface* newInterface=calloc(1,sizeof* newInterface);
77 NETSIM_ID nInteraceId=NETWORK->ppstruDeviceList[i]->nNumOfInterface;
78 //Add new virtual interface
79 NETWORK->ppstruDeviceList[i]->nNumOfInterface++;
80 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList = realloc(NETWORK->ppstruDeviceList[i]->ppstruInterfaceList,
81 (NETWORK->ppstruDeviceList[i]->nNumOfInterface*sizeof(struct stru_NetSim_Interface*)));
82 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId]=newInterface;
83 newInterface->nInterfaceType=INTERFACE_VIRTUAL;
84 newInterface->nInterfaceId=nInteraceId+1;
85 newInterface->nProtocolId=NW_PROTOCOL_IPV4;
86 if(!fn_NetSim_IP_VPN_GetIPFromServer(devVar->serverIP,&newInterface->szAddress,&newInterface->szSubnetMask,i+1))
87 {
88 devVar->nVPNStatus=0;//VPN fails
89 continue;
90 }
91 newInterface->nLocalNetworkProtocol=PROTOCOL_VPN;
92 newInterface->pstruPrevInterface=NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1];
93 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1]->pstruNextInterface=newInterface;
94 in = nInteraceId + 1;
95 iptable_add(IP_WRAPPER_GET(i + 1),
96 IP_NETWORK_ADDRESS_IPV4(newInterface->szAddress, newInterface->szSubnetMask),
97 newInterface->szSubnetMask,
98 0,
99 NULL,
100 1,
101 &newInterface->szAddress,
102 &in,
103 VPN_METRIC,
104 "VPN");
105 iptable_add(IP_WRAPPER_GET(i + 1),
106 devVar->serverIP,
107 STR_TO_IP4("255.255.255.255"),
108 0,
109 NULL,
110 1,
111 &newInterface->szAddress,
112 &in,
113 1,
114 "VPN");
115 vpn_addtable(IP_WRAPPER_GET(i + 1),
116 fn_NetSim_Stack_GetDeviceId_asIP(devVar->serverIP, &in),
117 newInterface->szAddress,
118 nInteraceId + 1);
119 }
120 }
121 }
122 return 1;
123}
124/**
125 This function is to get the ip address from the server.
126*/
127_declspec(dllexport) int fn_NetSim_IP_VPN_GetIPFromServer(NETSIM_IPAddress server,NETSIM_IPAddress* ip,NETSIM_IPAddress* mask,NETSIM_ID nCurrentDeviceId)
128{
129 NETSIM_IPAddress ipStart;
130 NETSIM_IPAddress ipEnd;
131 IP_DEVVAR* devVar;
132 NETSIM_IPAddress newip;
133 VPN* vpn;
134 NETSIM_ID i;
135 NETSIM_ID devid=fn_NetSim_Stack_GetDeviceId_asIP(server,&i);
136 if(!devid)
137 {
138 fnNetSimError("%s is not valid ip for any device.Check VPN server ip setting.\n",server);
139 }
140 devVar = DEVICE_NWLAYER(devid)->ipVar;
141 ipStart=devVar->ipPoolStart;
142 ipEnd=devVar->ipPoolEnd;
143 vpn=devVar->vpn;
144 if(!vpn)
145 {
146 vpn=calloc(1,sizeof* vpn);
147 devVar->vpn=vpn;
148 vpn->LocalIP=calloc(1,sizeof* vpn->LocalIP);
149 vpn->LocalIP[0]=IP_COPY(devVar->ipPoolStart);//Server ip
150 vpn->InternetIP=calloc(1,sizeof* vpn->InternetIP);
151 vpn->InternetIP[0]=fn_NetSim_IP_FindWANInterfaceIP(devid);
152 }
153 newip=IP_COPY_FORCE(ipStart);
154NEWIP_RECHECK:
155 if(newip->type == 4)
156 {
157 while(newip->IP.IPV4.byte4<ipEnd->IP.IPV4.byte4)
158 {
159 int flag=0;
160 unsigned int i;
161 for(i=0;i<=vpn->nConnectedDeviceCount;i++)
162 {
163 if(!IP_COMPARE(vpn->LocalIP[i],newip))
164 flag=1;
165 }
166 if(!flag)
167 {
168 DNS* dns;
169 vpn->nConnectedDeviceCount++;
170 vpn->LocalIP=realloc(vpn->LocalIP,(vpn->nConnectedDeviceCount+1)*(sizeof* vpn->LocalIP));
171 vpn->LocalIP[vpn->nConnectedDeviceCount]=newip;
172 vpn->InternetIP=realloc(vpn->InternetIP,(vpn->nConnectedDeviceCount+1)*(sizeof* vpn->InternetIP));
173 vpn->InternetIP[vpn->nConnectedDeviceCount]=fn_NetSim_Stack_GetFirstIPAddressAsId(nCurrentDeviceId,0);
174 *ip=newip;
175 *mask=devVar->ipPoolMask;
176 dns=DNS_ALLOC();
177 dns->deviceId=nCurrentDeviceId;
178 dns->ip=newip;
179 LIST_ADD_LAST(&(devVar->dnsList),dns);
180 return 1;
181 }
182 newip->IP.IPV4.byte4++;
183 }
184 newip->IP.IPV4.byte4=ipStart->IP.IPV4.byte4;
185 while(newip->IP.IPV4.byte3<ipEnd->IP.IPV4.byte3)
186 {
187 newip->IP.IPV4.byte3++;
188 goto NEWIP_RECHECK;
189 }
190 newip->IP.IPV4.byte3=ipStart->IP.IPV4.byte3;
191 while(newip->IP.IPV4.byte2<ipEnd->IP.IPV4.byte2)
192 {
193 newip->IP.IPV4.byte2++;
194 goto NEWIP_RECHECK;
195 }
196 newip->IP.IPV4.byte2=ipStart->IP.IPV4.byte2;
197 while(newip->IP.IPV4.byte1<ipEnd->IP.IPV4.byte1)
198 {
199 newip->IP.IPV4.byte1++;
200 goto NEWIP_RECHECK;
201 }
202 }
203 else if(newip->type == 6)
204 {
205#pragma message(__LOC__"fn_NetSim_IP_VPN_GetIPFromServer needs to be implemented for IPV6")
206 }
207 return 0;
208}
209/**
210 This function is to trigger the events of VPN, which includes NETWORK_OUT and NETWORK_IN events.
211*/
212_declspec(dllexport) int fn_NetSim_IP_VPN_Run()
213{
214 switch(pstruEventDetails->nEventType)
215 {
216 case NETWORK_OUT_EVENT:
217 {
218 IP_DEVVAR* devVar=DEVICE_NWLAYER(pstruEventDetails->nDeviceId)->ipVar;
219 VPN* vpn=devVar->vpn;
220 NetSim_PACKET* packet=pstruEventDetails->pPacket;
221 VPN_PACKET* vpnPacket=calloc(1,sizeof* vpnPacket);
222 vpnPacket->destIP=packet->pstruNetworkData->szDestIP;
223 vpnPacket->ipVar=packet->pstruNetworkData->Packet_NetworkProtocol;
224 vpnPacket->sourceIP=packet->pstruNetworkData->szSourceIP;
225 vpnPacket->nPacketType = packet->nPacketType;
226 vpnPacket->nControlPacketType = packet->nControlDataType;
227 packet->nPacketType = PacketType_Control;
228 packet->nControlDataType = PACKET_VPN;
229 strcpy(packet->szPacketType, "VPN_Packet");
230 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket;
231 packet->pstruNetworkData->nPacketFlag=PACKET_VPN;
232 switch(devVar->nVPNStatus)
233 {
234 case VPN_SERVER:
235 {
236 unsigned int i;
237 packet->pstruNetworkData->szSourceIP=vpn->InternetIP[0];
238 for(i=0;i<=vpn->nConnectedDeviceCount;i++)
239 {
240 if(!IP_COMPARE(vpn->LocalIP[i],packet->pstruNetworkData->szDestIP))
241 {
242 packet->pstruNetworkData->szDestIP=vpn->InternetIP[i];
243 break;
244 }
245 }
246 }
247 break;
248 case VPN_CLIENT:
249 {
250 vpnPacket->sourceIP=vpn_getVirtualIp(pstruEventDetails->nDeviceId);
251 packet->pstruNetworkData->szDestIP=devVar->serverIP;
252 packet->pstruNetworkData->szSourceIP=fn_NetSim_Stack_GetFirstIPAddressAsId(pstruEventDetails->nDeviceId,0);
253 }
254 break;
255 default:
256 fnNetSimError("Unknown Vpn status");
257 break;
258 }
259 packet->pstruNetworkData->szGatewayIP=packet->pstruNetworkData->szSourceIP;
260 packet->pstruNetworkData->szNextHopIp=NULL;
261 pstruEventDetails->nInterfaceId=0;
262 fnpAddEvent(pstruEventDetails);
263 pstruEventDetails->pPacket=NULL;
264 }
265 break;
266 case NETWORK_IN_EVENT:
267 {
268 NetSim_PACKET* packet = pstruEventDetails->pPacket;
269 VPN_PACKET* vpnPacket=packet->pstruNetworkData->Packet_NetworkProtocol;
270 IP_DEVVAR* devVar=DEVICE_NWLAYER(pstruEventDetails->nDeviceId)->ipVar;
271 VPN* vpn=devVar->vpn;
272 if(devVar->nVPNStatus==VPN_SERVER && !IP_COMPARE(packet->pstruNetworkData->szDestIP,vpn->InternetIP[0]))
273 {
274 packet->nPacketType=vpnPacket->nPacketType;
275 packet->nControlDataType=vpnPacket->nControlPacketType;
276 packet->pstruNetworkData->szSourceIP=vpnPacket->sourceIP;
277 packet->pstruNetworkData->szDestIP=vpnPacket->destIP;
278 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket->ipVar;
279 packet->pstruNetworkData->nPacketFlag=0;
280 }
281 else if(devVar->nVPNStatus == VPN_CLIENT && !IP_COMPARE(packet->pstruNetworkData->szSourceIP,devVar->serverIP))
282 {
283 packet->nPacketType=vpnPacket->nPacketType;
284 packet->nControlDataType=vpnPacket->nControlPacketType;
285 packet->pstruNetworkData->szDestIP=getVirtualIP(pstruEventDetails->nDeviceId);
286 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket->ipVar;
287 packet->pstruNetworkData->nPacketFlag=0;
288 }
289 free(vpnPacket);
290 }
291 break;
292 default:
293 fnNetSimError("Unknown event type for VPN");
294 break;
295 }
296 return 1;
297}
298/**
299 This function is to find the WLAN interface IP address.
300*/
301NETSIM_IPAddress fn_NetSim_IP_FindWANInterfaceIP(NETSIM_ID ndeviceId)
302{
303 NETSIM_ID i;
304 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
305 {
306 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_WAN_ROUTER)
307 {
308 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
309 }
310 }
311 return fn_NetSim_Stack_GetFirstIPAddressAsId(ndeviceId,0);
312}
313NETSIM_IPAddress getVirtualIP(NETSIM_ID ndeviceId)
314{
315 NETSIM_ID i;
316 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
317 {
318 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_VIRTUAL)
319 {
320 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
321 }
322 }
323 return NULL;
324}
325int vpn_addtable(ptrIP_WRAPPER wrapper,NETSIM_ID serverId,NETSIM_IPAddress virtualIp,NETSIM_ID virtualInterface)
326{
327 NETSIM_ID i;
328 for(i=0;i<NETWORK->ppstruDeviceList[serverId-1]->nNumOfInterface;i++)
329 {
330 if(DEVICE_INTERFACE(serverId,i+1)->nInterfaceType!=INTERFACE_WAN_ROUTER && DEVICE_INTERFACE(serverId,i+1)->nInterfaceType!=INTERFACE_VIRTUAL && DEVICE_INTERFACE(serverId,i+1)->szAddress)
331 {
332 iptable_add(wrapper,
333 IP_NETWORK_ADDRESS(DEVICE_INTERFACE(serverId, i + 1)->szAddress, DEVICE_INTERFACE(serverId, i + 1)->szSubnetMask, DEVICE_INTERFACE(serverId, i + 1)->prefix_len),
334 DEVICE_INTERFACE(serverId, i + 1)->szSubnetMask,
335 DEVICE_INTERFACE(serverId, i + 1)->prefix_len,
336 NULL, 1, &virtualIp, &virtualInterface, VPN_METRIC, "VPN");
337 }
338 }
339 return 1;
340}
341/**
342 This function is to get the virtual ip.
343*/
344NETSIM_IPAddress vpn_getVirtualIp(NETSIM_ID ndeviceId)
345{
346 NETSIM_ID i;
347 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
348 {
349 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_VIRTUAL)
350 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
351 }
352 return NULL;
353}
354/**
355 This function is to free the VPN
356*/
357int freeVPN(VPN* vpn)
358{
359 if(vpn)
360 {
361 free(vpn->InternetIP);
362 free(vpn->LocalIP);
363 free(vpn);
364 }
365 return 1;
366}
367/**
368 This function is to free the packets of VPN.
369*/
370int freeVPNPacket(VPN_PACKET* vpnPacket)
371{
372 free(vpnPacket);
373 return 1;
374}
375/**
376 This function is used to copy the vpn packet.
377*/
378void* copyVPNPacket(VPN_PACKET* vpnPacket)
379{
380 VPN_PACKET* newVpn=calloc(1,sizeof* newVpn);
381 memcpy(newVpn,vpnPacket,sizeof* newVpn);
382 return (void*)newVpn;
383}
void * ipVar
Original IP header.
Definition VPN.h:26
unsigned int nControlPacketType
Original control packet type.
Definition VPN.h:28
NETSIM_IPAddress sourceIP
Original source.
Definition VPN.h:24
PACKET_TYPE nPacketType
Original packet type.
Definition VPN.h:27
NETSIM_IPAddress destIP
Original destination.
Definition VPN.h:25