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
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
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide 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
...
...
@@ -346,8 +346,28 @@ class Automaton:
## Services
def
debug
(
self
,
lvl
,
msg
):
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
def
__init__
(
self
,
external_fd
=
{},
*
args
,
**
kargs
):
...
...
@@ -386,17 +406,18 @@ class Automaton:
setattr
(
self
.
io
,
n
,
self
.
_IO_mixer
(
ioout
,
ioin
))
setattr
(
self
.
oi
,
n
,
self
.
_IO_mixer
(
ioin
,
ioout
))
for
stname
in
self
.
states
:
setattr
(
self
,
stname
,
instance_state
(
getattr
(
self
,
stname
)))
self
.
parse_args
(
*
args
,
**
kargs
)
self
.
start
()
def
run_condition
(
self
,
cond
,
*
args
,
**
kargs
):
def
__iter__
(
self
):
return
self
def
_run_condition
(
self
,
cond
,
*
args
,
**
kargs
):
try
:
cond
(
self
,
*
args
,
**
kargs
)
except
ATMT
.
NewStateRequested
,
state_req
:
...
...
@@ -410,10 +431,7 @@ class Automaton:
else
:
self
.
debug
(
2
,
"
%s [%s] not taken
"
%
(
cond
.
atmt_type
,
cond
.
atmt_condname
))
def
__iter__
(
self
):
return
self
def
do_control
(
self
,
*
args
,
**
kargs
):
def
_do_control
(
self
,
*
args
,
**
kargs
):
with
self
.
running
:
self
.
threadid
=
thread
.
get_ident
()
...
...
@@ -443,7 +461,7 @@ class Automaton:
break
while
True
:
try
:
state
=
self
.
do_next
()
state
=
self
.
_
do_next
()
except
self
.
CommandMessage
:
break
if
singlestep
:
...
...
@@ -460,8 +478,7 @@ class Automaton:
self
.
debug
(
3
,
"
Stopping control thread (tid=%i)
"
%
self
.
threadid
)
self
.
threadid
=
None
def
do_next
(
self
):
def
_do_next
(
self
):
try
:
self
.
debug
(
1
,
"
## state=[%s]
"
%
self
.
state
.
state
)
...
...
@@ -484,7 +501,7 @@ class Automaton:
# Then check immediate conditions
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
(
len
(
self
.
recv_conditions
[
self
.
state
.
state
])
==
0
and
...
...
@@ -506,7 +523,7 @@ class Automaton:
t
=
time
.
time
()
-
t0
if
next_timeout
is
not
None
:
if
next_timeout
<=
t
:
self
.
run_condition
(
timeout_func
,
*
state_output
)
self
.
_
run_condition
(
timeout_func
,
*
state_output
)
next_timeout
,
timeout_func
=
expirations
.
next
()
if
next_timeout
is
None
:
remain
=
None
...
...
@@ -524,14 +541,14 @@ class Automaton:
if
self
.
master_filter
(
pkt
):
self
.
debug
(
3
,
"
RECVD: %s
"
%
pkt
.
summary
())
for
rcvcond
in
self
.
recv_conditions
[
self
.
state
.
state
]:
self
.
run_condition
(
rcvcond
,
pkt
,
*
state_output
)
self
.
_
run_condition
(
rcvcond
,
pkt
,
*
state_output
)
else
:
self
.
debug
(
4
,
"
FILTR: %s
"
%
pkt
.
summary
())
else
:
self
.
debug
(
3
,
"
IOEVENT on %s
"
%
fd
.
ioname
)
for
ioevt
in
self
.
ioevents
[
self
.
state
.
state
]:
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
:
self
.
debug
(
2
,
"
switching from [%s] to [%s]
"
%
(
self
.
state
.
state
,
state_req
.
state
))
...
...
@@ -539,7 +556,6 @@ class Automaton:
return
state_req
## Public API
def
add_interception_points
(
self
,
*
ipts
):
for
ipt
in
ipts
:
...
...
@@ -568,7 +584,7 @@ class Automaton:
self
.
breakpoints
.
remove
(
pb
)
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
):
if
resume
is
None
:
...
...
@@ -589,6 +605,9 @@ class Automaton:
elif
c
.
type
==
_ATMT_Command
.
EXCEPTION
:
raise
c
.
exception
def
runbg
(
self
,
resume
=
None
,
wait
=
False
):
self
.
run
(
resume
,
wait
)
def
next
(
self
):
return
self
.
run
(
resume
=
Message
(
type
=
_ATMT_Command
.
NEXT
))
...
...
@@ -607,28 +626,10 @@ class Automaton:
rsm
.
type
=
_ATMT_Command
.
REPLACE
rsm
.
pkt
=
pkt
return
self
.
run
(
resume
=
rsm
)
def
reject_packet
(
self
):
rsm
=
Message
(
type
=
_ATMT_Command
.
REJECT
)
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