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);
25_declspec(dllexport)
int fn_NetSim_IP_VPN_Init()
28 for(i=0;i<NETWORK->nDeviceCount;i++)
30 if(NETWORK->ppstruDeviceList[i]->pstruNetworkLayer)
32 IP_DEVVAR* devVar=DEVICE_NWLAYER(i+1)->ipVar;
33 if(devVar->nVPNStatus==VPN_SERVER)
37 NETSIM_ID nInteraceId=NETWORK->ppstruDeviceList[i]->nNumOfInterface;
39 NETWORK->ppstruDeviceList[i]->nNumOfInterface++;
40 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList = realloc(NETWORK->ppstruDeviceList[i]->ppstruInterfaceList,
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,
58 &newInterface->szAddress,
64 vpn=calloc(1,
sizeof* vpn);
66 vpn->LocalIP=calloc(1,
sizeof* vpn->LocalIP);
67 vpn->LocalIP[0]=IP_COPY(devVar->ipPoolStart);
68 vpn->InternetIP=calloc(1,
sizeof* vpn->InternetIP);
69 vpn->InternetIP[0]=fn_NetSim_IP_FindWANInterfaceIP(i+1);
73 else if(devVar->nVPNStatus==VPN_CLIENT)
77 NETSIM_ID nInteraceId=NETWORK->ppstruDeviceList[i]->nNumOfInterface;
79 NETWORK->ppstruDeviceList[i]->nNumOfInterface++;
80 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList = realloc(NETWORK->ppstruDeviceList[i]->ppstruInterfaceList,
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))
91 newInterface->nLocalNetworkProtocol=PROTOCOL_VPN;
92 newInterface->pstruPrevInterface=NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1];
93 NETWORK->ppstruDeviceList[i]->ppstruInterfaceList[nInteraceId-1]->pstruNextInterface=newInterface;
95 iptable_add(IP_WRAPPER_GET(i + 1),
96 IP_NETWORK_ADDRESS_IPV4(newInterface->szAddress, newInterface->szSubnetMask),
97 newInterface->szSubnetMask,
101 &newInterface->szAddress,
105 iptable_add(IP_WRAPPER_GET(i + 1),
107 STR_TO_IP4(
"255.255.255.255"),
111 &newInterface->szAddress,
115 vpn_addtable(IP_WRAPPER_GET(i + 1),
116 fn_NetSim_Stack_GetDeviceId_asIP(devVar->serverIP, &in),
117 newInterface->szAddress,
127_declspec(dllexport)
int fn_NetSim_IP_VPN_GetIPFromServer(NETSIM_IPAddress server,NETSIM_IPAddress* ip,NETSIM_IPAddress* mask,NETSIM_ID nCurrentDeviceId)
129 NETSIM_IPAddress ipStart;
130 NETSIM_IPAddress ipEnd;
132 NETSIM_IPAddress newip;
135 NETSIM_ID devid=fn_NetSim_Stack_GetDeviceId_asIP(server,&i);
138 fnNetSimError(
"%s is not valid ip for any device.Check VPN server ip setting.\n",server);
140 devVar = DEVICE_NWLAYER(devid)->ipVar;
141 ipStart=devVar->ipPoolStart;
142 ipEnd=devVar->ipPoolEnd;
146 vpn=calloc(1,
sizeof* vpn);
148 vpn->LocalIP=calloc(1,
sizeof* vpn->LocalIP);
149 vpn->LocalIP[0]=IP_COPY(devVar->ipPoolStart);
150 vpn->InternetIP=calloc(1,
sizeof* vpn->InternetIP);
151 vpn->InternetIP[0]=fn_NetSim_IP_FindWANInterfaceIP(devid);
153 newip=IP_COPY_FORCE(ipStart);
157 while(newip->IP.IPV4.byte4<ipEnd->IP.IPV4.byte4)
161 for(i=0;i<=vpn->nConnectedDeviceCount;i++)
163 if(!IP_COMPARE(vpn->LocalIP[i],newip))
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);
175 *mask=devVar->ipPoolMask;
177 dns->deviceId=nCurrentDeviceId;
179 LIST_ADD_LAST(&(devVar->dnsList),dns);
182 newip->IP.IPV4.byte4++;
184 newip->IP.IPV4.byte4=ipStart->IP.IPV4.byte4;
185 while(newip->IP.IPV4.byte3<ipEnd->IP.IPV4.byte3)
187 newip->IP.IPV4.byte3++;
190 newip->IP.IPV4.byte3=ipStart->IP.IPV4.byte3;
191 while(newip->IP.IPV4.byte2<ipEnd->IP.IPV4.byte2)
193 newip->IP.IPV4.byte2++;
196 newip->IP.IPV4.byte2=ipStart->IP.IPV4.byte2;
197 while(newip->IP.IPV4.byte1<ipEnd->IP.IPV4.byte1)
199 newip->IP.IPV4.byte1++;
203 else if(newip->type == 6)
205#pragma message(__LOC__"fn_NetSim_IP_VPN_GetIPFromServer needs to be implemented for IPV6")
212_declspec(dllexport)
int fn_NetSim_IP_VPN_Run()
214 switch(pstruEventDetails->nEventType)
216 case NETWORK_OUT_EVENT:
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;
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)
237 packet->pstruNetworkData->szSourceIP=vpn->InternetIP[0];
238 for(i=0;i<=vpn->nConnectedDeviceCount;i++)
240 if(!IP_COMPARE(vpn->LocalIP[i],packet->pstruNetworkData->szDestIP))
242 packet->pstruNetworkData->szDestIP=vpn->InternetIP[i];
250 vpnPacket->
sourceIP=vpn_getVirtualIp(pstruEventDetails->nDeviceId);
251 packet->pstruNetworkData->szDestIP=devVar->serverIP;
252 packet->pstruNetworkData->szSourceIP=fn_NetSim_Stack_GetFirstIPAddressAsId(pstruEventDetails->nDeviceId,0);
256 fnNetSimError(
"Unknown Vpn status");
259 packet->pstruNetworkData->szGatewayIP=packet->pstruNetworkData->szSourceIP;
260 packet->pstruNetworkData->szNextHopIp=NULL;
261 pstruEventDetails->nInterfaceId=0;
262 fnpAddEvent(pstruEventDetails);
263 pstruEventDetails->pPacket=NULL;
266 case NETWORK_IN_EVENT:
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]))
276 packet->pstruNetworkData->szSourceIP=vpnPacket->
sourceIP;
277 packet->pstruNetworkData->szDestIP=vpnPacket->
destIP;
278 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket->
ipVar;
279 packet->pstruNetworkData->nPacketFlag=0;
281 else if(devVar->nVPNStatus == VPN_CLIENT && !IP_COMPARE(packet->pstruNetworkData->szSourceIP,devVar->serverIP))
285 packet->pstruNetworkData->szDestIP=getVirtualIP(pstruEventDetails->nDeviceId);
286 packet->pstruNetworkData->Packet_NetworkProtocol=vpnPacket->
ipVar;
287 packet->pstruNetworkData->nPacketFlag=0;
293 fnNetSimError(
"Unknown event type for VPN");
301NETSIM_IPAddress fn_NetSim_IP_FindWANInterfaceIP(NETSIM_ID ndeviceId)
304 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
306 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_WAN_ROUTER)
308 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
311 return fn_NetSim_Stack_GetFirstIPAddressAsId(ndeviceId,0);
313NETSIM_IPAddress getVirtualIP(NETSIM_ID ndeviceId)
316 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
318 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_VIRTUAL)
320 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
325int vpn_addtable(ptrIP_WRAPPER wrapper,NETSIM_ID serverId,NETSIM_IPAddress virtualIp,NETSIM_ID virtualInterface)
328 for(i=0;i<NETWORK->ppstruDeviceList[serverId-1]->nNumOfInterface;i++)
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)
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");
344NETSIM_IPAddress vpn_getVirtualIp(NETSIM_ID ndeviceId)
347 for(i=0;i<NETWORK->ppstruDeviceList[ndeviceId-1]->nNumOfInterface;i++)
349 if(DEVICE_INTERFACE(ndeviceId,i+1)->nInterfaceType==INTERFACE_VIRTUAL)
350 return DEVICE_INTERFACE(ndeviceId,i+1)->szAddress;
361 free(vpn->InternetIP);
370int freeVPNPacket(VPN_PACKET* vpnPacket)
378void* copyVPNPacket(VPN_PACKET* vpnPacket)
380 VPN_PACKET* newVpn=calloc(1,
sizeof* newVpn);
381 memcpy(newVpn,vpnPacket,
sizeof* newVpn);
382 return (
void*)newVpn;
void * ipVar
Original IP header.
unsigned int nControlPacketType
Original control packet type.
NETSIM_IPAddress sourceIP
Original source.
PACKET_TYPE nPacketType
Original packet type.
NETSIM_IPAddress destIP
Original destination.