Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
awit-perl-toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
awit-frameworks
awit-perl-toolkit
Commits
064b2ea7
Commit
064b2ea7
authored
15 years ago
by
Nigel Kukard
Browse files
Options
Downloads
Patches
Plain Diff
* Added function to test IPv4/6 validity
- Robert Anderson <randerson@lbsd.net>
parent
6f0a9ec4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
awitpt/netip.pm
+82
-0
82 additions, 0 deletions
awitpt/netip.pm
with
82 additions
and
0 deletions
awitpt/netip.pm
+
82
−
0
View file @
064b2ea7
...
...
@@ -86,6 +86,88 @@ sub new
}
# Check for a valid ipv6/ipv4 address
sub
is_valid
{
my
$ip
=
shift
;
# Check if defined
if
(
!
defined
(
$ip
))
{
return
0
;
}
# Check for valid IPv4 address
if
(
$ip
=~
/^(\d{1,3})(?:\.(\d{1,3})(?:\.(\d{1,3})(?:\.(\d{1,3}))?)?)?(?:\/(\d{1,2}))?$/
)
{
# Check octets are within limits
foreach
(
$
1
,
$
2
,
$
3
,
$
4
)
{
if
(
defined
(
$_
))
{
if
(
$_
>
255
||
$_
<
0
)
{
return
0
;
}
}
}
# Check bitmask is within limits
if
(
defined
(
$
5
))
{
if
(
$
5
>
32
||
$
5
<
1
)
{
return
0
;
}
}
# Check for valid IPv6 address
}
elsif
(
$ip
=~
/:/
)
{
# Pull off and check bitmask
if
(
$ip
=~
s/\/(\d{1,3})$//g
)
{
if
(
$
1
>
128
||
$
1
<
1
)
{
return
0
;
}
}
# Check for illegal characters
if
(
!
(
$ip
=~
/^[a-f\d:]+$/i
))
{
return
0
;
}
# Does the IP address have more than one '::' pattern ?
my
$count
;
while
(
$ip
=~
/::/g
)
{
$count
++
;
}
if
(
$count
>
1
)
{
return
0
;
}
# Count octets
my
$n
=
(
$ip
=~
tr/:/:/
);
if
(
!
(
$n
>
0
and
$n
<
8
))
{
return
0
;
}
# Check octets
foreach
(
split
/:/
,
$ip
)
{
# Empty octet ?
next
if
(
$_
eq
'');
# Normal v6 octet ?
next
if
(
/^[a-f\d]{1,4}$/i
);
return
0
;
}
# Do not recognise
}
else
{
return
0
;
}
return
1
;
}
# Clone an object
sub
copy
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment