net_utils.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package Rstat::net_utils;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use strict;
  6. use English;
  7. use FindBin qw($Bin);
  8. use lib "$Bin";
  9. use base 'Exporter';
  10. use vars qw(@EXPORT @ISA);
  11. use FileHandle;
  12. use POSIX;
  13. use Rstat::config;
  14. use Rstat::main;
  15. use Net::Ping;
  16. use Net::Patricia;
  17. use NetAddr::IP;
  18. use Net::DNS;
  19. our @ISA = qw(Exporter);
  20. our @EXPORT = qw(
  21. $RFC1918
  22. $Special_Nets
  23. $Loopback
  24. IPv4Numeric
  25. is_gate_valid
  26. CheckIP
  27. GetDhcpRange
  28. GetIpRange
  29. GetIP
  30. GetSubNet
  31. is_ipip_valid
  32. is_ip_valid
  33. print_net
  34. ping
  35. HostIsLive
  36. InitSubnets
  37. mac_simplify
  38. isc_mac_simplify
  39. mac_cisco
  40. mac2dec
  41. mac_splitted
  42. ResolveNames
  43. );
  44. BEGIN
  45. {
  46. #local nets
  47. our $RFC1918;
  48. our $Special_Nets;
  49. our $Loopback;
  50. #------------------------------------------------------------------------------------------------------------
  51. sub ResolveNames {
  52. my $hostname = shift;
  53. my @result=();
  54. my $res = Net::DNS::Resolver->new;
  55. $res->nameservers($dns_server);
  56. my $query = $res->search($hostname);
  57. my $result;
  58. if ($query) {
  59. foreach my $rr ($query->answer) {
  60. if ($rr->type eq "A") { push(@result,$result = $rr->address); }
  61. }
  62. }
  63. return (@result);
  64. }
  65. #------------------------------------------------------------------------------------------------------------
  66. sub IPv4Numeric {
  67. my $ip = shift;
  68. return 0 if (!$ip);
  69. my $net=NetAddr::IP->new($ip);
  70. return $net->numeric();
  71. }
  72. #------------------------------------------------------------------------------------------------------------
  73. sub is_gate_valid {
  74. my $ip_str = trim($_[0]);
  75. if ($ip_str =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  76. my $mask = $5;
  77. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  78. $mask =~s/\/// if ($mask);
  79. $mask = 32 if (!$mask);
  80. return 0 if ($mask > 31);
  81. if ($Special_Nets->match_string($ip_str)) { log_error("$ip_str in illegal net range"); return 0; };
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. #---------------------------------------------------------------------------------------------------------
  87. sub CheckIP {
  88. my $ip_str = shift;
  89. return 0 if (!$ip_str);
  90. $ip_str = trim($ip_str);
  91. if ($ip_str =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  92. my $mask = $5;
  93. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  94. $mask =~s/\/// if ($mask);
  95. $mask = 32 if (!$mask);
  96. my $ip = NetAddr::IP->new($ip_str)->addr();
  97. if ($mask<32) { $ip = $ip."/".$mask; }
  98. return $ip;
  99. }
  100. return 0;
  101. }
  102. #--------------------------------------------------------------------------------------------------------
  103. sub GetDhcpRange {
  104. my $gate_ip = trim($_[0]);
  105. my $mask;
  106. if ($gate_ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  107. $mask = $5;
  108. return if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  109. $mask =~s/\/// if ($mask);
  110. $mask = 32 if ((!$mask) or ($mask >32));
  111. } else { return; }
  112. my $gate = NetAddr::IP->new($gate_ip)->addr();
  113. my $net=NetAddr::IP->new($gate."/".$mask);
  114. my %dhcp;
  115. $dhcp{gate} = $gate;
  116. $dhcp{network} = $net->network()->addr();
  117. $dhcp{broadcast} = $net->broadcast()->addr();
  118. $dhcp{mask} = $net->mask();
  119. $dhcp{masklen} = $net->masklen();
  120. $dhcp{first_ip} = $net->first()->addr();
  121. $dhcp{count} = $net->broadcast() - $net->network() - 2;
  122. if ($mask < 32) {
  123. if ($dhcp{first_ip} eq $dhcp{gate}) {
  124. $dhcp{first_ip} = $net->nth(1)->addr();
  125. }
  126. $dhcp{last_ip} = $net->last()->addr();
  127. if ($dhcp{last_ip} eq $dhcp{gate}) {
  128. $dhcp{last_ip} = $net->nth($dhcp{count}-1)->addr();
  129. }
  130. }
  131. return \%dhcp;
  132. }
  133. #--------------------------------------------------------------------------------------------------------
  134. sub GetIpRange {
  135. my $gate_ip = trim($_[0]);
  136. my $mask = 30;
  137. if ($gate_ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  138. $mask = $5;
  139. return if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  140. $mask =~s/\/// if ($mask);
  141. $mask = 30 if ((!$mask) or ($mask >30));
  142. } else { return; }
  143. my $gate = NetAddr::IP->new($gate_ip)->addr();
  144. my $net=NetAddr::IP->new($gate."/".$mask);
  145. my @range=();
  146. if ($mask >=29) {
  147. my $ip_count = $net->broadcast() - $net->network() - 2;
  148. for (my $index = 1; $index<=$ip_count; $index++) {
  149. my $ip = $net->nth($index)->addr();
  150. next if ($ip eq $gate);
  151. push(@range,$ip."/32");
  152. }
  153. } else {
  154. push(@range,$net->network()->addr()."/".$mask);
  155. }
  156. return \@range;
  157. }
  158. #--------------------------------------------------------------------------------------------------------
  159. sub GetIP {
  160. my $ip_str = trim($_[0]);
  161. if ($ip_str =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  162. return if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  163. return NetAddr::IP->new($ip_str)->addr();
  164. }
  165. return;
  166. }
  167. #---------------------------------------------------------------------------------------------------------
  168. sub GetSubNet {
  169. my $ip_str = trim($_[0]);
  170. my $mask = 32;
  171. if ($ip_str =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  172. $mask = $5;
  173. return if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  174. $mask =~s/\/// if ($mask);
  175. $mask = 32 if (!$mask);
  176. }
  177. else { return; }
  178. my $ip = NetAddr::IP->new($ip_str)->network()->addr();
  179. return $ip."/".$mask;
  180. }
  181. #---------------------------------------------------------------------------------------------------------
  182. sub is_ipip_valid {
  183. my $ip1 = shift;
  184. my $ip2 = shift;
  185. my $lo1 = 0;
  186. my $lo2 = 0;
  187. my $ok = 0;
  188. eval {
  189. if ($ip1) {
  190. if ($ip1 ne "0/0") {
  191. $lo1 = $Loopback->match_string($ip1);
  192. $lo1 = 1 if ($lo1);
  193. }
  194. };
  195. if ($ip2) {
  196. if ($ip2 ne "0/0") {
  197. $lo2 = $Loopback->match_string($ip1);
  198. $lo2 = 1 if ($lo2);
  199. }
  200. };
  201. if (!$lo1) { $lo1=0; };
  202. if (!$lo2) { $lo2=0; };
  203. $ok = ((($ip1 ne "0/0") or ($ip2 ne "0/0")) and ($lo1==0) and ($lo2==0));
  204. };
  205. return $ok;
  206. }
  207. #---------------------------------------------------------------------------------------------------------
  208. sub is_ip_valid {
  209. my $ip_str = trim($_[0]);
  210. if ($ip_str =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  211. my $mask = $5;
  212. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  213. $mask =~s/\/// if ($mask);
  214. $mask = 32 if (!$mask);
  215. return 0 if ($mask > 32);
  216. if ($Special_Nets->match_string($ip_str)) { log_error("$ip_str in illegal net range"); return 0; };
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. #---------------------------------------------------------------------------------------------------------
  222. sub print_net {
  223. my $ip = shift;
  224. my $max_mask = shift || 26;
  225. return if (!$ip);
  226. my @result = ();
  227. my $user_ip=NetAddr::IP->new($ip);
  228. my $netmask = $user_ip->masklen();
  229. return if ($netmask<$max_mask);
  230. my $ip_first = $user_ip->network();
  231. my $ip_last = $user_ip->broadcast();
  232. my $ip_count = $ip_last - $ip_first;
  233. if ($ip_count) {
  234. for (my $i=0; $i<=$ip_count; $i++) {
  235. my $ip1 = $ip_first;
  236. $ip1=~s/\/\d+$//g;
  237. push(@result,$ip1);
  238. $ip_first ++;
  239. }
  240. } else {
  241. $ip_first=~s/\/\d+$//g;
  242. push(@result,$ip_first);
  243. }
  244. return @result;
  245. }
  246. #---------------------------------------------------------------------------------------------------------
  247. sub ping {
  248. use Net::Ping;
  249. use Time::HiRes;
  250. my ($host,$time) = @_;
  251. my $p = Net::Ping->new();
  252. $p->hires();
  253. my ($ret, $duration, $ip) = $p->ping($host, $time);
  254. $p->close();
  255. $ret ? return 1: return 0;
  256. }
  257. #---------------------------------------------------------------------------------------------------------
  258. sub HostIsLive {
  259. my $host=shift;
  260. my $p = Net::Ping->new("tcp",1,1);
  261. my $ok= $p->ping($host);
  262. $p->close();
  263. return $ok;
  264. }
  265. #----------------------------------------------------------------------------------
  266. sub InitSubnets {
  267. $RFC1918 = new Net::Patricia;
  268. #local nets RFC1918
  269. $RFC1918->add_string("192.168.0.0/16");
  270. $RFC1918->add_string("10.0.0.0/8");
  271. $RFC1918->add_string("172.16.0.0/12");
  272. #----------------------------------------------------------------------------------
  273. $Special_Nets = new Net::Patricia;
  274. #"This" Network [RFC1700, page 4]
  275. $Special_Nets->add_string("0.0.0.0/8");
  276. #Public-Data Networks [RFC1700, page 181]
  277. #$Special_Nets->add_string("14.0.0.0/8");
  278. #Cable Television Networks
  279. #$Special_Nets->add_string("24.0.0.0/8");
  280. #Reserved - [RFC1797]
  281. #$Special_Nets->add_string("39.0.0.0/8");
  282. #loopback [RFC1700, page 5]
  283. $Special_Nets->add_string("127.0.0.0/8");
  284. #Reserved
  285. $Special_Nets->add_string("128.0.0.0/16");
  286. #Link Local
  287. $Special_Nets->add_string("169.254.0.0/16");
  288. #Reserved
  289. #$Special_Nets->add_string("191.255.0.0/16");
  290. #Reserved
  291. #$Special_Nets->add_string("192.0.0.0/24");
  292. #Test-Net
  293. #$Special_Nets->add_string("192.0.2.0/24");
  294. #6to4 Relay Anycast [RFC3068]
  295. $Special_Nets->add_string("192.88.99.0/24");
  296. #Network Interconnect Device Benchmark Testing [RFC2544]
  297. #$Special_Nets->add_string("198.18.0.0/15");
  298. #Reserved
  299. #$Special_Nets->add_string("223.255.255.0/24");
  300. #Multicast [RFC3171]
  301. $Special_Nets->add_string("224.0.0.0/4");
  302. #Reserved for Future Use [RFC1700, page 4]
  303. #$Special_Nets->add_string("240.0.0.0/4");
  304. #----------------------------------------------------------------------------------
  305. $Loopback = new Net::Patricia;
  306. #loopback [RFC1700, page 5]
  307. $Loopback->add_string("127.0.0.0/8");
  308. }
  309. #--------------------------------- Utils ------------------------------------------
  310. sub mac_simplify{
  311. my $mac=shift;
  312. return if (!$mac);
  313. $mac=~s/\.//g;
  314. $mac=~s/://g;
  315. $mac=~s/-//g;
  316. $mac=~tr/[A-Z]/[a-z]/;
  317. return $mac;
  318. }
  319. #--------------------------------------------------------------------------------
  320. sub mac_cisco{
  321. my $mac=shift;
  322. return if (!$mac);
  323. $mac=mac_simplify($mac);
  324. $mac=~s/(\S{4})(\S{4})(\S{4})/$1\.$2\.$3/g;
  325. return $mac;
  326. }
  327. #--------------------------------------------------------------------------------
  328. sub mac2dec{
  329. my $mac=shift;
  330. return if (!$mac);
  331. $mac=mac_simplify($mac);
  332. $mac=mac_splitted($mac);
  333. my @m=split(":",$mac);
  334. $mac="";
  335. foreach my $i (@m) { $mac=$mac.".".hex($i); }
  336. $mac=~s/^\.//;
  337. return $mac;
  338. }
  339. #--------------------------------------------------------------------------------
  340. sub isc_mac_simplify{
  341. my $mac=shift;
  342. return if (!$mac);
  343. my @mac_array;
  344. foreach my $octet (split(/\:/,$mac)){
  345. my $dec=hex($octet);
  346. push(@mac_array,sprintf("%02x",$dec));
  347. }
  348. $mac=join('',@mac_array);
  349. return $mac;
  350. }
  351. #--------------------------------------------------------------------------------
  352. sub mac_splitted{
  353. my $mac=shift;
  354. return if (!$mac);
  355. my $ch=shift || ":";
  356. $mac=mac_simplify($mac);
  357. $mac=~s/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/$1:$2:$3:$4:$5:$6/g;
  358. if ($ch ne ":") { $mac=~s/\:/$ch/g; }
  359. return $mac;
  360. }
  361. InitSubnets();
  362. 1;
  363. }