# Path: pyisis/lib/util.py
# def debug_exception ():
#     if debug_exception.entered:
#         return
#     debug_exception.entered = True
#     traceback.print_exc()
#     pdb.set_trace()
# 
# Path: pyisis/lib/util.py
# def stringify3 (b):
#     if type(b) == memoryview:
#         return b.tobytes()
#     else:
#         return str(buffer(b))                           # pylint: disable=E0602
# 
# def tlvrdb (val):
#     # In python3 byte strings return integers when indexed
#     return val
# 
# Path: pyisis/lib/cksum.py
# def iso_cksum (mess, ckoff=None):
#     # RFC1008 calls this k and points to the 2nd byte (calling it the first)
#     # so ckoff = k -1, our ckoff points at the actual first byte of the chksum
#     # or None if not required.
# 
#     p3 = len(mess)
# 
#     # We can't modify the data so we look for it later while iterating
#     # if k > 0:
#     #     mess[k - 1] = 0
#     #     mess[k] = 0
#     c0 = 0
#     c1 = 0
#     p1 = 0
# 
#     # Outer sum accumulation loop
#     while p1 < p3:
#         p2 = p1 + MODX
#         if p2 > p3:
#             p2 = p3
# 
#         # Inner sum accumulation loop
#         for p in xrange3(p1, p2):
#             # if these are the cksum bytes skip addition (treat as zero)
#             if not ckoff or (p != ckoff and p != (ckoff + 1)):
#                 c0 = c0 + ord3(mess[p])
#             c1 = c1 + c0
# 
#         # Adjust accumulated sums to mod 255
#         c0 = c0 % 255
#         c1 = c1 % 255
#         p1 = p2
# 
#     # concatenate c1 and c0
#     ip = ((c1 & 0xFF) << 8) + (c0 & 0xFF)
#     if ckoff is None:
#         return ip
# 
#     # iq = ((mlen - k) * c0 - c1) % 255
#     iq = ((p3 - (ckoff + 1)) * c0 - c1) % 255
#     if iq <= 0:
#         iq = iq + 255
#     # mess[ckoff] = chr(iq)     # Can't modify data
# 
#     ir = (510 - c0 - iq)
#     if ir > 255:
#         ir = ir - 255
#     # mess[ckoff + 1] = chr(ir)         # Can't modify data
# 
#     # Return in host order
#     return ((iq & 0xFF) << 8) | (ir & 0xFF)


from pyisis.lib.util import debug_exception
from ctypes import sizeof
from pyisis.lib.util import stringify3, tlvrdb
from pyisis.lib.cksum import iso_cksum
import logbook
import threading
import pyisis.clns as clns
import pyisis.lsp as lsp
import pyisis.pdu as pdu
import pyisis.lib.timers as timers
import pyisis.tlv as tlv
import pyisis.lib.util as util
                import pdb
        assert frame.seqno                                  # XXX deal with rollover
        frame.checksum = 0
        # XXX what if we are purging our own?
        if not force or frame.lifetime != 0:
            frame.lifetime = 30 # lsp.MAX_AGE
        frame.checksum = iso_cksum(pdubuf[ckoff:], 12)

        if dblsp:
            fstr = "force " if force else ""
            logger.info("{}: {}updating own LSP segment: {} from: {}", self, fstr, dblsp, frame)
            dblsp.update(pdubuf, tlvs)
        else:
            if frame.lifetime == 0:
                # 17.3.16.4: a
                # XXX send ack on link do not retain
                logger.info("{}: (XXX not impl.) acking own LSP segment: from: {}", self, frame)
                return
            logger.info("{}: adding own LSP segment: from: {}", self, frame)
            dblsp = lsp.LSPSegment(self.inst, self.lindex, pdubuf, tlvs)
            self.dbhash[lspid] = dblsp
        self.inst.linkdb.set_all_srm(dblsp)

    def receive_lsp (self, link, unused_pkt, pdubuf, frame, tlvs):        # pylint: disable=R0912,R0914,R0915
        if len(pdubuf) > clns.receiveLSPBufferSize():
            # ISO 7.3.14.2 - Treat as invalid checksum
            logger.info("TRAP corruptedLSPReceived: {} dropping", link)
            return

        lspbuf = pdubuf[sizeof(pdu.CLNSHeader):]
        if frame.lifetime:

