net_utils.pm 11 KB

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