NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
IP_Utility.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 "List.h"
26#include "IP.h"
27
28static bool isPublicIPReqd(NETSIM_ID d)
29{
30 NETSIM_ID i;
31 bool ret = false;
32 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
33 {
34 if (DEVICE_INTERFACE(d, i + 1) &&
35 DEVICE_INTERFACE(d, i + 1)->nInterfaceType == INTERFACE_WAN_ROUTER)
36 return false;
37
38 if (DEVICE_INTERFACE(d, i + 1) &&
39 DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay)
40 ret = true;
41 }
42 return ret;
43}
44
45static NETSIM_IPAddress find_default_gateway(NETSIM_ID d,NETSIM_ID* di)
46{
47 NETSIM_ID i;
48 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
49 {
50 if (DEVICE_INTERFACE(d, i + 1) &&
51 DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay)
52 {
53 *di = i + 1;
54 return DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay;
55 }
56 }
57 *di = 0;
58 return NULL;
59}
60
61static NETSIM_IPAddress find_wan_ip(NETSIM_ID d, NETSIM_ID* wi)
62{
63 NETSIM_ID i;
64 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
65 {
66 if (DEVICE_INTERFACE(d, i + 1) &&
67 DEVICE_INTERFACE(d, i + 1)->nInterfaceType == INTERFACE_WAN_ROUTER)
68 {
69 *wi = i + 1;
70 return DEVICE_INTERFACE(d, i + 1)->szAddress;
71 }
72 }
73 *wi = 0;
74 return NULL;
75}
76
77static void assign_public_ip(NETSIM_ID d, NETSIM_IPAddress ip)
78{
79 NETSIM_ID i;
80 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
81 {
82 if (DEVICE_INTERFACE(d, i + 1) &&
83 DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay)
84 {
85 DEVICE_PUBLICIP(d, i + 1) = ip;
86 fprintf(stdout, "Public IP of %d-%d is %s\n",
87 d, i + 1, ip->str_ip);
88 }
89 }
90}
91
92typedef struct stru_ip_list
93{
94 NETSIM_IPAddress ip;
95 NETSIM_IPAddress subnet;
96 UINT prefix_len;
97 struct stru_ip_list* next;
98}IPLIST, * ptrIPLIST;
99
100static void add_route(NETSIM_ID d, ptrIPLIST list,
101 NETSIM_IPAddress interfaceAddress,NETSIM_IPAddress nextHop)
102{
103 NETSIM_ID di = fn_NetSim_Stack_GetInterfaceIdFromIP(d, interfaceAddress);
104 while (list)
105 {
106 NETSIM_IPAddress dest = IP_NETWORK_ADDRESS(list->ip, list->subnet,list->prefix_len);
107
108 iptable_add(IP_WRAPPER_GET(d),
109 dest, list->subnet, list->prefix_len,
110 nextHop, 1, &interfaceAddress, &di, 1, "IP");
111 list = list->next;
112 }
113}
114
115static void setup_route_table_for_all_network(NETSIM_ID d, NETSIM_ID w, NETSIM_ID wi,
116 ptrIPLIST ipList)
117{
118 NETSIM_ID c = 0;
119 NETSIM_ID ci = 0;
120 NETSIM_ID di = 0;
121
122 while (true)
123 {
124 NETSIM_IPAddress defaultGateway = find_default_gateway(d, &di);
125 c = fn_NetSim_Stack_GetDeviceId_asIP(defaultGateway, &ci);
126 add_route(c, ipList, defaultGateway, DEVICE_NWADDRESS(d, di));
127
128 if (c == w)
129 break;
130
131 d = c;
132 }
133}
134
135static void setup_route_table(NETSIM_ID d, NETSIM_ID w, NETSIM_ID wi)
136{
137 NETSIM_IPAddress pubIP = DEVICE_NWADDRESS(w, wi);
138
139 ptrIPLIST head = NULL;
140 ptrIPLIST tail = NULL;
141 NETSIM_IPAddress defaultGateway = NULL;
142 NETSIM_ID i;
143
144 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
145 {
146 if (DEVICE_INTERFACE(d, i + 1) &&
147 DEVICE_NWADDRESS(d, i + 1))
148 {
149 if (DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay && !defaultGateway)
150 {
151 defaultGateway = DEVICE_INTERFACE(d, i + 1)->szDefaultGateWay;
152 }
153 else
154 {
155 ptrIPLIST l = calloc(1, sizeof * l);
156 l->ip = DEVICE_INTERFACE(d, i + 1)->szAddress;
157 l->subnet = DEVICE_INTERFACE(d, i + 1)->szSubnetMask;
158 l->prefix_len = DEVICE_INTERFACE(d, i + 1)->prefix_len;;
159
160 if (head)
161 {
162 tail->next = l;
163 tail = l;
164 }
165 else
166 {
167 head = l;
168 tail = l;
169 }
170 }
171 }
172 }
173
174 if(head)
175 setup_route_table_for_all_network(d, w, wi, head);
176
177 while (head)
178 {
179 ptrIPLIST t = head;
180 head = head->next;
181 free(t);
182 }
183}
184
185void set_public_ip(NETSIM_ID d)
186{
187 if (!isPublicIPReqd(d))
188 return;
189
190 NETSIM_ID c = d;
191 NETSIM_ID ci = 0;
192 while (true)
193 {
194 NETSIM_IPAddress defaultGateway = find_default_gateway(c, &ci);
195 if (!defaultGateway)
196 break;
197
198 c = fn_NetSim_Stack_GetDeviceId_asIP(defaultGateway, &ci);
199
200 NETSIM_IPAddress wanIP = find_wan_ip(c, &ci);
201
202 if (wanIP)
203 {
204 assign_public_ip(d, wanIP);
205 c = fn_NetSim_Stack_GetDeviceId_asIP(wanIP, &ci);
206 setup_route_table(d, c, ci);
207 break;
208 }
209 }
210
211}