AMXX-BG.INFO ip.inc Raw include

ip.inc

Original include source with line numbers.

Back Download .inc
1 #if defined _ip_included
2 #endinput
3 #endif
4 #define _ip_included
5
6 /*
7 IP converter by Zefir<[email protected]>
8 developed for Cerberus project
9 http://cerberus.cstrike.in.ua/
10 2 jule 2009 (c) Zefir
11
12
13 WARNING!!! INPUT DATA NOT CHECKED FOR AVAILABLE RANGE
14 WARNING!!! mask used only left filled, not mask as 11101111 11110000 000...
15
16 inet_aton(ip_string[])
17 convert x.x.x.x to LONG
18 return unsigned long
19
20 inet_ntoa(ip_number)
21 convert LONG to x.x.x.x
22 return string[16]
23
24 inet_atom(mask_string)
25 convert x.x.x.x to /xx
26 return digit 0-32
27
28 inet_ntom(mask_number)
29 convert LONG to /xx
30 return digit 0-32
31
32 inet_mtoa(mask_string)
33 convert /xx to x.x.x.x
34 return string[16]
35
36 inet_mton(mask_string)
37 convert /xx to LONG
38 return unsigned long
39
40 inet_maskton(mask_string[])
41 convert both types (/xx or x.x.x.x) to LONG
42 return unsigned long
43
44 inet_range(net_string, &net, &mask)
45 get net and mask as LONG from subnet x.x.x.x/xx OR x.x.x.x/x.x.x.x
46 return true if mask /32
47
48 in_range(net[], ip[])
49 check net[] subnet contain ip[] address
50
51 ip_local(ip)
52 return true if ip as LONG be contained in any from this networks:
53 127.0.0.0/8
54 10.0.0.0/8
55 172.16.0.0/12
56 192.168.0.0/16
57 169.254.0.0/16
58
59 */
60
61 stock inet_aton(const ip_string[]) {
62 static ip[16], ips[1], i, j, cur; ips[0] = 0; j = 0; cur = 0;
63 copy(ip, 15, ip_string);
64 while (j < 3 && (i = contain(ip[cur], ".")) > -1) {
65 ip[cur + i++] = EOS;
66 ips{j++} = str_to_num(ip[cur]);
67 cur += i;
68 }
69 ips{j} = str_to_num(ip[cur]);
70 return ips[0]
71 }
72
73 stock inet_ntoa(ip) {
74 static ip_string[16], ips[1]; ip_string[0] = EOS; ips[0] = ip
75 formatex(ip_string, 15, "%d.%d.%d.%d", ips{0}, ips{1}, ips{2}, ips{3});
76 return ip_string;
77 }
78
79
80 stock inet_atom(const mask_string[]) {
81 static ip, i; ip = inet_aton(mask_string), i = 0;
82 while (!(ip & 1) && i++ < 31) ip >>>= 1;
83 return 31 - i;
84 }
85
86 stock inet_ntom(mask) {
87 static i; i = 0;
88 while (!(mask & 1) && i++ < 31) mask >>>= 1;
89 return 31 - i;
90 }
91
92 stock bool:inet_range(const net_string[], &net, &mask) {
93 static lnet[16], i;
94 if ((i = contain(net_string, "/")) > -1) {
95 copy(lnet, i, net_string);
96 mask = inet_maskton(net_string[i + 1]);
97 } else {
98 mask = 0;
99 }
100 net = inet_aton(lnet) & mask;
101 return mask == 4294967295;
102 }
103
104 stock bool:in_range(const net_string[], const ip_string[]) {
105 static net, mask;
106 inet_range(net_string, net, mask);
107 return inet_aton(ip_string) & mask == net;
108 }
109
110 // All int is signed. And mask first bit, its bit of sign. :(
111 // Set if mask > 0 and signed shift of mask bits count - 1
112 stock inet_mton(const mask_string[]) {
113 static mask;
114 mask = str_to_num(mask_string);
115 if (!mask) return 0;
116 return 4294967295 << (31 - mask);
117 }
118
119 stock inet_mtoa(const mask_string[])
120 return inet_ntoa(inet_mton(mask_string));
121
122 stock inet_maskton(const mask_string[]) {
123 if (contain(mask_string, ".") > -1)
124 return inet_aton(mask_string);
125 return inet_mton(mask_string);
126 }
127
128 /*
129 net: 127.0.0.0/8, net = 2130706432, mask = 4278190080
130 net: 10.0.0.0/8, net = 167772160, mask = 4278190080
131 net: 172.16.0.0/12, net = 2886729728, mask = 4293918720
132 net: 192.168.0.0/16, net = 3232235520, mask = 4294901760
133 net: 169.254.0.0/16, net = 2851995648, mask = 4294901760
134 */
135 stock bool:ip_local(ip) {
136 return 2130706432 == ip & 4278190080 ||
137 167772160 == ip & 4278190080 ||
138 2886729728 == ip & 4293918720 ||
139 3232235520 == ip & 4294901760 ||
140 2851995648 == ip & 4294901760;
141 }
142
143
144