NetSim Source Code Help
Loading...
Searching...
No Matches
NAT.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
17{
18 NetSim_PACKET_NETWORKLAYER* nw = calloc(1, sizeof* nw);
19
20 memcpy(nw, packet->pstruNetworkData, sizeof* nw);
21 nw->next = packet->pstruNetworkData;
22 packet->pstruNetworkData = nw;
23
24 nw->szDestIP = ip;
25 nw->Packet_NetworkProtocol = NULL;
26 nw->Packet_RoutingProtocol = NULL;
27}
28
29static void remove_dest(NetSim_PACKET* packet)
30{
33 if (nnw)
34 {
35 void* n = nnw->next;
36 void* i = nnw->szDestIP;
37
38 memcpy(nnw, nw, sizeof* nnw);
39 nnw->next = n;
40 nnw->szDestIP = i;
41 packet->pstruNetworkData = nnw;
42 nw->next = NULL;
43 free(nw);
44 }
45}
46
48{
50 NETSIM_ID i;
51 NETSIM_IPAddress newDest = NULL;
52
53 UINT destCount;
54 NETSIM_ID* destList = get_dest_from_packet(packet, &destCount);
55
56 if(destCount>1)
57 return 1; //Broadcast or Multicast
58
59 if (destList[0] == 0)
60 return 1; //Broadcast
61
62 if (isMulticastIP(dest))
63 return 1;
64
65 for(i=0;i<DEVICE(ndev)->nNumOfInterface;i++)
66 {
67 if(DEVICE_INTERFACE(ndev,i+1) && DEVICE_INTERFACE(ndev,i+1)->szAddress)
68 {
69 NETSIM_IPAddress ip = DEVICE_INTERFACE(ndev,i+1)->szAddress;
70 NETSIM_IPAddress mask = DEVICE_INTERFACE(ndev,i+1)->szSubnetMask;
71 unsigned int prefix = DEVICE_INTERFACE(ndev,i+1)->prefix_len;
72 NETSIM_IPAddress n1,n2;
73 if(dest->type != ip->type)
74 continue;
75 n1=IP_NETWORK_ADDRESS(ip,mask,prefix);
76 n2=IP_NETWORK_ADDRESS(dest,mask,prefix);
77 if(!IP_COMPARE(n1,n2))
78 return 2;
79 }
80 }
81 for(i=0;i<DEVICE(ndev)->nNumOfInterface;i++)
82 {
83 if(DEVICE_INTERFACE(ndev,i+1) && DEVICE_INTERFACE(ndev,i+1)->szAddress && DEVICE_INTERFACE(ndev,i+1)->szDefaultGateWay)
84 {
85 newDest = DEVICE_INTERFACE(ndev,i+1)->szDefaultGateWay;
86 break;
87 }
88 }
89
90 if (newDest)
91 {
92 //Set me to default
93 add_new_dest(packet, newDest);
94 }
95 else if (DEVICE_FIRST_PUBLICIP(destList[0]))
96 {
97 NETSIM_IPAddress pub = DEVICE_FIRST_PUBLICIP(destList[0]);
98 if (!IP_COMPARE(pub, dest))
99 return -1; // Already public ip is set
100
101 for (i = 0; i < DEVICE(ndev)->nNumOfInterface; i++)
102 {
103 if (DEVICE_INTERFACE(ndev, i + 1) && DEVICE_INTERFACE(ndev, i + 1)->szAddress)
104 {
105 if (!IP_COMPARE(DEVICE_NWADDRESS(ndev, i + 1), pub))
106 return -1; // I am public ip of dest
107 }
108 }
109
110 //set me to public ip
111 add_new_dest(packet, DEVICE_FIRST_PUBLICIP(destList[0]));
112 }
113 return 0;
114}
115
117{
118 UINT destCount;
119 NETSIM_ID* dest = get_dest_from_packet(packet, &destCount);
120 if (destCount > 1)
121 return -1; //Broadcast or Multicast
122
123 if (ndev == dest[0])
124 {
125 remove_dest(packet);
126 return -2;
127 }
128
130 return -3;
131
133 return -4;
134
135 NETSIM_ID i;
137 bool flag = false;
138 for (i = 0; i < DEVICE(ndev)->nNumOfInterface; i++)
139 {
140 if (DEVICE_INTERFACE(ndev, i + 1) &&
141 DEVICE_INTERFACE(ndev, i + 1)->szAddress &&
142 !IP_COMPARE(ip, DEVICE_NWADDRESS(ndev, i + 1)))
143 {
144 flag = true;
145 break;
146 }
147 }
148
149 if (flag)
150 remove_dest(packet);
151
152 return 0;
153}
unsigned int NETSIM_ID
Definition: Animation.h:45
bool isBroadcastIP(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
bool isMulticastIP(NETSIM_IPAddress ip)
#define UINT
Definition: Linux.h:38
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
int fn_NetSim_NAT_NetworkIn(NETSIM_ID ndev, NetSim_PACKET *packet)
Definition: NAT.c:116
static void remove_dest(NetSim_PACKET *packet)
Definition: NAT.c:29
static void add_new_dest(NetSim_PACKET *packet, NETSIM_IPAddress ip)
Definition: NAT.c:16
int fn_NetSim_NAT_NetworkOut(NETSIM_ID ndev, NetSim_PACKET *packet)
Definition: NAT.c:47
NETSIM_ID * get_dest_from_packet(NetSim_PACKET *packet, UINT *count)
#define DEVICE(DeviceId)
Definition: Stack.h:769
#define DEVICE_FIRST_PUBLICIP(DeviceId)
Definition: Stack.h:810
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
struct stru_NetSim_Packet_NetworkLayer * next
Definition: Packet.h:209
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275