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
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CodeLinaro
public-release-test-restored
platform
external
scapy
Commits
135d5a3e
Commit
135d5a3e
authored
16 years ago
by
Phil
Browse files
Options
Downloads
Patches
Plain Diff
Automaton: a bit of reorganization and made internal methods begin with underscore
parent
a5e447b6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
scapy/automaton.py
+38
-37
38 additions, 37 deletions
scapy/automaton.py
with
38 additions
and
37 deletions
scapy/automaton.py
+
38
−
37
View file @
135d5a3e
...
@@ -348,6 +348,26 @@ class Automaton:
...
@@ -348,6 +348,26 @@ class Automaton:
if
self
.
debug_level
>=
lvl
:
if
self
.
debug_level
>=
lvl
:
log_interactive
.
debug
(
msg
)
log_interactive
.
debug
(
msg
)
def
send
(
self
,
pkt
):
if
self
.
state
.
state
in
self
.
interception_points
:
self
.
debug
(
3
,
"
INTERCEPT: packet intercepted: %s
"
%
pkt
.
summary
())
cmd
=
Message
(
type
=
_ATMT_Command
.
INTERCEPT
,
pkt
=
pkt
)
self
.
cmdout
.
send
(
cmd
)
cmd
=
self
.
cmdin
.
recv
()
if
cmd
.
type
==
_ATMT_Command
.
REJECT
:
self
.
debug
(
3
,
"
INTERCEPT: packet rejected
"
)
return
elif
cmd
.
type
==
_ATMT_Command
.
REPLACE
:
self
.
debug
(
3
,
"
INTERCEPT: packet replaced
"
)
pkt
=
cmd
.
pkt
elif
cmd
.
type
==
_ATMT_Command
.
ACCEPT
:
self
.
debug
(
3
,
"
INTERCEPT: packet accepted
"
)
else
:
self
.
debug
(
1
,
"
INTERCEPT: unkown verdict: %r
"
%
cmd
.
type
)
self
.
my_send
(
pkt
)
self
.
debug
(
3
,
"
SENT : %s
"
%
pkt
.
summary
())
self
.
packets
.
append
(
pkt
.
copy
())
## Internals
## Internals
def
__init__
(
self
,
external_fd
=
{},
*
args
,
**
kargs
):
def
__init__
(
self
,
external_fd
=
{},
*
args
,
**
kargs
):
...
@@ -386,17 +406,18 @@ class Automaton:
...
@@ -386,17 +406,18 @@ class Automaton:
setattr
(
self
.
io
,
n
,
self
.
_IO_mixer
(
ioout
,
ioin
))
setattr
(
self
.
io
,
n
,
self
.
_IO_mixer
(
ioout
,
ioin
))
setattr
(
self
.
oi
,
n
,
self
.
_IO_mixer
(
ioin
,
ioout
))
setattr
(
self
.
oi
,
n
,
self
.
_IO_mixer
(
ioin
,
ioout
))
for
stname
in
self
.
states
:
for
stname
in
self
.
states
:
setattr
(
self
,
stname
,
setattr
(
self
,
stname
,
instance_state
(
getattr
(
self
,
stname
)))
instance_state
(
getattr
(
self
,
stname
)))
self
.
parse_args
(
*
args
,
**
kargs
)
self
.
parse_args
(
*
args
,
**
kargs
)
self
.
start
()
self
.
start
()
def
run_condition
(
self
,
cond
,
*
args
,
**
kargs
):
def
__iter__
(
self
):
return
self
def
_run_condition
(
self
,
cond
,
*
args
,
**
kargs
):
try
:
try
:
cond
(
self
,
*
args
,
**
kargs
)
cond
(
self
,
*
args
,
**
kargs
)
except
ATMT
.
NewStateRequested
,
state_req
:
except
ATMT
.
NewStateRequested
,
state_req
:
...
@@ -410,10 +431,7 @@ class Automaton:
...
@@ -410,10 +431,7 @@ class Automaton:
else
:
else
:
self
.
debug
(
2
,
"
%s [%s] not taken
"
%
(
cond
.
atmt_type
,
cond
.
atmt_condname
))
self
.
debug
(
2
,
"
%s [%s] not taken
"
%
(
cond
.
atmt_type
,
cond
.
atmt_condname
))
def
__iter__
(
self
):
def
_do_control
(
self
,
*
args
,
**
kargs
):
return
self
def
do_control
(
self
,
*
args
,
**
kargs
):
with
self
.
running
:
with
self
.
running
:
self
.
threadid
=
thread
.
get_ident
()
self
.
threadid
=
thread
.
get_ident
()
...
@@ -443,7 +461,7 @@ class Automaton:
...
@@ -443,7 +461,7 @@ class Automaton:
break
break
while
True
:
while
True
:
try
:
try
:
state
=
self
.
do_next
()
state
=
self
.
_
do_next
()
except
self
.
CommandMessage
:
except
self
.
CommandMessage
:
break
break
if
singlestep
:
if
singlestep
:
...
@@ -460,8 +478,7 @@ class Automaton:
...
@@ -460,8 +478,7 @@ class Automaton:
self
.
debug
(
3
,
"
Stopping control thread (tid=%i)
"
%
self
.
threadid
)
self
.
debug
(
3
,
"
Stopping control thread (tid=%i)
"
%
self
.
threadid
)
self
.
threadid
=
None
self
.
threadid
=
None
def
_do_next
(
self
):
def
do_next
(
self
):
try
:
try
:
self
.
debug
(
1
,
"
## state=[%s]
"
%
self
.
state
.
state
)
self
.
debug
(
1
,
"
## state=[%s]
"
%
self
.
state
.
state
)
...
@@ -484,7 +501,7 @@ class Automaton:
...
@@ -484,7 +501,7 @@ class Automaton:
# Then check immediate conditions
# Then check immediate conditions
for
cond
in
self
.
conditions
[
self
.
state
.
state
]:
for
cond
in
self
.
conditions
[
self
.
state
.
state
]:
self
.
run_condition
(
cond
,
*
state_output
)
self
.
_
run_condition
(
cond
,
*
state_output
)
# If still there and no conditions left, we are stuck!
# If still there and no conditions left, we are stuck!
if
(
len
(
self
.
recv_conditions
[
self
.
state
.
state
])
==
0
and
if
(
len
(
self
.
recv_conditions
[
self
.
state
.
state
])
==
0
and
...
@@ -506,7 +523,7 @@ class Automaton:
...
@@ -506,7 +523,7 @@ class Automaton:
t
=
time
.
time
()
-
t0
t
=
time
.
time
()
-
t0
if
next_timeout
is
not
None
:
if
next_timeout
is
not
None
:
if
next_timeout
<=
t
:
if
next_timeout
<=
t
:
self
.
run_condition
(
timeout_func
,
*
state_output
)
self
.
_
run_condition
(
timeout_func
,
*
state_output
)
next_timeout
,
timeout_func
=
expirations
.
next
()
next_timeout
,
timeout_func
=
expirations
.
next
()
if
next_timeout
is
None
:
if
next_timeout
is
None
:
remain
=
None
remain
=
None
...
@@ -524,14 +541,14 @@ class Automaton:
...
@@ -524,14 +541,14 @@ class Automaton:
if
self
.
master_filter
(
pkt
):
if
self
.
master_filter
(
pkt
):
self
.
debug
(
3
,
"
RECVD: %s
"
%
pkt
.
summary
())
self
.
debug
(
3
,
"
RECVD: %s
"
%
pkt
.
summary
())
for
rcvcond
in
self
.
recv_conditions
[
self
.
state
.
state
]:
for
rcvcond
in
self
.
recv_conditions
[
self
.
state
.
state
]:
self
.
run_condition
(
rcvcond
,
pkt
,
*
state_output
)
self
.
_
run_condition
(
rcvcond
,
pkt
,
*
state_output
)
else
:
else
:
self
.
debug
(
4
,
"
FILTR: %s
"
%
pkt
.
summary
())
self
.
debug
(
4
,
"
FILTR: %s
"
%
pkt
.
summary
())
else
:
else
:
self
.
debug
(
3
,
"
IOEVENT on %s
"
%
fd
.
ioname
)
self
.
debug
(
3
,
"
IOEVENT on %s
"
%
fd
.
ioname
)
for
ioevt
in
self
.
ioevents
[
self
.
state
.
state
]:
for
ioevt
in
self
.
ioevents
[
self
.
state
.
state
]:
if
ioevt
.
atmt_ioname
==
fd
.
ioname
:
if
ioevt
.
atmt_ioname
==
fd
.
ioname
:
self
.
run_condition
(
ioevt
,
fd
,
*
state_output
)
self
.
_
run_condition
(
ioevt
,
fd
,
*
state_output
)
except
ATMT
.
NewStateRequested
,
state_req
:
except
ATMT
.
NewStateRequested
,
state_req
:
self
.
debug
(
2
,
"
switching from [%s] to [%s]
"
%
(
self
.
state
.
state
,
state_req
.
state
))
self
.
debug
(
2
,
"
switching from [%s] to [%s]
"
%
(
self
.
state
.
state
,
state_req
.
state
))
...
@@ -539,7 +556,6 @@ class Automaton:
...
@@ -539,7 +556,6 @@ class Automaton:
return
state_req
return
state_req
## Public API
## Public API
def
add_interception_points
(
self
,
*
ipts
):
def
add_interception_points
(
self
,
*
ipts
):
for
ipt
in
ipts
:
for
ipt
in
ipts
:
...
@@ -568,7 +584,7 @@ class Automaton:
...
@@ -568,7 +584,7 @@ class Automaton:
self
.
breakpoints
.
remove
(
pb
)
self
.
breakpoints
.
remove
(
pb
)
def
start
(
self
,
*
args
,
**
kargs
):
def
start
(
self
,
*
args
,
**
kargs
):
thread
.
start_new_thread
(
self
.
do_control
,
args
,
kargs
)
thread
.
start_new_thread
(
self
.
_
do_control
,
args
,
kargs
)
def
run
(
self
,
resume
=
None
,
wait
=
True
):
def
run
(
self
,
resume
=
None
,
wait
=
True
):
if
resume
is
None
:
if
resume
is
None
:
...
@@ -589,6 +605,9 @@ class Automaton:
...
@@ -589,6 +605,9 @@ class Automaton:
elif
c
.
type
==
_ATMT_Command
.
EXCEPTION
:
elif
c
.
type
==
_ATMT_Command
.
EXCEPTION
:
raise
c
.
exception
raise
c
.
exception
def
runbg
(
self
,
resume
=
None
,
wait
=
False
):
self
.
run
(
resume
,
wait
)
def
next
(
self
):
def
next
(
self
):
return
self
.
run
(
resume
=
Message
(
type
=
_ATMT_Command
.
NEXT
))
return
self
.
run
(
resume
=
Message
(
type
=
_ATMT_Command
.
NEXT
))
...
@@ -607,28 +626,10 @@ class Automaton:
...
@@ -607,28 +626,10 @@ class Automaton:
rsm
.
type
=
_ATMT_Command
.
REPLACE
rsm
.
type
=
_ATMT_Command
.
REPLACE
rsm
.
pkt
=
pkt
rsm
.
pkt
=
pkt
return
self
.
run
(
resume
=
rsm
)
return
self
.
run
(
resume
=
rsm
)
def
reject_packet
(
self
):
def
reject_packet
(
self
):
rsm
=
Message
(
type
=
_ATMT_Command
.
REJECT
)
rsm
=
Message
(
type
=
_ATMT_Command
.
REJECT
)
return
self
.
run
(
resume
=
rsm
)
return
self
.
run
(
resume
=
rsm
)
def
send
(
self
,
pkt
):
if
self
.
state
.
state
in
self
.
interception_points
:
self
.
debug
(
3
,
"
INTERCEPT: packet intercepted: %s
"
%
pkt
.
summary
())
cmd
=
Message
(
type
=
_ATMT_Command
.
INTERCEPT
,
pkt
=
pkt
)
self
.
cmdout
.
send
(
cmd
)
cmd
=
self
.
cmdin
.
recv
()
if
cmd
.
type
==
_ATMT_Command
.
REJECT
:
self
.
debug
(
3
,
"
INTERCEPT: packet rejected
"
)
return
elif
cmd
.
type
==
_ATMT_Command
.
REPLACE
:
self
.
debug
(
3
,
"
INTERCEPT: packet replaced
"
)
pkt
=
cmd
.
pkt
elif
cmd
.
type
==
_ATMT_Command
.
ACCEPT
:
self
.
debug
(
3
,
"
INTERCEPT: packet accepted
"
)
else
:
self
.
debug
(
1
,
"
INTERCEPT: unkown verdict: %r
"
%
cmd
.
type
)
self
.
my_send
(
pkt
)
self
.
debug
(
3
,
"
SENT : %s
"
%
pkt
.
summary
())
self
.
packets
.
append
(
pkt
.
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