Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scapy
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CodeLinaro
public-release-test-restored
platform
external
scapy
Commits
4d7fd6a2
Commit
4d7fd6a2
authored
15 years ago
by
Phil
Browse files
Options
Downloads
Patches
Plain Diff
Have IP comparisons with Net() objects work. Also added "xxx in Net()" operation
parent
d88f0f11
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
scapy/base_classes.py
+40
-16
40 additions, 16 deletions
scapy/base_classes.py
with
40 additions
and
16 deletions
scapy/base_classes.py
+
40
−
16
View file @
4d7fd6a2
...
@@ -46,28 +46,34 @@ class Net(Gen):
...
@@ -46,28 +46,34 @@ class Net(Gen):
"""
Generate a list of IPs from a network address or a name
"""
"""
Generate a list of IPs from a network address or a name
"""
name
=
"
ip
"
name
=
"
ip
"
ipaddress
=
re
.
compile
(
r
"
^(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)(/[0-3]?[0-9])?$
"
)
ipaddress
=
re
.
compile
(
r
"
^(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)(/[0-3]?[0-9])?$
"
)
def
__init__
(
self
,
net
):
self
.
repr
=
net
@staticmethod
def
_parse_digit
(
a
,
netmask
):
netmask
=
min
(
8
,
max
(
netmask
,
0
))
if
a
==
"
*
"
:
a
=
(
0
,
256
)
elif
a
.
find
(
"
-
"
)
>=
0
:
x
,
y
=
map
(
int
,
a
.
split
(
"
-
"
))
if
x
>
y
:
y
=
x
a
=
(
x
&
(
0xff
L
<<
netmask
)
,
max
(
y
,
(
x
|
(
0xff
L
>>
(
8
-
netmask
))))
+
1
)
else
:
a
=
(
int
(
a
)
&
(
0xff
L
<<
netmask
),(
int
(
a
)
|
(
0xff
L
>>
(
8
-
netmask
)))
+
1
)
return
a
@classmethod
def
_parse_net
(
cls
,
net
):
tmp
=
net
.
split
(
'
/
'
)
+
[
"
32
"
]
tmp
=
net
.
split
(
'
/
'
)
+
[
"
32
"
]
if
not
self
.
ipaddress
.
match
(
net
):
if
not
cls
.
ipaddress
.
match
(
net
):
tmp
[
0
]
=
socket
.
gethostbyname
(
tmp
[
0
])
tmp
[
0
]
=
socket
.
gethostbyname
(
tmp
[
0
])
netmask
=
int
(
tmp
[
1
])
netmask
=
int
(
tmp
[
1
])
return
map
(
lambda
x
,
y
:
cls
.
_parse_digit
(
x
,
y
),
tmp
[
0
].
split
(
"
.
"
),
map
(
lambda
x
,
nm
=
netmask
:
x
-
nm
,
(
8
,
16
,
24
,
32
))),
netmask
def
__init__
(
self
,
net
):
self
.
repr
=
net
self
.
parsed
,
self
.
netmask
=
self
.
_parse_net
(
net
)
def
parse_digit
(
a
,
netmask
):
netmask
=
min
(
8
,
max
(
netmask
,
0
))
if
a
==
"
*
"
:
a
=
(
0
,
256
)
elif
a
.
find
(
"
-
"
)
>=
0
:
x
,
y
=
map
(
int
,
a
.
split
(
"
-
"
))
if
x
>
y
:
y
=
x
a
=
(
x
&
(
0xff
L
<<
netmask
)
,
max
(
y
,
(
x
|
(
0xff
L
>>
(
8
-
netmask
))))
+
1
)
else
:
a
=
(
int
(
a
)
&
(
0xff
L
<<
netmask
),(
int
(
a
)
|
(
0xff
L
>>
(
8
-
netmask
)))
+
1
)
return
a
self
.
parsed
=
map
(
lambda
x
,
y
:
parse_digit
(
x
,
y
),
tmp
[
0
].
split
(
"
.
"
),
map
(
lambda
x
,
nm
=
netmask
:
x
-
nm
,
(
8
,
16
,
24
,
32
)))
def
__iter__
(
self
):
def
__iter__
(
self
):
for
d
in
xrange
(
*
self
.
parsed
[
3
]):
for
d
in
xrange
(
*
self
.
parsed
[
3
]):
...
@@ -83,6 +89,24 @@ class Net(Gen):
...
@@ -83,6 +89,24 @@ class Net(Gen):
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"
Net(%r)
"
%
self
.
repr
return
"
Net(%r)
"
%
self
.
repr
def
__eq__
(
self
,
other
):
if
hasattr
(
other
,
"
parsed
"
):
p2
=
other
.
parsed
else
:
p2
,
nm2
=
self
.
_parse_net
(
other
)
return
self
.
parsed
==
p2
def
__contains__
(
self
,
other
):
if
hasattr
(
other
,
"
parsed
"
):
p2
=
other
.
parsed
else
:
p2
,
nm2
=
self
.
_parse_net
(
other
)
for
(
a1
,
b1
),(
a2
,
b2
)
in
zip
(
self
.
parsed
,
p2
):
if
a1
>
a2
or
b1
<
b2
:
return
False
return
True
def
__rcontains__
(
self
,
other
):
return
self
in
self
.
__class__
(
other
)
class
OID
(
Gen
):
class
OID
(
Gen
):
name
=
"
OID
"
name
=
"
OID
"
...
...
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