-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathiputils.pm
135 lines (106 loc) · 2.77 KB
/
iputils.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env perl
#
# Author: soarpenguin <[email protected]>
# First release Jan.3 2014
##################################################
# usage of iputils.pm, add code like:
# use lib "path"; #path is the file of iputils.pm
# use iputils;
##################################################
package iputils;
use FileHandle;
use Getopt::Long;
use Time::Local qw(timelocal);
use Term::ANSIColor;
use strict;
$|=1;
BEGIN {
use Exporter();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
$VERSION = 0.0.1;
@ISA = qw(Exporter);
@EXPORT = qw(
validate_ip ip2long long2ip ip2hex hex2ip
__FUNC__ __func__ get_now_time trim
);
@EXPORT_OK = qw(&mk_fd_nonblocking &get_winsize);
}
use constant true => 1;
use constant false => 0;
use constant MAX_IP => 0xffffffff;
use constant MIN_IP => 0x0;
sub validate_ip {
my $ip = shift;
my @array = ();
return false unless $ip;
$ip = trim($ip);
if ($ip =~ /^(\d+\.){3}(\d+)$/) {
@array = split(/\./, $ip);
foreach my $i (@array) {
if ($i > 255 || $i < 0) {
return false;
}
}
return true;
}
return false;
}
sub ip2long {
my $ip = shift;
my $long = 0;
my $ret = validate_ip($ip);
if ($ret == false) {
return $long;
}
my @array = split(/\./, $ip);
foreach my $e (@array) {
$long = ($long << 8 | $e);
}
return $long;
}
sub long2ip {
my $l = shift;
return "0.0.0.0" unless $l;
if ($l > MAX_IP or $l < MIN_IP) {
return "0.0.0.0";
}
return sprintf("%d.%d.%d.%d",
$l >> 24 & 255, $l >> 16 & 255, $l >> 8 & 255, $l & 255);
}
sub ip2hex {
my $netip = shift;
return "0.0.0.0" unless $netip;
$netip = ip2long($netip);
return sprintf("%08x", $netip);
}
sub hex2ip {
my $netip = shift;
#$netip = sprintf("%08x", $netip);
return long2ip($netip);
}
# set socket nonblocking.
sub mk_fd_nonblocking {
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
my $fd = shift;
my $flags = fcntl($fd, F_GETFL, 0)
or warn "Can't get flags for the fd: $!\n";
$flags = fcntl($fd, F_SETFL, $flags | O_NONBLOCK)
or warn "Can't set flags for the fd: $!\n";
}
sub get_now_time {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
my $now_time = sprintf("%d.%d.%d %d:%d:%d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
return "[$now_time]";
}
sub trim
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Retrieve the name of the current function
sub __FUNC__ { (caller(1))[3] . '()' }
# display the name of the current function
sub __func__ { (caller(1))[3] . '(' . join(', ', @_) . ')' }
1;