# Path: pyisis/lib/util.py
# def debug_exception ():
#     if debug_exception.entered:
#         return
#     debug_exception.entered = True
#     traceback.print_exc()
#     pdb.set_trace()
# 
# Path: pyisis/lib/util.py
# def stringify3 (b):
#     if type(b) == memoryview:
#         return b.tobytes()
#     else:
#         return str(buffer(b))                           # pylint: disable=E0602
# 
# def tlvrdb (val):
#     # In python3 byte strings return integers when indexed
#     return val
# 
# Path: pyisis/lib/cksum.py
# def iso_cksum (mess, ckoff=None):
#     # RFC1008 calls this k and points to the 2nd byte (calling it the first)
#     # so ckoff = k -1, our ckoff points at the actual first byte of the chksum
#     # or None if not required.
# 
#     p3 = len(mess)
# 
#     # We can't modify the data so we look for it later while iterating
#     # if k > 0:
#     #     mess[k - 1] = 0
#     #     mess[k] = 0
#     c0 = 0
#     c1 = 0
#     p1 = 0
# 
#     # Outer sum accumulation loop
#     while p1 < p3:
#         p2 = p1 + MODX
#         if p2 > p3:
#             p2 = p3
# 
#         # Inner sum accumulation loop
#         for p in xrange3(p1, p2):
#             # if these are the cksum bytes skip addition (treat as zero)
#             if not ckoff or (p != ckoff and p != (ckoff + 1)):
#                 c0 = c0 + ord3(mess[p])
#             c1 = c1 + c0
# 
#         # Adjust accumulated sums to mod 255
#         c0 = c0 % 255
#         c1 = c1 % 255
#         p1 = p2
# 
#     # concatenate c1 and c0
#     ip = ((c1 & 0xFF) << 8) + (c0 & 0xFF)
#     if ckoff is None:
#         return ip
# 
#     # iq = ((mlen - k) * c0 - c1) % 255
#     iq = ((p3 - (ckoff + 1)) * c0 - c1) % 255
#     if iq <= 0:
#         iq = iq + 255
#     # mess[ckoff] = chr(iq)     # Can't modify data
# 
#     ir = (510 - c0 - iq)
#     if ir > 255:
#         ir = ir - 255
#     # mess[ckoff + 1] = chr(ir)         # Can't modify data
# 
#     # Return in host order
#     return ((iq & 0xFF) << 8) | (ir & 0xFF)


# Path: pyisis/update.py
from pyisis.lib.util import debug_exception
from ctypes import sizeof
from pyisis.lib.util import stringify3, tlvrdb
from pyisis.lib.cksum import iso_cksum
import logbook
import threading
import pyisis.clns as clns
import pyisis.lsp as lsp
import pyisis.pdu as pdu
import pyisis.lib.timers as timers
import pyisis.tlv as tlv
import pyisis.lib.util as util
                import pdb

        assert frame.seqno                                  # XXX deal with rollover
        frame.checksum = 0
        # XXX what if we are purging our own?
        if not force or frame.lifetime != 0:
            frame.lifetime = 30 # lsp.MAX_AGE
        frame.checksum = iso_cksum(pdubuf[ckoff:], 12)

        if dblsp:
            fstr = "force " if force else ""
            logger.info("{}: {}updating own LSP segment: {} from: {}", self, fstr, dblsp, frame)
            dblsp.update(pdubuf, tlvs)
        else:
            if frame.lifetime == 0:
                # 17.3.16.4: a
                # XXX send ack on link do not retain
                logger.info("{}: (XXX not impl.) acking own LSP segment: from: {}", self, frame)
                return
            logger.info("{}: adding own LSP segment: from: {}", self, frame)
            dblsp = lsp.LSPSegment(self.inst, self.lindex, pdubuf, tlvs)
            self.dbhash[lspid] = dblsp
        self.inst.linkdb.set_all_srm(dblsp)

    def receive_lsp (self, link, unused_pkt, pdubuf, frame, tlvs):        # pylint: disable=R0912,R0914,R0915
        if len(pdubuf) > clns.receiveLSPBufferSize():
            # ISO 7.3.14.2 - Treat as invalid checksum
            logger.info("TRAP corruptedLSPReceived: {} dropping", link)
            return

        lspbuf = pdubuf[sizeof(pdu.CLNSHeader):]
        if frame.lifetime:

            cksum = iso_cksum(lspbuf[4:])