Skip to content

Commit e5a285f

Browse files
committed
Unify RandString & RandBin
1 parent d8475c6 commit e5a285f

3 files changed

Lines changed: 16 additions & 24 deletions

File tree

scapy/arch/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
SOLARIS, WINDOWS, BSD, IS_64BITS, LOOPBACK_NAME
1515
from scapy.error import *
1616
import scapy.config
17-
from scapy.pton_ntop import inet_pton
17+
from scapy.pton_ntop import inet_pton, inet_ntop
1818
from scapy.data import *
1919

2020
def str2mac(s):
@@ -25,7 +25,7 @@ def str2mac(s):
2525
from scapy.arch.bpf.core import get_if_raw_addr
2626

2727
def get_if_addr(iff):
28-
return socket.inet_ntoa(get_if_raw_addr(iff))
28+
return inet_ntop(socket.AF_INET, get_if_raw_addr(iff))
2929

3030
def get_if_hwaddr(iff):
3131
addrfamily, mac = get_if_raw_hwaddr(iff)

scapy/volatile.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def next(self):
7070
__next__ = next
7171

7272

73-
class VolatileValue:
73+
class VolatileValue(object):
7474
def __repr__(self):
7575
return "<%s>" % self.__class__.__name__
7676
def __eq__(self, other):
@@ -266,41 +266,33 @@ def _fix(self):
266266
return random.choice(self._choice)
267267

268268
class RandString(RandField):
269-
def __init__(self, size=None, chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"):
269+
def __init__(self, size=None, chars=b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"):
270270
if size is None:
271271
size = RandNumExpo(0.01)
272272
self.size = size
273273
self.chars = chars
274274
def _fix(self):
275-
s = ""
275+
s = b""
276276
for _ in range(self.size):
277-
s += random.choice(self.chars)
277+
s += chb(random.choice(self.chars))
278278
return s
279+
def __str__(self):
280+
return plain_str(self._fix())
281+
def __bytes__(self):
282+
return raw(self._fix())
279283
def __mul__(self, n):
280284
return self._fix()*n
281285

282-
class RandBin(RandString, object):
286+
class RandBin(RandString):
283287
def __init__(self, size=None):
284288
super(RandBin, self).__init__(size=size, chars=b"".join(chb(c) for c in range(256)))
285-
def _fix(self):
286-
s = b""
287-
for _ in range(self.size):
288-
s += chb(random.choice(self.chars))
289-
return s
290-
291289

292-
class RandTermString(RandString):
290+
class RandTermString(RandBin):
293291
def __init__(self, size, term):
294-
RandString.__init__(self, size, "".join(map(chr, range(1,256))))
295-
self.term = term
292+
self.term = raw(term)
293+
super(RandTermString, self).__init__(size=size)
296294
def _fix(self):
297-
return RandString._fix(self)+self.term
298-
299-
def __str__(self):
300-
return str(self._fix())
301-
302-
def __bytes__(self):
303-
return raw(self._fix())
295+
return RandBin._fix(self)+self.term
304296

305297

306298
class RandIP(RandString):

test/regression.uts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8899,7 +8899,7 @@ assert(rss == ("CON:" if six.PY2 else "foo.exe:"))
88998899

89008900
random.seed(0x2807)
89018901
rts = RandTermString(4, "scapy")
8902-
assert(sane(raw(rts)) in ["...[scapy", "......scapy"])
8902+
assert(sane(raw(rts)) in ["...Zscapy", "$#..scapy"])
89038903

89048904

89058905
############

0 commit comments

Comments
 (0)