NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
IP_Addressing.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 * Author: ,~~. *
12 ( 6 )-_,
13 (\___ )=='-'
14 \ . ) )
15 \ `-' /
16 ~'`~'`~'`~'`~ Shashi kant suman
17 * *
18 * ---------------------------------------------------------------------------------*/
19#ifndef _NETSIM_IPADDRESSING_H_
20#define _NETSIM_IPADDRESSING_H_
21#ifdef __cplusplus
22extern "C" {
23#endif
24#ifdef _WIN32
25#else
26 #include "Linux.h"
27// #define _declspec(dllexport) extern
28#endif
29#define _NETSIM_IP_LEN 50
30#define _NETSIM_BIN_IP_LEN 130 //128+1(\0)+1(precaution) for IPv6
31#define MAX_TTL 255
32 typedef struct stru_ip* NETSIM_IPAddress;
33
34struct stru_ip
35{
36 int type;
37 union
38 {
39 struct ipv4
40 {
41 unsigned int byte1;
42 unsigned int byte2;
43 unsigned int byte3;
44 unsigned int byte4;
45 }IPV4;
46 struct ipv6
47 {
48 unsigned int byte[8];
49 }IPV6;
50 }IP;
51 unsigned int int_ip[4];
52
53 //The following are stored to do fast calculation
54 char str_ip[_NETSIM_IP_LEN];
55 char bin_ip[130];
56 void *nets;
57 void* macAddress;
58};
59
60#define IP_COMPARE6(ip1,ip2) (ip1->int_ip[0]==ip2->int_ip[0] \
61 ?(ip1->int_ip[1]==ip2->int_ip[1] \
62 ?(ip1->int_ip[2]==ip2->int_ip[2] \
63 ?(ip1->int_ip[3]==ip2->int_ip[3]?0:1) :1) :1) :1)
64
65#define IP_COMPARE4(ip1,ip2) (ip1->int_ip[0]==ip2->int_ip[0]?0:1)
66
67#define IP_COMPARE(ip1,ip2) ((ip1)->type==(ip2)->type \
68 ? ((ip1)->type==4?IP_COMPARE4((ip1),(ip2)):IP_COMPARE6((ip1),(ip2))) \
69 :1)
70
71#define IP_TO_STR(ip,ipstr) ((ip)?(strcpy((ipstr),((ip)->str_ip))):strcpy(ipstr,"0.0.0.0"))
72
73
74_declspec(dllexport) NETSIM_IPAddress STR_TO_IP(char* ipStr,int type);
75
76_declspec(dllexport) NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip);
77_declspec(dllexport) void IP_FREE(NETSIM_IPAddress ip);
78_declspec(dllexport) NETSIM_IPAddress IP_COPY_FORCE(NETSIM_IPAddress ip);
79_declspec(dllexport) void IP_FREE_FORCE(NETSIM_IPAddress ip);
80_declspec(dllexport) void IP_TO_BINARY(NETSIM_IPAddress ip,char* binary);
81_declspec(dllexport) unsigned int IP_ADD_TO_LIST(NETSIM_IPAddress** pipList,unsigned int* count,const NETSIM_IPAddress ip);
82_declspec(dllexport) unsigned int IP_REMOVE_FROM_LIST(NETSIM_IPAddress* pipList,unsigned int* count,const NETSIM_IPAddress ip);
83_declspec(dllexport) int IP_CHECK_IN_LIST(const NETSIM_IPAddress* ipList,unsigned int count,const NETSIM_IPAddress ip);
84_declspec(dllexport) int IP_IS_IN_SAME_NETWORK(NETSIM_IPAddress ip1,NETSIM_IPAddress ip2,NETSIM_IPAddress subnet,unsigned int prefix_len);
85_declspec(dllexport) NETSIM_IPAddress IP_NETWORK_ADDRESS(NETSIM_IPAddress ip,NETSIM_IPAddress subnet,unsigned int prefix_len);
86_declspec(dllexport) NETSIM_IPAddress GET_BROADCAST_IP(int type);
87
88_declspec(dllexport) int IP_FREE_ALL(); //Free all IP. Used only by NetworkStack.dll after simulation
89//Validate all configured ip. return 1 if all ip is valid else return 0
90/** Very costly function. Use only for debugging **/
91_declspec(dllexport) int IP_VALIDATE_ALL_IP();
92
93_declspec(dllexport) int STR_GET_IP_TYPE(char* ip);
94#define STR_TO_IP4(ipstr) STR_TO_IP(ipstr,4)
95#define STR_TO_IP6(ipstr) STR_TO_IP(ipstr,6)
96
97#define IP_IS_IN_SAME_NETWORK_IPV4(ip1,ip2,subnet) IP_IS_IN_SAME_NETWORK(ip1,ip2,subnet,0)
98#define IP_IS_IN_SAME_NETWORK_IPV6(ip1,ip2,prefix) IP_IS_IN_SAME_NETWORK(ip1,ip2,NULL,prefix)
99
100#define IP_NETWORK_ADDRESS_IPV4(ip,subnet) IP_NETWORK_ADDRESS(ip,subnet,0)
101#define IP_NETWORK_ADDRESS_IPV6(ip,prefix) IP_NETWORK_ADDRESS(ip,NULL,prefix)
102
103_declspec(dllexport) bool isBroadcastIP(NETSIM_IPAddress ip);
104_declspec(dllexport) bool isMulticastIP(NETSIM_IPAddress ip);
105_declspec(dllexport) bool isValidIPAddress(char* ip);
106_declspec(dllexport) int IP_find_match_length(NETSIM_IPAddress ip1, NETSIM_IPAddress ip2);
107
108#ifdef __cplusplus
109}
110#endif
111#endif