NetSim Source Code Help
Loading...
Searching...
No Matches
IP.h
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2022 *
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 //microsec
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 {
59
60 /// Enumeration for IP control packets
61 typedef enum
62 {
63 //ICMP Packet
68
69 //VPN
71
72 //IGMP Packet
76
77 //PIM Packet
88#define isPIMPacket(packet) (packet->pstruNetworkData->IPProtocol == IPPROTOCOL_PIM)
89
90 /// Enumeration for ip sub-events.
91 typedef enum
92 {
93 //ICMP
96
97 //IGMP
104
105 //PIM
110
112
113 //PING
115
116 //Proceesing delay
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 {
129 /// Enumeration for VPN states.
130 typedef enum
131 {
135 }VPN_STATE;
136
137 typedef enum
138 {
143
144 typedef struct ForwardRoute
145 {
154
155/// Structure to store ip routing table
157{
163 unsigned int prefix_len;
164 unsigned int Metric;
166 char* szType;
169 //NetSim specific
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
229
230 //Firewall variable
231 void* ACL;
234
235 //ICMP Variable
236 bool isICMP;
237 //Router Advertisement
242 //ICMP POLL
243 unsigned int nICMPPollingTime;
244 unsigned int nGatewayCount;
249
250 //VPN variable
251 VPN_STATE nVPNStatus; /* 0--Disable
252 1--Server
253 2--Client */
254 //Client variable
256 //Server variable
260 void* vpn;
261 void* dnsList;
262
263 //DHCP
264 void* dhcp;
265
266 //Multicast
268
269 //IGMP
271 void* igmp;
272
273 //PIM
275 void* pim;
276
277 //Processing delay
279};
280/// Structure to store the IP metrics.
282{
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};
292/// Data structure for dns.
293typedef struct stru_dnsList
294{
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
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);
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);
327 NETSIM_IPAddress dest,
329 char* type /* NULL will delete all */);
331 char* type);
333 ptrIP_ROUTINGTABLE route);
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);
346
348
349//VPN
350_declspec(dllexport) int fn_NetSim_IP_VPN_Run();
351
352//IP Routing
354 NETSIM_ID dev);
357
358//NAT
361
362//ICMP
366
367//Multicast
372
373//IGMP
374void igmp_configure(NETSIM_ID d, void* xmlNode);
375void igmp_init(NETSIM_ID d);
379void IGMP_FreePacket(NetSim_PACKET* packet);
382void send_query_msg(NETSIM_ID d, NETSIM_IPAddress groupAddr, double time);
384void host_send_report();
387void igmp_free(NETSIM_ID d);
388
389//PIM
390void pim_configure(NETSIM_ID d, void* xmlNetSimNode);
395void process_pim_packet();
396
397//PCAP writer
399 NETSIM_ID d,
400 NETSIM_ID i,
401 double time);
402
403//Ping
405 NETSIM_ID dest,
406 UINT count,
407 bool(*resp)(void*, char*, bool),
408 void* arg);
410
411#ifdef __cplusplus
412}
413#endif
414#endif
unsigned int NETSIM_ID
Definition: Animation.h:45
GROUP_MOBILITY * group
Definition: GroupMobility.c:30
void process_icmp_packet()
Definition: ICMP.c:533
void host_send_report()
Definition: IGMP_Host.c:119
IP_PROTOCOL_ACTION pim_decide_action(NetSim_PACKET *packet, NETSIM_ID d)
Definition: PIM_SM.c:139
int iptable_change_byRoute(ptrIP_WRAPPER wrapper, ptrIP_ROUTINGTABLE route, NETSIM_IPAddress subnet, unsigned int prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric)
IP_GATEWAYSTATE
Enumeration for IP gateway states.
Definition: IP.h:123
@ GATEWAYSTATE_CLEARANCE_PENDING
Definition: IP.h:127
@ GATEWAYSTATE_NOTIFICATION_PENDING
Definition: IP.h:126
@ GATEWAYSTATE_UP
Definition: IP.h:124
@ GATEWAYSTATE_DOWN
Definition: IP.h:125
void pim_join_group(NETSIM_ID d, NETSIM_IPAddress group)
Definition: PIM_SM.c:228
void igmp_host_join_group(NETSIM_ID d, NETSIM_IPAddress group)
Definition: IGMP_Host.c:159
struct stru_NetSim_IPRoutingTable ** pptrIP_ROUTINGTABLE
IP_PROTOCOL_ACTION
Definition: IP.h:138
@ ACTION_DROP
Definition: IP.h:139
@ ACTION_MOVEUP
Definition: IP.h:140
@ ACTION_REROUTE
Definition: IP.h:141
int ICMP_CHECKSTATE(NETSIM_IPAddress ip)
void ICMP_copyPacket(NetSim_PACKET *d, NetSim_PACKET *s)
Definition: ICMP.c:518
void pim_configure(NETSIM_ID d, void *xmlNetSimNode)
Definition: PIM_SM.c:107
int fn_NetSim_NAT_NetworkIn(NETSIM_ID ndev, NetSim_PACKET *packet)
Definition: NAT.c:116
IP_PROTOCOL_ACTION router_is_ip_present_in_db(NETSIM_ID d, NETSIM_IPAddress ip, NetSim_PACKET *packet)
Definition: IGMP_Router.c:180
ptrIP_ROUTINGTABLE iptable_check(ptrIP_ROUTINGTABLE *table, NETSIM_IPAddress dest, NETSIM_IPAddress subnet)
struct stru_NetSim_IPRoutingTable * ptrIP_ROUTINGTABLE
int iptable_delete_by_route(ptrIP_WRAPPER wrapper, ptrIP_ROUTINGTABLE route)
IP_PROTOCOL_ACTION check_ip_in_multicastgroup(NETSIM_IPAddress ip, NETSIM_ID d, NetSim_PACKET *packet)
ptrIP_FORWARD_ROUTE fn_NetSim_IP_RoutePacketViaStaticEntry(NetSim_PACKET *packet, NETSIM_ID dev)
Definition: IP_Routing.c:558
int iptable_delete(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress in, char *type)
ptrIP_FORWARD_ROUTE fn_NetSim_IP_RoutePacket(NetSim_PACKET *packet, NETSIM_ID dev)
Definition: IP_Routing.c:371
void process_pim_packet()
Definition: PIM_SM.c:149
void free_ip_route(ptrIP_FORWARD_ROUTE route)
Definition: IP_Routing.c:157
struct stru_NetSim_IPRoutingTable IP_ROUTINGTABLE
Structure to store ip routing table.
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)
IP_SUBEVENT
Enumeration for ip sub-events.
Definition: IP.h:92
@ EVENT_PIM_JT
Definition: IP.h:108
@ EVENT_IGMP_SendQuery
Definition: IP.h:100
@ EVENT_PIM_NEIGHBOR_TIMEOUT
Definition: IP.h:107
@ EVENT_PIM_ET
Definition: IP.h:109
@ EVENT_ADVERTISE_ROUTER
Definition: IP.h:95
@ EVENT_IP_PROCESSING_DELAY
Definition: IP.h:117
@ EVENT_IGMP_OtherQuerierPresentTimer
Definition: IP.h:101
@ EVENT_IGMP_DelayTimer
Definition: IP.h:102
@ EVENT_ICMP_POLL
Definition: IP.h:94
@ EVENT_IGMP_SendStartupQuery
Definition: IP.h:99
@ EVENT_IGMP_GroupMembershipTimer
Definition: IP.h:103
@ EVENT_IGMP_Unsolicited_report
Definition: IP.h:98
@ EVENT_IP_INIT_TABLE
Definition: IP.h:111
@ EVENT_ICMP_SEND_ECHO
Definition: IP.h:114
@ EVENT_PIM_SEND_HELLO
Definition: IP.h:106
struct stru_IP_Metrics ** ipMetrics
Definition: IP.h:291
void igmp_configure(NETSIM_ID d, void *xmlNode)
Definition: IGMP.c:43
void Router_PIM_Init(NETSIM_ID d)
Definition: PIM_SM.c:67
VPN_STATE
Enumeration for VPN states.
Definition: IP.h:131
@ VPN_CLIENT
Definition: IP.h:134
@ VPN_SERVER
Definition: IP.h:133
@ VPN_DISABLE
Definition: IP.h:132
int iptable_change(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, UINT prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric)
void IGMP_FreePacket(NetSim_PACKET *packet)
Definition: IGMP_Msg.c:81
bool isCorrectRoute(pptrIP_ROUTINGTABLE table, NETSIM_IPAddress dest, NETSIM_IPAddress src)
ptrIP_ROUTINGTABLE * iptable_get_table_by_type(ptrIP_ROUTINGTABLE table, char *type, UINT *count)
void process_igmp_packet()
Definition: IGMP_Msg.c:195
int iptable_print(FILE *fp, ptrIP_ROUTINGTABLE routeTable)
void icmp_send_echo_request()
Definition: ICMP.c:668
void ip_write_to_pcap(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID i, double time)
Definition: IP.c:1476
struct ForwardRoute * ptrIP_FORWARD_ROUTE
ROUTING_TYPE
Enumeration for routing types.
Definition: IP.h:54
@ RoutingType_string
Definition: IP.h:55
@ RoutingType_DEFAULT
Definition: IP.h:56
@ RoutingType_STATIC
Definition: IP.h:57
void igmp_router_processOtherQuerierPresentTime()
Definition: IGMP_Router.c:239
void igmp_free(NETSIM_ID d)
Definition: IGMP.c:164
struct stru_dnsList DNS
Data structure for dns.
IP_CONTROL_PACKET
Enumeration for IP control packets.
Definition: IP.h:62
@ PACKET_IGMP_LEAVE
Definition: IP.h:75
@ PACKET_PIM_REGISTER
Definition: IP.h:79
@ PACKET_PIM_REGISTERSTOP
Definition: IP.h:80
@ PACKET_PIM_JOINPRUNE
Definition: IP.h:81
@ PACKET_ICMP_DstUnreachableMsg
Definition: IP.h:64
@ PACKET_PIM_GRAFTACK
Definition: IP.h:85
@ PACKET_IGMP_QUERY
Definition: IP.h:73
@ PACKET_PIM_GRAFT
Definition: IP.h:84
@ PACKET_PIM_CANDRPADVER
Definition: IP.h:86
@ PACKET_ICMP_ECHOReply
Definition: IP.h:66
@ PACKET_PIM_BOOTSTRAP
Definition: IP.h:82
@ PACKET_ROUTER_ADVERTISEMENT
Definition: IP.h:67
@ PACKET_PIM_ASSERT
Definition: IP.h:83
@ PACKET_PIM_HELLO
Definition: IP.h:78
@ PACKET_IGMP_REPORT
Definition: IP.h:74
@ PACKET_VPN
Definition: IP.h:70
@ PACKET_ICMP_ECHORequest
Definition: IP.h:65
void * ICMP_StartPingRequest(NETSIM_ID src, NETSIM_ID dest, UINT count, bool(*resp)(void *, char *, bool), void *arg)
void copy_igmp_packet(NetSim_PACKET *d, NetSim_PACKET *s)
Definition: IGMP_Msg.c:77
int fn_NetSim_IP_VPN_Run()
Definition: VPN.c:212
void igmp_init(NETSIM_ID d)
Definition: IGMP.c:141
void igmp_router_ProcessGroupMembershipTimer()
Definition: IGMP_Router.c:307
IP_PROTOCOL_ACTION host_is_ip_present_in_db(NETSIM_ID d, NETSIM_IPAddress ip, NetSim_PACKET *packet)
Definition: IGMP_Host.c:69
void pim_handle_timer_event()
Definition: PIM_SM.c:187
int iptable_add_check(ptrIP_ROUTINGTABLE current, ptrIP_ROUTINGTABLE mem)
void multicast_join_group()
void send_query_msg(NETSIM_ID d, NETSIM_IPAddress groupAddr, double time)
Definition: IGMP_Msg.c:233
bool is_reserved_multicast_address(NETSIM_IPAddress ip)
struct ForwardRoute IP_FORWARD_ROUTE
NETSIM_IPAddress dns_query(NETSIM_ID nDeviceId, NETSIM_ID id)
Definition: DNS.c:42
void host_handle_unsolicited_report_timer()
Definition: IGMP_Host.c:183
int iptable_delete_by_type(ptrIP_WRAPPER wrapper, char *type)
void pass_to_lower_layer(NetSim_PACKET *packet, ptrIP_FORWARD_ROUTE route, UINT c)
Definition: IP_Routing.c:568
int fn_NetSim_NAT_NetworkOut(NETSIM_ID ndev, NetSim_PACKET *packet)
Definition: NAT.c:47
#define c
#define UINT
Definition: Linux.h:38
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
bool resp(ptrCLIENTINFO info, char *msg, bool isMore)
Definition: Ping.c:44
@ NW_PROTOCOL_IPV4
Definition: Stack.h:189
UINT count
Definition: IP.h:146
NETSIM_IPAddress * nextHop
Definition: IP.h:147
NETSIM_ID * nextHopId
Definition: IP.h:150
NETSIM_IPAddress * gateway
Definition: IP.h:148
struct ForwardRoute * next
Definition: IP.h:151
NETSIM_ID * interfaceId
Definition: IP.h:149
Definition: List.h:43
Structure to store the device ip details.
Definition: IP.h:226
NETSIM_IPAddress ipPoolEnd
Definition: IP.h:258
void * ACL
Definition: IP.h:231
NETSIM_IPAddress * GatewayIPAddress
Definition: IP.h:245
void * pim
Definition: IP.h:275
NETSIM_IPAddress ipPoolStart
Definition: IP.h:257
unsigned int nRouterAdverLifeTime
Definition: IP.h:241
void * igmp
Definition: IP.h:271
unsigned int nRouterAdverMaxInterval
Definition: IP.h:240
bool isICMP
Definition: IP.h:236
NETSIM_IPAddress ipPoolMask
Definition: IP.h:259
void * multicast
Definition: IP.h:267
IP_GATEWAYSTATE * nGatewayState
Definition: IP.h:248
NETSIM_ID * nGatewayId
Definition: IP.h:246
char * staticIPTableFile
Definition: IP.h:228
unsigned int nRouterAdvertisementFlag
Definition: IP.h:238
unsigned int nICMPPollingTime
Definition: IP.h:243
double processingDelay
Definition: IP.h:278
void * dhcp
Definition: IP.h:264
bool isFirewallConfigured
Definition: IP.h:232
VPN_STATE nVPNStatus
Definition: IP.h:251
bool isPIMConfigured
Definition: IP.h:274
void * vpn
Definition: IP.h:260
NETSIM_IPAddress serverIP
Definition: IP.h:255
unsigned int nRouterAdverMinInterval
Definition: IP.h:239
NETSIM_ID * nInterfaceId
Definition: IP.h:247
char * firewallConfig
Definition: IP.h:233
unsigned int nGatewayCount
Definition: IP.h:244
void * dnsList
Definition: IP.h:261
bool isIGMPConfigured
Definition: IP.h:270
Structure to store the IP metrics.
Definition: IP.h:282
unsigned int nFirewallBlocked
Definition: IP.h:288
NETSIM_ID nDeviceId
Definition: IP.h:283
unsigned int nPacketReceived
Definition: IP.h:285
unsigned int nPacketDiscarded
Definition: IP.h:287
unsigned int nPacketForwarded
Definition: IP.h:286
unsigned int nTTLDrop
Definition: IP.h:289
unsigned int nPacketSent
Definition: IP.h:284
Structure to store ip routing table.
Definition: IP.h:157
NETSIM_ID * nInterfaceId
Definition: IP.h:170
unsigned int prefix_len
Definition: IP.h:163
NETSIM_ID nGatewayId
Definition: IP.h:171
NETSIM_IPAddress * Interface
Definition: IP.h:162
NETSIM_IPAddress networkDestination
Definition: IP.h:158
NETSIM_IPAddress gateway
Definition: IP.h:160
ROUTING_TYPE type
Definition: IP.h:165
unsigned int Metric
Definition: IP.h:164
NETSIM_IPAddress netMask
Definition: IP.h:159
Data structure for dns.
Definition: IP.h:294
_ele * ele
Definition: IP.h:297
NETSIM_ID deviceId
Definition: IP.h:295
NETSIM_IPAddress ip
Definition: IP.h:296