1
0

net_utils.pm 11 KB

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