NetSim Source Code Help
Loading...
Searching...
No Matches
VPN.c
Go to the documentation of this file.
1/************************************************************************************
2 * Copyright (C) 2020 *
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"
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*/
26{
27 NETSIM_ID i;
28 for(i=0;i<NETWORK->nDeviceCount;i++)
29 {
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);
38 //Add new virtual interface
42 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId]=newInterface;
44 newInterface->nInterfaceId=nInteraceId+1;
45 newInterface->nProtocolId=NW_PROTOCOL_IPV4;
46 newInterface->szAddress=devVar->ipPoolStart;
47 newInterface->szSubnetMask=devVar->ipPoolMask;
49 newInterface->pstruPrevInterface=NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1];
50 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1]->pstruNextInterface=newInterface;
51 NETSIM_ID in = nInteraceId + 1;
53 IP_NETWORK_ADDRESS_IPV4(newInterface->szAddress, newInterface->szSubnetMask),
54 newInterface->szSubnetMask,
55 0,
56 NULL,
57 1,
58 &newInterface->szAddress,
59 &in,
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);
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);
78 //Add new virtual interface
82 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId]=newInterface;
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 }
92 newInterface->pstruPrevInterface=NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1];
93 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1]->pstruNextInterface=newInterface;
94 in = nInteraceId + 1;
96 IP_NETWORK_ADDRESS_IPV4(newInterface->szAddress, newInterface->szSubnetMask),
97 newInterface->szSubnetMask,
98 0,
99 NULL,
100 1,
101 &newInterface->szAddress,
102 &in,
104 "VPN");
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");
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*/
128{
129 NETSIM_IPAddress ipStart;
130 NETSIM_IPAddress ipEnd;
131 IP_DEVVAR* devVar;
132 NETSIM_IPAddress newip;
133 VPN* vpn;
134 NETSIM_ID 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);
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;
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));
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*/
213{
215 {
217 {
219 VPN* vpn=devVar->vpn;
221 VPN_PACKET* vpnPacket=calloc(1,sizeof* vpnPacket);
222 vpnPacket->destIP=packet->pstruNetworkData->szDestIP;
224 vpnPacket->sourceIP=packet->pstruNetworkData->szSourceIP;
225 vpnPacket->nPacketType = packet->nPacketType;
226 vpnPacket->nControlPacketType = packet->nControlDataType;
229 strcpy(packet->szPacketType, "VPN_Packet");
230 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket;
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 {
251 packet->pstruNetworkData->szDestIP=devVar->serverIP;
253 }
254 break;
255 default:
256 fnNetSimError("Unknown Vpn status");
257 break;
258 }
260 packet->pstruNetworkData->szNextHopIp=NULL;
264 }
265 break;
266 case NETWORK_IN_EVENT:
267 {
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;
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;
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*/
302{
303 NETSIM_ID i;
304 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
305 {
307 {
308 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
309 }
310 }
311 return fn_NetSim_Stack_GetFirstIPAddressAsId(ndeviceId,0);
312}
314{
315 NETSIM_ID i;
316 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
317 {
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 {
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*/
345{
346 NETSIM_ID i;
347 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
348 {
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*/
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}
unsigned int NETSIM_ID
Definition: Animation.h:45
#define VPN_METRIC
Definition: IP.h:47
#define PROTOCOL_VPN
Definition: IP.h:46
ptrIP_ROUTINGTABLE iptable_add(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, unsigned int prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric, char *type)
@ VPN_CLIENT
Definition: IP.h:134
@ VPN_SERVER
Definition: IP.h:133
#define DNS_ALLOC()
Definition: IP.h:299
@ PACKET_VPN
Definition: IP.h:70
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define IP_NETWORK_ADDRESS_IPV4(ip, subnet)
NETSIM_IPAddress IP_COPY_FORCE(NETSIM_IPAddress ip)
NETSIM_IPAddress IP_NETWORK_ADDRESS(NETSIM_IPAddress ip, NETSIM_IPAddress subnet, unsigned int prefix_len)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define STR_TO_IP4(ipstr)
Definition: IP_Addressing.h:94
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define LIST_ADD_LAST(ls, mem)
Definition: List.h:30
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
@ PacketType_Control
Definition: Packet.h:66
@ NW_PROTOCOL_IPV4
Definition: Stack.h:189
NETSIM_IPAddress fn_NetSim_Stack_GetFirstIPAddressAsId(NETSIM_ID nDeviceId, unsigned int type)
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ NETWORK_OUT_EVENT
Definition: Stack.h:108
@ NETWORK_IN_EVENT
Definition: Stack.h:109
#define DEVICE_NWLAYER(DeviceId)
Definition: Stack.h:785
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
@ INTERFACE_VIRTUAL
Definition: Stack.h:255
@ INTERFACE_WAN_ROUTER
Definition: Stack.h:254
#define IP_WRAPPER_GET(DeviceId)
Definition: Stack.h:801
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asIP(NETSIM_IPAddress ip, NETSIM_ID *nInterfaceId)
int freeVPNPacket(VPN_PACKET *vpnPacket)
Definition: VPN.c:370
int freeVPN(VPN *vpn)
Definition: VPN.c:357
int fn_NetSim_IP_VPN_Init()
Definition: VPN.c:25
NETSIM_IPAddress fn_NetSim_IP_FindWANInterfaceIP(NETSIM_ID ndeviceId)
Definition: VPN.c:301
void * copyVPNPacket(VPN_PACKET *vpnPacket)
Definition: VPN.c:378
int fn_NetSim_IP_VPN_Run()
Definition: VPN.c:212
int fn_NetSim_IP_VPN_GetIPFromServer(NETSIM_IPAddress server, NETSIM_IPAddress *ip, NETSIM_IPAddress *mask, NETSIM_ID nCurrentDeviceId)
Definition: VPN.c:127
NETSIM_IPAddress vpn_getVirtualIp(NETSIM_ID ndeviceId)
Definition: VPN.c:344
int vpn_addtable(ptrIP_WRAPPER wrapper, NETSIM_ID serverId, NETSIM_IPAddress virtualIp, NETSIM_ID virtualInterface)
Definition: VPN.c:325
NETSIM_IPAddress getVirtualIP(NETSIM_ID ndeviceId)
Definition: VPN.c:313
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
Structure to store the device ip details.
Definition: IP.h:226
NETSIM_IPAddress ipPoolEnd
Definition: IP.h:258
NETSIM_IPAddress ipPoolStart
Definition: IP.h:257
NETSIM_IPAddress ipPoolMask
Definition: IP.h:259
VPN_STATE nVPNStatus
Definition: IP.h:251
void * vpn
Definition: IP.h:260
NETSIM_IPAddress serverIP
Definition: IP.h:255
void * dnsList
Definition: IP.h:261
struct stru_NetSim_NetworkLayer * pstruNetworkLayer
Definition: Stack.h:721
struct stru_NetSim_Interface ** ppstruInterfaceList
Definition: Stack.h:717
NETSIM_ID nNumOfInterface
Definition: Stack.h:714
EVENT_TYPE nEventType
Definition: Stack.h:747
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nDeviceId
Definition: Stack.h:750
NETSIM_ID nInterfaceId
Definition: Stack.h:751
NETWORK_LAYER_PROTOCOL nLocalNetworkProtocol
Definition: Stack.h:695
unsigned int prefix_len
Definition: Stack.h:691
NETSIM_IPAddress szSubnetMask
Definition: Stack.h:690
NETSIM_ID nInterfaceId
Definition: Stack.h:675
INTERFACE_TYPE nInterfaceType
Definition: Stack.h:677
struct stru_NetSim_Interface * pstruPrevInterface
Definition: Stack.h:701
NETWORK_LAYER_PROTOCOL nProtocolId
Definition: Stack.h:687
struct stru_NetSim_Interface * pstruNextInterface
Definition: Stack.h:700
NETSIM_IPAddress szAddress
Definition: Stack.h:688
struct stru_NetSim_Device ** ppstruDeviceList
Definition: Stack.h:737
NETSIM_IPAddress szGatewayIP
Definition: Packet.h:200
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
char szPacketType[MAX_PACKET_TYPE_LEN]
Definition: Packet.h:271
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
unsigned int nControlDataType
Definition: Packet.h:258
PACKET_TYPE nPacketType
Definition: Packet.h:257
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
Definition: VPN.h:16
NETSIM_IPAddress * LocalIP
Definition: VPN.h:18
unsigned int nConnectedDeviceCount
Definition: VPN.h:17
NETSIM_IPAddress * InternetIP
Definition: VPN.h:19
Data structure for dns.
Definition: IP.h:294
NETSIM_ID deviceId
Definition: IP.h:295
NETSIM_IPAddress ip
Definition: IP.h:296
struct stru_ip::@5::ipv4 IPV4
union stru_ip::@5 IP