summaryrefslogtreecommitdiff
path: root/common/lib/scammer/commonfields.py
diff options
context:
space:
mode:
Diffstat (limited to 'common/lib/scammer/commonfields.py')
-rw-r--r--common/lib/scammer/commonfields.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/common/lib/scammer/commonfields.py b/common/lib/scammer/commonfields.py
new file mode 100644
index 0000000000..82f4eaad14
--- /dev/null
+++ b/common/lib/scammer/commonfields.py
@@ -0,0 +1,107 @@
+#!/usr/bin/python
+
+#############################################################################
+# Copyright (C) 2011 Spidcom
+#############################################################################
+
+from scapy.fields import *
+
+class XLEShortField (LEShortField):
+ """This class is used to represent LEShortField (scapy) in Hexadecimal
+ representation, this functionality should exist in the future version of
+ scapy called 'XLEShortField' """
+ def i2repr(self, pkt, x):
+ if x is None:
+ x = 0
+ return lhex(self.i2h(pkt, x))
+
+class LEBitField (BitField):
+ def __init__(self, name, default, size):
+ BitField.__init__(self, name, default, size)
+
+ def addfield (self, pkt, s, val):
+ val = self.i2m(pkt, val)
+ if type(s) is tuple:
+ s,bitsdone,v = s
+ else:
+ bitsdone = 0
+ v = 0
+ if self.rev:
+ val = self.reverse(val)
+ v |= val << bitsdone
+ bitsdone += self.size
+ while bitsdone >= 8:
+ bitsdone -= 8
+ s = s+struct.pack("B", v >> bitsdone)
+ v &= (1L<<bitsdone)-1
+ if bitsdone:
+ return s,bitsdone,v
+ else:
+ return s
+
+ def getfield(self, pkt, s):
+ if type(s) is tuple:
+ s,bn = s
+ else:
+ bn = 0
+ # we don't want to process all the string
+ nb_bytes = (self.size+bn-1)/8 + 1
+ w = s[:nb_bytes]
+
+ # split the substring byte by byte
+ bytes = struct.unpack('%dB' % nb_bytes , w)
+
+ b = 0L
+ for c in range(nb_bytes):
+ b |= long(bytes[nb_bytes - 1 - c]) << (nb_bytes-c-1)*8
+
+ # Decal of bn.
+ b = b >> bn
+ # get rid of high order bits
+ b &= (1L << (self.size)) - 1
+
+ if self.rev:
+ b = self.reverse(b)
+
+ bn += self.size
+ s = s[bn/8:]
+ if bn % 8 != 0:
+ sbyte = struct.unpack ('B', s[0])[0]
+ decal_bits = (8 - (nb_bytes * 8 - self.size))
+ sbyte &= ~((1 << decal_bits) - 1)
+ sbyte = struct.pack ('B', sbyte)
+ s = sbyte + s[1:]
+
+ bn = bn%8
+ b = self.m2i(pkt, b)
+ if bn:
+ return (s,bn),b
+ else:
+ return s,b
+
+class XLEBitField (LEBitField):
+ """This class is used to represent LEShortField (scapy) in Hexadecimal
+ representation, this functionality should exist in the future version of
+ scapy called 'XLEShortField' """
+ def i2repr(self, pkt, x):
+ if x is None:
+ x = 0
+ return lhex(self.i2h(pkt, x))
+
+class HPAVNIDField (Field):
+ """Special Field for NID HPAV field."""
+ def __init__(self, name, default):
+ Field.__init__(self, name, default, "7s")
+
+ def addfield (self, pkt, s, val):
+ nb_bytes = 7
+ w = struct.pack ('Q', val)
+ w = w[:nb_bytes]
+ return s + w
+
+ def getfield (self, pkt, s):
+ nb_bytes = 7
+ w = "%s\0" % s[0:nb_bytes]
+ w = struct.unpack ('Q', w)[0]
+ s = s[nb_bytes:]
+ return s,w