NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
IP.h
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#ifndef _NETSIM_IP_H_
25#define _NETSIM_IP_H_
26#ifdef __cplusplus
27extern "C" {
28#endif
29#ifndef _NETSIM_IP_LIB_
30#pragma comment(lib,"IP.lib")
31#endif
32
33//#define PRINT_MATCH_TABLE
34
35#define IP_IGMP_STATUS_DEFAULT false
36#define IP_PIM_STATUS_DEFAULT false
37#define IP_ICMP_STATUS_DEFAULT false
38#define IP_STATIC_IP_ROUTE_DEFAULT _strdup("")
39#define IP_PROCESSING_DELAY_DEFAULT 0 //µs
40
41#define on_link NULL
42#define DEFAULT_METRIC 999
43#define ONLINK_METRIC 300
44#define MULTICAST_METRIC 306
45#define IPV4_HEADER_SIZE 20
46#define PROTOCOL_VPN 1
47#define VPN_METRIC 200
48
49#define GET_IP_DEVVAR(d) (DEVICE_NWLAYER(d)?(IP_DEVVAR*)(DEVICE_NWLAYER(d)->ipVar):NULL)
50
51 typedef struct stru_IP_DevVar IP_DEVVAR;
52 /// Enumeration for routing types
53 typedef enum
54 {
55 RoutingType_string,
56 RoutingType_DEFAULT=1,
57 RoutingType_STATIC,
58 }ROUTING_TYPE;
59
60 /// Enumeration for IP control packets
61 typedef enum
62 {
63 //ICMP Packet
64 PACKET_ICMP_DstUnreachableMsg=NW_PROTOCOL_IPV4*100+10,
65 PACKET_ICMP_ECHORequest,
66 PACKET_ICMP_ECHOReply,
67 PACKET_ROUTER_ADVERTISEMENT,
68
69 //VPN
70 PACKET_VPN = NW_PROTOCOL_IPV4*100+20,
71
72 //IGMP Packet
73 PACKET_IGMP_QUERY = NW_PROTOCOL_IPV4*100+30,
74 PACKET_IGMP_REPORT,
75 PACKET_IGMP_LEAVE,
76
77 //PIM Packet
78 PACKET_PIM_HELLO = NW_PROTOCOL_IPV4*100+40,
79 PACKET_PIM_REGISTER,
80 PACKET_PIM_REGISTERSTOP,
81 PACKET_PIM_JOINPRUNE,
82 PACKET_PIM_BOOTSTRAP,
83 PACKET_PIM_ASSERT,
84 PACKET_PIM_GRAFT,
85 PACKET_PIM_GRAFTACK,
86 PACKET_PIM_CANDRPADVER,
87 }IP_CONTROL_PACKET;
88#define isPIMPacket(packet) (packet->pstruNetworkData->IPProtocol == IPPROTOCOL_PIM)
89
90 /// Enumeration for ip sub-events.
91 typedef enum
92 {
93 //ICMP
94 EVENT_ICMP_POLL=NW_PROTOCOL_IPV4*100+1,
95 EVENT_ADVERTISE_ROUTER,
96
97 //IGMP
98 EVENT_IGMP_Unsolicited_report,
99 EVENT_IGMP_SendStartupQuery,
100 EVENT_IGMP_SendQuery,
101 EVENT_IGMP_OtherQuerierPresentTimer,
102 EVENT_IGMP_DelayTimer,
103 EVENT_IGMP_GroupMembershipTimer,
104
105 //PIM
106 EVENT_PIM_SEND_HELLO,
107 EVENT_PIM_NEIGHBOR_TIMEOUT,
108 EVENT_PIM_JT,
109 EVENT_PIM_ET,
110
111 EVENT_IP_INIT_TABLE,
112
113 //PING
114 EVENT_ICMP_SEND_ECHO,
115
116 //Proceesing delay
117 EVENT_IP_PROCESSING_DELAY,
118 }IP_SUBEVENT;
119
120 //http://www.cisco.com/en/US/docs/net_mgmt/ciscoworks_ip_communications_operations_manager/1.0/user/guide/SNMPInfo.html#wp1024287
121 /// Enumeration for IP gateway states.
122 typedef enum
123 {
124 GATEWAYSTATE_UP,
125 GATEWAYSTATE_DOWN,
126 GATEWAYSTATE_NOTIFICATION_PENDING,
127 GATEWAYSTATE_CLEARANCE_PENDING,
128 }IP_GATEWAYSTATE;
129 /// Enumeration for VPN states.
130 typedef enum
131 {
132 VPN_DISABLE=0,
133 VPN_SERVER,
134 VPN_CLIENT,
135 }VPN_STATE;
136
137 typedef enum
138 {
139 ACTION_DROP,
140 ACTION_MOVEUP,
141 ACTION_REROUTE,
142 }IP_PROTOCOL_ACTION;
143
144 typedef struct ForwardRoute
145 {
146 UINT count;
147 NETSIM_IPAddress* nextHop;
148 NETSIM_IPAddress* gateway;
149 NETSIM_ID* interfaceId;
150 NETSIM_ID* nextHopId;
151 struct ForwardRoute* next;
152 }IP_FORWARD_ROUTE,*ptrIP_FORWARD_ROUTE;
153 void free_ip_route(ptrIP_FORWARD_ROUTE route);
154
155/// Structure to store ip routing table
157{
158 NETSIM_IPAddress networkDestination;
159 NETSIM_IPAddress netMask;
160 NETSIM_IPAddress gateway;
161 UINT interfaceCount;
162 NETSIM_IPAddress* Interface;
163 unsigned int prefix_len;
164 unsigned int Metric;
165 ROUTING_TYPE type;
166 char* szType;
167 _ele* ele;
168 double update_time;
169 //NetSim specific
170 NETSIM_ID* nInterfaceId;
171 NETSIM_ID nGatewayId;
172}IP_ROUTINGTABLE, *ptrIP_ROUTINGTABLE,**pptrIP_ROUTINGTABLE;
173/*
1743.1. Internet Header Format
175
176 A summary of the contents of the internet header follows:
177
178
179 0 1 2 3
180 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
181 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182 |Version| IHL |Type of Service| Total Length |
183 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184 | Identification |Flags| Fragment Offset |
185 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186 | Time to Live | Protocol | Header Checksum |
187 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
188 | Source Address |
189 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
190 | Destination Address |
191 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
192 | Options | Padding |
193 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194
195 Example Internet Datagram Header
196
197 Figure 4.
198*/
199/*typedef struct stru_IP_Option
200{
201 unsigned int optType;
202 unsigned int optLen;
203 void* option;
204 struct stru_IP_Option* next;
205}IP_OPTION;
206struct stru_NetSim_IPHeader
207{
208 unsigned int version:4;
209 unsigned int IHL:4;
210 unsigned int TypeofService:8;
211 unsigned int TotalLength:16;
212 unsigned int Identification:16;
213 unsigned int flags:3;
214 unsigned int FragmentOffset:13;
215 unsigned int TimetoLive:8;
216 unsigned int Protocol:8;
217 unsigned int HeaderChecksum:16;
218 NETSIM_IPAddress SourceAddress;
219 NETSIM_IPAddress DestinationAddess;
220 IP_OPTION* options;
221 unsigned int Padding;
222};*/
223
224/// Structure to store the device ip details
226{
227 //Static IP table file
228 char* staticIPTableFile;
229
230 //Firewall variable
231 void* ACL;
232 bool isFirewallConfigured;
233 char* firewallConfig;
234
235 //ICMP Variable
236 bool isICMP;
237 //Router Advertisement
238 unsigned int nRouterAdvertisementFlag;
239 unsigned int nRouterAdverMinInterval;
240 unsigned int nRouterAdverMaxInterval;
241 unsigned int nRouterAdverLifeTime;
242 //ICMP POLL
243 unsigned int nICMPPollingTime;
244 unsigned int nGatewayCount;
245 NETSIM_IPAddress* GatewayIPAddress;
246 NETSIM_ID* nGatewayId;
247 NETSIM_ID* nInterfaceId;
248 IP_GATEWAYSTATE* nGatewayState;
249
250 //VPN variable
251 VPN_STATE nVPNStatus; /* 0--Disable
252 1--Server
253 2--Client */
254 //Client variable
255 NETSIM_IPAddress serverIP;
256 //Server variable
257 NETSIM_IPAddress ipPoolStart;
258 NETSIM_IPAddress ipPoolEnd;
259 NETSIM_IPAddress ipPoolMask;
260 void* vpn;
261 void* dnsList;
262
263 //DHCP
264 void* dhcp;
265
266 //Multicast
267 void* multicast;
268
269 //IGMP
270 bool isIGMPConfigured;
271 void* igmp;
272
273 //PIM
274 bool isPIMConfigured;
275 void* pim;
276
277 //Processing delay
278 double processingDelay;
279};
280/// Structure to store the IP metrics.
282{
283 NETSIM_ID nDeviceId;
284 unsigned int nPacketSent;
285 unsigned int nPacketReceived;
286 unsigned int nPacketForwarded;
287 unsigned int nPacketDiscarded;
288 unsigned int nFirewallBlocked;
289 unsigned int nTTLDrop;
290};
291struct stru_IP_Metrics** ipMetrics;
292/// Data structure for dns.
293typedef struct stru_dnsList
294{
295 NETSIM_ID deviceId;
296 NETSIM_IPAddress ip;
297 _ele* ele;
298}DNS;
299#define DNS_ALLOC() (struct stru_dnsList*)list_alloc(sizeof(struct stru_dnsList),offsetof(struct stru_dnsList,ele))
300
301#define IPROUTINGTABLE_ALLOC() (ptrIP_ROUTINGTABLE)list_alloc(sizeof(IP_ROUTINGTABLE),offsetof(IP_ROUTINGTABLE,ele))
302#define IPROUTINGTABLE_ADD(table,current,fun) list_add(((void**)(table)),current,offsetof(IP_ROUTINGTABLE,ele),fun)
303
304
305//Lib function
306int iptable_add_check(ptrIP_ROUTINGTABLE current,ptrIP_ROUTINGTABLE mem);
307ptrIP_ROUTINGTABLE iptable_check(ptrIP_ROUTINGTABLE* table,NETSIM_IPAddress dest,NETSIM_IPAddress subnet);
308int iptable_change(ptrIP_WRAPPER wrapper,
309 NETSIM_IPAddress dest,
310 NETSIM_IPAddress subnet,
311 UINT prefix_len,
312 NETSIM_IPAddress gateway,
313 UINT interfaceCount,
314 NETSIM_IPAddress* interfaceIp,
315 NETSIM_ID* interfaceId,
316 unsigned int metric);
317int iptable_change_byRoute(ptrIP_WRAPPER wrapper,
318 ptrIP_ROUTINGTABLE route,
319 NETSIM_IPAddress subnet,
320 unsigned int prefix_len,
321 NETSIM_IPAddress gateway,
322 UINT interfaceCount,
323 NETSIM_IPAddress* interfaceIp,
324 NETSIM_ID* interfaceId,
325 unsigned int metric);
326int iptable_delete(ptrIP_WRAPPER wrapper,
327 NETSIM_IPAddress dest,
328 NETSIM_IPAddress in,
329 char* type /* NULL will delete all */);
330int iptable_delete_by_type(ptrIP_WRAPPER wrapper,
331 char* type);
332int iptable_delete_by_route(ptrIP_WRAPPER wrapper,
333 ptrIP_ROUTINGTABLE route);
334ptrIP_ROUTINGTABLE iptable_add(ptrIP_WRAPPER wrapper,
335 NETSIM_IPAddress dest,
336 NETSIM_IPAddress subnet,
337 unsigned int prefix_len,
338 NETSIM_IPAddress gateway,
339 UINT interfaceCount,
340 NETSIM_IPAddress* interfaceIp,
341 NETSIM_ID* interfaceId,
342 unsigned int metric,
343 char* type);
344int iptable_print(FILE* fp, ptrIP_ROUTINGTABLE routeTable);
345ptrIP_ROUTINGTABLE* iptable_get_table_by_type(ptrIP_ROUTINGTABLE table, char* type, UINT* count);
346
347_declspec(dllexport) NETSIM_IPAddress dns_query(NETSIM_ID nDeviceId,NETSIM_ID id);
348
349//VPN
350_declspec(dllexport) int fn_NetSim_IP_VPN_Run();
351
352//IP Routing
353ptrIP_FORWARD_ROUTE fn_NetSim_IP_RoutePacket(NetSim_PACKET* packet,
354 NETSIM_ID dev);
355ptrIP_FORWARD_ROUTE fn_NetSim_IP_RoutePacketViaStaticEntry(NetSim_PACKET* packet, NETSIM_ID dev);
356void pass_to_lower_layer(NetSim_PACKET* packet, ptrIP_FORWARD_ROUTE route, UINT c);
357
358//NAT
359int fn_NetSim_NAT_NetworkOut(NETSIM_ID ndev,NetSim_PACKET* packet);
360int fn_NetSim_NAT_NetworkIn(NETSIM_ID ndev,NetSim_PACKET* packet);
361
362//ICMP
363void ICMP_copyPacket(NetSim_PACKET* d, NetSim_PACKET* s);
364void process_icmp_packet();
365_declspec(dllexport) int ICMP_CHECKSTATE(NETSIM_IPAddress ip);
366
367//Multicast
368void multicast_join_group();
369IP_PROTOCOL_ACTION check_ip_in_multicastgroup(NETSIM_IPAddress ip, NETSIM_ID d, NetSim_PACKET* packet);
370bool is_reserved_multicast_address(NETSIM_IPAddress ip);
371bool isCorrectRoute(pptrIP_ROUTINGTABLE table, NETSIM_IPAddress dest, NETSIM_IPAddress src);
372
373//IGMP
374void igmp_configure(NETSIM_ID d, void* xmlNode);
375void igmp_init(NETSIM_ID d);
376void igmp_host_join_group(NETSIM_ID d, NETSIM_IPAddress group);
377void host_handle_unsolicited_report_timer();
378void process_igmp_packet();
379void IGMP_FreePacket(NetSim_PACKET* packet);
380IP_PROTOCOL_ACTION host_is_ip_present_in_db(NETSIM_ID d, NETSIM_IPAddress ip, NetSim_PACKET* packet);
381IP_PROTOCOL_ACTION router_is_ip_present_in_db(NETSIM_ID d, NETSIM_IPAddress ip, NetSim_PACKET* packet);
382void send_query_msg(NETSIM_ID d, NETSIM_IPAddress groupAddr, double time);
383void igmp_router_processOtherQuerierPresentTime();
384void host_send_report();
385void igmp_router_ProcessGroupMembershipTimer();
386void copy_igmp_packet(NetSim_PACKET* d, NetSim_PACKET* s);
387void igmp_free(NETSIM_ID d);
388
389//PIM
390void pim_configure(NETSIM_ID d, void* xmlNetSimNode);
391void Router_PIM_Init(NETSIM_ID d);
392void pim_handle_timer_event();
393void pim_join_group(NETSIM_ID d, NETSIM_IPAddress group);
394IP_PROTOCOL_ACTION pim_decide_action(NetSim_PACKET* packet, NETSIM_ID d);
395void process_pim_packet();
396
397//PCAP writer
398void ip_write_to_pcap(NetSim_PACKET* packet,
399 NETSIM_ID d,
400 NETSIM_ID i,
401 double time);
402
403//Ping
404_declspec(dllexport) void* ICMP_StartPingRequest(NETSIM_ID src,
405 NETSIM_ID dest,
406 UINT count,
407 bool(*resp)(void*, char*, bool),
408 void* arg);
409void icmp_send_echo_request();
410
411#ifdef __cplusplus
412}
413#endif
414#endif
Structure to store the device ip details.
Definition IP.h:226
Structure to store the IP metrics.
Definition IP.h:282
Structure to store ip routing table.
Definition IP.h:157
Data structure for dns.
Definition IP.h:294