/usr/share/pyshared/d_rats/sessions/control.py is in d-rats 0.3.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #!/usr/bin/python
#
# Copyright 2009 Dan Smith <dsmith@danplanet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import struct
from d_rats.utils import log_exception
from d_rats.ddt2 import DDT2EncodedFrame
from d_rats.sessions import base, stateful, stateless
from d_rats.sessions import file, form, sock
T_PNG = 0
T_END = 1
T_ACK = 2
T_NEW = 3
class ControlSession(base.Session):
stateless = True
def ack_req(self, dest, data):
f = DDT2EncodedFrame()
f.type = T_ACK
f.seq = 0
f.d_station = dest
f.data = data
self._sm.outgoing(self, f)
def ctl_ack(self, frame):
try:
l, r = struct.unpack("BB", frame.data)
session = self._sm.sessions[l]
session._rs = r
print "Signaled waiting session thread (l=%i r=%i)" % (l, r)
except Exception, e:
print "Failed to lookup new session event: %s" % e
if session.get_state() == base.ST_CLSW:
session.set_state(base.ST_CLSD)
elif session.get_state() == base.ST_OPEN:
pass
elif session.get_state() == base.ST_SYNC:
session.set_state(base.ST_OPEN)
else:
print "ACK for session in invalid state: %i" % session.get_state()
def ctl_end(self, frame):
print "End of session %s" % frame.data
try:
id = int(frame.data)
except Exception, e:
print "Session end request had invalid ID: %s" % e
return
try:
session = self._sm.sessions[id]
session.set_state(base.ST_CLSD)
self._sm.stop_session(session)
except Exception, e:
print "Session %s ended but not registered" % id
return
frame.d_station = frame.s_station
if session._rs:
frame.data = str(session._rs)
else:
frame.data = str(session._id)
self._sm.outgoing(self, frame)
def ctl_new(self, frame):
try:
(id,) = struct.unpack("B", frame.data[:1])
name = frame.data[1:]
except Exception, e:
print "Session request had invalid ID: %s" % e
return
print "New session %i from remote" % id
exist = self._sm.get_session(rid=id, rst=frame.s_station)
if exist:
print "Re-acking existing session %s:%i:%i" % (frame.s_station,
id,
exist._id)
self.ack_req(frame.s_station, struct.pack("BB", id, exist._id))
return
print "ACK'ing session request for %i" % id
try:
c = self.stypes[frame.type]
print "Got type: %s" % c
s = c(name)
s._rs = id
s.set_state(base.ST_OPEN)
except Exception, e:
log_exception()
print "Can't start session type `%s': %s" % (frame.type, e)
return
num = self._sm._register_session(s, frame.s_station, "new,in")
data = struct.pack("BB", id, num)
self.ack_req(frame.s_station, data)
def ctl(self, frame):
if frame.d_station != self._sm.station:
print "Control ignoring frame for station %s" % frame.d_station
return
if frame.type == T_ACK:
self.ctl_ack(frame)
elif frame.type == T_END:
self.ctl_end(frame)
elif frame.type >= T_NEW:
self.ctl_new(frame)
else:
print "Unknown control message type %i" % frame.type
def new_session(self, session):
f = DDT2EncodedFrame()
f.type = T_NEW + session.type
f.seq = 0
f.d_station = session._st
f.data = struct.pack("B", int(session._id)) + session.name
wait_time = 5
for i in range(0,10):
self._sm.outgoing(self, f)
f.sent_event.wait(10)
f.sent_event.clear()
print "Sent request, blocking..."
session.wait_for_state_change(wait_time)
state = session.get_state()
if state == base.ST_CLSD:
print "Session is closed"
break
elif state == base.ST_SYNC:
print "Waiting for synchronization"
wait_time = 15
else:
print "Established session %i:%i" % (session._id, session._rs)
session.set_state(base.ST_OPEN)
return True
session.set_state(base.ST_CLSD)
print "Failed to establish session"
return False
def end_session(self, session):
if session.stateless:
return
while session.get_state() == base.ST_SYNC:
print "Waiting for session in SYNC"
session.wait_for_state_change(2)
f = DDT2EncodedFrame()
f.type = T_END
f.seq = 0
f.d_station = session._st
if session._rs:
f.data = str(session._rs)
else:
f.data = str(session._id)
session.set_state(base.ST_CLSW)
for i in range(0, 3):
print "Sending End-of-Session"
self._sm.outgoing(self, f)
f.sent_event.wait(10)
f.sent_event.clear()
print "Sent, waiting for response"
session.wait_for_state_change(15)
if session.get_state() == base.ST_CLSD:
print "Session closed"
return True
session.set_state(base.ST_CLSD)
print "Session closed because no response"
return False
def __init__(self):
base.Session.__init__(self, "control")
self.handler = self.ctl
self.stypes = { T_NEW + base.T_GENERAL : stateful.StatefulSession,
T_NEW + base.T_FILEXFER : file.FileTransferSession,
T_NEW + base.T_FORMXFER : form.FormTransferSession,
T_NEW + base.T_SOCKET : sock.SocketSession,
}
|