NetSim Source Code Help
Loading...
Searching...
No Matches
IP_Utility.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2021 *
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
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
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
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
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{
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
109 dest, list->subnet, list->prefix_len,
110 nextHop, 1, &interfaceAddress, &di, 1, "IP");
111 list = list->next;
112 }
113}
114
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
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
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);
206 setup_route_table(d, c, ci);
207 break;
208 }
209 }
210
211}
unsigned int NETSIM_ID
Definition: Animation.h:45
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)
NETSIM_IPAddress IP_NETWORK_ADDRESS(NETSIM_IPAddress ip, NETSIM_IPAddress subnet, unsigned int prefix_len)
static void setup_route_table_for_all_network(NETSIM_ID d, NETSIM_ID w, NETSIM_ID wi, ptrIPLIST ipList)
Definition: IP_Utility.c:115
struct stru_ip_list * ptrIPLIST
struct stru_ip_list IPLIST
static void add_route(NETSIM_ID d, ptrIPLIST list, NETSIM_IPAddress interfaceAddress, NETSIM_IPAddress nextHop)
Definition: IP_Utility.c:100
static NETSIM_IPAddress find_default_gateway(NETSIM_ID d, NETSIM_ID *di)
Definition: IP_Utility.c:45
static NETSIM_IPAddress find_wan_ip(NETSIM_ID d, NETSIM_ID *wi)
Definition: IP_Utility.c:61
static void setup_route_table(NETSIM_ID d, NETSIM_ID w, NETSIM_ID wi)
Definition: IP_Utility.c:135
static void assign_public_ip(NETSIM_ID d, NETSIM_IPAddress ip)
Definition: IP_Utility.c:77
static bool isPublicIPReqd(NETSIM_ID d)
Definition: IP_Utility.c:28
void set_public_ip(NETSIM_ID d)
Definition: IP_Utility.c:185
#define c
#define UINT
Definition: Linux.h:38
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
#define DEVICE(DeviceId)
Definition: Stack.h:769
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
NETSIM_ID fn_NetSim_Stack_GetInterfaceIdFromIP(NETSIM_ID nDeviceId, NETSIM_IPAddress ip)
#define DEVICE_PUBLICIP(DeviceId, InterfaceId)
Definition: Stack.h:809
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
@ 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)
UINT prefix_len
Definition: IP_Utility.c:96
NETSIM_IPAddress ip
Definition: IP_Utility.c:94
struct stru_ip_list * next
Definition: IP_Utility.c:97
NETSIM_IPAddress subnet
Definition: IP_Utility.c:95
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54