net_utils.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_ip
  33. is_ipip_valid
  34. is_ip_valid
  35. print_net
  36. ping
  37. HostIsLive
  38. InitSubnets
  39. mac_simplify
  40. isc_mac_simplify
  41. mac_cisco
  42. mac2dec
  43. mac_splitted
  44. ResolveNames
  45. );
  46. BEGIN
  47. {
  48. #local nets
  49. our $RFC1918;
  50. our $Special_Nets;
  51. our $Loopback;
  52. #------------------------------------------------------------------------------------------------------------
  53. sub ResolveNames {
  54. my $hostname = shift;
  55. my @result=();
  56. my $res = Net::DNS::Resolver->new;
  57. $res->nameservers($dns_server) if (!$dns_server);
  58. my $query = $res->search($hostname,"A");
  59. my $result;
  60. if ($query) {
  61. foreach my $rr ($query->answer) { push(@result,$result = $rr->address); }
  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 {
  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. return 1;
  217. }
  218. return 0;
  219. }
  220. #---------------------------------------------------------------------------------------------------------
  221. sub is_ip_valid {
  222. my $ip_str = trim($_[0]);
  223. 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}/) {
  224. my $mask = $5;
  225. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  226. $mask =~s/\/// if ($mask);
  227. $mask = 32 if (!$mask);
  228. return 0 if ($mask > 32);
  229. if ($Special_Nets->match_string($ip_str)) { log_error("$ip_str in illegal net range"); return 0; };
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. #---------------------------------------------------------------------------------------------------------
  235. sub print_net {
  236. my $ip = shift;
  237. my $max_mask = shift || 26;
  238. return if (!$ip);
  239. my @result = ();
  240. my $user_ip=NetAddr::IP->new($ip);
  241. my $netmask = $user_ip->masklen();
  242. return if ($netmask<$max_mask);
  243. my $ip_first = $user_ip->network();
  244. my $ip_last = $user_ip->broadcast();
  245. my $ip_count = $ip_last - $ip_first;
  246. if ($ip_count) {
  247. for (my $i=0; $i<=$ip_count; $i++) {
  248. my $ip1 = $ip_first;
  249. $ip1=~s/\/\d+$//g;
  250. push(@result,$ip1);
  251. $ip_first ++;
  252. }
  253. } else {
  254. $ip_first=~s/\/\d+$//g;
  255. push(@result,$ip_first);
  256. }
  257. return @result;
  258. }
  259. #---------------------------------------------------------------------------------------------------------
  260. sub ping {
  261. use Net::Ping;
  262. use Time::HiRes;
  263. my ($host,$time) = @_;
  264. my $p = Net::Ping->new();
  265. $p->hires();
  266. my ($ret, $duration, $ip) = $p->ping($host, $time);
  267. $p->close();
  268. $ret ? return 1: return 0;
  269. }
  270. #---------------------------------------------------------------------------------------------------------
  271. sub HostIsLive {
  272. my $host=shift;
  273. my $proto=shift || "tcp";
  274. if ($< eq 0) { $proto="icmp"; }
  275. my $p = Net::Ping->new($proto);
  276. my $ok= $p->ping($host,5);
  277. $p->close();
  278. return $ok;
  279. }
  280. #----------------------------------------------------------------------------------
  281. sub InitSubnets {
  282. $RFC1918 = new Net::Patricia;
  283. #local nets RFC1918
  284. $RFC1918->add_string("192.168.0.0/16");
  285. $RFC1918->add_string("10.0.0.0/8");
  286. $RFC1918->add_string("172.16.0.0/12");
  287. #----------------------------------------------------------------------------------
  288. $Special_Nets = new Net::Patricia;
  289. #"This" Network [RFC1700, page 4]
  290. $Special_Nets->add_string("0.0.0.0/8");
  291. #Public-Data Networks [RFC1700, page 181]
  292. #$Special_Nets->add_string("14.0.0.0/8");
  293. #Cable Television Networks
  294. #$Special_Nets->add_string("24.0.0.0/8");
  295. #Reserved - [RFC1797]
  296. #$Special_Nets->add_string("39.0.0.0/8");
  297. #loopback [RFC1700, page 5]
  298. $Special_Nets->add_string("127.0.0.0/8");
  299. #Reserved
  300. $Special_Nets->add_string("128.0.0.0/16");
  301. #Link Local
  302. $Special_Nets->add_string("169.254.0.0/16");
  303. #Reserved
  304. #$Special_Nets->add_string("191.255.0.0/16");
  305. #Reserved
  306. #$Special_Nets->add_string("192.0.0.0/24");
  307. #Test-Net
  308. #$Special_Nets->add_string("192.0.2.0/24");
  309. #6to4 Relay Anycast [RFC3068]
  310. $Special_Nets->add_string("192.88.99.0/24");
  311. #Network Interconnect Device Benchmark Testing [RFC2544]
  312. #$Special_Nets->add_string("198.18.0.0/15");
  313. #Reserved
  314. #$Special_Nets->add_string("223.255.255.0/24");
  315. #Multicast [RFC3171]
  316. $Special_Nets->add_string("224.0.0.0/4");
  317. #Reserved for Future Use [RFC1700, page 4]
  318. #$Special_Nets->add_string("240.0.0.0/4");
  319. #----------------------------------------------------------------------------------
  320. $Loopback = new Net::Patricia;
  321. #loopback [RFC1700, page 5]
  322. $Loopback->add_string("127.0.0.0/8");
  323. }
  324. #--------------------------------- Utils ------------------------------------------
  325. sub mac_simplify{
  326. my $mac=shift;
  327. return if (!$mac);
  328. $mac=~s/\.//g;
  329. $mac=~s/://g;
  330. $mac=~s/-//g;
  331. $mac=~tr/[A-Z]/[a-z]/;
  332. return $mac;
  333. }
  334. #--------------------------------------------------------------------------------
  335. sub mac_cisco{
  336. my $mac=shift;
  337. return if (!$mac);
  338. $mac=mac_simplify($mac);
  339. $mac=~s/(\S{4})(\S{4})(\S{4})/$1\.$2\.$3/g;
  340. return $mac;
  341. }
  342. #--------------------------------------------------------------------------------
  343. sub mac2dec{
  344. my $mac=shift;
  345. return if (!$mac);
  346. $mac=mac_simplify($mac);
  347. $mac=mac_splitted($mac);
  348. my @m=split(":",$mac);
  349. $mac="";
  350. foreach my $i (@m) { $mac=$mac.".".hex($i); }
  351. $mac=~s/^\.//;
  352. return $mac;
  353. }
  354. #--------------------------------------------------------------------------------
  355. sub isc_mac_simplify{
  356. my $mac=shift;
  357. return if (!$mac);
  358. my @mac_array;
  359. foreach my $octet (split(/\:/,$mac)){
  360. my $dec=hex($octet);
  361. push(@mac_array,sprintf("%02x",$dec));
  362. }
  363. $mac=join('',@mac_array);
  364. return $mac;
  365. }
  366. #--------------------------------------------------------------------------------
  367. sub mac_splitted{
  368. my $mac=shift;
  369. return if (!$mac);
  370. my $ch=shift || ":";
  371. $mac=mac_simplify($mac);
  372. $mac=~s/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/$1:$2:$3:$4:$5:$6/g;
  373. if ($ch ne ":") { $mac=~s/\:/$ch/g; }
  374. return $mac;
  375. }
  376. InitSubnets();
  377. 1;
  378. }