Skip to content

Commit

Permalink
Merge pull request #28 from JarryShaw/test/rc/protocols
Browse files Browse the repository at this point in the history
New distribution [0.14.2]

* bugfix in exceptions & TCP reassembly algo.
* moved venv in project
  • Loading branch information
JarryShaw authored Mar 28, 2019
2 parents d22e3ca + 0daa432 commit 9750a05
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.PHONY: clean const date dist release pipenv pypi update

export PIPENV_VENV_IN_PROJECT=1

SHELL := /usr/local/bin/bash
DIR ?= .

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# basic info
FROM library/ubuntu
LABEL version 2019.03.07
LABEL version 2019.03.28
LABEL description "Ubuntu Environment"

# prepare environment
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from distutils.core import setup

# version string
__version__ = '0.14.1'
__version__ = '0.14.2'

# README
with open('README.md', encoding='utf-8') as file:
Expand Down
114 changes: 57 additions & 57 deletions src/reassembly/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
for further handling. Otherwise, return.
"""
import copy
import io
import sys

Expand Down Expand Up @@ -250,68 +249,69 @@ def reassembly(self, info):
self._buffer[BUFID] = {
'hdl': [Info(first=info.len, last=sys.maxsize)],
ACK: dict(
ind=list(),
ind=[info.num],
isn=info.dsn,
len=info.len,
raw=info.payload,
),
}

# initialise buffer with ACK
if ACK not in self._buffer[BUFID]:
self._buffer[BUFID][ACK] = dict(
ind=list(),
isn=info.dsn,
len=info.len,
raw=info.payload,
)

# append packet index
self._buffer[BUFID][ACK]['ind'].append(info.num)

# record fragment payload
ISN = self._buffer[BUFID][ACK]['isn'] # Initial Sequence Number
RAW = self._buffer[BUFID][ACK]['raw'] # Raw Payload Data
if DSN >= ISN: # if fragment goes after existing payload
LEN = self._buffer[BUFID][ACK]['len']
GAP = DSN - (ISN + LEN) # gap length between payloads
if GAP >= 0: # if fragment goes after existing payload
RAW += bytearray(GAP) + info.payload
else: # if fragment partially overlaps existing payload
RAW[DSN-ISN:] = info.payload
else: # if fragment exceeds existing payload
LEN = info.len
GAP = ISN - (DSN + LEN) # gap length between payloads
self._buffer[BUFID][ACK]['isn'] = DSN
if GAP >= 0: # if fragment exceeds existing payload
RAW = info.payload + bytearray(GAP) + RAW
else: # if fragment partially overlaps existing payload
RAW = info.payload + RAW[ISN-GAP:]
self._buffer[BUFID][ACK]['raw'] = RAW # update payload datagram
self._buffer[BUFID][ACK]['len'] = len(RAW) # update payload length

# update hole descriptor list
HDL = copy.deepcopy(self._buffer[BUFID]['hdl'])
for (index, hole) in enumerate(self._buffer[BUFID]['hdl']): # step one
if info.first > hole.last: # step two
continue
if info.last < hole.first: # step three
continue
del HDL[index] # step four
if info.first > hole.first: # step five
new_hole = Info(
first=hole.first,
last=info.first - 1,
)
HDL.insert(index, new_hole)
if info.last < hole.last and not FIN and not RST: # step six
new_hole = Info(
first=info.last + 1,
last=hole.last
else:
# initialise buffer with ACK
if ACK not in self._buffer[BUFID]:
self._buffer[BUFID][ACK] = dict(
ind=[info.num],
isn=info.dsn,
len=info.len,
raw=info.payload,
)
HDL.insert(index+1, new_hole)
break # step seven
self._buffer[BUFID]['hdl'] = HDL # update HDL
else:
# append packet index
self._buffer[BUFID][ACK]['ind'].append(info.num)

# record fragment payload
ISN = self._buffer[BUFID][ACK]['isn'] # Initial Sequence Number
RAW = self._buffer[BUFID][ACK]['raw'] # Raw Payload Data
if DSN >= ISN: # if fragment goes after existing payload
LEN = self._buffer[BUFID][ACK]['len']
GAP = DSN - (ISN + LEN) # gap length between payloads
if GAP >= 0: # if fragment goes after existing payload
RAW += bytearray(GAP) + info.payload
else: # if fragment partially overlaps existing payload
RAW[DSN-ISN:] = info.payload
else: # if fragment exceeds existing payload
LEN = info.len
GAP = ISN - (DSN + LEN) # gap length between payloads
self._buffer[BUFID][ACK]['isn'] = DSN
if GAP >= 0: # if fragment exceeds existing payload
RAW = info.payload + bytearray(GAP) + RAW
else: # if fragment partially overlaps existing payload
RAW = info.payload + RAW[ISN-GAP:]
self._buffer[BUFID][ACK]['raw'] = RAW # update payload datagram
self._buffer[BUFID][ACK]['len'] = len(RAW) # update payload length

# update hole descriptor list
HDL = self._buffer[BUFID]['hdl']
for (index, hole) in enumerate(HDL): # step one
if info.first > hole.last: # step two
continue
if info.last < hole.first: # step three
continue
del HDL[index] # step four
if info.first > hole.first: # step five
new_hole = Info(
first=hole.first,
last=info.first - 1,
)
HDL.insert(index, new_hole)
index += 1
if info.last < hole.last and not FIN and not RST: # step six
new_hole = Info(
first=info.last + 1,
last=hole.last
)
HDL.insert(index, new_hole)
break # step seven
self._buffer[BUFID]['hdl'] = HDL # update HDL

# when FIN/RST is set, submit buffer of this session
if FIN or RST:
Expand Down
3 changes: 1 addition & 2 deletions src/utilities/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ class BaseError(Exception):
* In Python 2.7, `trace.print_stack(limit=None)` dose not support negative limit.
"""
def __init__(self, *args, **kwargs):
def __init__(self, *args, quiet=False, **kwargs):
if DEVMODE:
index = stacklevel()
quiet = kwargs.pop('quiet', False)
if not quiet and index:
fmt_exc = traceback.format_exc(limit=-index)
if len(fmt_exc.splitlines(True)) > 1:
Expand Down

0 comments on commit 9750a05

Please sign in to comment.