/usr/share/pyshared/d_rats/ddt2.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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | #!/usr/bin/python
#
# Copyright 2008 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
import zlib
import base64
import yencode
import threading
import utils
ENCODED_HEADER = "[SOB]"
ENCODED_TRAILER = "[EOB]"
def update_crc(c, crc):
for _ in range(0,8):
c <<= 1
if (c & 0400) != 0:
v = 1
else:
v = 0
if (crc & 0x8000):
crc <<= 1
crc += v
crc ^= 0x1021
else:
crc <<= 1
crc += v
return crc & 0xFFFF
def calc_checksum(data):
checksum = 0
for i in data:
checksum = update_crc(ord(i), checksum)
checksum = update_crc(0, checksum)
checksum = update_crc(0, checksum)
return checksum
def encode(data):
return yencode.yencode_buffer(data)
def decode(data):
return yencode.ydecode_buffer(data)
class DDT2Frame(object):
format = "!BHBBHH8s8s"
cso = 6
csl = 2
def __init__(self):
self.seq = 0
self.session = 0
self.type = 0
self.d_station = ""
self.s_station = ""
self.data = ""
self.magic = 0xDD
self.sent_event = threading.Event()
self.ackd_event = threading.Event()
self.compress = True
self._xmit_s = 0
self._xmit_e = 0
self._xmit_z = 0
def get_xmit_bps(self):
if not self._xmit_e:
print "Block not sent, can't determine BPS!"
return 0
if self._xmit_s == self._xmit_e:
return self._xmit_z * 100 # Fudge for sockets
return self._xmit_z / (self._xmit_e - self._xmit_s)
def set_compress(self, compress=True):
self.compress = compress
def get_packed(self):
if self.compress:
data = zlib.compress(self.data, 9)
else:
data = self.data
self.magic = (~self.magic) & 0xFF
length = len(data)
s_station = self.s_station.ljust(8, "~")
d_station = self.d_station.ljust(8, "~")
val = struct.pack(self.format,
self.magic,
self.seq,
self.session,
self.type,
0,
length,
s_station,
d_station)
checksum = calc_checksum(val + data)
val = struct.pack(self.format,
self.magic,
self.seq,
self.session,
self.type,
checksum,
length,
s_station,
d_station)
self._xmit_z = len(val) + len(data)
return val + data
def unpack(self, val):
magic = ord(val[0])
if magic == 0xDD:
self.compress = True
elif magic == 0x22:
self.compress = False
else:
print "Magic 0x%X not recognized" % magic
return False
header = val[:25]
data = val[25:]
(magic, self.seq, self.session, self.type,
checksum, length,
self.s_station, self.d_station) = struct.unpack(self.format, header)
_header = struct.pack(self.format,
magic,
self.seq,
self.session,
self.type,
0,
length,
self.s_station,
self.d_station)
_checksum = calc_checksum(_header + data)
self.s_station = self.s_station.replace("~", "")
self.d_station = self.d_station.replace("~", "")
if _checksum != checksum:
print "Checksum failed: %s != %s" % (checksum, _checksum)
return False
if self.compress:
self.data = zlib.decompress(data)
else:
self.data = data
return True
def __str__(self):
if self.compress:
c = "+"
else:
c = "-"
data = utils.filter_to_ascii(self.data[:20])
return "DDT2%s: %i:%i:%i %s->%s (%s...[%i])" % (c,
self.seq,
self.session,
self.type,
self.s_station,
self.d_station,
data,
len(self.data))
def get_copy(self):
f = self.__class__()
f.seq = self.seq
f.session = self.session
f.type = self.type
f.s_station = self.s_station
f.d_station = self.d_station
f.data = self.data
f.set_compress(self.compress)
return f
class DDT2EncodedFrame(DDT2Frame):
def get_packed(self):
raw = DDT2Frame.get_packed(self)
encoded = encode(raw)
return ENCODED_HEADER + encoded + ENCODED_TRAILER
def unpack(self, val):
try:
h = val.index(ENCODED_HEADER) + len(ENCODED_TRAILER)
t = val.rindex(ENCODED_TRAILER)
payload = val[h:t]
except Exception, e:
print "Block has no header/trailer: %s" % e
return False
try:
decoded = decode(payload)
except Exception, e:
print "Unable to decode frame: %s" % e
return False
return DDT2Frame.unpack(self, decoded)
class DDT2RawData(DDT2Frame):
def get_packed(self):
return self.data
def unpack(self, string):
return self.data
def test_symmetric(compress=True):
fin = DDT2EncodedFrame()
fin.type = 1
fin.session = 2
fin.seq = 3
fin.s_station = "FOO"
fin.d_station = "BAR"
fin.data = "This is a test"
fin.set_compress(compress)
p = fin.get_packed()
print p
fout = DDT2EncodedFrame()
fout.unpack(p)
#print fout.__dict__
print fout
def test_crap():
f = DDT2EncodedFrame()
try:
if f.unpack("[SOB]foobar[EOB]"):
print "FAIL"
else:
print "PASS"
except Exception, e:
print "PASS"
if __name__ == "__main__":
test_symmetric()
test_symmetric(False)
test_crap()
|